Debian Codegraph @

src/main/java/org/debian/security/UpdateCertificates.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.BufferedReader;
23 import java.io.IOException;
24 import java.io.InputStreamReader;
25 import java.io.Reader;
26 import java.security.GeneralSecurityException;
27 import java.security.cert.Certificate;
28
29 /**
30 * This code is a re-implementation of the idea from Ludwig Nussel found in
31 * https://github.com/openSUSE/ca-certificates/blob/41917f5a/keystore.java
32 * for the Debian operating system. It updates the global JVM keystore.
33 *
34 * @author Torsten Werner
35 * @author Damien Raude-Morvan
36 */
37 public class UpdateCertificates {
38
39 private KeyStoreHandler keystore;
40
41 public static void main(String[] args) throws IOException, GeneralSecurityException {
42 String passwordString = "changeit";
43 if (args.length == 2 && args[0].equals("-storepass")) {
44 passwordString = args[1];
45 } else if (args.length > 0) {
46 System.err.println("Usage: java org.debian.security.UpdateCertificates [-storepass <password>]");
47 System.exit(1);
48 }
49
50 try {
51 UpdateCertificates uc = new UpdateCertificates("/etc/ssl/certs/java/cacerts", passwordString);
52 // Force reading of inputstream in UTF-8
53 uc.processChanges(new InputStreamReader(System.in, "UTF8"));
54 uc.finish();
55 } catch (InvalidKeystorePasswordException e) {
56 e.printStackTrace(System.err);
57 System.exit(1);
58 } catch (UnableToSaveKeystoreException e) {
59 e.printStackTrace(System.err);
60 System.exit(1);
61 }
62 }
63
64 public UpdateCertificates(String keystoreFile, String password) throws IOException, GeneralSecurityException, InvalidKeystorePasswordException {
65 this.keystore = new KeyStoreHandler(keystoreFile, password.toCharArray());
66 }
67
68 /**
69 * Until reader EOF, try to read changes and send each to {@link #parseLine(String)}.
70 */
71 protected void processChanges(Reader reader) throws IOException, GeneralSecurityException {
72 String line;
73 BufferedReader in = new BufferedReader(reader);
74 while ((line = in.readLine()) != null) {
75 try {
76 parseLine(line);
77 } catch (UnknownInputException e) {
78 System.err.println("Unknown input: " + line);
79 // Keep processing for others lines
80 }
81 }
82 }
83
84 /**
85 * Parse given line to choose between {@link #addAlias(String, Certificate)}
86 * or {@link #deleteAlias(String)}.
87 */
88 protected void parseLine(final String line) throws GeneralSecurityException, IOException, UnknownInputException {
89 if (line.isEmpty()) {
90 return;
91 }
92 String path = line.substring(1);
93 String filename = path.substring(path.lastIndexOf("/") + 1);
94 String alias = "debian:" + filename;
95 if (line.startsWith("+")) {
96 keystore.addAlias(alias, path);
97 } else if (line.startsWith("-")) {
98 keystore.deleteAlias(alias);
99 } else {
100 throw new UnknownInputException(line);
101 }
102 }
103
104 /**
105 * Write the pending changes to the keystore file.
106 */
107 protected void finish() throws GeneralSecurityException, UnableToSaveKeystoreException {
108 keystore.save();
109 }
110 }