]> WPIA git - gigi.git/blob - src/org/cacert/gigi/GigiConfig.java
Merge branch 'autotesting'
[gigi.git] / src / org / cacert / gigi / GigiConfig.java
1 package org.cacert.gigi;
2
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;
10
11 import org.kamranzafar.jtar.TarEntry;
12 import org.kamranzafar.jtar.TarInputStream;
13
14 public class GigiConfig {
15         public static final String GIGI_CONFIG_VERSION = "GigiConfigV1.0";
16         byte[] cacerts;
17         byte[] keystore;
18         Properties mainProps = new Properties();
19         private char[] keystorpw;
20         private char[] truststorepw;
21
22         private GigiConfig() {
23         }
24         public byte[] getCacerts() {
25                 return cacerts;
26         }
27         public byte[] getKeystore() {
28                 return keystore;
29         }
30         public Properties getMainProps() {
31                 return mainProps;
32         }
33
34         public static GigiConfig parse(InputStream input) throws IOException {
35                 TarInputStream tis = new TarInputStream(input);
36                 TarEntry t;
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));
49                         } else {
50                                 System.out.println("Unknown config: " + t.getName());
51                         }
52                 }
53                 tis.close();
54                 return gc;
55         }
56         public static byte[] readFully(InputStream is) throws IOException {
57                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
58                 byte[] buffer = new byte[1024];
59                 int len = 0;
60                 while ((len = is.read(buffer)) > 0) {
61                         baos.write(buffer, 0, len);
62                 }
63                 baos.close();
64                 return baos.toByteArray();
65         }
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];
70                         readChunk[i] = 0;
71                 }
72                 return res;
73         }
74
75         public KeyStore getPrivateStore() throws GeneralSecurityException,
76                         IOException {
77                 KeyStore ks1 = KeyStore.getInstance("pkcs12");
78                 ks1.load(new ByteArrayInputStream(keystore), keystorpw);
79                 return ks1;
80         }
81         public KeyStore getTrustStore() throws GeneralSecurityException,
82                         IOException {
83                 KeyStore ks1 = KeyStore.getInstance("jks");
84                 ks1.load(new ByteArrayInputStream(cacerts), truststorepw);
85                 return ks1;
86         }
87 }