Debian Codegraph @

src/main/java/org/debian/security/KeyStoreHandler.java

raw
1 /*
2 * Copyright (C) 2011 Torsten Werner <[email protected]>
3 * Copyright (C) 2012 Damien Raude-Morvan <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 package org.debian.security;
21
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.security.GeneralSecurityException;
27 import java.security.KeyStore;
28 import java.security.KeyStoreException;
29 import java.security.cert.Certificate;
30 import java.security.cert.CertificateFactory;
31
32 /**
33 * Handles read/write operations on a keystore.
34 */
35 class KeyStoreHandler {
36
37 /** The path of the keystore */
38 private String filename;
39
40 /** The password of the keystore */
41 private char[] password;
42
43 private KeyStore ks;
44
45 private CertificateFactory certFactory;
46
47 KeyStoreHandler(String filename, char[] password) throws GeneralSecurityException, IOException, InvalidKeystorePasswordException {
48 this.filename = filename;
49 this.password = password;
50 this.certFactory = CertificateFactory.getInstance("X.509");
51
52 load();
53 }
54
55 /**
56 * Try to open an existing keystore or create an new one.
57 */
58 public void load() throws GeneralSecurityException, IOException, InvalidKeystorePasswordException {
59 KeyStore ks = KeyStore.getInstance("JKS");
60 File file = new File(filename);
61 FileInputStream in = null;
62 if (file.canRead()) {
63 in = new FileInputStream(file);
64 }
65 try {
66 ks.load(in, password);
67 } catch (IOException e) {
68 throw new InvalidKeystorePasswordException("Cannot open Java keystore. Is the password correct?", e);
69 } finally {
70 if (in != null) {
71 in.close();
72 }
73 }
74 this.ks = ks;
75 }
76
77 /**
78 * Write actual keystore content to disk.
79 */
80 public void save() throws GeneralSecurityException, UnableToSaveKeystoreException {
81 try {
82 FileOutputStream certOutputFile = new FileOutputStream(filename);
83 ks.store(certOutputFile, password);
84 certOutputFile.close();
85 } catch (IOException e) {
86 throw new UnableToSaveKeystoreException("There was a problem saving the new Java keystore.", e);
87 }
88 }
89
90 /**
91 * Add or replace existing cert in keystore with given alias.
92 */
93 public void addAlias(String alias, String path) throws KeyStoreException {
94 Certificate cert = loadCertificate(path);
95 if (cert == null) {
96 return;
97 }
98 addAlias(alias, cert);
99 }
100
101 /**
102 * Add or replace existing cert in keystore with given alias.
103 */
104 public void addAlias(String alias, Certificate cert) throws KeyStoreException {
105 if (contains(alias)) {
106 System.out.println("Replacing " + alias);
107 ks.deleteEntry(alias);
108 } else {
109 System.out.println("Adding " + alias);
110 }
111 ks.setCertificateEntry(alias, cert);
112 }
113
114 /**
115 * Delete cert in keystore at given alias.
116 */
117 public void deleteAlias(String alias) throws GeneralSecurityException {
118 if (contains(alias)) {
119 System.out.println("Removing " + alias);
120 ks.deleteEntry(alias);
121 }
122 }
123
124 /**
125 * Returns true when alias exist in keystore.
126 */
127 public boolean contains(String alias) throws KeyStoreException {
128 return ks.containsAlias(alias);
129 }
130
131 /**
132 * Try to load a certificate instance from given path.
133 */
134 private Certificate loadCertificate(String path) {
135 Certificate certificate = null;
136 try {
137 FileInputStream in = new FileInputStream(path);
138 certificate = certFactory.generateCertificate(in);
139 in.close();
140 } catch (Exception e) {
141 System.err.println("Warning: there was a problem reading the certificate file " +
142 path + ". Message:\n " + e.getMessage());
143 }
144 return certificate;
145 }
146 }