public KeyStore generateServer(String commonName, JKS jks,
                                          Certificate caCert, PrivateKey caPrivKey)
            throws NoSuchAlgorithmException, NoSuchProviderException,
            IOException, OperatorCreationException, CertificateException,
            InvalidKeyException, SignatureException, KeyStoreException {

        KeyPair keyPair = generateKeyPair(SERVER_KEY_SIZE);

        X500Name issuer = new X509CertificateHolder(caCert.getEncoded()).getSubject();
        BigInteger serial = BigInteger.valueOf(randomSerial());
        X500NameBuilder name = new X500NameBuilder(BCStyle.INSTANCE);
        name.addRDN(BCStyle.CN, commonName);
        name.addRDN(BCStyle.O, jks.certOrganisation());
        name.addRDN(BCStyle.OU, jks.certOrganizationalUnitName());
        X500Name subject = name.build();

        X509v3CertificateBuilder builder = new JcaX509v3CertificateBuilder(issuer, serial, NOT_BEFORE,
                new Date(System.currentTimeMillis() + ONE_DAY), subject, keyPair.getPublic());
        builder.addExtension(Extension.subjectKeyIdentifier, false,
                createSubjectKeyIdentifier(keyPair.getPublic()));
        builder.addExtension(Extension.basicConstraints, false,
                new BasicConstraints(false));
        builder.addExtension(Extension.subjectAlternativeName, false,
                new DERSequence(new GeneralName(GeneralName.dNSName, commonName)));

        X509Certificate cert = signCertificate(builder, caPrivKey);

        cert.checkValidity(new Date());
        cert.verify(caCert.getPublicKey());

        KeyStore result = KeyStore.getInstance(KeyStore.getDefaultType());
        result.load(null, null);
        Certificate[] chain = { cert, caCert };
        result.setKeyEntry(jks.alias(), keyPair.getPrivate(), jks.password(), chain);
        return result;
    }
Generate a root keystore by a given {@link JKS}.

@param jks A java keystore object.
@return A root {@link KeyStore}.
netbare-core/src/main/java/com/github/megatronking/netbare/ssl/CertificateGenerator.java