]> WPIA git - gigi.git/blob - src/club/wpia/gigi/GigiConfig.java
upd: rename package name and all references to it
[gigi.git] / src / club / wpia / gigi / GigiConfig.java
1 package club.wpia.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
16     public static final String GIGI_CONFIG_VERSION = "GigiConfigV1.0";
17
18     private byte[] cacerts;
19
20     private byte[] keystore;
21
22     private Properties mainProps = new Properties();
23
24     private char[] keystorpw;
25
26     private char[] truststorepw;
27
28     private GigiConfig() {}
29
30     public byte[] getCacerts() {
31         return cacerts;
32     }
33
34     public byte[] getKeystore() {
35         return keystore;
36     }
37
38     public Properties getMainProps() {
39         return mainProps;
40     }
41
42     public static GigiConfig parse(InputStream input) throws IOException {
43         TarInputStream tis = new TarInputStream(input);
44         TarEntry t;
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));
57             } else {
58                 System.out.println("Unknown config: " + t.getName());
59             }
60         }
61         tis.close();
62         return gc;
63     }
64
65     public static byte[] readFully(InputStream is) throws IOException {
66         ByteArrayOutputStream baos = new ByteArrayOutputStream();
67         byte[] buffer = new byte[1024];
68         int len = 0;
69         while ((len = is.read(buffer)) > 0) {
70             baos.write(buffer, 0, len);
71         }
72         baos.close();
73         return baos.toByteArray();
74     }
75
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];
80             readChunk[i] = 0;
81         }
82         return res;
83     }
84
85     public KeyStore getPrivateStore() throws GeneralSecurityException, IOException {
86         if (keystore == null || keystorpw == null) {
87             return null;
88         }
89         KeyStore ks1 = KeyStore.getInstance("pkcs12");
90         ks1.load(new ByteArrayInputStream(keystore), keystorpw);
91         return ks1;
92     }
93
94     public KeyStore getTrustStore() throws GeneralSecurityException, IOException {
95         KeyStore ks1 = KeyStore.getInstance("jks");
96         ks1.load(new ByteArrayInputStream(cacerts), truststorepw);
97         return ks1;
98     }
99
100     public String getPrivateStorePw() {
101         return new String(keystorpw);
102     }
103 }