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() {
25 public byte[] getCacerts() {
29 public byte[] getKeystore() {
33 public Properties getMainProps() {
37 public static GigiConfig parse(InputStream input) throws IOException {
38 TarInputStream tis = new TarInputStream(input);
40 GigiConfig gc = new GigiConfig();
41 while ((t = tis.getNextEntry()) != null) {
42 if (t.getName().equals("gigi.properties")) {
43 gc.mainProps.load(tis);
44 } else if (t.getName().equals("cacerts.jks")) {
45 gc.cacerts = readFully(tis);
46 } else if (t.getName().equals("keystore.pkcs12")) {
47 gc.keystore = readFully(tis);
48 } else if (t.getName().equals("keystorepw")) {
49 gc.keystorpw = transformSafe(readFully(tis));
50 } else if (t.getName().equals("truststorepw")) {
51 gc.truststorepw = transformSafe(readFully(tis));
53 System.out.println("Unknown config: " + t.getName());
60 public static byte[] readFully(InputStream is) throws IOException {
61 ByteArrayOutputStream baos = new ByteArrayOutputStream();
62 byte[] buffer = new byte[1024];
64 while ((len = is.read(buffer)) > 0) {
65 baos.write(buffer, 0, len);
68 return baos.toByteArray();
71 private static char[] transformSafe(byte[] readChunk) {
72 char[] res = new char[readChunk.length];
73 for (int i = 0; i < res.length; i++) {
74 res[i] = (char) readChunk[i];
80 public KeyStore getPrivateStore() throws GeneralSecurityException, IOException {
81 KeyStore ks1 = KeyStore.getInstance("pkcs12");
82 ks1.load(new ByteArrayInputStream(keystore), keystorpw);
86 public KeyStore getTrustStore() throws GeneralSecurityException, IOException {
87 KeyStore ks1 = KeyStore.getInstance("jks");
88 ks1.load(new ByteArrayInputStream(cacerts), truststorepw);
92 public String getPrivateStorePw() {
93 return new String(keystorpw);