1 package org.cacert.gigi.util;
4 import java.io.FileInputStream;
5 import java.io.FileReader;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.math.BigInteger;
9 import java.security.GeneralSecurityException;
10 import java.security.cert.CertificateFactory;
11 import java.security.cert.X509Certificate;
12 import java.sql.PreparedStatement;
13 import java.sql.ResultSet;
14 import java.sql.SQLException;
15 import java.util.Properties;
17 import org.cacert.gigi.database.DatabaseConnection;
19 public class SimpleSigner {
20 private static PreparedStatement warnMail;
21 private static PreparedStatement updateMail;
22 private static PreparedStatement readyMail;
23 private static PreparedStatement revoke;
24 private static PreparedStatement revokeCompleted;
25 private static boolean running = true;
26 private static Thread runner;
28 public static void main(String[] args) throws IOException, SQLException, InterruptedException {
29 Properties p = new Properties();
30 p.load(new FileReader("config/gigi.properties"));
31 DatabaseConnection.init(p);
36 public synchronized static void stopSigner() throws InterruptedException {
38 throw new IllegalStateException("already stopped");
46 public synchronized static void runSigner() throws SQLException, IOException, InterruptedException {
48 throw new IllegalStateException("already running");
51 readyMail = DatabaseConnection.getInstance().prepare(
52 "SELECT id, csr_name, subject FROM emailcerts" + " WHERE csr_name is not null"//
54 + " AND crt_name=''"//
57 updateMail = DatabaseConnection.getInstance().prepare(
58 "UPDATE emailcerts SET crt_name=?," + " created=NOW(), serial=? WHERE id=?");
59 warnMail = DatabaseConnection.getInstance().prepare("UPDATE emailcerts SET warning=warning+1 WHERE id=?");
61 revoke = DatabaseConnection.getInstance().prepare(
62 "SELECT id, csr_name FROM emailcerts" + " WHERE csr_name is not null"//
63 + " AND created != 0"//
64 + " AND revoked = '1970-01-01'");
65 revokeCompleted = DatabaseConnection.getInstance().prepare("UPDATE emailcerts SET revoked=NOW() WHERE id=?");
66 runner = new Thread() {
71 } catch (IOException e2) {
73 } catch (InterruptedException e2) {
81 } catch (IOException e) {
83 } catch (SQLException e) {
85 } catch (InterruptedException e1) {
93 private static void revokeCertificates() throws SQLException, IOException, InterruptedException {
94 ResultSet rs = revoke.executeQuery();
95 boolean worked = false;
97 int id = rs.getInt(1);
98 File crt = KeyStorage.locateCrt(id);
99 String[] call = new String[] { "openssl", "ca",//
100 "-cert", "testca.crt",//
101 "-keyfile", "testca.key",//
102 "-revoke", "../" + crt.getPath(),//
104 "-config", "selfsign.config"
107 Process p1 = Runtime.getRuntime().exec(call, null, new File("keys"));
108 System.out.println("revoking: " + crt.getPath());
109 if (p1.waitFor() == 0) {
111 revokeCompleted.setInt(1, id);
112 revokeCompleted.execute();
114 System.out.println("Failed");
122 private static void gencrl() throws IOException, InterruptedException {
123 String[] call = new String[] { "openssl", "ca",//
124 "-cert", "testca.crt",//
125 "-keyfile", "testca.key",//
129 "-out", "testca.crl",//
130 "-config", "selfsign.config"
133 Process p1 = Runtime.getRuntime().exec(call, null, new File("keys"));
134 if (p1.waitFor() != 0) {
135 System.out.println("Error while generating crl.");
139 private static void signCertificates() throws SQLException, IOException, InterruptedException {
140 ResultSet rs = readyMail.executeQuery();
142 String csrname = rs.getString(2);
143 System.out.println("sign: " + csrname);
144 int id = rs.getInt(1);
145 File crt = KeyStorage.locateCrt(id);
146 String[] call = new String[] { "openssl", "ca",//
147 "-cert", "testca.crt",//
148 "-keyfile", "testca.key",//
149 "-in", "../" + csrname,//
150 "-out", "../" + crt.getPath(),//
153 "-subj", rs.getString(3),//
154 "-config", "selfsign.config"
157 Process p1 = Runtime.getRuntime().exec(call, null, new File("keys"));
159 int waitFor = p1.waitFor();
161 try (InputStream is = new FileInputStream(crt)) {
162 CertificateFactory cf = CertificateFactory.getInstance("X.509");
163 X509Certificate crtp = (X509Certificate) cf.generateCertificate(is);
164 BigInteger serial = crtp.getSerialNumber();
165 updateMail.setString(1, crt.getPath());
166 updateMail.setString(2, serial.toString());
167 updateMail.setInt(3, id);
168 updateMail.execute();
169 System.out.println("sign: " + id);
171 } catch (GeneralSecurityException e) {
174 System.out.println("ERROR: " + id);
175 warnMail.setInt(1, id);
178 System.out.println("ERROR: " + id);
179 warnMail.setInt(1, id);