예제 #1
0
        /// <include file='doc\Reference.uex' path='docs/doc[@for="Reference.LoadXml"]/*' />
        public void LoadXml(XmlElement value)
        {
            // Guard against nulls
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

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

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

            // cache the Xml
            m_cachedXml    = value;
            m_originalNode = value;

            m_strId   = value.GetAttribute("Id");
            m_strUri  = value.GetAttribute("URI");
            m_strType = value.GetAttribute("Type");

            // Transforms
            m_transformChain = new TransformChain();

            XmlNodeList transformsNodes = value.SelectNodes("ds:Transforms", nsm);

            if (transformsNodes.Count != 0)
            {
                XmlElement  transformsElement = (XmlElement)transformsNodes.Item(0);
                XmlNodeList transformNodes    = transformsElement.SelectNodes("ds:Transform", nsm);
                if (transformNodes.Count == 0)
                {
                    throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_InvalidElement"), "Transforms");
                }
                for (int i = 0; i < transformNodes.Count; ++i)
                {
                    XmlElement transformElement = (XmlElement)transformNodes.Item(i);
                    String     strAlgorithm     = transformElement.GetAttribute("Algorithm");
                    Transform  transform        = (Transform)CryptoConfig.CreateFromName(strAlgorithm);
                    if (transform == null)
                    {
                        throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_UnknownTransform"));
                    }
                    // Hack! this is done to get around the lack of here() function support in XPath
                    if (transform is XmlDsigEnvelopedSignatureTransform)
                    {
                        // Walk back to the Signature tag. Find the nearest signature ancestor
                        // Signature-->SignedInfo-->Reference-->Transforms-->Transform
                        XmlNode     signatureTag  = transformElement.SelectSingleNode("ancestor::ds:Signature[1]", nsm);
                        XmlNodeList signatureList = transformElement.SelectNodes("//ds:Signature", nsm);
                        if (signatureList != null)
                        {
                            int position = 0;
                            foreach (XmlNode node in signatureList)
                            {
                                position++;
                                if (node == signatureTag)
                                {
                                    ((XmlDsigEnvelopedSignatureTransform)transform).SignaturePosition = position;
                                    break;
                                }
                            }
                        }
                    }
                    // let the transform read the children of the transformElement for data
                    transform.LoadInnerXml(transformElement.ChildNodes);
                    AddTransform(transform);
                }
            }

            // DigestMethod
            XmlNodeList digestMethodNodes = value.SelectNodes("ds:DigestMethod", nsm);

            if (digestMethodNodes.Count == 0)
            {
                throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_InvalidElement"), "Reference/DigestMethod");
            }
            XmlElement digestMethodElement = (XmlElement)digestMethodNodes.Item(0);

            m_strDigestMethod = digestMethodElement.GetAttribute("Algorithm");

            // DigestValue
            XmlNodeList digestValueNodes = value.SelectNodes("ds:DigestValue", nsm);

            if (digestValueNodes.Count == 0)
            {
                throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_InvalidElement"), "Reference/DigestValue");
            }
            XmlElement digestValueElement = (XmlElement)digestValueNodes.Item(0);

            m_rgbDigestValue = Convert.FromBase64String(SignedXml.DiscardWhiteSpaces(digestValueElement.InnerText));
        }
예제 #2
0
        /// <include file='doc\Signature.uex' path='docs/doc[@for="Signature.LoadXml"]/*' />
        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");
            }

            XmlAttributeCollection signatureAttributes = signatureElement.Attributes;
            XmlNode idAttribute = signatureAttributes["Id"];

            if (idAttribute == null)
            {
                m_strId = null;
            }
            //throw new CryptographicException(String.Format(Environment.GetResourceString("Cryptography_XML_MalformedXML"),"Signature"));

            // Look for SignedInfo and SignatureValue. There may optionally be
            // a KeyInfo and some Objects

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

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

            // SignedInfo
            //XmlNodeList signatureChilds = signatureElement.GetElementsByTagName("SignedInfo", SignedXml.XmlDsigNamespaceUrl);
            XmlNodeList signatureChilds = signatureElement.SelectNodes("ds:SignedInfo", nsm);

            if (signatureChilds.Count == 0)
            {
                throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_InvalidElement"), "SignedInfo");
            }
            XmlElement signedInfoElement = (XmlElement)signatureChilds.Item(0);

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

            // SignatureValue
            XmlNodeList signatureValueNodes = signatureElement.SelectNodes("ds:SignatureValue", nsm);

            if (signatureValueNodes.Count == 0)
            {
                throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_InvalidElement"), "SignedInfo/SignatureValue");
            }
            XmlElement signatureValueElement = (XmlElement)signatureValueNodes.Item(0);

            m_rgbSignatureValue = Convert.FromBase64String(SignedXml.DiscardWhiteSpaces(signatureValueElement.InnerText));

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

            if (keyInfoNodes.Count != 0)
            {
                XmlElement keyInfoElement = (XmlElement)keyInfoNodes.Item(0);
                m_keyInfo = new KeyInfo();
                m_keyInfo.LoadXml(keyInfoElement);
            }

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

            for (int i = 0; i < objectNodes.Count; ++i)
            {
                XmlElement objectElement = (XmlElement)objectNodes.Item(i);
                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);
                }
            }
        }
예제 #3
0
        /// <include file='doc\KeyInfo.uex' path='docs/doc[@for="KeyInfoX509Data.LoadXml"]/*' />
        public override void LoadXml(XmlElement element)
        {
            int iNumNodes = 0;

            // Guard against nulls
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }

            XmlNodeList x509IssuerSerialNodes = element.GetElementsByTagName("X509IssuerSerial", SignedXml.XmlDsigNamespaceUrl);
            XmlNodeList x509SKINodes          = element.GetElementsByTagName("X509SKI", SignedXml.XmlDsigNamespaceUrl);
            XmlNodeList x509SubjectNameNodes  = element.GetElementsByTagName("X509SubjectName", SignedXml.XmlDsigNamespaceUrl);
            XmlNodeList x509CertificateNodes  = element.GetElementsByTagName("X509Certificate", SignedXml.XmlDsigNamespaceUrl);
            XmlNodeList x509CRLNodes          = element.GetElementsByTagName("X509CRL", SignedXml.XmlDsigNamespaceUrl);

            iNumNodes += x509IssuerSerialNodes.Count;
            iNumNodes += x509SKINodes.Count;
            iNumNodes += x509SubjectNameNodes.Count;
            iNumNodes += x509CertificateNodes.Count;

            if ((x509CRLNodes.Count != 0 && iNumNodes != 0) || (x509CRLNodes.Count == 0 && iNumNodes == 0))  // Bad X509Data tag, or Empty tag
            {
                throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_InvalidElement"), "X509Data");
            }

            if (x509CRLNodes.Count > 1)
            {
                throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_InvalidElement"), "X509Data");
            }

            // Flush anything in the lists
            Clear();

            if (x509CRLNodes.Count != 0)
            {
                m_CRL = Convert.FromBase64String(SignedXml.DiscardWhiteSpaces(x509CRLNodes.Item(0).InnerText));
                return;
            }

            if (x509IssuerSerialNodes != null)
            {
                foreach (XmlNode node in x509IssuerSerialNodes)
                {
                    XmlNodeList elem = ((XmlNode)node).ChildNodes;
                    if (elem == null || elem.Count < 2)
                    {
                        throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_InvalidElement"), "X509IssuerSerial");
                    }
                    string strIssuerName   = null;
                    string strSerialNumber = null;
                    foreach (XmlNode node1 in elem)
                    {
                        if (node1.Name.Equals("X509IssuerName"))
                        {
                            strIssuerName = node1.InnerText;
                        }
                        if (node1.Name.Equals("X509SerialNumber"))
                        {
                            strSerialNumber = node1.InnerText;
                        }
                    }
                    AddIssuerSerial(strIssuerName, strSerialNumber);
                }
            }

            if (x509SKINodes != null)
            {
                foreach (XmlNode node in x509SKINodes)
                {
                    string strSKI = node.InnerText;
                    AddSubjectKeyId(Convert.FromBase64String(SignedXml.DiscardWhiteSpaces(strSKI)));
                }
            }

            if (x509SubjectNameNodes != null)
            {
                foreach (XmlNode node in x509SubjectNameNodes)
                {
                    AddSubjectName(node.InnerText);
                }
            }

            if (x509CertificateNodes != null)
            {
                foreach (XmlNode node in x509CertificateNodes)
                {
                    AddCertificate(new X509Certificate(Convert.FromBase64String(SignedXml.DiscardWhiteSpaces(node.InnerText))));
                }
            }
        }