Changing GigiConfig Exchange format to tar.
authorFelix Dörre <felix@dogcraft.de>
Tue, 24 Jun 2014 22:37:36 +0000 (00:37 +0200)
committerFelix Dörre <felix@dogcraft.de>
Tue, 24 Jun 2014 22:37:36 +0000 (00:37 +0200)
src/org/cacert/gigi/DevelLauncher.java
src/org/cacert/gigi/GigiConfig.java
src/org/cacert/gigi/database/DatabaseConnection.java

index d3368c0dd96bcf55dab9e9653a887d181419b508..74a4ae6a515ff95385e41a147c1c120a7fd27d1b 100644 (file)
@@ -7,10 +7,15 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
 import java.nio.file.Files;
 import java.nio.file.Paths;
 import java.util.Properties;
 
+import org.kamranzafar.jtar.TarEntry;
+import org.kamranzafar.jtar.TarHeader;
+import org.kamranzafar.jtar.TarOutputStream;
+
 public class DevelLauncher {
        public static void main(String[] args) throws Exception {
                Properties mainProps = new Properties();
@@ -36,19 +41,29 @@ public class DevelLauncher {
                Launcher.main(args);
                System.setIn(oldin);
        }
-       public static void writeGigiConfig(DataOutputStream target,
-                       byte[] keystorepw, byte[] truststorepw, Properties mainprop,
-                       byte[] cacerts, byte[] keystore) throws IOException {
-               writeChunk(target, GigiConfig.GIGI_CONFIG_VERSION.getBytes());
-               writeChunk(target, keystorepw);
-               writeChunk(target, truststorepw);
-               ByteArrayOutputStream props = new ByteArrayOutputStream();
-               mainprop.store(props, "");
-               writeChunk(target, props.toByteArray());
-               writeChunk(target, cacerts);
-               writeChunk(target, keystore);
+       public static void writeGigiConfig(OutputStream target, byte[] keystorepw,
+                       byte[] truststorepw, Properties mainprop, byte[] cacerts,
+                       byte[] keystore) throws IOException {
+               TarOutputStream tos = new TarOutputStream(target);
+               ByteArrayOutputStream baos = new ByteArrayOutputStream();
+               mainprop.store(baos, "");
+
+               putTarEntry(baos.toByteArray(), tos, "gigi.properties");
+               putTarEntry(keystorepw, tos, "keystorepw");
+               putTarEntry(truststorepw, tos, "truststorepw");
+               putTarEntry(keystore, tos, "keystore.pkcs12");
+               putTarEntry(cacerts, tos, "cacerts.jks");
+               tos.close();
 
        }
+       private static void putTarEntry(byte[] data, TarOutputStream tos,
+                       String name) throws IOException {
+               TarHeader th = new TarHeader();
+               th.name = new StringBuffer(name);
+               th.size = data.length;
+               tos.putNextEntry(new TarEntry(th));
+               tos.write(data);
+       }
        public static void writeChunk(DataOutputStream dos, byte[] chunk)
                        throws IOException {
                dos.writeInt(chunk.length);
index 0bf926e0999139571c91bd08f82390482e85a4a4..69c95bbdea18f54e817f7a2ec2062aa46187f4d2 100644 (file)
@@ -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");
index 424723dd2ef7d4f9026ea9d0c7cffef2896163bd..9f9193a7ad0f2fedafaa45fccb415fd22e33b8d2 100644 (file)
@@ -13,7 +13,7 @@ public class DatabaseConnection {
        public static final int CONNECTION_TIMEOUT = 24 * 60 * 60;
        Connection c;
        HashMap<String, PreparedStatement> statements = new HashMap<String, PreparedStatement>();
-       private static Properties credentials = new Properties();
+       private static Properties credentials;
        Statement adHoc;
        public DatabaseConnection() {
                try {