Merge pull request #347 from JamesB7/master
[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 //      Tim Coleman (tim@timcoleman.com)
8 //
9 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
10 // Copyright (C) Tim Coleman, 2004
11 // (C) 2004 Novell Inc.
12 //
13
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34
35 using System;
36 using System.Collections;
37 using System.Xml;
38
39 namespace System.Security.Cryptography.Xml {
40
41         // following the design of WSE
42         internal class XmlSignature {
43
44                 public class ElementNames {
45
46                         public const string CanonicalizationMethod = "CanonicalizationMethod";
47                         public const string DigestMethod = "DigestMethod";
48                         public const string DigestValue = "DigestValue";
49                         public const string DSAKeyValue = "DSAKeyValue";
50                         public const string EncryptedKey = "EncryptedKey";
51                         public const string HMACOutputLength = "HMACOutputLength";
52                         public const string KeyInfo = "KeyInfo";
53                         public const string KeyName = "KeyName";
54                         public const string KeyValue = "KeyValue";
55                         public const string Manifest = "Manifest";
56                         public const string Object = "Object";
57                         public const string Reference = "Reference";
58                         public const string RetrievalMethod = "RetrievalMethod";
59                         public const string RSAKeyValue = "RSAKeyValue";
60                         public const string Signature = "Signature";
61                         public const string SignatureMethod = "SignatureMethod";
62                         public const string SignatureValue = "SignatureValue";
63                         public const string SignedInfo = "SignedInfo";
64                         public const string Transform = "Transform";
65                         public const string Transforms = "Transforms";
66                         public const string X509Data = "X509Data";
67                         public const string X509IssuerSerial = "X509IssuerSerial";
68                         public const string X509IssuerName = "X509IssuerName";
69                         public const string X509SerialNumber = "X509SerialNumber";
70                         public const string X509SKI = "X509SKI";
71                         public const string X509SubjectName = "X509SubjectName";
72                         public const string X509Certificate = "X509Certificate";
73                         public const string X509CRL = "X509CRL";
74
75                         public ElementNames () {}
76                 }
77
78                 public class AttributeNames {
79
80                         public const string Algorithm = "Algorithm";
81                         public const string Encoding = "Encoding";
82                         public const string Id = "Id";
83                         public const string MimeType = "MimeType";
84                         public const string Type = "Type";
85                         public const string URI = "URI";
86
87                         public AttributeNames () {}
88                 }
89
90                 public class AlgorithmNamespaces {
91                         public const string XmlDsigBase64Transform = "http://www.w3.org/2000/09/xmldsig#base64";
92                         public const string XmlDsigC14NTransform = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315";
93                         public const string XmlDsigC14NWithCommentsTransform = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments";
94                         public const string XmlDsigEnvelopedSignatureTransform = "http://www.w3.org/2000/09/xmldsig#enveloped-signature";
95                         public const string XmlDsigXPathTransform = "http://www.w3.org/TR/1999/REC-xpath-19991116";
96                         public const string XmlDsigXsltTransform =  "http://www.w3.org/TR/1999/REC-xslt-19991116";
97 #if NET_2_0
98                         public const string XmlDsigExcC14NTransform = "http://www.w3.org/2001/10/xml-exc-c14n#";
99                         public const string XmlDsigExcC14NWithCommentsTransform = "http://www.w3.org/2001/10/xml-exc-c14n#WithComments";
100                         public const string XmlDecryptionTransform = "http://www.w3.org/2002/07/decrypt#XML";
101                         public const string XmlLicenseTransform = "urn:mpeg:mpeg21:2003:01-REL-R-NS:licenseTransform";
102 #endif
103                 }
104
105                 public class Uri {
106                         public const string Manifest = "http://www.w3.org/2000/09/xmldsig#Manifest";
107                 }
108
109                 public const string NamespaceURI = "http://www.w3.org/2000/09/xmldsig#";
110                 public const string Prefix = "ds";
111
112                 public XmlSignature () 
113                 {
114                 }
115
116                 public static XmlElement GetChildElement (XmlElement xel, string element, string ns)
117                 {
118                         for (int i = 0; i < xel.ChildNodes.Count; i++) {
119                                 XmlNode n = xel.ChildNodes [i];
120                                 if (n.NodeType == XmlNodeType.Element && n.LocalName == element && n.NamespaceURI == ns)
121                                         return n as XmlElement;
122                         }
123                         return null;
124                 }
125
126                 public static string GetAttributeFromElement (XmlElement xel, string attribute, string element) 
127                 {
128                         XmlElement el = GetChildElement (xel, element, XmlSignature.NamespaceURI);
129                         return el != null ? el.GetAttribute (attribute) : null;
130                 }
131
132                 public static XmlElement [] GetChildElements (XmlElement xel, string element)
133                 {
134                         ArrayList al = new ArrayList ();
135                         for (int i = 0; i < xel.ChildNodes.Count; i++) {
136                                 XmlNode n = xel.ChildNodes [i];
137                                 if (n.NodeType == XmlNodeType.Element && n.LocalName == element && n.NamespaceURI == XmlSignature.NamespaceURI)
138                                         al.Add (n);
139                         }
140                         return al.ToArray (typeof (XmlElement)) as XmlElement [];
141                 }
142         }
143 }