有一个如何进行XAdES-BES包络的例子吗?

时间:2018-06-07 22:02:38

标签: c# xml-signature

我正在尝试为e-Declaration实施XAdES-BES包络签名。不幸的是,签名和摘要值都不正确。

任何示例代码?

void SignEmbedded(string path, X509Certificate2 x509)
{
    // Wczytaj.
    XmlDocument doc = LoadXmlDocument(path);

    // SignedXml object
    XadesSignedXml signedXml = new XadesSignedXml(doc);

    signedXml.Signature.Id = GenerateGUIDWithID();
    signedXml.SigningKey = x509.PrivateKey;
    signedXml.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigCanonicalizationUrl;
    signedXml.SignedInfo.Id = GenerateGUIDWithID();
    signedXml.SignedInfo.SignatureMethod = SignedXml.XmlDsigRSASHA1Url;

    // dodaj referencję na dokument
    Reference reference = new Reference("#Dokument")
    {
        Id = GenerateGUIDWithID()
    };
    signedXml.AddReference(reference);

    // dodaj KeyInfo
    KeyInfo keyInfo = new KeyInfo();
    keyInfo.AddClause(new KeyInfoX509Data(x509));
    signedXml.KeyInfo = keyInfo;

    //
    XadesObject xo = new XadesObject();
    {
        Cert cert = new Cert();

        cert.IssuerSerial.X509IssuerName = x509.IssuerName.Name;
        cert.IssuerSerial.X509SerialNumber = x509.SerialNumber;

        {
            SHA1 cryptoServiceProvider = new SHA1CryptoServiceProvider();
            cert.CertDigest.DigestValue = cryptoServiceProvider.ComputeHash(x509.RawData);
            cert.CertDigest.DigestMethod.Algorithm = SignedXml.XmlDsigSHA1Url;
        }

        xo.QualifyingProperties.Target = "#" + signedXml.Signature.Id;
        xo.QualifyingProperties.SignedProperties.SignedSignatureProperties.SigningTime = DateTime.Now;
        xo.QualifyingProperties.SignedProperties.SignedSignatureProperties.SignaturePolicyIdentifier.SignaturePolicyImplied = true;

        xo.QualifyingProperties.SignedProperties.SignedSignatureProperties.SigningCertificate.CertCollection.Add(cert);

        DataObjectFormat dof = new DataObjectFormat
        {
            ObjectReferenceAttribute = "#Dokument",
            Description = "Dokument w formacie xml [XML]",
            Encoding = SignedXml.XmlDsigBase64TransformUrl, // ...xmldsig/#base64
            MimeType = "text/plain"
        };
        xo.QualifyingProperties.SignedProperties.SignedDataObjectProperties.DataObjectFormatCollection.Add(dof);
    }
    signedXml.AddXadesObject(xo);

    // W dokumentacji 2.9.9.a, Id dla <ds:Object> ma mieć wartość "Dokument", ale nie ma tego w przykładach
    var data = new DataObject("Dokument", "text/xml", "", doc.DocumentElement);
    signedXml.AddObject(data);

    // Podpisz
    signedXml.ComputeSignature();

    File.WriteAllText(path, signedXml.GetXml().OuterXml, Encoding.UTF8);
}

以下是我要签名的示例xml:http://pastebin.pl/view/91f09552

0 个答案:

没有答案