Sign CSR using Bouncy Castle

后端 未结 5 551
我寻月下人不归
我寻月下人不归 2020-11-27 11:04

I cannot find any code/doc describing how to sign a CSR using BC. As input I have a CSR as a byte array and would like to get the cert in PEM and/or DER format.

I ha

5条回答
  •  佛祖请我去吃肉
    2020-11-27 11:38

    Archie thanks!

    I made some changes to you code, see below.

    The main changes are to pass the issuer's name and use the public key from the CSR.

    val caCert = PEMToCert(issuerPEM).get
    val issuer = PrincipalUtil.getIssuerX509Principal(caCert)
    val csr = new PKCS10CertificationRequestHolder(csrData)
    val serial = BigInt(CertSerialNumber.nextSerialNumber)
    val spi = csr.getSubjectPublicKeyInfo();
    
    val certgen = new X509v3CertificateBuilder(
        new X500Name(issuer.getName),
        serial.bigInteger,
        new java.util.Date(),
        new Date(System.currentTimeMillis() + 30 * 365 * 24 * 60 * 60 * 1000),
        csr.getSubject,
        csr.getSubjectPublicKeyInfo())
    
    certgen.addExtension(
        X509Extension.subjectKeyIdentifier,
        false,
        spi
    )
    
    val issuerPK = PEMToPK(issuerPKPEM, caPassword).get
    val contentSigner = new JcaContentSignerBuilder(contentSignerAlg).setProvider(BC).build(issuerPK.getPrivate())
    val x509 = (new JcaX509CertificateConverter).setProvider(BC).getCertificate(certgen.build(contentSigner))
    

提交回复
热议问题