From d8361220ec9dd092cbd81b44ab94e2db2a5dec41 Mon Sep 17 00:00:00 2001 From: Sebastien Pouliot Date: Sat, 6 Sep 2003 03:55:44 +0000 Subject: [PATCH] 2003-09-06 Sebastien Pouliot * EncryptedKey.cs: Fixed typo. * Security.cs: Fixed compilation issues. * SignedXml.cs: Fixed compilation issues. * SignedXmlSignature.cs: Copied from System.Security assembly (where it is known as Signature.cs) as MS duplicate the class (in WSE) to fixed some issues. * XmlSignature.cs: Added more missing string used in System.Security. svn path=/trunk/mcs/; revision=17940 --- .../Microsoft.Web.Services.Security/ChangeLog | 10 + .../EncryptedKey.cs | 160 ++++++------- .../Security.cs | 214 +++++++++--------- .../SignedXml.cs | 60 +++-- .../SignedXmlSignature.cs | 160 +++++++++++++ .../XmlSignature.cs | 2 + 6 files changed, 397 insertions(+), 209 deletions(-) create mode 100755 mcs/class/Microsoft.Web.Services/Microsoft.Web.Services.Security/SignedXmlSignature.cs diff --git a/mcs/class/Microsoft.Web.Services/Microsoft.Web.Services.Security/ChangeLog b/mcs/class/Microsoft.Web.Services/Microsoft.Web.Services.Security/ChangeLog index e1fbfa3b87a..d471b553b92 100755 --- a/mcs/class/Microsoft.Web.Services/Microsoft.Web.Services.Security/ChangeLog +++ b/mcs/class/Microsoft.Web.Services/Microsoft.Web.Services.Security/ChangeLog @@ -1,3 +1,13 @@ +2003-09-06 Sebastien Pouliot + + * EncryptedKey.cs: Fixed typo. + * Security.cs: Fixed compilation issues. + * SignedXml.cs: Fixed compilation issues. + * SignedXmlSignature.cs: Copied from System.Security assembly (where + it is known as Signature.cs) as MS duplicate the class (in WSE) to + fixed some issues. + * XmlSignature.cs: Added more missing string used in System.Security. + 2003-09-04 Sebastien Pouliot * Reference.cs: Copied from System.Security assembly as MS duplicate diff --git a/mcs/class/Microsoft.Web.Services/Microsoft.Web.Services.Security/EncryptedKey.cs b/mcs/class/Microsoft.Web.Services/Microsoft.Web.Services.Security/EncryptedKey.cs index 19df36ad81c..f6f5cd9bd90 100644 --- a/mcs/class/Microsoft.Web.Services/Microsoft.Web.Services.Security/EncryptedKey.cs +++ b/mcs/class/Microsoft.Web.Services/Microsoft.Web.Services.Security/EncryptedKey.cs @@ -1,80 +1,80 @@ -// -// EncryptedKey.cs: Handles WS-Security EncryptedKey -// -// Author: -// Sebastien Pouliot (spouliot@motus.com) -// -// (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com) -// -// Licensed under MIT X11 (see LICENSE) with this specific addition: -// -// “This source code may incorporate intellectual property owned by Microsoft -// Corporation. Our provision of this source code does not include any licenses -// or any other rights to you under any Microsoft intellectual property. If you -// would like a license from Microsoft (e.g. rebrand, redistribute), you need -// to contact Microsoft directly.” -// - -using System; -using System.Security.Cryptography.Xml; -using System.Xml; - -namespace Microsoft.Web.Services.Security { - - public class EncryptedKey : IXmlElement { - - private AsymmetricEncryptionKey aek; - private KeyInfo ki; - private ReferenceList list; - - internal EncryptedKey () - { - list = new ReferenceList (); - } - - public EncryptedKey (AsymmetricEncryptionKey key) : this () - { - if (key == null) - throw new ArgumentNullException ("key"); - aek = key; - ki = ki.KeyInfo; - } - - public EncryptedKey (XmlElement element) : this () - { - LoadXml (element); - } - - public string EncryptionMethod { - get { return null; } - } - - public KeyInfo KeyInfo { - get { return ki; } - } - - public ReferenceList ReferenceList { - get { return list; } - } - - public XmlElement GetXml (XmlDocument document) - { - if (document == null) - throw new ArgumentNullException ("document"); - return ki.GetXml (); - } - - public void LoadXml (XmlElement element) - { - if ((element.LocalName != "") || (element.NamespaceURI != "")) - throw new System.ArgumentException ("invalid LocalName or NamespaceURI"); - ki = new KeyInfo (); - try { - ki.LoadXml (element); - } - catch { - throw new ArgumentException ("element has no KeyInfo"); - } - } - } -} +// +// EncryptedKey.cs: Handles WS-Security EncryptedKey +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com) +// +// Licensed under MIT X11 (see LICENSE) with this specific addition: +// +// “This source code may incorporate intellectual property owned by Microsoft +// Corporation. Our provision of this source code does not include any licenses +// or any other rights to you under any Microsoft intellectual property. If you +// would like a license from Microsoft (e.g. rebrand, redistribute), you need +// to contact Microsoft directly.” +// + +using System; +using System.Security.Cryptography.Xml; +using System.Xml; + +namespace Microsoft.Web.Services.Security { + + public class EncryptedKey : IXmlElement { + + private AsymmetricEncryptionKey aek; + private KeyInfo ki; + private ReferenceList list; + + internal EncryptedKey () + { + list = new ReferenceList (); + } + + public EncryptedKey (AsymmetricEncryptionKey key) : this () + { + if (key == null) + throw new ArgumentNullException ("key"); + aek = key; + ki = new KeyInfo (); + } + + public EncryptedKey (XmlElement element) : this () + { + LoadXml (element); + } + + public string EncryptionMethod { + get { return null; } + } + + public KeyInfo KeyInfo { + get { return ki; } + } + + public ReferenceList ReferenceList { + get { return list; } + } + + public XmlElement GetXml (XmlDocument document) + { + if (document == null) + throw new ArgumentNullException ("document"); + return ki.GetXml (); + } + + public void LoadXml (XmlElement element) + { + if ((element.LocalName != "") || (element.NamespaceURI != "")) + throw new System.ArgumentException ("invalid LocalName or NamespaceURI"); + ki = new KeyInfo (); + try { + ki.LoadXml (element); + } + catch { + throw new ArgumentException ("element has no KeyInfo"); + } + } + } +} diff --git a/mcs/class/Microsoft.Web.Services/Microsoft.Web.Services.Security/Security.cs b/mcs/class/Microsoft.Web.Services/Microsoft.Web.Services.Security/Security.cs index 84be58f3274..c62eed241a6 100644 --- a/mcs/class/Microsoft.Web.Services/Microsoft.Web.Services.Security/Security.cs +++ b/mcs/class/Microsoft.Web.Services/Microsoft.Web.Services.Security/Security.cs @@ -1,107 +1,107 @@ -// -// Security.cs: Handles WS-Security Security -// -// Author: -// Sebastien Pouliot (spouliot@motus.com) -// -// (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com) -// -// Licensed under MIT X11 (see LICENSE) with this specific addition: -// -// “This source code may incorporate intellectual property owned by Microsoft -// Corporation. Our provision of this source code does not include any licenses -// or any other rights to you under any Microsoft intellectual property. If you -// would like a license from Microsoft (e.g. rebrand, redistribute), you need -// to contact Microsoft directly.” -// - -using System; -using System.Web.Services.Protocols; -using System.Xml; - -namespace Microsoft.Web.Services.Security { - - public class Security : SoapHeader, IXmlElement { - - private static string SoapActor = "actor"; // not Actor - no capital A - private static string SoapNamespaceURI = "http://www.w3.org/2001/12/soap-envelope"; - - private SecurityElementCollection elems; - private SecurityTokenCollection tokens; - - public Security (string actor) - { - if (actor == null) - throw new ArgumentNullException ("actor"); - Actor = actor; - } - - public Security (XmlElement element) - { - LoadXml (element); - } - - public SecurityElementCollection Elements { - get { return elems; } - } - - public SecurityTokenCollection Tokens { - get { return tokens; } - } - - public XmlElement GetXml (XmlDocument document) - { - if (document == null) - throw new ArgumentNullException ("document"); - - // much cleaner than using StringBuilder! - XmlElement xel = document.CreateElement (WSSecurity.Prefix, WSSecurity.ElementNames.Security, WSSecurity.NamespaceURI); - xel.SetAttribute (SoapActor, SoapNamespaceURI, Actor); - - foreach (ISecurityElement se in Elements) { - if (se is Signature) { - // TODO - } - else if (se is EncryptedData) { - xel.AppendChild ((se as EncryptedData).GetXml (document)); - } - } - - foreach (SecurityToken st in Tokens) - xel.AppendChild (st.GetXml (document)); - - return xel; - } - - // base class doesn't have a LoadXml method - public void LoadXml (XmlElement element) - { - if ((element.LocalName != WSSecurity.ElementNames.Security) || (element.NamespaceURI != WSSecurity.NamespaceURI)) - throw new System.ArgumentException ("invalid LocalName or NamespaceURI"); - - // get attributes - XmlAttribute xa = element.Attributes [SoapActor, SoapNamespaceURI]; - Actor = ((xa == null) ? null : xa.Value); - - Elements.Clear (); - Tokens.Clear (); - foreach (XmlNode xn in element.ChildNodes) { - XmlElement xel = (XmlElement) xn; - switch (xn.NamespaceURI) { - case WSSecurity.NamespaceURI: - switch (xn.LocalName) { - case WSSecurity.ElementNames.UsernameToken: - UsernameToken unt = new UsernameToken (xel); - Tokens.Add (unt); - break; - case WSSecurity.ElementNames.BinarySecurityToken: - BinarySecurityToken bst = new BinarySecurityToken (xel); - Tokens.Add (bst); - break; - } - break; - } - } - } - } -} +// +// Security.cs: Handles WS-Security Security +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com) +// +// Licensed under MIT X11 (see LICENSE) with this specific addition: +// +// “This source code may incorporate intellectual property owned by Microsoft +// Corporation. Our provision of this source code does not include any licenses +// or any other rights to you under any Microsoft intellectual property. If you +// would like a license from Microsoft (e.g. rebrand, redistribute), you need +// to contact Microsoft directly.” +// + +using System; +using System.Web.Services.Protocols; +using System.Xml; + +namespace Microsoft.Web.Services.Security { + + public class Security : SoapHeader, IXmlElement { + + private static string SoapActor = "actor"; // not Actor - no capital A + private static string SoapNamespaceURI = "http://www.w3.org/2001/12/soap-envelope"; + + private SecurityElementCollection elems; + private SecurityTokenCollection tokens; + + public Security (string actor) + { + if (actor == null) + throw new ArgumentNullException ("actor"); + Actor = actor; + } + + public Security (XmlElement element) + { + LoadXml (element); + } + + public SecurityElementCollection Elements { + get { return elems; } + } + + public SecurityTokenCollection Tokens { + get { return tokens; } + } + + public XmlElement GetXml (XmlDocument document) + { + if (document == null) + throw new ArgumentNullException ("document"); + + // much cleaner than using StringBuilder! + XmlElement xel = document.CreateElement (WSSecurity.Prefix, WSSecurity.ElementNames.Security, WSSecurity.NamespaceURI); + xel.SetAttribute (SoapActor, SoapNamespaceURI, Actor); + + foreach (ISecurityElement se in Elements) { + if (se is Signature) { + // TODO + } + else if (se is EncryptedData) { + xel.AppendChild ((se as EncryptedData).GetXml (document)); + } + } + + foreach (SecurityToken st in Tokens) + xel.AppendChild (st.GetXml (document)); + + return xel; + } + + // base class doesn't have a LoadXml method + public void LoadXml (XmlElement element) + { + if ((element.LocalName != WSSecurity.ElementNames.Security) || (element.NamespaceURI != WSSecurity.NamespaceURI)) + throw new System.ArgumentException ("invalid LocalName or NamespaceURI"); + + // get attributes + XmlAttribute xa = element.Attributes [SoapActor, SoapNamespaceURI]; + Actor = ((xa == null) ? null : xa.Value); + + Elements.Clear (); + Tokens.Clear (); + foreach (XmlNode xn in element.ChildNodes) { + XmlElement xel = (XmlElement) xn; + switch (xn.NamespaceURI) { + case WSSecurity.NamespaceURI: + switch (xn.LocalName) { + case WSSecurity.ElementNames.UsernameToken: + UsernameToken unt = new UsernameToken (xel); + Tokens.Add (unt); + break; + case WSSecurity.ElementNames.BinarySecurityToken: +//FIXME BinarySecurityToken bst = new BinarySecurityToken (xel); +//FIXME Tokens.Add (bst); + break; + } + break; + } + } + } + } +} diff --git a/mcs/class/Microsoft.Web.Services/Microsoft.Web.Services.Security/SignedXml.cs b/mcs/class/Microsoft.Web.Services/Microsoft.Web.Services.Security/SignedXml.cs index 2283cab1d9a..2be94a088b1 100755 --- a/mcs/class/Microsoft.Web.Services/Microsoft.Web.Services.Security/SignedXml.cs +++ b/mcs/class/Microsoft.Web.Services/Microsoft.Web.Services.Security/SignedXml.cs @@ -7,36 +7,49 @@ // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com) // +using System; using System.Collections; using System.IO; using System.Runtime.InteropServices; using System.Security.Cryptography; using System.Xml; +using SSCX = System.Security.Cryptography.Xml; + #if (WSE1 || WSE2) -using System.Security.Cryptography.Xml; +using Microsoft.Web.Services.Security; namespace Microsoft.Web.Services.Security { #else +using System.Security.Cryptography.Xml; + namespace System.Security.Cryptography.Xml { #endif public class SignedXml { - private Signature signature; - private AsymmetricAlgorithm key; - private string keyName; - private XmlDocument envdoc; +#if (WSE1 || WSE2) + private SignedXmlSignature signature; public SignedXml () { - signature = new Signature (); + signature = new SignedXmlSignature (); signature.SignedInfo = new SignedInfo (); } +#else + private Signature signature; - public SignedXml (XmlDocument document) + public SignedXml () { signature = new Signature (); signature.SignedInfo = new SignedInfo (); + } +#endif + private AsymmetricAlgorithm key; + private string keyName; + private XmlDocument envdoc; + + public SignedXml (XmlDocument document) : this () + { envdoc = document; } @@ -44,8 +57,6 @@ namespace System.Security.Cryptography.Xml { { 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"; @@ -57,15 +68,20 @@ namespace System.Security.Cryptography.Xml { public const string XmlDsigRSASHA1Url = XmlDsigNamespaceUrl + "rsa-sha1"; public const string XmlDsigSHA1Url = XmlDsigNamespaceUrl + "sha1"; - public KeyInfo KeyInfo { + public SSCX.KeyInfo KeyInfo { get { return signature.KeyInfo; } set { signature.KeyInfo = value; } } +#if (WSE1 || WSE2) + public SignedXmlSignature Signature { + get { return signature; } + } +#else public Signature Signature { get { return signature; } } - +#endif public string SignatureLength { get { return signature.SignedInfo.SignatureLength; } } @@ -92,7 +108,7 @@ namespace System.Security.Cryptography.Xml { set { keyName = value; } } - public void AddObject (DataObject dataObject) + public void AddObject (SSCX.DataObject dataObject) { signature.AddObject (dataObject); } @@ -102,10 +118,10 @@ namespace System.Security.Cryptography.Xml { signature.SignedInfo.AddReference (reference); } - private Stream ApplyTransform (Transform t, XmlDocument doc) + private Stream ApplyTransform (SSCX.Transform t, XmlDocument doc) { t.LoadInput (doc); - if (t is XmlDsigEnvelopedSignatureTransform) { + if (t is SSCX.XmlDsigEnvelopedSignatureTransform) { XmlDocument d = (XmlDocument) t.GetOutput (); MemoryStream ms = new MemoryStream (); d.Save (ms); @@ -115,7 +131,7 @@ namespace System.Security.Cryptography.Xml { return (Stream) t.GetOutput (); } - private Stream ApplyTransform (Transform t, Stream s) + private Stream ApplyTransform (SSCX.Transform t, Stream s) { try { t.LoadInput (s); @@ -135,7 +151,7 @@ namespace System.Security.Cryptography.Xml { if (r.Uri == "") doc = envdoc; else { - foreach (DataObject obj in signature.ObjectList) { + foreach (SSCX.DataObject obj in signature.ObjectList) { if ("#" + obj.Id == r.Uri) { doc.LoadXml (obj.GetXml ().OuterXml); break; @@ -145,7 +161,7 @@ namespace System.Security.Cryptography.Xml { Stream s = null; if (r.TransformChain.Count > 0) { - foreach (Transform t in r.TransformChain) { + foreach (SSCX.Transform t in r.TransformChain) { if (s == null) s = ApplyTransform (t, doc); else @@ -153,7 +169,7 @@ namespace System.Security.Cryptography.Xml { } } else - s = ApplyTransform (new XmlDsigC14NTransform (), doc); + s = ApplyTransform (new SSCX.XmlDsigC14NTransform (), doc); // TODO: We should reuse the same hash object (when possible) HashAlgorithm hash = (HashAlgorithm) CryptoConfig.CreateFromName (r.DigestMethod); @@ -174,7 +190,7 @@ namespace System.Security.Cryptography.Xml { private Stream SignedInfoTransformed () { - Transform t = (Transform) CryptoConfig.CreateFromName (signature.SignedInfo.CanonicalizationMethod); + SSCX.Transform t = (SSCX.Transform) CryptoConfig.CreateFromName (signature.SignedInfo.CanonicalizationMethod); if (t == null) return null; @@ -326,10 +342,10 @@ namespace System.Security.Cryptography.Xml { { AsymmetricAlgorithm key = null; if (signature.KeyInfo != null) { - foreach (KeyInfoClause kic in signature.KeyInfo) { - if (kic is DSAKeyValue) + foreach (SSCX.KeyInfoClause kic in signature.KeyInfo) { + if (kic is SSCX.DSAKeyValue) key = DSA.Create (); - else if (kic is RSAKeyValue) + else if (kic is SSCX.RSAKeyValue) key = RSA.Create (); if (key != null) { diff --git a/mcs/class/Microsoft.Web.Services/Microsoft.Web.Services.Security/SignedXmlSignature.cs b/mcs/class/Microsoft.Web.Services/Microsoft.Web.Services.Security/SignedXmlSignature.cs new file mode 100755 index 00000000000..5eb379a13a8 --- /dev/null +++ b/mcs/class/Microsoft.Web.Services/Microsoft.Web.Services.Security/SignedXmlSignature.cs @@ -0,0 +1,160 @@ +// +// For System.Security.Cryptography.Xml +// Signature.cs - Signature implementation for XML Signature +// For Microsoft.Web.Services.Security +// SignedXmlSignature.cs +// +// Author: +// Sebastien Pouliot (spouliot@motus.com) +// +// (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com) +// + +using System; +using System.Collections; +using System.Security.Cryptography; +using System.Xml; + +#if (WSE1 || WSE2) +using System.Security.Cryptography.Xml; + +namespace Microsoft.Web.Services.Security { + + public class SignedXmlSignature { + + public SignedXmlSignature () +#else +namespace System.Security.Cryptography.Xml { + + public class Signature { + + public Signature () +#endif + { + list = new ArrayList (); + } + + private ArrayList list; + private SignedInfo info; + private KeyInfo key; + private string id; + private byte[] signature; + + 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"); + + XmlDocument document = new XmlDocument (); + XmlElement xel = document.CreateElement (XmlSignature.ElementNames.Signature, XmlSignature.NamespaceURI); + if (id != null) + xel.SetAttribute (XmlSignature.AttributeNames.Id, id); + + XmlNode xn = info.GetXml (); + XmlNode newNode = document.ImportNode (xn, true); + xel.AppendChild (newNode); + + if (signature != null) { + XmlElement sv = document.CreateElement (XmlSignature.ElementNames.SignatureValue, XmlSignature.NamespaceURI); + sv.InnerText = Convert.ToBase64String (signature); + xel.AppendChild (sv); + } + + if (key != null) { + xn = key.GetXml (); + newNode = document.ImportNode (xn, true); + xel.AppendChild (newNode); + } + + if (list.Count > 0) { + foreach (DataObject obj in list) { + xn = obj.GetXml (); + newNode = document.ImportNode (xn, true); + xel.AppendChild (newNode); + } + } + + return xel; + } + + private string GetAttribute (XmlElement xel, string attribute) + { + XmlAttribute xa = xel.Attributes [attribute]; + return ((xa != null) ? xa.InnerText : null); + } + + public void LoadXml (XmlElement value) + { + if (value == null) + throw new ArgumentNullException ("value"); + + if ((value.LocalName == XmlSignature.ElementNames.Signature) && (value.NamespaceURI == XmlSignature.NamespaceURI)) { + id = GetAttribute (value, XmlSignature.AttributeNames.Id); + + XmlNodeList xnl = value.GetElementsByTagName (XmlSignature.ElementNames.SignedInfo); + if ((xnl != null) && (xnl.Count == 1)) { + info = new SignedInfo (); + info.LoadXml ((XmlElement) xnl[0]); + } + + xnl = value.GetElementsByTagName (XmlSignature.ElementNames.SignatureValue); + if ((xnl != null) && (xnl.Count == 1)) { + signature = Convert.FromBase64String (xnl[0].InnerText); + } + + xnl = value.GetElementsByTagName (XmlSignature.ElementNames.KeyInfo); + if ((xnl != null) && (xnl.Count == 1)) { + key = new KeyInfo (); + key.LoadXml ((XmlElement) xnl[0]); + } + + xnl = value.GetElementsByTagName (XmlSignature.ElementNames.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/Microsoft.Web.Services/Microsoft.Web.Services.Security/XmlSignature.cs b/mcs/class/Microsoft.Web.Services/Microsoft.Web.Services.Security/XmlSignature.cs index 6e36698ddf2..7194a504b56 100644 --- a/mcs/class/Microsoft.Web.Services/Microsoft.Web.Services.Security/XmlSignature.cs +++ b/mcs/class/Microsoft.Web.Services/Microsoft.Web.Services.Security/XmlSignature.cs @@ -53,6 +53,8 @@ namespace Microsoft.Web.Services.Security { internal const string Transforms = "Transforms"; internal const string DigestMethod = "DigestMethod"; internal const string DigestValue = "DigestValue"; + internal const string SignatureValue = "SignatureValue"; + internal const string Object = "Object"; public ElementNames () {} } -- 2.25.1