2 // DataObject.cs - DataObject implementation for XML Signature
3 // http://www.w3.org/2000/09/xmldsig#Object
6 // Sebastien Pouliot (spouliot@motus.com)
7 // Atsushi Enomoto (atsushi@ximian.com)
9 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
10 // (C) 2004 Novell Inc.
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 namespace System
.Security
.Cryptography
.Xml
{
38 // XmlElement part of the signature
39 // Note: Looks like KeyInfoNode (but the later is XmlElement inside KeyInfo)
40 // required for "enveloping signatures"
41 public class DataObject
{
44 private string mimeType
;
45 private string encoding
;
46 private XmlElement element
;
47 private bool propertyModified
;
51 Build (null, null, null, null);
54 public DataObject (string id
, string mimeType
, string encoding
, XmlElement data
)
57 throw new ArgumentNullException ("data");
59 Build (id
, mimeType
, encoding
, data
);
62 // this one accept a null "data" parameter
63 private void Build (string id
, string mimeType
, string encoding
, XmlElement data
)
65 XmlDocument document
= new XmlDocument ();
66 XmlElement xel
= document
.CreateElement (XmlSignature
.ElementNames
.Object
, XmlSignature
.NamespaceURI
);
69 xel
.SetAttribute (XmlSignature
.AttributeNames
.Id
, id
);
71 if (mimeType
!= null) {
72 this.mimeType
= mimeType
;
73 xel
.SetAttribute (XmlSignature
.AttributeNames
.MimeType
, mimeType
);
75 if (encoding
!= null) {
76 this.encoding
= encoding
;
77 xel
.SetAttribute (XmlSignature
.AttributeNames
.Encoding
, encoding
);
80 XmlNode newNode
= document
.ImportNode (data
, true);
81 xel
.AppendChild (newNode
);
83 document
.AppendChild (xel
); // FIXME: it should not be appended
85 element
= document
.DocumentElement
;
88 // why is data a XmlNodeList instead of a XmlElement ?
89 public XmlNodeList Data
{
91 return element
.ChildNodes
;
95 throw new ArgumentNullException ("value");
96 XmlDocument doc
= new XmlDocument ();
97 XmlElement el
= (XmlElement
) doc
.ImportNode (element
, true);
98 doc
.AppendChild (el
); // FIXME: it should not be appended
100 foreach (XmlNode n
in value)
101 el
.AppendChild (doc
.ImportNode (n
, true));
103 propertyModified
= true;
107 // default to null - no encoding
108 public string Encoding
{
109 get { return GetField (XmlSignature.AttributeNames.Encoding); }
110 set { SetField (XmlSignature.AttributeNames.Encoding, value); }
115 get { return GetField (XmlSignature.AttributeNames.Id); }
116 set { SetField (XmlSignature.AttributeNames.Id, value); }
120 public string MimeType
{
121 get { return GetField (XmlSignature.AttributeNames.MimeType); }
122 set { SetField (XmlSignature.AttributeNames.MimeType, value); }
125 private string GetField (string attribute
)
127 XmlNode attr
= element
.Attributes
[attribute
];
128 return attr
!= null ? attr
.Value
: null;
131 private void SetField (string attribute
, string value)
133 // MS-BUGS: it never cleans attribute value up.
137 if (propertyModified
)
138 element
.SetAttribute (attribute
, value);
140 XmlDocument document
= new XmlDocument ();
141 XmlElement el
= document
.ImportNode (element
, true) as XmlElement
;
142 el
.Attributes
.RemoveAll ();
143 document
.AppendChild (el
); // FIXME: it should not be appended
144 el
.SetAttribute (attribute
, value);
146 propertyModified
= true;
150 public XmlElement
GetXml ()
152 if (propertyModified
) {
153 // It looks MS.NET returns element which comes from new XmlDocument every time
154 XmlElement oldElement
= element
;
155 XmlDocument doc
= new XmlDocument ();
156 element
= doc
.CreateElement (XmlSignature
.ElementNames
.Object
, XmlSignature
.NamespaceURI
);
157 doc
.AppendChild (element
); // FIXME: it should not be appended
158 foreach (XmlAttribute attribute
in oldElement
.Attributes
) {
159 switch (attribute
.Name
) {
160 case XmlSignature
.AttributeNames
.Id
:
161 case XmlSignature
.AttributeNames
.Encoding
:
162 case XmlSignature
.AttributeNames
.MimeType
:
163 element
.SetAttribute (attribute
.Name
, attribute
.Value
);
167 foreach (XmlNode n
in oldElement
.ChildNodes
)
168 element
.AppendChild (doc
.ImportNode (n
, true));
173 public void LoadXml (XmlElement
value)
176 throw new ArgumentNullException ("value");
178 propertyModified
= false;