private static bool VerifySmevRequestSignature(XmlDocument signedSmevRequest) { // Создание подписчика XML-документа var signedXml = new GostSignedXml(signedSmevRequest) { GetIdElementHandler = GetSmevIdElement }; // Поиск узла с подписью var nodeList = signedSmevRequest.GetElementsByTagName("Signature", SignedXml.XmlDsigNamespaceUrl); // Загрузка найденной подписи signedXml.LoadXml((XmlElement)nodeList[0]); // Поиск ссылки на BinarySecurityToken var references = signedXml.KeyInfo.GetXml().GetElementsByTagName("Reference", WsSecurityExtNamespace); if (references.Count > 0) { // Определение ссылки на сертификат (ссылка на узел документа) var binaryTokenReference = ((XmlElement)references[0]).GetAttribute("URI"); if (!String.IsNullOrEmpty(binaryTokenReference) && binaryTokenReference[0] == '#') { // Поиск элемента с закодированным в Base64 сертификатом var binaryTokenElement = signedXml.GetIdElement(signedSmevRequest, binaryTokenReference.Substring(1)); if (binaryTokenElement != null) { // Загрузка сертификата, который был использован для подписи var signingCertificate = new X509Certificate2(Convert.FromBase64String(binaryTokenElement.InnerText)); // Проверка подписи return signedXml.CheckSignature(signingCertificate.GetPublicKeyAlgorithm()); } } } return false; }
public new EncryptedData Encrypt(XmlElement element, X509Certificate2 certificate) { if (element == null) { return base.Encrypt(element, certificate); } if (certificate == null) { return base.Encrypt(element, certificate); } if (!string.Equals(certificate.PublicKey.Oid.Value, GostCryptoConfig.DefaultSignOid, StringComparison.OrdinalIgnoreCase)) { return base.Encrypt(element, certificate); } var encryptedKey = new EncryptedKey { EncryptionMethod = new EncryptionMethod(GostEncryptedXml.XmlEncGostKeyTransportUrl) }; encryptedKey.KeyInfo.AddClause(new KeyInfoX509Data(certificate)); var encriptionKey = new Gost28147SymmetricAlgorithm(); var publicKey = certificate.GetPublicKeyAlgorithm(); encryptedKey.CipherData.CipherValue = EncryptKey(encriptionKey, publicKey as Gost3410AsymmetricAlgorithmBase); var encryptedData = new EncryptedData { Type = XmlEncElementUrl, EncryptionMethod = new EncryptionMethod(GostEncryptedXml.XmlEncGost28147Url) }; encryptedData.KeyInfo.AddClause(new KeyInfoEncryptedKey(encryptedKey)); encryptedData.CipherData.CipherValue = EncryptData(element, encriptionKey, false); return encryptedData; }