use of com.adaptris.security.certificate.CertificateBuilder in project interlok by adaptris.
the class JunitSecurityHelper method getBuilder.
private static CertificateBuilder getBuilder(String commonName) throws Exception {
CertificateBuilder builder = CertificateBuilderFactory.getInstance().createBuilder();
CertificateParameter cp = new CertificateParameter();
X500NameBuilder subject = new X500NameBuilder();
subject.addRDN(X509ObjectIdentifiers.countryName, "GB");
subject.addRDN(X509ObjectIdentifiers.stateOrProvinceName, "Middlesex");
subject.addRDN(X509ObjectIdentifiers.localityName, "Uxbridge");
subject.addRDN(X509ObjectIdentifiers.organization, "Adaptris");
subject.addRDN(X509ObjectIdentifiers.organizationalUnitName, "JUNIT");
subject.addRDN(X509ObjectIdentifiers.commonName, commonName);
subject.addRDN(PKCSObjectIdentifiers.pkcs_9_at_emailAddress, "myname@adaptris.com");
cp.setSignatureAlgorithm("SHA256WithRSAEncryption");
// Changed to 1024 as the key size, otherwise jdk8_66 appears to have a fit
// wrt to java.security limiting the certpath algorithms
// jdk.certpath.disabledAlgorithms=MD2, RSA keySize < 1024 (it was like this in _40, but doesn't
// apparently break things
cp.setKeyAlgorithm("RSA", 1024);
cp.setSubjectInfo(subject.build());
builder.setCertificateParameters(cp);
return builder;
}
use of com.adaptris.security.certificate.CertificateBuilder in project interlok by adaptris.
the class JunitSecurityHelper method newKeystore.
private void newKeystore(String url, String commonName, char[] password) throws Exception {
KeystoreProxy ksp = null;
KeystoreLocation ksc = KeystoreFactory.getDefault().create(url, password);
CertificateBuilder builder = getBuilder(commonName);
Certificate selfCert = builder.createSelfSignedCertificate();
PrivateKey privkey = builder.getPrivateKey();
ksp = KeystoreFactory.getDefault().create(ksc);
try {
ksp.load();
} catch (Exception e) {
// Ignore the error...
}
String alias = config.getProperty(SECURITY_ALIAS);
Certificate[] certChain = new Certificate[1];
certChain[0] = selfCert;
ksp.setPrivateKey(alias, privkey, password, certChain);
ksp.commit();
}
use of com.adaptris.security.certificate.CertificateBuilder in project interlok by adaptris.
the class Config method getBuilder.
public CertificateBuilder getBuilder(String commonName) throws Exception {
CertificateBuilder builder = CertificateBuilderFactory.getInstance().createBuilder();
CertificateParameter cp = new CertificateParameter();
X500NameBuilder subject = new X500NameBuilder();
subject.addRDN(X509ObjectIdentifiers.countryName, config.getProperty(CERTIFICATE_C));
subject.addRDN(X509ObjectIdentifiers.stateOrProvinceName, config.getProperty(CERTIFICATE_ST));
subject.addRDN(X509ObjectIdentifiers.localityName, config.getProperty(CERTIFICATE_L));
subject.addRDN(X509ObjectIdentifiers.organization, config.getProperty(CERTIFICATE_O));
subject.addRDN(X509ObjectIdentifiers.organizationalUnitName, config.getProperty(CERTIFICATE_OU));
subject.addRDN(X509ObjectIdentifiers.commonName, commonName);
subject.addRDN(PKCSObjectIdentifiers.pkcs_9_at_emailAddress, config.getProperty(CERTIFICATE_EMAIL));
cp.setSignatureAlgorithm(config.getProperty(CERTIFICATE_SIGALG));
cp.setKeyAlgorithm(config.getProperty(CERTIFICATE_KEYALG), Integer.parseInt(config.getProperty(CERTIFICATE_KEYSIZE)));
cp.setSubjectInfo(subject.build());
builder.setCertificateParameters(cp);
return builder;
}
use of com.adaptris.security.certificate.CertificateBuilder in project interlok by adaptris.
the class Config method buildKeystore.
public KeystoreLocation buildKeystore(String ksUrl, String cn, boolean overwrite) throws Exception {
String commonName = StringUtils.defaultIfBlank(cn, config.getProperty(KEYSTORE_COMMON_PRIVKEY_ALIAS));
KeystoreLocation ksc = KeystoreFactory.getDefault().create(ksUrl, config.getProperty(Config.KEYSTORE_COMMON_KEYSTORE_PW).toCharArray());
KeystoreProxy ksp = KeystoreFactory.getDefault().create(ksc);
if (ksc.exists() && overwrite == false) {
ksp.load();
}
CertificateBuilder builder = getBuilder(commonName);
Certificate selfCert = builder.createSelfSignedCertificate();
PrivateKey privkey = builder.getPrivateKey();
char[] password = config.getProperty(KEYSTORE_COMMON_PRIVKEY_PW).toCharArray();
Certificate[] certChain = new Certificate[1];
certChain[0] = selfCert;
ksp.setPrivateKey(commonName, privkey, password, certChain);
ksp.commit();
return ksc;
}
use of com.adaptris.security.certificate.CertificateBuilder in project interlok by adaptris.
the class TestCertificateGeneration method testCertificateAndPrivateKeyToKeystore.
@Test
public void testCertificateAndPrivateKeyToKeystore() throws Exception {
String commonName = String.valueOf(random.nextInt(1000));
CertificateBuilder builder = Config.getInstance().getBuilder(commonName);
Certificate selfCert = builder.createSelfSignedCertificate();
PrivateKey privkey = builder.getPrivateKey();
ksp = KeystoreFactory.getDefault().create(ksc);
try {
ksp.load();
} catch (Exception e) {
// Ignore the error...
}
String alias = cfg.getProperty(Config.KEYSTORE_COMMON_PRIVKEY_ALIAS);
char[] password = cfg.getProperty(Config.KEYSTORE_COMMON_PRIVKEY_PW).toCharArray();
Certificate[] certChain = new Certificate[1];
certChain[0] = selfCert;
ksp.setPrivateKey(alias, privkey, password, certChain);
ksp.commit();
}
Aggregations