Example usage for org.bouncycastle.x509 X509V1CertificateGenerator generateX509Certificate

List of usage examples for org.bouncycastle.x509 X509V1CertificateGenerator generateX509Certificate

Introduction

In this page you can find the example usage for org.bouncycastle.x509 X509V1CertificateGenerator generateX509Certificate.

Prototype

public X509Certificate generateX509Certificate(PrivateKey key, String provider)
        throws NoSuchProviderException, SecurityException, SignatureException, InvalidKeyException 

Source Link

Document

generate an X509 certificate, based on the current issuer and subject, using the passed in provider for the signing, and the passed in source of randomness (if required).

Usage

From source file:chapter6.X509V1CreateExample.java

public static X509Certificate generateV1Certificate(KeyPair pair) throws Exception {
    X509V1CertificateGenerator certGen = new X509V1CertificateGenerator();

    certGen.setSerialNumber(BigInteger.valueOf(System.currentTimeMillis()));
    certGen.setIssuerDN(new X500Principal("CN=Test Certificate"));
    certGen.setNotBefore(new Date(System.currentTimeMillis() - 50000));
    certGen.setNotAfter(new Date(System.currentTimeMillis() + 50000));
    certGen.setSubjectDN(new X500Principal("CN=Test Certificate"));
    certGen.setPublicKey(pair.getPublic());
    certGen.setSignatureAlgorithm("SHA256WithRSAEncryption");

    return certGen.generateX509Certificate(pair.getPrivate(), CryptoDefs.Provider.BC.getName());
}

From source file:chapter7.Utils.java

/**
 * Generate a sample V1 certificate to use as a CA root certificate.
 * @param pair//from  www .  j  av  a 2 s .  c  o  m
 * @return
 * @throws Exception
 */
public static X509Certificate generateRootCert(final KeyPair pair) throws Exception {
    X509V1CertificateGenerator certGen = new X509V1CertificateGenerator();

    certGen.setSerialNumber(BigInteger.ONE);
    certGen.setIssuerDN(new X500Principal("CN=Test CA Certificate"));
    certGen.setNotBefore(new Date(System.currentTimeMillis()));
    certGen.setNotAfter(new Date(System.currentTimeMillis() + VALIDITY_PERIOD));
    certGen.setSubjectDN(new X500Principal("CN=Test CA Certificate"));
    certGen.setPublicKey(pair.getPublic());
    certGen.setSignatureAlgorithm("SHA1WithRSAEncryption");

    return certGen.generateX509Certificate(pair.getPrivate(), CryptoDefs.Provider.BC.getName());
}

From source file:com.example.androidtest.SslUtil.java

License:Open Source License

/**
 * Generates a new, self-signed X509 V1 certificate for a KeyPair.
 * /* w  w w  . j a va  2 s .  c om*/
 * @param  pair                      the {@link KeyPair} to be used
 * @param  name                      X.500 distinguished name
 * @return                           the new certificate
 * @throws GeneralSecurityException  on error generating the certificate
 */
@SuppressWarnings("deprecation")
public static X509Certificate generateX509V1Certificate(KeyPair pair, String name)
        throws GeneralSecurityException {
    java.security.Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

    Calendar calendar = Calendar.getInstance();
    calendar.set(2009, 0, 1);
    Date startDate = new Date(calendar.getTimeInMillis());
    calendar.set(2029, 0, 1);
    Date expiryDate = new Date(calendar.getTimeInMillis());

    BigInteger serialNumber = BigInteger.valueOf(Math.abs(System.currentTimeMillis()));

    X509V1CertificateGenerator certGen = new X509V1CertificateGenerator();
    X500Principal dnName = new X500Principal(name);
    certGen.setSerialNumber(serialNumber);
    certGen.setIssuerDN(dnName);
    certGen.setNotBefore(startDate);
    certGen.setNotAfter(expiryDate);
    certGen.setSubjectDN(dnName); // note: same as issuer
    certGen.setPublicKey(pair.getPublic());
    certGen.setSignatureAlgorithm("SHA256WithRSAEncryption");

    // This method is deprecated, but Android Eclair does not provide the 
    // generate() methods.
    X509Certificate cert = certGen.generateX509Certificate(pair.getPrivate(), "BC");
    return cert;
}

From source file:io.aos.crypto.spl06.X509V1CreateExample.java

License:Apache License

public static X509Certificate generateV1Certificate(KeyPair pair)
        throws InvalidKeyException, NoSuchProviderException, SignatureException {
    // generate the certificate
    X509V1CertificateGenerator certGen = new X509V1CertificateGenerator();

    certGen.setSerialNumber(BigInteger.valueOf(System.currentTimeMillis()));
    certGen.setIssuerDN(new X500Principal("CN=Test Certificate"));
    certGen.setNotBefore(new Date(System.currentTimeMillis() - 50000));
    certGen.setNotAfter(new Date(System.currentTimeMillis() + 50000));
    certGen.setSubjectDN(new X500Principal("CN=Test Certificate"));
    certGen.setPublicKey(pair.getPublic());
    certGen.setSignatureAlgorithm("SHA256WithRSAEncryption");

    return certGen.generateX509Certificate(pair.getPrivate(), "BC");
}

From source file:io.aos.crypto.spl07.Utils.java

License:Apache License

/**
 * Generate a sample V1 certificate to use as a CA root certificate
 *//*w ww .j  av  a  2s. c om*/
public static X509Certificate generateRootCert(KeyPair pair) throws Exception {
    X509V1CertificateGenerator certGen = new X509V1CertificateGenerator();

    certGen.setSerialNumber(BigInteger.valueOf(1));
    certGen.setIssuerDN(new X500Principal("CN=Test CA Certificate"));
    certGen.setNotBefore(new Date(System.currentTimeMillis()));
    certGen.setNotAfter(new Date(System.currentTimeMillis() + VALIDITY_PERIOD));
    certGen.setSubjectDN(new X500Principal("CN=Test CA Certificate"));
    certGen.setPublicKey(pair.getPublic());
    certGen.setSignatureAlgorithm("SHA1WithRSAEncryption");

    return certGen.generateX509Certificate(pair.getPrivate(), "BC");
}

From source file:org.apache.synapse.transport.certificatevalidation.Utils.java

License:Apache License

public X509Certificate generateFakeRootCert(KeyPair pair) throws Exception {

    X509V1CertificateGenerator certGen = new X509V1CertificateGenerator();
    certGen.setSerialNumber(BigInteger.valueOf(1));
    certGen.setIssuerDN(new X500Principal("CN=Test CA Certificate"));
    certGen.setNotBefore(new Date(System.currentTimeMillis()));
    certGen.setNotAfter(new Date(System.currentTimeMillis() + TestConstants.VALIDITY_PERIOD));
    certGen.setSubjectDN(new X500Principal("CN=Test CA Certificate"));
    certGen.setPublicKey(pair.getPublic());
    certGen.setSignatureAlgorithm("SHA1WithRSAEncryption");

    return certGen.generateX509Certificate(pair.getPrivate(), "BC");
}

From source file:org.droid2droid.RAApplication.java

License:Apache License

@SuppressWarnings("deprecation")
private static X509Certificate generateX509V1Certificate(KeyPair pair, SecureRandom sr) {
    try {//from w w  w  . ja v a 2s .co m
        String dn = "CN=" + sUuid.toString();

        final Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR, -1);
        final Date startDate = new Date(calendar.getTimeInMillis());
        calendar.add(Calendar.YEAR, 1);
        final Date expiryDate = new Date(calendar.getTimeInMillis());
        final BigInteger serialNumber = BigInteger.valueOf(Math.abs(System.currentTimeMillis()));

        X509V1CertificateGenerator certGen = new X509V1CertificateGenerator();
        X500Principal dnName = new X500Principal(dn);
        certGen.setSerialNumber(serialNumber);
        certGen.setIssuerDN(dnName);
        certGen.setNotBefore(startDate);
        certGen.setNotAfter(expiryDate);
        certGen.setSubjectDN(dnName); // note: same as issuer
        certGen.setPublicKey(pair.getPublic());
        certGen.setSignatureAlgorithm(SIGNATURE_ALGORITHM);

        // FIXME: This method is deprecated, but Android Eclair does not provide the
        // generate() methods.
        if (VERSION.SDK_INT < VERSION_CODES.GINGERBREAD)
            return certGen.generateX509Certificate(pair.getPrivate(), "BC");
        else
            return certGen.generate(pair.getPrivate(), sr);
    } catch (Exception e) {
        throw new Error(e);
    }
}