Bouncy Castle:从signerInfos中删除算法保护属性

Ara*_*nSG 5 java bouncycastle java-8

我正在升级用 Java 6 编写的系统,使用 Bouncy Castle 1.43,它用于生成 CMS。新系统使用Java 8和Bouncy Castle 1.58。

我已经使用两者生成了 CMS,并成功地使用 openssl 验证了它们,不幸的是,接收 CMS 的第三方拒绝了来自新系统的 CMS。我强烈怀疑其背后的原因是“加密消息语法(CMS)算法保护属性”(OID:1.2.840.113549.1.9.52),它被添加为signerInfos->signedAttrs的一部分:

object: undefined (1.2.840.113549.1.9.52)
value.set:
              SEQUENCE:
    0:d=0  hl=2 l=  30 cons: SEQUENCE          
    2:d=1  hl=2 l=  13 cons:  SEQUENCE          
    4:d=2  hl=2 l=   9 prim:   OBJECT            :sha512
   15:d=2  hl=2 l=   0 prim:   NULL              
   17:d=1  hl=2 l=  13 cons:  cont [ 1 ]        
   19:d=2  hl=2 l=   9 prim:   OBJECT            :sha512WithRSAEncryption
   30:d=2  hl=2 l=   0 prim:   NULL
Run Code Online (Sandbox Code Playgroud)

这是我能看到的新旧 CMS 之间唯一的显着差异。我尝试使用 setSignedAttributeGenerator() 设置属性表,但是即使未设置算法保护,也会添加此属性。有没有办法在不深入 ASN1 并手动执行的情况下删除它?

我的 CMS 是这样创建的:

String signatureAlgorithm = "SHA512withRSA";
CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
ContentSigner shaSigner = new JcaContentSignerBuilder(signatureAlgorithm).build( privateKey );
gen.addSignerInfoGenerator(new
                JcaSignerInfoGeneratorBuilder( new JcaDigestCalculatorProviderBuilder().build())
                .build(shaSigner, certificate)
        );
gen.addCertificate( new X509CertificateHolder( certificate.getEncoded() ));

CMSTypedData processable = new CMSProcessableByteArray(toSign);
CMSSignedData signed = gen.generate(processable, true);
Run Code Online (Sandbox Code Playgroud)

谢谢!

Ara*_*nSG 6

我设法通过重写 DefaultSignedAttributeTableGenerator 中的 getAttributes() 来做到这一点。

    SignerInfoGenerator sigGen = new JcaSignerInfoGeneratorBuilder( new JcaDigestCalculatorProviderBuilder().build())
            .build(shaSigner, certificate);

    final CMSAttributeTableGenerator sAttrGen = sigGen.getSignedAttributeTableGenerator();
    sigGen = new SignerInfoGenerator(sigGen, new
            DefaultSignedAttributeTableGenerator(){
                @Override
                public AttributeTable getAttributes(@SuppressWarnings("rawtypes")
                                                            Map parameters) {
                    AttributeTable ret = sAttrGen.getAttributes(parameters);
                    return ret.remove(CMSAttributes.cmsAlgorithmProtect);
                }
            }, sigGen.getUnsignedAttributeTableGenerator());
    gen.addSignerInfoGenerator(sigGen);
Run Code Online (Sandbox Code Playgroud)