CertificateUtil { public static PKCS10CertificationRequest generateCsr( KeyPair keyPair, X509Certificate certificate ) throws Exception { PKCS10CertificationRequestBuilder builder = new JcaPKCS10CertificationRequestBuilder( certificate.getSubjectX500Principal(), certificate.getPublicKey() ); GeneralNames subjectAltNames = new GeneralNames( getSubjectAltNames(certificate) .toArray(new GeneralName[0]) ); ExtensionsGenerator extGen = new ExtensionsGenerator(); extGen.addExtension(Extension.subjectAlternativeName, false, subjectAltNames); builder.addAttribute(PKCSObjectIdentifiers.pkcs_9_at_extensionRequest, extGen.generate()); JcaContentSignerBuilder signerBuilder = new JcaContentSignerBuilder( certificate.getSigAlgName() ); ContentSigner signer = signerBuilder.build(keyPair.getPrivate()); return builder.build(signer); }  static X509Certificate decodeCertificate(byte[] certificateBytes); static X509Certificate decodeCertificate(InputStream inputStream); static List<X509Certificate> decodeCertificates(byte[] certificateBytes); static List<X509Certificate> decodeCertificates(InputStream inputStream); static PKCS10CertificationRequest generateCsr(
        KeyPair keyPair,
        X509Certificate certificate
    ); static PKCS10CertificationRequest generateCsr(
        KeyPair keyPair,
        String subjectName,
        String sanUri,
        List<String> sanDnsNames,
        List<String> sanIpAddresses,
        String signatureAlgorithm
    ); static PKCS10CertificationRequest generateCsr(
        KeyPair keyPair,
        X500Name subject,
        String sanUri,
        List<String> sanDnsNames,
        List<String> sanIpAddresses,
        String signatureAlgorithm
    ); static String getCsrPem(PKCS10CertificationRequest csr); static List<Object> getSubjectAltNameField(X509Certificate certificate, int field); static Optional<String> getSanUri(X509Certificate certificate); static List<String> getSanDnsNames(X509Certificate certificate); static List<String> getSanIpAddresses(X509Certificate certificate); static ByteString thumbprint(X509Certificate certificate); static ByteString getCertificateChainBytes(List<X509Certificate> certificateChain); static final int SUBJECT_ALT_NAME_DNS_NAME; static final int SUBJECT_ALT_NAME_IP_ADDRESS; static final int SUBJECT_ALT_NAME_URI; }
@Test public void testGenerateCsr() throws Exception { assertNotNull(CertificateUtil.generateCsr(keyPair, certificate)); }