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 {
16 public static final String GIGI_CONFIG_VERSION = "GigiConfigV1.0";
18 private byte[] cacerts;
20 private byte[] keystore;
22 private Properties mainProps = new Properties();
24 private char[] keystorpw;
26 private char[] truststorepw;
28 private GigiConfig() {}
30 public byte[] getCacerts() {
34 public byte[] getKeystore() {
38 public Properties getMainProps() {
42 public static GigiConfig parse(InputStream input) throws IOException {
43 TarInputStream tis = new TarInputStream(input);
45 GigiConfig gc = new GigiConfig();
46 while ((t = tis.getNextEntry()) != null) {
47 if (t.getName().equals("gigi.properties")) {
48 gc.mainProps.load(tis);
49 } else if (t.getName().equals("cacerts.jks")) {
50 gc.cacerts = readFully(tis);
51 } else if (t.getName().equals("keystore.pkcs12")) {
52 gc.keystore = readFully(tis);
53 } else if (t.getName().equals("keystorepw")) {
54 gc.keystorpw = transformSafe(readFully(tis));
55 } else if (t.getName().equals("truststorepw")) {
56 gc.truststorepw = transformSafe(readFully(tis));
58 System.out.println("Unknown config: " + t.getName());
65 public static byte[] readFully(InputStream is) throws IOException {
66 ByteArrayOutputStream baos = new ByteArrayOutputStream();
67 byte[] buffer = new byte[1024];
69 while ((len = is.read(buffer)) > 0) {
70 baos.write(buffer, 0, len);
73 return baos.toByteArray();
76 private static char[] transformSafe(byte[] readChunk) {
77 char[] res = new char[readChunk.length];
78 for (int i = 0; i < res.length; i++) {
79 res[i] = (char) readChunk[i];
85 public KeyStore getPrivateStore() throws GeneralSecurityException, IOException {
86 KeyStore ks1 = KeyStore.getInstance("pkcs12");
87 ks1.load(new ByteArrayInputStream(keystore), keystorpw);
91 public KeyStore getTrustStore() throws GeneralSecurityException, IOException {
92 KeyStore ks1 = KeyStore.getInstance("jks");
93 ks1.load(new ByteArrayInputStream(cacerts), truststorepw);
97 public String getPrivateStorePw() {
98 return new String(keystorpw);