添加属性到证书请求,java + bouncycastle 1.48



我目前正在使用bouncycastle 1.48创建属性证书请求。由于API有一些变化(我是初学者),我无法向创建的请求添加属性我当前的代码是

        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
    keyGen.initialize(512);
    KeyPair rsaKey = keyGen.generateKeyPair();
    PrivateKey privateKey = rsaKey.getPrivate();
    PublicKey publicKey = rsaKey.getPublic();
    System.out.println(privateKey.getEncoded());
    System.out.println(publicKey.getEncoded());
    ContentSigner sigGen = new JcaContentSignerBuilder("SHA1withRSA").build(privateKey);
    AlgorithmIdentifier rsaEncryption = new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, null); 
    SubjectPublicKeyInfo publicKeyInfo = new SubjectPublicKeyInfo(rsaEncryption, publicKey.getEncoded());
    Date startDate = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000);
    Date endDate = new Date(System.currentTimeMillis() + 365 * 24 * 60 * 60 * 1000);
    X500NameBuilder nameBuilder = new X500NameBuilder();
    nameBuilder.addRDN(BCStyle.CN, "test request");
    nameBuilder.addRDN(BCStyle.C, "UK");
    nameBuilder.addRDN(BCStyle.E,"qwerasd@gmail.com");
    nameBuilder.addRDN(BCStyle.GENDER,"M");
    X500Name name = nameBuilder.build();
    PKCS10CertificationRequestBuilder genReq = new PKCS10CertificationRequestBuilder(name,publicKeyInfo);
    PKCS10CertificationRequest request = genReq.build(sigGen);
    PEMWriter pemWriter = new PEMWriter(new FileWriter(new File("C:\certs\request.txt")));
    pemWriter.writeObject(request);
    pemWriter.flush();      

我的问题是-如何正确的语法看起来像addAttribute方法?提前感谢

这取决于您想要添加什么。最重要的是要记住证书请求上的属性和证书中的扩展名不是一回事。一般来说,人们试图添加一个或多个扩展,但在这种情况下,您需要使用适当的pkcs# 9属性来表示这一点,而不是与扩展相关联的OID。

比如说,您想从CA请求一个特定的KeyUsage扩展名,您可以这样写:

ExtensionsGenerator extGen = new ExtensionsGenerator();
extGen.addExtension(Extension.keyUsage, true, new KeyUsage(KeyUsage.keyCertSign | KeyUsage.cRLSign));
genReq.addAttribute(PKCSObjectIdentifiers.pkcs_9_at_extensionRequest, extGen.generate());

CA应该假定extensionRequest块包含您想要的扩展。

相关内容

  • 没有找到相关文章

最新更新