String type = "PKCS12";
char[] password = "password".toCharArray();
String alias = "CA";
if (ks.exists()) {
try {
return new AutoGeneratingContextSelector(ks, type, password,
password, alias);
} catch (GeneralSecurityException e) {
System.err.println("Error loading CA keys from keystore: "
+ e.getLocalizedMessage());
} catch (IOException e) {
System.err.println("Error loading CA keys from keystore: "
+ e.getLocalizedMessage());
}
}
System.err.println("Generating a new CA");
X500Principal ca = new X500Principal("cn=OWASP Custom CA for "
+ java.net.InetAddress.getLocalHost().getHostName()
+ ",ou=OWASP Custom CA,o=OWASP,l=OWASP,st=OWASP,c=OWASP");
AutoGeneratingContextSelector ssl = new AutoGeneratingContextSelector(
ca);
try {
ssl.save(ks, type, password, password, alias);
} catch (GeneralSecurityException e) {
System.err.println("Error saving CA keys to keystore: "
+ e.getLocalizedMessage());
} catch (IOException e) {
System.err.println("Error saving CA keys to keystore: "
+ e.getLocalizedMessage());
}
FileWriter pem = null;
try {
pem = new FileWriter("ca.pem");
pem.write(ssl.getCACert());
} catch (IOException e) {
System.err.println("Error writing CA cert : "
+ e.getLocalizedMessage());
} finally {
if (pem != null)