示例#1
0
        internal XmlElement GetXml(XmlDocument document)
        {
            // Create the root element
            XmlElement signedInfoElement = document.CreateElement("SignedInfo", SignedXml.XmlDsigNamespaceUrl);

            if (!string.IsNullOrEmpty(_id))
            {
                signedInfoElement.SetAttribute("Id", _id);
            }

            // Add the canonicalization method, defaults to SignedXml.XmlDsigNamespaceUrl
            XmlElement canonicalizationMethodElement = CanonicalizationMethodObject.GetXml(document, "CanonicalizationMethod");

            signedInfoElement.AppendChild(canonicalizationMethodElement);

            // Add the signature method
            if (string.IsNullOrEmpty(_signatureMethod))
            {
                throw new CryptographicException(SR.Cryptography_Xml_SignatureMethodRequired);
            }

            XmlElement signatureMethodElement = document.CreateElement("SignatureMethod", SignedXml.XmlDsigNamespaceUrl);

            signatureMethodElement.SetAttribute("Algorithm", _signatureMethod);
            // Add HMACOutputLength tag if we have one
            if (_signatureLength != null)
            {
                XmlElement hmacLengthElement = document.CreateElement(null, "HMACOutputLength", SignedXml.XmlDsigNamespaceUrl);
                XmlText    outputLength      = document.CreateTextNode(_signatureLength);
                hmacLengthElement.AppendChild(outputLength);
                signatureMethodElement.AppendChild(hmacLengthElement);
            }

            signedInfoElement.AppendChild(signatureMethodElement);

            // Add the references
            if (_references.Count == 0)
            {
                throw new CryptographicException(SR.Cryptography_Xml_ReferenceElementRequired);
            }

            for (int i = 0; i < _references.Count; ++i)
            {
                Reference reference = (Reference)_references[i];
                signedInfoElement.AppendChild(reference.GetXml(document));
            }

            return(signedInfoElement);
        }
示例#2
0
        internal XmlElement GetXml(XmlDocument document)
        {
            XmlElement element = document.CreateElement("SignedInfo", "http://www.w3.org/2000/09/xmldsig#");

            if (!string.IsNullOrEmpty(this.m_id))
            {
                element.SetAttribute("Id", this.m_id);
            }
            XmlElement xml = this.CanonicalizationMethodObject.GetXml(document, "CanonicalizationMethod");

            element.AppendChild(xml);
            if (string.IsNullOrEmpty(this.m_signatureMethod))
            {
                throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_SignatureMethodRequired"));
            }
            XmlElement newChild = document.CreateElement("SignatureMethod", "http://www.w3.org/2000/09/xmldsig#");

            newChild.SetAttribute("Algorithm", this.m_signatureMethod);
            if (this.m_signatureLength != null)
            {
                XmlElement element4 = document.CreateElement(null, "HMACOutputLength", "http://www.w3.org/2000/09/xmldsig#");
                XmlText    text     = document.CreateTextNode(this.m_signatureLength);
                element4.AppendChild(text);
                newChild.AppendChild(element4);
            }
            element.AppendChild(newChild);
            if (this.m_references.Count == 0)
            {
                throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_ReferenceElementRequired"));
            }
            for (int i = 0; i < this.m_references.Count; i++)
            {
                Reference reference = (Reference)this.m_references[i];
                element.AppendChild(reference.GetXml(document));
            }
            return(element);
        }
示例#3
0
		public void KeepDocument ()
		{
			string result = @"<dsig:Reference URI="""" xmlns:dsig=""http://www.w3.org/2000/09/xmldsig#""><dsig:Transforms><dsig:Transform Algorithm=""http://www.w3.org/2000/09/xmldsig#enveloped-signature"" /></dsig:Transforms><dsig:DigestMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#sha1"" /><dsig:DigestValue>nDF2V/bzRd0VE3EwShWtsBzTEDc=</dsig:DigestValue></dsig:Reference>";

			XmlDocument doc = new XmlDocument ();
			doc.LoadXml (xml);
			XmlElement org = (XmlElement) doc.SelectSingleNode ("//*[local-name()='Reference']");
			Reference r = new Reference ();
			r.LoadXml (org);
			XmlElement el = r.GetXml ();
			Assert.AreEqual (doc, el.OwnerDocument);
			Assert.AreEqual (org, el);
			Assert.AreEqual (result, el.OuterXml);
		}
示例#4
0
        /// <include file='doc\SignedInfo.uex' path='docs/doc[@for="SignedInfo.GetXml"]/*' />
        public XmlElement GetXml()
        {
            // If I built this from some Xml that's cached, return it
            // We have to check that the cache is still valid, which means recursively checking that
            // everything cached within up is valid
            if (CacheValid)
            {
                return(m_cachedXml);
            }

            XmlDocument document = new XmlDocument();
            // Create the root element
            XmlElement signedInfoElement = document.CreateElement("SignedInfo", SignedXml.XmlDsigNamespaceUrl);

            // Add the canonicalization method, defaults to
            // SignedXml.XmlDsigW3CCanonicalizationUrl
            XmlElement canonicalizationMethodElement = document.CreateElement("CanonicalizationMethod", SignedXml.XmlDsigNamespaceUrl);

            if (m_strCanonicalizationMethod != null)
            {
                canonicalizationMethodElement.SetAttribute("Algorithm", m_strCanonicalizationMethod);
            }
            else
            {
                canonicalizationMethodElement.SetAttribute("Algorithm", SignedXml.XmlDsigCanonicalizationUrl);
            }
            signedInfoElement.AppendChild(canonicalizationMethodElement);

            // Add the signature method
            if (m_strSignatureMethod == null)
            {
                throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_SignatureMethodRequired"));
            }

            XmlElement signatureMethodElement = document.CreateElement("SignatureMethod", SignedXml.XmlDsigNamespaceUrl);

            signatureMethodElement.SetAttribute("Algorithm", m_strSignatureMethod);
            // Add HMACOutputLength tag if we have one
            if (m_strSignatureLength != null && m_strSignatureMethod == SignedXml.XmlDsigHMACSHA1Url)
            {
                XmlElement hmacLengthElement = document.CreateElement(null, "HMACOutputLength", SignedXml.XmlDsigNamespaceUrl);
                XmlText    outputLength      = document.CreateTextNode(m_strSignatureLength);
                hmacLengthElement.AppendChild(outputLength);
                signatureMethodElement.AppendChild(hmacLengthElement);
            }

            signedInfoElement.AppendChild(signatureMethodElement);

            // Add the references
            if (m_references.Count == 0)
            {
                throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_ReferenceElementRequired"));
            }

            for (int i = 0; i < m_references.Count; ++i)
            {
                Reference reference = (Reference)m_references[i];
                signedInfoElement.AppendChild(document.ImportNode(reference.GetXml(), true));
            }

            return(signedInfoElement);
        }