]> WPIA git - gigi.git/blob - src/org/cacert/gigi/GigiConfig.java
[EMPTY] Formatting with configured formatter.
[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
25         public byte[] getCacerts() {
26                 return cacerts;
27         }
28
29         public byte[] getKeystore() {
30                 return keystore;
31         }
32
33         public Properties getMainProps() {
34                 return mainProps;
35         }
36
37         public static GigiConfig parse(InputStream input) throws IOException {
38                 TarInputStream tis = new TarInputStream(input);
39                 TarEntry t;
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));
52                         } else {
53                                 System.out.println("Unknown config: " + t.getName());
54                         }
55                 }
56                 tis.close();
57                 return gc;
58         }
59
60         public static byte[] readFully(InputStream is) throws IOException {
61                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
62                 byte[] buffer = new byte[1024];
63                 int len = 0;
64                 while ((len = is.read(buffer)) > 0) {
65                         baos.write(buffer, 0, len);
66                 }
67                 baos.close();
68                 return baos.toByteArray();
69         }
70
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];
75                         readChunk[i] = 0;
76                 }
77                 return res;
78         }
79
80         public KeyStore getPrivateStore() throws GeneralSecurityException, IOException {
81                 KeyStore ks1 = KeyStore.getInstance("pkcs12");
82                 ks1.load(new ByteArrayInputStream(keystore), keystorpw);
83                 return ks1;
84         }
85
86         public KeyStore getTrustStore() throws GeneralSecurityException, IOException {
87                 KeyStore ks1 = KeyStore.getInstance("jks");
88                 ks1.load(new ByteArrayInputStream(cacerts), truststorepw);
89                 return ks1;
90         }
91
92         public String getPrivateStorePw() {
93                 return new String(keystorpw);
94         }
95 }