Search in sources :

Example 1 with X509V1CertificateGenerator

use of org.bouncycastle.x509.X509V1CertificateGenerator in project OpenAttestation by OpenAttestation.

the class CertificateUtils method generateSelfSignedX509Certificate.

/**
	 * Generate a self signed X509 certificate with Bouncy Castle.
	 * @throws SignatureException 
	 * @throws IllegalStateException 
	 * @throws InvalidKeyException 
	 * @throws CertificateEncodingException 
	 */
public static X509Certificate generateSelfSignedX509Certificate() throws NoSuchAlgorithmException, NoSuchProviderException, CertificateEncodingException, InvalidKeyException, IllegalStateException, SignatureException {
    Security.addProvider(new BouncyCastleProvider());
    int validityDays = 3652;
    // GENERATE THE PUBLIC/PRIVATE RSA KEY PAIR
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", "BC");
    keyPairGenerator.initialize(1024, new SecureRandom());
    KeyPair keyPair = keyPairGenerator.generateKeyPair();
    // GENERATE THE X509 CERTIFICATE
    X509V1CertificateGenerator certGen = new X509V1CertificateGenerator();
    X500Principal dnName = new X500Principal("CN=OATServer");
    certGen.setSerialNumber(BigInteger.valueOf(System.currentTimeMillis()));
    certGen.setSubjectDN(dnName);
    // use the same
    certGen.setIssuerDN(dnName);
    certGen.setNotBefore(new java.sql.Time(System.currentTimeMillis()));
    Calendar expiry = Calendar.getInstance();
    expiry.add(Calendar.DAY_OF_YEAR, validityDays);
    certGen.setNotAfter(expiry.getTime());
    certGen.setPublicKey(keyPair.getPublic());
    certGen.setSignatureAlgorithm("SHA256WithRSAEncryption");
    X509Certificate cert = certGen.generate(keyPair.getPrivate(), "BC");
    return cert;
}
Also used : KeyPair(java.security.KeyPair) X509V1CertificateGenerator(org.bouncycastle.x509.X509V1CertificateGenerator) Calendar(java.util.Calendar) SecureRandom(java.security.SecureRandom) X500Principal(javax.security.auth.x500.X500Principal) KeyPairGenerator(java.security.KeyPairGenerator) X509Certificate(java.security.cert.X509Certificate) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider)

Example 2 with X509V1CertificateGenerator

use of org.bouncycastle.x509.X509V1CertificateGenerator in project symmetric-ds by JumpMind.

the class BouncyCastleSecurityService method generateV1Certificate.

public X509Certificate generateV1Certificate(String host, KeyPair pair) throws Exception {
    host = host == null ? AppUtils.getHostName() : host;
    String certString = String.format("CN=%s, OU=SymmetricDS, O=JumpMind, L=Unknown, ST=Unknown, C=Unknown", host);
    log.info("Installing a default SSL certificate: {}", certString);
    X509V1CertificateGenerator certGen = new X509V1CertificateGenerator();
    certGen.setSerialNumber(BigInteger.valueOf(System.currentTimeMillis()));
    certGen.setIssuerDN(new X500Principal(certString));
    certGen.setNotBefore(new Date(System.currentTimeMillis() - 86400000));
    certGen.setNotAfter(new Date(System.currentTimeMillis() + 788400000000l));
    certGen.setSubjectDN(new X500Principal(certString));
    certGen.setPublicKey(pair.getPublic());
    certGen.setSignatureAlgorithm("SHA256WithRSAEncryption");
    return certGen.generate(pair.getPrivate(), "BC");
}
Also used : X509V1CertificateGenerator(org.bouncycastle.x509.X509V1CertificateGenerator) X500Principal(javax.security.auth.x500.X500Principal) Date(java.util.Date)

Example 3 with X509V1CertificateGenerator

use of org.bouncycastle.x509.X509V1CertificateGenerator in project hbase by apache.

the class KeyStoreTestUtil method generateCertificate.

/**
   * Create a self-signed X.509 Certificate.
   *
   * @param dn the X.509 Distinguished Name, eg "CN=Test, L=London, C=GB"
   * @param pair the KeyPair
   * @param days how many days from now the Certificate is valid for
   * @param algorithm the signing algorithm, eg "SHA1withRSA"
   * @return the self-signed certificate
   */
public static X509Certificate generateCertificate(String dn, KeyPair pair, int days, String algorithm) throws CertificateEncodingException, InvalidKeyException, IllegalStateException, NoSuchProviderException, NoSuchAlgorithmException, SignatureException {
    Date from = new Date();
    Date to = new Date(from.getTime() + days * 86400000l);
    BigInteger sn = new BigInteger(64, new SecureRandom());
    KeyPair keyPair = pair;
    X509V1CertificateGenerator certGen = new X509V1CertificateGenerator();
    X500Principal dnName = new X500Principal(dn);
    certGen.setSerialNumber(sn);
    certGen.setIssuerDN(dnName);
    certGen.setNotBefore(from);
    certGen.setNotAfter(to);
    certGen.setSubjectDN(dnName);
    certGen.setPublicKey(keyPair.getPublic());
    certGen.setSignatureAlgorithm(algorithm);
    X509Certificate cert = certGen.generate(pair.getPrivate());
    return cert;
}
Also used : KeyPair(java.security.KeyPair) X509V1CertificateGenerator(org.bouncycastle.x509.X509V1CertificateGenerator) BigInteger(java.math.BigInteger) SecureRandom(java.security.SecureRandom) X500Principal(javax.security.auth.x500.X500Principal) Date(java.util.Date) X509Certificate(java.security.cert.X509Certificate)

Example 4 with X509V1CertificateGenerator

use of org.bouncycastle.x509.X509V1CertificateGenerator in project gocd by gocd.

the class X509CertificateGenerator method createTypeOneX509Certificate.

private X509Certificate createTypeOneX509Certificate(Date startDate, String principalDn, KeyPair keyPair) {
    X509V1CertificateGenerator certGen = new X509V1CertificateGenerator();
    X500Principal principal = new X500Principal(principalDn);
    certGen.setSerialNumber(serialNumber());
    certGen.setIssuerDN(principal);
    certGen.setNotBefore(startDate);
    DateTime now = new DateTime(new Date());
    certGen.setNotAfter(now.plusYears(YEARS).toDate());
    // note: same as issuer
    certGen.setSubjectDN(principal);
    certGen.setPublicKey(keyPair.getPublic());
    certGen.setSignatureAlgorithm(new SystemEnvironment().get(GO_SSL_CERTS_ALGORITHM));
    try {
        return certGen.generate(keyPair.getPrivate(), "BC");
    } catch (Exception e) {
        throw bomb(e);
    }
}
Also used : SystemEnvironment(com.thoughtworks.go.util.SystemEnvironment) X509V1CertificateGenerator(org.bouncycastle.x509.X509V1CertificateGenerator) X500Principal(javax.security.auth.x500.X500Principal) DateTime(org.joda.time.DateTime) Date(java.util.Date) CertificateParsingException(java.security.cert.CertificateParsingException) UnknownHostException(java.net.UnknownHostException)

Example 5 with X509V1CertificateGenerator

use of org.bouncycastle.x509.X509V1CertificateGenerator in project gocd by gocd.

the class HttpTestUtil method generateCert.

private X509Certificate generateCert(final KeyPair keyPair) {
    Date startDate = day(-1);
    Date expiryDate = day(+1);
    BigInteger serialNumber = new BigInteger("1000200030004000");
    X509V1CertificateGenerator certGen = new X509V1CertificateGenerator();
    X500Principal dnName = new X500Principal("CN=Test CA Certificate");
    certGen.setSerialNumber(serialNumber);
    certGen.setIssuerDN(dnName);
    certGen.setNotBefore(startDate);
    certGen.setNotAfter(expiryDate);
    // note: same as issuer
    certGen.setSubjectDN(dnName);
    certGen.setPublicKey(keyPair.getPublic());
    certGen.setSignatureAlgorithm("SHA1WITHRSA");
    try {
        return certGen.generate(keyPair.getPrivate());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : X509V1CertificateGenerator(org.bouncycastle.x509.X509V1CertificateGenerator) BigInteger(java.math.BigInteger) X500Principal(javax.security.auth.x500.X500Principal) Date(java.util.Date) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Aggregations

X500Principal (javax.security.auth.x500.X500Principal)7 X509V1CertificateGenerator (org.bouncycastle.x509.X509V1CertificateGenerator)7 Date (java.util.Date)5 BigInteger (java.math.BigInteger)3 KeyPair (java.security.KeyPair)3 SecureRandom (java.security.SecureRandom)3 X509Certificate (java.security.cert.X509Certificate)3 DateTime (org.joda.time.DateTime)2 SystemEnvironment (com.thoughtworks.go.util.SystemEnvironment)1 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 KeyPairGenerator (java.security.KeyPairGenerator)1 CertificateParsingException (java.security.cert.CertificateParsingException)1 Calendar (java.util.Calendar)1 ServletException (javax.servlet.ServletException)1 BouncyCastleProvider (org.bouncycastle.jce.provider.BouncyCastleProvider)1