Merge pull request #2237 from xmcclure/container-owner
[mono.git] / mcs / class / System.Security / System.Security.Cryptography.Xml / SignedInfo.cs
index af9657fe7db21ed66790944614fb745e5e7f7ff6..f9d6d9f0048610ece2aeac44f9a0d08b4f625ad1 100644 (file)
@@ -2,12 +2,35 @@
 // SignedInfo.cs - SignedInfo implementation for XML Signature
 //
 // Author:
-//     Sebastien Pouliot (spouliot@motus.com)
+//     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 { 
@@ -19,6 +42,7 @@ namespace System.Security.Cryptography.Xml {
                private string id;
                private string signatureMethod;
                private string signatureLength;
+               private XmlElement element;
 
                public SignedInfo() 
                {
@@ -28,7 +52,16 @@ namespace System.Security.Cryptography.Xml {
 
                public string CanonicalizationMethod {
                        get { return c14nMethod; }
-                       set { c14nMethod = value; }
+                       set {
+                               c14nMethod = value;
+                               element = null;
+                       }
+               }
+
+               [ComVisible (false)]
+               [MonoTODO]
+               public Transform CanonicalizationMethodObject {
+                       get { throw new NotImplementedException (); }
                }
 
                // documented as not supported (and throwing exception)
@@ -38,7 +71,10 @@ namespace System.Security.Cryptography.Xml {
 
                public string Id {
                        get { return id; }
-                       set { id = value; }
+                       set {
+                               element = null;
+                               id = value;
+                       }
                }
 
                // documented as not supported (and throwing exception)
@@ -51,18 +87,27 @@ namespace System.Security.Cryptography.Xml {
                        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 { signatureLength = value; }
+                       set {
+                               element = null;
+                               signatureLength = value;
+                       }
                }
 
                public string SignatureMethod {
                        get { return signatureMethod; }
-                       set { signatureMethod = value; }
+                       set {
+                               element = null;
+                               signatureMethod = value;
+                       }
                }
 
                // documented as not supported (and throwing exception)
@@ -86,8 +131,11 @@ namespace System.Security.Cryptography.Xml {
                        return references.GetEnumerator ();
                }
 
-               public XmlElement GetXml() 
+               public XmlElement GetXml ()
                {
+                       if (element != null)
+                               return element;
+
                        if (signatureMethod == null)
                                throw new CryptographicException ("SignatureMethod");
                        if (references.Count == 0)
@@ -114,6 +162,10 @@ namespace System.Security.Cryptography.Xml {
                                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) {
@@ -125,25 +177,12 @@ namespace System.Security.Cryptography.Xml {
                        return xel;
                }
 
-               private string GetAttributeFromElement (XmlElement xel, string attribute, string element) 
-               {
-                       string result = null;
-                       XmlNodeList xnl = xel.GetElementsByTagName (element);
-                       if ((xnl != null) && (xnl.Count > 0)) {
-                               XmlAttribute xa = xnl[0].Attributes [attribute];
-                               if (xa != null)
-                                       result = xa.InnerText;
-                       }
-                       return result;
-               }
-
                private string GetAttribute (XmlElement xel, string attribute) 
                {
                        XmlAttribute xa = xel.Attributes [attribute];
                        return ((xa != null) ? xa.InnerText : null);
                }
 
-               [MonoTODO("signatureLength for HMAC")]
                public void LoadXml (XmlElement value) 
                {
                        if (value == null)
@@ -153,15 +192,28 @@ namespace System.Security.Cryptography.Xml {
                                throw new CryptographicException ();
 
                        id = GetAttribute (value, XmlSignature.AttributeNames.Id);
-                       c14nMethod = GetAttributeFromElement (value, XmlSignature.AttributeNames.Algorithm, XmlSignature.ElementNames.CanonicalizationMethod);
-                       signatureMethod = GetAttributeFromElement (value, XmlSignature.AttributeNames.Algorithm, XmlSignature.ElementNames.SignatureMethod);
-                       // TODO signatureLength for HMAC
-                       XmlNodeList xnl = value.GetElementsByTagName (XmlSignature.ElementNames.Reference);
-                       foreach (XmlNode xn in xnl) {
-                               Reference r = new Reference ();
-                               r.LoadXml ((XmlElement) xn);
-                               AddReference (r);
+                       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;
                }
        }
 }