diff options
Diffstat (limited to 'mcs/class/System.Security')
45 files changed, 4742 insertions, 0 deletions
diff --git a/mcs/class/System.Security/ChangeLog b/mcs/class/System.Security/ChangeLog new file mode 100644 index 00000000000..699b342991e --- /dev/null +++ b/mcs/class/System.Security/ChangeLog @@ -0,0 +1,7 @@ +2003-01-17 Sebastien Pouliot <spouliot@videotron.ca> + + * System.Security.build: Simplified and fixed some issues. + +2002-11-28 Sebastien Pouliot <spouliot@videotron.ca> + + * System.Security.build: New. Nant build file for Windows. diff --git a/mcs/class/System.Security/System.Security.Cryptography.Xml/ChangeLog b/mcs/class/System.Security/System.Security.Cryptography.Xml/ChangeLog new file mode 100644 index 00000000000..8b7882eea4d --- /dev/null +++ b/mcs/class/System.Security/System.Security.Cryptography.Xml/ChangeLog @@ -0,0 +1,48 @@ +2003-01-17 Sebastien Pouliot <spouliot@videotron.ca> + + * KeyInfo.cs: Changed some protected declaration to private. + * KeyInfoName.cs: Changed some protected declaration to private. + * KeyInfoNode.cs: Changed some protected declaration to private. + * KeyInfoRetrievalMethod.cs: Changed some protected declaration to private. + * KeyInfoX509Data.cs: Changed some protected declaration to private. + * Transform.cs: Changed some protected declaration to private. + * XmlDsigBase64Transform.cs: Changed some protected declaration to private. + * XmlDsigC14NTransform.cs: Changed some protected declaration to private. + * XmlDsigC14NWithCommentsTransform.cs: Changed some protected declaration to private. + * XmlDsigEnvelopedSignatureTransform.cs: Changed some protected declaration to private. + * XmlDsigXPathTransform.cs: Changed some protected declaration to private. + * XmlDsigXsltTransform.cs: Changed some protected declaration to private. + +2002-11-28 Sebastien Pouliot <spouliot@videotron.ca> + + * TODOAttribute.cs: New. Still much to do ;-) + * XmlDsigXPathTransform.cs: Corrected to compile. Transform is non- + functionnal. + * XmlDsigXsltTransform.cs: Corrected to compile. Transform is non- + functionnal. + +2002-11-20 Sebastien Pouliot <spouliot@videotron.ca> + + * DataObject.cs: New. Complete implementation. + * DSAKeyValue.cs: New. Complete implementation. + * KeyInfo.cs: New. Complete implementation. + * KeyInfoClause.cs: New. Abstract class (complete). + * KeyInfoName.cs: New. Complete implementation. + * KeyInfoNode.cs: New. Complete implementation. + * KeyInfoRetrievalMethod.cs: New. Complete implementation. + * KeyInfoX509Data.cs: New. Complete implementation. + * Reference.cs: New. Incomplete implementation. + * RSAKeyValue.cs: New. Complete implementation. + * Signature.cs: New. Almost complete implementation - returned + XML isn't exactly like the MS implementation. + * SignedInfo.cs: New. Complete except SignatureLength. + * SignedXml.cs: New. Minimal implementation (only enveloped signatures). + Still many TODO! + * Transform.cs: New. Abstract class (complete). + * TransformChain.cs: New. Complete implementation. + * XmlDsigBase64Transform.cs: New. Stub + basic logic. + * XmlDsigC14NTransform.cs: New. Stub + basic logic. + * XmlDsigC14NWithCommentsTransform.cs: New. Stub + basic logic. + * XmlDsigEnvelopedSignatureTransform.cs: New. Stub + basic logic. + * XmlDsigXPathTransform.cs: New. Stub + basic logic. + * XmlDsigXsltTransform.cs: New. Stub + basic logic. diff --git a/mcs/class/System.Security/System.Security.Cryptography.Xml/DSAKeyValue.cs b/mcs/class/System.Security/System.Security.Cryptography.Xml/DSAKeyValue.cs new file mode 100644 index 00000000000..e451cd16596 --- /dev/null +++ b/mcs/class/System.Security/System.Security.Cryptography.Xml/DSAKeyValue.cs @@ -0,0 +1,60 @@ +// +// DSAKeyValue.cs - DSAKeyInfo implementation for XML Signature +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002 Motus Technologies Inc. (http://www.motus.com) +// + +using System.Security.Cryptography; +using System.Text; +using System.Xml; + +namespace System.Security.Cryptography.Xml { + +public class DSAKeyValue : KeyInfoClause { + + private DSA dsa; + + public DSAKeyValue () + { + dsa = DSA.Create (); + } + + public DSAKeyValue (DSA key) + { + dsa = key; + } + + public DSA Key + { + get { return dsa; } + set { dsa = value; } + } + + public override XmlElement GetXml () + { + StringBuilder sb = new StringBuilder (); + sb.Append ("<KeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\">"); + sb.Append (dsa.ToXmlString (false)); + sb.Append ("</KeyValue>"); + + XmlDocument doc = new XmlDocument (); + doc.LoadXml(sb.ToString ()); + return doc.DocumentElement; + } + + public override void LoadXml (XmlElement value) + { + if (value == null) + throw new ArgumentNullException (); + + if ((value.LocalName == "KeyValue") && (value.NamespaceURI == "http://www.w3.org/2000/09/xmldsig#")) + dsa.FromXmlString (value.InnerXml); + else + throw new CryptographicException ("value"); + } +} + +}
\ No newline at end of file diff --git a/mcs/class/System.Security/System.Security.Cryptography.Xml/DataObject.cs b/mcs/class/System.Security/System.Security.Cryptography.Xml/DataObject.cs new file mode 100644 index 00000000000..609bd452a23 --- /dev/null +++ b/mcs/class/System.Security/System.Security.Cryptography.Xml/DataObject.cs @@ -0,0 +1,162 @@ +// +// DataObject.cs - DataObject implementation for XML Signature +// http://www.w3.org/2000/09/xmldsig#Object +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002 Motus Technologies Inc. (http://www.motus.com) +// + +using System.Security.Cryptography; +using System.Text; +using System.Xml; + +namespace System.Security.Cryptography.Xml { + +// XmlElement part of the signature +// Note: Looks like KeyInfoNode (but the later is XmlElement inside KeyInfo) +// required for "enveloping signatures" +public class DataObject { + + private string id; + private string mimeType; + private string encoding; + private string data; + private XmlDocument doc; + + static private string xmldsig = "http://www.w3.org/2000/09/xmldsig#"; + + public DataObject () + { + Build (null, null, null, null); + } + + public DataObject (string id, string mimeType, string encoding, XmlElement data) + { + if (data == null) + throw new ArgumentNullException ("data"); + + Build (id, mimeType, encoding, data); + } + + private void Build (string id, string mimeType, string encoding, XmlElement data) + { + StringBuilder sb = new StringBuilder (); + sb.Append ("<Object "); + if (id != null) { + this.id = id; + sb.Append ("Id=\""); + sb.Append (id); + sb.Append ("\" "); + } + if (mimeType != null) { + this.mimeType = mimeType; + sb.Append ("MimeType=\""); + sb.Append (mimeType); + sb.Append ("\" "); + } + if (encoding != null) { + this.encoding = encoding; + sb.Append ("Encoding=\""); + sb.Append (encoding); + sb.Append ("\" "); + } + sb.Append ("xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />"); + + doc = new XmlDocument (); + doc.LoadXml (sb.ToString ()); + if (data != null) { + XmlNodeList xnl = doc.GetElementsByTagName ("Object"); + XmlNode newNode = doc.ImportNode (data, true); + xnl[0].AppendChild (newNode); + } + } + + // why is data a XmlNodeList instead of a XmlElement ? + public XmlNodeList Data { + get { + XmlNodeList xnl = doc.GetElementsByTagName ("Object"); + return xnl[0].ChildNodes; + } + set { + if (value == null) + throw new ArgumentNullException ("value"); + + Build (id, mimeType, encoding, null); + XmlNodeList xnl = doc.GetElementsByTagName ("Object"); + if ((xnl != null) && (xnl.Count > 0)) { + foreach (XmlNode xn in value) { + XmlNode newNode = doc.ImportNode (xn, true); + xnl[0].AppendChild (newNode); + } + } + } + } + + // default to null - no encoding + public string Encoding { + get { return encoding; } + set { encoding = value; } + } + + // default to null + public string Id { + get { return id; } + set { id = value; } + } + + // default to null + public string MimeType { + get { return mimeType; } + set { mimeType = value; } + } + + public XmlElement GetXml () + { + if ((doc.DocumentElement.LocalName == "Object") && (doc.DocumentElement.NamespaceURI == "http://www.w3.org/2000/09/xmldsig#")) { + // recreate all attributes in order + XmlAttribute xa = null; + doc.DocumentElement.Attributes.RemoveAll (); + if (id != null) { + xa = doc.CreateAttribute ("Id"); + xa.Value = id; + doc.DocumentElement.Attributes.Append (xa); + } + if (mimeType != null) { + xa = doc.CreateAttribute ("MimeType"); + xa.Value = mimeType; + doc.DocumentElement.Attributes.Append (xa); + } + if (encoding != null) { + xa = doc.CreateAttribute ("Encoding"); + xa.Value = encoding; + doc.DocumentElement.Attributes.Append (xa); + } + xa = doc.CreateAttribute ("xmlns"); + xa.Value = xmldsig; + doc.DocumentElement.Attributes.Append (xa); + } + return doc.DocumentElement; + } + + public void LoadXml (XmlElement value) + { + if (value == null) + throw new ArgumentNullException ("value"); + + if ((value.LocalName == "Object") && (value.NamespaceURI == "http://www.w3.org/2000/09/xmldsig#")) { + doc.LoadXml (value.OuterXml); + XmlAttribute xa = value.Attributes ["Id"]; + id = ((xa != null) ? xa.InnerText : null); + xa = value.Attributes ["MimeType"]; + mimeType = ((xa != null) ? xa.InnerText : null); + xa = value.Attributes ["Encoding"]; + encoding = ((xa != null) ? xa.InnerText : null); + } + else + doc.LoadXml (value.OuterXml); + } +} + +}
\ No newline at end of file diff --git a/mcs/class/System.Security/System.Security.Cryptography.Xml/KeyInfo.cs b/mcs/class/System.Security/System.Security.Cryptography.Xml/KeyInfo.cs new file mode 100644 index 00000000000..9ce4bc4162f --- /dev/null +++ b/mcs/class/System.Security/System.Security.Cryptography.Xml/KeyInfo.cs @@ -0,0 +1,136 @@ +// +// KeyInfo.cs - Xml Signature KeyInfo implementation +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002 Motus Technologies Inc. (http://www.motus.com) +// + +using System.Collections; +using System.Text; +using System.Xml; + +namespace System.Security.Cryptography.Xml { + +public class KeyInfo : IEnumerable { + + static private string xmldsig = "http://www.w3.org/2000/09/xmldsig#"; + + private ArrayList Info; + private string id; + + public KeyInfo() + { + Info = new ArrayList (); + } + + public int Count { + get { return Info.Count; } + } + + public string Id { + get { return id; } + set { id = value; } + } + + public void AddClause (KeyInfoClause clause) + { + Info.Add (clause); + } + + public IEnumerator GetEnumerator () + { + return Info.GetEnumerator (); + } + + public IEnumerator GetEnumerator (Type requestedObjectType) + { + // Build a new ArrayList... + ArrayList TypeList = new ArrayList (); + IEnumerator e = Info.GetEnumerator (); + while (true) { + // ...with all object of specified type... + if ((e.Current).GetType().Equals (requestedObjectType)) + TypeList.Add (e.Current); + if (!e.MoveNext ()) + break; + } + // ...and return its enumerator + return TypeList.GetEnumerator (); + } + + public XmlElement GetXml () + { + StringBuilder sb = new StringBuilder (); + sb.Append ("<KeyInfo xmlns=\""); + sb.Append (xmldsig); + sb.Append ("\" />"); + + XmlDocument doc = new XmlDocument (); + doc.LoadXml (sb.ToString ()); + // we add References afterward so we don't end up with extraneous + // xmlns="..." in each reference elements. + foreach (KeyInfoClause kic in Info) { + XmlNode xn = kic.GetXml (); + XmlNode newNode = doc.ImportNode (xn, true); + doc.DocumentElement.AppendChild (newNode); + } + return doc.DocumentElement; + } + + public void LoadXml (XmlElement value) + { + if (value == null) + throw new ArgumentNullException ("value"); + + if ((value.LocalName == "KeyInfo") && (value.NamespaceURI == xmldsig)) { + foreach (XmlNode n in value.ChildNodes) { + KeyInfoClause kic = null; + if (n is XmlWhitespace) + continue; + + switch (n.LocalName) { + case "KeyValue": + XmlNodeList xnl = n.ChildNodes; + if (xnl.Count > 0) { + // we must now treat the whitespace ! + foreach (XmlNode m in xnl) { + switch (m.LocalName) { + case "DSAKeyValue": + kic = (KeyInfoClause) new DSAKeyValue (); + break; + case "RSAKeyValue": + kic = (KeyInfoClause) new RSAKeyValue (); + break; + } + } + } + break; + case "KeyName": + kic = (KeyInfoClause) new KeyInfoName (); + break; + case "RetrievalMethod": + kic = (KeyInfoClause) new KeyInfoRetrievalMethod (); + break; + case "X509Data": + kic = (KeyInfoClause) new KeyInfoX509Data (); + break; + case "RSAKeyValue": + kic = (KeyInfoClause) new RSAKeyValue (); + break; + default: + kic = (KeyInfoClause) new KeyInfoNode (); + break; + } + + if (kic != null) { + kic.LoadXml ((XmlElement) n); + AddClause (kic); + } + } + } + } +} + +} diff --git a/mcs/class/System.Security/System.Security.Cryptography.Xml/KeyInfoClause.cs b/mcs/class/System.Security/System.Security.Cryptography.Xml/KeyInfoClause.cs new file mode 100644 index 00000000000..a4851412e5f --- /dev/null +++ b/mcs/class/System.Security/System.Security.Cryptography.Xml/KeyInfoClause.cs @@ -0,0 +1,23 @@ +// +// KeyInfoClause.cs - Abstract KeyInfoClause implementation for XML Signature +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002 Motus Technologies Inc. (http://www.motus.com) +// + +using System.Xml; + +namespace System.Security.Cryptography.Xml { + +public abstract class KeyInfoClause { + + public KeyInfoClause () {} + + public abstract XmlElement GetXml (); + + public abstract void LoadXml (XmlElement element); +} + +}
\ No newline at end of file diff --git a/mcs/class/System.Security/System.Security.Cryptography.Xml/KeyInfoName.cs b/mcs/class/System.Security/System.Security.Cryptography.Xml/KeyInfoName.cs new file mode 100644 index 00000000000..4537e3dc631 --- /dev/null +++ b/mcs/class/System.Security/System.Security.Cryptography.Xml/KeyInfoName.cs @@ -0,0 +1,50 @@ +// +// KeyInfoName.cs - KeyInfoName implementation for XML Signature +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002 Motus Technologies Inc. (http://www.motus.com) +// + +using System.Text; +using System.Xml; + +namespace System.Security.Cryptography.Xml { + +public class KeyInfoName : KeyInfoClause { + + private string Name; + + public KeyInfoName() {} + + public string Value { + get { return Name; } + set { Name = value; } + } + + public override XmlElement GetXml () + { + StringBuilder sb = new StringBuilder (); + sb.Append ("<KeyName xmlns=\"http://www.w3.org/2000/09/xmldsig#\">"); + sb.Append (Name); + sb.Append ("</KeyName>"); + + XmlDocument doc = new XmlDocument (); + doc.LoadXml(sb.ToString ()); + return doc.DocumentElement; + } + + public override void LoadXml (XmlElement value) + { + if (value == null) + throw new ArgumentNullException (); + + if ((value.LocalName == "KeyName") && (value.NamespaceURI == "http://www.w3.org/2000/09/xmldsig#")) + Name = value.InnerXml; + else + Name = null; + } +} + +}
\ No newline at end of file diff --git a/mcs/class/System.Security/System.Security.Cryptography.Xml/KeyInfoNode.cs b/mcs/class/System.Security/System.Security.Cryptography.Xml/KeyInfoNode.cs new file mode 100644 index 00000000000..4e0eef0199f --- /dev/null +++ b/mcs/class/System.Security/System.Security.Cryptography.Xml/KeyInfoNode.cs @@ -0,0 +1,43 @@ +// +// KeyInfoNode.cs - KeyInfoNode implementation for XML Signature +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002 Motus Technologies Inc. (http://www.motus.com) +// + +using System.Text; +using System.Xml; + +namespace System.Security.Cryptography.Xml { + +public class KeyInfoNode : KeyInfoClause { + + private XmlElement Node; + + public KeyInfoNode () {} + + public KeyInfoNode (XmlElement node) + { + LoadXml (node); + } + + public XmlElement Value { + get { return Node; } + set { Node = value; } + } + + public override XmlElement GetXml () + { + return Node; + } + + // LAMESPEC: No ArgumentNullException is thrown if value == null + public override void LoadXml (XmlElement value) + { + Node = value; + } +} + +}
\ No newline at end of file diff --git a/mcs/class/System.Security/System.Security.Cryptography.Xml/KeyInfoRetrievalMethod.cs b/mcs/class/System.Security/System.Security.Cryptography.Xml/KeyInfoRetrievalMethod.cs new file mode 100644 index 00000000000..ae7c2aa268c --- /dev/null +++ b/mcs/class/System.Security/System.Security.Cryptography.Xml/KeyInfoRetrievalMethod.cs @@ -0,0 +1,60 @@ +// +// KeyInfoRetrievalMethod.cs - KeyInfoRetrievalMethod implementation for XML Signature +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002 Motus Technologies Inc. (http://www.motus.com) +// + +using System.Text; +using System.Xml; + +namespace System.Security.Cryptography.Xml { + +public class KeyInfoRetrievalMethod : KeyInfoClause { + + private string URI; + + public KeyInfoRetrievalMethod () {} + + public KeyInfoRetrievalMethod (string strUri) + { + URI = strUri; + } + + public string Uri { + get { return URI; } + set { URI = value; } + } + + public override XmlElement GetXml () + { + StringBuilder sb = new StringBuilder (); + sb.Append ("<RetrievalElement "); + if (URI != null) { + sb.Append ("URI=\""); + sb.Append (URI); + sb.Append ("\" "); + } + sb.Append ("xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />"); + + XmlDocument doc = new XmlDocument (); + doc.LoadXml(sb.ToString ()); + return doc.DocumentElement; + } + + public override void LoadXml (XmlElement value) + { + if (value == null) + throw new ArgumentNullException (); + + if ((value.LocalName == "RetrievalElement") && (value.NamespaceURI == "http://www.w3.org/2000/09/xmldsig#")) { + URI = value.Attributes["URI"].Value; + } + else + URI = ""; // not null - so we return URI="" as attribute !!! + } +} + +}
\ No newline at end of file diff --git a/mcs/class/System.Security/System.Security.Cryptography.Xml/KeyInfoX509Data.cs b/mcs/class/System.Security/System.Security.Cryptography.Xml/KeyInfoX509Data.cs new file mode 100644 index 00000000000..09a742fe739 --- /dev/null +++ b/mcs/class/System.Security/System.Security.Cryptography.Xml/KeyInfoX509Data.cs @@ -0,0 +1,214 @@ +// +// KeyInfoX509Data.cs - KeyInfoX509Data implementation for XML Signature +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002 Motus Technologies Inc. (http://www.motus.com) +// + +using System.Collections; +using System.Security.Cryptography.X509Certificates; +using System.Text; +using System.Xml; + +namespace System.Security.Cryptography.Xml { + +// FIXME: framework class isn't documented so compatibility isn't assured! +internal class IssuerSerial { + public string Issuer; + public string Serial; + + public IssuerSerial (string issuer, string serial) + { + Issuer = issuer; + Serial = serial; + } +} + +public class KeyInfoX509Data : KeyInfoClause { + + private byte[] x509crl; + private ArrayList IssuerSerialList; + private ArrayList SubjectKeyIdList; + private ArrayList SubjectNameList; + private ArrayList X509CertificateList; + + public KeyInfoX509Data () + { + IssuerSerialList = new ArrayList (); + SubjectKeyIdList = new ArrayList (); + SubjectNameList = new ArrayList (); + X509CertificateList = new ArrayList (); + } + + public KeyInfoX509Data (byte[] rgbCert) : this () + { + AddCertificate (new X509Certificate (rgbCert)); + } + + public KeyInfoX509Data (X509Certificate cert) : this () + { + AddCertificate (cert); + } + + public ArrayList Certificates { + get { return X509CertificateList; } + } + + public byte[] CRL { + get { return x509crl; } + set { x509crl = value; } + } + + public ArrayList IssuerSerials { + get { return IssuerSerialList; } + } + + public ArrayList SubjectKeyIds { + get { return SubjectKeyIdList; } + } + + public ArrayList SubjectNames { + get { return SubjectNameList; } + } + + public void AddCertificate (X509Certificate certificate) + { + X509CertificateList.Add (certificate); + } + + public void AddIssuerSerial (string issuerName, string serialNumber) + { + IssuerSerial isser = new IssuerSerial (issuerName, serialNumber); + IssuerSerialList.Add (isser); + } + + public void AddSubjectKeyId (byte[] subjectKeyId) + { + SubjectKeyIdList.Add (subjectKeyId); + } + + public void AddSubjectName (string subjectName) + { + SubjectNameList.Add (subjectName); + } + + public override XmlElement GetXml () + { + // sanity check + int count = IssuerSerialList.Count + SubjectKeyIdList.Count + SubjectNameList.Count + X509CertificateList.Count; + if ((x509crl == null) && (count == 0)) + throw new CryptographicException ("value"); + + StringBuilder sb = new StringBuilder (); + sb.Append ("<X509Data xmlns=\"http://www.w3.org/2000/09/xmldsig#\">"); + // <X509IssuerSerial> + if (IssuerSerialList.Count > 0) { + sb.Append ("<X509IssuerSerial>"); + foreach (IssuerSerial iser in IssuerSerialList) { + sb.Append ("<X509IssuerName>"); + sb.Append (iser.Issuer); + sb.Append ("</X509IssuerName>"); + sb.Append ("<X509SerialNumber>"); + sb.Append (iser.Serial); + sb.Append ("</X509SerialNumber>"); + } + sb.Append ("</X509IssuerSerial>"); + } + // <X509SKI> + if (SubjectKeyIdList.Count > 0) { + foreach (byte[] skid in SubjectKeyIdList) { + sb.Append ("<X509SKI>"); + sb.Append (Convert.ToBase64String (skid)); + sb.Append ("</X509SKI>"); + } + } + // <X509SubjectName> + if (SubjectNameList.Count > 0) { + foreach (string subject in SubjectNameList) { + sb.Append ("<X509SubjectName>"); + sb.Append (subject); + sb.Append ("</X509SubjectName>"); + } + } + // <X509Certificate> + if (X509CertificateList.Count > 0) { + foreach (X509Certificate x509 in X509CertificateList) { + sb.Append ("<X509Certificate>"); + sb.Append (Convert.ToBase64String (x509.GetRawCertData ())); + sb.Append ("</X509Certificate>"); + } + } + // only one <X509CRL> + if (x509crl != null) { + sb.Append ("<X509CRL>"); + sb.Append (Convert.ToBase64String (x509crl)); + sb.Append ("</X509CRL>"); + } + sb.Append ("</X509Data>"); + + XmlDocument doc = new XmlDocument (); + doc.LoadXml(sb.ToString ()); + return doc.DocumentElement; + } + + public override void LoadXml (XmlElement element) + { + if (element == null) + throw new ArgumentNullException ("element"); + + IssuerSerialList.Clear (); + SubjectKeyIdList.Clear (); + SubjectNameList.Clear (); + X509CertificateList.Clear (); + x509crl = null; + + string ns = "http://www.w3.org/2000/09/xmldsig#"; + if ((element.LocalName == "X509Data") && (element.NamespaceURI == ns)) { + XmlNodeList xnl = null; + // <X509IssuerSerial> + xnl = element.GetElementsByTagName ("X509IssuerSerial", ns); + if (xnl != null) { + for (int i=0; i < xnl.Count; i++) { + XmlElement xel = (XmlElement) xnl[i]; + XmlNodeList issuer = xel.GetElementsByTagName ("X509IssuerName", ns); + XmlNodeList serial = xel.GetElementsByTagName ("X509SerialNumber", ns); + AddIssuerSerial (issuer[0].InnerText, serial[0].InnerText); + } + } + // <X509SKI> + xnl = element.GetElementsByTagName ("X509SKI", ns); + if (xnl != null) { + for (int i=0; i < xnl.Count; i++) { + byte[] skid = Convert.FromBase64String (xnl[i].InnerXml); + AddSubjectKeyId (skid); + } + } + // <X509SubjectName> + xnl = element.GetElementsByTagName ("X509SubjectName", ns); + if (xnl != null) { + for (int i=0; i < xnl.Count; i++) { + AddSubjectName (xnl[i].InnerXml); + } + } + // <X509Certificate> + xnl = element.GetElementsByTagName ("X509Certificate", ns); + if (xnl != null) { + for (int i=0; i < xnl.Count; i++) { + byte[] cert = Convert.FromBase64String (xnl[i].InnerXml); + AddCertificate (new X509Certificate (cert)); + } + } + // only one <X509CRL> + xnl = element.GetElementsByTagName ("X509CRL", ns); + if ((xnl != null) && (xnl.Count > 0)) { + x509crl = Convert.FromBase64String (xnl[0].InnerXml); + } + } + else + throw new CryptographicException ("element"); + } +} + +}
\ No newline at end of file diff --git a/mcs/class/System.Security/System.Security.Cryptography.Xml/RSAKeyValue.cs b/mcs/class/System.Security/System.Security.Cryptography.Xml/RSAKeyValue.cs new file mode 100644 index 00000000000..20c40c8506a --- /dev/null +++ b/mcs/class/System.Security/System.Security.Cryptography.Xml/RSAKeyValue.cs @@ -0,0 +1,58 @@ +// +// RSAKeyValue.cs - RSAKeyValue implementation for XML Signature +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002 Motus Technologies Inc. (http://www.motus.com) +// + +using System.Text; +using System.Xml; + +namespace System.Security.Cryptography.Xml { + +public class RSAKeyValue : KeyInfoClause { + + private RSA rsa; + + public RSAKeyValue () + { + rsa = RSA.Create (); + } + + public RSAKeyValue (RSA key) + { + rsa = key; + } + + public RSA Key { + get { return rsa; } + set { rsa = value; } + } + + public override XmlElement GetXml () + { + StringBuilder sb = new StringBuilder (); + sb.Append ("<KeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\">"); + sb.Append (rsa.ToXmlString (false)); + sb.Append ("</KeyValue>"); + + XmlDocument doc = new XmlDocument (); + doc.LoadXml(sb.ToString ()); + return doc.DocumentElement; + } + + public override void LoadXml (XmlElement value) + { + if (value == null) + throw new ArgumentNullException (); + + if ((value.LocalName == "KeyValue") && (value.NamespaceURI == "http://www.w3.org/2000/09/xmldsig#")) + rsa.FromXmlString (value.InnerXml); + else + throw new CryptographicException ("value"); + } +} + +}
\ No newline at end of file diff --git a/mcs/class/System.Security/System.Security.Cryptography.Xml/Reference.cs b/mcs/class/System.Security/System.Security.Cryptography.Xml/Reference.cs new file mode 100644 index 00000000000..4dde3c48a0f --- /dev/null +++ b/mcs/class/System.Security/System.Security.Cryptography.Xml/Reference.cs @@ -0,0 +1,209 @@ +// +// Reference.cs - Reference implementation for XML Signature +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002 Motus Technologies Inc. (http://www.motus.com) +// + +using System.Collections; +using System.IO; +using System.Text; +using System.Xml; + +namespace System.Security.Cryptography.Xml { + +// http://www.w3.org/TR/2002/REC-xmldsig-core-20020212/Overview.html#sec-Reference +public class Reference { + + private TransformChain chain; + private string digestMethod; + private byte[] digestValue; + private string id; + private string uri; + private string type; + private HashAlgorithm hash; + + static private string xmldsig = "http://www.w3.org/2000/09/xmldsig#"; + static private string sha1 = xmldsig + "sha1"; + + public Reference () + { + chain = new TransformChain (); + digestMethod = sha1; + } + + public Reference (Stream stream) : this () + { + } + + public Reference (string uri) : this () + { + this.uri = uri; + } + + // default to SHA1 + public string DigestMethod { + get { return digestMethod; } + set { digestMethod = value; } + } + + public byte[] DigestValue { + get { return digestValue; } + set { digestValue = value; } + } + + public string Id { + get { return id; } + set { id = value; } + } + + public TransformChain TransformChain { + get { return chain; } + } + + public string Type { + get { return type; } + set { type = value; } + } + + public string Uri { + get { return uri; } + set { uri = value; } + } + + public void AddTransform (Transform transform) + { + chain.Add (transform); + } + + public XmlElement GetXml () + { + if (digestMethod == null) + throw new CryptographicException ("DigestMethod"); + if (digestValue == null) + throw new NullReferenceException ("DigestValue"); + + StringBuilder sb = new StringBuilder (); + sb.Append ("<Reference"); + if (id != null) { + sb.Append (" Id=\""); + sb.Append (id); + sb.Append ("\""); + } + if (uri != null) { + sb.Append (" URI=\""); + sb.Append (uri); + sb.Append ("\""); + } + if (type != null) { + sb.Append (" Type=\""); + sb.Append (type); + sb.Append ("\""); + } + sb.Append (" xmlns=\""); + sb.Append (xmldsig); + sb.Append ("\" >"); + + if (chain.Count > 0) { + sb.Append ("<Transforms>"); + sb.Append ("</Transforms>"); + } + + sb.Append ("<DigestMethod Algorithm=\""); + sb.Append (digestMethod); + sb.Append ("\" />"); + sb.Append ("<DigestValue>"); + sb.Append (Convert.ToBase64String (digestValue)); + sb.Append ("</DigestValue>"); + sb.Append ("</Reference>"); + + XmlDocument doc = new XmlDocument (); + doc.LoadXml (sb.ToString ()); + + if (chain.Count > 0) { + XmlNodeList xnl = doc.GetElementsByTagName ("Transforms"); + foreach (Transform t in chain) { + XmlNode xn = t.GetXml (); + XmlNode newNode = doc.ImportNode (xn, true); + xnl[0].AppendChild (newNode); + } + } + + return doc.DocumentElement; + } + + 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; + } + + // note: we do NOT return null -on purpose- if attribute isn't found + 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 == "Reference") && (value.NamespaceURI == xmldsig)) { + id = GetAttribute (value, "Id"); + uri = GetAttribute (value, "URI"); + type = GetAttribute (value, "Type"); + // Note: order is important for validations + XmlNodeList xnl = value.GetElementsByTagName ("Transform"); + if ((xnl != null) && (xnl.Count > 0)) { + Transform t = null; + foreach (XmlNode xn in xnl) { + string a = GetAttribute ((XmlElement)xn, "Algorithm"); + switch (a) { + case "http://www.w3.org/2000/09/xmldsig#base64": + t = new XmlDsigBase64Transform (); + break; + case "http://www.w3.org/TR/2001/REC-xml-c14n-20010315": + t = new XmlDsigC14NTransform (); + break; + case "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments": + t = new XmlDsigC14NWithCommentsTransform (); + break; + case "http://www.w3.org/2000/09/xmldsig#enveloped-signature": + t = new XmlDsigEnvelopedSignatureTransform (); + break; + case "http://www.w3.org/TR/1999/REC-xpath-19991116": + t = new XmlDsigXPathTransform (); + break; + case "http://www.w3.org/TR/1999/REC-xslt-19991116": + t = new XmlDsigXsltTransform (); + break; + default: + throw new NotSupportedException (); + } + AddTransform (t); + } + } + // get DigestMethod + DigestMethod = GetAttributeFromElement (value, "Algorithm", "DigestMethod"); + // get DigestValue + xnl = value.GetElementsByTagName ("DigestValue"); + if ((xnl != null) && (xnl.Count > 0)) { + DigestValue = Convert.FromBase64String (xnl[0].InnerText); + } + } + else + throw new CryptographicException (); + } +} + +} diff --git a/mcs/class/System.Security/System.Security.Cryptography.Xml/Signature.cs b/mcs/class/System.Security/System.Security.Cryptography.Xml/Signature.cs new file mode 100644 index 00000000000..1712a31f5b1 --- /dev/null +++ b/mcs/class/System.Security/System.Security.Cryptography.Xml/Signature.cs @@ -0,0 +1,171 @@ +// +// Signature.cs - Signature implementation for XML Signature +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002 Motus Technologies Inc. (http://www.motus.com) +// + +using System.Collections; +using System.Security.Cryptography; +using System.Text; +using System.Xml; + +namespace System.Security.Cryptography.Xml { + +public class Signature { + + private ArrayList list; + private SignedInfo info; + private KeyInfo key; + private string id; + private byte[] signature; + + static private string xmldsig = "http://www.w3.org/2000/09/xmldsig#"; + + public Signature() + { + list = new ArrayList (); + } + + public string Id { + get { return id; } + set { id = value; } + } + + public KeyInfo KeyInfo { + get { return key; } + set { key = value; } + } + + public IList ObjectList { + get { return list; } + set { list = ArrayList.Adapter (value); } + } + + public byte[] SignatureValue { + get { return signature; } + set { signature = value; } + } + + public SignedInfo SignedInfo { + get { return info; } + set { info = value; } + } + + public void AddObject (DataObject dataObject) + { + list.Add (dataObject); + } + + public XmlElement GetXml () + { + if (info == null) + throw new CryptographicException ("SignedInfo"); + if (signature == null) + throw new CryptographicException ("SignatureValue"); + + StringBuilder sb = new StringBuilder (); + sb.Append ("<Signature"); + if (id != null) { + sb.Append (" Id = \""); + sb.Append (id); + sb.Append ("\""); + } + sb.Append (" xmlns=\""); + sb.Append (xmldsig); +/* if (info != null) { + sb.Append ("\">"); + sb.Append (info.GetXml ().OuterXml); + sb.Append ("</Signature>"); + } + else*/ + sb.Append ("\" />"); + + XmlDocument doc = new XmlDocument (); + doc.LoadXml (sb.ToString ()); + + XmlNode xn = null; + XmlNode newNode = null; + + if (info != null) { + // this adds the xmlns=xmldsig + xn = info.GetXml (); + newNode = doc.ImportNode (xn, true); + doc.DocumentElement.AppendChild (newNode); + } + + if (signature != null) { + XmlElement sv = doc.CreateElement ("SignatureValue", xmldsig); + sv.InnerText = Convert.ToBase64String (signature); + doc.DocumentElement.AppendChild (sv); + } + + if (key != null) { + xn = key.GetXml (); + newNode = doc.ImportNode (xn, true); + doc.DocumentElement.AppendChild (newNode); + } + + if (list.Count > 0) { + foreach (DataObject obj in list) { + xn = obj.GetXml (); + newNode = doc.ImportNode (xn, true); + doc.DocumentElement.AppendChild (newNode); + } + } + + return doc.DocumentElement; + } + + 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 == "Signature") && (value.NamespaceURI == xmldsig)) { + id = GetAttribute (value, "Id"); + + XmlNodeList xnl = value.GetElementsByTagName ("SignedInfo"); + if ((xnl != null) && (xnl.Count == 1)) { + info = new SignedInfo (); + info.LoadXml ((XmlElement) xnl[0]); + } + + xnl = value.GetElementsByTagName ("SignatureValue"); + if ((xnl != null) && (xnl.Count == 1)) { + signature = Convert.FromBase64String (xnl[0].InnerText); + } + + xnl = value.GetElementsByTagName ("KeyInfo"); + if ((xnl != null) && (xnl.Count == 1)) { + key = new KeyInfo (); + key.LoadXml ((XmlElement) xnl[0]); + } + + xnl = value.GetElementsByTagName ("Object"); + if ((xnl != null) && (xnl.Count > 0)) { + foreach (XmlNode xn in xnl) { + DataObject obj = new DataObject (); + obj.LoadXml ((XmlElement) xn); + AddObject (obj); + } + } + } + + // if invalid + if (info == null) + throw new CryptographicException ("SignedInfo"); + if (signature == null) + throw new CryptographicException ("SignatureValue"); + } +} + +}
\ No newline at end of file diff --git a/mcs/class/System.Security/System.Security.Cryptography.Xml/SignedInfo.cs b/mcs/class/System.Security/System.Security.Cryptography.Xml/SignedInfo.cs new file mode 100644 index 00000000000..a5d4b2b4e91 --- /dev/null +++ b/mcs/class/System.Security/System.Security.Cryptography.Xml/SignedInfo.cs @@ -0,0 +1,182 @@ +// +// SignedInfo.cs - SignedInfo implementation for XML Signature +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002 Motus Technologies Inc. (http://www.motus.com) +// + +using System.Collections; +using System.Text; +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; + + static private string xmldsig = "http://www.w3.org/2000/09/xmldsig#"; + + public SignedInfo() + { + references = new ArrayList (); + c14nMethod = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315"; + } + + public string CanonicalizationMethod { + get { return c14nMethod; } + set { c14nMethod = value; } + } + + // documented as not supported (and throwing exception) + public int Count { + get { throw new NotSupportedException (); } + } + + public string Id { + get { return id; } + set { 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 (); } + } + + public ArrayList References { + get { return references; } + } + + public string SignatureLength { + get { return signatureLength; } + set { signatureLength = value; } + } + + public string SignatureMethod { + get { return signatureMethod; } + set { 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 (signatureMethod == null) + throw new CryptographicException ("SignatureMethod"); + if (references.Count == 0) + throw new CryptographicException ("References empty"); + + StringBuilder sb = new StringBuilder (); + sb.Append ("<SignedInfo"); + if (id != null) { + sb.Append (" Id=\""); + sb.Append (id); + sb.Append ("\""); + } + sb.Append (" xmlns=\""); + sb.Append (xmldsig); + sb.Append ("\">"); + if (c14nMethod != null) { + sb.Append ("<CanonicalizationMethod Algorithm=\""); + sb.Append (c14nMethod); + sb.Append ("\" />"); + } + if (signatureMethod != null) { + sb.Append ("<SignatureMethod Algorithm=\""); + sb.Append (signatureMethod); + if (signatureLength != null) { + sb.Append ("\">"); + sb.Append ("<HMACOutputLength>"); + sb.Append (signatureLength); + sb.Append ("</HMACOutputLength>"); + sb.Append ("</SignatureMethod>"); + } + else + sb.Append ("\" />"); + } + sb.Append ("</SignedInfo>"); + + XmlDocument doc = new XmlDocument (); + doc.LoadXml (sb.ToString ()); + // 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 = doc.ImportNode (xn, true); + doc.DocumentElement.AppendChild (newNode); + } + + return doc.DocumentElement; + } + + 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); + } + + public void LoadXml (XmlElement value) + { + if (value == null) + throw new ArgumentNullException ("value"); + + if ((value.LocalName == "SignedInfo") && (value.NamespaceURI == xmldsig)) { + id = GetAttribute (value, "Id"); + c14nMethod = GetAttributeFromElement (value, "Algorithm", "CanonicalizationMethod"); + signatureMethod = GetAttributeFromElement (value, "Algorithm", "SignatureMethod"); + // signatureLength for HMAC + XmlNodeList xnl = value.GetElementsByTagName ("Reference"); + foreach (XmlNode xn in xnl) { + Reference r = new Reference (); + r.LoadXml ((XmlElement) xn); + AddReference (r); + } + } + else + throw new CryptographicException (); + } +} + +} diff --git a/mcs/class/System.Security/System.Security.Cryptography.Xml/SignedXml.cs b/mcs/class/System.Security/System.Security.Cryptography.Xml/SignedXml.cs new file mode 100644 index 00000000000..fa054bc0b56 --- /dev/null +++ b/mcs/class/System.Security/System.Security.Cryptography.Xml/SignedXml.cs @@ -0,0 +1,354 @@ +// +// SignedXml.cs - SignedXml implementation for XML Signature +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002 Motus Technologies Inc. (http://www.motus.com) +// + +using System.Collections; +using System.IO; +using System.Security.Cryptography; +using System.Text; +using System.Xml; + +namespace System.Security.Cryptography.Xml { + +public class SignedXml { + + private Signature signature; + private AsymmetricAlgorithm key; + private string keyName; + private XmlDocument envdoc; + + public SignedXml () + { + signature = new Signature (); + signature.SignedInfo = new SignedInfo (); + } + + public SignedXml (XmlDocument document) + { + signature = new Signature (); + signature.SignedInfo = new SignedInfo (); + envdoc = document; + } + + public SignedXml (XmlElement elem) : this () + { + if (elem == null) + throw new ArgumentNullException ("elem"); + signature = new Signature (); + signature.SignedInfo = new SignedInfo (); + } + + public const string XmlDsigCanonicalizationUrl = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315"; + public const string XmlDsigCanonicalizationWithCommentsUrl = XmlDsigCanonicalizationUrl + "#WithComments"; + public const string XmlDsigNamespaceUrl = "http://www.w3.org/2000/09/xmldsig#"; + public const string XmlDsigDSAUrl = XmlDsigNamespaceUrl + "dsa-sha1"; + public const string XmlDsigHMACSHA1Url = XmlDsigNamespaceUrl + "hmac-sha1"; + public const string XmlDsigMinimalCanonicalizationUrl = XmlDsigNamespaceUrl + "minimal"; + public const string XmlDsigRSASHA1Url = XmlDsigNamespaceUrl + "rsa-sha1"; + public const string XmlDsigSHA1Url = XmlDsigNamespaceUrl + "sha1"; + + public KeyInfo KeyInfo { + get { return signature.KeyInfo; } + set { signature.KeyInfo = value; } + } + + public Signature Signature { + get { return signature; } + } + + public string SignatureLength { + get { return signature.SignedInfo.SignatureLength; } + } + + public string SignatureMethod { + get { return signature.SignedInfo.SignatureMethod; } + } + + public byte[] SignatureValue { + get { return signature.SignatureValue; } + } + + public SignedInfo SignedInfo { + get { return signature.SignedInfo; } + } + + public AsymmetricAlgorithm SigningKey { + get { return key; } + set { key = value; } + } + + public string SigningKeyName { + get { return keyName; } + set { keyName = value; } + } + + public void AddObject (DataObject dataObject) + { + signature.AddObject (dataObject); + } + + public void AddReference (Reference reference) + { + signature.SignedInfo.AddReference (reference); + } + + private Stream ApplyTransform (Transform t, XmlDocument doc) + { + t.LoadInput (doc); + if (t is XmlDsigEnvelopedSignatureTransform) { + XmlDocument d = (XmlDocument) t.GetOutput (); + MemoryStream ms = new MemoryStream (); + d.Save (ms); + return ms; + } + else + return (Stream) t.GetOutput (); + } + + private Stream ApplyTransform (Transform t, Stream s) + { + try { + t.LoadInput (s); + s = (Stream) t.GetOutput (); + } + catch (Exception e) { + string temp = e.ToString (); // stop debugger + } + return s; + } + + [MonoTODO("incomplete")] + private byte[] GetReferenceHash (Reference r) + { + XmlDocument doc = new XmlDocument (); + doc.PreserveWhitespace = true; + if (r.Uri == "") + doc = envdoc; + else { + foreach (DataObject obj in signature.ObjectList) { + if ("#" + obj.Id == r.Uri) { + doc.LoadXml (obj.GetXml ().OuterXml); + break; + } + } + } + + Stream s = null; + if (r.TransformChain.Count > 0) { + foreach (Transform t in r.TransformChain) { + if (s == null) + s = ApplyTransform (t, doc); + else + s = ApplyTransform (t, s); + } + } + else + s = ApplyTransform (new XmlDsigC14NTransform (), doc); + + // TODO: We should reuse the same hash object (when possible) + HashAlgorithm hash = (HashAlgorithm) CryptoConfig.CreateFromName (r.DigestMethod); + return hash.ComputeHash (s); + } + + private void DigestReferences () + { + // we must tell each reference which hash algorithm to use + // before asking for the SignedInfo XML ! + foreach (Reference r in signature.SignedInfo.References) { + // assume SHA-1 if nothing is specified + if (r.DigestMethod == null) + r.DigestMethod = "http://www.w3.org/2000/09/xmldsig#sha1"; + r.DigestValue = GetReferenceHash (r); + } + } + + private Stream SignedInfoTransformed () + { + Transform t = (Transform) CryptoConfig.CreateFromName (signature.SignedInfo.CanonicalizationMethod); + if (t == null) + return null; + + XmlDocument doc = new XmlDocument (); + doc.LoadXml (signature.SignedInfo.GetXml ().OuterXml); + return ApplyTransform (t, doc); + } + + [MonoTODO("Become hash algorithm independant")] + private byte[] Hash () + { + // we must select the hash using ??? SignatureMethod (yuck) + // FIXME: Hardcoded to SHA1 - which is, right now, the only digest defined in XMLDSIG + SHA1 sha = SHA1.Create (); + // get the hash of the C14N SignedInfo element + return sha.ComputeHash (SignedInfoTransformed ()); + } + + public bool CheckSignature () + { + // CryptographicException + if (key == null) + key = GetPublicKey (); + return CheckSignature (key); + } + + private bool CheckReferenceIntegrity () + { + // check digest (hash) for every reference + foreach (Reference r in signature.SignedInfo.References) { + // stop at first broken reference + if (! Compare (r.DigestValue, GetReferenceHash (r))) + return false; + } + return true; + } + + public bool CheckSignature (AsymmetricAlgorithm key) + { + if (key == null) + throw new ArgumentNullException ("key"); + + // Part 1: Are all references digest valid ? + bool result = CheckReferenceIntegrity (); + if (result) { + // Part 2: Is the signature (over SignedInfo) valid ? + byte[] hash = Hash (); + AsymmetricSignatureDeformatter verifier = null; + + if (key is DSA) + verifier = new DSASignatureDeformatter (key); + else if (key is RSA) + verifier = new RSAPKCS1SignatureDeformatter (key); + else + result = false; + + if (verifier != null) { + verifier.SetHashAlgorithm ("SHA1"); + result = verifier.VerifySignature (hash, signature.SignatureValue); + } + } + + return result; + } + + private bool Compare (byte[] expected, byte[] actual) + { + bool result = ((expected != null) && (actual != null)); + if (result) { + int l = expected.Length; + result = (l == actual.Length); + if (result) { + for (int i=0; i < l; i++) { + if (expected[i] != actual[i]) + return false; + } + } + } + return result; + } + + public bool CheckSignature (KeyedHashAlgorithm macAlg) + { + if (macAlg == null) + throw new ArgumentNullException ("macAlg"); + + // Part 1: Are all references digest valid ? + bool result = CheckReferenceIntegrity (); + if (result) { + // Part 2: Is the signature (over SignedInfo) valid ? + byte[] actual = macAlg.ComputeHash (SignedInfoTransformed ()); + result = Compare (signature.SignatureValue, actual); + } + return result; + } + + public bool CheckSignatureReturningKey (out AsymmetricAlgorithm signingKey) + { + // here's the key used for verifying the signature + if (key == null) + key = GetPublicKey (); + signingKey = key; + // we'll find the key if we haven't already + return CheckSignature (key); + } + + public void ComputeSignature () + { + if (key != null) { + // required before hashing + signature.SignedInfo.SignatureMethod = key.SignatureAlgorithm; + DigestReferences (); + + // the hard part - C14Ning the KeyInfo + byte[] hash = Hash (); + AsymmetricSignatureFormatter signer = null; + + // in need for a CryptoConfig factory + if (key is DSA) + signer = new DSASignatureFormatter (key); + else if (key is RSA) + signer = new RSAPKCS1SignatureFormatter (key); + + if (signer != null) { + signer.SetHashAlgorithm ("SHA1"); + signature.SignatureValue = signer.CreateSignature (hash); + } + } + } + + public void ComputeSignature (KeyedHashAlgorithm macAlg) + { + if (macAlg == null) + throw new ArgumentNullException ("macAlg"); + + if (macAlg is HMACSHA1) { + DigestReferences (); + + signature.SignedInfo.SignatureMethod = "http://www.w3.org/2000/09/xmldsig#hmac-sha1"; + signature.SignatureValue = macAlg.ComputeHash (SignedInfoTransformed ()); + } + else + throw new CryptographicException ("unsupported algorithm"); + } + + // is that all ? + public virtual XmlElement GetIdElement (XmlDocument document, string idValue) + { + return document.GetElementById (idValue); + } + + protected virtual AsymmetricAlgorithm GetPublicKey () + { + AsymmetricAlgorithm key = null; + if (signature.KeyInfo != null) { + foreach (KeyInfoClause kic in signature.KeyInfo) { + if (kic is DSAKeyValue) + key = DSA.Create (); + else if (kic is RSAKeyValue) + key = RSA.Create (); + + if (key != null) { + key.FromXmlString (kic.GetXml ().InnerXml); + break; + } + } + } + return key; + } + + public XmlElement GetXml () + { + return signature.GetXml (); + } + + public void LoadXml (XmlElement value) + { + signature.LoadXml (value); + } +} + +} diff --git a/mcs/class/System.Security/System.Security.Cryptography.Xml/TODOAttribute.cs b/mcs/class/System.Security/System.Security.Cryptography.Xml/TODOAttribute.cs new file mode 100644 index 00000000000..0920ce8f92d --- /dev/null +++ b/mcs/class/System.Security/System.Security.Cryptography.Xml/TODOAttribute.cs @@ -0,0 +1,37 @@ +// +// TODOAttribute.cs +// +// Author: +// Ravi Pratap (ravi@ximian.com) +// +// (C) Ximian, Inc. http://www.ximian.com +// + +namespace System { + + /// <summary> + /// The TODO attribute is used to flag all incomplete bits in our class libraries + /// </summary> + /// + /// <remarks> + /// Use this to decorate any element which you think is not complete + /// </remarks> + [AttributeUsage (AttributeTargets.All, AllowMultiple=true)] + public class MonoTODOAttribute : Attribute { + + private string comment; + + public MonoTODOAttribute () + {} + + public MonoTODOAttribute (string comment) + { + this.comment = comment; + } + + public string Comment + { + get { return comment; } + } + } +} diff --git a/mcs/class/System.Security/System.Security.Cryptography.Xml/Transform.cs b/mcs/class/System.Security/System.Security.Cryptography.Xml/Transform.cs new file mode 100644 index 00000000000..32375dd6716 --- /dev/null +++ b/mcs/class/System.Security/System.Security.Cryptography.Xml/Transform.cs @@ -0,0 +1,57 @@ +// +// Transform.cs - Transform implementation for XML Signature +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002 Motus Technologies Inc. (http://www.motus.com) +// + +using System.Text; +using System.Xml; + +namespace System.Security.Cryptography.Xml { + +public abstract class Transform { + + private string algo; + + public Transform () {} + + public string Algorithm { + get { return algo; } + set { algo = value; } + } + + public abstract Type[] InputTypes { + get; + } + + public abstract Type[] OutputTypes { + get; + } + + protected abstract XmlNodeList GetInnerXml (); + + public abstract object GetOutput (); + + public abstract object GetOutput (Type type); + + public XmlElement GetXml () + { + StringBuilder sb = new StringBuilder (); + sb.Append ("<Transform Algorithm=\""); + sb.Append (algo); + sb.Append ("\"/>"); + + XmlDocument doc = new XmlDocument (); + doc.LoadXml (sb.ToString ()); + return doc.DocumentElement; + } + + public abstract void LoadInnerXml (XmlNodeList nodeList); + + public abstract void LoadInput (object obj); +} + +} diff --git a/mcs/class/System.Security/System.Security.Cryptography.Xml/TransformChain.cs b/mcs/class/System.Security/System.Security.Cryptography.Xml/TransformChain.cs new file mode 100644 index 00000000000..04e8696ecdb --- /dev/null +++ b/mcs/class/System.Security/System.Security.Cryptography.Xml/TransformChain.cs @@ -0,0 +1,42 @@ +// +// TransformChain.cs - TransformChain implementation for XML Signature +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002 Motus Technologies Inc. (http://www.motus.com) +// + +using System.Collections; + +namespace System.Security.Cryptography.Xml { + +public class TransformChain { + + private ArrayList chain; + + public TransformChain() + { + chain = new ArrayList (); + } + + public int Count { + get { return chain.Count; } + } + + public Transform this [int index] { + get { return (Transform) chain [index]; } + } + + public void Add (Transform transform) + { + chain.Add (transform); + } + + public IEnumerator GetEnumerator () + { + return chain.GetEnumerator (); + } +} + +} diff --git a/mcs/class/System.Security/System.Security.Cryptography.Xml/XmlDsigBase64Transform.cs b/mcs/class/System.Security/System.Security.Cryptography.Xml/XmlDsigBase64Transform.cs new file mode 100644 index 00000000000..22fde9fd873 --- /dev/null +++ b/mcs/class/System.Security/System.Security.Cryptography.Xml/XmlDsigBase64Transform.cs @@ -0,0 +1,107 @@ +// +// XmlDsigBase64Transform.cs - Base64 Transform implementation for XML Signature +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002 Motus Technologies Inc. (http://www.motus.com) +// + +using System.IO; +using System.Security.Cryptography; +using System.Text; +using System.Xml; + +namespace System.Security.Cryptography.Xml { + +// http://www.w3.org/2000/09/xmldsig#base64 +public class XmlDsigBase64Transform : Transform { + + private Type[] input; + private Type[] output; + private CryptoStream cs; + + public XmlDsigBase64Transform () + { + Algorithm = "http://www.w3.org/2000/09/xmldsig#base64"; + } + + public override Type[] InputTypes { + get { + if (input == null) { + lock (this) { + // this way the result is cached if called multiple time + input = new Type [3]; + input[0] = typeof (System.IO.Stream); + input[1] = typeof (System.Xml.XmlDocument); + input[2] = typeof (System.Xml.XmlNodeList); + } + } + return input; + } + } + + public override Type[] OutputTypes { + get { + if (output == null) { + lock (this) { + // this way the result is cached if called multiple time + output = new Type [1]; + output[0] = typeof (System.IO.Stream); + } + } + return output; + } + } + + protected override XmlNodeList GetInnerXml () + { + return null; // THIS IS DOCUMENTED AS SUCH + } + + public override object GetOutput () + { + return (object) cs; + } + + public override object GetOutput (Type type) + { + if (type != Type.GetType ("System.IO.Stream")) + throw new ArgumentException ("type"); + return GetOutput (); + } + + public override void LoadInnerXml (XmlNodeList nodeList) + { + // documented as not changing the state of the transform + } + + public override void LoadInput (object obj) + { + XmlNodeList xnl = null; + Stream stream = null; + + if (obj is Stream) + stream = (obj as Stream); + else if (obj is XmlDocument) + xnl = (obj as XmlDocument).ChildNodes; + else if (obj is XmlNodeList) + xnl = (XmlNodeList) obj; + + if (xnl != null) { + StringBuilder sb = new StringBuilder (); + foreach (XmlNode xn in xnl) + sb.Append (xn.InnerText); + + UTF8Encoding utf8 = new UTF8Encoding (); + byte[] data = utf8.GetBytes (sb.ToString ()); + stream = new MemoryStream (data); + } + + if (stream != null) + cs = new CryptoStream (stream, new FromBase64Transform (), CryptoStreamMode.Read); + // note: there is no default are other types won't throw an exception + } +} + +} diff --git a/mcs/class/System.Security/System.Security.Cryptography.Xml/XmlDsigC14NTransform.cs b/mcs/class/System.Security/System.Security.Cryptography.Xml/XmlDsigC14NTransform.cs new file mode 100644 index 00000000000..a689f0f118b --- /dev/null +++ b/mcs/class/System.Security/System.Security.Cryptography.Xml/XmlDsigC14NTransform.cs @@ -0,0 +1,110 @@ +// +// XmlDsigC14NTransform.cs - C14N Transform implementation for XML Signature +// http://www.w3.org/TR/xml-c14n +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002 Motus Technologies Inc. (http://www.motus.com) +// + +using System.IO; +using System.Text; +using System.Xml; + +namespace System.Security.Cryptography.Xml { + +public class XmlDsigC14NTransform : Transform { + + private Type[] input; + private Type[] output; + private bool comments; + private Stream s; + + public XmlDsigC14NTransform () + { + Algorithm = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315"; + comments = false; + } + + public XmlDsigC14NTransform (bool includeComments) + { + comments = includeComments; + } + + public override Type[] InputTypes { + get { + if (input == null) { + lock (this) { + // this way the result is cached if called multiple time + input = new Type [3]; + input[0] = typeof (System.IO.Stream); + input[1] = typeof (System.Xml.XmlDocument); + input[2] = typeof (System.Xml.XmlNodeList); + } + } + return input; + } + } + + public override Type[] OutputTypes { + get { + if (output == null) { + lock (this) { + // this way the result is cached if called multiple time + output = new Type [1]; + output[0] = typeof (System.IO.Stream); + } + } + return output; + } + } + + protected override XmlNodeList GetInnerXml () + { + return null; // THIS IS DOCUMENTED AS SUCH + } + + public override object GetOutput () + { + return (object) s; + } + + public override object GetOutput (Type type) + { + if (type == Type.GetType ("Stream")) + return GetOutput (); + throw new ArgumentException ("type"); + } + + public override void LoadInnerXml (XmlNodeList nodeList) + { + // documented as not changing the state of the transform + } + + public override void LoadInput (object obj) + { + XmlNodeList xnl = null; + + if (obj is Stream) + s = (obj as Stream); + else if (obj is XmlDocument) + xnl = (obj as XmlDocument).ChildNodes; + else if (obj is XmlNodeList) + xnl = (XmlNodeList) obj; + + if (xnl != null) { + StringBuilder sb = new StringBuilder (); + foreach (XmlNode xn in xnl) + sb.Append (xn.InnerText); + + UTF8Encoding utf8 = new UTF8Encoding (); + byte[] data = utf8.GetBytes (sb.ToString ()); + s = new MemoryStream (data); + } + + // note: there is no default are other types won't throw an exception + } +} + +} diff --git a/mcs/class/System.Security/System.Security.Cryptography.Xml/XmlDsigC14NWithCommentsTransform.cs b/mcs/class/System.Security/System.Security.Cryptography.Xml/XmlDsigC14NWithCommentsTransform.cs new file mode 100644 index 00000000000..cc32ecf6152 --- /dev/null +++ b/mcs/class/System.Security/System.Security.Cryptography.Xml/XmlDsigC14NWithCommentsTransform.cs @@ -0,0 +1,21 @@ +// +// XmlDsigC14NWithCommentsTransform.cs - +// C14N with comments Transform implementation for XML Signature +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002 Motus Technologies Inc. (http://www.motus.com) +// + +using System.IO; +using System.Xml; + +namespace System.Security.Cryptography.Xml { + +public class XmlDsigC14NWithCommentsTransform : XmlDsigC14NTransform { + + public XmlDsigC14NWithCommentsTransform() : base (true) {} +} + +} diff --git a/mcs/class/System.Security/System.Security.Cryptography.Xml/XmlDsigEnvelopedSignatureTransform.cs b/mcs/class/System.Security/System.Security.Cryptography.Xml/XmlDsigEnvelopedSignatureTransform.cs new file mode 100644 index 00000000000..f67a8579d79 --- /dev/null +++ b/mcs/class/System.Security/System.Security.Cryptography.Xml/XmlDsigEnvelopedSignatureTransform.cs @@ -0,0 +1,90 @@ +// +// XmlDsigEnvelopedSignatureTransform.cs - +// Enveloped Signature Transform implementation for XML Signature +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002 Motus Technologies Inc. (http://www.motus.com) +// + +using System.IO; +using System.Xml; + +namespace System.Security.Cryptography.Xml { + +public class XmlDsigEnvelopedSignatureTransform : Transform { + + private Type[] input; + private Type[] output; + private bool comments; + + public XmlDsigEnvelopedSignatureTransform () + { + comments = false; + } + + public XmlDsigEnvelopedSignatureTransform (bool includeComments) + { + comments = includeComments; + } + + public override Type[] InputTypes { + get { + if (input == null) { + lock (this) { + // this way the result is cached if called multiple time + input = new Type [3]; + input[0] = typeof (System.IO.Stream); + input[1] = typeof (System.Xml.XmlDocument); + input[2] = typeof (System.Xml.XmlNodeList); + } + } + return input; + } + } + + public override Type[] OutputTypes { + get { + if (output == null) { + lock (this) { + // this way the result is cached if called multiple time + output = new Type [2]; + input[0] = typeof (System.Xml.XmlDocument); + input[1] = typeof (System.Xml.XmlNodeList); + } + } + return output; + } + } + + protected override XmlNodeList GetInnerXml () + { + return null; // THIS IS DOCUMENTED AS SUCH + } + + public override object GetOutput() + { +// return (object) new XmlNodeList (); + return null; + } + + public override object GetOutput (Type type) + { + if (type == Type.GetType ("Stream")) + return GetOutput (); + throw new ArgumentException ("type"); + } + + public override void LoadInnerXml (XmlNodeList nodeList) + { + // NO CHANGE + } + + public override void LoadInput (object obj) + { + // if (type.Equals (Stream.GetType ()) + } +} + +} diff --git a/mcs/class/System.Security/System.Security.Cryptography.Xml/XmlDsigXPathTransform.cs b/mcs/class/System.Security/System.Security.Cryptography.Xml/XmlDsigXPathTransform.cs new file mode 100644 index 00000000000..5c5fd963b8b --- /dev/null +++ b/mcs/class/System.Security/System.Security.Cryptography.Xml/XmlDsigXPathTransform.cs @@ -0,0 +1,108 @@ +// +// XmlDsigXPathTransform.cs - +// XmlDsigXPathTransform implementation for XML Signature +// http://www.w3.org/TR/1999/REC-xpath-19991116 +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002 Motus Technologies Inc. (http://www.motus.com) +// + +using System.IO; +using System.Security.Cryptography; +using System.Text; +using System.Xml; + +namespace System.Security.Cryptography.Xml { + +// www.w3.org/TR/xmldsig-core/ +// see Section 6.6.3 of the XMLDSIG specification +public class XmlDsigXPathTransform : Transform { + + private Type[] input; + private Type[] output; + private XmlNodeList xnl; + private XmlNodeList xpathNodes; + + public XmlDsigXPathTransform () + { + } + + public override Type[] InputTypes { + get { + if (input == null) { + lock (this) { + // this way the result is cached if called multiple time + input = new Type [3]; + input[0] = typeof (System.IO.Stream); + input[1] = typeof (System.Xml.XmlDocument); + input[2] = typeof (System.Xml.XmlNodeList); + } + } + return input; + } + } + + public override Type[] OutputTypes { + get { + if (output == null) { + lock (this) { + // this way the result is cached if called multiple time + output = new Type [1]; + output[0] = typeof (System.IO.Stream); + } + } + return output; + } + } + + protected override XmlNodeList GetInnerXml () + { + return xnl; + } + + public override object GetOutput () + { + return xpathNodes; + } + + public override object GetOutput (Type type) + { + if (type != typeof (XmlNodeList)) + throw new ArgumentException ("type"); + return GetOutput (); + } + + public override void LoadInnerXml (XmlNodeList nodeList) + { + if (nodeList == null) + throw new CryptographicException ("nodeList"); + xnl = nodeList; + } + + public override void LoadInput (object obj) + { + XmlNode xn = null; + // possible input: Stream, XmlDocument, and XmlNodeList + if (obj is Stream) { + XmlDocument doc = new XmlDocument (); + doc.Load (obj as Stream); + } + else if (obj is XmlDocument) { + } + else if (obj is XmlNodeList) { + xnl = (XmlNodeList) obj; + } + + if (xn != null) { + string xpath = xn.InnerXml; + // only possible output: XmlNodeList + xpathNodes = xnl[0].SelectNodes (xpath); + } + else + xpathNodes = null; + } +} + +} diff --git a/mcs/class/System.Security/System.Security.Cryptography.Xml/XmlDsigXsltTransform.cs b/mcs/class/System.Security/System.Security.Cryptography.Xml/XmlDsigXsltTransform.cs new file mode 100644 index 00000000000..524541ea67c --- /dev/null +++ b/mcs/class/System.Security/System.Security.Cryptography.Xml/XmlDsigXsltTransform.cs @@ -0,0 +1,121 @@ +// +// XmlDsigEnvelopedSignatureTransform.cs - +// Enveloped Signature Transform implementation for XML Signature +// http://www.w3.org/TR/1999/REC-xslt-19991116 +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002 Motus Technologies Inc. (http://www.motus.com) +// + +using System.IO; +using System.Security.Cryptography; +using System.Xml; +using System.Xml.Xsl; + +namespace System.Security.Cryptography.Xml { + +public class XmlDsigXsltTransform : Transform { + + private Type[] input; + private Type[] output; + private bool comments; + private XmlNodeList xnl; + private CryptoStream cs; + + public XmlDsigXsltTransform () : this (false) {} + + public XmlDsigXsltTransform (bool includeComments) + { + comments = includeComments; + Algorithm = "http://www.w3.org/TR/1999/REC-xslt-19991116"; + } + + public override Type[] InputTypes { + get { + if (input == null) { + lock (this) { + // this way the result is cached if called multiple time + input = new Type [3]; + input[0] = typeof (System.IO.Stream); + input[1] = typeof (System.Xml.XmlDocument); + input[2] = typeof (System.Xml.XmlNodeList); + } + } + return input; + } + } + + public override Type[] OutputTypes { + get { + if (output == null) { + lock (this) { + // this way the result is cached if called multiple time + output = new Type [1]; + output[0] = typeof (System.IO.Stream); + } + } + return output; + } + } + + protected override XmlNodeList GetInnerXml () + { + return xnl; + } + + public override object GetOutput () + { + return (object) cs; + } + + public override object GetOutput (Type type) + { + if (type != Type.GetType ("System.IO.Stream")) + throw new ArgumentException ("type"); + return GetOutput (); + } + + public override void LoadInnerXml (XmlNodeList nodeList) + { + if (nodeList == null) + throw new CryptographicException ("nodeList"); + xnl = nodeList; + } + + [MonoTODO()] + public override void LoadInput (object obj) + { + XslTransform xsl = new XslTransform (); + XmlDocument doc = new XmlDocument (); + Stream stream = null; + + // possible input: Stream, XmlDocument, and XmlNodeList + if (obj is Stream) { + doc.Load (obj as Stream); + xsl.Load (doc); + } + else if (obj is XmlDocument) { + xsl.Load (obj as XmlDocument); + } + else if (obj is XmlNodeList) { +// xnl = (XmlNodeList) obj; +// xsl.Load (obj a); + } + + if (xnl != null) { + stream = new MemoryStream (); + // only possible output: Stream + xsl.Transform (doc, null, stream); + } + + if (stream != null) + cs = new CryptoStream (stream, new FromBase64Transform (), CryptoStreamMode.Read); + else + cs = null; + // note: there is no default are other types won't throw an exception + } +} + +} diff --git a/mcs/class/System.Security/System.Security.build b/mcs/class/System.Security/System.Security.build new file mode 100644 index 00000000000..a2c4b72ca53 --- /dev/null +++ b/mcs/class/System.Security/System.Security.build @@ -0,0 +1,27 @@ +<?xml version="1.0" encoding="iso-8859-1"?> + +<!-- NAnt build file for System.Security.dll --> + +<project name="System.Security" default="build"> + <property name="debug" value="false"/> + + <target name="build"> + <csc target="library" output="../lib/System.Security.dll" debug="${debug}"> + <arg value="/nowarn:0169"/> + <arg value="/noconfig"/> + <arg value="/r:..\lib\System.XML.dll"/> + <sources basedir="."> + <includes name="System.Security.Cryptography.Xml/*.cs"/> + </sources> + </csc> + </target> + + <target name="test" depends="build"> + <nant basedir="Test" target="test"/> + </target> + + <target name="clean"> + <delete file="../lib/System.Security.dll" failonerror="false"/> + <nant basedir="Test" target="clean"/> + </target> +</project> diff --git a/mcs/class/System.Security/Test/AllTests.cs b/mcs/class/System.Security/Test/AllTests.cs new file mode 100644 index 00000000000..a469555a313 --- /dev/null +++ b/mcs/class/System.Security/Test/AllTests.cs @@ -0,0 +1,28 @@ +// +// MonoTests.System.Security.AllTests.cs +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002 Motus Technologies Inc. (http://www.motus.com) +// + +using System; +using NUnit.Framework; + +namespace MonoTests.System.Security { + +public class AllTests : TestCase { + + public AllTests (string name) : base (name) {} + + public static ITest Suite { + get { + TestSuite suite = new TestSuite (); + suite.AddTest (System.Security.Cryptography.Xml.AllTests.Suite); + return suite; + } + } +} + +} diff --git a/mcs/class/System.Security/Test/ChangeLog b/mcs/class/System.Security/Test/ChangeLog new file mode 100644 index 00000000000..8051ae7fb8f --- /dev/null +++ b/mcs/class/System.Security/Test/ChangeLog @@ -0,0 +1,10 @@ +2003-01-17 Sebastien Pouliot <spouliot@videotron.ca> + + * AllTests.cs: Removed System.Security.Cryptography from using. + * System.Security_test.build: Simplified and fixed some issues. + +2002-11-28 Sebastien Pouliot <spouliot@videotron.ca> + + * AllTests.cs: New. Include System.Security.Cryptography.Xml + test suites. + * System.Security_test.build: New. Nant build file for Windows. diff --git a/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/AllTests.cs b/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/AllTests.cs new file mode 100644 index 00000000000..45ff7bd5a74 --- /dev/null +++ b/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/AllTests.cs @@ -0,0 +1,66 @@ +// +// TestSuite.System.Security.Cryptography.Xml.AllTests.cs +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002 Motus Technologies Inc. (http://www.motus.com) +// + +using System; +using System.Security.Cryptography; +using NUnit.Framework; + +namespace MonoTests.System.Security.Cryptography.Xml { + +public class AllTests : TestCase { + + public AllTests (string name) : base (name) {} + + // because most crypto stuff works with byte[] buffers + static public void AssertEquals (string msg, byte[] array1, byte[] array2) + { + if ((array1 == null) && (array2 == null)) + return; + if (array1 == null) + Fail (msg + " -> First array is NULL"); + if (array2 == null) + Fail (msg + " -> Second array is NULL"); + + bool a = (array1.Length == array2.Length); + if (a) { + for (int i = 0; i < array1.Length; i++) { + if (array1 [i] != array2 [i]) { + a = false; + break; + } + } + } + msg += " -> Expected " + BitConverter.ToString (array1, 0); + msg += " is different than " + BitConverter.ToString (array2, 0); + Assert (msg, a); + } + + public static ITest Suite { + get { + TestSuite suite = new TestSuite (); + suite.AddTest (DataObjectTest.Suite); + suite.AddTest (DSAKeyValueTest.Suite); + suite.AddTest (KeyInfoNameTest.Suite); + suite.AddTest (KeyInfoNodeTest.Suite); + suite.AddTest (KeyInfoRetrievalMethodTest.Suite); + suite.AddTest (KeyInfoTest.Suite); + suite.AddTest (KeyInfoX509DataTest.Suite); + suite.AddTest (ReferenceTest.Suite); + suite.AddTest (RSAKeyValueTest.Suite); + suite.AddTest (SignatureTest.Suite); + suite.AddTest (SignedInfoTest.Suite); + suite.AddTest (SignedXmlTest.Suite); + suite.AddTest (TransformChainTest.Suite); + suite.AddTest (XmlDsigBase64TransformTest.Suite); + suite.AddTest (XmlDsigXsltTransformTest.Suite); + return suite; + } + } +} +} diff --git a/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/ChangeLog b/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/ChangeLog new file mode 100644 index 00000000000..0f0b86331d4 --- /dev/null +++ b/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/ChangeLog @@ -0,0 +1,24 @@ +2002-11-28 Sebastien Pouliot <spouliot@videotron.ca> + + * AllTests.cs: Added missing test suites. + +2002-11-20 Sebastien Pouliot <spouliot@videotron.ca> + + * AllTests.cs: New. Added suites for very listed files. + * DataObjectTest.cs: New. Test suite for DataObject. + * DSAKeyValueTest.cs: New. Test suite for DSAKeyValue. + * KeyInfoNameTest.cs: New. Test suite for KeyInfoName. + * KeyInfoNodeTest.cs: New. Test suite for KeyInfoNode. + * KeyInfoRetrievalMethodTest.cs: New. Test suite for DSAKeyValue. + * KeyInfoTest.cs: New. Test suite for KeyInfo. + * KeyInfoX509DataTest.cs: New. Test suite for KeyInfoX509Data. + * ReferenceTest.cs: New. Incomplete test suite for Reference. + * RSAKeyValueTest.cs: New. Test suite for RSAKeyValue. + * SignatureTest.cs: New. Partial test suite for Signature. + * SignedInfoTest.cs: New. Partial test suite for SignedInfo. + * SignedXmlTest.cs: New. Partial test suite for SignedXml. + * TransformChainTest.cs: New. Test suite for TransformChain. + * XmlDsigBase64TransformTest.cs: New. Partial test suite for + XmlDsigBase64Transform + * XmlDsigXsltTransformTest.cs: New. Partial test suite for + XmlDsigXsltTransform. diff --git a/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/DSAKeyValueTest.cs b/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/DSAKeyValueTest.cs new file mode 100644 index 00000000000..28c9b185af2 --- /dev/null +++ b/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/DSAKeyValueTest.cs @@ -0,0 +1,95 @@ +// +// DSAKeyValueTest.cs - NUnit Test Cases for DSAKeyValue +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002 Motus Technologies Inc. (http://www.motus.com) +// + +using NUnit.Framework; +using System; +using System.Security.Cryptography; +using System.Security.Cryptography.Xml; +using System.Xml; + +namespace MonoTests.System.Security.Cryptography.Xml { + +public class DSAKeyValueTest : TestCase { + + public DSAKeyValueTest () : base ("System.Security.Cryptography.Xml.DSAKeyValue testsuite") {} + public DSAKeyValueTest (string name) : base (name) {} + + protected override void SetUp () {} + + protected override void TearDown () {} + + public static ITest Suite { + get { + return new TestSuite (typeof (DSAKeyValueTest)); + } + } + + public void TestGeneratedKey () + { + DSAKeyValue dsa1 = new DSAKeyValue (); + AssertNotNull ("Key", dsa1.Key); + XmlElement xmlkey = dsa1.GetXml (); + + DSAKeyValue dsa2 = new DSAKeyValue (); + dsa2.LoadXml (xmlkey); + + Assert ("dsa1==dsa2", (dsa1.GetXml ().OuterXml) == (dsa2.GetXml ().OuterXml)); + + DSA key = dsa1.Key; + DSAKeyValue dsa3 = new DSAKeyValue (key); + Assert ("dsa3==dsa1", (dsa3.GetXml ().OuterXml) == (dsa1.GetXml ().OuterXml)); + Assert ("dsa3==dsa2", (dsa3.GetXml ().OuterXml) == (dsa2.GetXml ().OuterXml)); + } + + public void TestImportKey () + { + string dsaKey = "<KeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><DSAKeyValue><P>xc+QZRWTgr390gzwNXF+WzoepZkvAQvCzfCm+YyXj0KPoeHHeSc5ORzXQw81V+7XJR3gupvlI4F7lW9YC538l+3eqGm8IQlCIS+U+7ICTDOFFKevqsYX0BnjO0vvE4aAtDyxfSOTCOAo1cJ+6G6xgcC1JGIBEYCtg1tH8wUewDE=</P><Q>yyfZb0S/rimXl9ScJ3zIba2oGl8=</Q><G>crLazMg+vgI7u6+Idgi9iTLdRa4fptat3gdY97zcc857+OVdmT+lVRpK3okWpmBbw2wSffU8QltwFf42BVs+/HGUOUo2hNqSSXgzl1i+1frO7/cqooHVcy5WX0xxaIPsKcREPI5pNPj/3g8apTgErLMGsHkFdngwbMed9DArTks=</G><Y>FlAozo17wV/LCMRrtnmMKxVQNpidJVkZNM1/0eR65x8giwPs6yXzJmFT8f2tmPJY2FIOAtp5JYin4xUhwIHF452Gg50wUrjV6WTGkiC+gzLC2fVIyGlVsFecLj6ue7J+MACG+b3NQnxFuT5maQnPnEeuGgjLXfwYsAR1vfU0Gas=</Y><J>+UPMvUPq9Fo6Q1fr2oEYDxfGMMtfdoQmVBxI+TkUYQsReodRzBbnvGV1uPLWTpKKd/uJNUHO/QGb05Cvc6u49/AToDJIyi4e01hTLNCzeQk/Hj19gowb5wkTIjyaH04VyPE5zYoTYfuu3Y3Q</J><Seed>+cvoO7bzdpAwAjnDDApPzBCl6zg=</Seed><PgenCounter>ATM=</PgenCounter></DSAKeyValue></KeyValue>"; + XmlDocument doc = new XmlDocument (); + doc.LoadXml (dsaKey); + + DSAKeyValue dsa1 = new DSAKeyValue (); + dsa1.LoadXml (doc.DocumentElement); + + string s = (dsa1.GetXml ().OuterXml); + AssertEquals ("DSA Key", dsaKey, s); + } + + public void TestInvalidValue () + { + string badKey = "<Test></Test>"; + XmlDocument doc = new XmlDocument (); + doc.LoadXml (badKey); + + DSAKeyValue dsa1 = new DSAKeyValue (); + + try { + dsa1.LoadXml (null); + Fail ("Expected ArgumentNullException but got none"); + } + catch (ArgumentNullException) { + // this is what we expect + } + catch (Exception e) { + Fail ("Expected ArgumentNullException but got: " + e.ToString ()); + } + + try { + dsa1.LoadXml (doc.DocumentElement); + Fail ("Expected CryptographicException but got none"); + } + catch (CryptographicException) { + // this is what we expect + } + catch (Exception e) { + Fail ("Expected CryptographicException but got: " + e.ToString ()); + } + } +} + +}
\ No newline at end of file diff --git a/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/DataObjectTest.cs b/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/DataObjectTest.cs new file mode 100644 index 00000000000..362fa0c7c9e --- /dev/null +++ b/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/DataObjectTest.cs @@ -0,0 +1,139 @@ +// +// DataObjectTest.cs - NUnit Test Cases for DataObject +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002 Motus Technologies Inc. (http://www.motus.com) +// + +using NUnit.Framework; +using System; +using System.Security.Cryptography; +using System.Security.Cryptography.Xml; +using System.Xml; + +namespace MonoTests.System.Security.Cryptography.Xml { + +public class DataObjectTest : TestCase { + + public DataObjectTest () : base ("System.Security.Cryptography.Xml.DataObject testsuite") {} + public DataObjectTest (string name) : base (name) {} + + protected override void SetUp () {} + + protected override void TearDown () {} + + public static ITest Suite { + get { + return new TestSuite (typeof (DataObjectTest)); + } + } + + public void TestNewDataObject () + { + string test = "<Test>DataObject</Test>"; + XmlDocument doc = new XmlDocument (); + doc.LoadXml (test); + + DataObject obj1 = new DataObject (); + Assert ("Data.Count==0", (obj1.Data.Count == 0)); + + obj1.Id = "id"; + obj1.MimeType = "mime"; + obj1.Encoding = "encoding"; + AssertEquals ("Only attributes", "<Object Id=\"id\" MimeType=\"mime\" Encoding=\"encoding\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />", (obj1.GetXml ().OuterXml)); + + obj1.Data = doc.ChildNodes; + Assert ("Data.Count==1", (obj1.Data.Count == 1)); + + XmlElement xel = obj1.GetXml (); + + DataObject obj2 = new DataObject (); + obj2.LoadXml (xel); + AssertEquals ("obj1==obj2", (obj1.GetXml ().OuterXml), (obj2.GetXml ().OuterXml)); + + DataObject obj3 = new DataObject (obj1.Id, obj1.MimeType, obj1.Encoding, doc.DocumentElement); + AssertEquals ("obj2==obj3", (obj2.GetXml ().OuterXml), (obj3.GetXml ().OuterXml)); + } + + public void TestImportDataObject () + { + string value1 = "<Object Id=\"id\" MimeType=\"mime\" Encoding=\"encoding\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><Test xmlns=\"\">DataObject1</Test><Test xmlns=\"\">DataObject2</Test></Object>"; + XmlDocument doc = new XmlDocument (); + doc.LoadXml (value1); + + DataObject obj1 = new DataObject (); + obj1.LoadXml (doc.DocumentElement); + Assert ("Data.Count==2", (obj1.Data.Count == 2)); + + string s = (obj1.GetXml ().OuterXml); + AssertEquals ("DataObject 1", value1, s); + + string value2 = "<Object xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><Test xmlns=\"\" /></Object>"; + doc = new XmlDocument (); + doc.LoadXml (value2); + + DataObject obj2 = new DataObject (); + obj2.LoadXml (doc.DocumentElement); + + s = (obj2.GetXml ().OuterXml); + AssertEquals ("DataObject 2", value2, s); + + string value3 = "<Object Id=\"id\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><Test xmlns=\"\" /></Object>"; + doc = new XmlDocument (); + doc.LoadXml (value3); + + DataObject obj3 = new DataObject (); + obj3.LoadXml (doc.DocumentElement); + + s = (obj3.GetXml ().OuterXml); + AssertEquals ("DataObject 3", value3, s); + + string value4 = "<Object MimeType=\"mime\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><Test xmlns=\"\" /></Object>"; + doc = new XmlDocument (); + doc.LoadXml (value4); + + DataObject obj4 = new DataObject (); + obj4.LoadXml (doc.DocumentElement); + + s = (obj4.GetXml ().OuterXml); + AssertEquals ("DataObject 4", value4, s); + } + + public void TestInvalidDataObject () + { + DataObject obj1 = new DataObject (); + try { + obj1.Data = null; + Fail ("Expected ArgumentNullException but none"); + } + catch (ArgumentNullException) { + // this is expected + } + catch (Exception e) { + Fail ("Expected ArgumentNullException but got: " + e.ToString ()); + } + + try { + obj1.LoadXml (null); + Fail ("Expected ArgumentNullException but none"); + } + catch (ArgumentNullException) { + // this is expected + } + catch (Exception e) { + Fail ("Expected ArgumentNullException but got: " + e.ToString ()); + } + + // seems this isn't invalid !?! + string value = "<Test>Bad</Test>"; + XmlDocument doc = new XmlDocument (); + doc.LoadXml (value); + obj1.LoadXml (doc.DocumentElement); + string s = (obj1.GetXml ().OuterXml); + AssertEquals ("DataObject Bad", value, s); + } +} + +}
\ No newline at end of file diff --git a/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/KeyInfoNameTest.cs b/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/KeyInfoNameTest.cs new file mode 100644 index 00000000000..4280d1588ee --- /dev/null +++ b/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/KeyInfoNameTest.cs @@ -0,0 +1,82 @@ +// +// KeyInfoNameTest.cs - NUnit Test Cases for KeyInfoName +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002 Motus Technologies Inc. (http://www.motus.com) +// + +using NUnit.Framework; +using System; +using System.Security.Cryptography; +using System.Security.Cryptography.Xml; +using System.Xml; + +namespace MonoTests.System.Security.Cryptography.Xml { + +public class KeyInfoNameTest : TestCase { + + public KeyInfoNameTest () : base ("System.Security.Cryptography.Xml.KeyInfoName testsuite") {} + public KeyInfoNameTest (string name) : base (name) {} + + protected override void SetUp () {} + + protected override void TearDown () {} + + public static ITest Suite { + get { + return new TestSuite (typeof (KeyInfoNameTest)); + } + } + + public void TestNewKeyValue () + { + string newKeyValue = "Mono::"; + KeyInfoName name1 = new KeyInfoName (); + name1.Value = newKeyValue; + XmlElement xel = name1.GetXml (); + + KeyInfoName name2 = new KeyInfoName (); + name2.LoadXml (xel); + + AssertEquals ("name1==name2", (name1.GetXml ().OuterXml), (name2.GetXml ().OuterXml)); + AssertEquals ("newKeyValue==value", newKeyValue, name1.Value); + } + + public void TestImportKeyValue () + { + string value = "<KeyName xmlns=\"http://www.w3.org/2000/09/xmldsig#\">Mono::</KeyName>"; + XmlDocument doc = new XmlDocument (); + doc.LoadXml (value); + + KeyInfoName name1 = new KeyInfoName (); + name1.LoadXml (doc.DocumentElement); + + string s = (name1.GetXml ().OuterXml); + AssertEquals ("Name", value, s); + } + + public void TestInvalidValue () + { + string bad = "<Test></Test>"; + XmlDocument doc = new XmlDocument (); + doc.LoadXml (bad); + + KeyInfoName name1 = new KeyInfoName (); + try { + name1.LoadXml (null); + Fail ("Expected ArgumentNullException but got none"); + } + catch (ArgumentNullException) { + // this is what we expect + } + catch (Exception e) { + Fail ("Expected ArgumentNullException but got: " + e.ToString ()); + } + name1.LoadXml (doc.DocumentElement); + AssertEquals("invalid", "<KeyName xmlns=\"http://www.w3.org/2000/09/xmldsig#\"></KeyName>", (name1.GetXml ().OuterXml)); + } +} + +}
\ No newline at end of file diff --git a/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/KeyInfoNodeTest.cs b/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/KeyInfoNodeTest.cs new file mode 100644 index 00000000000..3a1a94f4f2d --- /dev/null +++ b/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/KeyInfoNodeTest.cs @@ -0,0 +1,77 @@ +// +// KeyInfoNodeTest.cs - NUnit Test Cases for KeyInfoNode +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002 Motus Technologies Inc. (http://www.motus.com) +// + +using NUnit.Framework; +using System; +using System.Security.Cryptography; +using System.Security.Cryptography.Xml; +using System.Xml; + +namespace MonoTests.System.Security.Cryptography.Xml { + +public class KeyInfoNodeTest : TestCase { + + public KeyInfoNodeTest () : base ("System.Security.Cryptography.Xml.KeyInfoNode testsuite") {} + public KeyInfoNodeTest (string name) : base (name) {} + + protected override void SetUp () {} + + protected override void TearDown () {} + + public static ITest Suite { + get { + return new TestSuite (typeof (KeyInfoNodeTest)); + } + } + + public void TestNewKeyNode () + { + string test = "<Test></Test>"; + XmlDocument doc = new XmlDocument (); + doc.LoadXml (test); + + KeyInfoNode node1 = new KeyInfoNode (); + node1.Value = doc.DocumentElement; + XmlElement xel = node1.GetXml (); + + KeyInfoNode node2 = new KeyInfoNode (node1.Value); + node2.LoadXml (xel); + + AssertEquals ("node1==node2", (node1.GetXml ().OuterXml), (node2.GetXml ().OuterXml)); + } + + public void TestImportKeyNode () + { + // Note: KeyValue is a valid KeyNode + string value = "<KeyName xmlns=\"http://www.w3.org/2000/09/xmldsig#\">Mono::</KeyName>"; + XmlDocument doc = new XmlDocument (); + doc.LoadXml (value); + + KeyInfoNode node1 = new KeyInfoNode (); + node1.LoadXml (doc.DocumentElement); + + string s = (node1.GetXml ().OuterXml); + AssertEquals ("Node", value, s); + } + + // well there's no invalid value - unless you read the doc ;-) + public void TestInvalidKeyNode () + { + string bad = "<Test></Test>"; + XmlDocument doc = new XmlDocument (); + doc.LoadXml (bad); + + KeyInfoNode node1 = new KeyInfoNode (); + // LAMESPEC: No ArgumentNullException is thrown if value == null + node1.LoadXml (null); + AssertNull ("Value==null", node1.Value); + } +} + +}
\ No newline at end of file diff --git a/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/KeyInfoRetrievalMethodTest.cs b/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/KeyInfoRetrievalMethodTest.cs new file mode 100644 index 00000000000..40625904de0 --- /dev/null +++ b/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/KeyInfoRetrievalMethodTest.cs @@ -0,0 +1,92 @@ +// +// KeyInfoRetrievalMethodTest.cs - NUnit Test Cases for KeyInfoRetrievalMethod +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002 Motus Technologies Inc. (http://www.motus.com) +// + +using NUnit.Framework; +using System; +using System.Security.Cryptography; +using System.Security.Cryptography.Xml; +using System.Xml; + +namespace MonoTests.System.Security.Cryptography.Xml { + +public class KeyInfoRetrievalMethodTest : TestCase { + + public KeyInfoRetrievalMethodTest () : base ("System.Security.Cryptography.Xml.KeyInfoRetrievalMethod testsuite") {} + public KeyInfoRetrievalMethodTest (string name) : base (name) {} + + protected override void SetUp () {} + + protected override void TearDown () {} + + public static ITest Suite { + get { + return new TestSuite (typeof (KeyInfoRetrievalMethodTest)); + } + } + + public void TestNewKeyNode () + { + string uri = "http://www.go-mono.com/"; + KeyInfoRetrievalMethod uri1 = new KeyInfoRetrievalMethod (); + + // verify empty XML + AssertEquals ("Empty", "<RetrievalElement xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />", (uri1.GetXml ().OuterXml)); + + uri1.Uri = uri; + XmlElement xel = uri1.GetXml (); + + KeyInfoRetrievalMethod uri2 = new KeyInfoRetrievalMethod (uri1.Uri); + uri2.LoadXml (xel); + + AssertEquals ("uri1==uri2", (uri1.GetXml ().OuterXml), (uri2.GetXml ().OuterXml)); + AssertEquals ("uri==Uri", uri, uri1.Uri); + } + + public void TestImportKeyNode () + { + string value = "<RetrievalElement URI=\"http://www.go-mono.com/\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />"; + XmlDocument doc = new XmlDocument (); + doc.LoadXml (value); + + KeyInfoRetrievalMethod uri1 = new KeyInfoRetrievalMethod (); + uri1.LoadXml (doc.DocumentElement); + + // verify that proper XML is generated (equals to original) + string s = (uri1.GetXml ().OuterXml); + AssertEquals ("Xml", value, s); + + // verify that property is parsed correctly + AssertEquals ("Uri", "http://www.go-mono.com/", uri1.Uri); + } + + public void TestInvalidKeyNode () + { + KeyInfoRetrievalMethod uri1 = new KeyInfoRetrievalMethod (); + try { + uri1.LoadXml (null); + Fail ("Expected ArgumentNullException but got none"); + } + catch (ArgumentNullException) { + // this is what we expect + } + catch (Exception e) { + Fail ("Expected ArgumentNullException but got: " + e.ToString ()); + } + + string bad = "<Test></Test>"; + XmlDocument doc = new XmlDocument (); + doc.LoadXml (bad); + // no exception is thrown + uri1.LoadXml (doc.DocumentElement); + // note that URI="" is present (unlike a empty Uri) + AssertEquals("invalid", "<RetrievalElement URI=\"\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />", (uri1.GetXml ().OuterXml)); + } +} + +}
\ No newline at end of file diff --git a/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/KeyInfoTest.cs b/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/KeyInfoTest.cs new file mode 100644 index 00000000000..47949ea062b --- /dev/null +++ b/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/KeyInfoTest.cs @@ -0,0 +1,205 @@ +// +// KeyInfoTest.cs - NUnit Test Cases for KeyInfo +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002 Motus Technologies Inc. (http://www.motus.com) +// + +using NUnit.Framework; +using System; +using System.Security.Cryptography; +using System.Security.Cryptography.X509Certificates; +using System.Security.Cryptography.Xml; +using System.Xml; + +namespace MonoTests.System.Security.Cryptography.Xml { + +public class KeyInfoTest : TestCase { + + public KeyInfoTest () : base ("System.Security.Cryptography.Xml.KeyInfo testsuite") {} + public KeyInfoTest (string name) : base (name) {} + + private KeyInfo info; + + + protected override void SetUp () + { + info = new KeyInfo (); + } + + protected override void TearDown () {} + + public static ITest Suite { + get { + return new TestSuite (typeof (KeyInfoTest)); + } + } + + public void TestEmptyKeyInfo () + { + AssertEquals ("empty", "<KeyInfo xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />", (info.GetXml ().OuterXml)); + AssertEquals ("empty count", 0, info.Count); + } + + public void TestKeyInfoName () + { + KeyInfoName name = new KeyInfoName (); + name.Value = "Mono::"; + info.AddClause (name); + AssertEquals ("name", "<KeyInfo xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><KeyName>Mono::</KeyName></KeyInfo>", (info.GetXml ().OuterXml)); + AssertEquals ("name count", 1, info.Count); + } + + public void TestKeyInfoNode () + { + string test = "<Test>KeyInfoNode</Test>"; + XmlDocument doc = new XmlDocument (); + doc.LoadXml (test); + + KeyInfoNode node = new KeyInfoNode (doc.DocumentElement); + info.AddClause (node); + AssertEquals ("node", "<KeyInfo xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><Test xmlns=\"\">KeyInfoNode</Test></KeyInfo>", (info.GetXml ().OuterXml)); + AssertEquals ("node count", 1, info.Count); + } + + string xmlDSA = "<DSAKeyValue><P>rjxsMU368YOCTQejWkiuO9e/vUVwkLtq1jKiU3TtJ53hBJqjFRuTa228vZe+BH2su9RPn/vYFWfQDv6zgBYe3eNdu4Afw+Ny0FatX6dl3E77Ra6Tsd3MmLXBiGSQ1mMNd5G2XQGpbt9zsGlUaexXekeMLxIufgfZLwYp67M+2WM=</P><Q>tf0K9rMyvUrU4cIkwbCrDRhQAJk=</Q><G>S8Z+1pGCed00w6DtVcqZLKjfqlCJ7JsugEFIgSy/Vxtu9YGCMclV4ijGEbPo/jU8YOSMuD7E9M7UaopMRcmKQjoKZzoJjkgVFP48Ohxl1f08lERnButsxanx3+OstFwUGQ8XNaGg3KrIoZt1FUnfxN3RHHTvVhjzNSHxMGULGaU=</G><Y>LnrxxRGLYeV2XLtK3SYz8RQHlHFZYrtznDZyMotuRfO5uC5YODhSFyLXvb1qB3WeGtF4h3Eo4KzHgMgfN2ZMlffxFRhJgTtH3ctbL8lfQoDkjeiPPnYGhspdJxr0tyZmiy0gkjJG3vwHYrLnvZWx9Wm/unqiOlGBPNuxJ+hOeP8=</Y><J>9RhE5TycDtdEIXxS3HfxFyXYgpy81zY5lVjwD6E9JP37MWEi80BlX6ab1YPm6xYSEoqReMPP9RgGiW6DuACpgI7+8vgCr4i/7VhzModJAA56PwvTu6UMt9xxKU/fT672v8ucREkMWoc7lEey</J><Seed>HxW3N4RHWVgqDQKuGg7iJTUTiCs=</Seed><PgenCounter>Asw=</PgenCounter></DSAKeyValue>"; + + public void TestDSAKeyValue () + { + DSA key = DSA.Create (); + key.FromXmlString (xmlDSA); + DSAKeyValue dsa = new DSAKeyValue (key); + info.AddClause (dsa); + AssertEquals ("dsa", "<KeyInfo xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><KeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\">" + xmlDSA + "</KeyValue></KeyInfo>", (info.GetXml ().OuterXml)); + AssertEquals ("dsa count", 1, info.Count); + } + + static string xmlRSA = "<RSAKeyValue><Modulus>9DC4XNdQJwMRnz5pP2a6U51MHCODRilaIoVXqUPhCUb0lJdGroeqVYT84ZyIVrcarzD7Tqs3aEOIa3rKox0N1bxQpZPqayVQeLAkjLLtzJW/ScRJx3uEDJdgT1JnM1FH0GZTinmEdCUXdLc7+Y/c/qqIkTfbwHbRZjW0bBJyExM=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue></KeyValue>"; + + public void TestRSAKeyValue () + { + RSA key = RSA.Create (); + key.FromXmlString (xmlRSA); + RSAKeyValue rsa = new RSAKeyValue (key); + info.AddClause (rsa); + AssertEquals ("rsa", "<KeyInfo xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><KeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\">" + xmlRSA + "</KeyInfo>", (info.GetXml ().OuterXml)); + AssertEquals ("rsa count", 1, info.Count); + } + + public void TestRetrievalMethod () + { + KeyInfoRetrievalMethod retrieval = new KeyInfoRetrievalMethod (); + retrieval.Uri = "http://www.go-mono.org/"; + info.AddClause (retrieval); + AssertEquals ("RetrievalMethod", "<KeyInfo xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><RetrievalElement URI=\"http://www.go-mono.org/\" /></KeyInfo>", (info.GetXml ().OuterXml)); + AssertEquals ("RetrievalMethod count", 1, info.Count); + } + + static byte[] cert = { 0x30,0x82,0x02,0x1D,0x30,0x82,0x01,0x86,0x02,0x01,0x14,0x30,0x0D,0x06,0x09,0x2A,0x86,0x48,0x86,0xF7,0x0D,0x01,0x01,0x04,0x05,0x00,0x30,0x58,0x31,0x0B,0x30,0x09,0x06,0x03,0x55,0x04,0x06,0x13,0x02,0x43,0x41,0x31,0x1F,0x30,0x1D,0x06,0x03,0x55,0x04,0x03,0x13,0x16,0x4B,0x65,0x79,0x77,0x69,0x74,0x6E,0x65,0x73,0x73,0x20,0x43,0x61,0x6E,0x61,0x64,0x61,0x20,0x49,0x6E,0x63,0x2E,0x31,0x28,0x30,0x26,0x06,0x0A,0x2B,0x06,0x01,0x04,0x01,0x2A,0x02,0x0B,0x02,0x01,0x13,0x18,0x6B,0x65,0x79,0x77,0x69,0x74,0x6E,0x65,0x73, + 0x73,0x40,0x6B,0x65,0x79,0x77,0x69,0x74,0x6E,0x65,0x73,0x73,0x2E,0x63,0x61,0x30,0x1E,0x17,0x0D,0x39,0x36,0x30,0x35,0x30,0x37,0x30,0x30,0x30,0x30,0x30,0x30,0x5A,0x17,0x0D,0x39,0x39,0x30,0x35,0x30,0x37,0x30,0x30,0x30,0x30,0x30,0x30,0x5A,0x30,0x58,0x31,0x0B,0x30,0x09,0x06,0x03,0x55,0x04,0x06,0x13,0x02,0x43,0x41,0x31,0x1F,0x30,0x1D,0x06,0x03,0x55,0x04,0x03,0x13,0x16,0x4B,0x65,0x79,0x77,0x69,0x74,0x6E,0x65,0x73,0x73,0x20,0x43,0x61,0x6E,0x61,0x64,0x61,0x20,0x49,0x6E,0x63,0x2E,0x31,0x28,0x30,0x26,0x06, + 0x0A,0x2B,0x06,0x01,0x04,0x01,0x2A,0x02,0x0B,0x02,0x01,0x13,0x18,0x6B,0x65,0x79,0x77,0x69,0x74,0x6E,0x65,0x73,0x73,0x40,0x6B,0x65,0x79,0x77,0x69,0x74,0x6E,0x65,0x73,0x73,0x2E,0x63,0x61,0x30,0x81,0x9D,0x30,0x0D,0x06,0x09,0x2A,0x86,0x48,0x86,0xF7,0x0D,0x01,0x01,0x01,0x05,0x00,0x03,0x81,0x8B,0x00,0x30,0x81,0x87,0x02,0x81,0x81,0x00,0xCD,0x23,0xFA,0x2A,0xE1,0xED,0x98,0xF4,0xE9,0xD0,0x93,0x3E,0xD7,0x7A,0x80,0x02,0x4C,0xCC,0xC1,0x02,0xAF,0x5C,0xB6,0x1F,0x7F,0xFA,0x57,0x42,0x6F,0x30,0xD1,0x20,0xC5,0xB5, + 0x21,0x07,0x40,0x2C,0xA9,0x86,0xC2,0xF3,0x64,0x84,0xAE,0x3D,0x85,0x2E,0xED,0x85,0xBD,0x54,0xB0,0x18,0x28,0xEF,0x6A,0xF8,0x1B,0xE7,0x0B,0x16,0x1F,0x93,0x25,0x4F,0xC7,0xF8,0x8E,0xC3,0xB9,0xCA,0x98,0x84,0x0E,0x55,0xD0,0x2F,0xEF,0x78,0x77,0xC5,0x72,0x28,0x5F,0x60,0xBF,0x19,0x2B,0xD1,0x72,0xA2,0xB7,0xD8,0x3F,0xE0,0x97,0x34,0x5A,0x01,0xBD,0x04,0x9C,0xC8,0x78,0x45,0xCD,0x93,0x8D,0x15,0xF2,0x76,0x10,0x11,0xAB,0xB8,0x5B,0x2E,0x9E,0x52,0xDD,0x81,0x3E,0x9C,0x64,0xC8,0x29,0x93,0x02,0x01,0x03,0x30,0x0D,0x06, + 0x09,0x2A,0x86,0x48,0x86,0xF7,0x0D,0x01,0x01,0x04,0x05,0x00,0x03,0x81,0x81,0x00,0x32,0x1A,0x35,0xBA,0xBF,0x43,0x27,0xD6,0xB4,0xD4,0xB8,0x76,0xE5,0xE3,0x9B,0x4D,0x6C,0xC0,0x86,0xC9,0x77,0x35,0xBA,0x6B,0x16,0x2D,0x13,0x46,0x4A,0xB0,0x32,0x53,0xA1,0x5B,0x5A,0xE9,0x99,0xE2,0x0C,0x86,0x88,0x17,0x4E,0x0D,0xFE,0x82,0xAC,0x4E,0x47,0xEF,0xFB,0xFF,0x39,0xAC,0xEE,0x35,0xC8,0xFA,0x52,0x37,0x0A,0x49,0xAD,0x59,0xAD,0xE2,0x8A,0xA9,0x1C,0xC6,0x5F,0x1F,0xF8,0x6F,0x73,0x7E,0xCD,0xA0,0x31,0xE8,0x0C,0xBE,0xF5,0x4D, + 0xD9,0xB2,0xAB,0x8A,0x12,0xB6,0x30,0x78,0x68,0x11,0x7C,0x0D,0xF1,0x49,0x4D,0xA3,0xFD,0xB2,0xE9,0xFF,0x1D,0xF0,0x91,0xFA,0x54,0x85,0xFF,0x33,0x90,0xE8,0xC1,0xBF,0xA4,0x9B,0xA4,0x62,0x46,0xBD,0x61,0x12,0x59,0x98,0x41,0x89 }; + + public void TestX509Data () + { + X509Certificate x509 = new X509Certificate (cert); + KeyInfoX509Data x509data = new KeyInfoX509Data (x509); + info.AddClause (x509data); + AssertEquals ("X509Data", "<KeyInfo xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><X509Data xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><X509Certificate>MIICHTCCAYYCARQwDQYJKoZIhvcNAQEEBQAwWDELMAkGA1UEBhMCQ0ExHzAdBgNVBAMTFktleXdpdG5lc3MgQ2FuYWRhIEluYy4xKDAmBgorBgEEASoCCwIBExhrZXl3aXRuZXNzQGtleXdpdG5lc3MuY2EwHhcNOTYwNTA3MDAwMDAwWhcNOTkwNTA3MDAwMDAwWjBYMQswCQYDVQQGEwJDQTEfMB0GA1UEAxMWS2V5d2l0bmVzcyBDYW5hZGEgSW5jLjEoMCYGCisGAQQBKgILAgETGGtleXdpdG5lc3NAa2V5d2l0bmVzcy5jYTCBnTANBgkqhkiG9w0BAQEFAAOBiwAwgYcCgYEAzSP6KuHtmPTp0JM+13qAAkzMwQKvXLYff/pXQm8w0SDFtSEHQCyphsLzZISuPYUu7YW9VLAYKO9q+BvnCxYfkyVPx/iOw7nKmIQOVdAv73h3xXIoX2C/GSvRcqK32D/glzRaAb0EnMh4Rc2TjRXydhARq7hbLp5S3YE+nGTIKZMCAQMwDQYJKoZIhvcNAQEEBQADgYEAMho1ur9DJ9a01Lh25eObTWzAhsl3NbprFi0TRkqwMlOhW1rpmeIMhogXTg3+gqxOR+/7/zms7jXI+lI3CkmtWa3iiqkcxl8f+G9zfs2gMegMvvVN2bKrihK2MHhoEXwN8UlNo/2y6f8d8JH6VIX/M5Dowb+km6RiRr1hElmYQYk=</X509Certificate></X509Data></KeyInfo>", (info.GetXml ().OuterXml)); + AssertEquals ("X509Data count", 1, info.Count); + } + + public void TestComplex () + { + KeyInfoName name = new KeyInfoName (); + name.Value = "Mono::"; + info.AddClause (name); + + DSA keyDSA = DSA.Create (); + keyDSA.FromXmlString (xmlDSA); + DSAKeyValue dsa = new DSAKeyValue (keyDSA); + info.AddClause (dsa); + + RSA keyRSA = RSA.Create (); + keyRSA.FromXmlString (xmlRSA); + RSAKeyValue rsa = new RSAKeyValue (keyRSA); + info.AddClause (rsa); + + KeyInfoRetrievalMethod retrieval = new KeyInfoRetrievalMethod (); + retrieval.Uri = "http://www.go-mono.org/"; + info.AddClause (retrieval); + + X509Certificate x509 = new X509Certificate (cert); + KeyInfoX509Data x509data = new KeyInfoX509Data (x509); + info.AddClause (x509data); + + string s = "<KeyInfo xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><KeyName>Mono::</KeyName><KeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><DSAKeyValue><P>rjxsMU368YOCTQejWkiuO9e/vUVwkLtq1jKiU3TtJ53hBJqjFRuTa228vZe+BH2su9RPn/vYFWfQDv6zgBYe3eNdu4Afw+Ny0FatX6dl3E77Ra6Tsd3MmLXBiGSQ1mMNd5G2XQGpbt9zsGlUaexXekeMLxIufgfZLwYp67M+2WM=</P><Q>tf0K9rMyvUrU4cIkwbCrDRhQAJk=</Q><G>S8Z+1pGCed00w6DtVcqZLKjfqlCJ7JsugEFIgSy/Vxtu9YGCMclV4ijGEbPo/jU8YOSMuD7E9M7UaopMRcmKQjoKZzoJjkgVFP48Ohxl1f08lERnButsxanx3+OstFwUGQ8XNaGg3KrIoZt1FUnfxN3RHHTvVhjzNSHxMGULGaU=</G><Y>LnrxxRGLYeV2XLtK3SYz8RQHlHFZYrtznDZyMotuRfO5uC5YODhSFyLXvb1qB3WeGtF4h3Eo4KzHgMgfN2ZMlffxFRhJgTtH3ctbL8lfQoDkjeiPPnYGhspdJxr0tyZmiy0gkjJG3vwHYrLnvZWx9Wm/unqiOlGBPNuxJ+hOeP8=</Y><J>9RhE5TycDtdEIXxS3HfxFyXYgpy81zY5lVjwD6E9JP37MWEi80BlX6ab1YPm6xYSEoqReMPP9RgGiW6DuACpgI7+8vgCr4i/7VhzModJAA56PwvTu6UMt9xxKU/fT672v8ucREkMWoc7lEey</J><Seed>HxW3N4RHWVgqDQKuGg7iJTUTiCs=</Seed><PgenCounter>Asw=</PgenCounter></DSAKeyValue></KeyValue>"; + s += "<KeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><RSAKeyValue><Modulus>9DC4XNdQJwMRnz5pP2a6U51MHCODRilaIoVXqUPhCUb0lJdGroeqVYT84ZyIVrcarzD7Tqs3aEOIa3rKox0N1bxQpZPqayVQeLAkjLLtzJW/ScRJx3uEDJdgT1JnM1FH0GZTinmEdCUXdLc7+Y/c/qqIkTfbwHbRZjW0bBJyExM=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue></KeyValue><RetrievalElement URI=\"http://www.go-mono.org/\" /><X509Data xmlns=\"http://www.w3.org/2000/09/xmldsig#\">"; + s += "<X509Certificate>MIICHTCCAYYCARQwDQYJKoZIhvcNAQEEBQAwWDELMAkGA1UEBhMCQ0ExHzAdBgNVBAMTFktleXdpdG5lc3MgQ2FuYWRhIEluYy4xKDAmBgorBgEEASoCCwIBExhrZXl3aXRuZXNzQGtleXdpdG5lc3MuY2EwHhcNOTYwNTA3MDAwMDAwWhcNOTkwNTA3MDAwMDAwWjBYMQswCQYDVQQGEwJDQTEfMB0GA1UEAxMWS2V5d2l0bmVzcyBDYW5hZGEgSW5jLjEoMCYGCisGAQQBKgILAgETGGtleXdpdG5lc3NAa2V5d2l0bmVzcy5jYTCBnTANBgkqhkiG9w0BAQEFAAOBiwAwgYcCgYEAzSP6KuHtmPTp0JM+13qAAkzMwQKvXLYff/pXQm8w0SDFtSEHQCyphsLzZISuPYUu7YW9VLAYKO9q+BvnCxYfkyVPx/iOw7nKmIQOVdAv73h3xXIoX2C/GSvRcqK32D/glzRaAb0EnMh4Rc2TjRXydhARq7hbLp5S3YE+nGTIKZMCAQMwDQYJKoZIhvcNAQEEBQADgYEAMho1ur9DJ9a01Lh25eObTWzAhsl3NbprFi0TRkqwMlOhW1rpmeIMhogXTg3+gqxOR+/7/zms7jXI+lI3CkmtWa3iiqkcxl8f+G9zfs2gMegMvvVN2bKrihK2MHhoEXwN8UlNo/2y6f8d8JH6VIX/M5Dowb+km6RiRr1hElmYQYk=</X509Certificate></X509Data></KeyInfo>"; + AssertEquals ("Complex", s, (info.GetXml ().OuterXml)); + AssertEquals ("RetrievalMethod count", 5, info.Count); + } + + public void TestImportKeyNode () + { + string value = "<KeyInfo xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><KeyName>Mono::</KeyName><KeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><DSAKeyValue><P>rjxsMU368YOCTQejWkiuO9e/vUVwkLtq1jKiU3TtJ53hBJqjFRuTa228vZe+BH2su9RPn/vYFWfQDv6zgBYe3eNdu4Afw+Ny0FatX6dl3E77Ra6Tsd3MmLXBiGSQ1mMNd5G2XQGpbt9zsGlUaexXekeMLxIufgfZLwYp67M+2WM=</P><Q>tf0K9rMyvUrU4cIkwbCrDRhQAJk=</Q><G>S8Z+1pGCed00w6DtVcqZLKjfqlCJ7JsugEFIgSy/Vxtu9YGCMclV4ijGEbPo/jU8YOSMuD7E9M7UaopMRcmKQjoKZzoJjkgVFP48Ohxl1f08lERnButsxanx3+OstFwUGQ8XNaGg3KrIoZt1FUnfxN3RHHTvVhjzNSHxMGULGaU=</G><Y>LnrxxRGLYeV2XLtK3SYz8RQHlHFZYrtznDZyMotuRfO5uC5YODhSFyLXvb1qB3WeGtF4h3Eo4KzHgMgfN2ZMlffxFRhJgTtH3ctbL8lfQoDkjeiPPnYGhspdJxr0tyZmiy0gkjJG3vwHYrLnvZWx9Wm/unqiOlGBPNuxJ+hOeP8=</Y><J>9RhE5TycDtdEIXxS3HfxFyXYgpy81zY5lVjwD6E9JP37MWEi80BlX6ab1YPm6xYSEoqReMPP9RgGiW6DuACpgI7+8vgCr4i/7VhzModJAA56PwvTu6UMt9xxKU/fT672v8ucREkMWoc7lEey</J><Seed>HxW3N4RHWVgqDQKuGg7iJTUTiCs=</Seed><PgenCounter>Asw=</PgenCounter></DSAKeyValue></KeyValue>"; + value += "<KeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><RSAKeyValue><Modulus>9DC4XNdQJwMRnz5pP2a6U51MHCODRilaIoVXqUPhCUb0lJdGroeqVYT84ZyIVrcarzD7Tqs3aEOIa3rKox0N1bxQpZPqayVQeLAkjLLtzJW/ScRJx3uEDJdgT1JnM1FH0GZTinmEdCUXdLc7+Y/c/qqIkTfbwHbRZjW0bBJyExM=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue></KeyValue><RetrievalElement URI=\"http://www.go-mono.org/\" /><X509Data xmlns=\"http://www.w3.org/2000/09/xmldsig#\">"; + value += "<X509Certificate>MIICHTCCAYYCARQwDQYJKoZIhvcNAQEEBQAwWDELMAkGA1UEBhMCQ0ExHzAdBgNVBAMTFktleXdpdG5lc3MgQ2FuYWRhIEluYy4xKDAmBgorBgEEASoCCwIBExhrZXl3aXRuZXNzQGtleXdpdG5lc3MuY2EwHhcNOTYwNTA3MDAwMDAwWhcNOTkwNTA3MDAwMDAwWjBYMQswCQYDVQQGEwJDQTEfMB0GA1UEAxMWS2V5d2l0bmVzcyBDYW5hZGEgSW5jLjEoMCYGCisGAQQBKgILAgETGGtleXdpdG5lc3NAa2V5d2l0bmVzcy5jYTCBnTANBgkqhkiG9w0BAQEFAAOBiwAwgYcCgYEAzSP6KuHtmPTp0JM+13qAAkzMwQKvXLYff/pXQm8w0SDFtSEHQCyphsLzZISuPYUu7YW9VLAYKO9q+BvnCxYfkyVPx/iOw7nKmIQOVdAv73h3xXIoX2C/GSvRcqK32D/glzRaAb0EnMh4Rc2TjRXydhARq7hbLp5S3YE+nGTIKZMCAQMwDQYJKoZIhvcNAQEEBQADgYEAMho1ur9DJ9a01Lh25eObTWzAhsl3NbprFi0TRkqwMlOhW1rpmeIMhogXTg3+gqxOR+/7/zms7jXI+lI3CkmtWa3iiqkcxl8f+G9zfs2gMegMvvVN2bKrihK2MHhoEXwN8UlNo/2y6f8d8JH6VIX/M5Dowb+km6RiRr1hElmYQYk=</X509Certificate></X509Data></KeyInfo>"; + XmlDocument doc = new XmlDocument (); + doc.LoadXml (value); + info.LoadXml (doc.DocumentElement); + + AssertEquals ("Import", value, (info.GetXml ().OuterXml)); + AssertEquals ("Import count", 5, info.Count); + } + + public void TestNullClause () + { + AssertEquals ("empty count", 0, info.Count); + // null is accepted... + info.AddClause (null); + AssertEquals ("null count", 1, info.Count); + // but can't get XML out if it! + try { + XmlElement xel = info.GetXml (); + Fail ("Expected NullReferenceException but got none"); + } + catch (NullReferenceException) { + // this is expected + } + catch (Exception e) { + Fail ("Expected NullReferenceException but got: " + e.ToString ()); + } + } + + public void TestNullXml () + { + try { + info.LoadXml (null); + Fail ("Expected ArgumentNullException but none"); + } + catch (ArgumentNullException) { + // this is expected + } + catch (Exception e) { + Fail ("Expected ArgumentNullException but got: " + e.ToString ()); + } + } + + public void TestInvalid () + { + string bad = "<Test></Test>"; + XmlDocument doc = new XmlDocument (); + doc.LoadXml (bad); + info.LoadXml (doc.DocumentElement); + // LAMESPEC: no expection but Xml isn't loaded + AssertEquals ("invalid", "<KeyInfo xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />", (info.GetXml ().OuterXml)); + AssertEquals ("invalid count", 0, info.Count); + } +} + +}
\ No newline at end of file diff --git a/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/KeyInfoX509DataTest.cs b/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/KeyInfoX509DataTest.cs new file mode 100644 index 00000000000..36ce73ad0ab --- /dev/null +++ b/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/KeyInfoX509DataTest.cs @@ -0,0 +1,234 @@ +// +// KeyInfoX509DataTest.cs - NUnit Test Cases for KeyInfoX509Data +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002 Motus Technologies Inc. (http://www.motus.com) +// + +using NUnit.Framework; +using System; +using System.IO; +using System.Security.Cryptography; +using System.Security.Cryptography.X509Certificates; +using System.Security.Cryptography.Xml; +using System.Xml; + +namespace MonoTests.System.Security.Cryptography.Xml { + +public class KeyInfoX509DataTest : TestCase { + + static byte[] cert = { 0x30,0x82,0x09,0xB9,0x30,0x82,0x09,0x22,0xA0,0x03,0x02,0x01,0x02,0x02,0x10,0x20,0x0B,0x35,0x5E,0xCE,0xC4,0xB0,0x63,0xB7,0xDE,0xC6,0x34,0xB9,0x70,0x34,0x44,0x30,0x0D,0x06,0x09,0x2A,0x86,0x48,0x86,0xF7,0x0D,0x01,0x01,0x04,0x05,0x00,0x30,0x62,0x31,0x11,0x30,0x0F,0x06,0x03,0x55,0x04,0x07,0x13,0x08,0x49,0x6E,0x74,0x65,0x72,0x6E,0x65,0x74,0x31,0x17,0x30,0x15,0x06,0x03,0x55,0x04,0x0A,0x13,0x0E,0x56,0x65,0x72,0x69,0x53,0x69,0x67,0x6E,0x2C,0x20,0x49,0x6E,0x63,0x2E,0x31,0x34,0x30,0x32,0x06,0x03,0x55,0x04,0x0B, + 0x13,0x2B,0x56,0x65,0x72,0x69,0x53,0x69,0x67,0x6E,0x20,0x43,0x6C,0x61,0x73,0x73,0x20,0x31,0x20,0x43,0x41,0x20,0x2D,0x20,0x49,0x6E,0x64,0x69,0x76,0x69,0x64,0x75,0x61,0x6C,0x20,0x53,0x75,0x62,0x73,0x63,0x72,0x69,0x62,0x65,0x72,0x30,0x1E,0x17,0x0D,0x39,0x36,0x30,0x38,0x32,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5A,0x17,0x0D,0x39,0x37,0x30,0x38,0x32,0x30,0x32,0x33,0x35,0x39,0x35,0x39,0x5A,0x30,0x82,0x01,0x0A,0x31,0x11,0x30,0x0F,0x06,0x03,0x55,0x04,0x07,0x13,0x08,0x49,0x6E,0x74,0x65,0x72,0x6E,0x65,0x74, + 0x31,0x17,0x30,0x15,0x06,0x03,0x55,0x04,0x0A,0x13,0x0E,0x56,0x65,0x72,0x69,0x53,0x69,0x67,0x6E,0x2C,0x20,0x49,0x6E,0x63,0x2E,0x31,0x34,0x30,0x32,0x06,0x03,0x55,0x04,0x0B,0x13,0x2B,0x56,0x65,0x72,0x69,0x53,0x69,0x67,0x6E,0x20,0x43,0x6C,0x61,0x73,0x73,0x20,0x31,0x20,0x43,0x41,0x20,0x2D,0x20,0x49,0x6E,0x64,0x69,0x76,0x69,0x64,0x75,0x61,0x6C,0x20,0x53,0x75,0x62,0x73,0x63,0x72,0x69,0x62,0x65,0x72,0x31,0x46,0x30,0x44,0x06,0x03,0x55,0x04,0x0B,0x13,0x3D,0x77,0x77,0x77,0x2E,0x76,0x65,0x72,0x69,0x73,0x69, + 0x67,0x6E,0x2E,0x63,0x6F,0x6D,0x2F,0x72,0x65,0x70,0x6F,0x73,0x69,0x74,0x6F,0x72,0x79,0x2F,0x43,0x50,0x53,0x20,0x49,0x6E,0x63,0x6F,0x72,0x70,0x2E,0x20,0x62,0x79,0x20,0x52,0x65,0x66,0x2E,0x2C,0x4C,0x49,0x41,0x42,0x2E,0x4C,0x54,0x44,0x28,0x63,0x29,0x39,0x36,0x31,0x26,0x30,0x24,0x06,0x03,0x55,0x04,0x0B,0x13,0x1D,0x44,0x69,0x67,0x69,0x74,0x61,0x6C,0x20,0x49,0x44,0x20,0x43,0x6C,0x61,0x73,0x73,0x20,0x31,0x20,0x2D,0x20,0x4E,0x65,0x74,0x73,0x63,0x61,0x70,0x65,0x31,0x16,0x30,0x14,0x06,0x03,0x55,0x04,0x03, + 0x13,0x0D,0x44,0x61,0x76,0x69,0x64,0x20,0x54,0x2E,0x20,0x47,0x72,0x61,0x79,0x31,0x1E,0x30,0x1C,0x06,0x09,0x2A,0x86,0x48,0x86,0xF7,0x0D,0x01,0x09,0x01,0x16,0x0F,0x64,0x61,0x76,0x69,0x64,0x40,0x66,0x6F,0x72,0x6D,0x61,0x6C,0x2E,0x69,0x65,0x30,0x5C,0x30,0x0D,0x06,0x09,0x2A,0x86,0x48,0x86,0xF7,0x0D,0x01,0x01,0x01,0x05,0x00,0x03,0x4B,0x00,0x30,0x48,0x02,0x41,0x00,0xC5,0x81,0x07,0xA2,0xEB,0x0F,0xB8,0xFF,0xF8,0xF8,0x1C,0xEE,0x32,0xFF,0xBF,0x12,0x35,0x6A,0xF9,0x6B,0xC8,0xBE,0x2F,0xFB,0x3E,0xAF,0x04,0x51, + 0x4A,0xAC,0xDD,0x10,0x29,0xA8,0xCD,0x40,0x5B,0x66,0x1E,0x98,0xEF,0xF2,0x4C,0x77,0xFA,0x8F,0x86,0xD1,0x21,0x67,0x92,0x44,0x4A,0xC4,0x89,0xC9,0x83,0xCF,0x88,0x9F,0x6F,0xE2,0x32,0x35,0x02,0x03,0x01,0x00,0x01,0xA3,0x82,0x07,0x08,0x30,0x82,0x07,0x04,0x30,0x09,0x06,0x03,0x55,0x1D,0x13,0x04,0x02,0x30,0x00,0x30,0x82,0x02,0x1F,0x06,0x03,0x55,0x1D,0x03,0x04,0x82,0x02,0x16,0x30,0x82,0x02,0x12,0x30,0x82,0x02,0x0E,0x30,0x82,0x02,0x0A,0x06,0x0B,0x60,0x86,0x48,0x01,0x86,0xF8,0x45,0x01,0x07,0x01,0x01,0x30,0x82, + 0x01,0xF9,0x16,0x82,0x01,0xA7,0x54,0x68,0x69,0x73,0x20,0x63,0x65,0x72,0x74,0x69,0x66,0x69,0x63,0x61,0x74,0x65,0x20,0x69,0x6E,0x63,0x6F,0x72,0x70,0x6F,0x72,0x61,0x74,0x65,0x73,0x20,0x62,0x79,0x20,0x72,0x65,0x66,0x65,0x72,0x65,0x6E,0x63,0x65,0x2C,0x20,0x61,0x6E,0x64,0x20,0x69,0x74,0x73,0x20,0x75,0x73,0x65,0x20,0x69,0x73,0x20,0x73,0x74,0x72,0x69,0x63,0x74,0x6C,0x79,0x20,0x73,0x75,0x62,0x6A,0x65,0x63,0x74,0x20,0x74,0x6F,0x2C,0x20,0x74,0x68,0x65,0x20,0x56,0x65,0x72,0x69,0x53,0x69,0x67,0x6E,0x20,0x43, + 0x65,0x72,0x74,0x69,0x66,0x69,0x63,0x61,0x74,0x69,0x6F,0x6E,0x20,0x50,0x72,0x61,0x63,0x74,0x69,0x63,0x65,0x20,0x53,0x74,0x61,0x74,0x65,0x6D,0x65,0x6E,0x74,0x20,0x28,0x43,0x50,0x53,0x29,0x2C,0x20,0x61,0x76,0x61,0x69,0x6C,0x61,0x62,0x6C,0x65,0x20,0x61,0x74,0x3A,0x20,0x68,0x74,0x74,0x70,0x73,0x3A,0x2F,0x2F,0x77,0x77,0x77,0x2E,0x76,0x65,0x72,0x69,0x73,0x69,0x67,0x6E,0x2E,0x63,0x6F,0x6D,0x2F,0x43,0x50,0x53,0x3B,0x20,0x62,0x79,0x20,0x45,0x2D,0x6D,0x61,0x69,0x6C,0x20,0x61,0x74,0x20,0x43,0x50,0x53,0x2D, + 0x72,0x65,0x71,0x75,0x65,0x73,0x74,0x73,0x40,0x76,0x65,0x72,0x69,0x73,0x69,0x67,0x6E,0x2E,0x63,0x6F,0x6D,0x3B,0x20,0x6F,0x72,0x20,0x62,0x79,0x20,0x6D,0x61,0x69,0x6C,0x20,0x61,0x74,0x20,0x56,0x65,0x72,0x69,0x53,0x69,0x67,0x6E,0x2C,0x20,0x49,0x6E,0x63,0x2E,0x2C,0x20,0x32,0x35,0x39,0x33,0x20,0x43,0x6F,0x61,0x73,0x74,0x20,0x41,0x76,0x65,0x2E,0x2C,0x20,0x4D,0x6F,0x75,0x6E,0x74,0x61,0x69,0x6E,0x20,0x56,0x69,0x65,0x77,0x2C,0x20,0x43,0x41,0x20,0x39,0x34,0x30,0x34,0x33,0x20,0x55,0x53,0x41,0x20,0x54,0x65, + 0x6C,0x2E,0x20,0x2B,0x31,0x20,0x28,0x34,0x31,0x35,0x29,0x20,0x39,0x36,0x31,0x2D,0x38,0x38,0x33,0x30,0x20,0x43,0x6F,0x70,0x79,0x72,0x69,0x67,0x68,0x74,0x20,0x28,0x63,0x29,0x20,0x31,0x39,0x39,0x36,0x20,0x56,0x65,0x72,0x69,0x53,0x69,0x67,0x6E,0x2C,0x20,0x49,0x6E,0x63,0x2E,0x20,0x20,0x41,0x6C,0x6C,0x20,0x52,0x69,0x67,0x68,0x74,0x73,0x20,0x52,0x65,0x73,0x65,0x72,0x76,0x65,0x64,0x2E,0x20,0x43,0x45,0x52,0x54,0x41,0x49,0x4E,0x20,0x57,0x41,0x52,0x52,0x41,0x4E,0x54,0x49,0x45,0x53,0x20,0x44,0x49,0x53,0x43, + 0x4C,0x41,0x49,0x4D,0x45,0x44,0x20,0x61,0x6E,0x64,0x20,0x4C,0x49,0x41,0x42,0x49,0x4C,0x49,0x54,0x59,0x20,0x4C,0x49,0x4D,0x49,0x54,0x45,0x44,0x2E,0xA0,0x0E,0x06,0x0C,0x60,0x86,0x48,0x01,0x86,0xF8,0x45,0x01,0x07,0x01,0x01,0x01,0xA1,0x0E,0x06,0x0C,0x60,0x86,0x48,0x01,0x86,0xF8,0x45,0x01,0x07,0x01,0x01,0x02,0x30,0x2C,0x30,0x2A,0x16,0x28,0x68,0x74,0x74,0x70,0x73,0x3A,0x2F,0x2F,0x77,0x77,0x77,0x2E,0x76,0x65,0x72,0x69,0x73,0x69,0x67,0x6E,0x2E,0x63,0x6F,0x6D,0x2F,0x72,0x65,0x70,0x6F,0x73,0x69,0x74,0x6F, + 0x72,0x79,0x2F,0x43,0x50,0x53,0x20,0x30,0x11,0x06,0x09,0x60,0x86,0x48,0x01,0x86,0xF8,0x42,0x01,0x01,0x04,0x04,0x03,0x02,0x07,0x80,0x30,0x36,0x06,0x09,0x60,0x86,0x48,0x01,0x86,0xF8,0x42,0x01,0x08,0x04,0x29,0x16,0x27,0x68,0x74,0x74,0x70,0x73,0x3A,0x2F,0x2F,0x77,0x77,0x77,0x2E,0x76,0x65,0x72,0x69,0x73,0x69,0x67,0x6E,0x2E,0x63,0x6F,0x6D,0x2F,0x72,0x65,0x70,0x6F,0x73,0x69,0x74,0x6F,0x72,0x79,0x2F,0x43,0x50,0x53,0x30,0x82,0x04,0x87,0x06,0x09,0x60,0x86,0x48,0x01,0x86,0xF8,0x42,0x01,0x0D,0x04,0x82,0x04, + 0x78,0x16,0x82,0x04,0x74,0x43,0x41,0x55,0x54,0x49,0x4F,0x4E,0x3A,0x20,0x54,0x68,0x65,0x20,0x43,0x6F,0x6D,0x6D,0x6F,0x6E,0x20,0x4E,0x61,0x6D,0x65,0x20,0x69,0x6E,0x20,0x74,0x68,0x69,0x73,0x20,0x43,0x6C,0x61,0x73,0x73,0x20,0x31,0x20,0x44,0x69,0x67,0x69,0x74,0x61,0x6C,0x20,0x0A,0x49,0x44,0x20,0x69,0x73,0x20,0x6E,0x6F,0x74,0x20,0x61,0x75,0x74,0x68,0x65,0x6E,0x74,0x69,0x63,0x61,0x74,0x65,0x64,0x20,0x62,0x79,0x20,0x56,0x65,0x72,0x69,0x53,0x69,0x67,0x6E,0x2E,0x20,0x49,0x74,0x20,0x6D,0x61,0x79,0x20,0x62, + 0x65,0x20,0x74,0x68,0x65,0x0A,0x68,0x6F,0x6C,0x64,0x65,0x72,0x27,0x73,0x20,0x72,0x65,0x61,0x6C,0x20,0x6E,0x61,0x6D,0x65,0x20,0x6F,0x72,0x20,0x61,0x6E,0x20,0x61,0x6C,0x69,0x61,0x73,0x2E,0x20,0x56,0x65,0x72,0x69,0x53,0x69,0x67,0x6E,0x20,0x64,0x6F,0x65,0x73,0x20,0x61,0x75,0x74,0x68,0x2D,0x0A,0x65,0x6E,0x74,0x69,0x63,0x61,0x74,0x65,0x20,0x74,0x68,0x65,0x20,0x65,0x2D,0x6D,0x61,0x69,0x6C,0x20,0x61,0x64,0x64,0x72,0x65,0x73,0x73,0x20,0x6F,0x66,0x20,0x74,0x68,0x65,0x20,0x68,0x6F,0x6C,0x64,0x65,0x72,0x2E, + 0x0A,0x0A,0x54,0x68,0x69,0x73,0x20,0x63,0x65,0x72,0x74,0x69,0x66,0x69,0x63,0x61,0x74,0x65,0x20,0x69,0x6E,0x63,0x6F,0x72,0x70,0x6F,0x72,0x61,0x74,0x65,0x73,0x20,0x62,0x79,0x20,0x72,0x65,0x66,0x65,0x72,0x65,0x6E,0x63,0x65,0x2C,0x20,0x61,0x6E,0x64,0x20,0x0A,0x69,0x74,0x73,0x20,0x75,0x73,0x65,0x20,0x69,0x73,0x20,0x73,0x74,0x72,0x69,0x63,0x74,0x6C,0x79,0x20,0x73,0x75,0x62,0x6A,0x65,0x63,0x74,0x20,0x74,0x6F,0x2C,0x20,0x74,0x68,0x65,0x20,0x56,0x65,0x72,0x69,0x53,0x69,0x67,0x6E,0x20,0x0A,0x43,0x65,0x72, + 0x74,0x69,0x66,0x69,0x63,0x61,0x74,0x69,0x6F,0x6E,0x20,0x50,0x72,0x61,0x63,0x74,0x69,0x63,0x65,0x20,0x53,0x74,0x61,0x74,0x65,0x6D,0x65,0x6E,0x74,0x20,0x28,0x43,0x50,0x53,0x29,0x2C,0x20,0x61,0x76,0x61,0x69,0x6C,0x61,0x62,0x6C,0x65,0x0A,0x69,0x6E,0x20,0x74,0x68,0x65,0x20,0x56,0x65,0x72,0x69,0x53,0x69,0x67,0x6E,0x20,0x72,0x65,0x70,0x6F,0x73,0x69,0x74,0x6F,0x72,0x79,0x20,0x61,0x74,0x3A,0x20,0x0A,0x68,0x74,0x74,0x70,0x73,0x3A,0x2F,0x2F,0x77,0x77,0x77,0x2E,0x76,0x65,0x72,0x69,0x73,0x69,0x67,0x6E,0x2E, + 0x63,0x6F,0x6D,0x3B,0x20,0x62,0x79,0x20,0x45,0x2D,0x6D,0x61,0x69,0x6C,0x20,0x61,0x74,0x0A,0x43,0x50,0x53,0x2D,0x72,0x65,0x71,0x75,0x65,0x73,0x74,0x73,0x40,0x76,0x65,0x72,0x69,0x73,0x69,0x67,0x6E,0x2E,0x63,0x6F,0x6D,0x3B,0x20,0x6F,0x72,0x20,0x62,0x79,0x20,0x6D,0x61,0x69,0x6C,0x20,0x61,0x74,0x20,0x56,0x65,0x72,0x69,0x53,0x69,0x67,0x6E,0x2C,0x0A,0x49,0x6E,0x63,0x2E,0x2C,0x20,0x32,0x35,0x39,0x33,0x20,0x43,0x6F,0x61,0x73,0x74,0x20,0x41,0x76,0x65,0x2E,0x2C,0x20,0x4D,0x6F,0x75,0x6E,0x74,0x61,0x69,0x6E, + 0x20,0x56,0x69,0x65,0x77,0x2C,0x20,0x43,0x41,0x20,0x39,0x34,0x30,0x34,0x33,0x20,0x55,0x53,0x41,0x0A,0x0A,0x43,0x6F,0x70,0x79,0x72,0x69,0x67,0x68,0x74,0x20,0x28,0x63,0x29,0x31,0x39,0x39,0x36,0x20,0x56,0x65,0x72,0x69,0x53,0x69,0x67,0x6E,0x2C,0x20,0x49,0x6E,0x63,0x2E,0x20,0x20,0x41,0x6C,0x6C,0x20,0x52,0x69,0x67,0x68,0x74,0x73,0x20,0x0A,0x52,0x65,0x73,0x65,0x72,0x76,0x65,0x64,0x2E,0x20,0x43,0x45,0x52,0x54,0x41,0x49,0x4E,0x20,0x57,0x41,0x52,0x52,0x41,0x4E,0x54,0x49,0x45,0x53,0x20,0x44,0x49,0x53,0x43, + 0x4C,0x41,0x49,0x4D,0x45,0x44,0x20,0x41,0x4E,0x44,0x20,0x0A,0x4C,0x49,0x41,0x42,0x49,0x4C,0x49,0x54,0x59,0x20,0x4C,0x49,0x4D,0x49,0x54,0x45,0x44,0x2E,0x0A,0x0A,0x57,0x41,0x52,0x4E,0x49,0x4E,0x47,0x3A,0x20,0x54,0x48,0x45,0x20,0x55,0x53,0x45,0x20,0x4F,0x46,0x20,0x54,0x48,0x49,0x53,0x20,0x43,0x45,0x52,0x54,0x49,0x46,0x49,0x43,0x41,0x54,0x45,0x20,0x49,0x53,0x20,0x53,0x54,0x52,0x49,0x43,0x54,0x4C,0x59,0x0A,0x53,0x55,0x42,0x4A,0x45,0x43,0x54,0x20,0x54,0x4F,0x20,0x54,0x48,0x45,0x20,0x56,0x45,0x52,0x49, + 0x53,0x49,0x47,0x4E,0x20,0x43,0x45,0x52,0x54,0x49,0x46,0x49,0x43,0x41,0x54,0x49,0x4F,0x4E,0x20,0x50,0x52,0x41,0x43,0x54,0x49,0x43,0x45,0x0A,0x53,0x54,0x41,0x54,0x45,0x4D,0x45,0x4E,0x54,0x2E,0x20,0x20,0x54,0x48,0x45,0x20,0x49,0x53,0x53,0x55,0x49,0x4E,0x47,0x20,0x41,0x55,0x54,0x48,0x4F,0x52,0x49,0x54,0x59,0x20,0x44,0x49,0x53,0x43,0x4C,0x41,0x49,0x4D,0x53,0x20,0x43,0x45,0x52,0x54,0x41,0x49,0x4E,0x0A,0x49,0x4D,0x50,0x4C,0x49,0x45,0x44,0x20,0x41,0x4E,0x44,0x20,0x45,0x58,0x50,0x52,0x45,0x53,0x53,0x20, + 0x57,0x41,0x52,0x52,0x41,0x4E,0x54,0x49,0x45,0x53,0x2C,0x20,0x49,0x4E,0x43,0x4C,0x55,0x44,0x49,0x4E,0x47,0x20,0x57,0x41,0x52,0x52,0x41,0x4E,0x54,0x49,0x45,0x53,0x0A,0x4F,0x46,0x20,0x4D,0x45,0x52,0x43,0x48,0x41,0x4E,0x54,0x41,0x42,0x49,0x4C,0x49,0x54,0x59,0x20,0x4F,0x52,0x20,0x46,0x49,0x54,0x4E,0x45,0x53,0x53,0x20,0x46,0x4F,0x52,0x20,0x41,0x20,0x50,0x41,0x52,0x54,0x49,0x43,0x55,0x4C,0x41,0x52,0x0A,0x50,0x55,0x52,0x50,0x4F,0x53,0x45,0x2C,0x20,0x41,0x4E,0x44,0x20,0x57,0x49,0x4C,0x4C,0x20,0x4E,0x4F, + 0x54,0x20,0x42,0x45,0x20,0x4C,0x49,0x41,0x42,0x4C,0x45,0x20,0x46,0x4F,0x52,0x20,0x43,0x4F,0x4E,0x53,0x45,0x51,0x55,0x45,0x4E,0x54,0x49,0x41,0x4C,0x2C,0x0A,0x50,0x55,0x4E,0x49,0x54,0x49,0x56,0x45,0x2C,0x20,0x41,0x4E,0x44,0x20,0x43,0x45,0x52,0x54,0x41,0x49,0x4E,0x20,0x4F,0x54,0x48,0x45,0x52,0x20,0x44,0x41,0x4D,0x41,0x47,0x45,0x53,0x2E,0x20,0x53,0x45,0x45,0x20,0x54,0x48,0x45,0x20,0x43,0x50,0x53,0x0A,0x46,0x4F,0x52,0x20,0x44,0x45,0x54,0x41,0x49,0x4C,0x53,0x2E,0x0A,0x0A,0x43,0x6F,0x6E,0x74,0x65,0x6E, + 0x74,0x73,0x20,0x6F,0x66,0x20,0x74,0x68,0x65,0x20,0x56,0x65,0x72,0x69,0x53,0x69,0x67,0x6E,0x20,0x72,0x65,0x67,0x69,0x73,0x74,0x65,0x72,0x65,0x64,0x0A,0x6E,0x6F,0x6E,0x76,0x65,0x72,0x69,0x66,0x69,0x65,0x64,0x53,0x75,0x62,0x6A,0x65,0x63,0x74,0x41,0x74,0x74,0x72,0x69,0x62,0x75,0x74,0x65,0x73,0x20,0x65,0x78,0x74,0x65,0x6E,0x73,0x69,0x6F,0x6E,0x20,0x76,0x61,0x6C,0x75,0x65,0x20,0x73,0x68,0x61,0x6C,0x6C,0x20,0x0A,0x6E,0x6F,0x74,0x20,0x62,0x65,0x20,0x63,0x6F,0x6E,0x73,0x69,0x64,0x65,0x72,0x65,0x64,0x20, + 0x61,0x73,0x20,0x61,0x63,0x63,0x75,0x72,0x61,0x74,0x65,0x20,0x69,0x6E,0x66,0x6F,0x72,0x6D,0x61,0x74,0x69,0x6F,0x6E,0x20,0x76,0x61,0x6C,0x69,0x64,0x61,0x74,0x65,0x64,0x20,0x0A,0x62,0x79,0x20,0x74,0x68,0x65,0x20,0x49,0x41,0x2E,0x30,0x0D,0x06,0x09,0x2A,0x86,0x48,0x86,0xF7,0x0D,0x01,0x01,0x04,0x05,0x00,0x03,0x81,0x81,0x00,0x2B,0x3D,0x44,0xC7,0x32,0x59,0xAE,0xF1,0x5F,0x8F,0x3F,0x87,0xE3,0x3E,0xEB,0x81,0x30,0xF8,0xA9,0x96,0xDB,0x01,0x42,0x0B,0x04,0xEF,0x37,0x02,0x3F,0xD4,0x20,0x61,0x58,0xC4,0x4A,0x3A, + 0x39,0xB3,0xFB,0xD9,0xF8,0xA5,0xC4,0x5E,0x33,0x5A,0x0E,0xFA,0x93,0x56,0x2F,0x6F,0xD6,0x61,0xA2,0xAF,0xA5,0x0C,0x1D,0xE2,0x41,0x65,0xF3,0x40,0x75,0x66,0x83,0xD2,0x5A,0xB4,0xB7,0x56,0x0B,0x8E,0x0D,0xA1,0x33,0x13,0x7D,0x49,0xC3,0xB1,0x00,0x68,0x83,0x7F,0xB5,0x66,0xD4,0x32,0x32,0xFE,0x8B,0x9A,0x5A,0xD6,0x01,0x72,0x31,0x5D,0x85,0x91,0xBC,0x93,0x9B,0x65,0x60,0x25,0xC6,0x1F,0xBC,0xDD,0x69,0x44,0x62,0xC2,0xB2,0x6F,0x46,0xAB,0x2F,0x20,0xA5,0x6F,0xDA,0x48,0x6C,0x9C }; + + static byte[] cert2 = { 0x30,0x82,0x02,0x1D,0x30,0x82,0x01,0x86,0x02,0x01,0x14,0x30,0x0D,0x06,0x09,0x2A,0x86,0x48,0x86,0xF7,0x0D,0x01,0x01,0x04,0x05,0x00,0x30,0x58,0x31,0x0B,0x30,0x09,0x06,0x03,0x55,0x04,0x06,0x13,0x02,0x43,0x41,0x31,0x1F,0x30,0x1D,0x06,0x03,0x55,0x04,0x03,0x13,0x16,0x4B,0x65,0x79,0x77,0x69,0x74,0x6E,0x65,0x73,0x73,0x20,0x43,0x61,0x6E,0x61,0x64,0x61,0x20,0x49,0x6E,0x63,0x2E,0x31,0x28,0x30,0x26,0x06,0x0A,0x2B,0x06,0x01,0x04,0x01,0x2A,0x02,0x0B,0x02,0x01,0x13,0x18,0x6B,0x65,0x79,0x77,0x69,0x74,0x6E,0x65,0x73, + 0x73,0x40,0x6B,0x65,0x79,0x77,0x69,0x74,0x6E,0x65,0x73,0x73,0x2E,0x63,0x61,0x30,0x1E,0x17,0x0D,0x39,0x36,0x30,0x35,0x30,0x37,0x30,0x30,0x30,0x30,0x30,0x30,0x5A,0x17,0x0D,0x39,0x39,0x30,0x35,0x30,0x37,0x30,0x30,0x30,0x30,0x30,0x30,0x5A,0x30,0x58,0x31,0x0B,0x30,0x09,0x06,0x03,0x55,0x04,0x06,0x13,0x02,0x43,0x41,0x31,0x1F,0x30,0x1D,0x06,0x03,0x55,0x04,0x03,0x13,0x16,0x4B,0x65,0x79,0x77,0x69,0x74,0x6E,0x65,0x73,0x73,0x20,0x43,0x61,0x6E,0x61,0x64,0x61,0x20,0x49,0x6E,0x63,0x2E,0x31,0x28,0x30,0x26,0x06, + 0x0A,0x2B,0x06,0x01,0x04,0x01,0x2A,0x02,0x0B,0x02,0x01,0x13,0x18,0x6B,0x65,0x79,0x77,0x69,0x74,0x6E,0x65,0x73,0x73,0x40,0x6B,0x65,0x79,0x77,0x69,0x74,0x6E,0x65,0x73,0x73,0x2E,0x63,0x61,0x30,0x81,0x9D,0x30,0x0D,0x06,0x09,0x2A,0x86,0x48,0x86,0xF7,0x0D,0x01,0x01,0x01,0x05,0x00,0x03,0x81,0x8B,0x00,0x30,0x81,0x87,0x02,0x81,0x81,0x00,0xCD,0x23,0xFA,0x2A,0xE1,0xED,0x98,0xF4,0xE9,0xD0,0x93,0x3E,0xD7,0x7A,0x80,0x02,0x4C,0xCC,0xC1,0x02,0xAF,0x5C,0xB6,0x1F,0x7F,0xFA,0x57,0x42,0x6F,0x30,0xD1,0x20,0xC5,0xB5, + 0x21,0x07,0x40,0x2C,0xA9,0x86,0xC2,0xF3,0x64,0x84,0xAE,0x3D,0x85,0x2E,0xED,0x85,0xBD,0x54,0xB0,0x18,0x28,0xEF,0x6A,0xF8,0x1B,0xE7,0x0B,0x16,0x1F,0x93,0x25,0x4F,0xC7,0xF8,0x8E,0xC3,0xB9,0xCA,0x98,0x84,0x0E,0x55,0xD0,0x2F,0xEF,0x78,0x77,0xC5,0x72,0x28,0x5F,0x60,0xBF,0x19,0x2B,0xD1,0x72,0xA2,0xB7,0xD8,0x3F,0xE0,0x97,0x34,0x5A,0x01,0xBD,0x04,0x9C,0xC8,0x78,0x45,0xCD,0x93,0x8D,0x15,0xF2,0x76,0x10,0x11,0xAB,0xB8,0x5B,0x2E,0x9E,0x52,0xDD,0x81,0x3E,0x9C,0x64,0xC8,0x29,0x93,0x02,0x01,0x03,0x30,0x0D,0x06, + 0x09,0x2A,0x86,0x48,0x86,0xF7,0x0D,0x01,0x01,0x04,0x05,0x00,0x03,0x81,0x81,0x00,0x32,0x1A,0x35,0xBA,0xBF,0x43,0x27,0xD6,0xB4,0xD4,0xB8,0x76,0xE5,0xE3,0x9B,0x4D,0x6C,0xC0,0x86,0xC9,0x77,0x35,0xBA,0x6B,0x16,0x2D,0x13,0x46,0x4A,0xB0,0x32,0x53,0xA1,0x5B,0x5A,0xE9,0x99,0xE2,0x0C,0x86,0x88,0x17,0x4E,0x0D,0xFE,0x82,0xAC,0x4E,0x47,0xEF,0xFB,0xFF,0x39,0xAC,0xEE,0x35,0xC8,0xFA,0x52,0x37,0x0A,0x49,0xAD,0x59,0xAD,0xE2,0x8A,0xA9,0x1C,0xC6,0x5F,0x1F,0xF8,0x6F,0x73,0x7E,0xCD,0xA0,0x31,0xE8,0x0C,0xBE,0xF5,0x4D, + 0xD9,0xB2,0xAB,0x8A,0x12,0xB6,0x30,0x78,0x68,0x11,0x7C,0x0D,0xF1,0x49,0x4D,0xA3,0xFD,0xB2,0xE9,0xFF,0x1D,0xF0,0x91,0xFA,0x54,0x85,0xFF,0x33,0x90,0xE8,0xC1,0xBF,0xA4,0x9B,0xA4,0x62,0x46,0xBD,0x61,0x12,0x59,0x98,0x41,0x89 }; + + static byte[] cert3 = { 0x30,0x82,0x03,0x04,0x30,0x82,0x02,0xC4,0xA0,0x03,0x02,0x01,0x02,0x02,0x01,0x03,0x30,0x09,0x06,0x07,0x2A,0x86,0x48,0xCE,0x38,0x04,0x03,0x30,0x51,0x31,0x0B,0x30,0x09,0x06,0x03,0x55,0x04,0x06,0x13,0x02,0x55,0x53,0x31,0x18,0x30,0x16,0x06,0x03,0x55,0x04,0x0A,0x13,0x0F,0x55,0x2E,0x53,0x2E,0x20,0x47,0x6F,0x76,0x65,0x72,0x6E,0x6D,0x65,0x6E,0x74,0x31,0x0C,0x30,0x0A,0x06,0x03,0x55,0x04,0x0B,0x13,0x03,0x44,0x6F,0x44,0x31,0x1A,0x30,0x18,0x06,0x03,0x55,0x04,0x03,0x13,0x11,0x41,0x72,0x6D,0x65,0x64,0x20,0x46,0x6F, + 0x72,0x63,0x65,0x73,0x20,0x52,0x6F,0x6F,0x74,0x30,0x1E,0x17,0x0D,0x30,0x30,0x31,0x30,0x32,0x35,0x30,0x30,0x30,0x30,0x30,0x30,0x5A,0x17,0x0D,0x30,0x33,0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5A,0x30,0x51,0x31,0x0B,0x30,0x09,0x06,0x03,0x55,0x04,0x06,0x13,0x02,0x55,0x53,0x31,0x18,0x30,0x16,0x06,0x03,0x55,0x04,0x0A,0x13,0x0F,0x55,0x2E,0x53,0x2E,0x20,0x47,0x6F,0x76,0x65,0x72,0x6E,0x6D,0x65,0x6E,0x74,0x31,0x0C,0x30,0x0A,0x06,0x03,0x55,0x04,0x0B,0x13,0x03,0x44,0x6F,0x44,0x31,0x1A,0x30,0x18, + 0x06,0x03,0x55,0x04,0x03,0x13,0x11,0x41,0x72,0x6D,0x65,0x64,0x20,0x46,0x6F,0x72,0x63,0x65,0x73,0x20,0x52,0x6F,0x6F,0x74,0x30,0x82,0x01,0xB6,0x30,0x82,0x01,0x2B,0x06,0x07,0x2A,0x86,0x48,0xCE,0x38,0x04,0x01,0x30,0x82,0x01,0x1E,0x02,0x81,0x81,0x00,0x90,0x89,0x3E,0x18,0x1B,0xFE,0xA3,0x1D,0x16,0x89,0x00,0xB4,0xD5,0x40,0x82,0x4C,0x2E,0xEC,0x3D,0x66,0x0D,0x0D,0xB9,0x17,0x40,0x6E,0x3A,0x5C,0x03,0x7B,0x1B,0x93,0x28,0x0C,0xEF,0xB9,0x97,0xE3,0xA1,0xEB,0xE2,0xA3,0x7C,0x61,0xDD,0x6F,0xD5,0xAD,0x15,0x69,0x00, + 0x16,0xB2,0xC3,0x08,0x3D,0xC4,0x59,0xC6,0xF2,0x70,0xA5,0xB0,0xF5,0x1F,0x1D,0xF4,0xB0,0x15,0xDA,0x7E,0x28,0x39,0x24,0x99,0x36,0x5B,0xEC,0x39,0x25,0xFA,0x92,0x49,0x65,0xD2,0x43,0x05,0x6A,0x9E,0xA3,0x7B,0xF0,0xDE,0xA3,0x2F,0xD3,0x6F,0x3A,0xF9,0x35,0xC3,0x29,0xD4,0x45,0x6C,0x56,0x9A,0xDE,0x36,0x6E,0xFE,0x12,0x68,0x96,0x7B,0x45,0x1D,0x2C,0xFF,0xB9,0x2D,0xF5,0x52,0x8C,0xDF,0x3E,0x2F,0x63,0x02,0x15,0x00,0x81,0xA9,0xB5,0xD0,0x04,0xF2,0x9B,0xA7,0xD8,0x55,0x4C,0x3B,0x32,0xA1,0x45,0x32,0x4F,0xF5,0x51,0xDD, + 0x02,0x81,0x80,0x64,0x7A,0x88,0x0B,0xF2,0x3E,0x91,0x81,0x59,0x9C,0xF4,0xEA,0xC6,0x7B,0x0E,0xBE,0xEA,0x05,0xE8,0x77,0xFD,0x20,0x34,0x87,0xA1,0xC4,0x69,0xF6,0xC8,0x8B,0x19,0xDA,0xCD,0xFA,0x21,0x8A,0x57,0xA9,0x7A,0x26,0x0A,0x56,0xD4,0xED,0x4B,0x1B,0x7C,0x70,0xED,0xB4,0xE6,0x7A,0x6A,0xDE,0xD3,0x29,0xE2,0xE9,0x9A,0x33,0xED,0x09,0x8D,0x9E,0xDF,0xDA,0x2E,0x4A,0xC1,0x50,0x92,0xEE,0x2F,0xE5,0x5A,0xF3,0x85,0x62,0x6A,0x48,0xDC,0x1B,0x02,0x98,0xA6,0xB0,0xD1,0x09,0x4B,0x10,0xD1,0xF0,0xFA,0xE0,0xB1,0x1D,0x13, + 0x54,0x4B,0xC0,0xA8,0x40,0xEF,0x71,0xE8,0x56,0x6B,0xA2,0x29,0xCB,0x1E,0x09,0x7D,0x27,0x39,0x91,0x3B,0x20,0x4F,0x98,0x39,0xE8,0x39,0xCA,0x98,0xC5,0xAF,0x54,0x03,0x81,0x84,0x00,0x02,0x81,0x80,0x54,0xA8,0x88,0xB5,0x8F,0x01,0x56,0xCE,0x18,0x8F,0xA6,0xD6,0x7C,0x29,0x29,0x75,0x45,0xE8,0x31,0xA4,0x07,0x17,0xED,0x1E,0x5D,0xB2,0x7B,0xBB,0xCE,0x3C,0x97,0x67,0x1E,0x88,0x0A,0xFE,0x7D,0x00,0x22,0x27,0x1D,0x66,0xEE,0xF6,0x1B,0xB6,0x95,0x7F,0x5A,0xFF,0x06,0x34,0x02,0x43,0xC3,0x83,0xC4,0x66,0x2C,0xA1,0x05,0x0E, + 0x68,0xB3,0xCA,0xDC,0xD3,0xF9,0x0C,0xC0,0x66,0xDF,0x85,0x84,0x4B,0x20,0x5D,0x41,0xAC,0xC0,0xEC,0x37,0x92,0x0E,0x97,0x19,0xBF,0x53,0x35,0x63,0x27,0x18,0x33,0x35,0x42,0x4D,0xF0,0x2D,0x6D,0xA7,0xA4,0x98,0xAA,0x57,0xF3,0xD2,0xB8,0x6E,0x4E,0x8F,0xFF,0xBE,0x6F,0x4E,0x0F,0x0B,0x44,0x24,0xEE,0xDF,0x4C,0x22,0x5B,0x44,0x98,0x94,0xCB,0xB8,0xA3,0x2F,0x30,0x2D,0x30,0x1D,0x06,0x03,0x55,0x1D,0x0E,0x04,0x16,0x04,0x14,0x9D,0x2D,0x73,0xC3,0xB8,0xE3,0x4D,0x29,0x28,0xC3,0x65,0xBE,0xA9,0x98,0xCB,0xD6,0x8A,0x06,0x68, + 0x9C,0x30,0x0C,0x06,0x03,0x55,0x1D,0x13,0x04,0x05,0x30,0x03,0x01,0x01,0xFF,0x30,0x09,0x06,0x07,0x2A,0x86,0x48,0xCE,0x38,0x04,0x03,0x03,0x2F,0x00,0x30,0x2C,0x02,0x14,0x5A,0x1B,0x2D,0x08,0x0E,0xE6,0x99,0x38,0x8F,0xB5,0x09,0xC9,0x89,0x79,0x7E,0x01,0x30,0xBD,0xCE,0xF0,0x02,0x14,0x71,0x7B,0x08,0x51,0x97,0xCE,0x4D,0x1F,0x6A,0x84,0x47,0x3A,0xC0,0xBD,0x13,0x89,0x81,0xB9,0x01,0x97 }; + + static byte[] x509crl = { 0x30, 0x82, 0x01, 0x05, 0x30, 0x72, 0x02, 0x01, 0x01, 0x30, 0x0B, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x05, 0x30, 0x51, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x13, 0x0F, 0x55, 0x2E, 0x53, 0x2E, 0x20, 0x47, 0x6F, 0x76, 0x65, 0x72, 0x6E, 0x6D, 0x65, 0x6E, 0x74, 0x31, 0x0C, 0x30, 0x0A, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13, 0x03, 0x44, 0x6F, 0x44, 0x31, 0x1A, 0x30, 0x18, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x11, 0x41, 0x72, 0x6D, 0x65, 0x64, 0x20, 0x46, 0x6F, 0x72, 0x63, 0x65, 0x73, 0x20, 0x52, 0x6F, 0x6F, 0x74, 0x17, 0x0D, 0x30, 0x32, 0x31, 0x30, 0x31, 0x31, 0x31, 0x33, 0x31, 0x32, 0x35, 0x30, 0x5A, 0x30, 0x0B, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x05, 0x03, 0x81, 0x81, 0x00, 0x7D, 0xA2, 0xD1, 0x19, 0x6D, 0x0F, 0x0F, 0xCB, 0xE4, 0xA3, 0xBE, 0xE0, 0x36, 0x0A, 0xF3, 0x4C, 0x9B, 0xAF, 0xE6, 0x4F, 0xF6, 0xE3, 0xAF, 0xCF, 0x55, 0xF3, 0xC6, 0xDB, 0xAB, 0x4C, 0x16, 0x32, 0xAA, 0x73, 0xAD, 0xCC, 0xDC, 0x32, 0x33, 0x60, 0xDF, 0x8B, 0xCC, 0x93, 0xB5, 0x4F, 0x6A, 0xEC, 0x70, 0x53, 0xAF, 0xCF, 0x07, 0x0F, 0xA0, 0xCD, 0x66, 0xAC, 0x00, 0x57, 0xC6, 0x5C, 0x5D, 0x21, 0xB1, 0xBD, 0x30, 0x89, 0x8E, 0x77, 0x8D, 0xD4, 0x69, 0x7E, 0xC0, 0x36, 0x7E, 0xD2, 0xD8, 0x20, 0x71, 0x08, 0x80, 0xD2, 0xCB, 0x74, 0x8B, 0xD8, 0x42, 0x17, 0x04, 0x99, 0x80, 0xA4, 0x52, 0x70, 0x2E, 0xC0, 0xE3, 0x8C, 0x0B, 0xFF, 0x79, 0xB7, 0x45, 0x77, 0xDC, 0xC5, 0xCF, 0x43, 0x98, 0x91, 0x7D, 0xF1, 0x01, 0xF7, 0x53, 0xD7, 0xC6, 0x51, 0x35, 0xF0, 0x89, 0xCC, 0xC1, 0xFF, 0xE2, 0x89 }; + + public KeyInfoX509DataTest () : base ("System.Security.Cryptography.Xml.KeyInfoX509Data testsuite") {} + public KeyInfoX509DataTest (string name) : base (name) {} + + protected override void SetUp () {} + + protected override void TearDown () {} + + public static ITest Suite { + get { + return new TestSuite (typeof (KeyInfoX509DataTest)); + } + } + + public void TestConstructors () + { + KeyInfoX509Data data1 = new KeyInfoX509Data (); + try { + XmlElement invalid = data1.GetXml (); + Fail ("Expected CryptographicException but got none"); + } + catch (CryptographicException) { + // can't compare at this stage - so empty test is impossible + } + catch (Exception e) { + Fail ("Expected CryptographicException but got: " + e.ToString ()); + } + + KeyInfoX509Data data2 = new KeyInfoX509Data (cert); + + XmlElement xel = data2.GetXml (); + string s = "<X509Data xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><X509Certificate>MIIJuTCCCSKgAwIBAgIQIAs1Xs7EsGO33sY0uXA0RDANBgkqhkiG9w0BAQQFADBiMREwDwYDVQQHEwhJbnRlcm5ldDEXMBUGA1UEChMOVmVyaVNpZ24sIEluYy4xNDAyBgNVBAsTK1ZlcmlTaWduIENsYXNzIDEgQ0EgLSBJbmRpdmlkdWFsIFN1YnNjcmliZXIwHhcNOTYwODIxMDAwMDAwWhcNOTcwODIwMjM1OTU5WjCCAQoxETAPBgNVBAcTCEludGVybmV0MRcwFQYDVQQKEw5WZXJpU2lnbiwgSW5jLjE0MDIGA1UECxMrVmVyaVNpZ24gQ2xhc3MgMSBDQSAtIEluZGl2aWR1YWwgU3Vic2NyaWJlcjFGMEQGA1UECxM9d3d3LnZlcmlzaWduLmNvbS9yZXBvc2l0b3J5L0NQUyBJbmNvcnAuIGJ5IFJlZi4sTElBQi5MVEQoYyk5NjEmMCQGA1UECxMdRGlnaXRhbCBJRCBDbGFzcyAxIC0gTmV0c2NhcGUxFjAUBgNVBAMTDURhdmlkIFQuIEdyYXkxHjAcBgkqhkiG9w0BCQEWD2RhdmlkQGZvcm1hbC5pZTBcMA0GCSqGSIb3DQEBAQUAA0sAMEgCQQDFgQei6w+4//j4HO4y/78SNWr5a8i+L/s+rwRRSqzdECmozUBbZh6Y7/JMd/qPhtEhZ5JESsSJyYPPiJ9v4jI1AgMBAAGjggcIMIIHBDAJBgNVHRMEAjAAMIICHwYDVR0DBIICFjCCAhIwggIOMIICCgYLYIZIAYb4RQEHAQEwggH5FoIBp1RoaXMgY2VydGlmaWNhdGUgaW5jb3Jwb3JhdGVzIGJ5IHJlZmVyZW5jZSwgYW5kIGl0cyB1c2UgaXMgc3RyaWN0bHkgc3ViamVjdCB0bywgdGhlIFZlcmlTaWduIENlcnRpZmljYXRpb24gUHJhY3RpY2UgU3RhdGVtZW50IChDUFMpLCBhdmFpbGFibGUgYXQ6IGh0dHBzOi8vd3d3LnZlcmlzaWduLmNvbS9DUFM7IGJ5IEUtbWFpbCBhdCBDUFMtcmVxdWVzdHNAdmVyaXNpZ24uY29tOyBvciBieSBtYWlsIGF0IFZlcmlTaWduLCBJbmMuLCAyNTkzIENvYXN0IEF2ZS4sIE1vdW50YWluIFZpZXcsIENBIDk0MDQzIFVTQSBUZWwuICsxICg0MTUpIDk2MS04ODMwIENvcHlyaWdodCAoYykgMTk5NiBWZXJpU2lnbiwgSW5jLiAgQWxsIFJpZ2h0cyBSZXNlcnZlZC4gQ0VSVEFJTiBXQVJSQU5USUVTIERJU0NMQUlNRUQgYW5kIExJQUJJTElUWSBMSU1JVEVELqAOBgxghkgBhvhFAQcBAQGhDgYMYIZIAYb4RQEHAQECMCwwKhYoaHR0cHM6Ly93d3cudmVyaXNpZ24uY29tL3JlcG9zaXRvcnkvQ1BTIDARBglghkgBhvhCAQEEBAMCB4AwNgYJYIZIAYb4QgEIBCkWJ2h0dHBzOi8vd3d3LnZlcmlzaWduLmNvbS9yZXBvc2l0b3J5L0NQUzCCBIcGCWCGSAGG+EIBDQSCBHgWggR0Q0FVVElPTjogV"; + s += "GhlIENvbW1vbiBOYW1lIGluIHRoaXMgQ2xhc3MgMSBEaWdpdGFsIApJRCBpcyBub3QgYXV0aGVudGljYXRlZCBieSBWZXJpU2lnbi4gSXQgbWF5IGJlIHRoZQpob2xkZXIncyByZWFsIG5hbWUgb3IgYW4gYWxpYXMuIFZlcmlTaWduIGRvZXMgYXV0aC0KZW50aWNhdGUgdGhlIGUtbWFpbCBhZGRyZXNzIG9mIHRoZSBob2xkZXIuCgpUaGlzIGNlcnRpZmljYXRlIGluY29ycG9yYXRlcyBieSByZWZlcmVuY2UsIGFuZCAKaXRzIHVzZSBpcyBzdHJpY3RseSBzdWJqZWN0IHRvLCB0aGUgVmVyaVNpZ24gCkNlcnRpZmljYXRpb24gUHJhY3RpY2UgU3RhdGVtZW50IChDUFMpLCBhdmFpbGFibGUKaW4gdGhlIFZlcmlTaWduIHJlcG9zaXRvcnkgYXQ6IApodHRwczovL3d3dy52ZXJpc2lnbi5jb207IGJ5IEUtbWFpbCBhdApDUFMtcmVxdWVzdHNAdmVyaXNpZ24uY29tOyBvciBieSBtYWlsIGF0IFZlcmlTaWduLApJbmMuLCAyNTkzIENvYXN0IEF2ZS4sIE1vdW50YWluIFZpZXcsIENBIDk0MDQzIFVTQQoKQ29weXJpZ2h0IChjKTE5OTYgVmVyaVNpZ24sIEluYy4gIEFsbCBSaWdodHMgClJlc2VydmVkLiBDRVJUQUlOIFdBUlJBTlRJRVMgRElTQ0xBSU1FRCBBTkQgCkxJQUJJTElUWSBMSU1JVEVELgoKV0FSTklORzogVEhFIFVTRSBPRiBUSElTIENFUlRJRklDQVRFIElTIFNUUklDVExZClNVQkpFQ1QgVE8gVEhFIFZFUklTSUdOIENFUlRJRklDQVRJT04gUFJBQ1RJQ0UKU1RBVEVNRU5ULiAgVEhFIElTU1VJTkcgQVVUSE9SSVRZIERJU0NMQUlNUyBDRVJUQUlOCklNUExJRUQgQU5EIEVYUFJFU1MgV0FSUkFOVElFUywgSU5DTFVESU5HIFdBUlJBTlRJRVMKT0YgTUVSQ0hBTlRBQklMSVRZIE9SIEZJVE5FU1MgRk9SIEEgUEFSVElDVUxBUgpQVVJQT1NFLCBBTkQgV0lMTCBOT1QgQkUgTElBQkxFIEZPUiBDT05TRVFVRU5USUFMLApQVU5JVElWRSwgQU5EIENFUlRBSU4gT1RIRVIgREFNQUdFUy4gU0VFIFRIRSBDUFMKRk9SIERFVEFJTFMuCgpDb250ZW50cyBvZiB0aGUgVmVyaVNpZ24gcmVnaXN0ZXJlZApub252ZXJpZmllZFN1YmplY3RBdHRyaWJ1dGVzIGV4dGVuc2lvbiB2YWx1ZSBzaGFsbCAKbm90IGJlIGNvbnNpZGVyZWQgYXMgYWNjdXJhdGUgaW5mb3JtYXRpb24gdmFsaWRhdGVkIApieSB0aGUgSUEuMA0GCSqGSIb3DQEBBAUAA4GBACs9RMcyWa7xX48/h+M+64Ew+KmW2wFCCwTvNwI/1CBhWMRKOjmz+9n4pcReM1oO+pNWL2/WYaKvpQwd4kFl80B1ZoPSWrS3VguODaEzE31Jw7EAaIN/tWbUMjL+i5pa1gFyMV2FkbyTm2VgJcYfvN1pRGLCsm9Gqy8gpW/aSGyc</X509Certificate></X509Data>"; + AssertEquals ("1 cert", s, (data2.GetXml ().OuterXml)); + + data1.LoadXml (xel); + AssertEquals ("data1==data2", (data1.GetXml ().OuterXml), (data2.GetXml ().OuterXml)); + + X509Certificate x509 = new X509Certificate (cert); + KeyInfoX509Data data3 = new KeyInfoX509Data (x509); + AssertEquals ("data2==data3", (data2.GetXml ().OuterXml), (data3.GetXml ().OuterXml)); + } + + public void TestComplex () + { + KeyInfoX509Data data1 = new KeyInfoX509Data (cert); + KeyInfoX509Data data2 = new KeyInfoX509Data (); + + XmlElement xel = data1.GetXml (); + data2.LoadXml (xel); + + AssertEquals ("data1==data2", (data1.GetXml ().OuterXml), (data2.GetXml ().OuterXml)); + byte[] c = (data1.Certificates[0] as X509Certificate).GetRawCertData(); + AllTests.AssertEquals ("Certificate[0]", cert, c); + + // add a second X.509 certificate + X509Certificate x509 = new X509Certificate (cert2); + data1.AddCertificate (x509); + xel = data1.GetXml (); + data2.LoadXml (xel); + AssertEquals ("data1==data2", (data1.GetXml ().OuterXml), (data2.GetXml ().OuterXml)); + c = (data1.Certificates [1] as X509Certificate).GetRawCertData(); + AllTests.AssertEquals ("Certificate[1]", cert2, c); + + // add properties from a third X.509 certificate + x509 = new X509Certificate (cert3); + data1.AddIssuerSerial (x509.GetIssuerName (), x509.GetSerialNumberString ()); + xel = data1.GetXml (); + data2.LoadXml (xel); + AssertEquals ("data1==data2", (data1.GetXml ().OuterXml), (data2.GetXml ().OuterXml)); + // TODO: The type of IssuerSerial isn't documented + + // X509Certificate doesn't export SubjectKeyId so we must improvise + byte[] skid = { 0xDE, 0xAD, 0xC0, 0xDE }; + data1.AddSubjectKeyId (skid); + xel = data1.GetXml (); + data2.LoadXml (xel); + AssertEquals ("data1==data2", (data1.GetXml ().OuterXml), (data2.GetXml ().OuterXml)); + AllTests.AssertEquals ("SubjectKeyId", skid, data1.SubjectKeyIds[0]); + + data1.AddSubjectName (x509.GetName ()); + xel = data1.GetXml (); + data2.LoadXml (xel); + AssertEquals ("data1==data2", (data1.GetXml ().OuterXml), (data2.GetXml ().OuterXml)); + string s = (string) data1.SubjectNames [0]; + AllTests.AssertEquals ("SubjectName", x509.GetName (), s); + } + + // There's a bug in the framework 1.0 where a CRL entry cannot be included + // in a same X509Data entry. This is not per XMLDSIG spec so Mono will accept + // this (anyway the bug should be fixed by Microsoft in future release). + public void TestCRL () + { + KeyInfoX509Data data1 = new KeyInfoX509Data (); + data1.CRL = x509crl; + XmlElement xel = data1.GetXml (); + + KeyInfoX509Data data2 = new KeyInfoX509Data (); + data2.LoadXml (xel); + + AssertEquals ("data1==data2", (data1.GetXml ().OuterXml), (data2.GetXml ().OuterXml)); + AllTests.AssertEquals ("crl1==crl2", data1.CRL, data2.CRL); + } + + public void TestImportX509Data () + { + string simple = "<X509Data xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><X509Certificate>MIIJuTCCCSKgAwIBAgIQIAs1Xs7EsGO33sY0uXA0RDANBgkqhkiG9w0BAQQFADBiMREwDwYDVQQHEwhJbnRlcm5ldDEXMBUGA1UEChMOVmVyaVNpZ24sIEluYy4xNDAyBgNVBAsTK1ZlcmlTaWduIENsYXNzIDEgQ0EgLSBJbmRpdmlkdWFsIFN1YnNjcmliZXIwHhcNOTYwODIxMDAwMDAwWhcNOTcwODIwMjM1OTU5WjCCAQoxETAPBgNVBAcTCEludGVybmV0MRcwFQYDVQQKEw5WZXJpU2lnbiwgSW5jLjE0MDIGA1UECxMrVmVyaVNpZ24gQ2xhc3MgMSBDQSAtIEluZGl2aWR1YWwgU3Vic2NyaWJlcjFGMEQGA1UECxM9d3d3LnZlcmlzaWduLmNvbS9yZXBvc2l0b3J5L0NQUyBJbmNvcnAuIGJ5IFJlZi4sTElBQi5MVEQoYyk5NjEmMCQGA1UECxMdRGlnaXRhbCBJRCBDbGFzcyAxIC0gTmV0c2NhcGUxFjAUBgNVBAMTDURhdmlkIFQuIEdyYXkxHjAcBgkqhkiG9w0BCQEWD2RhdmlkQGZvcm1hbC5pZTBcMA0GCSqGSIb3DQEBAQUAA0sAMEgCQQDFgQei6w+4//j4HO4y/78SNWr5a8i+L/s+rwRRSqzdECmozUBbZh6Y7/JMd/qPhtEhZ5JESsSJyYPPiJ9v4jI1AgMBAAGjggcIMIIHBDAJBgNVHRMEAjAAMIICHwYDVR0DBIICFjCCAhIwggIOMIICCgYLYIZIAYb4RQEHAQEwggH5FoIBp1RoaXMgY2VydGlmaWNhdGUgaW5jb3Jwb3JhdGVzIGJ5IHJlZmVyZW5jZSwgYW5kIGl0cyB1c2UgaXMgc3RyaWN0bHkgc3ViamVjdCB0bywgdGhlIFZlcmlTaWduIENlcnRpZmljYXRpb24gUHJhY3RpY2UgU3RhdGVtZW50IChDUFMpLCBhdmFpbGFibGUgYXQ6IGh0dHBzOi8vd3d3LnZlcmlzaWduLmNvbS9DUFM7IGJ5IEUtbWFpbCBhdCBDUFMtcmVxdWVzdHNAdmVyaXNpZ24uY29tOyBvciBieSBtYWlsIGF0IFZlcmlTaWduLCBJbmMuLCAyNTkzIENvYXN0IEF2ZS4sIE1vdW50YWluIFZpZXcsIENBIDk0MDQzIFVTQSBUZWwuICsxICg0MTUpIDk2MS04ODMwIENvcHlyaWdodCAoYykgMTk5NiBWZXJpU2lnbiwgSW5jLiAgQWxsIFJpZ2h0cyBSZXNlcnZlZC4gQ0VSVEFJTiBXQVJSQU5USUVTIERJU0NMQUlNRUQgYW5kIExJQUJJTElUWSBMSU1JVEVELqAOBgxghkgBhvhFAQcBAQGhDgYMYIZIAYb4RQEHAQECMCwwKhYoaHR0cHM6Ly93d3cudmVyaXNpZ24uY29tL3JlcG9zaXRvcnkvQ1BTIDARBglghkgBhvhCAQEEBAMCB4AwNgYJYIZIAYb4QgEIBCkWJ2h0dHBzOi8vd3d3LnZlcmlzaWduLmNvbS9yZXBvc2l0b3J5L0NQUzCCBIcGCWCGSAGG+EIBDQSCBHgWggR0Q0FVVElPTjogV"; + simple += "GhlIENvbW1vbiBOYW1lIGluIHRoaXMgQ2xhc3MgMSBEaWdpdGFsIApJRCBpcyBub3QgYXV0aGVudGljYXRlZCBieSBWZXJpU2lnbi4gSXQgbWF5IGJlIHRoZQpob2xkZXIncyByZWFsIG5hbWUgb3IgYW4gYWxpYXMuIFZlcmlTaWduIGRvZXMgYXV0aC0KZW50aWNhdGUgdGhlIGUtbWFpbCBhZGRyZXNzIG9mIHRoZSBob2xkZXIuCgpUaGlzIGNlcnRpZmljYXRlIGluY29ycG9yYXRlcyBieSByZWZlcmVuY2UsIGFuZCAKaXRzIHVzZSBpcyBzdHJpY3RseSBzdWJqZWN0IHRvLCB0aGUgVmVyaVNpZ24gCkNlcnRpZmljYXRpb24gUHJhY3RpY2UgU3RhdGVtZW50IChDUFMpLCBhdmFpbGFibGUKaW4gdGhlIFZlcmlTaWduIHJlcG9zaXRvcnkgYXQ6IApodHRwczovL3d3dy52ZXJpc2lnbi5jb207IGJ5IEUtbWFpbCBhdApDUFMtcmVxdWVzdHNAdmVyaXNpZ24uY29tOyBvciBieSBtYWlsIGF0IFZlcmlTaWduLApJbmMuLCAyNTkzIENvYXN0IEF2ZS4sIE1vdW50YWluIFZpZXcsIENBIDk0MDQzIFVTQQoKQ29weXJpZ2h0IChjKTE5OTYgVmVyaVNpZ24sIEluYy4gIEFsbCBSaWdodHMgClJlc2VydmVkLiBDRVJUQUlOIFdBUlJBTlRJRVMgRElTQ0xBSU1FRCBBTkQgCkxJQUJJTElUWSBMSU1JVEVELgoKV0FSTklORzogVEhFIFVTRSBPRiBUSElTIENFUlRJRklDQVRFIElTIFNUUklDVExZClNVQkpFQ1QgVE8gVEhFIFZFUklTSUdOIENFUlRJRklDQVRJT04gUFJBQ1RJQ0UKU1RBVEVNRU5ULiAgVEhFIElTU1VJTkcgQVVUSE9SSVRZIERJU0NMQUlNUyBDRVJUQUlOCklNUExJRUQgQU5EIEVYUFJFU1MgV0FSUkFOVElFUywgSU5DTFVESU5HIFdBUlJBTlRJRVMKT0YgTUVSQ0hBTlRBQklMSVRZIE9SIEZJVE5FU1MgRk9SIEEgUEFSVElDVUxBUgpQVVJQT1NFLCBBTkQgV0lMTCBOT1QgQkUgTElBQkxFIEZPUiBDT05TRVFVRU5USUFMLApQVU5JVElWRSwgQU5EIENFUlRBSU4gT1RIRVIgREFNQUdFUy4gU0VFIFRIRSBDUFMKRk9SIERFVEFJTFMuCgpDb250ZW50cyBvZiB0aGUgVmVyaVNpZ24gcmVnaXN0ZXJlZApub252ZXJpZmllZFN1YmplY3RBdHRyaWJ1dGVzIGV4dGVuc2lvbiB2YWx1ZSBzaGFsbCAKbm90IGJlIGNvbnNpZGVyZWQgYXMgYWNjdXJhdGUgaW5mb3JtYXRpb24gdmFsaWRhdGVkIApieSB0aGUgSUEuMA0GCSqGSIb3DQEBBAUAA4GBACs9RMcyWa7xX48/h+M+64Ew+KmW2wFCCwTvNwI/1CBhWMRKOjmz+9n4pcReM1oO+pNWL2/WYaKvpQwd4kFl80B1ZoPSWrS3VguODaEzE31Jw7EAaIN/tWbUMjL+i5pa1gFyMV2FkbyTm2VgJcYfvN1pRGLCsm9Gqy8gpW/aSGyc</X509Certificate></X509Data>"; + XmlDocument doc = new XmlDocument (); + doc.LoadXml (simple); + + KeyInfoX509Data data1 = new KeyInfoX509Data (); + data1.LoadXml (doc.DocumentElement); + + // verify that proper XML is generated (equals to original) + string s = (data1.GetXml ().OuterXml); + AssertEquals ("Xml-Simple", simple, s); + + // verify that property is parsed correctly + byte[] c = (data1.Certificates[0] as X509Certificate).GetRawCertData(); + AllTests.AssertEquals ("Certificate[0]", cert, c); + + string complex = "<X509Data xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><X509IssuerSerial><X509IssuerName>C=US, O=U.S. Government, OU=DoD, CN=Armed Forces Root</X509IssuerName><X509SerialNumber>03</X509SerialNumber></X509IssuerSerial><X509SKI>3q3A3g==</X509SKI><X509SubjectName>C=US, O=U.S. Government, OU=DoD, CN=Armed Forces Root</X509SubjectName><X509Certificate>MIIJuTCCCSKgAwIBAgIQIAs1Xs7EsGO33sY0uXA0RDANBgkqhkiG9w0BAQQFADBiMREwDwYDVQQHEwhJbnRlcm5ldDEXMBUGA1UEChMOVmVyaVNpZ24sIEluYy4xNDAyBgNVBAsTK1ZlcmlTaWduIENsYXNzIDEgQ0EgLSBJbmRpdmlkdWFsIFN1YnNjcmliZXIwHhcNOTYwODIxMDAwMDAwWhcNOTcwODIwMjM1OTU5WjCCAQoxETAPBgNVBAcTCEludGVybmV0MRcwFQYDVQQKEw5WZXJpU2lnbiwgSW5jLjE0MDIGA1UECxMrVmVyaVNpZ24gQ2xhc3MgMSBDQSAtIEluZGl2aWR1YWwgU3Vic2NyaWJlcjFGMEQGA1UECxM9d3d3LnZlcmlzaWduLmNvbS9yZXBvc2l0b3J5L0NQUyBJbmNvcnAuIGJ5IFJlZi4sTElBQi5MVEQoYyk5NjEmMCQGA1UECxMdRGlnaXRhbCBJRCBDbGFzcyAxIC0gTmV0c2NhcGUxFjAUBgNVBAMTDURhdmlkIFQuIEdyYXkxHjAcBgkqhkiG9w0BCQEWD2RhdmlkQGZvcm1hbC5pZTBcMA0GCSqGSIb3DQEBAQUAA0sAMEgCQQDFgQei6w+4//j4HO4y/78SNWr5a8i+L/s+rwRRSqzdECmozUBbZh6Y7/JMd/qPhtEhZ5JESsSJyYPPiJ9v4jI1AgMBAAGjggcIMIIHBDAJBgNVHRMEAjAAMIICHwYDVR0DBIICFjCCAhIwggIOMIICCgYLYIZIAYb4RQEHAQEwggH5FoIBp1RoaXMgY2VydGlmaWNhdGUgaW5jb3Jwb3JhdGVzIGJ5IHJlZmVyZW5jZSwgYW5kIGl0cyB1c2UgaXMgc3RyaWN0bHkgc3ViamVjdCB0bywgdGhlIFZlcmlTaWduIENlcnRpZmljYXRpb24gUHJhY3RpY2UgU3RhdGVtZW50IChDUFMpLCBhdmFpbGFibGUgYXQ6IGh0dHBzOi8vd3d3LnZlcmlzaWduLmNvbS9DUFM7IGJ5IEUtbWFpbCBhdCBDUFMtcmVxdWVzdHNAdmVyaXNpZ24uY29tOyBvciBieSBtYWlsIGF0IFZlcmlTaWduLCBJbmMuLCAyNTkzIENvYXN0IEF2ZS4sIE1vdW50YWluIFZpZXcsIENBIDk0MDQzIFVTQSBUZWwuICsxICg0MTUpIDk2MS04ODMwIENvcHlyaWdodCAoYykgMTk5NiBWZXJpU2lnbiwgSW5jLiAgQWxsIFJpZ2h0cyBSZXNlcnZlZC4gQ0VSVEFJTiBXQVJSQU5USUVTIERJU0NMQUlNRUQgYW5kIExJQUJJTElUWSBMSU1JVEVELq"; + complex += "AOBgxghkgBhvhFAQcBAQGhDgYMYIZIAYb4RQEHAQECMCwwKhYoaHR0cHM6Ly93d3cudmVyaXNpZ24uY29tL3JlcG9zaXRvcnkvQ1BTIDARBglghkgBhvhCAQEEBAMCB4AwNgYJYIZIAYb4QgEIBCkWJ2h0dHBzOi8vd3d3LnZlcmlzaWduLmNvbS9yZXBvc2l0b3J5L0NQUzCCBIcGCWCGSAGG+EIBDQSCBHgWggR0Q0FVVElPTjogVGhlIENvbW1vbiBOYW1lIGluIHRoaXMgQ2xhc3MgMSBEaWdpdGFsIApJRCBpcyBub3QgYXV0aGVudGljYXRlZCBieSBWZXJpU2lnbi4gSXQgbWF5IGJlIHRoZQpob2xkZXIncyByZWFsIG5hbWUgb3IgYW4gYWxpYXMuIFZlcmlTaWduIGRvZXMgYXV0aC0KZW50aWNhdGUgdGhlIGUtbWFpbCBhZGRyZXNzIG9mIHRoZSBob2xkZXIuCgpUaGlzIGNlcnRpZmljYXRlIGluY29ycG9yYXRlcyBieSByZWZlcmVuY2UsIGFuZCAKaXRzIHVzZSBpcyBzdHJpY3RseSBzdWJqZWN0IHRvLCB0aGUgVmVyaVNpZ24gCkNlcnRpZmljYXRpb24gUHJhY3RpY2UgU3RhdGVtZW50IChDUFMpLCBhdmFpbGFibGUKaW4gdGhlIFZlcmlTaWduIHJlcG9zaXRvcnkgYXQ6IApodHRwczovL3d3dy52ZXJpc2lnbi5jb207IGJ5IEUtbWFpbCBhdApDUFMtcmVxdWVzdHNAdmVyaXNpZ24uY29tOyBvciBieSBtYWlsIGF0IFZlcmlTaWduLApJbmMuLCAyNTkzIENvYXN0IEF2ZS4sIE1vdW50YWluIFZpZXcsIENBIDk0MDQzIFVTQQoKQ29weXJpZ2h0IChjKTE5OTYgVmVyaVNpZ24sIEluYy4gIEFsbCBSaWdodHMgClJlc2VydmVkLiBDRVJUQUlOIFdBUlJBTlRJRVMgRElTQ0xBSU1FRCBBTkQgCkxJQUJJTElUWSBMSU1JVEVELgoKV0FSTklORzogVEhFIFVTRSBPRiBUSElTIENFUlRJRklDQVRFIElTIFNUUklDVExZClNVQkpFQ1QgVE8gVEhFIFZFUklTSUdOIENFUlRJRklDQVRJT04gUFJBQ1RJQ0UKU1RBVEVNRU5ULiAgVEhFIElTU1VJTkcgQVVUSE9SSVRZIERJU0NMQUlNUyBDRVJUQUlOCklNUExJRUQgQU5EIEVYUFJFU1MgV0FSUkFOVElFUywgSU5DTFVESU5HIFdBUlJBTlRJRVMKT0YgTUVSQ0hBTlRBQklMSVRZIE9SIEZJVE5FU1MgRk9SIEEgUEFSVElDVUxBUgpQVVJQT1NFLCBBTkQgV0lMTCBOT1QgQkUgTElBQkxFIEZPUiBDT05TRVFVRU5USUFMLApQVU5JVElWRSwgQU5EIENFUlRBSU4gT1RIRVIgREFNQUdFUy4gU0VFIFRIRSBDUFMKRk9SIERFVEFJTFMuCgpDb250ZW50cyBvZiB0aGUgVmVyaVNpZ24gcmVnaXN0ZXJlZApub252ZXJpZmllZFN1YmplY3RBdHRyaWJ1dGVzIGV4dGVuc2lvbiB2YWx1ZSBzaGFsbCAKbm90IGJlIGNvbnNpZGVyZWQgYXMgYWNjdXJhdGUgaW5mb3JtYXRpb24gdmF"; + complex += "saWRhdGVkIApieSB0aGUgSUEuMA0GCSqGSIb3DQEBBAUAA4GBACs9RMcyWa7xX48/h+M+64Ew+KmW2wFCCwTvNwI/1CBhWMRKOjmz+9n4pcReM1oO+pNWL2/WYaKvpQwd4kFl80B1ZoPSWrS3VguODaEzE31Jw7EAaIN/tWbUMjL+i5pa1gFyMV2FkbyTm2VgJcYfvN1pRGLCsm9Gqy8gpW/aSGyc</X509Certificate><X509Certificate>MIICHTCCAYYCARQwDQYJKoZIhvcNAQEEBQAwWDELMAkGA1UEBhMCQ0ExHzAdBgNVBAMTFktleXdpdG5lc3MgQ2FuYWRhIEluYy4xKDAmBgorBgEEASoCCwIBExhrZXl3aXRuZXNzQGtleXdpdG5lc3MuY2EwHhcNOTYwNTA3MDAwMDAwWhcNOTkwNTA3MDAwMDAwWjBYMQswCQYDVQQGEwJDQTEfMB0GA1UEAxMWS2V5d2l0bmVzcyBDYW5hZGEgSW5jLjEoMCYGCisGAQQBKgILAgETGGtleXdpdG5lc3NAa2V5d2l0bmVzcy5jYTCBnTANBgkqhkiG9w0BAQEFAAOBiwAwgYcCgYEAzSP6KuHtmPTp0JM+13qAAkzMwQKvXLYff/pXQm8w0SDFtSEHQCyphsLzZISuPYUu7YW9VLAYKO9q+BvnCxYfkyVPx/iOw7nKmIQOVdAv73h3xXIoX2C/GSvRcqK32D/glzRaAb0EnMh4Rc2TjRXydhARq7hbLp5S3YE+nGTIKZMCAQMwDQYJKoZIhvcNAQEEBQADgYEAMho1ur9DJ9a01Lh25eObTWzAhsl3NbprFi0TRkqwMlOhW1rpmeIMhogXTg3+gqxOR+/7/zms7jXI+lI3CkmtWa3iiqkcxl8f+G9zfs2gMegMvvVN2bKrihK2MHhoEXwN8UlNo/2y6f8d8JH6VIX/M5Dowb+km6RiRr1hElmYQYk=</X509Certificate></X509Data>"; + doc.LoadXml (complex); + KeyInfoX509Data data2 = new KeyInfoX509Data (); + data2.LoadXml (doc.DocumentElement); + s = (data2.GetXml ().OuterXml); + AssertEquals ("Xml-Complex", complex, s); + + string crl = "<X509Data xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><X509CRL>HoIBBTByAgEBMAsGCSqGSIb3DQEBBTBRMQswCQYDVQQGEwJVUzEYMBYGA1UEChMPVS5TLiBHb3Zlcm5tZW50MQwwCgYDVQQLEwNEb0QxGjAYBgNVBAMTEUFybWVkIEZvcmNlcyBSb290Fw0wMjEwMTExMzEyNTBaMAsGCSqGSIb3DQEBBQOBgQB9otEZbQ8Py+SjvuA2CvNMm6/mT/bjr89V88bbq0wWMqpzrczcMjNg34vMk7VPauxwU6/PBw+gzWasAFfGXF0hsb0wiY53jdRpfsA2ftLYIHEIgNLLdIvYQhcEmYCkUnAuwOOMC/95t0V33MXPQ5iRffEB91PXxlE18InMwf/iiQ==</X509CRL></X509Data>"; + doc.LoadXml (crl); + KeyInfoX509Data data3 = new KeyInfoX509Data (); + data3.LoadXml (doc.DocumentElement); + s = (data3.GetXml ().OuterXml); + AssertEquals ("Xml-Crl", crl, s); + } + + public void TestInvalidKeyNode () + { + KeyInfoX509Data data1 = new KeyInfoX509Data (); + try { + data1.LoadXml (null); + Fail ("Expected ArgumentNullException but got none"); + } + catch (ArgumentNullException) { + // this is what we expect + } + catch (Exception e) { + Fail ("Expected ArgumentNullException but got: " + e.ToString ()); + } + + string bad = "<Test></Test>"; + XmlDocument doc = new XmlDocument (); + doc.LoadXml (bad); + try { + data1.LoadXml (doc.DocumentElement); + Fail ("Expected CryptographicException but got none"); + } + catch (CryptographicException) { + // malformed certificate (none) + } + catch (Exception e) { + Fail ("Expected CryptographicException but got: " + e.ToString ()); + } + } +} + +}
\ No newline at end of file diff --git a/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/RSAKeyValueTest.cs b/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/RSAKeyValueTest.cs new file mode 100644 index 00000000000..b346a010580 --- /dev/null +++ b/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/RSAKeyValueTest.cs @@ -0,0 +1,95 @@ +// +// RSAKeyValueTest.cs - NUnit Test Cases for RSAKeyValue +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002 Motus Technologies Inc. (http://www.motus.com) +// + +using NUnit.Framework; +using System; +using System.Security.Cryptography; +using System.Security.Cryptography.Xml; +using System.Xml; + +namespace MonoTests.System.Security.Cryptography.Xml { + +public class RSAKeyValueTest : TestCase { + + public RSAKeyValueTest () : base ("System.Security.Cryptography.Xml.RSAKeyValue testsuite") {} + public RSAKeyValueTest (string name) : base (name) {} + + protected override void SetUp () {} + + protected override void TearDown () {} + + public static ITest Suite { + get { + return new TestSuite (typeof (RSAKeyValueTest)); + } + } + + public void TestGeneratedKey () + { + RSAKeyValue rsa1 = new RSAKeyValue (); + AssertNotNull ("Key", rsa1.Key); + XmlElement xmlkey = rsa1.GetXml (); + + RSAKeyValue rsa2 = new RSAKeyValue (); + rsa2.LoadXml (xmlkey); + + Assert ("rsa1==rsa2", (rsa1.GetXml ().OuterXml) == (rsa2.GetXml ().OuterXml)); + + RSA key = rsa1.Key; + RSAKeyValue rsa3 = new RSAKeyValue (key); + Assert ("rsa3==rsa1", (rsa3.GetXml ().OuterXml) == (rsa1.GetXml ().OuterXml)); + Assert ("rsa3==rsa2", (rsa3.GetXml ().OuterXml) == (rsa2.GetXml ().OuterXml)); + } + + public void TestImportKey () + { + string rsaKey = "<KeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><RSAKeyValue><Modulus>ogZ1/O7iks9ncETqNxLDKoPvgrT4nFx1a3lOmpywEmgbc5+8vI5dSzReH4v0YrflY75rIJx13CYWMsaHfQ78GtXvaeshHlQ3lLTuSdYEJceKll/URlBoKQtOj5qYIVSFOIVGHv4Y/0lnLftOzIydem29KKH6lJQlJawBBssR12s=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue></KeyValue>"; + XmlDocument doc = new XmlDocument (); + doc.LoadXml (rsaKey); + + RSAKeyValue rsa1 = new RSAKeyValue (); + rsa1.LoadXml (doc.DocumentElement); + + string s = (rsa1.GetXml ().OuterXml); + AssertEquals ("RSA Key", rsaKey, s); + } + + public void TestInvalidValue () + { + string badKey = "<Test></Test>"; + XmlDocument doc = new XmlDocument (); + doc.LoadXml (badKey); + + RSAKeyValue rsa1 = new RSAKeyValue (); + + try { + rsa1.LoadXml (null); + Fail ("Expected ArgumentNullException but got none"); + } + catch (ArgumentNullException) { + // this is what we expect + } + catch (Exception e) { + Fail ("Expected ArgumentNullException but got: " + e.ToString ()); + } + + try { + rsa1.LoadXml (doc.DocumentElement); + Fail ("Expected CryptographicException but got none"); + } + catch (CryptographicException) { + // this is what we expect + } + catch (Exception e) { + Fail ("Expected CryptographicException but got: " + e.ToString ()); + } + } +} + +}
\ No newline at end of file diff --git a/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/ReferenceTest.cs b/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/ReferenceTest.cs new file mode 100644 index 00000000000..60368601066 --- /dev/null +++ b/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/ReferenceTest.cs @@ -0,0 +1,199 @@ +// +// ReferenceTest.cs - NUnit Test Cases for Reference +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002 Motus Technologies Inc. (http://www.motus.com) +// + +using NUnit.Framework; +using System; +using System.Security.Cryptography; +using System.Security.Cryptography.Xml; +using System.Xml; + +namespace MonoTests.System.Security.Cryptography.Xml { + +public class ReferenceTest : TestCase { + + public ReferenceTest () : base ("System.Security.Cryptography.Xml.Reference testsuite") {} + public ReferenceTest (string name) : base (name) {} + + private Reference reference; + + protected override void SetUp () + { + reference = new Reference (); + } + + protected override void TearDown () {} + + public static ITest Suite { + get { + return new TestSuite (typeof (ReferenceTest)); + } + } + + public void TestProperties () + { + AssertNull ("Uri (null)", reference.Uri); + AssertNotNull ("TransformChain", reference.TransformChain); + AssertEquals ("ToString()", "System.Security.Cryptography.Xml.Reference", reference.ToString ()); + // test uri constructor + string uri = "uri"; + reference = new Reference (uri); + AssertEquals ("DigestMethod", "http://www.w3.org/2000/09/xmldsig#sha1", reference.DigestMethod); + AssertNull ("DigestValue", reference.DigestValue); + AssertNull ("Id", reference.Id); + AssertNull ("Type", reference.Type); + AssertEquals ("Uri", uri, reference.Uri); + } + + public void TestLoadNoTransform () + { + string value = "<Reference URI=\"#MyObjectId\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" /><DigestValue>/Vvq6sXEVbtZC8GwNtLQnGOy/VI=</DigestValue></Reference>"; + XmlDocument doc = new XmlDocument (); + doc.LoadXml (value); + reference.LoadXml (doc.DocumentElement); + AssertEquals ("Load-Xml", value, (reference.GetXml().OuterXml)); + AssertEquals ("Load-URI", "#MyObjectId", reference.Uri); + byte[] hash = { 0xFD, 0x5B, 0xEA, 0xEA, 0xC5, 0xC4, 0x55, 0xBB, 0x59, 0x0B, 0xC1, 0xB0, 0x36, 0xD2, 0xD0, 0x9C, 0x63, 0xB2, 0xFD, 0x52 }; + AllTests.AssertEquals("Load-Digest", hash, reference.DigestValue); + AssertEquals ("Load-#Transform", 0, reference.TransformChain.Count); + } + + public void TestLoadBase64Transform () + { + string value = "<Reference xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><Transforms><Transform Algorithm=\"http://www.w3.org/2000/09/xmldsig#base64\" /></Transforms><DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" /><DigestValue>AAAAAAAAAAAAAAAAAAAAAAAAAAA=</DigestValue></Reference>"; + XmlDocument doc = new XmlDocument (); + doc.LoadXml (value); + reference.LoadXml (doc.DocumentElement); + AssertEquals ("Load-Base64", value, (reference.GetXml().OuterXml)); + AssertEquals ("Load-#Transform", 1, reference.TransformChain.Count); + } + + public void TestLoadC14NTransform () + { + string value = "<Reference xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><Transforms><Transform Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\" /></Transforms><DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" /><DigestValue>AAAAAAAAAAAAAAAAAAAAAAAAAAA=</DigestValue></Reference>"; + XmlDocument doc = new XmlDocument (); + doc.LoadXml (value); + reference.LoadXml (doc.DocumentElement); + AssertEquals ("Load-C14N", value, (reference.GetXml().OuterXml)); + AssertEquals ("Load-#Transform", 1, reference.TransformChain.Count); + } + + public void TestLoadC14NWithCommentsTransforms () + { + string value = "<Reference xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><Transforms><Transform Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments\" /></Transforms><DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" /><DigestValue>AAAAAAAAAAAAAAAAAAAAAAAAAAA=</DigestValue></Reference>"; + XmlDocument doc = new XmlDocument (); + doc.LoadXml (value); + reference.LoadXml (doc.DocumentElement); + AssertEquals ("Load-C14NWithComments", value, (reference.GetXml().OuterXml)); + AssertEquals ("Load-#Transform", 1, reference.TransformChain.Count); + } + + public void TestLoadEnvelopedSignatureTransforms () + { + string value = "<Reference xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><Transforms><Transform Algorithm=\"http://www.w3.org/2000/09/xmldsig#enveloped-signature\" /></Transforms><DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" /><DigestValue>AAAAAAAAAAAAAAAAAAAAAAAAAAA=</DigestValue></Reference>"; + XmlDocument doc = new XmlDocument (); + doc.LoadXml (value); + reference.LoadXml (doc.DocumentElement); + AssertEquals ("Load-Enveloped", value, (reference.GetXml().OuterXml)); + AssertEquals ("Load-#Transform", 1, reference.TransformChain.Count); + } + + public void TestLoadXPathTransforms () + { + string value = "<Reference xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><Transforms><Transform Algorithm=\"http://www.w3.org/TR/1999/REC-xpath-19991116\"><XPath></XPath></Transform></Transforms><DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" /><DigestValue>AAAAAAAAAAAAAAAAAAAAAAAAAAA=</DigestValue></Reference>"; + XmlDocument doc = new XmlDocument (); + doc.LoadXml (value); + reference.LoadXml (doc.DocumentElement); + AssertEquals ("Load-XPath", value, (reference.GetXml().OuterXml)); + AssertEquals ("Load-#Transform", 1, reference.TransformChain.Count); + } + + public void TestLoadXsltTransforms () + { + string value = "<Reference xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><Transforms><Transform Algorithm=\"http://www.w3.org/TR/1999/REC-xslt-19991116\" /></Transforms><DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" /><DigestValue>AAAAAAAAAAAAAAAAAAAAAAAAAAA=</DigestValue></Reference>"; + XmlDocument doc = new XmlDocument (); + doc.LoadXml (value); + try { + reference.LoadXml (doc.DocumentElement); + } + catch (Exception e) { + Fail (e.ToString ()); + } + AssertEquals ("Load-Xslt", value, (reference.GetXml().OuterXml)); + AssertEquals ("Load-#Transform", 1, reference.TransformChain.Count); + } + + public void TestLoadAllTransforms () + { + string value = "<Reference xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><Transforms><Transform Algorithm=\"http://www.w3.org/2000/09/xmldsig#base64\" /><Transform Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\" /><Transform Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments\" /><Transform Algorithm=\"http://www.w3.org/2000/09/xmldsig#enveloped-signature\" /><Transform Algorithm=\"http://www.w3.org/TR/1999/REC-xpath-19991116\"><XPath></XPath></Transform><Transform Algorithm=\"http://www.w3.org/TR/1999/REC-xslt-19991116\" /></Transforms><DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" /><DigestValue>AAAAAAAAAAAAAAAAAAAAAAAAAAA=</DigestValue></Reference>"; + XmlDocument doc = new XmlDocument (); + doc.LoadXml (value); + reference.LoadXml (doc.DocumentElement); + AssertEquals ("Load-Xml", value, (reference.GetXml().OuterXml)); + AssertEquals ("Load-#Transform", 6, reference.TransformChain.Count); + } + + public void TestAllTransform () + { + // adding an empty hash value + byte[] hash = new byte [20]; + reference.DigestValue = hash; + XmlElement xel = reference.GetXml (); + // this is the minimal Reference (DisestValue)! + AssertNotNull ("GetXml", xel); + + reference.AddTransform (new XmlDsigBase64Transform ()); + reference.AddTransform (new XmlDsigC14NTransform ()); + reference.AddTransform (new XmlDsigC14NWithCommentsTransform ()); + reference.AddTransform (new XmlDsigEnvelopedSignatureTransform ()); + reference.AddTransform (new XmlDsigXPathTransform ()); + reference.AddTransform (new XmlDsigXsltTransform ()); + + string value = "<Reference xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><Transforms><Transform Algorithm=\"http://www.w3.org/2000/09/xmldsig#base64\" /><Transform Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\" /><Transform Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments\" /><Transform Algorithm=\"http://www.w3.org/2000/09/xmldsig#enveloped-signature\" /><Transform Algorithm=\"http://www.w3.org/TR/1999/REC-xpath-19991116\"><XPath></XPath></Transform><Transform Algorithm=\"http://www.w3.org/TR/1999/REC-xslt-19991116\" /></Transforms><DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" /><DigestValue>AAAAAAAAAAAAAAAAAAAAAAAAAAA=</DigestValue></Reference>"; + AssertEquals("Get-Xml", value, (reference.GetXml().OuterXml)); + // however this value cannot be loaded as it's missing some transform (xslt) parameters + + // can we add them again ? + reference.AddTransform (new XmlDsigBase64Transform ()); + reference.AddTransform (new XmlDsigC14NTransform ()); + reference.AddTransform (new XmlDsigC14NWithCommentsTransform ()); + reference.AddTransform (new XmlDsigEnvelopedSignatureTransform ()); + reference.AddTransform (new XmlDsigXPathTransform ()); + reference.AddTransform (new XmlDsigXsltTransform ()); + + // seems so ;-) + AssertEquals ("# Transforms", 12, reference.TransformChain.Count); + } + + public void TestNull () + { + // null DigestMethod -> "" DigestMethod !!! + reference.DigestMethod = null; + AssertNull ("DigestMethod null", reference.DigestMethod); + } + + public void TestBadness () + { + Reference reference = new Reference (); + reference.Uri = "#MyObjectId"; + // not enough info + try { + XmlElement bad = reference.GetXml (); + Fail ("Expected NullReferenceException but got none"); + } + catch (NullReferenceException) { + // this is expected + } + catch (Exception e) { + Fail ("Expected NullReferenceException but got: " + e.ToString ()); + } + // bad hash - there's no validation! + reference.DigestMethod = "http://www.w3.org/2000/09/xmldsig#mono"; + } +} +} diff --git a/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/SignatureTest.cs b/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/SignatureTest.cs new file mode 100644 index 00000000000..54bcdbd2bd9 --- /dev/null +++ b/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/SignatureTest.cs @@ -0,0 +1,76 @@ +// +// SignatureTest.cs - NUnit Test Cases for SignedXml +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002 Motus Technologies Inc. (http://www.motus.com) +// + +using NUnit.Framework; +using System; +using System.Security.Cryptography; +using System.Security.Cryptography.Xml; +using System.Xml; + +namespace MonoTests.System.Security.Cryptography.Xml { + +public class SignatureTest : TestCase { + + public SignatureTest () : base ("System.Security.Cryptography.Xml.Signature testsuite") {} + public SignatureTest (string name) : base (name) {} + + protected Signature signature; + + protected override void SetUp () + { + signature = new Signature (); + } + + protected override void TearDown () {} + + public static ITest Suite { + get { + return new TestSuite (typeof (SignatureTest)); + } + } + + public void TestSignature () + { + XmlElement xel = null; + // empty - missing SignedInfo + try { + xel = signature.GetXml (); + Fail ("Expected CryptographicException but got none"); + } + catch (CryptographicException) { + // this is expected + } + catch (Exception e) { + Fail ("Expected CryptographicException but got :" + e.ToString ()); + } + + SignedInfo info = new SignedInfo (); + signature.SignedInfo = info; + info.SignatureMethod = "http://www.w3.org/2000/09/xmldsig#dsa-sha1"; + signature.SignatureValue = new byte [128]; + try { + xel = signature.GetXml (); + } + catch (Exception e) { + Fail ("Expected ... but got :" + e.ToString ()); + } + } + + public void TestLoad () + { + string value = "<Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><SignedInfo><CanonicalizationMethod Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\" /><SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\" /><Reference URI=\"#MyObjectId\"><DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" /><DigestValue>/Vvq6sXEVbtZC8GwNtLQnGOy/VI=</DigestValue></Reference></SignedInfo><SignatureValue>A6XuE8Cy9iOffRXaW9b0+dUcMUJQnlmwLsiqtQnADbCtZXnXAaeJ6nGnQ4Mm0IGi0AJc7/2CoJReXl7iW4hltmFguG1e3nl0VxCyCTHKGOCo1u8R3K+B1rTaenFbSxs42EM7/D9KETsPlzfYfis36yM3PqatiCUOsoMsAiMGzlc=</SignatureValue><KeyInfo><KeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><RSAKeyValue><Modulus>tI8QYIpbG/m6JLyvP+S3X8mzcaAIayxomyTimSh9UCpEucRnGvLw0P73uStNpiF7wltTZA1HEsv+Ha39dY/0j/Wiy3RAodGDRNuKQao1wu34aNybZ673brbsbHFUfw/o7nlKD2xO84fbajBZmKtBBDy63NHt+QL+grSrREPfCTM=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue></KeyValue></KeyInfo><Object Id=\"MyObjectId\"><MyElement xmlns=\"samples\">This is some text</MyElement></Object></Signature>"; + XmlDocument doc = new XmlDocument (); + doc.LoadXml (value); + signature.LoadXml (doc.DocumentElement); + string s = signature.GetXml ().OuterXml; + AssertEquals ("Load", value, s); + } +} + +}
\ No newline at end of file diff --git a/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/SignedInfoTest.cs b/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/SignedInfoTest.cs new file mode 100644 index 00000000000..d3e6209dc64 --- /dev/null +++ b/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/SignedInfoTest.cs @@ -0,0 +1,156 @@ +// +// SignedInfoTest.cs - NUnit Test Cases for SignedInfo +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002 Motus Technologies Inc. (http://www.motus.com) +// + +using NUnit.Framework; +using System; +using System.Security.Cryptography; +using System.Security.Cryptography.Xml; +using System.Xml; + +namespace MonoTests.System.Security.Cryptography.Xml { + +public class SignedInfoTest : TestCase { + + public SignedInfoTest () : base ("System.Security.Cryptography.Xml.SignedInfo testsuite") {} + public SignedInfoTest (string name) : base (name) {} + + protected SignedInfo info; + + protected override void SetUp () + { + info = new SignedInfo (); + } + + protected override void TearDown () {} + + public static ITest Suite { + get { + return new TestSuite (typeof (SignedInfoTest)); + } + } + + public void TestEmpty () + { + AssertEquals ("CanonicalizationMethod", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315", info.CanonicalizationMethod); + // NON-WORKING ??? AssertEquals ("Id", "", info.Id); + AssertNotNull ("References", info.References); + AssertEquals ("References.Count", 0, info.References.Count); + // NON-WORKING ??? AssertEquals ("SignatureLength", "", info.SignatureLength); + // NON-WORKING ??? AssertEquals ("SignatureMethod", "", info.SignatureMethod); + AssertEquals ("ToString()", "System.Security.Cryptography.Xml.SignedInfo", info.ToString ()); + + try { + string xml = info.GetXml().OuterXml; + Fail ("Empty Xml: Expected CryptographicException but got none"); + } + catch (CryptographicException) { + // this is expected + } + catch (Exception e) { + Fail ("Empty Xml: Expected CryptographicException but got: " + e.ToString ()); + } + } + + public void TestProperties () + { + info.CanonicalizationMethod = "http://www.go-mono.com/"; + AssertEquals ("CanonicalizationMethod", "http://www.go-mono.com/", info.CanonicalizationMethod); + info.Id = "Mono::"; + AssertEquals ("Id", "Mono::", info.Id); + } + + public void TestReferences () + { + Reference r1 = new Reference (); + r1.Uri = "http://www.go-mono.com/"; + r1.AddTransform (new XmlDsigBase64Transform ()); + info.AddReference (r1); + AssertEquals ("References.Count 1", 1, info.References.Count); + + Reference r2 = new Reference ("http://www.motus.com/"); + r2.AddTransform (new XmlDsigBase64Transform ()); + info.AddReference (r2); + AssertEquals ("References.Count 2", 2, info.References.Count); + + info.SignatureMethod = "http://www.w3.org/2000/09/xmldsig#dsa-sha1"; + } + + public void TestLoad () + { + string xml = "<SignedInfo xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><CanonicalizationMethod Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\" /><SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\" /><Reference URI=\"#MyObjectId\"><DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" /><DigestValue>/Vvq6sXEVbtZC8GwNtLQnGOy/VI=</DigestValue></Reference></SignedInfo>"; + XmlDocument doc = new XmlDocument (); + doc.LoadXml (xml); + info.LoadXml (doc.DocumentElement); + AssertEquals ("LoadXml", xml, (info.GetXml ().OuterXml)); + AssertEquals ("LoadXml-C14N", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315", info.CanonicalizationMethod); + AssertEquals ("LoadXml-Algo", "http://www.w3.org/2000/09/xmldsig#rsa-sha1", info.SignatureMethod); + AssertEquals ("LoadXml-Ref1", 1, info.References.Count); + } + + // there are many (documented) not supported methods in SignedInfo + public void TestNotSupported () + { + try { + int n = info.Count; + Fail ("Count: Expected NotSupportedException but got none"); + } + catch (NotSupportedException) { + // this is expected + } + catch (Exception e) { + Fail ("Count: Expected NotSupportedException but got: " + e.ToString ()); + } + + try { + bool b = info.IsReadOnly; + Fail ("IsReadOnly: Expected NotSupportedException but got none"); + } + catch (NotSupportedException) { + // this is expected + } + catch (Exception e) { + Fail ("IsReadOnly: Expected NotSupportedException but got: " + e.ToString ()); + } + + try { + bool b = info.IsSynchronized; + Fail ("IsSynchronized: Expected NotSupportedException but got none"); + } + catch (NotSupportedException) { + // this is expected + } + catch (Exception e) { + Fail ("IsSynchronized: Expected NotSupportedException but got: " + e.ToString ()); + } + + try { + object o = info.SyncRoot; + Fail ("SyncRoot: Expected NotSupportedException but got none"); + } + catch (NotSupportedException) { + // this is expected + } + catch (Exception e) { + Fail ("SyncRoot: Expected NotSupportedException but got: " + e.ToString ()); + } + + try { + info.CopyTo (null, 0); + Fail ("CopyTo: Expected NotSupportedException but got none"); + } + catch (NotSupportedException) { + // this is expected + } + catch (Exception e) { + Fail ("CopyTo: Expected NotSupportedException but got: " + e.ToString ()); + } + } +} + +} diff --git a/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/SignedXmlTest.cs b/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/SignedXmlTest.cs new file mode 100644 index 00000000000..aa764314498 --- /dev/null +++ b/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/SignedXmlTest.cs @@ -0,0 +1,233 @@ +// +// SignedXmlTest.cs - NUnit Test Cases for SignedXml +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002 Motus Technologies Inc. (http://www.motus.com) +// + +using NUnit.Framework; +using System; +using System.Security.Cryptography; +using System.Security.Cryptography.Xml; +using System.Text; +using System.Xml; + +namespace MonoTests.System.Security.Cryptography.Xml { + +public class SignedXmlTest : TestCase { + + public SignedXmlTest () : base ("System.Security.Cryptography.Xml.SignedXml testsuite") {} + public SignedXmlTest (string name) : base (name) {} + + protected override void SetUp () {} + + protected override void TearDown () {} + + public static ITest Suite { + get { + return new TestSuite (typeof (SignedXmlTest)); + } + } + + public void TestStatic () + { + AssertEquals ("XmlDsigCanonicalizationUrl", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315", SignedXml.XmlDsigCanonicalizationUrl); + AssertEquals ("XmlDsigCanonicalizationWithCommentsUrl", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments", SignedXml.XmlDsigCanonicalizationWithCommentsUrl); + AssertEquals ("XmlDsigDSAUrl", "http://www.w3.org/2000/09/xmldsig#dsa-sha1", SignedXml.XmlDsigDSAUrl); + AssertEquals ("XmlDsigHMACSHA1Url", "http://www.w3.org/2000/09/xmldsig#hmac-sha1", SignedXml.XmlDsigHMACSHA1Url); + AssertEquals ("XmlDsigMinimalCanonicalizationUrl", "http://www.w3.org/2000/09/xmldsig#minimal", SignedXml.XmlDsigMinimalCanonicalizationUrl); + AssertEquals ("XmlDsigNamespaceUrl", "http://www.w3.org/2000/09/xmldsig#", SignedXml.XmlDsigNamespaceUrl); + AssertEquals ("XmlDsigRSASHA1Url", "http://www.w3.org/2000/09/xmldsig#rsa-sha1", SignedXml.XmlDsigRSASHA1Url); + AssertEquals ("XmlDsigSHA1Url", "http://www.w3.org/2000/09/xmldsig#sha1", SignedXml.XmlDsigSHA1Url); + } + + // sample from MSDN (url) + public SignedXml MSDNSample () + { + // Create example data to sign. + XmlDocument document = new XmlDocument (); + XmlNode node = document.CreateNode (XmlNodeType.Element, "", "MyElement", "samples"); + node.InnerText = "This is some text"; + document.AppendChild (node); + + // Create the SignedXml message. + SignedXml signedXml = new SignedXml (); + + // Create a data object to hold the data to sign. + DataObject dataObject = new DataObject (); + dataObject.Data = document.ChildNodes; + dataObject.Id = "MyObjectId"; + + // Add the data object to the signature. + signedXml.AddObject (dataObject); + + // Create a reference to be able to package everything into the + // message. + Reference reference = new Reference (); + reference.Uri = "#MyObjectId"; + + // Add it to the message. + signedXml.AddReference (reference); + + return signedXml; + } + + public void TestAsymmetricRSASignature () + { + SignedXml signedXml = MSDNSample (); + + RSA key = RSA.Create (); + signedXml.SigningKey = key; + + // Add a KeyInfo. + KeyInfo keyInfo = new KeyInfo (); + keyInfo.AddClause (new RSAKeyValue (key)); + signedXml.KeyInfo = keyInfo; + + // Compute the signature. + signedXml.ComputeSignature (); + + // Get the XML representation of the signature. + XmlElement xmlSignature = signedXml.GetXml (); + + // LAMESPEC: we must reload the signature or it won't work + // MS framework throw a "malformed element" + SignedXml vrfy = new SignedXml (); + vrfy.LoadXml (xmlSignature); + + // assert that we can verify our own signature + Assert ("RSA-Compute/Verify", vrfy.CheckSignature ()); + } + + public void TestAsymmetricDSASignature () + { + SignedXml signedXml = MSDNSample (); + + DSA key = DSA.Create (); + signedXml.SigningKey = key; + + // Add a KeyInfo. + KeyInfo keyInfo = new KeyInfo (); + keyInfo.AddClause (new DSAKeyValue (key)); + signedXml.KeyInfo = keyInfo; + + // Compute the signature. + signedXml.ComputeSignature (); + + // Get the XML representation of the signature. + XmlElement xmlSignature = signedXml.GetXml (); + + // LAMESPEC: we must reload the signature or it won't work + // MS framework throw a "malformed element" + SignedXml vrfy = new SignedXml (); + vrfy.LoadXml (xmlSignature); + + // assert that we can verify our own signature + Assert ("DSA-Compute/Verify", vrfy.CheckSignature ()); + } + + public void TestSymmetricHMACSHA1Signature () + { + SignedXml signedXml = MSDNSample (); + + // Compute the signature. + byte[] secretkey = Encoding.Default.GetBytes ("password"); + HMACSHA1 hmac = new HMACSHA1 (secretkey); + signedXml.ComputeSignature (hmac); + + // Get the XML representation of the signature. + XmlElement xmlSignature = signedXml.GetXml (); + + // LAMESPEC: we must reload the signature or it won't work + // MS framework throw a "malformed element" + SignedXml vrfy = new SignedXml (); + vrfy.LoadXml (xmlSignature); + + // assert that we can verify our own signature + Assert ("HMACSHA1-Compute/Verify", vrfy.CheckSignature (hmac)); + } + + public void TestSymmetricMACTripleDESSignature () + { + SignedXml signedXml = MSDNSample (); + + // Compute the signature. + byte[] secretkey = Encoding.Default.GetBytes ("password"); + MACTripleDES hmac = new MACTripleDES (secretkey); + try { + signedXml.ComputeSignature (hmac); + Fail ("Expected CryptographicException but none"); + } + catch (CryptographicException) { + // this is expected + } + catch (Exception e) { + Fail ("Expected CryptographicException but got: " + e.ToString ()); + } + } + + // Using empty constructor + // LAMESPEC: The two other constructors don't seems to apply in verifying signatures + public void TestAsymmetricRSAVerify () + { + string value = "<Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><SignedInfo><CanonicalizationMethod Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\" /><SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\" /><Reference URI=\"#MyObjectId\"><DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" /><DigestValue>/Vvq6sXEVbtZC8GwNtLQnGOy/VI=</DigestValue></Reference></SignedInfo><SignatureValue>A6XuE8Cy9iOffRXaW9b0+dUcMUJQnlmwLsiqtQnADbCtZXnXAaeJ6nGnQ4Mm0IGi0AJc7/2CoJReXl7iW4hltmFguG1e3nl0VxCyCTHKGOCo1u8R3K+B1rTaenFbSxs42EM7/D9KETsPlzfYfis36yM3PqatiCUOsoMsAiMGzlc=</SignatureValue><KeyInfo><KeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><RSAKeyValue><Modulus>tI8QYIpbG/m6JLyvP+S3X8mzcaAIayxomyTimSh9UCpEucRnGvLw0P73uStNpiF7wltTZA1HEsv+Ha39dY/0j/Wiy3RAodGDRNuKQao1wu34aNybZ673brbsbHFUfw/o7nlKD2xO84fbajBZmKtBBDy63NHt+QL+grSrREPfCTM=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue></KeyValue></KeyInfo><Object Id=\"MyObjectId\"><MyElement xmlns=\"samples\">This is some text</MyElement></Object></Signature>"; + XmlDocument doc = new XmlDocument (); + doc.LoadXml (value); + + SignedXml v1 = new SignedXml (); + v1.LoadXml (doc.DocumentElement); + Assert ("RSA-CheckSignature()", v1.CheckSignature ()); + + SignedXml v2 = new SignedXml (); + v2.LoadXml (doc.DocumentElement); + AsymmetricAlgorithm key = null; + bool vrfy = v2.CheckSignatureReturningKey (out key); + Assert ("RSA-CheckSignatureReturningKey()", vrfy); + + SignedXml v3 = new SignedXml (); + v3.LoadXml (doc.DocumentElement); + Assert ("RSA-CheckSignature(key)", v3.CheckSignature (key)); + } + + // Using empty constructor + // LAMESPEC: The two other constructors don't seems to apply in verifying signatures + public void TestAsymmetricDSAVerify () + { + string value = "<Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><SignedInfo><CanonicalizationMethod Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\" /><SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#dsa-sha1\" /><Reference URI=\"#MyObjectId\"><DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" /><DigestValue>/Vvq6sXEVbtZC8GwNtLQnGOy/VI=</DigestValue></Reference></SignedInfo><SignatureValue>BYz/qRGjGsN1yMFPxWa3awUZm1y4I/IxOQroMxkOteRGgk1HIwhRYw==</SignatureValue><KeyInfo><KeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><DSAKeyValue><P>iglVaZ+LsSL8Y0aDXmFMBwva3xHqIypr3l/LtqBH9ziV2Sh1M4JVasAiKqytWIWt/s/Uk8Ckf2tO2Ww1vsNi1NL+Kg9T7FE52sn380/rF0miwGkZeidzm74OWhykb3J+wCTXaIwOzAWI1yN7FoeoN7wzF12jjlSXAXeqPMlViqk=</P><Q>u4sowiJMHilNRojtdmIuQY2YnB8=</Q><G>SdnN7d+wn1n+HH4Hr8MIryIRYgcXdbZ5TH7jAnuWc1koqRc1AZfcYAZ6RDf+orx6Lzn055FTFiN+1NHQfGUtXJCWW0zz0FVV1NJux7WRj8vGTldjJ5ef0oCenkpwDjcIxWsZgVobve4GPoyN1sAc1scnkJB59oupibklmF4y72A=</G><Y>XejzS8Z51yfl0zbYnxSYYbHqreSLjNCoGPB/KjM1TOyV5sMjz0StKtGrFWryTWc7EgvFY7kUth4e04VKf9HbK8z/FifHTXj8+Tszbjzw8GfInnBwLN+vJgbpnjtypmiI5Bm2nLiRbfkdAHP+OrKtr/EauM9GQfYuaxm3/Vj8B84=</Y><J>vGwGg9wqwwWP9xsoPoXu6kHArJtadiNKe9azBiUx5Ob883gd5wlKfEcGuKkBmBySGbgwxyOsIBovd9Kk48hF01ymfQzAAuHR0EdJECSsTsTTKVTLQNBU32O+PRbLYpv4E8kt6rNL83JLJCBY</J><Seed>sqzn8J6fd2gtEyq6YOqiUSHgPE8=</Seed><PgenCounter>sQ==</PgenCounter></DSAKeyValue></KeyValue></KeyInfo><Object Id=\"MyObjectId\"><MyElement xmlns=\"samples\">This is some text</MyElement></Object></Signature>"; + XmlDocument doc = new XmlDocument (); + doc.LoadXml (value); + + SignedXml v1 = new SignedXml (); + v1.LoadXml (doc.DocumentElement); + Assert ("DSA-CheckSignature()", v1.CheckSignature ()); + + SignedXml v2 = new SignedXml (); + v2.LoadXml (doc.DocumentElement); + AsymmetricAlgorithm key = null; + bool vrfy = v2.CheckSignatureReturningKey (out key); + Assert ("DSA-CheckSignatureReturningKey()", vrfy); + + SignedXml v3 = new SignedXml (); + v3.LoadXml (doc.DocumentElement); + Assert ("DSA-CheckSignature(key)", v3.CheckSignature (key)); + } + + public void TestSymmetricHMACSHA1Verify () + { + string value = "<Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><SignedInfo><CanonicalizationMethod Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\" /><SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#hmac-sha1\" /><Reference URI=\"#MyObjectId\"><DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" /><DigestValue>/Vvq6sXEVbtZC8GwNtLQnGOy/VI=</DigestValue></Reference></SignedInfo><SignatureValue>e2RxYr5yGbvTqZLCFcgA2RAC0yE=</SignatureValue><Object Id=\"MyObjectId\"><MyElement xmlns=\"samples\">This is some text</MyElement></Object></Signature>"; + XmlDocument doc = new XmlDocument (); + doc.LoadXml (value); + + SignedXml v1 = new SignedXml (); + v1.LoadXml (doc.DocumentElement); + + byte[] secretkey = Encoding.Default.GetBytes ("password"); + HMACSHA1 hmac = new HMACSHA1 (secretkey); + + Assert ("HMACSHA1-CheckSignature(key)", v1.CheckSignature (hmac)); + } +} + +}
\ No newline at end of file diff --git a/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/TransformChainTest.cs b/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/TransformChainTest.cs new file mode 100644 index 00000000000..fb1d025fcd6 --- /dev/null +++ b/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/TransformChainTest.cs @@ -0,0 +1,80 @@ +// +// TransformChainTest.cs - NUnit Test Cases for TransformChain +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002 Motus Technologies Inc. (http://www.motus.com) +// + +using NUnit.Framework; +using System; +using System.Security.Cryptography; +using System.Security.Cryptography.Xml; +using System.Xml; + +namespace MonoTests.System.Security.Cryptography.Xml { + +public class TransformChainTest : TestCase { + + public TransformChainTest () : base ("System.Security.Cryptography.Xml.TransformChain testsuite") {} + public TransformChainTest (string name) : base (name) {} + + protected TransformChain chain; + + protected override void SetUp () + { + chain = new TransformChain (); + } + + protected override void TearDown () {} + + public static ITest Suite { + get { + return new TestSuite (typeof (TransformChainTest)); + } + } + + public void TestEmpty () + { + AssertEquals ("empty count", 0, chain.Count); + AssertNotNull ("IEnumerator", chain.GetEnumerator ()); + AssertEquals ("ToString()", "System.Security.Cryptography.Xml.TransformChain", chain.ToString ()); + } + + public void TestChain () + { + XmlDsigBase64Transform base64 = new XmlDsigBase64Transform (); + chain.Add (base64); + AssertEquals ("XmlDsigBase64Transform", base64, chain[0]); + AssertEquals ("count 1", 1, chain.Count); + + XmlDsigC14NTransform c14n = new XmlDsigC14NTransform (); + chain.Add (c14n); + AssertEquals ("XmlDsigC14NTransform", c14n, chain[1]); + AssertEquals ("count 2", 2, chain.Count); + + XmlDsigC14NWithCommentsTransform c14nc = new XmlDsigC14NWithCommentsTransform (); + chain.Add (c14nc); + AssertEquals ("XmlDsigC14NWithCommentsTransform", c14nc, chain[2]); + AssertEquals ("count 3", 3, chain.Count); + + XmlDsigEnvelopedSignatureTransform esign = new XmlDsigEnvelopedSignatureTransform (); + chain.Add (esign); + AssertEquals ("XmlDsigEnvelopedSignatureTransform", esign, chain[3]); + AssertEquals ("count 4", 4, chain.Count); + + XmlDsigXPathTransform xpath = new XmlDsigXPathTransform (); + chain.Add (xpath); + AssertEquals ("XmlDsigXPathTransform", xpath, chain[4]); + AssertEquals ("count 5", 5, chain.Count); + + XmlDsigXsltTransform xslt = new XmlDsigXsltTransform (); + chain.Add (xslt); + AssertEquals ("XmlDsigXsltTransform", xslt, chain[5]); + AssertEquals ("count 6", 6, chain.Count); + } + +} + +} diff --git a/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/XmlDsigBase64TransformTest.cs b/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/XmlDsigBase64TransformTest.cs new file mode 100644 index 00000000000..89941a5b9e3 --- /dev/null +++ b/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/XmlDsigBase64TransformTest.cs @@ -0,0 +1,159 @@ +// +// XmlDsigBase64TransformTest.cs - NUnit Test Cases for XmlDsigBase64Transform +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002 Motus Technologies Inc. (http://www.motus.com) +// + +using NUnit.Framework; +using System; +using System.IO; +using System.Security.Cryptography; +using System.Security.Cryptography.Xml; +using System.Text; +using System.Xml; + +namespace MonoTests.System.Security.Cryptography.Xml { + +public class XmlDsigBase64TransformTest : TestCase { + + public XmlDsigBase64TransformTest () : base ("System.Security.Cryptography.Xml.XmlDsigBase64Transform testsuite") {} + public XmlDsigBase64TransformTest (string name) : base (name) {} + + protected XmlDsigBase64Transform transform; + + protected override void SetUp () + { + transform = new XmlDsigBase64Transform (); + } + + protected override void TearDown () {} + + public static ITest Suite { + get { + return new TestSuite (typeof (XmlDsigBase64TransformTest)); + } + } + + public void TestProperties () + { + AssertEquals ("Algorithm", "http://www.w3.org/2000/09/xmldsig#base64", transform.Algorithm); + + Type[] input = transform.InputTypes; + Assert ("Input #", (input.Length == 3)); + // check presence of every supported input types + bool istream = false; + bool ixmldoc = false; + bool ixmlnl = false; + foreach (Type t in input) { + if (t.ToString () == "System.IO.Stream") + istream = true; + if (t.ToString () == "System.Xml.XmlDocument") + ixmldoc = true; + if (t.ToString () == "System.Xml.XmlNodeList") + ixmlnl = true; + } + Assert ("Input Stream", istream); + Assert ("Input XmlDocument", ixmldoc); + Assert ("Input XmlNodeList", ixmlnl); + + Type[] output = transform.OutputTypes; + Assert ("Output #", (output.Length == 1)); + // check presence of every supported output types + bool ostream = false; + foreach (Type t in input) { + if (t.ToString () == "System.IO.Stream") + ostream = true; + } + Assert ("Output Stream", ostream); + } + + private string Stream2String (Stream s) + { + StringBuilder sb = new StringBuilder (); + int b = s.ReadByte (); + while (b != -1) { + sb.Append (b.ToString("X2")); + b = s.ReadByte (); + } + return sb.ToString (); + } + + private byte[] Stream2Array (Stream s) + { + string st = Stream2String (s); + byte[] array = new byte [st.Length / 2]; + for (int i=0; i < array.Length; i++) { + string hex = st.Substring (i*2, 2); + array [i] = Convert.ToByte(hex, 16); + } + return array; + } + + public void TestLoadInput () + { + string base64 = "XmlDsigBase64Transform"; + UTF8Encoding utf8 = new UTF8Encoding (); + byte[] base64array = utf8.GetBytes (base64); + string xml = "<Test>" + Convert.ToBase64String (base64array) + "</Test>"; + XmlDocument doc = new XmlDocument (); + doc.LoadXml (xml); + + // load as XmlDocument + transform.LoadInput (doc); + Stream s = (Stream) transform.GetOutput (); + byte[] output = Stream2Array (s); + AssertEquals("XmlDocument", base64, utf8.GetString (output)); + + // load as XmlNodeList + XmlNodeList xpath = doc.SelectNodes ("//."); + transform.LoadInput (xpath); + s = (Stream) transform.GetOutput (); + output = Stream2Array (s); + // works with MS ??? why does xpath return 3 nodes ??? + // AssertEquals("XPathNodeList", base64, utf8.GetString (output)); + + // load as XmlNodeList + transform.LoadInput (doc.ChildNodes); + s = (Stream) transform.GetOutput (); + output = Stream2Array (s); + // FIXME: works with Mono ??? why doesn't this works with MS ??? + AssertEquals("XmlChildNodes", base64, utf8.GetString (output)); + + // load as Stream + MemoryStream ms = new MemoryStream (); + byte[] x = utf8.GetBytes (Convert.ToBase64String (base64array)); + ms.Write (x, 0, x.Length); + ms.Position = 0; + transform.LoadInput (ms); + s = (Stream) transform.GetOutput (); + output = Stream2Array (s); + AssertEquals("MemoryStream", base64, utf8.GetString (output)); + } + + public void TestUnsupportedInput () + { + byte[] bad = { 0xBA, 0xD }; + // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented) + transform.LoadInput (bad); + } + + public void TestUnsupportedOutput () + { + try { + XmlDocument doc = new XmlDocument(); + object o = transform.GetOutput (doc.GetType ()); + Fail ("Expected ArgumentException but got none"); + } + catch (ArgumentException) { + // this is what we expected + } + catch (Exception e) { + Fail ("Expected ArgumentException but got: " + e.ToString ()); + } + } +} + +}
\ No newline at end of file diff --git a/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/XmlDsigXsltTransformTest.cs b/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/XmlDsigXsltTransformTest.cs new file mode 100644 index 00000000000..aee7a08359b --- /dev/null +++ b/mcs/class/System.Security/Test/System.Security.Cryptography.Xml/XmlDsigXsltTransformTest.cs @@ -0,0 +1,156 @@ +// +// XmlDsigXsltTransformTest.cs - NUnit Test Cases for XmlDsigXsltTransform +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002 Motus Technologies Inc. (http://www.motus.com) +// + +using NUnit.Framework; +using System; +using System.IO; +using System.Security.Cryptography; +using System.Security.Cryptography.Xml; +using System.Text; +using System.Xml; + +namespace MonoTests.System.Security.Cryptography.Xml { + +public class XmlDsigXsltTransformTest : TestCase { + + public XmlDsigXsltTransformTest () : base ("System.Security.Cryptography.Xml.XmlDsigXsltTransform testsuite") {} + public XmlDsigXsltTransformTest (string name) : base (name) {} + + protected XmlDsigXsltTransform transform; + + protected override void SetUp () + { + transform = new XmlDsigXsltTransform (); + } + + protected override void TearDown () {} + + public static ITest Suite { + get { + return new TestSuite (typeof (XmlDsigXsltTransformTest)); + } + } + + public void TestProperties () + { + AssertEquals ("Algorithm", "http://www.w3.org/TR/1999/REC-xslt-19991116", transform.Algorithm); + + Type[] input = transform.InputTypes; + Assert ("Input #", (input.Length == 3)); + // check presence of every supported input types + bool istream = false; + bool ixmldoc = false; + bool ixmlnl = false; + foreach (Type t in input) { + if (t.ToString () == "System.IO.Stream") + istream = true; + if (t.ToString () == "System.Xml.XmlDocument") + ixmldoc = true; + if (t.ToString () == "System.Xml.XmlNodeList") + ixmlnl = true; + } + Assert ("Input Stream", istream); + Assert ("Input XmlDocument", ixmldoc); + Assert ("Input XmlNodeList", ixmlnl); + + Type[] output = transform.OutputTypes; + Assert ("Output #", (output.Length == 1)); + // check presence of every supported output types + bool ostream = false; + foreach (Type t in input) { + if (t.ToString () == "System.IO.Stream") + ostream = true; + } + Assert ("Output Stream", ostream); + } + + private string Stream2Array (Stream s) + { + StringBuilder sb = new StringBuilder (); + int b = s.ReadByte (); + while (b != -1) { + sb.Append (b.ToString("X2")); + b = s.ReadByte (); + } + return sb.ToString (); + } + + public void Test () + { + string test = "<Test>XmlDsigXsltTransform</Test>"; + XmlDocument doc = new XmlDocument (); + doc.LoadXml (test); + + transform.LoadInnerXml (doc.ChildNodes); + Stream s = (Stream) transform.GetOutput (); + string output = Stream2Array (s); + + // load as XmlDocument + transform.LoadInput (doc); + s = (Stream) transform.GetOutput (); + output = Stream2Array (s); + + // load as XmlNodeList + transform.LoadInput (doc.ChildNodes); + s = (Stream) transform.GetOutput (); + output = Stream2Array (s); + + // load as Stream + MemoryStream ms = new MemoryStream (); + doc.Save (ms); + ms.Position = 0; + transform.LoadInput (ms); + s = (Stream) transform.GetOutput (); + output = Stream2Array (s); + } + + protected void AssertEquals (string msg, XmlNodeList expected, XmlNodeList actual) + { + for (int i=0; i < expected.Count; i++) { + if (expected[i].OuterXml != actual[i].OuterXml) { + Fail (msg + " [" + i + "] expected " + expected[i].OuterXml + " bug got " + actual[i].OuterXml); + } + } + } + + public void TestLoadInnerXml () + { + string value = "<Transform Algorithm=\"http://www.w3.org/TR/1999/REC-xslt-19991116\" />"; + XmlDocument doc = new XmlDocument (); + doc.LoadXml (value); + + transform.LoadInnerXml (doc.ChildNodes); + // note: GetInnerXml is protected so we can't AssertEquals :-( + // unless we use reflection (making ti a lot more complicated) + } + + public void TestUnsupportedInput () + { + byte[] bad = { 0xBA, 0xD }; + // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented) + transform.LoadInput (bad); + } + + public void TestUnsupportedOutput () + { + try { + XmlDocument doc = new XmlDocument(); + object o = transform.GetOutput (doc.GetType ()); + Fail ("Expected ArgumentException but got none"); + } + catch (ArgumentException) { + // this is what we expected + } + catch (Exception e) { + Fail ("Expected ArgumentException but got: " + e.ToString ()); + } + } +} + +}
\ No newline at end of file diff --git a/mcs/class/System.Security/Test/System.Security_test.build b/mcs/class/System.Security/Test/System.Security_test.build new file mode 100644 index 00000000000..9d0910d5c40 --- /dev/null +++ b/mcs/class/System.Security/Test/System.Security_test.build @@ -0,0 +1,39 @@ +<?xml version="1.0" encoding="iso-8859-1"?> + +<!-- NAnt build file for System.Security_test.dll --> +<!-- Target build (default) builds tests --> +<!-- Target test runs tests --> + +<project name="System.Security_test" default="build"> + <property name="debug" value="false"/> + <property name="nunit_home" value="..\..\..\nunit"/> + + <target name="build"> + </target> + + <target name="assemblies"> + <csc target="library" output="System.Security_test.dll" debug="${debug}"> + <sources> + <includes name="**/*.cs"/> + </sources> + <references basedir="..\..\..\nunit"> + <includes name="NUnitCore.dll"/> + <includes name="..\class\lib\corlib.dll"/> + <includes name="..\class\lib\System.XML.dll"/> + <includes name="..\class\lib\System.Security.dll"/> + </references> + </csc> + <copy file="../../lib/System.Security.dll" tofile="System.Security.dll"/> + <copy file="../../lib/System.XML.dll" tofile="System.XML.dll"/> + </target> + + <target name="test" depends="assemblies"> + <exec program="..\..\..\nunit\NUnitConsole" commandline="MonoTests.System.Security.AllTests,System.Security_test.dll" failonerror="false"/> + </target> + + <target name="clean"> + <delete file="System.Security_test.dll" failonerror="false"/> + <delete file="System.Security.dll" failonerror="false"/> + <delete file="System.XML.dll" failonerror="false"/> + </target> +</project> |