Exemplo n.º 1
0
        public void LoadXml(XmlElement value)
        {
            // Make sure we don't get passed null
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            // Signature
            XmlElement signatureElement = value;

            if (!signatureElement.LocalName.Equals("Signature"))
            {
                throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_InvalidElement"), "Signature");
            }

            // Id attribute -- optional
            m_id = Utils.GetAttribute(signatureElement, "Id", SignedXml.XmlDsigNamespaceUrl);

            XmlNamespaceManager nsm = new XmlNamespaceManager(value.OwnerDocument.NameTable);

            nsm.AddNamespace("ds", SignedXml.XmlDsigNamespaceUrl);

            // SignedInfo
            XmlElement signedInfoElement = signatureElement.SelectSingleNode("ds:SignedInfo", nsm) as XmlElement;

            if (signedInfoElement == null)
            {
                throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_InvalidElement"), "SignedInfo");
            }

            this.SignedInfo = new SignedInfo();
            this.SignedInfo.LoadXml(signedInfoElement);

            // SignatureValue
            XmlElement signatureValueElement = signatureElement.SelectSingleNode("ds:SignatureValue", nsm) as XmlElement;

            if (signatureValueElement == null)
            {
                throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_InvalidElement"), "SignedInfo/SignatureValue");
            }
            m_signatureValue   = Convert.FromBase64String(Utils.DiscardWhiteSpaces(signatureValueElement.InnerText));
            m_signatureValueId = Utils.GetAttribute(signatureValueElement, "Id", SignedXml.XmlDsigNamespaceUrl);

            XmlNodeList keyInfoNodes = signatureElement.SelectNodes("ds:KeyInfo", nsm);

            m_keyInfo = new KeyInfo();
            if (keyInfoNodes != null)
            {
                foreach (XmlNode node in keyInfoNodes)
                {
                    XmlElement keyInfoElement = node as XmlElement;
                    if (keyInfoElement != null)
                    {
                        m_keyInfo.LoadXml(keyInfoElement);
                    }
                }
            }

            XmlNodeList objectNodes = signatureElement.SelectNodes("ds:Object", nsm);

            m_embeddedObjects.Clear();
            if (objectNodes != null)
            {
                foreach (XmlNode node in objectNodes)
                {
                    XmlElement objectElement = node as XmlElement;
                    if (objectElement != null)
                    {
                        DataObject dataObj = new DataObject();
                        dataObj.LoadXml(objectElement);
                        m_embeddedObjects.Add(dataObj);
                    }
                }
            }

            // Select all elements that have Id attributes
            XmlNodeList nodeList = signatureElement.SelectNodes("//*[@Id]", nsm);

            if (nodeList != null)
            {
                foreach (XmlNode node in nodeList)
                {
                    m_referencedItems.Add(node);
                }
            }
        }
Exemplo n.º 2
0
        protected override AsymmetricAlgorithm GetPublicKey()
        {
            if (KeyInfo == null)
            {
                throw ExceptionUtility.CryptographicException(Resources.XmlKeyInfoRequired);
            }

            if (X509Enumumerable != null)
            {
                var nextCertificatePublicKey = GetNextCertificatePublicKey();

                if (nextCertificatePublicKey != null)
                {
                    return(nextCertificatePublicKey);
                }
            }

            if (KeyInfoEnumerable == null)
            {
                KeyInfoEnumerable = KeyInfo.GetEnumerator();
            }

            var keyInfoEnum = KeyInfoEnumerable;

            while (keyInfoEnum.MoveNext())
            {
                var rsaKeyValue = keyInfoEnum.Current as RSAKeyValue;

                if (rsaKeyValue != null)
                {
                    return(rsaKeyValue.Key);
                }

                var dsaKeyValue = keyInfoEnum.Current as DSAKeyValue;

                if (dsaKeyValue != null)
                {
                    return(dsaKeyValue.Key);
                }

                var gostKeyValue = keyInfoEnum.Current as GostKeyValue;

                if (gostKeyValue != null)
                {
                    return(gostKeyValue.Key);
                }

                var keyInfoX509Data = keyInfoEnum.Current as KeyInfoX509Data;

                if (keyInfoX509Data != null)
                {
                    X509Collection = GostXmlUtils.BuildBagOfCertsVerification(keyInfoX509Data);

                    if (X509Collection.Count > 0)
                    {
                        X509Enumumerable = X509Collection.GetEnumerator();

                        var nextCertificatePublicKey = GetNextCertificatePublicKey();

                        if (nextCertificatePublicKey != null)
                        {
                            return(nextCertificatePublicKey);
                        }
                    }
                }
            }

            return(null);
        }