Search in sources :

Example 1 with X509V3CertificateGenerator

use of org.gudy.bouncycastle.jce.X509V3CertificateGenerator in project BiglyBT by BiglySoftware.

the class SESecurityManagerBC method createSelfSignedCertificate.

public static Certificate createSelfSignedCertificate(SESecurityManagerImpl manager, String alias, String cert_dn, int strength) throws Exception {
    KeyPairGenerator kg = KeyPairGenerator.getInstance("RSA");
    kg.initialize(strength, RandomUtils.SECURE_RANDOM);
    KeyPair pair = kg.generateKeyPair();
    X509V3CertificateGenerator certificateGenerator = new X509V3CertificateGenerator();
    certificateGenerator.setSignatureAlgorithm("MD5WithRSAEncryption");
    certificateGenerator.setSerialNumber(new BigInteger("" + SystemTime.getCurrentTime()));
    X509Name issuer_dn = new X509Name(true, cert_dn);
    certificateGenerator.setIssuerDN(issuer_dn);
    X509Name subject_dn = new X509Name(true, cert_dn);
    certificateGenerator.setSubjectDN(subject_dn);
    Calendar not_after = Calendar.getInstance();
    not_after.add(Calendar.YEAR, 1);
    certificateGenerator.setNotAfter(not_after.getTime());
    certificateGenerator.setNotBefore(Calendar.getInstance().getTime());
    certificateGenerator.setPublicKey(pair.getPublic());
    X509Certificate certificate = certificateGenerator.generateX509Certificate(pair.getPrivate());
    java.security.cert.Certificate[] certChain = { (java.security.cert.Certificate) certificate };
    manager.addCertToKeyStore(alias, pair.getPrivate(), certChain);
    return (certificate);
}
Also used : KeyPair(java.security.KeyPair) X509V3CertificateGenerator(org.gudy.bouncycastle.jce.X509V3CertificateGenerator) X509Name(org.gudy.bouncycastle.asn1.x509.X509Name) Calendar(java.util.Calendar) BigInteger(java.math.BigInteger) KeyPairGenerator(java.security.KeyPairGenerator) X509Certificate(java.security.cert.X509Certificate) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Aggregations

BigInteger (java.math.BigInteger)1 KeyPair (java.security.KeyPair)1 KeyPairGenerator (java.security.KeyPairGenerator)1 Certificate (java.security.cert.Certificate)1 X509Certificate (java.security.cert.X509Certificate)1 Calendar (java.util.Calendar)1 X509Name (org.gudy.bouncycastle.asn1.x509.X509Name)1 X509V3CertificateGenerator (org.gudy.bouncycastle.jce.X509V3CertificateGenerator)1