Search in sources :

Example 6 with X509V1CertificateGenerator

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

the class KeyStoreTestUtil method generateCertificate.

@SuppressWarnings("deprecation")
public static /**
   * 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
   */
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 7 with X509V1CertificateGenerator

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

the class SAMLUtils method generateRandomX509Certificate.

public static X509Certificate generateRandomX509Certificate(KeyPair keyPair) throws NoSuchAlgorithmException, NoSuchProviderException, CertificateEncodingException, SignatureException, InvalidKeyException {
    DateTime now = DateTime.now(DateTimeZone.UTC);
    X500Principal dnName = new X500Principal("CN=ApacheCloudStack");
    X509V1CertificateGenerator certGen = new X509V1CertificateGenerator();
    certGen.setSerialNumber(BigInteger.valueOf(System.currentTimeMillis()));
    certGen.setSubjectDN(dnName);
    certGen.setIssuerDN(dnName);
    certGen.setNotBefore(now.minusDays(1).toDate());
    certGen.setNotAfter(now.plusYears(3).toDate());
    certGen.setPublicKey(keyPair.getPublic());
    certGen.setSignatureAlgorithm("SHA256WithRSAEncryption");
    return certGen.generate(keyPair.getPrivate(), "BC");
}
Also used : X509V1CertificateGenerator(org.bouncycastle.x509.X509V1CertificateGenerator) X500Principal(javax.security.auth.x500.X500Principal) DateTime(org.joda.time.DateTime)

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