Example usage for org.bouncycastle.jce.provider BouncyCastleProvider BouncyCastleProvider

List of usage examples for org.bouncycastle.jce.provider BouncyCastleProvider BouncyCastleProvider

Introduction

In this page you can find the example usage for org.bouncycastle.jce.provider BouncyCastleProvider BouncyCastleProvider.

Prototype

public BouncyCastleProvider() 

Source Link

Document

Construct a new provider.

Usage

From source file:com.github.ambry.commons.TestSSLUtils.java

License:Open Source License

/**
 * Create a self-signed X.509 Certificate.
 * From http://bfo.com/blog/2011/03/08/odds_and_ends_creating_a_new_x_509_certificate.html.
 *
 * @param dn the X.509 Distinguished Name, eg "CN(commonName)=Test, O(organizationName)=Org"
 * @param pair the KeyPair/*from   www.  ja  v  a2 s. com*/
 * @param days how many days from now the Certificate is valid for
 * @param algorithm the signing algorithm, eg "SHA1withRSA"
 * @return the self-signed certificate
 * @throws java.security.cert.CertificateException thrown if a security error or an IO error ocurred.
 */
public static X509Certificate generateCertificate(String dn, KeyPair pair, int days, String algorithm)
        throws CertificateException {
    try {
        Security.addProvider(new BouncyCastleProvider());
        AlgorithmIdentifier sigAlgId = new DefaultSignatureAlgorithmIdentifierFinder().find(algorithm);
        AlgorithmIdentifier digAlgId = new DefaultDigestAlgorithmIdentifierFinder().find(sigAlgId);
        AsymmetricKeyParameter privateKeyAsymKeyParam = PrivateKeyFactory
                .createKey(pair.getPrivate().getEncoded());
        SubjectPublicKeyInfo subPubKeyInfo = SubjectPublicKeyInfo.getInstance(pair.getPublic().getEncoded());
        ContentSigner sigGen = new BcRSAContentSignerBuilder(sigAlgId, digAlgId).build(privateKeyAsymKeyParam);
        X500Name name = new X500Name(dn);
        Date from = new Date();
        Date to = new Date(from.getTime() + days * 86400000L);
        BigInteger sn = new BigInteger(64, new SecureRandom());

        X509v1CertificateBuilder v1CertGen = new X509v1CertificateBuilder(name, sn, from, to, name,
                subPubKeyInfo);
        X509CertificateHolder certificateHolder = v1CertGen.build(sigGen);
        return new JcaX509CertificateConverter().setProvider("BC").getCertificate(certificateHolder);
    } catch (CertificateException ce) {
        throw ce;
    } catch (Exception e) {
        throw new CertificateException(e);
    }
}

From source file:com.github.ambry.router.GCMCryptoService.java

License:Open Source License

GCMCryptoService(CryptoServiceConfig cryptoServiceConfig) {
    config = cryptoServiceConfig;// w  ww.j  av a2s.co m
    ivValSize = cryptoServiceConfig.cryptoServiceIvSizeInBytes;
    Security.addProvider(new BouncyCastleProvider());
    if (!config.cryptoServiceEncryptionDecryptionMode.equals("GCM")) {
        throw new IllegalArgumentException(
                "Unrecognized Encryption Decryption Mode " + config.cryptoServiceEncryptionDecryptionMode);
    }
}

From source file:com.github.spyhunter99.simplejks.CertGenBouncy.java

public static java.security.cert.Certificate selfSign(KeyPair keyPair, String subjectDN)
        throws OperatorCreationException, CertificateException, IOException {
    Provider bcProvider = new BouncyCastleProvider();
    Security.addProvider(bcProvider);

    long now = System.currentTimeMillis();
    Date startDate = new Date(now);

    X500Name dnName = new X500Name(subjectDN);
    BigInteger certSerialNumber = new BigInteger(Long.toString(now)); // <-- Using the current timestamp as the certificate serial number

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(startDate);//from w w  w .ja  v a2  s. c  o  m
    calendar.add(Calendar.YEAR, 30); // <-- 1 Yr validity

    Date endDate = calendar.getTime();

    String signatureAlgorithm = "SHA256WithRSA"; // <-- Use appropriate signature algorithm based on your keyPair algorithm.

    ContentSigner contentSigner = new JcaContentSignerBuilder(signatureAlgorithm).build(keyPair.getPrivate());

    JcaX509v3CertificateBuilder certBuilder = new JcaX509v3CertificateBuilder(dnName, certSerialNumber,
            startDate, endDate, dnName, keyPair.getPublic());

    // Extensions --------------------------
    // Basic Constraints
    BasicConstraints basicConstraints = new BasicConstraints(true); // <-- true for CA, false for EndEntity

    certBuilder.addExtension(new ASN1ObjectIdentifier("2.5.29.19"), true, basicConstraints); // Basic Constraints is usually marked as critical.

    ASN1Encodable[] subjectAlternativeNames = new ASN1Encodable[] {
            new GeneralName(GeneralName.dNSName, "server"),
            new GeneralName(GeneralName.dNSName, "server.mydomain.com") };
    DERSequence subjectAlternativeNamesExtension = new DERSequence(subjectAlternativeNames);
    certBuilder.addExtension(X509Extension.subjectAlternativeName, false, subjectAlternativeNamesExtension);

    // -------------------------------------
    return new JcaX509CertificateConverter().setProvider(bcProvider)
            .getCertificate(certBuilder.build(contentSigner));
}

From source file:com.goodvikings.cryptim.api.Cryptim.java

License:BEER-WARE LICENSE

/**
 * Create a new CryptimAPI instance.//  w  w w  .  j  av a2s.  c om
 */
public Cryptim() {
    Security.insertProviderAt(new BouncyCastleProvider(), 1);

    kr = new KeyRing();
    chats = new TreeMap<>();
}

From source file:com.google.api.auth.TestUtils.java

License:Open Source License

/**
 * Generate a PEM-encoded X509 using the given {@link RsaJsonWebKey}.
 *//*from  www .  j a v a 2 s. com*/
public static String generateX509Cert(RsaJsonWebKey rsaJsonWebKey) {
    try {
        Provider provider = new BouncyCastleProvider();
        String providerName = provider.getName();
        Security.addProvider(provider);

        long currentTimeMillis = System.currentTimeMillis();
        Date start = new Date(currentTimeMillis - TimeUnit.DAYS.toMillis(1));
        Date end = new Date(currentTimeMillis + TimeUnit.DAYS.toMillis(1));
        X509v3CertificateBuilder x509v3CertificateBuilder = new X509v3CertificateBuilder(
                new X500Name("cn=example"), BigInteger.valueOf(currentTimeMillis), start, end,
                new X500Name("cn=example"),
                SubjectPublicKeyInfo.getInstance(rsaJsonWebKey.getPublicKey().getEncoded()));
        ContentSigner contentSigner = new JcaContentSignerBuilder("SHA1WithRSAEncryption")
                .setProvider(providerName).build(rsaJsonWebKey.getPrivateKey());
        X509CertificateHolder x509CertHolder = x509v3CertificateBuilder.build(contentSigner);
        X509Certificate certificate = new JcaX509CertificateConverter().getCertificate(x509CertHolder);
        Security.removeProvider(providerName);

        return String.format("%s%n%s%n%s", DefaultJwksSupplier.X509_CERT_PREFIX,
                new X509Util().toPem(certificate), DefaultJwksSupplier.X509_CERT_SUFFIX);
    } catch (Exception exception) {
        throw new RuntimeException(exception);
    }
}

From source file:com.google.code.commons.checksum.digest.TestDigestUtils.java

License:Apache License

@Before
public void addBouncyCastleProvider() throws Exception {
    Security.addProvider(new BouncyCastleProvider());
}

From source file:com.google.jenkins.plugins.credentials.oauth.JsonServiceAccountConfigTestUtil.java

License:Open Source License

public static PrivateKey generatePrivateKey() throws NoSuchProviderException, NoSuchAlgorithmException {
    Security.addProvider(new BouncyCastleProvider());
    final KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", "BC");
    keyPairGenerator.initialize(1024);//from  w ww.j a  v a 2s .  co  m
    final KeyPair keyPair = keyPairGenerator.generateKeyPair();
    return keyPair.getPrivate();
}

From source file:com.google.jenkins.plugins.credentials.oauth.P12ServiceAccountConfigTestUtil.java

License:Open Source License

public static KeyPair generateKeyPair() throws NoSuchProviderException, NoSuchAlgorithmException {
    Security.addProvider(new BouncyCastleProvider());
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", "BC");
    keyPairGenerator.initialize(1024);/*from  w  ww  .jav  a 2  s  . c  o  m*/
    return keyPairGenerator.generateKeyPair();
}

From source file:com.google.security.wycheproof.BouncyCastleAllTests.java

License:Open Source License

@BeforeClass
public static void setUp() throws Exception {
    TestUtil.installOnlyThisProvider(new BouncyCastleProvider());
}

From source file:com.googlecode.androidannotations.test15.SSLConnectionTest.java

License:Apache License

@BeforeClass
public static void addSecurityProvider() {
    Security.addProvider(new BouncyCastleProvider());
}