File: SignedInfo.cs

package info (click to toggle)
mono 1.2.2.1-1etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 142,720 kB
  • ctags: 256,408
  • sloc: cs: 1,495,736; ansic: 249,442; sh: 18,327; xml: 12,463; makefile: 5,046; perl: 1,248; asm: 635; yacc: 285; sql: 7
file content (227 lines) | stat: -rw-r--r-- 7,015 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//
// SignedInfo.cs - SignedInfo implementation for XML Signature
//
// Author:
//	Sebastien Pouliot  <sebastien@ximian.com>
//      Tim Coleman (tim@timcoleman.com)
//
// (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) Tim Coleman, 2004
// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System.Collections;
using System.Runtime.InteropServices;
using System.Xml;

namespace System.Security.Cryptography.Xml { 

	public class SignedInfo : ICollection, IEnumerable {

		private ArrayList references;
		private string c14nMethod;
		private string id;
		private string signatureMethod;
		private string signatureLength;
		private XmlElement element;

#if NET_2_0
		XmlDsigC14NTransform canonicalizationMethodObject;
#endif

		public SignedInfo() 
		{
			references = new ArrayList ();
			c14nMethod = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315";
#if NET_2_0	
			canonicalizationMethodObject = new XmlDsigC14NTransform ();
#endif
		}

		public string CanonicalizationMethod {
			get { return c14nMethod; }
			set {
				c14nMethod = value;
				element = null;
			}
		}

#if NET_2_0
		[ComVisible (false)]
		public Transform CanonicalizationMethodObject {
			get { return canonicalizationMethodObject; }
		}
#endif

		// documented as not supported (and throwing exception)
		public int Count {
			get { throw new NotSupportedException (); }
		}

		public string Id {
			get { return id; }
			set {
				element = null;
				id = value;
			}
		}

		// documented as not supported (and throwing exception)
		public bool IsReadOnly {
			get { throw new NotSupportedException (); }
		}

		// documented as not supported (and throwing exception)
		public bool IsSynchronized {
			get { throw new NotSupportedException (); }
		}

		// Manipulating this array never affects GetXml() when 
		// LoadXml() was used. 
		// (Actually, there is no way to detect modification.)
		public ArrayList References {
			get { return references; }
		}

		public string SignatureLength {
			get { return signatureLength; }
			set {
				element = null;
				signatureLength = value;
			}
		}

		public string SignatureMethod {
			get { return signatureMethod; }
			set {
				element = null;
				signatureMethod = value;
			}
		}

		// documented as not supported (and throwing exception)
		public object SyncRoot {
			get { throw new NotSupportedException (); }
		}

		public void AddReference (Reference reference) 
		{
			references.Add (reference);
		}

		// documented as not supported (and throwing exception)
		public void CopyTo (Array array, int index) 
		{
			throw new NotSupportedException ();
		}

		public IEnumerator GetEnumerator () 
		{
			return references.GetEnumerator ();
		}

		public XmlElement GetXml ()
		{
			if (element != null)
				return element;

			if (signatureMethod == null)
				throw new CryptographicException ("SignatureMethod");
			if (references.Count == 0)
				throw new CryptographicException ("References empty");

			XmlDocument document = new XmlDocument ();
			XmlElement xel = document.CreateElement (XmlSignature.ElementNames.SignedInfo, XmlSignature.NamespaceURI);
			if (id != null)
				xel.SetAttribute (XmlSignature.AttributeNames.Id, id);

			if (c14nMethod != null) {
				XmlElement c14n = document.CreateElement (XmlSignature.ElementNames.CanonicalizationMethod, XmlSignature.NamespaceURI);
				c14n.SetAttribute (XmlSignature.AttributeNames.Algorithm, c14nMethod);
				xel.AppendChild (c14n);
			}
			if (signatureMethod != null) {
				XmlElement sm = document.CreateElement (XmlSignature.ElementNames.SignatureMethod, XmlSignature.NamespaceURI);
				sm.SetAttribute (XmlSignature.AttributeNames.Algorithm, signatureMethod);
				if (signatureLength != null) {
					XmlElement hmac = document.CreateElement (XmlSignature.ElementNames.HMACOutputLength, XmlSignature.NamespaceURI);
					hmac.InnerText = signatureLength;
					sm.AppendChild (hmac);
				}
				xel.AppendChild (sm);
			}

			// This check is only done when element is created here.
			if (references.Count == 0)
				throw new CryptographicException ("At least one Reference element is required in SignedInfo.");

			// we add References afterward so we don't end up with extraneous
			// xmlns="..." in each reference elements.
			foreach (Reference r in references) {
				XmlNode xn = r.GetXml ();
				XmlNode newNode = document.ImportNode (xn, true);
				xel.AppendChild (newNode);
			}

			return xel;
		}

		private string GetAttribute (XmlElement xel, string attribute) 
		{
			XmlAttribute xa = xel.Attributes [attribute];
			return ((xa != null) ? xa.InnerText : null);
		}

		public void LoadXml (XmlElement value) 
		{
			if (value == null)
				throw new ArgumentNullException ("value");

			if ((value.LocalName != XmlSignature.ElementNames.SignedInfo) || (value.NamespaceURI != XmlSignature.NamespaceURI))
				throw new CryptographicException ();

			id = GetAttribute (value, XmlSignature.AttributeNames.Id);
			c14nMethod = XmlSignature.GetAttributeFromElement (value, XmlSignature.AttributeNames.Algorithm, XmlSignature.ElementNames.CanonicalizationMethod);

			XmlElement sm = XmlSignature.GetChildElement (value, XmlSignature.ElementNames.SignatureMethod, XmlSignature.NamespaceURI);
			if (sm != null) {
				signatureMethod = sm.GetAttribute (XmlSignature.AttributeNames.Algorithm);
				XmlElement length = XmlSignature.GetChildElement (sm, XmlSignature.ElementNames.HMACOutputLength, XmlSignature.NamespaceURI);
				if (length != null) {
					signatureLength = length.InnerText;
				}
			}

			for (int i = 0; i < value.ChildNodes.Count; i++) {
				XmlNode n = value.ChildNodes [i];
				if (n.NodeType == XmlNodeType.Element &&
					n.LocalName == XmlSignature.ElementNames.Reference &&
					n.NamespaceURI == XmlSignature.NamespaceURI) {
					Reference r = new Reference ();
					r.LoadXml ((XmlElement) n);
					AddReference (r);
				}
			}
			element = value;
		}
	}
}