private static X509Certificate getSelfCertificate(
      String myname, long validity, String sigAlg, KeyPair keyPair, String provider)
      throws OperatorCreationException, CertificateException {
    final long currentTime = new Date().getTime();
    final Date firstDate = new Date(currentTime - 24 * 60 * 60 * 1000);
    final Date lastDate = new Date(currentTime + validity * 1000);

    // Add all mandatory attributes
    if (LOG.isDebugEnabled()) {
      LOG.debug("keystore signing algorithm " + sigAlg);
    }

    final PublicKey publicKey = keyPair.getPublic();
    if (publicKey == null) {
      throw new IllegalArgumentException("Public key is null");
    }

    X509v3CertificateBuilder cg =
        new JcaX509v3CertificateBuilder(
            new X500Principal(myname),
            BigInteger.valueOf(firstDate.getTime()),
            firstDate,
            lastDate,
            new X500Principal(myname),
            publicKey);
    final JcaContentSignerBuilder contentSignerBuilder = new JcaContentSignerBuilder(sigAlg);
    contentSignerBuilder.setProvider(provider);

    final ContentSigner contentSigner = contentSignerBuilder.build(keyPair.getPrivate());

    return new JcaX509CertificateConverter().getCertificate(cg.build(contentSigner));
  }
Exemple #2
0
 public static JcaPKCS10CertificationRequest generateCertificationRequest(
     String requestedDn, KeyPair keyPair, String signingAlgorithm)
     throws OperatorCreationException {
   JcaPKCS10CertificationRequestBuilder jcaPKCS10CertificationRequestBuilder =
       new JcaPKCS10CertificationRequestBuilder(new X500Name(requestedDn), keyPair.getPublic());
   JcaContentSignerBuilder jcaContentSignerBuilder = new JcaContentSignerBuilder(signingAlgorithm);
   return new JcaPKCS10CertificationRequest(
       jcaPKCS10CertificationRequestBuilder.build(
           jcaContentSignerBuilder.build(keyPair.getPrivate())));
 }