2 // SignedInfo.cs - SignedInfo implementation for XML Signature
5 // Sebastien Pouliot (spouliot@motus.com)
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
11 using System
.Collections
;
15 using System
.Security
.Cryptography
;
16 using System
.Security
.Cryptography
.Xml
;
18 namespace Microsoft
.Web
.Services
.Security
{
20 namespace System
.Security
.Cryptography
.Xml
{
22 public class SignedInfo
: ICollection
, IEnumerable
{
24 private ArrayList references
;
25 private string c14nMethod
;
27 private string signatureMethod
;
28 private string signatureLength
;
32 references
= new ArrayList ();
33 c14nMethod
= "http://www.w3.org/TR/2001/REC-xml-c14n-20010315";
36 public string CanonicalizationMethod
{
37 get { return c14nMethod; }
38 set { c14nMethod = value; }
41 // documented as not supported (and throwing exception)
43 get { throw new NotSupportedException (); }
51 // documented as not supported (and throwing exception)
52 public bool IsReadOnly
{
53 get { throw new NotSupportedException (); }
56 // documented as not supported (and throwing exception)
57 public bool IsSynchronized
{
58 get { throw new NotSupportedException (); }
61 public ArrayList References
{
62 get { return references; }
65 public string SignatureLength
{
66 get { return signatureLength; }
67 set { signatureLength = value; }
70 public string SignatureMethod
{
71 get { return signatureMethod; }
72 set { signatureMethod = value; }
75 // documented as not supported (and throwing exception)
76 public object SyncRoot
{
77 get { throw new NotSupportedException (); }
80 public void AddReference (Reference reference
)
82 references
.Add (reference
);
85 // documented as not supported (and throwing exception)
86 public void CopyTo (Array array
, int index
)
88 throw new NotSupportedException ();
91 public IEnumerator
GetEnumerator ()
93 return references
.GetEnumerator ();
96 public XmlElement
GetXml()
98 if (signatureMethod
== null)
99 throw new CryptographicException ("SignatureMethod");
100 if (references
.Count
== 0)
101 throw new CryptographicException ("References empty");
103 XmlDocument document
= new XmlDocument ();
104 XmlElement xel
= document
.CreateElement (XmlSignature
.ElementNames
.SignedInfo
, XmlSignature
.NamespaceURI
);
106 xel
.SetAttribute (XmlSignature
.AttributeNames
.Id
, id
);
108 if (c14nMethod
!= null) {
109 XmlElement c14n
= document
.CreateElement (XmlSignature
.ElementNames
.CanonicalizationMethod
, XmlSignature
.NamespaceURI
);
110 c14n
.SetAttribute (XmlSignature
.AttributeNames
.Algorithm
, c14nMethod
);
111 xel
.AppendChild (c14n
);
113 if (signatureMethod
!= null) {
114 XmlElement sm
= document
.CreateElement (XmlSignature
.ElementNames
.SignatureMethod
, XmlSignature
.NamespaceURI
);
115 sm
.SetAttribute (XmlSignature
.AttributeNames
.Algorithm
, signatureMethod
);
116 if (signatureLength
!= null) {
117 XmlElement hmac
= document
.CreateElement (XmlSignature
.ElementNames
.HMACOutputLength
, XmlSignature
.NamespaceURI
);
118 hmac
.InnerText
= signatureLength
;
119 sm
.AppendChild (hmac
);
121 xel
.AppendChild (sm
);
124 // we add References afterward so we don't end up with extraneous
125 // xmlns="..." in each reference elements.
126 foreach (Reference r
in references
) {
127 XmlNode xn
= r
.GetXml ();
128 XmlNode newNode
= document
.ImportNode (xn
, true);
129 xel
.AppendChild (newNode
);
135 private string GetAttributeFromElement (XmlElement xel
, string attribute
, string element
)
137 string result
= null;
138 XmlNodeList xnl
= xel
.GetElementsByTagName (element
);
139 if ((xnl
!= null) && (xnl
.Count
> 0)) {
140 XmlAttribute xa
= xnl
[0].Attributes
[attribute
];
142 result
= xa
.InnerText
;
147 private string GetAttribute (XmlElement xel
, string attribute
)
149 XmlAttribute xa
= xel
.Attributes
[attribute
];
150 return ((xa
!= null) ? xa
.InnerText
: null);
153 [MonoTODO("signatureLength for HMAC")]
154 public void LoadXml (XmlElement
value)
157 throw new ArgumentNullException ("value");
159 if ((value.LocalName
!= XmlSignature
.ElementNames
.SignedInfo
) || (value.NamespaceURI
!= XmlSignature
.NamespaceURI
))
160 throw new CryptographicException ();
162 id
= GetAttribute (value, XmlSignature
.AttributeNames
.Id
);
163 c14nMethod
= GetAttributeFromElement (value, XmlSignature
.AttributeNames
.Algorithm
, XmlSignature
.ElementNames
.CanonicalizationMethod
);
164 signatureMethod
= GetAttributeFromElement (value, XmlSignature
.AttributeNames
.Algorithm
, XmlSignature
.ElementNames
.SignatureMethod
);
165 // TODO signatureLength for HMAC
166 XmlNodeList xnl
= value.GetElementsByTagName (XmlSignature
.ElementNames
.Reference
);
167 foreach (XmlNode xn
in xnl
) {
168 Reference r
= new Reference ();
169 r
.LoadXml ((XmlElement
) xn
);