X-Git-Url: https://code.wpia.club/?p=gigi.git;a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2FGigiConfig.java;h=f8cf76378aa1f6539e6e18403b95fa77191c08bc;hp=0bf926e0999139571c91bd08f82390482e85a4a4;hb=d690eda36eba121aa79e4f456d5f0eb481be8b86;hpb=634b7f75c8fc2ed8799bad74731278fb59198c48 diff --git a/src/org/cacert/gigi/GigiConfig.java b/src/org/cacert/gigi/GigiConfig.java index 0bf926e0..f8cf7637 100644 --- a/src/org/cacert/gigi/GigiConfig.java +++ b/src/org/cacert/gigi/GigiConfig.java @@ -1,13 +1,16 @@ package org.cacert.gigi; import java.io.ByteArrayInputStream; -import java.io.DataInputStream; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.security.GeneralSecurityException; import java.security.KeyStore; import java.util.Properties; +import org.kamranzafar.jtar.TarEntry; +import org.kamranzafar.jtar.TarInputStream; + public class GigiConfig { public static final String GIGI_CONFIG_VERSION = "GigiConfigV1.0"; byte[] cacerts; @@ -29,20 +32,37 @@ public class GigiConfig { } public static GigiConfig parse(InputStream input) throws IOException { - DataInputStream dis = new DataInputStream(input); - String version = new String(readChunk(dis)); - if (!version.equals(GIGI_CONFIG_VERSION)) { - System.out.println("Invalid config format"); - System.exit(0); - } + TarInputStream tis = new TarInputStream(input); + TarEntry t; GigiConfig gc = new GigiConfig(); - gc.keystorpw = transformSafe(readChunk(dis)); - gc.truststorepw = transformSafe(readChunk(dis)); - gc.mainProps.load(new ByteArrayInputStream(readChunk(dis))); - gc.cacerts = readChunk(dis); - gc.keystore = readChunk(dis); + 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++) { @@ -51,12 +71,7 @@ public class GigiConfig { } return res; } - private static byte[] readChunk(DataInputStream dis) throws IOException { - int length = dis.readInt(); - byte[] contents = new byte[length]; - dis.readFully(contents); - return contents; - } + public KeyStore getPrivateStore() throws GeneralSecurityException, IOException { KeyStore ks1 = KeyStore.getInstance("pkcs12"); @@ -69,4 +84,7 @@ public class GigiConfig { ks1.load(new ByteArrayInputStream(cacerts), truststorepw); return ks1; } + public String getPrivateStorePw() { + return new String(keystorpw); + } }