【问题标题】:CAdES Digital SignatureCAdES 数字签名
【发布时间】:2015-01-09 17:08:28
【问题描述】:

我一直在尝试使用葡萄牙公民卡为 PDF 文件实施数字签名 (CAdES),但是我很难找到完美的解决方案。目前我有两套代码。

第一个:

public void signCAdES(...)
{
        String pkcs11Config = "name=GemPC" + "\n" + "library=C:\\WINDOWS\\SysWOW64\\pteidpkcs11.dll";
        ByteArrayInputStream configStream = new ByteArrayInputStream(pkcs11Config.getBytes());
        Provider pkcs11Provider = new sun.security.pkcs11.SunPKCS11(configStream);

                    //provider_name: SunPKCS11-GemPC
        Security.addProvider(pkcs11Provider);

        javax.security.auth.callback.CallbackHandler cmdLineHdlr = new DialogCallbackHandler();

        KeyStore.Builder builder = KeyStore.Builder.newInstance("PKCS11", pkcs11Provider,
                new KeyStore.CallbackHandlerProtection(cmdLineHdlr));
        KeyStore ks= builder.getKeyStore();

        PdfReader reader = new PdfReader(src);
        FileOutputStream os = new FileOutputStream(dest);

        PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0', new File(tempPath), true);
        PdfSignatureAppearance appearance = stamper.getSignatureAppearance();

        appearance.setReason(reason);
        appearance.setLocation(location);
        appearance.setCertificationLevel(level);

        String alias = "CITIZEN SIGNATURE CERTIFICATE";

                    //certificates from electronic card and resources folder
        Certificate[] certs = getSignatureCertificatesChain(ks);

        PrivateKey pk = (PrivateKey) ks.getKey(alias, null);

        ExternalSignature es = new PrivateKeySignature(pk, "SHA-1", pkcs11Provider.getName());
        ExternalDigest digest = new BouncyCastleDigest();

        MakeSignature.signDetached(appearance, digest, es, certs, null, null, null, 0, MakeSignature.CryptoStandard.CADES);
}

第一个有效,但是我有一个验证器来验证 PDF 的签名是否满足标准,并且似乎缺少其中一个属性(签名证书颁发者的序列号)。

第二个不同,我必须手动添加属性,但是生成的 PDF 已损坏(然后我可能还需要添加颁发者序列属性):

private static void signCAdES(byte[] aDocument, PrivateKey aPrivateKey, Certificate[] certChain, String outputPath) {
    try {

        Security.addProvider(new BouncyCastleProvider());
        ArrayList<X509Certificate> certsin = new ArrayList<X509Certificate>();
        for (Certificate certChain1 : certChain) {
            certsin.add((X509Certificate) certChain1);
        }

        X509Certificate signingCertificate= certsin.get(0);

        MessageDigest dig = MessageDigest.getInstance("SHA-1");
        byte[] certHash = dig.digest(signingCertificate.getEncoded());

        ESSCertID essCertid = new ESSCertID(certHash);
        DERSet set = new DERSet(new SigningCertificate(essCertid));

        Attribute certHAttribute = new Attribute(PKCSObjectIdentifiers.id_aa_signingCertificate, set);
        AttributeTable at = getAttributeTableWithSigningCertificateAttribute(certHAttribute);
        CMSAttributeTableGenerator attrGen = new DefaultSignedAttributeTableGenerator(at);

        SignerInfoGeneratorBuilder genBuild = new SignerInfoGeneratorBuilder(new BcDigestCalculatorProvider());
        genBuild.setSignedAttributeGenerator(attrGen);

        CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
        ContentSigner shaSigner = new JcaContentSignerBuilder("SHA1withRSA").build(aPrivateKey);
        SignerInfoGenerator sifGen = genBuild.build(shaSigner, new X509CertificateHolder(signingCertificate.getEncoded()));
        gen.addSignerInfoGenerator(sifGen);
        JcaCertStore jcaCertStore = new JcaCertStore(certsin);
        gen.addCertificates(jcaCertStore);

        CMSTypedData msg = new CMSProcessableByteArray(aDocument);
        CMSSignedData sigData = gen.generate(msg, false); // false=detached

        byte[] encoded = sigData.getEncoded();

        ASN1InputStream in = new ASN1InputStream(encoded);
        CMSSignedData sigData2 = new CMSSignedData(new CMSProcessableByteArray(aDocument), in);
        byte[] encoded2 = sigData2.getEncoded();

        FileOutputStream fos = new FileOutputStream(outputPath);
        fos.write(encoded2);
//      fos.write(encoded);
        fos.flush();
        fos.close();
    } catch (CMSException | IOException | OperatorCreationException | CertificateEncodingException ex) {
        log("signCAdES", "Error: " + ex.toString());
    }
}

有没有人了解使用 Java 的 CAdES 数字签名?任何帮助将不胜感激!

【问题讨论】:

  • 请分享示例文件。此外,您能命名验证者并分享验证报告吗?
  • 验证器的源代码在这里:github.com/arhs/sd-dss (by ARHS Group)。验证报告说第一组代码所做的签名是有效的,但是有一个警告“'issuer-serial'属性不存在或不匹配!”。您所说的示例文档是指签名/签名的 PDF 吗?
  • 您所说的示例文档是指签名/签名的 PDF 吗? - 是的,签名的 PDF。关于警告:它是否指示错误或缺少 issuer-serial 属性的位置?如果它是您包含的证书的属性,则无法在代码中修复它,而只能通过切换到不同的更好的颁发者来修复。
  • 由于安全问题,我无法共享已签名的文档,尽管第二组代码生成的已签名文档已损坏,因此无法对其进行分析。
  • 是否无法将您的签名应用于示例文档以防止安全问题。没有这样的示例,我不知道如何提供帮助。

标签: java pdf digital-signature bouncycastle


【解决方案1】:

“issuer-serial”属性不存在或不匹配!

说明你的cades签名没有签名属性:签名证书的签名引用或者这个引用被篡改了。

请查看:ETSI TS 101 733 V2.2.1 (2013-04) 了解更多信息:

5.7.3 签名证书参考属性

签名证书引用属性通过使用 ESS 签名证书属性或 ESS 签名证书v2 属性...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-18
    • 2022-07-13
    • 1970-01-01
    • 1970-01-01
    • 2013-09-05
    • 2022-06-28
    • 1970-01-01
    • 2011-09-17
    相关资源
    最近更新 更多