1 package org.cacert.gigi.util;
4 import java.io.FileReader;
5 import java.io.IOException;
6 import java.sql.PreparedStatement;
7 import java.sql.ResultSet;
8 import java.sql.SQLException;
9 import java.util.Properties;
11 import org.cacert.gigi.database.DatabaseConnection;
13 public class SimpleSigner {
14 private static PreparedStatement warnMail;
15 private static PreparedStatement updateMail;
16 private static PreparedStatement readyMail;
17 private static PreparedStatement revoke;
18 private static PreparedStatement revokeCompleted;
20 public static void main(String[] args) throws IOException, SQLException,
21 InterruptedException {
22 Properties p = new Properties();
23 p.load(new FileReader("config/gigi.properties"));
24 DatabaseConnection.init(p);
26 readyMail = DatabaseConnection.getInstance().prepare(
27 "SELECT id, csr_name, subject FROM emailcerts"
28 + " WHERE csr_name is not null"//
30 + " AND crt_name=''"//
33 updateMail = DatabaseConnection.getInstance().prepare(
34 "UPDATE emailcerts SET crt_name=?,"
35 + " created=NOW() WHERE id=?");
36 warnMail = DatabaseConnection.getInstance().prepare(
37 "UPDATE emailcerts SET warning=warning+1 WHERE id=?");
39 revoke = DatabaseConnection.getInstance().prepare(
40 "SELECT id, csr_name FROM emailcerts"
41 + " WHERE csr_name is not null"//
42 + " AND created != 0"//
43 + " AND revoked = '1970-01-01'");
44 revokeCompleted = DatabaseConnection.getInstance().prepare(
45 "UPDATE emailcerts SET revoked=NOW() WHERE id=?");
48 System.out.println("ping");
55 private static void revokeCertificates() throws SQLException, IOException,
56 InterruptedException {
57 ResultSet rs = revoke.executeQuery();
58 boolean worked = false;
60 int id = rs.getInt(1);
61 File crt = KeyStorage.locateCrt(id);
62 String[] call = new String[]{"openssl", "ca",//
63 "-cert", "testca.crt",//
64 "-keyfile", "testca.key",//
65 "-revoke", "../" + crt.getPath(),//
67 "-config", "selfsign.config"
70 Process p1 = Runtime.getRuntime()
71 .exec(call, null, new File("keys"));
72 System.out.println("revoking: " + crt.getPath());
73 if (p1.waitFor() == 0) {
75 revokeCompleted.setInt(1, id);
76 revokeCompleted.execute();
78 System.out.println("Failed");
85 private static void gencrl() throws IOException, InterruptedException {
86 String[] call = new String[]{"openssl", "ca",//
87 "-cert", "testca.crt",//
88 "-keyfile", "testca.key",//
92 "-out", "testca.crl",//
93 "-config", "selfsign.config"
96 Process p1 = Runtime.getRuntime().exec(call, null, new File("keys"));
97 if (p1.waitFor() != 0) {
98 System.out.println("Error while generating crl.");
101 private static void signCertificates() throws SQLException, IOException,
102 InterruptedException {
103 ResultSet rs = readyMail.executeQuery();
105 String csrname = rs.getString(2);
106 System.out.println("sign: " + csrname);
107 int id = rs.getInt(1);
108 File crt = KeyStorage.locateCrt(id);
109 String[] call = new String[]{"openssl", "ca",//
110 "-cert", "testca.crt",//
111 "-keyfile", "testca.key",//
112 "-in", "../" + csrname,//
113 "-out", "../" + crt.getPath(),//
116 "-subj", rs.getString(3),//
117 "-config", "selfsign.config"
120 Process p1 = Runtime.getRuntime()
121 .exec(call, null, new File("keys"));
123 int waitFor = p1.waitFor();
125 updateMail.setString(1, crt.getPath());
126 updateMail.setInt(2, id);
127 updateMail.execute();
128 System.out.println("sign: " + id);
130 System.out.println("ERROR: " + id);
131 warnMail.setInt(1, id);