Created
March 24, 2020 14:09
-
-
Save peterkir/59aaab0fc99093e8f238b25a4cdaf387 to your computer and use it in GitHub Desktop.
Java import certificate into keystore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.klib.certificates; | |
import java.io.ByteArrayInputStream; | |
import java.io.DataInputStream; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.security.KeyStore; | |
import java.security.cert.Certificate; | |
import java.security.cert.CertificateFactory; | |
public class ImportCertificateInKeyStore { | |
public static void main(String[] argv) throws Exception { | |
String certFile = ""; | |
String certAlias = ""; | |
String keystoreFileString = ""; | |
String keystorePass = ""; | |
for (int i = 0; i < argv.length; i++) { | |
String[] argArray = argv[i].split("="); | |
String key = argArray[0]; | |
String value = argArray[1]; | |
switch (key) { | |
case "certFile": | |
certFile = value; | |
break; | |
case "certAlias": | |
certAlias = value; | |
break; | |
case "keystoreFile": | |
keystoreFileString = value; | |
break; | |
case "keystorePass": | |
keystorePass = value; | |
break; | |
default: | |
System.out.format("wrong argument specified %s", key); | |
System.exit(1); | |
break; | |
} | |
} | |
if (certFile == "" || certAlias == "" || keystoreFileString == "" || keystorePass == "") { | |
System.out.format( | |
"\nfollowing args are required \ncertFile=%s\ncertAlias=%s\nkeystoreFile=%s\nkeystorePass=%s\n\n", | |
certFile, certAlias, keystoreFileString, keystorePass); | |
System.exit(1); | |
} | |
System.out.format("\nimporting certificate %s with alias %s into keystore %s\n\n", certFile, certAlias, | |
keystoreFileString); | |
FileInputStream is = new FileInputStream(keystoreFileString); | |
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType()); | |
keystore.load(is, keystorePass.toCharArray()); | |
char[] password = keystorePass.toCharArray(); | |
////// | |
CertificateFactory cf = CertificateFactory.getInstance("X.509"); | |
InputStream certstream = fullStream(certFile); | |
Certificate certs = cf.generateCertificate(certstream); | |
/// | |
File keystoreFile = new File(keystoreFileString); | |
// Load the keystore contents | |
FileInputStream in = new FileInputStream(keystoreFile); | |
keystore.load(in, password); | |
in.close(); | |
// Add the certificate | |
keystore.setCertificateEntry(certAlias, certs); | |
// Save the new keystore contents | |
FileOutputStream out = new FileOutputStream(keystoreFile); | |
keystore.store(out, password); | |
out.close(); | |
System.out.format("finished\n"); | |
} | |
private static InputStream fullStream(String fname) throws IOException { | |
FileInputStream fis = new FileInputStream(fname); | |
DataInputStream dis = new DataInputStream(fis); | |
byte[] bytes = new byte[dis.available()]; | |
dis.readFully(bytes); | |
ByteArrayInputStream bais = new ByteArrayInputStream(bytes); | |
return bais; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment