Search in sources :

Example 16 with X500NameBuilder

use of org.spongycastle.asn1.x500.X500NameBuilder in project bnd by bndtools.

the class Server method createSelfSignedCertifcate.

private X509Certificate[] createSelfSignedCertifcate(KeyPair keyPair) throws Exception {
    X500NameBuilder nameBuilder = new X500NameBuilder(BCStyle.INSTANCE);
    nameBuilder.addRDN(BCStyle.CN, "localhost");
    Date notBefore = new Date();
    Date notAfter = new Date(System.currentTimeMillis() + 24 * 3 * 60 * 60 * 1000);
    BigInteger serialNumber = new BigInteger(128, random);
    X509v3CertificateBuilder certificateBuilder = new JcaX509v3CertificateBuilder(nameBuilder.build(), serialNumber, notBefore, notAfter, nameBuilder.build(), keyPair.getPublic());
    ContentSigner contentSigner = new JcaContentSignerBuilder("SHA256WithRSAEncryption").build(keyPair.getPrivate());
    X509Certificate certificate = new JcaX509CertificateConverter().getCertificate(certificateBuilder.build(contentSigner));
    return new X509Certificate[] { certificate };
}
Also used : X500NameBuilder(org.bouncycastle.asn1.x500.X500NameBuilder) JcaX509v3CertificateBuilder(org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder) X509v3CertificateBuilder(org.bouncycastle.cert.X509v3CertificateBuilder) JcaContentSignerBuilder(org.bouncycastle.operator.jcajce.JcaContentSignerBuilder) JcaX509CertificateConverter(org.bouncycastle.cert.jcajce.JcaX509CertificateConverter) JcaX509v3CertificateBuilder(org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder) ContentSigner(org.bouncycastle.operator.ContentSigner) BigInteger(java.math.BigInteger) Date(java.util.Date) X509Certificate(java.security.cert.X509Certificate)

Aggregations

X500NameBuilder (org.bouncycastle.asn1.x500.X500NameBuilder)13 Date (java.util.Date)10 X509Certificate (java.security.cert.X509Certificate)8 X509v3CertificateBuilder (org.bouncycastle.cert.X509v3CertificateBuilder)7 JcaX509CertificateConverter (org.bouncycastle.cert.jcajce.JcaX509CertificateConverter)7 ContentSigner (org.bouncycastle.operator.ContentSigner)7 JcaContentSignerBuilder (org.bouncycastle.operator.jcajce.JcaContentSignerBuilder)7 BigInteger (java.math.BigInteger)6 KeyPair (java.security.KeyPair)6 X500Name (org.bouncycastle.asn1.x500.X500Name)6 JcaX509v3CertificateBuilder (org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder)6 KeyPairGenerator (java.security.KeyPairGenerator)5 PrivateKey (java.security.PrivateKey)5 SecureRandom (java.security.SecureRandom)5 CertificateException (java.security.cert.CertificateException)5 IOException (java.io.IOException)4 PublicKey (java.security.PublicKey)4 GeneralSecurityException (java.security.GeneralSecurityException)3 KeyStore (java.security.KeyStore)3 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)3