+2003-03-02 Sebastien Pouliot <spouliot@videotron.ca>
+
+ * DSAKeyValue.cs: New XML generation is commented. Old string technique
+ did a better job to match MS implementation.
+ * DataObject.cs: Replaced XML generation from StringBuilder to XmlElement.
+ * KeyInfo.cs: Replaced XML generation from StringBuilder to XmlElement.
+ * KeyInfoName.cs: Replaced XML generation from StringBuilder to XmlElement.
+ * KeyInfoNode.cs: Replaced XML generation from StringBuilder to XmlElement.
+ * KeyInfoRetrievalMethod.cs: Replaced XML generation from StringBuilder to XmlElement.
+ * KeyInfoX509Data.cs: Replaced XML generation from StringBuilder to XmlElement.
+ * RSAKeyValue.cs: New XML generation is commented. Old string technique
+ did a better job to match MS implementation.
+ * Reference.cs: Replaced XML generation from StringBuilder to XmlElement.
+ * Signature.cs: Replaced XML generation from StringBuilder to XmlElement.
+ * SignedInfo.cs: Replaced XML generation from StringBuilder to XmlElement.
+ * SignedXml.cs: Replaced XML generation from StringBuilder to XmlElement.
+ * Transform.cs: Replaced XML generation from StringBuilder to XmlElement.
+ * XmlDsigEnvelopedSignatureTransform.cs: Added missing Algorithm URL.
+ * XmlDsigXPathTransform.cs: Added missing Algorithm URL.
+ * XmlSignature.cs: New. Private contants (construct similar to WSE).
+
2003-02-19 Sebastien Pouliot <spouliot@videotron.ca>
* All: Corrected class indentation, minor fixes, added many MonoTODO (so class status
public class DSAKeyValue : KeyInfoClause {
- static private string xmldsig = "http://www.w3.org/2000/09/xmldsig#";
private DSA dsa;
public DSAKeyValue ()
public override XmlElement GetXml ()
{
- StringBuilder sb = new StringBuilder ();
- sb.Append ("<KeyValue xmlns=\"");
- sb.Append (xmldsig);
- sb.Append ("\">");
- sb.Append (dsa.ToXmlString (false));
- sb.Append ("</KeyValue>");
-
- XmlDocument doc = new XmlDocument ();
- doc.LoadXml(sb.ToString ());
- return doc.DocumentElement;
+ XmlDocument document = new XmlDocument ();
+ document.LoadXml ("<KeyValue xmlns=\"" + XmlSignature.NamespaceURI + "\">" + dsa.ToXmlString (false) + "</KeyValue>");
+ return document.DocumentElement;
+
+ // FIX: this way we get a xmlns="" in DSAKeyValue
+/* XmlElement xel = document.CreateElement (XmlSignature.ElementNames.KeyValue, XmlSignature.NamespaceURI);
+ xel.InnerXml = dsa.ToXmlString (false);
+ return xel;*/
}
public override void LoadXml (XmlElement value)
if (value == null)
throw new ArgumentNullException ();
- if ((value.LocalName == "KeyValue") && (value.NamespaceURI == xmldsig))
- dsa.FromXmlString (value.InnerXml);
- else
+ // FIXME: again hack to match MS implementation (required for previous hack)
+ if ((value.LocalName != XmlSignature.ElementNames.KeyValue) || ((value.NamespaceURI != XmlSignature.NamespaceURI) && (value.GetAttribute("xmlns") != XmlSignature.NamespaceURI)))
throw new CryptographicException ("value");
+
+ dsa.FromXmlString (value.InnerXml);
}
}
}
\ No newline at end of file
// (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
//
-using System.Text;
using System.Xml;
namespace System.Security.Cryptography.Xml {
private string id;
private string mimeType;
private string encoding;
- private XmlDocument doc;
+ private XmlDocument document;
- static private string xmldsig = "http://www.w3.org/2000/09/xmldsig#";
-
- public DataObject ()
+ public DataObject ()
{
Build (null, null, null, null);
}
Build (id, mimeType, encoding, data);
}
+ // this one accept a null "data" parameter
private void Build (string id, string mimeType, string encoding, XmlElement data)
{
- StringBuilder sb = new StringBuilder ();
- sb.Append ("<Object ");
+ document = new XmlDocument ();
+ XmlElement xel = document.CreateElement (XmlSignature.ElementNames.Object, XmlSignature.NamespaceURI);
if (id != null) {
this.id = id;
- sb.Append ("Id=\"");
- sb.Append (id);
- sb.Append ("\" ");
+ xel.SetAttribute (XmlSignature.AttributeNames.Id, id);
}
if (mimeType != null) {
this.mimeType = mimeType;
- sb.Append ("MimeType=\"");
- sb.Append (mimeType);
- sb.Append ("\" ");
+ xel.SetAttribute (XmlSignature.AttributeNames.MimeType, mimeType);
}
if (encoding != null) {
this.encoding = encoding;
- sb.Append ("Encoding=\"");
- sb.Append (encoding);
- sb.Append ("\" ");
+ xel.SetAttribute (XmlSignature.AttributeNames.Encoding, encoding);
}
- 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);
+ XmlNode newNode = document.ImportNode (data, true);
+ xel.AppendChild (newNode);
}
+ document.AppendChild (xel);
}
// why is data a XmlNodeList instead of a XmlElement ?
public XmlNodeList Data {
get {
- XmlNodeList xnl = doc.GetElementsByTagName ("Object");
+ XmlNodeList xnl = document.GetElementsByTagName (XmlSignature.ElementNames.Object);
return xnl[0].ChildNodes;
}
set {
throw new ArgumentNullException ("value");
Build (id, mimeType, encoding, null);
- XmlNodeList xnl = doc.GetElementsByTagName ("Object");
+ XmlNodeList xnl = document.GetElementsByTagName (XmlSignature.ElementNames.Object);
if ((xnl != null) && (xnl.Count > 0)) {
foreach (XmlNode xn in value) {
- XmlNode newNode = doc.ImportNode (xn, true);
- xnl[0].AppendChild (newNode);
+ XmlNode newNode = document.ImportNode (xn, true);
+ xnl [0].AppendChild (newNode);
}
}
}
public XmlElement GetXml ()
{
- if ((doc.DocumentElement.LocalName == "Object") && (doc.DocumentElement.NamespaceURI == xmldsig)) {
+ if ((document.DocumentElement.LocalName == XmlSignature.ElementNames.Object) && (document.DocumentElement.NamespaceURI == XmlSignature.NamespaceURI)) {
// recreate all attributes in order
XmlAttribute xa = null;
- doc.DocumentElement.Attributes.RemoveAll ();
+ document.DocumentElement.Attributes.RemoveAll ();
if (id != null) {
- xa = doc.CreateAttribute ("Id");
+ xa = document.CreateAttribute (XmlSignature.AttributeNames.Id);
xa.Value = id;
- doc.DocumentElement.Attributes.Append (xa);
+ document.DocumentElement.Attributes.Append (xa);
}
if (mimeType != null) {
- xa = doc.CreateAttribute ("MimeType");
+ xa = document.CreateAttribute (XmlSignature.AttributeNames.MimeType);
xa.Value = mimeType;
- doc.DocumentElement.Attributes.Append (xa);
+ document.DocumentElement.Attributes.Append (xa);
}
if (encoding != null) {
- xa = doc.CreateAttribute ("Encoding");
+ xa = document.CreateAttribute (XmlSignature.AttributeNames.Encoding);
xa.Value = encoding;
- doc.DocumentElement.Attributes.Append (xa);
+ document.DocumentElement.Attributes.Append (xa);
}
- xa = doc.CreateAttribute ("xmlns");
- xa.Value = xmldsig;
- doc.DocumentElement.Attributes.Append (xa);
+ xa = document.CreateAttribute ("xmlns");
+ xa.Value = XmlSignature.NamespaceURI;
+ document.DocumentElement.Attributes.Append (xa);
}
- return doc.DocumentElement;
+ return document.DocumentElement;
}
public void LoadXml (XmlElement value)
if (value == null)
throw new ArgumentNullException ("value");
- if ((value.LocalName == "Object") && (value.NamespaceURI == xmldsig)) {
- doc.LoadXml (value.OuterXml);
- XmlAttribute xa = value.Attributes ["Id"];
+ if ((value.LocalName != XmlSignature.ElementNames.Object) || (value.NamespaceURI != XmlSignature.NamespaceURI)) {
+ document.LoadXml (value.OuterXml);
+ }
+ else {
+ document.LoadXml (value.OuterXml);
+ XmlAttribute xa = value.Attributes [XmlSignature.AttributeNames.Id];
id = ((xa != null) ? xa.InnerText : null);
- xa = value.Attributes ["MimeType"];
+ xa = value.Attributes [XmlSignature.AttributeNames.MimeType];
mimeType = ((xa != null) ? xa.InnerText : null);
- xa = value.Attributes ["Encoding"];
+ xa = value.Attributes [XmlSignature.AttributeNames.Encoding];
encoding = ((xa != null) ? xa.InnerText : null);
}
- else
- doc.LoadXml (value.OuterXml);
}
}
}
\ No newline at end of file
//
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 XmlElement GetXml ()
{
- StringBuilder sb = new StringBuilder ();
- sb.Append ("<KeyInfo xmlns=\"");
- sb.Append (xmldsig);
- sb.Append ("\" />");
-
- XmlDocument doc = new XmlDocument ();
- doc.LoadXml (sb.ToString ());
+ XmlDocument document = new XmlDocument ();
+ XmlElement xel = document.CreateElement (XmlSignature.ElementNames.KeyInfo, XmlSignature.NamespaceURI);
// 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);
+ XmlNode newNode = document.ImportNode (xn, true);
+ xel.AppendChild (newNode);
}
- return doc.DocumentElement;
+ return xel;
}
public void LoadXml (XmlElement value)
if (value == null)
throw new ArgumentNullException ("value");
- if ((value.LocalName == "KeyInfo") && (value.NamespaceURI == xmldsig)) {
+ if ((value.LocalName == XmlSignature.ElementNames.KeyInfo) && (value.NamespaceURI == XmlSignature.NamespaceURI)) {
foreach (XmlNode n in value.ChildNodes) {
KeyInfoClause kic = null;
if (n is XmlWhitespace)
continue;
switch (n.LocalName) {
- case "KeyValue":
+ case XmlSignature.ElementNames.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":
+ case XmlSignature.ElementNames.DSAKeyValue:
kic = (KeyInfoClause) new DSAKeyValue ();
break;
- case "RSAKeyValue":
+ case XmlSignature.ElementNames.RSAKeyValue:
kic = (KeyInfoClause) new RSAKeyValue ();
break;
}
}
}
break;
- case "KeyName":
+ case XmlSignature.ElementNames.KeyName:
kic = (KeyInfoClause) new KeyInfoName ();
break;
- case "RetrievalMethod":
+ case XmlSignature.ElementNames.RetrievalMethod:
kic = (KeyInfoClause) new KeyInfoRetrievalMethod ();
break;
- case "X509Data":
+ case XmlSignature.ElementNames.X509Data:
kic = (KeyInfoClause) new KeyInfoX509Data ();
break;
- case "RSAKeyValue":
+/* case XmlSignature.ElementNames.RSAKeyValue:
kic = (KeyInfoClause) new RSAKeyValue ();
- break;
+ break;*/
default:
kic = (KeyInfoClause) new KeyInfoNode ();
break;
// (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
//
-using System.Text;
using System.Xml;
namespace System.Security.Cryptography.Xml {
public class KeyInfoName : KeyInfoClause {
- static private string xmldsig = "http://www.w3.org/2000/09/xmldsig#";
-
- private string Name;
+ private string name;
public KeyInfoName() {}
public string Value {
- get { return Name; }
- set { Name = value; }
+ get { return name; }
+ set { name = value; }
}
public override XmlElement GetXml ()
{
- StringBuilder sb = new StringBuilder ();
- sb.Append ("<KeyName xmlns=\"");
- sb.Append (xmldsig);
- sb.Append ("\">");
- sb.Append (Name);
- sb.Append ("</KeyName>");
-
- XmlDocument doc = new XmlDocument ();
- doc.LoadXml(sb.ToString ());
- return doc.DocumentElement;
+ XmlDocument document = new XmlDocument ();
+ XmlElement xel = document.CreateElement (XmlSignature.ElementNames.KeyName, XmlSignature.NamespaceURI);
+ xel.InnerText = name;
+ return xel;
}
public override void LoadXml (XmlElement value)
{
if (value == null)
throw new ArgumentNullException ();
-
- if ((value.LocalName == "KeyName") && (value.NamespaceURI == xmldsig))
- Name = value.InnerXml;
+ if ((value.LocalName != XmlSignature.ElementNames.KeyName) || (value.NamespaceURI != XmlSignature.NamespaceURI))
+ name = "";
else
- Name = null;
+ name = value.InnerText;
}
}
}
\ No newline at end of file
// (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
//
-using System.Text;
using System.Xml;
namespace System.Security.Cryptography.Xml {
// (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
//
-using System.Text;
using System.Xml;
namespace System.Security.Cryptography.Xml {
public class KeyInfoRetrievalMethod : KeyInfoClause {
- static private string xmldsig = "http://www.w3.org/2000/09/xmldsig#";
-
private string URI;
public KeyInfoRetrievalMethod () {}
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=\"");
- sb.Append (xmldsig);
- sb.Append ("\" />");
-
- XmlDocument doc = new XmlDocument ();
- doc.LoadXml(sb.ToString ());
- return doc.DocumentElement;
+ XmlDocument document = new XmlDocument ();
+ XmlElement xel = document.CreateElement (XmlSignature.ElementNames.RetrievalMethod, XmlSignature.NamespaceURI);
+ if (URI != null)
+ xel.SetAttribute (XmlSignature.AttributeNames.URI, URI);
+ return xel;
}
public override void LoadXml (XmlElement value)
if (value == null)
throw new ArgumentNullException ();
- if ((value.LocalName == "RetrievalElement") && (value.NamespaceURI == xmldsig)) {
- URI = value.Attributes["URI"].Value;
- }
- else
+ if ((value.LocalName != XmlSignature.ElementNames.RetrievalMethod) || (value.NamespaceURI != XmlSignature.NamespaceURI))
URI = ""; // not null - so we return URI="" as attribute !!!
+ else
+ URI = value.Attributes [XmlSignature.AttributeNames.URI].Value;
}
}
}
\ No newline at end of file
using System.Collections;
using System.Security.Cryptography.X509Certificates;
-using System.Text;
using System.Xml;
namespace System.Security.Cryptography.Xml {
public class KeyInfoX509Data : KeyInfoClause {
- static private string xmldsig = "http://www.w3.org/2000/09/xmldsig#";
-
private byte[] x509crl;
private ArrayList IssuerSerialList;
private ArrayList SubjectKeyIdList;
if ((x509crl == null) && (count == 0))
throw new CryptographicException ("value");
- StringBuilder sb = new StringBuilder ();
- sb.Append ("<X509Data xmlns=\"");
- sb.Append (xmldsig);
- sb.Append ("\">");
+ XmlDocument document = new XmlDocument ();
+ XmlElement xel = document.CreateElement (XmlSignature.ElementNames.X509Data, XmlSignature.NamespaceURI);
+ // FIXME: hack to match MS implementation
+ xel.SetAttribute ("xmlns", XmlSignature.NamespaceURI);
// <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>");
+ XmlElement isl = document.CreateElement (XmlSignature.ElementNames.X509IssuerSerial, XmlSignature.NamespaceURI);
+ XmlElement xin = document.CreateElement (XmlSignature.ElementNames.X509IssuerName, XmlSignature.NamespaceURI);
+ xin.InnerText = iser.Issuer;
+ isl.AppendChild (xin);
+ XmlElement xsn = document.CreateElement (XmlSignature.ElementNames.X509SerialNumber, XmlSignature.NamespaceURI);
+ xsn.InnerText = iser.Serial;
+ isl.AppendChild (xsn);
+ xel.AppendChild (isl);
}
- sb.Append ("</X509IssuerSerial>");
}
// <X509SKI>
if (SubjectKeyIdList.Count > 0) {
foreach (byte[] skid in SubjectKeyIdList) {
- sb.Append ("<X509SKI>");
- sb.Append (Convert.ToBase64String (skid));
- sb.Append ("</X509SKI>");
+ XmlElement ski = document.CreateElement (XmlSignature.ElementNames.X509SKI, XmlSignature.NamespaceURI);
+ ski.InnerText = Convert.ToBase64String (skid);
+ xel.AppendChild (ski);
}
}
// <X509SubjectName>
if (SubjectNameList.Count > 0) {
foreach (string subject in SubjectNameList) {
- sb.Append ("<X509SubjectName>");
- sb.Append (subject);
- sb.Append ("</X509SubjectName>");
+ XmlElement sn = document.CreateElement (XmlSignature.ElementNames.X509SubjectName, XmlSignature.NamespaceURI);
+ sn.InnerText = subject;
+ xel.AppendChild (sn);
}
}
// <X509Certificate>
if (X509CertificateList.Count > 0) {
foreach (X509Certificate x509 in X509CertificateList) {
- sb.Append ("<X509Certificate>");
- sb.Append (Convert.ToBase64String (x509.GetRawCertData ()));
- sb.Append ("</X509Certificate>");
+ XmlElement cert = document.CreateElement (XmlSignature.ElementNames.X509Certificate, XmlSignature.NamespaceURI);
+ cert.InnerText = Convert.ToBase64String (x509.GetRawCertData ());
+ xel.AppendChild (cert);
}
}
// only one <X509CRL>
if (x509crl != null) {
- sb.Append ("<X509CRL>");
- sb.Append (Convert.ToBase64String (x509crl));
- sb.Append ("</X509CRL>");
+ XmlElement crl = document.CreateElement (XmlSignature.ElementNames.X509CRL, XmlSignature.NamespaceURI);
+ crl.InnerText = Convert.ToBase64String (x509crl);
+ xel.AppendChild (crl);
}
- sb.Append ("</X509Data>");
-
- XmlDocument doc = new XmlDocument ();
- doc.LoadXml(sb.ToString ());
- return doc.DocumentElement;
+ return xel;
}
public override void LoadXml (XmlElement element)
X509CertificateList.Clear ();
x509crl = null;
- if ((element.LocalName == "X509Data") && (element.NamespaceURI == xmldsig)) {
- XmlNodeList xnl = null;
- // <X509IssuerSerial>
- xnl = element.GetElementsByTagName ("X509IssuerSerial", xmldsig);
- if (xnl != null) {
- for (int i=0; i < xnl.Count; i++) {
- XmlElement xel = (XmlElement) xnl[i];
- XmlNodeList issuer = xel.GetElementsByTagName ("X509IssuerName", xmldsig);
- XmlNodeList serial = xel.GetElementsByTagName ("X509SerialNumber", xmldsig);
- AddIssuerSerial (issuer[0].InnerText, serial[0].InnerText);
- }
- }
- // <X509SKI>
- xnl = element.GetElementsByTagName ("X509SKI", xmldsig);
- if (xnl != null) {
- for (int i=0; i < xnl.Count; i++) {
- byte[] skid = Convert.FromBase64String (xnl[i].InnerXml);
- AddSubjectKeyId (skid);
- }
+ if ((element.LocalName != XmlSignature.ElementNames.X509Data) || (element.NamespaceURI != XmlSignature.NamespaceURI))
+ throw new CryptographicException ("element");
+
+ XmlNodeList xnl = null;
+ // <X509IssuerSerial>
+ xnl = element.GetElementsByTagName (XmlSignature.ElementNames.X509IssuerSerial, XmlSignature.NamespaceURI);
+ if (xnl != null) {
+ for (int i=0; i < xnl.Count; i++) {
+ XmlElement xel = (XmlElement) xnl[i];
+ XmlNodeList issuer = xel.GetElementsByTagName (XmlSignature.ElementNames.X509IssuerName, XmlSignature.NamespaceURI);
+ XmlNodeList serial = xel.GetElementsByTagName (XmlSignature.ElementNames.X509SerialNumber, XmlSignature.NamespaceURI);
+ AddIssuerSerial (issuer[0].InnerText, serial[0].InnerText);
}
- // <X509SubjectName>
- xnl = element.GetElementsByTagName ("X509SubjectName", xmldsig);
- if (xnl != null) {
- for (int i=0; i < xnl.Count; i++) {
- AddSubjectName (xnl[i].InnerXml);
- }
+ }
+ // <X509SKI>
+ xnl = element.GetElementsByTagName (XmlSignature.ElementNames.X509SKI, XmlSignature.NamespaceURI);
+ if (xnl != null) {
+ for (int i=0; i < xnl.Count; i++) {
+ byte[] skid = Convert.FromBase64String (xnl[i].InnerXml);
+ AddSubjectKeyId (skid);
}
- // <X509Certificate>
- xnl = element.GetElementsByTagName ("X509Certificate", xmldsig);
- if (xnl != null) {
- for (int i=0; i < xnl.Count; i++) {
- byte[] cert = Convert.FromBase64String (xnl[i].InnerXml);
- AddCertificate (new X509Certificate (cert));
- }
+ }
+ // <X509SubjectName>
+ xnl = element.GetElementsByTagName (XmlSignature.ElementNames.X509SubjectName, XmlSignature.NamespaceURI);
+ if (xnl != null) {
+ for (int i=0; i < xnl.Count; i++) {
+ AddSubjectName (xnl[i].InnerXml);
}
- // only one <X509CRL>
- xnl = element.GetElementsByTagName ("X509CRL", xmldsig);
- if ((xnl != null) && (xnl.Count > 0)) {
- x509crl = Convert.FromBase64String (xnl[0].InnerXml);
+ }
+ // <X509Certificate>
+ xnl = element.GetElementsByTagName (XmlSignature.ElementNames.X509Certificate, XmlSignature.NamespaceURI);
+ if (xnl != null) {
+ for (int i=0; i < xnl.Count; i++) {
+ byte[] cert = Convert.FromBase64String (xnl[i].InnerXml);
+ AddCertificate (new X509Certificate (cert));
}
}
- else
- throw new CryptographicException ("element");
+ // only one <X509CRL>
+ xnl = element.GetElementsByTagName (XmlSignature.ElementNames.X509CRL, XmlSignature.NamespaceURI);
+ if ((xnl != null) && (xnl.Count > 0)) {
+ x509crl = Convert.FromBase64String (xnl[0].InnerXml);
+ }
}
}
}
\ No newline at end of file
public class RSAKeyValue : KeyInfoClause {
- static private string xmldsig = "http://www.w3.org/2000/09/xmldsig#";
-
private RSA rsa;
public RSAKeyValue ()
public override XmlElement GetXml ()
{
- StringBuilder sb = new StringBuilder ();
- sb.Append ("<KeyValue xmlns=\"");
- sb.Append (xmldsig);
- sb.Append ("\">");
- sb.Append (rsa.ToXmlString (false));
- sb.Append ("</KeyValue>");
-
- XmlDocument doc = new XmlDocument ();
- doc.LoadXml(sb.ToString ());
- return doc.DocumentElement;
+ XmlDocument document = new XmlDocument ();
+ document.LoadXml ("<KeyValue xmlns=\"" + XmlSignature.NamespaceURI + "\">" + rsa.ToXmlString (false) + "</KeyValue>");
+ return document.DocumentElement;
+
+ // FIX: this way we get a xmlns="" in RSAKeyValue
+/* XmlElement xel = document.CreateElement (XmlSignature.ElementNames.KeyValue, XmlSignature.NamespaceURI);
+ xel.InnerXml = rsa.ToXmlString (false);
+ return xel;*/
}
public override void LoadXml (XmlElement value)
if (value == null)
throw new ArgumentNullException ();
- if ((value.LocalName == "KeyValue") && (value.NamespaceURI == xmldsig))
- rsa.FromXmlString (value.InnerXml);
- else
+ // FIXME: again hack to match MS implementation (required for previous hack)
+ if ((value.LocalName != XmlSignature.ElementNames.KeyValue) || ((value.NamespaceURI != XmlSignature.NamespaceURI) && (value.GetAttribute("xmlns") != XmlSignature.NamespaceURI)))
throw new CryptographicException ("value");
+
+ rsa.FromXmlString (value.InnerXml);
}
}
}
\ No newline at end of file
//
using System.IO;
-using System.Text;
using System.Xml;
namespace System.Security.Cryptography.Xml {
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;
+ digestMethod = XmlSignature.NamespaceURI + "sha1";
}
[MonoTODO()]
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 ());
+ XmlDocument document = new XmlDocument ();
+ XmlElement xel = document.CreateElement (XmlSignature.ElementNames.Reference, XmlSignature.NamespaceURI);
+ if (id != null)
+ xel.SetAttribute (XmlSignature.AttributeNames.Id, id);
+ if (uri != null)
+ xel.SetAttribute (XmlSignature.AttributeNames.URI, uri);
+ if (type != null)
+ xel.SetAttribute (XmlSignature.AttributeNames.Type, type);
if (chain.Count > 0) {
- XmlNodeList xnl = doc.GetElementsByTagName ("Transforms");
+ XmlElement ts = document.CreateElement (XmlSignature.ElementNames.Transforms, XmlSignature.NamespaceURI);
foreach (Transform t in chain) {
XmlNode xn = t.GetXml ();
- XmlNode newNode = doc.ImportNode (xn, true);
- xnl[0].AppendChild (newNode);
+ XmlNode newNode = document.ImportNode (xn, true);
+ ts.AppendChild (newNode);
}
+ xel.AppendChild (ts);
}
- return doc.DocumentElement;
+ XmlElement dm = document.CreateElement (XmlSignature.ElementNames.DigestMethod, XmlSignature.NamespaceURI);
+ dm.SetAttribute (XmlSignature.AttributeNames.Algorithm, digestMethod);
+ xel.AppendChild (dm);
+
+ XmlElement dv = document.CreateElement (XmlSignature.ElementNames.DigestValue, XmlSignature.NamespaceURI);
+ dv.InnerText = Convert.ToBase64String (digestValue);
+ xel.AppendChild (dv);
+
+ return xel;
}
private string GetAttributeFromElement (XmlElement xel, string attribute, string element)
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);
+ if ((value.LocalName != XmlSignature.ElementNames.Reference) || (value.NamespaceURI != XmlSignature.NamespaceURI))
+ throw new CryptographicException ();
+
+ id = GetAttribute (value, XmlSignature.AttributeNames.Id);
+ uri = GetAttribute (value, XmlSignature.AttributeNames.URI);
+ type = GetAttribute (value, XmlSignature.AttributeNames.Type);
+ // Note: order is important for validations
+ XmlNodeList xnl = value.GetElementsByTagName (XmlSignature.ElementNames.Transform);
+ if ((xnl != null) && (xnl.Count > 0)) {
+ Transform t = null;
+ foreach (XmlNode xn in xnl) {
+ string a = GetAttribute ((XmlElement)xn, XmlSignature.AttributeNames.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 ();
}
- }
- // 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);
+ AddTransform (t);
}
}
- else
- throw new CryptographicException ();
+ // get DigestMethod
+ DigestMethod = GetAttributeFromElement (value, XmlSignature.AttributeNames.Algorithm, XmlSignature.ElementNames.DigestMethod);
+ // get DigestValue
+ xnl = value.GetElementsByTagName (XmlSignature.ElementNames.DigestValue);
+ if ((xnl != null) && (xnl.Count > 0)) {
+ DigestValue = Convert.FromBase64String (xnl[0].InnerText);
+ }
}
}
}
using System.Collections;
using System.Security.Cryptography;
-using System.Text;
using System.Xml;
namespace System.Security.Cryptography.Xml {
public class Signature {
- static private string xmldsig = "http://www.w3.org/2000/09/xmldsig#";
-
private ArrayList list;
private SignedInfo info;
private KeyInfo key;
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);
- sb.Append ("\" />");
-
- XmlDocument doc = new XmlDocument ();
- doc.LoadXml (sb.ToString ());
+ XmlDocument document = new XmlDocument ();
+ XmlElement xel = document.CreateElement (XmlSignature.ElementNames.Signature, XmlSignature.NamespaceURI);
+ if (id != null)
+ xel.SetAttribute (XmlSignature.AttributeNames.Id, id);
- 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);
- }
+ XmlNode xn = info.GetXml ();
+ XmlNode newNode = document.ImportNode (xn, true);
+ xel.AppendChild (newNode);
if (signature != null) {
- XmlElement sv = doc.CreateElement ("SignatureValue", xmldsig);
+ XmlElement sv = document.CreateElement (XmlSignature.ElementNames.SignatureValue, XmlSignature.NamespaceURI);
sv.InnerText = Convert.ToBase64String (signature);
- doc.DocumentElement.AppendChild (sv);
+ xel.AppendChild (sv);
}
if (key != null) {
xn = key.GetXml ();
- newNode = doc.ImportNode (xn, true);
- doc.DocumentElement.AppendChild (newNode);
+ newNode = document.ImportNode (xn, true);
+ xel.AppendChild (newNode);
}
if (list.Count > 0) {
foreach (DataObject obj in list) {
xn = obj.GetXml ();
- newNode = doc.ImportNode (xn, true);
- doc.DocumentElement.AppendChild (newNode);
+ newNode = document.ImportNode (xn, true);
+ xel.AppendChild (newNode);
}
}
- return doc.DocumentElement;
+ return xel;
}
private string GetAttribute (XmlElement xel, string attribute)
if (value == null)
throw new ArgumentNullException ("value");
- if ((value.LocalName == "Signature") && (value.NamespaceURI == xmldsig)) {
- id = GetAttribute (value, "Id");
+ if ((value.LocalName == XmlSignature.ElementNames.Signature) && (value.NamespaceURI == XmlSignature.NamespaceURI)) {
+ id = GetAttribute (value, XmlSignature.AttributeNames.Id);
- XmlNodeList xnl = value.GetElementsByTagName ("SignedInfo");
+ XmlNodeList xnl = value.GetElementsByTagName (XmlSignature.ElementNames.SignedInfo);
if ((xnl != null) && (xnl.Count == 1)) {
info = new SignedInfo ();
info.LoadXml ((XmlElement) xnl[0]);
}
- xnl = value.GetElementsByTagName ("SignatureValue");
+ xnl = value.GetElementsByTagName (XmlSignature.ElementNames.SignatureValue);
if ((xnl != null) && (xnl.Count == 1)) {
signature = Convert.FromBase64String (xnl[0].InnerText);
}
- xnl = value.GetElementsByTagName ("KeyInfo");
+ xnl = value.GetElementsByTagName (XmlSignature.ElementNames.KeyInfo);
if ((xnl != null) && (xnl.Count == 1)) {
key = new KeyInfo ();
key.LoadXml ((XmlElement) xnl[0]);
}
- xnl = value.GetElementsByTagName ("Object");
+ xnl = value.GetElementsByTagName (XmlSignature.ElementNames.Object);
if ((xnl != null) && (xnl.Count > 0)) {
foreach (XmlNode xn in xnl) {
DataObject obj = new DataObject ();
//
using System.Collections;
-using System.Text;
using System.Xml;
namespace System.Security.Cryptography.Xml {
public class SignedInfo : ICollection, IEnumerable {
- static private string xmldsig = "http://www.w3.org/2000/09/xmldsig#";
-
private ArrayList references;
private string c14nMethod;
private string id;
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 ("\">");
+ XmlDocument document = new XmlDocument ();
+ XmlElement xel = document.CreateElement (XmlSignature.ElementNames.SignedInfo, XmlSignature.NamespaceURI);
+ if (id != null)
+ xel.SetAttribute (XmlSignature.AttributeNames.Id, id);
+
if (c14nMethod != null) {
- sb.Append ("<CanonicalizationMethod Algorithm=\"");
- sb.Append (c14nMethod);
- sb.Append ("\" />");
+ XmlElement c14n = document.CreateElement (XmlSignature.ElementNames.CanonicalizationMethod, XmlSignature.NamespaceURI);
+ c14n.SetAttribute (XmlSignature.AttributeNames.Algorithm, c14nMethod);
+ xel.AppendChild (c14n);
}
if (signatureMethod != null) {
- sb.Append ("<SignatureMethod Algorithm=\"");
- sb.Append (signatureMethod);
+ XmlElement sm = document.CreateElement (XmlSignature.ElementNames.SignatureMethod, XmlSignature.NamespaceURI);
+ sm.SetAttribute (XmlSignature.AttributeNames.Algorithm, signatureMethod);
if (signatureLength != null) {
- sb.Append ("\">");
- sb.Append ("<HMACOutputLength>");
- sb.Append (signatureLength);
- sb.Append ("</HMACOutputLength>");
- sb.Append ("</SignatureMethod>");
+ XmlElement hmac = document.CreateElement (XmlSignature.ElementNames.HMACOutputLength, XmlSignature.NamespaceURI);
+ hmac.InnerText = signatureLength;
+ sm.AppendChild (hmac);
}
- else
- sb.Append ("\" />");
+ xel.AppendChild (sm);
}
- 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);
+ XmlNode newNode = document.ImportNode (xn, true);
+ xel.AppendChild (newNode);
}
- return doc.DocumentElement;
+ return xel;
}
private string GetAttributeFromElement (XmlElement xel, string attribute, string element)
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");
- // TODO signatureLength for HMAC
- XmlNodeList xnl = value.GetElementsByTagName ("Reference");
- foreach (XmlNode xn in xnl) {
- Reference r = new Reference ();
- r.LoadXml ((XmlElement) xn);
- AddReference (r);
- }
- }
- else
+ if ((value.LocalName != XmlSignature.ElementNames.SignedInfo) || (value.NamespaceURI != XmlSignature.NamespaceURI))
throw new CryptographicException ();
+
+ id = GetAttribute (value, XmlSignature.AttributeNames.Id);
+ c14nMethod = GetAttributeFromElement (value, XmlSignature.AttributeNames.Algorithm, XmlSignature.ElementNames.CanonicalizationMethod);
+ signatureMethod = GetAttributeFromElement (value, XmlSignature.AttributeNames.Algorithm, XmlSignature.ElementNames.SignatureMethod);
+ // TODO signatureLength for HMAC
+ XmlNodeList xnl = value.GetElementsByTagName (XmlSignature.ElementNames.Reference);
+ foreach (XmlNode xn in xnl) {
+ Reference r = new Reference ();
+ r.LoadXml ((XmlElement) xn);
+ AddReference (r);
+ }
}
}
}
using System.Collections;
using System.IO;
using System.Security.Cryptography;
-using System.Text;
using System.Xml;
namespace System.Security.Cryptography.Xml {
// (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
//
-using System.Text;
using System.Xml;
namespace System.Security.Cryptography.Xml {
public abstract class Transform {
- static private string xmldsig = "http://www.w3.org/2000/09/xmldsig#";
-
private string algo;
public Transform () {}
public XmlElement GetXml ()
{
- StringBuilder sb = new StringBuilder ();
- sb.Append ("<Transform Algorithm=\"");
- sb.Append (algo);
-// sb.Append ("\" xmlns=\"");
-// sb.Append (xmldsig);
- sb.Append ("\" />");
-
- XmlDocument doc = new XmlDocument ();
- doc.LoadXml (sb.ToString ());
- return doc.DocumentElement;
+ XmlDocument document = new XmlDocument ();
+ XmlElement xel = document.CreateElement (XmlSignature.ElementNames.Transform, XmlSignature.NamespaceURI);
+ xel.SetAttribute (XmlSignature.AttributeNames.Algorithm, algo);
+ return xel;
}
public abstract void LoadInnerXml (XmlNodeList nodeList);
public XmlDsigEnvelopedSignatureTransform ()
{
+ Algorithm = "http://www.w3.org/2000/09/xmldsig#enveloped-signature";
comments = false;
}
public XmlDsigXPathTransform ()
{
+ Algorithm = "http://www.w3.org/TR/1999/REC-xpath-19991116";
}
public override Type[] InputTypes {
--- /dev/null
+//
+// XmlSignature.cs: Handles Xml Signature
+//
+// Author:
+// Sebastien Pouliot (spouliot@motus.com)
+//
+// (C) 2003 Motus Technologies Inc. (http://www.motus.com)
+//
+
+using System;
+
+namespace System.Security.Cryptography.Xml {
+
+ // following the design of WSE
+ internal class XmlSignature {
+
+ public class ElementNames {
+
+ public const string CanonicalizationMethod = "CanonicalizationMethod";
+ public const string DigestMethod = "DigestMethod";
+ public const string DigestValue = "DigestValue";
+ public const string DSAKeyValue = "DSAKeyValue";
+ public const string HMACOutputLength = "HMACOutputLength";
+ public const string KeyInfo = "KeyInfo";
+ public const string KeyName = "KeyName";
+ public const string KeyValue = "KeyValue";
+ public const string Object = "Object";
+ public const string Reference = "Reference";
+ // RetrievalMethod - RetrievalElement ??? seems like a BUG to me ?
+ public const string RetrievalMethod = "RetrievalElement";
+ public const string RSAKeyValue = "RSAKeyValue";
+ public const string Signature = "Signature";
+ public const string SignatureMethod = "SignatureMethod";
+ public const string SignatureValue = "SignatureValue";
+ public const string SignedInfo = "SignedInfo";
+ public const string Transform = "Transform";
+ public const string Transforms = "Transforms";
+ public const string X509Data = "X509Data";
+ public const string X509IssuerSerial = "X509IssuerSerial";
+ public const string X509IssuerName = "X509IssuerName";
+ public const string X509SerialNumber = "X509SerialNumber";
+ public const string X509SKI = "X509SKI";
+ public const string X509SubjectName = "X509SubjectName";
+ public const string X509Certificate = "X509Certificate";
+ public const string X509CRL = "X509CRL";
+
+ public ElementNames () {}
+ }
+
+ public class AttributeNames {
+
+ public const string Algorithm = "Algorithm";
+ public const string Encoding = "Encoding";
+ public const string Id = "Id";
+ public const string MimeType = "MimeType";
+ public const string Type = "Type";
+ public const string URI = "URI";
+
+ public AttributeNames () {}
+ }
+
+ public const string NamespaceURI = "http://www.w3.org/2000/09/xmldsig#";
+ public const string Prefix = "ds";
+
+ public XmlSignature () {}
+ }
+}