XMLDSIG transforms from .NET Core.
[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 Uri {
91                         public const string Manifest = "http://www.w3.org/2000/09/xmldsig#Manifest";
92                 }
93
94                 public const string NamespaceURI = "http://www.w3.org/2000/09/xmldsig#";
95                 public const string Prefix = "ds";
96
97                 public XmlSignature () 
98                 {
99                 }
100
101                 public static XmlElement GetChildElement (XmlElement xel, string element, string ns)
102                 {
103                         for (int i = 0; i < xel.ChildNodes.Count; i++) {
104                                 XmlNode n = xel.ChildNodes [i];
105                                 if (n.NodeType == XmlNodeType.Element && n.LocalName == element && n.NamespaceURI == ns)
106                                         return n as XmlElement;
107                         }
108                         return null;
109                 }
110
111                 public static string GetAttributeFromElement (XmlElement xel, string attribute, string element) 
112                 {
113                         XmlElement el = GetChildElement (xel, element, XmlSignature.NamespaceURI);
114                         return el != null ? el.GetAttribute (attribute) : null;
115                 }
116
117                 public static XmlElement [] GetChildElements (XmlElement xel, string element)
118                 {
119                         ArrayList al = new ArrayList ();
120                         for (int i = 0; i < xel.ChildNodes.Count; i++) {
121                                 XmlNode n = xel.ChildNodes [i];
122                                 if (n.NodeType == XmlNodeType.Element && n.LocalName == element && n.NamespaceURI == XmlSignature.NamespaceURI)
123                                         al.Add (n);
124                         }
125                         return al.ToArray (typeof (XmlElement)) as XmlElement [];
126                 }
127         }
128 }