2004-03-20 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / System.Security / System.Security.Cryptography.Xml / XmlSignature.cs
1 //
2 // XmlSignature.cs: Handles Xml Signature
3 //
4 // Author:
5 //      Sebastien Pouliot (spouliot@motus.com)
6 //      Atsushi Enomoto (atsushi@ximian.com)
7 //
8 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
9 // (C) 2004 Novell Inc.
10 //
11
12 using System;
13 using System.Collections;
14 using System.Xml;
15
16 namespace System.Security.Cryptography.Xml {
17
18         // following the design of WSE
19         internal class XmlSignature {
20
21                 public class ElementNames {
22
23                         public const string CanonicalizationMethod = "CanonicalizationMethod";
24                         public const string DigestMethod = "DigestMethod";
25                         public const string DigestValue = "DigestValue";
26                         public const string DSAKeyValue = "DSAKeyValue";
27                         public const string HMACOutputLength = "HMACOutputLength";
28                         public const string KeyInfo = "KeyInfo";
29                         public const string KeyName = "KeyName";
30                         public const string KeyValue = "KeyValue";
31                         public const string Object = "Object";
32                         public const string Reference = "Reference";
33 #if NET_1_0
34                         // RetrievalMethod vs RetrievalElement -> BUG in MS Framework 1.0
35                         public const string RetrievalMethod = "RetrievalElement";
36 #else
37                         public const string RetrievalMethod = "RetrievalMethod";
38 #endif
39                         public const string RSAKeyValue = "RSAKeyValue";
40                         public const string Signature = "Signature";
41                         public const string SignatureMethod = "SignatureMethod";
42                         public const string SignatureValue = "SignatureValue";
43                         public const string SignedInfo = "SignedInfo";
44                         public const string Transform = "Transform";
45                         public const string Transforms = "Transforms";
46                         public const string X509Data = "X509Data";
47                         public const string X509IssuerSerial = "X509IssuerSerial";
48                         public const string X509IssuerName = "X509IssuerName";
49                         public const string X509SerialNumber = "X509SerialNumber";
50                         public const string X509SKI = "X509SKI";
51                         public const string X509SubjectName = "X509SubjectName";
52                         public const string X509Certificate = "X509Certificate";
53                         public const string X509CRL = "X509CRL";
54
55                         public ElementNames () {}
56                 }
57
58                 public class AttributeNames {
59
60                         public const string Algorithm = "Algorithm";
61                         public const string Encoding = "Encoding";
62                         public const string Id = "Id";
63                         public const string MimeType = "MimeType";
64                         public const string Type = "Type";
65                         public const string URI = "URI";
66
67                         public AttributeNames () {}
68                 }
69
70                 public class AlgorithmNamespaces {
71                         public const string XmlDsigBase64Transform = "http://www.w3.org/2000/09/xmldsig#base64";
72                         public const string XmlDsigC14NTransform = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315";
73                         public const string XmlDsigC14NWithCommentsTransform = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments";
74                         public const string XmlDsigEnvelopedSignatureTransform = "http://www.w3.org/2000/09/xmldsig#enveloped-signature";
75                         public const string XmlDsigXPathTransform = "http://www.w3.org/TR/1999/REC-xpath-19991116";
76                         public const string XmlDsigXsltTransform =  "http://www.w3.org/TR/1999/REC-xslt-19991116";
77                 }
78
79                 public const string NamespaceURI = "http://www.w3.org/2000/09/xmldsig#";
80                 public const string Prefix = "ds";
81
82                 public XmlSignature () 
83                 {
84                 }
85
86                 public static XmlElement GetChildElement (XmlElement xel, string element, string ns)
87                 {
88                         for (int i = 0; i < xel.ChildNodes.Count; i++) {
89                                 XmlNode n = xel.ChildNodes [i];
90                                 if (n.NodeType == XmlNodeType.Element && n.LocalName == element && n.NamespaceURI == ns)
91                                         return n as XmlElement;
92                         }
93                         return null;
94                 }
95
96                 public static string GetAttributeFromElement (XmlElement xel, string attribute, string element) 
97                 {
98                         XmlElement el = GetChildElement (xel, element, XmlSignature.NamespaceURI);
99                         return el != null ? el.GetAttribute (attribute) : null;
100                 }
101
102                 public static XmlElement [] GetChildElements (XmlElement xel, string element)
103                 {
104                         ArrayList al = new ArrayList ();
105                         for (int i = 0; i < xel.ChildNodes.Count; i++) {
106                                 XmlNode n = xel.ChildNodes [i];
107                                 if (n.NodeType == XmlNodeType.Element && n.LocalName == element && n.NamespaceURI == XmlSignature.NamespaceURI)
108                                         al.Add (n);
109                         }
110                         return al.ToArray (typeof (XmlElement)) as XmlElement [];
111                 }
112         }
113 }