1 package org.cacert.gigi;
3 import java.io.ByteArrayInputStream;
4 import java.io.ByteArrayOutputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.security.GeneralSecurityException;
8 import java.security.KeyStore;
9 import java.util.Properties;
11 import org.kamranzafar.jtar.TarEntry;
12 import org.kamranzafar.jtar.TarInputStream;
14 public class GigiConfig {
15 public static final String GIGI_CONFIG_VERSION = "GigiConfigV1.0";
18 Properties mainProps = new Properties();
19 private char[] keystorpw;
20 private char[] truststorepw;
22 private GigiConfig() {
24 public byte[] getCacerts() {
27 public byte[] getKeystore() {
30 public Properties getMainProps() {
34 public static GigiConfig parse(InputStream input) throws IOException {
35 TarInputStream tis = new TarInputStream(input);
37 GigiConfig gc = new GigiConfig();
38 while ((t = tis.getNextEntry()) != null) {
39 if (t.getName().equals("gigi.properties")) {
40 gc.mainProps.load(tis);
41 } else if (t.getName().equals("cacerts.jks")) {
42 gc.cacerts = readFully(tis);
43 } else if (t.getName().equals("keystore.pkcs12")) {
44 gc.keystore = readFully(tis);
45 } else if (t.getName().equals("keystorepw")) {
46 gc.keystorpw = transformSafe(readFully(tis));
47 } else if (t.getName().equals("truststorepw")) {
48 gc.truststorepw = transformSafe(readFully(tis));
50 System.out.println("Unknown config: " + t.getName());
56 public static byte[] readFully(InputStream is) throws IOException {
57 ByteArrayOutputStream baos = new ByteArrayOutputStream();
58 byte[] buffer = new byte[1024];
60 while ((len = is.read(buffer)) > 0) {
61 baos.write(buffer, 0, len);
64 return baos.toByteArray();
66 private static char[] transformSafe(byte[] readChunk) {
67 char[] res = new char[readChunk.length];
68 for (int i = 0; i < res.length; i++) {
69 res[i] = (char) readChunk[i];
75 public KeyStore getPrivateStore() throws GeneralSecurityException,
77 KeyStore ks1 = KeyStore.getInstance("pkcs12");
78 ks1.load(new ByteArrayInputStream(keystore), keystorpw);
81 public KeyStore getTrustStore() throws GeneralSecurityException,
83 KeyStore ks1 = KeyStore.getInstance("jks");
84 ks1.load(new ByteArrayInputStream(cacerts), truststorepw);