BindingFlags.Public needed here as Exception.HResult is now public in .NET 4.5. This...
[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 #if NET_2_0
51                         public const string EncryptedKey = "EncryptedKey";
52 #endif
53                         public const string HMACOutputLength = "HMACOutputLength";
54                         public const string KeyInfo = "KeyInfo";
55                         public const string KeyName = "KeyName";
56                         public const string KeyValue = "KeyValue";
57                         public const string Manifest = "Manifest";
58                         public const string Object = "Object";
59                         public const string Reference = "Reference";
60 #if NET_1_0
61                         // RetrievalMethod vs RetrievalElement -> BUG in MS Framework 1.0
62                         public const string RetrievalMethod = "RetrievalElement";
63 #else
64                         public const string RetrievalMethod = "RetrievalMethod";
65 #endif
66                         public const string RSAKeyValue = "RSAKeyValue";
67                         public const string Signature = "Signature";
68                         public const string SignatureMethod = "SignatureMethod";
69                         public const string SignatureValue = "SignatureValue";
70                         public const string SignedInfo = "SignedInfo";
71                         public const string Transform = "Transform";
72                         public const string Transforms = "Transforms";
73                         public const string X509Data = "X509Data";
74                         public const string X509IssuerSerial = "X509IssuerSerial";
75                         public const string X509IssuerName = "X509IssuerName";
76                         public const string X509SerialNumber = "X509SerialNumber";
77                         public const string X509SKI = "X509SKI";
78                         public const string X509SubjectName = "X509SubjectName";
79                         public const string X509Certificate = "X509Certificate";
80                         public const string X509CRL = "X509CRL";
81
82                         public ElementNames () {}
83                 }
84
85                 public class AttributeNames {
86
87                         public const string Algorithm = "Algorithm";
88                         public const string Encoding = "Encoding";
89                         public const string Id = "Id";
90                         public const string MimeType = "MimeType";
91                         public const string Type = "Type";
92                         public const string URI = "URI";
93
94                         public AttributeNames () {}
95                 }
96
97                 public class AlgorithmNamespaces {
98                         public const string XmlDsigBase64Transform = "http://www.w3.org/2000/09/xmldsig#base64";
99                         public const string XmlDsigC14NTransform = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315";
100                         public const string XmlDsigC14NWithCommentsTransform = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments";
101                         public const string XmlDsigEnvelopedSignatureTransform = "http://www.w3.org/2000/09/xmldsig#enveloped-signature";
102                         public const string XmlDsigXPathTransform = "http://www.w3.org/TR/1999/REC-xpath-19991116";
103                         public const string XmlDsigXsltTransform =  "http://www.w3.org/TR/1999/REC-xslt-19991116";
104 #if NET_2_0
105                         public const string XmlDsigExcC14NTransform = "http://www.w3.org/2001/10/xml-exc-c14n#";
106                         public const string XmlDsigExcC14NWithCommentsTransform = "http://www.w3.org/2001/10/xml-exc-c14n#WithComments";
107                         public const string XmlDecryptionTransform = "http://www.w3.org/2002/07/decrypt#XML";
108                         public const string XmlLicenseTransform = "urn:mpeg:mpeg21:2003:01-REL-R-NS:licenseTransform";
109 #endif
110                 }
111
112                 public class Uri {
113                         public const string Manifest = "http://www.w3.org/2000/09/xmldsig#Manifest";
114                 }
115
116                 public const string NamespaceURI = "http://www.w3.org/2000/09/xmldsig#";
117                 public const string Prefix = "ds";
118
119                 public XmlSignature () 
120                 {
121                 }
122
123                 public static XmlElement GetChildElement (XmlElement xel, string element, string ns)
124                 {
125                         for (int i = 0; i < xel.ChildNodes.Count; i++) {
126                                 XmlNode n = xel.ChildNodes [i];
127                                 if (n.NodeType == XmlNodeType.Element && n.LocalName == element && n.NamespaceURI == ns)
128                                         return n as XmlElement;
129                         }
130                         return null;
131                 }
132
133                 public static string GetAttributeFromElement (XmlElement xel, string attribute, string element) 
134                 {
135                         XmlElement el = GetChildElement (xel, element, XmlSignature.NamespaceURI);
136                         return el != null ? el.GetAttribute (attribute) : null;
137                 }
138
139                 public static XmlElement [] GetChildElements (XmlElement xel, string element)
140                 {
141                         ArrayList al = new ArrayList ();
142                         for (int i = 0; i < xel.ChildNodes.Count; i++) {
143                                 XmlNode n = xel.ChildNodes [i];
144                                 if (n.NodeType == XmlNodeType.Element && n.LocalName == element && n.NamespaceURI == XmlSignature.NamespaceURI)
145                                         al.Add (n);
146                         }
147                         return al.ToArray (typeof (XmlElement)) as XmlElement [];
148                 }
149         }
150 }