use of org.bouncycastle.x509.X509V3CertificateGenerator in project nhin-d by DirectProject.
the class PKCS11Commands method createKeyPair.
@Command(name = "CreateKeyPair", usage = CREATE_KEY_PAIR)
public void createKeyPair(String[] args) {
final String alias = StringArrayUtil.getRequiredValue(args, 0);
final String keySize = StringArrayUtil.getOptionalValue(args, 1, "2048");
try {
// create a local keygen for a private key to sign the certificate
final KeyPairGenerator localKeyGen = KeyPairGenerator.getInstance("RSA", "BC");
final KeyPair localKeyPair = localKeyGen.generateKeyPair();
final KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA", mgr.getKS().getProvider().getName());
keyGen.initialize(Integer.parseInt(keySize));
final KeyPair keyPair = keyGen.generateKeyPair();
// create a self signed certificate
X509V3CertificateGenerator v1CertGen = new X509V3CertificateGenerator();
v1CertGen.setPublicKey(keyPair.getPublic());
v1CertGen.setSignatureAlgorithm("SHA256WithRSAEncryption");
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
end.add(Calendar.DAY_OF_MONTH, 3000);
v1CertGen.setSerialNumber(BigInteger.valueOf(generatePositiveRandom()));
v1CertGen.setIssuerDN(new X509Principal("cn=test"));
v1CertGen.setNotBefore(start.getTime());
v1CertGen.setNotAfter(end.getTime());
// issuer and subject are the same for a CA
v1CertGen.setSubjectDN(new X509Principal("cn=test"));
v1CertGen.setPublicKey(keyPair.getPublic());
X509Certificate newCACert = v1CertGen.generate(localKeyPair.getPrivate(), "BC");
mgr.getKS().setKeyEntry(alias, keyPair.getPrivate(), "".toCharArray(), new X509Certificate[] { newCACert });
System.out.println("Key pair created and stored.");
} catch (Exception e) {
e.printStackTrace();
System.err.println("Failed to generate key pair: " + e.getMessage());
}
}
use of org.bouncycastle.x509.X509V3CertificateGenerator in project oxAuth by GluuFederation.
the class ECDSAKeyFactory method generateV3Certificate.
public Certificate generateV3Certificate(Date startDate, Date expirationDate, String dnName) throws CertificateEncodingException, InvalidKeyException, IllegalStateException, NoSuchProviderException, NoSuchAlgorithmException, SignatureException {
// Create certificate
// serial number for certificate
BigInteger serialNumber = new BigInteger(1024, new Random());
X509V3CertificateGenerator certGen = new X509V3CertificateGenerator();
X500Principal principal = new X500Principal(dnName);
certGen.setSerialNumber(serialNumber);
certGen.setIssuerDN(principal);
certGen.setNotBefore(startDate);
certGen.setNotAfter(expirationDate);
// note: same as issuer
certGen.setSubjectDN(principal);
certGen.setPublicKey(keyPair.getPublic());
certGen.setSignatureAlgorithm(signatureAlgorithm.getAlgorithm());
X509Certificate x509Certificate = certGen.generate(keyPair.getPrivate(), "BC");
return new Certificate(signatureAlgorithm, x509Certificate);
}
use of org.bouncycastle.x509.X509V3CertificateGenerator in project android_frameworks_base by ResurrectionRemix.
the class AndroidKeyStoreKeyPairGeneratorSpi method generateSelfSignedCertificateWithValidSignature.
@SuppressWarnings("deprecation")
private X509Certificate generateSelfSignedCertificateWithValidSignature(PrivateKey privateKey, PublicKey publicKey, String signatureAlgorithm) throws Exception {
final X509V3CertificateGenerator certGen = new X509V3CertificateGenerator();
certGen.setPublicKey(publicKey);
certGen.setSerialNumber(mSpec.getCertificateSerialNumber());
certGen.setSubjectDN(mSpec.getCertificateSubject());
certGen.setIssuerDN(mSpec.getCertificateSubject());
certGen.setNotBefore(mSpec.getCertificateNotBefore());
certGen.setNotAfter(mSpec.getCertificateNotAfter());
certGen.setSignatureAlgorithm(signatureAlgorithm);
return certGen.generate(privateKey);
}
use of org.bouncycastle.x509.X509V3CertificateGenerator in project android_frameworks_base by crdroidandroid.
the class AndroidKeyStoreKeyPairGeneratorSpi method generateSelfSignedCertificateWithValidSignature.
@SuppressWarnings("deprecation")
private X509Certificate generateSelfSignedCertificateWithValidSignature(PrivateKey privateKey, PublicKey publicKey, String signatureAlgorithm) throws Exception {
final X509V3CertificateGenerator certGen = new X509V3CertificateGenerator();
certGen.setPublicKey(publicKey);
certGen.setSerialNumber(mSpec.getCertificateSerialNumber());
certGen.setSubjectDN(mSpec.getCertificateSubject());
certGen.setIssuerDN(mSpec.getCertificateSubject());
certGen.setNotBefore(mSpec.getCertificateNotBefore());
certGen.setNotAfter(mSpec.getCertificateNotAfter());
certGen.setSignatureAlgorithm(signatureAlgorithm);
return certGen.generate(privateKey);
}
Aggregations