X-Git-Url: https://code.wpia.club/?p=gigi.git;a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2FGigiConfig.java;h=8b7c220202f83e8b3b07724fb57824c9fb49f368;hp=69c95bbdea18f54e817f7a2ec2062aa46187f4d2;hb=a4a022f3ef3f697298fca60520d422d8662ec706;hpb=065ca60170f2471227dc25784e1a4c3b7912d367 diff --git a/src/org/cacert/gigi/GigiConfig.java b/src/org/cacert/gigi/GigiConfig.java index 69c95bbd..8b7c2202 100644 --- a/src/org/cacert/gigi/GigiConfig.java +++ b/src/org/cacert/gigi/GigiConfig.java @@ -12,76 +12,92 @@ import org.kamranzafar.jtar.TarEntry; import org.kamranzafar.jtar.TarInputStream; public class GigiConfig { - public static final String GIGI_CONFIG_VERSION = "GigiConfigV1.0"; - byte[] cacerts; - byte[] keystore; - Properties mainProps = new Properties(); - private char[] keystorpw; - private char[] truststorepw; - - private GigiConfig() { - } - public byte[] getCacerts() { - return cacerts; - } - public byte[] getKeystore() { - return keystore; - } - public Properties getMainProps() { - return mainProps; - } - - public static GigiConfig parse(InputStream input) throws IOException { - TarInputStream tis = new TarInputStream(input); - TarEntry t; - GigiConfig gc = new GigiConfig(); - while ((t = tis.getNextEntry()) != null) { - if (t.getName().equals("gigi.properties")) { - gc.mainProps.load(tis); - } else if (t.getName().equals("cacerts.jks")) { - gc.cacerts = readFully(tis); - } else if (t.getName().equals("keystore.pkcs12")) { - gc.keystore = readFully(tis); - } else if (t.getName().equals("keystorepw")) { - gc.keystorpw = transformSafe(readFully(tis)); - } else if (t.getName().equals("truststorepw")) { - gc.truststorepw = transformSafe(readFully(tis)); - } else { - System.out.println("Unknown config: " + t.getName()); - } - } - tis.close(); - return gc; - } - public static byte[] readFully(InputStream is) throws IOException { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - byte[] buffer = new byte[1024]; - int len = 0; - while ((len = is.read(buffer)) > 0) { - baos.write(buffer, 0, len); - } - baos.close(); - return baos.toByteArray(); - } - private static char[] transformSafe(byte[] readChunk) { - char[] res = new char[readChunk.length]; - for (int i = 0; i < res.length; i++) { - res[i] = (char) readChunk[i]; - readChunk[i] = 0; - } - return res; - } - - public KeyStore getPrivateStore() throws GeneralSecurityException, - IOException { - KeyStore ks1 = KeyStore.getInstance("pkcs12"); - ks1.load(new ByteArrayInputStream(keystore), keystorpw); - return ks1; - } - public KeyStore getTrustStore() throws GeneralSecurityException, - IOException { - KeyStore ks1 = KeyStore.getInstance("jks"); - ks1.load(new ByteArrayInputStream(cacerts), truststorepw); - return ks1; - } + + public static final String GIGI_CONFIG_VERSION = "GigiConfigV1.0"; + + private byte[] cacerts; + + private byte[] keystore; + + private Properties mainProps = new Properties(); + + private char[] keystorpw; + + private char[] truststorepw; + + private GigiConfig() {} + + public byte[] getCacerts() { + return cacerts; + } + + public byte[] getKeystore() { + return keystore; + } + + public Properties getMainProps() { + return mainProps; + } + + public static GigiConfig parse(InputStream input) throws IOException { + TarInputStream tis = new TarInputStream(input); + TarEntry t; + GigiConfig gc = new GigiConfig(); + while ((t = tis.getNextEntry()) != null) { + if (t.getName().equals("gigi.properties")) { + gc.mainProps.load(tis); + } else if (t.getName().equals("cacerts.jks")) { + gc.cacerts = readFully(tis); + } else if (t.getName().equals("keystore.pkcs12")) { + gc.keystore = readFully(tis); + } else if (t.getName().equals("keystorepw")) { + gc.keystorpw = transformSafe(readFully(tis)); + } else if (t.getName().equals("truststorepw")) { + gc.truststorepw = transformSafe(readFully(tis)); + } else { + System.out.println("Unknown config: " + t.getName()); + } + } + tis.close(); + return gc; + } + + public static byte[] readFully(InputStream is) throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + byte[] buffer = new byte[1024]; + int len = 0; + while ((len = is.read(buffer)) > 0) { + baos.write(buffer, 0, len); + } + baos.close(); + return baos.toByteArray(); + } + + private static char[] transformSafe(byte[] readChunk) { + char[] res = new char[readChunk.length]; + for (int i = 0; i < res.length; i++) { + res[i] = (char) readChunk[i]; + readChunk[i] = 0; + } + return res; + } + + public KeyStore getPrivateStore() throws GeneralSecurityException, IOException { + if (keystore == null || keystorpw == null) { + return null; + } + KeyStore ks1 = KeyStore.getInstance("pkcs12"); + ks1.load(new ByteArrayInputStream(keystore), keystorpw); + return ks1; + } + + public KeyStore getTrustStore() throws GeneralSecurityException, IOException { + KeyStore ks1 = KeyStore.getInstance("jks"); + ks1.load(new ByteArrayInputStream(cacerts), truststorepw); + return ks1; + } + + public String getPrivateStorePw() { + return new String(keystorpw); + } }