使用PDFBox ETSI验证的Pades签名

问题描述

我使用PDFBOX创建了PDF PAdES签名,并且正在使用ETSI在线验证器1(需要注册),现在我在报告中仅收到两个错误,但是我对它们的含义有些迷失或我该如何解决。

这是etsi在线验证器报告:

这是我用来签名的代码:

@Override
    public byte[] sign(InputStream content) throws IOException {
        try {
            CMSSignedDataGenerator signGenerator = new CMSSignedDataGenerator();
            X509Certificate userCert = (X509Certificate) this.certificateChain[0];
            ContentSigner mySigner = new CustomSigner(this.signerKeyHandle);
            // TODO check to use cavium as digest provider
            MessageDigest md = MessageDigest.getInstance("SHA-256","Cavium");
            md.update(userCert.getEncoded());
            byte[] userCertHash = md.digest();
            X509CertificateHolder issuerCert = new X509CertificateHolder(this.certificateChain[1].getEncoded());
            // IssuerSerial is = new IssuerSerial(issuerCert.get,// issuerCert.getSerialNumber());
            ESSCertIDv2 certid = new ESSCertIDv2(new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha256),userCertHash);
            ESSCertIDv2[] essCert1Arr = { certid };
            SigningCertificateV2 sigcert = new SigningCertificateV2(certid);
            final DERSet attrValues = new DERSet(sigcert);
            Attribute attr = new Attribute(PKCSObjectIdentifiers.id_aa_signingCertificateV2,attrValues);
            ASN1EncodableVector v = new ASN1EncodableVector();
            v.add(attr);
            AttributeTable atttributeTable = new AttributeTable(v);
             //Create a standard attribute table from the passed in parameters - certhash
             CMSAttributeTableGenerator attrGen = new DefaultSignedAttributeTableGenerator(atttributeTable){
                protected Hashtable createStandardAttributeTable(Map parameters)
                {
                    Hashtable result = super.createStandardAttributeTable(parameters);
                    result.remove(CMSAttributes.signingTime);
                    return result;
                }
            };
            JcaSignerInfoGeneratorBuilder signerBuilder = new JcaSignerInfoGeneratorBuilder( new JcaDigestCalculatorProviderBuilder().build());
            signerBuilder.setSignedAttributeGenerator(attrGen);
            SignerInfoGenerator signerInfoGenerator = signerBuilder.build(mySigner,userCert);
            signGenerator.addSignerInfoGenerator(signerInfoGenerator);
            signGenerator.addCertificates(new JcaCertStore(Arrays.asList(certificateChain)));
            CMSProcessableInputStream msg = new CMSProcessableInputStream(content);
            CMSSignedData signedData = signGenerator.generate(msg,false);
            return signedData.getEncoded();
        } catch (GeneralSecurityException | CMSException | OperatorCreationException e) {
            System.err.println(e.getMessage());
            throw new RuntimeException("unable to sign pdf!");
        }
    }

我不太确定这些问题可能在哪里或为什么产生,一开始我只有5个,现在只剩下这两个,所以任何输入都会受到赞赏

解决方法

原始问题

您使用DefaultSignedAttributeTableGenerator

signerBuilder.setSignedAttributeGenerator(new DefaultSignedAttributeTableGenerator(new AttributeTable(v)));

根据其JavaDocs,

/* Create a standard attribute table from the passed in parameters - this will
 * normally include contentType,signingTime,messageDigest,and CMS algorithm protection.
 * If the constructor using an AttributeTable was used,entries in it for contentType,and
 * messageDigest will override the generated ones.

特别是它将创建一个signingTime签名的属性。

但是对于(非旧有)PAdES签名,嵌入式CMS容器不得包含signingTime属性,有关基线签名,请参见ETSI EN 319 142-1第6.3节

SPO:CMS签名中的签名时间属性...不应存在

并且已经有原始的ETSI TS 102 778-3第4.5.3节用于PAdES-BES和PAdES-EPES签名

不应使用signing-time属性。

(严格地说,当前的ETSI EN 319 142-2 PAdES-E-BES和PAdES-E-EPES配置文件似乎不再禁止使用,它们只是使用推荐代替M 签名字典条目。但是BES / EPES的软件检查通常仍基于确实禁止的旧TS,请参见上文。如今,无论如何应该使用Baseline签名...)

因此,您应该改用CMSAttributeTableGenerator实现,该实现不包含签名时间属性,例如通过复制DefaultSignedAttributeTableGenerator代码并从其createStandardAttributeTable方法中删除签名时间。

更新的问题

在评论中,您说在解决了上述问题之后,仍然存在一个错误:

现在仅是一个错误,其数字63表示位置-{CodeTest}:Contents / CAdESSignature / content / signedData / signerInfos / signerInfo 1 / signedAttrs / attribute [4] / attrValues / NotKnownComponent { {3}}-{ForAllTheChildrenDo}已到达未知组件。因此,TLCC不知道其子级及其处理方式。将不再对此组件做进一步的检查

您的SignerInfo中最后一个(第四个)签名属性是根据1自2011年4月以来的算法标识符保护属性。考虑ETSI签名一致性检查程序的使用期限它可能确实不知道此属性。

如果您希望一致性检查器不显示该错误,只需从DefaultSignedAttributeTableGenerator的标准属性表中删除该属性,就像删除签名时间属性一样。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...