public void AddTransform(Transform transform) { if (transform == null) { throw new ArgumentNullException(nameof(transform)); } TransformChain.Add(transform); }
public void AddTransform(Transform transform) { if (transform == null) { throw new ArgumentNullException("transform"); } transform.Reference = this; TransformChain.Add(transform); }
public void FullChain () { TransformChain chain = new TransformChain (); 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); }
public void FullChain () { TransformChain chain = new TransformChain (); XmlDsigBase64Transform base64 = new XmlDsigBase64Transform (); chain.Add (base64); Assert.AreEqual (base64, chain[0], "XmlDsigBase64Transform"); Assert.AreEqual (1, chain.Count, "count 1"); XmlDsigC14NTransform c14n = new XmlDsigC14NTransform (); chain.Add (c14n); Assert.AreEqual (c14n, chain[1], "XmlDsigC14NTransform"); Assert.AreEqual (2, chain.Count, "count 2"); XmlDsigC14NWithCommentsTransform c14nc = new XmlDsigC14NWithCommentsTransform (); chain.Add (c14nc); Assert.AreEqual (c14nc, chain[2], "XmlDsigC14NWithCommentsTransform"); Assert.AreEqual (3, chain.Count, "count 3"); XmlDsigEnvelopedSignatureTransform esign = new XmlDsigEnvelopedSignatureTransform (); chain.Add (esign); Assert.AreEqual (esign, chain[3], "XmlDsigEnvelopedSignatureTransform"); Assert.AreEqual (4, chain.Count, "count 4"); XmlDsigXPathTransform xpath = new XmlDsigXPathTransform (); chain.Add (xpath); Assert.AreEqual (xpath, chain[4], "XmlDsigXPathTransform"); Assert.AreEqual (5, chain.Count, "count 5"); XmlDsigXsltTransform xslt = new XmlDsigXsltTransform (); chain.Add (xslt); Assert.AreEqual (xslt, chain[5], "XmlDsigXsltTransform"); Assert.AreEqual (6, chain.Count, "count 6"); }
public void AddTransform(Transform transform) { chain.Add(transform); }
public override void LoadXml (XmlElement value) { if (value == null) throw new ArgumentNullException ("value"); if ((value.LocalName != XmlEncryption.ElementNames.CipherReference) || (value.NamespaceURI != EncryptedXml.XmlEncNamespaceUrl)) throw new CryptographicException ("Malformed CipherReference element."); else { Uri = null; TransformChain = new TransformChain (); foreach (XmlNode n in value.ChildNodes) { if (n is XmlWhitespace) continue; switch (n.LocalName) { case XmlEncryption.ElementNames.Transforms: foreach (XmlNode xn in ((XmlElement) n).GetElementsByTagName (XmlSignature.ElementNames.Transform, XmlSignature.NamespaceURI)) { Transform t = null; switch (((XmlElement) xn).Attributes [XmlSignature.AttributeNames.Algorithm].Value) { case XmlSignature.AlgorithmNamespaces.XmlDsigBase64Transform: t = new XmlDsigBase64Transform (); break; case XmlSignature.AlgorithmNamespaces.XmlDsigC14NTransform: t = new XmlDsigC14NTransform (); break; case XmlSignature.AlgorithmNamespaces.XmlDsigC14NWithCommentsTransform: t = new XmlDsigC14NWithCommentsTransform (); break; case XmlSignature.AlgorithmNamespaces.XmlDsigEnvelopedSignatureTransform: t = new XmlDsigEnvelopedSignatureTransform (); break; case XmlSignature.AlgorithmNamespaces.XmlDsigXPathTransform: t = new XmlDsigXPathTransform (); break; case XmlSignature.AlgorithmNamespaces.XmlDsigXsltTransform: t = new XmlDsigXsltTransform (); break; #if NET_2_0 case XmlSignature.AlgorithmNamespaces.XmlDecryptionTransform: t = new XmlDecryptionTransform (); break; #endif default: continue; } t.LoadInnerXml (((XmlElement) xn).ChildNodes); TransformChain.Add (t); } break; } } if (value.HasAttribute (XmlEncryption.AttributeNames.URI)) Uri = value.Attributes [XmlEncryption.AttributeNames.URI].Value; } }
public virtual void LoadXml(XmlElement value) { if (value == null) { throw new ArgumentNullException("value"); } else { Uri = null; TransformChain = new TransformChain(); foreach (XmlNode n in value.ChildNodes) { if (n is XmlWhitespace) { continue; } switch (n.LocalName) { case XmlEncryption.ElementNames.Transforms: foreach (XmlNode xn in ((XmlElement)n).GetElementsByTagName(XmlSignature.ElementNames.Transform, XmlSignature.NamespaceURI)) { Transform t = null; switch (((XmlElement)xn).Attributes [XmlSignature.AttributeNames.Algorithm].Value) { case XmlSignature.AlgorithmNamespaces.XmlDsigBase64Transform: t = new XmlDsigBase64Transform(); break; case XmlSignature.AlgorithmNamespaces.XmlDsigC14NTransform: t = new XmlDsigC14NTransform(); break; case XmlSignature.AlgorithmNamespaces.XmlDsigC14NWithCommentsTransform: t = new XmlDsigC14NWithCommentsTransform(); break; case XmlSignature.AlgorithmNamespaces.XmlDsigEnvelopedSignatureTransform: t = new XmlDsigEnvelopedSignatureTransform(); break; case XmlSignature.AlgorithmNamespaces.XmlDsigXPathTransform: t = new XmlDsigXPathTransform(); break; case XmlSignature.AlgorithmNamespaces.XmlDsigXsltTransform: t = new XmlDsigXsltTransform(); break; #if NET_2_0 case XmlSignature.AlgorithmNamespaces.XmlDsigExcC14NTransform: t = new XmlDsigExcC14NTransform(); break; case XmlSignature.AlgorithmNamespaces.XmlDsigExcC14NWithCommentsTransform: t = new XmlDsigExcC14NWithCommentsTransform(); break; case XmlSignature.AlgorithmNamespaces.XmlDecryptionTransform: t = new XmlDecryptionTransform(); break; #endif default: continue; } t.LoadInnerXml(((XmlElement)xn).ChildNodes); TransformChain.Add(t); } break; } } if (value.HasAttribute(XmlEncryption.AttributeNames.URI)) { Uri = value.Attributes [XmlEncryption.AttributeNames.URI].Value; } } }
public virtual void LoadXml (XmlElement value) { if (value == null) throw new ArgumentNullException ("value"); else { Uri = null; TransformChain = new TransformChain (); foreach (XmlNode n in value.ChildNodes) { if (n is XmlWhitespace) continue; switch (n.LocalName) { case XmlEncryption.ElementNames.Transforms: foreach (XmlNode xn in ((XmlElement) n).GetElementsByTagName (XmlSignature.ElementNames.Transform, XmlSignature.NamespaceURI)) { Transform t = null; switch (((XmlElement) xn).Attributes [XmlSignature.AttributeNames.Algorithm].Value) { case XmlSignature.AlgorithmNamespaces.XmlDsigBase64Transform: t = new XmlDsigBase64Transform (); break; case XmlSignature.AlgorithmNamespaces.XmlDsigC14NTransform: t = new XmlDsigC14NTransform (); break; case XmlSignature.AlgorithmNamespaces.XmlDsigC14NWithCommentsTransform: t = new XmlDsigC14NWithCommentsTransform (); break; case XmlSignature.AlgorithmNamespaces.XmlDsigEnvelopedSignatureTransform: t = new XmlDsigEnvelopedSignatureTransform (); break; case XmlSignature.AlgorithmNamespaces.XmlDsigXPathTransform: t = new XmlDsigXPathTransform (); break; case XmlSignature.AlgorithmNamespaces.XmlDsigXsltTransform: t = new XmlDsigXsltTransform (); break; case XmlSignature.AlgorithmNamespaces.XmlDsigExcC14NTransform: t = new XmlDsigExcC14NTransform (); break; case XmlSignature.AlgorithmNamespaces.XmlDsigExcC14NWithCommentsTransform: t = new XmlDsigExcC14NWithCommentsTransform (); break; case XmlSignature.AlgorithmNamespaces.XmlDecryptionTransform: t = new XmlDecryptionTransform (); break; default: continue; } t.LoadInnerXml (((XmlElement) xn).ChildNodes); TransformChain.Add (t); } break; } } if (value.HasAttribute (XmlEncryption.AttributeNames.URI)) Uri = value.Attributes [XmlEncryption.AttributeNames.URI].Value; } }
/// <include file='doc\SignedXml.uex' path='docs/doc[@for="SignedXml.ComputeSignature1"]/*' /> public void ComputeSignature(KeyedHashAlgorithm macAlg) { // Do some sanity checks if (macAlg == null) { throw new ArgumentNullException("macAlg"); } if (!(macAlg is HMACSHA1)) { throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_SignatureMethodKeyMismatch")); } int iSignatureLength; if (m_signature.SignedInfo.SignatureLength == null) { iSignatureLength = macAlg.HashSize; } else { iSignatureLength = Convert.ToInt32(m_signature.SignedInfo.SignatureLength); } // iSignatureLength should be less than hash size if (iSignatureLength < 0 || iSignatureLength > macAlg.HashSize) { throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_InvalidSignatureLength")); } if (iSignatureLength % 8 != 0) { throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_InvalidSignatureLength2")); } BuildDigestedReferences(); SignedInfo.SignatureMethod = XmlDsigHMACSHA1Url; // Compute the hash of the SignedInfo object XmlElement signedInfo = SignedInfo.GetXml().Clone() as XmlElement; // Add non default namespaces in scope if (m_namespaces != null) { foreach (XmlNode attrib in m_namespaces) { string name = ((attrib.Prefix != String.Empty) ? attrib.Prefix + ":" + attrib.LocalName : attrib.LocalName); // Skip the attribute if one with the same qualified name already exists if (signedInfo.HasAttribute(name) || (name.Equals("xmlns") && signedInfo.NamespaceURI != String.Empty)) { continue; } XmlAttribute nsattrib = m_containingDocument.CreateAttribute(name); nsattrib.Value = ((XmlNode)attrib).Value; signedInfo.SetAttributeNode(nsattrib); } } #if _DEBUG if (debug) { Console.WriteLine("computed signedInfo: "); Console.WriteLine(signedInfo.OuterXml); } #endif TransformChain tc = new TransformChain(); Transform c14nMethodTransform = (Transform)CryptoConfig.CreateFromName(SignedInfo.CanonicalizationMethod); if (c14nMethodTransform == null) { throw new CryptographicException(String.Format(SecurityResources.GetResourceString("Cryptography_Xml_CreateTransformFailed"), SignedInfo.CanonicalizationMethod)); } tc.Add(c14nMethodTransform); string strBaseUri = (m_containingDocument == null ? null : m_containingDocument.BaseURI); XmlResolver resolver = (m_bResolverSet ? m_xmlResolver : new XmlSecureResolver(new XmlUrlResolver(), strBaseUri)); Stream hashInput = tc.TransformToOctetStream(PreProcessElementInput(signedInfo, resolver, strBaseUri), resolver, strBaseUri); byte[] hashValue = macAlg.ComputeHash(hashInput); m_signature.SignatureValue = new byte[iSignatureLength / 8]; Buffer.BlockCopy(hashValue, 0, m_signature.SignatureValue, 0, iSignatureLength / 8); #if _DEBUG if (debug) { Console.WriteLine("computed hash value: " + Convert.ToBase64String(hashValue)); } #endif }
/// <include file='doc\SignedXml.uex' path='docs/doc[@for="SignedXml.ComputeSignature"]/*' /> public void ComputeSignature() { BuildDigestedReferences(); // Load the key AsymmetricAlgorithm key; if (SigningKey != null) { key = SigningKey; } else { throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_LoadKeyFailed")); } if (key == null) { throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_LoadKeyFailed")); } // Check the signature algorithm associated with the key so that we can accordingly set // the signature method if (key is DSA) { SignedInfo.SignatureMethod = XmlDsigDSAUrl; } else if (key is RSA) { // Default to RSA-SHA1 if (SignedInfo.SignatureMethod == null) { SignedInfo.SignatureMethod = XmlDsigRSASHA1Url; } } else { throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_CreatedKeyFailed")); } // Compute the hash of the SignedInfo object XmlElement signedInfo = SignedInfo.GetXml().Clone() as XmlElement; // Add non default namespaces in scope if (m_namespaces != null) { foreach (XmlNode attrib in m_namespaces) { string name = ((attrib.Prefix != String.Empty) ? attrib.Prefix + ":" + attrib.LocalName : attrib.LocalName); // Skip the attribute if one with the same qualified name already exists if (signedInfo.HasAttribute(name) || (name.Equals("xmlns") && signedInfo.NamespaceURI != String.Empty)) { continue; } XmlAttribute nsattrib = m_containingDocument.CreateAttribute(name); nsattrib.Value = ((XmlNode)attrib).Value; signedInfo.SetAttributeNode(nsattrib); } } #if _DEBUG if (debug) { Console.WriteLine("computed signedInfo: "); Console.WriteLine(signedInfo.OuterXml); } #endif TransformChain tc = new TransformChain(); Transform c14nMethodTransform = (Transform)CryptoConfig.CreateFromName(SignedInfo.CanonicalizationMethod); if (c14nMethodTransform == null) { throw new CryptographicException(String.Format(SecurityResources.GetResourceString("Cryptography_Xml_CreateTransformFailed"), SignedInfo.CanonicalizationMethod)); } tc.Add(c14nMethodTransform); string strBaseUri = (m_containingDocument == null ? null : m_containingDocument.BaseURI); XmlResolver resolver = (m_bResolverSet ? m_xmlResolver : new XmlSecureResolver(new XmlUrlResolver(), strBaseUri)); Stream hashInput = tc.TransformToOctetStream(PreProcessElementInput(signedInfo, resolver, strBaseUri), resolver, strBaseUri); // See if there is a signature description class defined through the Config file SignatureDescription signatureDescription = (SignatureDescription)CryptoConfig.CreateFromName(SignedInfo.SignatureMethod); if (signatureDescription == null) { throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_SignatureDescriptionNotCreated")); } // calculate the hash HashAlgorithm hashAlg = signatureDescription.CreateDigest(); if (hashAlg == null) { throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_CreateHashAlgorithmFailed")); } byte[] hashValue = hashAlg.ComputeHash(hashInput); AsymmetricSignatureFormatter asymmetricSignatureFormatter = signatureDescription.CreateFormatter(key); m_signature.SignatureValue = asymmetricSignatureFormatter.CreateSignature(hashAlg); #if _DEBUG if (debug) { Console.WriteLine("computed hash value: " + Convert.ToBase64String(hashValue)); } #endif }
/// <include file='doc\SignedXml.uex' path='docs/doc[@for="SignedXml.CheckSignature2"]/*' /> public bool CheckSignature(KeyedHashAlgorithm macAlg) { // Do some sanity checks if (macAlg == null) { throw new ArgumentNullException("macAlg"); } int iSignatureLength; if (m_signature.SignedInfo.SignatureLength == null) { iSignatureLength = macAlg.HashSize; } else { iSignatureLength = Convert.ToInt32(m_signature.SignedInfo.SignatureLength); } // iSignatureLength should be less than hash size if (iSignatureLength < 0 || iSignatureLength > macAlg.HashSize) { throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_InvalidSignatureLength")); } if (iSignatureLength % 8 != 0) { throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_InvalidSignatureLength2")); } if (m_signature.SignatureValue == null) { throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_SignatureValueRequired")); } if (m_signature.SignatureValue.Length != iSignatureLength / 8) { throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_InvalidSignatureLength")); } // set up the canonicalizer & canonicalize SignedInfo TransformChain tc = new TransformChain(); Transform c14nMethodTransform = (Transform)CryptoConfig.CreateFromName(SignedInfo.CanonicalizationMethod); if (c14nMethodTransform == null) { throw new CryptographicException(String.Format(SecurityResources.GetResourceString("Cryptography_Xml_CreateTransformFailed"), SignedInfo.CanonicalizationMethod)); } tc.Add(c14nMethodTransform); XmlElement signedInfo = SignedInfo.GetXml().Clone() as XmlElement; // Add non default namespaces in scope if (m_namespaces != null) { foreach (XmlNode attrib in m_namespaces) { string name = ((attrib.Prefix != String.Empty) ? attrib.Prefix + ":" + attrib.LocalName : attrib.LocalName); // Skip the attribute if one with the same qualified name already exists if (signedInfo.HasAttribute(name) || (name.Equals("xmlns") && signedInfo.NamespaceURI != String.Empty)) { continue; } XmlAttribute nsattrib = m_containingDocument.CreateAttribute(name); nsattrib.Value = ((XmlNode)attrib).Value; signedInfo.SetAttributeNode(nsattrib); } } string strBaseUri = (m_containingDocument == null ? null : m_containingDocument.BaseURI); XmlResolver resolver = (m_bResolverSet ? m_xmlResolver : new XmlSecureResolver(new XmlUrlResolver(), strBaseUri)); Stream canonicalizedSignedXml = tc.TransformToOctetStream(PreProcessElementInput(signedInfo, resolver, strBaseUri), resolver, strBaseUri); // Calculate the hash byte[] hashValue = macAlg.ComputeHash(canonicalizedSignedXml); #if _DEBUG if (debug) { Console.WriteLine("Computed canonicalized SignedInfo:"); Console.WriteLine(signedInfo.OuterXml); Console.WriteLine("Computed Hash:"); Console.WriteLine(Convert.ToBase64String(hashValue)); Console.WriteLine("m_signature.SignatureValue:"); Console.WriteLine(Convert.ToBase64String(m_signature.SignatureValue)); } #endif for (int i = 0; i < m_signature.SignatureValue.Length; i++) { if (m_signature.SignatureValue[i] != hashValue[i]) { return(false); } } return(CheckDigestedReferences()); }
/// <include file='doc\SignedXml.uex' path='docs/doc[@for="SignedXml.CheckSignature1"]/*' /> public bool CheckSignature(AsymmetricAlgorithm key) { if (key == null) { throw new ArgumentNullException("key"); } SignatureDescription signatureDescription = (SignatureDescription)CryptoConfig.CreateFromName(m_signature.SignedInfo.SignatureMethod); if (signatureDescription == null) { throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_SignatureDescriptionNotCreated")); } // Let's see if the key corresponds with the SignatureMethod Type ta = Type.GetType(signatureDescription.KeyAlgorithm); Type tb = key.GetType(); if ((ta != tb) && !ta.IsSubclassOf(tb) && !tb.IsSubclassOf(ta)) { // Signature method key mismatch return(false); } // set up the canonicalizer & canonicalize SignedInfo TransformChain tc = new TransformChain(); Transform c14nMethodTransform = (Transform)CryptoConfig.CreateFromName(SignedInfo.CanonicalizationMethod); if (c14nMethodTransform == null) { throw new CryptographicException(String.Format(SecurityResources.GetResourceString("Cryptography_Xml_CreateTransformFailed"), SignedInfo.CanonicalizationMethod)); } tc.Add(c14nMethodTransform); XmlElement signedInfo = SignedInfo.GetXml().Clone() as XmlElement; // Add non default namespaces in scope if (m_namespaces != null) { foreach (XmlNode attrib in m_namespaces) { string name = ((attrib.Prefix != String.Empty) ? attrib.Prefix + ":" + attrib.LocalName : attrib.LocalName); // Skip the attribute if one with the same qualified name already exists if (signedInfo.HasAttribute(name) || (name.Equals("xmlns") && signedInfo.NamespaceURI != String.Empty)) { continue; } XmlAttribute nsattrib = m_containingDocument.CreateAttribute(name); nsattrib.Value = ((XmlNode)attrib).Value; signedInfo.SetAttributeNode(nsattrib); } } string strBaseUri = (m_containingDocument == null ? null : m_containingDocument.BaseURI); XmlResolver resolver = (m_bResolverSet ? m_xmlResolver : new XmlSecureResolver(new XmlUrlResolver(), strBaseUri)); Stream canonicalizedSignedXml = tc.TransformToOctetStream(PreProcessElementInput(signedInfo, resolver, strBaseUri), resolver, strBaseUri); // calculate the hash HashAlgorithm hashAlgorithm = signatureDescription.CreateDigest(); if (hashAlgorithm == null) { throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_CreateHashAlgorithmFailed")); } byte[] hashval = hashAlgorithm.ComputeHash(canonicalizedSignedXml); // We can FINALLY generate the SignatureValue #if _DEBUG if (debug) { Console.WriteLine("Computed canonicalized SignedInfo:"); Console.WriteLine(signedInfo.OuterXml); Console.WriteLine("Computed Hash:"); Console.WriteLine(Convert.ToBase64String(hashval)); Console.WriteLine("m_signature.SignatureValue:"); Console.WriteLine(Convert.ToBase64String(m_signature.SignatureValue)); } #endif AsymmetricSignatureDeformatter asymmetricSignatureDeformatter = signatureDescription.CreateDeformatter(key); bool bRet = asymmetricSignatureDeformatter.VerifySignature(hashAlgorithm, m_signature.SignatureValue); if (bRet != true) { #if _DEBUG if (debug) { Console.WriteLine("Failed to verify the signature on SignedInfo."); } #endif return(false); } // Now is the time to go through all the references and see if their // DigestValue are good return(CheckDigestedReferences()); }
//------------------------- Public Methods -------------------------- /// <include file='doc\Reference.uex' path='docs/doc[@for="Reference.AddTransform"]/*' /> public void AddTransform(Transform transform) { m_transformChain.Add(transform); }