public void LoadXml(XmlElement value) { if (value == null) { throw new ArgumentNullException(nameof(value)); } XmlNamespaceManager nsm = new XmlNamespaceManager(value.OwnerDocument.NameTable); nsm.AddNamespace("enc", XmlNameSpace.Url[NS.XmlEncNamespaceUrl]); XmlNode cipherValueNode = value.SelectSingleNode("enc:CipherValue", nsm); XmlNode cipherReferenceNode = value.SelectSingleNode("enc:CipherReference", nsm); if (cipherValueNode != null) { if (cipherReferenceNode != null) { throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_CipherValueElementRequired); } _cipherValue = Convert.FromBase64String(ParserUtils.DiscardWhiteSpaces(cipherValueNode.InnerText)); } else if (cipherReferenceNode != null) { _cipherReference = new CipherReference(); _cipherReference.LoadXml((XmlElement)cipherReferenceNode); } else { throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_CipherValueElementRequired); } _cachedXml = value; }
private int LoadXml2(XmlElement signatureElement, XmlNamespaceManager nsm, int expectedChildNodes) { XmlNodeList signedInfoNodes = signatureElement.SelectNodes("ds:SignedInfo", nsm); if (signedInfoNodes == null || signedInfoNodes.Count == 0 || signedInfoNodes.Count > 1) { throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidElement, "SignedInfo"); } XmlElement signedInfoElement = signedInfoNodes[0] as XmlElement; expectedChildNodes += signedInfoNodes.Count; XmlNodeList signatureValueNodes = signatureElement.SelectNodes("ds:SignatureValue", nsm); if (signatureValueNodes == null || signatureValueNodes.Count == 0 || signatureValueNodes.Count > 1) { throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidElement, "SignatureValue"); } XmlElement signatureValueElement = signatureValueNodes[0] as XmlElement; expectedChildNodes += signatureValueNodes.Count; _signatureValue = Convert.FromBase64String(ParserUtils.DiscardWhiteSpaces(signatureValueElement.InnerText)); _signatureValueId = ElementUtils.GetAttribute(signatureValueElement, "Id", NS.XmlDsigNamespaceUrl); if (!ElementUtils.VerifyAttributes(signatureValueElement, "Id")) { throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidElement, "SignatureValue"); } SignedInfo = new SignedInfo(); SignedInfo.LoadXml(signedInfoElement); return(expectedChildNodes); }
public void LoadXml(XmlElement value) { if (value == null) { throw new ArgumentNullException(nameof(value)); } XmlNamespaceManager nsm = new XmlNamespaceManager(value.OwnerDocument.NameTable); nsm.AddNamespace("enc", XmlNameSpace.Url[NS.XmlEncNamespaceUrl]); XmlElement encryptionMethodElement = value; var algorithmUrl = ElementUtils.GetAttribute(encryptionMethodElement, "Algorithm", NS.XmlEncNamespaceUrl); _algorithm = XmlNameSpace.Url.FirstOrDefault(x => x.Value == algorithmUrl).Key; XmlNode keySizeNode = value.SelectSingleNode("enc:KeySize", nsm); if (keySizeNode != null) { KeySize = Convert.ToInt32(ParserUtils.DiscardWhiteSpaces(keySizeNode.InnerText), null); } _cachedXml = value; }
public override void LoadXml(XmlElement element) { if (element == null) { throw new ArgumentNullException(nameof(element)); } XmlNamespaceManager nsm = new XmlNamespaceManager(element.OwnerDocument.NameTable); nsm.AddNamespace("ds", XmlNameSpace.Url[NS.XmlDsigNamespaceUrl]); XmlNodeList x509IssuerSerialNodes = element.SelectNodes("ds:X509IssuerSerial", nsm); XmlNodeList x509SKINodes = element.SelectNodes("ds:X509SKI", nsm); XmlNodeList x509SubjectNameNodes = element.SelectNodes("ds:X509SubjectName", nsm); XmlNodeList x509CertificateNodes = element.SelectNodes("ds:X509Certificate", nsm); XmlNodeList x509CRLNodes = element.SelectNodes("ds:X509CRL", nsm); if ((x509CRLNodes.Count == 0 && x509IssuerSerialNodes.Count == 0 && x509SKINodes.Count == 0 && x509SubjectNameNodes.Count == 0 && x509CertificateNodes.Count == 0)) // Bad X509Data tag, or Empty tag { throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidElement, "X509Data"); } Clear(); if (x509CRLNodes.Count != 0) { _CRL = Convert.FromBase64String(ParserUtils.DiscardWhiteSpaces(x509CRLNodes.Item(0).InnerText)); } foreach (XmlNode issuerSerialNode in x509IssuerSerialNodes) { XmlNode x509IssuerNameNode = issuerSerialNode.SelectSingleNode("ds:X509IssuerName", nsm); XmlNode x509SerialNumberNode = issuerSerialNode.SelectSingleNode("ds:X509SerialNumber", nsm); if (x509IssuerNameNode == null || x509SerialNumberNode == null) { throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidElement, "IssuerSerial"); } InternalAddIssuerSerial(x509IssuerNameNode.InnerText.Trim(), x509SerialNumberNode.InnerText.Trim()); } foreach (XmlNode node in x509SKINodes) { AddSubjectKeyId(Convert.FromBase64String(ParserUtils.DiscardWhiteSpaces(node.InnerText))); } foreach (XmlNode node in x509SubjectNameNodes) { AddSubjectName(node.InnerText.Trim()); } var parser = new X509CertificateParser(); foreach (XmlNode node in x509CertificateNodes) { var cert = Convert.FromBase64String(ParserUtils.DiscardWhiteSpaces(node.InnerText)); AddCertificate(parser.ReadCertificate(cert)); } }
private void LoadXml2(XmlElement value, XmlNamespaceManager nsm, bool hasTransforms) { XmlNodeList digestMethodNodes = value.SelectNodes("ds:DigestMethod", nsm); if (digestMethodNodes == null || digestMethodNodes.Count == 0 || digestMethodNodes.Count > 1) { throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidElement, "Reference/DigestMethod"); } XmlElement digestMethodElement = digestMethodNodes[0] as XmlElement; _digestMethod = ElementUtils.GetAttribute(digestMethodElement, "Algorithm", NS.XmlDsigNamespaceUrl); if (_digestMethod == null || !ElementUtils.VerifyAttributes(digestMethodElement, "Algorithm")) { throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidElement, "Reference/DigestMethod"); } XmlNodeList digestValueNodes = value.SelectNodes("ds:DigestValue", nsm); if (digestValueNodes == null || digestValueNodes.Count == 0 || digestValueNodes.Count > 1) { throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidElement, "Reference/DigestValue"); } XmlElement digestValueElement = digestValueNodes[0] as XmlElement; _digestValue = Convert.FromBase64String(ParserUtils.DiscardWhiteSpaces(digestValueElement.InnerText)); if (!ElementUtils.VerifyAttributes(digestValueElement, (string[])null)) { throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidElement, "Reference/DigestValue"); } int expectedChildNodeCount = hasTransforms ? 3 : 2; if (value.SelectNodes("*").Count != expectedChildNodeCount) { throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidElement, "Reference"); } _cachedXml = value; }