2004-05-13 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 Manifest = "Manifest";
32                         public const string Object = "Object";
33                         public const string Reference = "Reference";
34 #if NET_1_0
35                         // RetrievalMethod vs RetrievalElement -> BUG in MS Framework 1.0
36                         public const string RetrievalMethod = "RetrievalElement";
37 #else
38                         public const string RetrievalMethod = "RetrievalMethod";
39 #endif
40                         public const string RSAKeyValue = "RSAKeyValue";
41                         public const string Signature = "Signature";
42                         public const string SignatureMethod = "SignatureMethod";
43                         public const string SignatureValue = "SignatureValue";
44                         public const string SignedInfo = "SignedInfo";
45                         public const string Transform = "Transform";
46                         public const string Transforms = "Transforms";
47                         public const string X509Data = "X509Data";
48                         public const string X509IssuerSerial = "X509IssuerSerial";
49                         public const string X509IssuerName = "X509IssuerName";
50                         public const string X509SerialNumber = "X509SerialNumber";
51                         public const string X509SKI = "X509SKI";
52                         public const string X509SubjectName = "X509SubjectName";
53                         public const string X509Certificate = "X509Certificate";
54                         public const string X509CRL = "X509CRL";
55
56                         public ElementNames () {}
57                 }
58
59                 public class AttributeNames {
60
61                         public const string Algorithm = "Algorithm";
62                         public const string Encoding = "Encoding";
63                         public const string Id = "Id";
64                         public const string MimeType = "MimeType";
65                         public const string Type = "Type";
66                         public const string URI = "URI";
67
68                         public AttributeNames () {}
69                 }
70
71                 public class AlgorithmNamespaces {
72                         public const string XmlDsigBase64Transform = "http://www.w3.org/2000/09/xmldsig#base64";
73                         public const string XmlDsigC14NTransform = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315";
74                         public const string XmlDsigC14NWithCommentsTransform = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments";
75                         public const string XmlDsigEnvelopedSignatureTransform = "http://www.w3.org/2000/09/xmldsig#enveloped-signature";
76                         public const string XmlDsigXPathTransform = "http://www.w3.org/TR/1999/REC-xpath-19991116";
77                         public const string XmlDsigXsltTransform =  "http://www.w3.org/TR/1999/REC-xslt-19991116";
78                 }
79
80                 public class Uri {
81                         public const string Manifest = "http://www.w3.org/2000/09/xmldsig#Manifest";
82                 }
83
84                 public const string NamespaceURI = "http://www.w3.org/2000/09/xmldsig#";
85                 public const string Prefix = "ds";
86
87                 public XmlSignature () 
88                 {
89                 }
90
91                 public static XmlElement GetChildElement (XmlElement xel, string element, string ns)
92                 {
93                         for (int i = 0; i < xel.ChildNodes.Count; i++) {
94                                 XmlNode n = xel.ChildNodes [i];
95                                 if (n.NodeType == XmlNodeType.Element && n.LocalName == element && n.NamespaceURI == ns)
96                                         return n as XmlElement;
97                         }
98                         return null;
99                 }
100
101                 public static string GetAttributeFromElement (XmlElement xel, string attribute, string element) 
102                 {
103                         XmlElement el = GetChildElement (xel, element, XmlSignature.NamespaceURI);
104                         return el != null ? el.GetAttribute (attribute) : null;
105                 }
106
107                 public static XmlElement [] GetChildElements (XmlElement xel, string element)
108                 {
109                         ArrayList al = new ArrayList ();
110                         for (int i = 0; i < xel.ChildNodes.Count; i++) {
111                                 XmlNode n = xel.ChildNodes [i];
112                                 if (n.NodeType == XmlNodeType.Element && n.LocalName == element && n.NamespaceURI == XmlSignature.NamespaceURI)
113                                         al.Add (n);
114                         }
115                         return al.ToArray (typeof (XmlElement)) as XmlElement [];
116                 }
117         }
118 }