Пример #1
0
        /// <summary>
        /// Deserialize from the XML representation.
        /// </summary>
        /// <remarks>
        /// Based upon https://www.w3.org/TR/xmldsig-core/#sec-DSAKeyValue.
        /// </remarks>
        /// <param name="value">
        /// An <see cref="XmlElement"/> containing the XML representation. This cannot be null.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="value"/> cannot be null.
        /// </exception>
        /// <exception cref="CryptographicException">
        /// The XML has the incorrect schema or the DSA parameters are invalid.
        /// </exception>
        public override void LoadXml(XmlElement value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (value.Name != KeyValueElementName ||
                value.NamespaceURI != SignedXml.XmlDsigNamespaceUrl)
            {
                throw new System.Security.Cryptography.CryptographicException(String.Format("Root element must be {KeyValueElementName} element in namepsace {SignedXml.XmlDsigNamespaceUrl}"));
            }

            const string        xmlDsigNamespacePrefix = "dsig";
            XmlNamespaceManager xmlNamespaceManager    = new XmlNamespaceManager(value.OwnerDocument.NameTable);

            xmlNamespaceManager.AddNamespace(xmlDsigNamespacePrefix, SignedXml.XmlDsigNamespaceUrl);

            XmlNode dsaKeyValueElement = value.SelectSingleNode(String.Format("{xmlDsigNamespacePrefix}:{DSAKeyValueElementName}"), xmlNamespaceManager);

            if (dsaKeyValueElement == null)
            {
                throw new System.Security.Cryptography.CryptographicException(String.Format("{KeyValueElementName} must contain child element {DSAKeyValueElementName}"));
            }

            XmlNode yNode = dsaKeyValueElement.SelectSingleNode(String.Format("{xmlDsigNamespacePrefix}:{YElementName}"), xmlNamespaceManager);

            if (yNode == null)
            {
                throw new System.Security.Cryptography.CryptographicException(String.Format("{YElementName} is missing"));
            }

            XmlNode pNode = dsaKeyValueElement.SelectSingleNode(String.Format("{xmlDsigNamespacePrefix}:{PElementName}"), xmlNamespaceManager);
            XmlNode qNode = dsaKeyValueElement.SelectSingleNode(String.Format("{xmlDsigNamespacePrefix}:{QElementName}"), xmlNamespaceManager);

            if ((pNode == null && qNode != null) || (pNode != null && qNode == null))
            {
                throw new System.Security.Cryptography.CryptographicException(String.Format("{PElementName} and {QElementName} can only occour in combination"));
            }


            XmlNode gNode = dsaKeyValueElement.SelectSingleNode(String.Format("{xmlDsigNamespacePrefix}:{GElementName}"), xmlNamespaceManager);
            XmlNode jNode = dsaKeyValueElement.SelectSingleNode(String.Format("{xmlDsigNamespacePrefix}:{JElementName}"), xmlNamespaceManager);

            XmlNode seedNode        = dsaKeyValueElement.SelectSingleNode(String.Format("{xmlDsigNamespacePrefix}:{SeedElementName}"), xmlNamespaceManager);
            XmlNode pgenCounterNode = dsaKeyValueElement.SelectSingleNode(String.Format("{xmlDsigNamespacePrefix}:{PgenCounterElementName}"), xmlNamespaceManager);

            if ((seedNode == null && pgenCounterNode != null) || (seedNode != null && pgenCounterNode == null))
            {
                throw new System.Security.Cryptography.CryptographicException(String.Format("{SeedElementName} and {PgenCounterElementName} can only occur in combination"));
            }

            try
            {
                _key = new DsaPublicKeyParameters(new Math.BigInteger(Convert.FromBase64String(yNode.InnerText)),
                                                  new DsaParameters(
                                                      new Math.BigInteger((pNode != null) ? Convert.FromBase64String(pNode.InnerText) : null),
                                                      new Math.BigInteger((qNode != null) ? Convert.FromBase64String(qNode.InnerText) : null),
                                                      new Math.BigInteger((gNode != null) ? Convert.FromBase64String(gNode.InnerText) : null),
                                                      new DsaValidationParameters(
                                                          (seedNode != null) ? Convert.FromBase64String(seedNode.InnerText) : null,
                                                          (pgenCounterNode != null) ? Utils.ConvertByteArrayToInt(Convert.FromBase64String(pgenCounterNode.InnerText)) : 0)));
            }
            catch (Exception ex)
            {
                throw new System.Security.Cryptography.CryptographicException("An error occurred parsing the key components", ex);
            }
        }