]> WPIA git - gigi.git/blob - util/org/cacert/gigi/util/SimpleSigner.java
Name Cleanup in SimpleSigner
[gigi.git] / util / org / cacert / gigi / util / SimpleSigner.java
1 package org.cacert.gigi.util;
2
3 import java.io.File;
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;
10
11 import org.cacert.gigi.database.DatabaseConnection;
12
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;
19
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);
25
26                 readyMail = DatabaseConnection.getInstance().prepare(
27                                 "SELECT id, csr_name, subject FROM emailcerts"
28                                                 + " WHERE csr_name is not null"//
29                                                 + " AND created=0"//
30                                                 + " AND crt_name=''"//
31                                                 + " AND warning<3");
32
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=?");
38
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=?");
46                 gencrl();
47                 while (true) {
48                         System.out.println("ping");
49                         signCertificates();
50                         revokeCertificates();
51                         Thread.sleep(5000);
52                 }
53         }
54
55         private static void revokeCertificates() throws SQLException, IOException,
56                         InterruptedException {
57                 ResultSet rs = revoke.executeQuery();
58                 boolean worked = false;
59                 while (rs.next()) {
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(),//
66                                         "-batch",//
67                                         "-config", "selfsign.config"
68
69                         };
70                         Process p1 = Runtime.getRuntime()
71                                         .exec(call, null, new File("keys"));
72                         System.out.println("revoking: " + crt.getPath());
73                         if (p1.waitFor() == 0) {
74                                 worked = true;
75                                 revokeCompleted.setInt(1, id);
76                                 revokeCompleted.execute();
77                         } else {
78                                 System.out.println("Failed");
79                         }
80                 }
81                 if (worked) {
82                         gencrl();
83                 }
84         }
85         private static void gencrl() throws IOException, InterruptedException {
86                 String[] call = new String[]{"openssl", "ca",//
87                                 "-cert", "testca.crt",//
88                                 "-keyfile", "testca.key",//
89                                 "-gencrl",//
90                                 "-crlhours",//
91                                 "12",//
92                                 "-out", "testca.crl",//
93                                 "-config", "selfsign.config"
94
95                 };
96                 Process p1 = Runtime.getRuntime().exec(call, null, new File("keys"));
97                 if (p1.waitFor() != 0) {
98                         System.out.println("Error while generating crl.");
99                 }
100         }
101         private static void signCertificates() throws SQLException, IOException,
102                         InterruptedException {
103                 ResultSet rs = readyMail.executeQuery();
104                 while (rs.next()) {
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(),//
114                                         "-days", "356",//
115                                         "-batch",//
116                                         "-subj", rs.getString(3),//
117                                         "-config", "selfsign.config"
118
119                         };
120                         Process p1 = Runtime.getRuntime()
121                                         .exec(call, null, new File("keys"));
122
123                         int waitFor = p1.waitFor();
124                         if (waitFor == 0) {
125                                 updateMail.setString(1, crt.getPath());
126                                 updateMail.setInt(2, id);
127                                 updateMail.execute();
128                                 System.out.println("sign: " + id);
129                         } else {
130                                 System.out.println("ERROR: " + id);
131                                 warnMail.setInt(1, id);
132                                 warnMail.execute();
133                         }
134
135                 }
136                 rs.close();
137         }
138 }