Merge "Suggestions to enhance the SQL call pattern."
[gigi.git] / util-testing / org / cacert / gigi / util / SimpleSigner.java
1 package org.cacert.gigi.util;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.io.InputStreamReader;
10 import java.io.PrintWriter;
11 import java.io.Reader;
12 import java.math.BigInteger;
13 import java.nio.file.Paths;
14 import java.security.GeneralSecurityException;
15 import java.security.KeyFactory;
16 import java.security.NoSuchAlgorithmException;
17 import java.security.PrivateKey;
18 import java.security.PublicKey;
19 import java.security.Signature;
20 import java.security.cert.CertificateFactory;
21 import java.security.cert.X509Certificate;
22 import java.security.spec.InvalidKeySpecException;
23 import java.security.spec.PKCS8EncodedKeySpec;
24 import java.sql.SQLException;
25 import java.sql.Timestamp;
26 import java.text.ParseException;
27 import java.text.SimpleDateFormat;
28 import java.util.Base64;
29 import java.util.Calendar;
30 import java.util.Date;
31 import java.util.HashMap;
32 import java.util.LinkedList;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Map.Entry;
36 import java.util.Properties;
37 import java.util.TimeZone;
38
39 import javax.security.auth.x500.X500Principal;
40
41 import org.cacert.gigi.crypto.SPKAC;
42 import org.cacert.gigi.database.DatabaseConnection;
43 import org.cacert.gigi.database.DatabaseConnection.Link;
44 import org.cacert.gigi.database.GigiPreparedStatement;
45 import org.cacert.gigi.database.GigiResultSet;
46 import org.cacert.gigi.dbObjects.Certificate.CSRType;
47 import org.cacert.gigi.dbObjects.Certificate.SANType;
48 import org.cacert.gigi.dbObjects.Certificate.SubjectAlternateName;
49 import org.cacert.gigi.dbObjects.CertificateProfile;
50 import org.cacert.gigi.dbObjects.Digest;
51 import org.cacert.gigi.output.DateSelector;
52
53 import sun.security.pkcs10.PKCS10;
54 import sun.security.util.DerOutputStream;
55 import sun.security.util.DerValue;
56 import sun.security.util.ObjectIdentifier;
57 import sun.security.x509.AVA;
58 import sun.security.x509.AlgorithmId;
59 import sun.security.x509.GeneralNameInterface;
60 import sun.security.x509.RDN;
61 import sun.security.x509.X500Name;
62
63 public class SimpleSigner {
64
65     private static GigiPreparedStatement warnMail;
66
67     private static GigiPreparedStatement updateMail;
68
69     private static GigiPreparedStatement readyCerts;
70
71     private static GigiPreparedStatement getSANSs;
72
73     private static GigiPreparedStatement revoke;
74
75     private static GigiPreparedStatement revokeCompleted;
76
77     private static GigiPreparedStatement finishJob;
78
79     private static GigiPreparedStatement locateCA;
80
81     private static volatile boolean running = true;
82
83     private static Thread runner;
84
85     private static SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss'Z'");
86
87     static {
88         TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
89         sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
90     }
91
92     public static void main(String[] args) throws IOException, SQLException, InterruptedException {
93         Properties p = new Properties();
94         try (Reader reader = new InputStreamReader(new FileInputStream("config/gigi.properties"), "UTF-8")) {
95             p.load(reader);
96         }
97         DatabaseConnection.init(p);
98
99         runSigner();
100     }
101
102     public static void stopSigner() throws InterruptedException {
103         Thread capturedRunner;
104         synchronized (SimpleSigner.class) {
105             if (runner == null) {
106                 throw new IllegalStateException("already stopped");
107             }
108             capturedRunner = runner;
109             running = false;
110             SimpleSigner.class.notifyAll();
111         }
112         capturedRunner.join();
113     }
114
115     public synchronized static void runSigner() throws SQLException, IOException, InterruptedException {
116         if (runner != null) {
117             throw new IllegalStateException("already running");
118         }
119         running = true;
120
121         runner = new Thread() {
122
123             @Override
124             public void run() {
125                 try (Link l = DatabaseConnection.newLink(false)) {
126                     readyCerts = new GigiPreparedStatement("SELECT certs.id AS id, certs.csr_name, jobs.id AS jobid, csr_type, md, `executeFrom`, `executeTo`, profile FROM jobs " + //
127                             "INNER JOIN certs ON certs.id=jobs.`targetId` " + //
128                             "INNER JOIN profiles ON profiles.id=certs.profile " + //
129                             "WHERE jobs.state='open' "//
130                             + "AND task='sign'");
131
132                     getSANSs = new GigiPreparedStatement("SELECT contents, type FROM `subjectAlternativeNames` " + //
133                             "WHERE `certId`=?");
134
135                     updateMail = new GigiPreparedStatement("UPDATE certs SET crt_name=?," + " created=NOW(), serial=?, caid=? WHERE id=?");
136                     warnMail = new GigiPreparedStatement("UPDATE jobs SET warning=warning+1, state=IF(warning<3, 'open','error') WHERE id=?");
137
138                     revoke = new GigiPreparedStatement("SELECT certs.id, certs.csr_name,jobs.id FROM jobs INNER JOIN certs ON jobs.`targetId`=certs.id" + " WHERE jobs.state='open' AND task='revoke'");
139                     revokeCompleted = new GigiPreparedStatement("UPDATE certs SET revoked=NOW() WHERE id=?");
140
141                     finishJob = new GigiPreparedStatement("UPDATE jobs SET state='done' WHERE id=?");
142
143                     locateCA = new GigiPreparedStatement("SELECT id FROM cacerts WHERE keyname=?");
144
145                     work();
146                 } catch (InterruptedException e) {
147                     throw new Error(e);
148                 }
149             }
150
151         };
152         runner.start();
153     }
154
155     private synchronized static void work() {
156         try {
157             gencrl();
158         } catch (IOException e2) {
159             e2.printStackTrace();
160         } catch (InterruptedException e2) {
161             e2.printStackTrace();
162         }
163
164         while (running) {
165             try {
166                 signCertificates();
167                 revokeCertificates();
168
169                 SimpleSigner.class.wait(5000);
170             } catch (IOException e) {
171                 e.printStackTrace();
172             } catch (SQLException e) {
173                 e.printStackTrace();
174             } catch (InterruptedException e1) {
175             }
176         }
177         runner = null;
178     }
179
180     private static void revokeCertificates() throws SQLException, IOException, InterruptedException {
181         GigiResultSet rs = revoke.executeQuery();
182         boolean worked = false;
183         while (rs.next()) {
184             int id = rs.getInt(1);
185             worked = true;
186             System.out.println("Revoke faked: " + id);
187             revokeCompleted.setInt(1, id);
188             revokeCompleted.execute();
189             finishJob.setInt(1, rs.getInt(3));
190             finishJob.execute();
191         }
192         if (worked) {
193             gencrl();
194         }
195     }
196
197     private static void gencrl() throws IOException, InterruptedException {
198         if (true) {
199             return;
200         }
201         String[] call = new String[] {
202                 "openssl", "ca",//
203                 "-cert",
204                 "../unassured.crt",//
205                 "-keyfile",
206                 "../unassured.key",//
207                 "-gencrl",//
208                 "-crlhours",//
209                 "12",//
210                 "-out",
211                 "../unassured.crl",//
212                 "-config",
213                 "../selfsign.config"
214
215         };
216         Process p1 = Runtime.getRuntime().exec(call, null, new File("keys/unassured.ca"));
217         if (p1.waitFor() != 0) {
218             System.out.println("Error while generating crl.");
219         }
220     }
221
222     private static int counter = 0;
223
224     private static void signCertificates() throws SQLException {
225         GigiResultSet rs = readyCerts.executeQuery();
226
227         Calendar c = Calendar.getInstance();
228         c.setTimeZone(TimeZone.getTimeZone("UTC"));
229         while (rs.next()) {
230             String csrname = rs.getString("csr_name");
231             int id = rs.getInt("id");
232             System.out.println("sign: " + csrname);
233             try {
234                 String csrType = rs.getString("csr_type");
235                 CSRType ct = CSRType.valueOf(csrType);
236                 File crt = KeyStorage.locateCrt(id);
237
238                 Timestamp from = rs.getTimestamp("executeFrom");
239                 String length = rs.getString("executeTo");
240                 Date fromDate;
241                 Date toDate;
242                 if (from == null) {
243                     fromDate = new Date(System.currentTimeMillis());
244                 } else {
245                     fromDate = new Date(from.getTime());
246                 }
247                 if (length.endsWith("m") || length.endsWith("y")) {
248                     String num = length.substring(0, length.length() - 1);
249                     int inter = Integer.parseInt(num);
250                     c.setTime(fromDate);
251                     if (length.endsWith("m")) {
252                         c.add(Calendar.MONTH, inter);
253                     } else {
254                         c.add(Calendar.YEAR, inter);
255                     }
256                     toDate = c.getTime();
257                 } else {
258                     toDate = DateSelector.getDateFormat().parse(length);
259                 }
260
261                 getSANSs.setInt(1, id);
262                 GigiResultSet san = getSANSs.executeQuery();
263
264                 LinkedList<SubjectAlternateName> altnames = new LinkedList<>();
265                 while (san.next()) {
266                     altnames.add(new SubjectAlternateName(SANType.valueOf(san.getString("type").toUpperCase()), san.getString("contents")));
267                 }
268                 // TODO look them up!
269                 // cfg.println("keyUsage=critical," +
270                 // "digitalSignature, keyEncipherment, keyAgreement");
271                 // cfg.println("extendedKeyUsage=critical," + "clientAuth");
272                 // cfg.close();
273
274                 int profile = rs.getInt("profile");
275                 CertificateProfile cp = CertificateProfile.getById(profile);
276                 String s = cp.getId() + "";
277                 while (s.length() < 4) {
278                     s = "0" + s;
279                 }
280                 s += "-" + cp.getKeyName() + ".cfg";
281                 Properties caP = new Properties();
282                 try (FileInputStream inStream = new FileInputStream("signer/profiles/" + s)) {
283                     caP.load(inStream);
284                 }
285                 String ca = caP.getProperty("ca") + "_2015_1";
286
287                 HashMap<String, String> subj = new HashMap<>();
288                 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT name, value FROM `certAvas` WHERE `certId`=?")) {
289                     ps.setInt(1, rs.getInt("id"));
290                     GigiResultSet rs2 = ps.executeQuery();
291                     while (rs2.next()) {
292                         String name = rs2.getString("name");
293                         if (name.equals("EMAIL")) {
294                             name = "emailAddress";
295                         }
296                         subj.put(name, rs2.getString("value"));
297                     }
298                 }
299                 if (subj.size() == 0) {
300                     subj.put("CN", "<empty>");
301                     System.out.println("WARNING: DN was empty");
302                 }
303                 System.out.println(subj);
304
305                 PublicKey pk;
306                 byte[] data = IOUtils.readURL(new FileInputStream(csrname));
307                 if (ct == CSRType.SPKAC) {
308                     String dt = new String(data, "UTF-8");
309                     if (dt.startsWith("SPKAC=")) {
310                         dt = dt.substring(6);
311                         data = dt.getBytes("UTF-8");
312                         System.out.println(dt);
313                     }
314                     SPKAC sp = new SPKAC(Base64.getDecoder().decode(data));
315                     pk = sp.getPubkey();
316                 } else {
317                     PKCS10 p10 = new PKCS10(PEM.decode("(NEW )?CERTIFICATE REQUEST", new String(data, "UTF-8")));
318                     pk = p10.getSubjectPublicKeyInfo();
319                 }
320                 PrivateKey i = loadOpensslKey(new File("signer/ca/" + ca + "/ca.key"));
321
322                 X509Certificate root = (X509Certificate) CertificateFactory.getInstance("X509").generateCertificate(new FileInputStream("signer/ca/" + ca + "/ca.crt"));
323                 byte[] cert = generateCert(pk, i, subj, root.getSubjectX500Principal(), altnames, fromDate, toDate, Digest.valueOf(rs.getString("md").toUpperCase()), caP.getProperty("eku"));
324                 PrintWriter out = new PrintWriter(crt);
325                 out.println("-----BEGIN CERTIFICATE-----");
326                 out.println(Base64.getMimeEncoder().encodeToString(cert));
327                 out.println("-----END CERTIFICATE-----");
328                 out.close();
329
330                 try (InputStream is = new FileInputStream(crt)) {
331                     locateCA.setString(1, ca);
332                     GigiResultSet caRs = locateCA.executeQuery();
333                     if ( !caRs.next()) {
334                         throw new Error("ca " + ca + " was not found");
335                     }
336
337                     CertificateFactory cf = CertificateFactory.getInstance("X.509");
338                     X509Certificate crtp = (X509Certificate) cf.generateCertificate(is);
339                     BigInteger serial = crtp.getSerialNumber();
340                     updateMail.setString(1, crt.getPath());
341                     updateMail.setString(2, serial.toString(16));
342                     updateMail.setInt(3, caRs.getInt("id"));
343                     updateMail.setInt(4, id);
344                     updateMail.execute();
345
346                     finishJob.setInt(1, rs.getInt("jobid"));
347                     finishJob.execute();
348                     System.out.println("signed: " + id);
349                     continue;
350                 }
351
352             } catch (GeneralSecurityException e) {
353                 e.printStackTrace();
354             } catch (IOException e) {
355                 e.printStackTrace();
356             } catch (ParseException e) {
357                 e.printStackTrace();
358             }
359             System.out.println("Error with: " + id);
360             warnMail.setInt(1, rs.getInt("jobid"));
361             warnMail.execute();
362
363         }
364         rs.close();
365     }
366
367     private static PrivateKey loadOpensslKey(File f) throws FileNotFoundException, IOException, InvalidKeySpecException, NoSuchAlgorithmException {
368         byte[] p8b = PEM.decode("RSA PRIVATE KEY", new String(IOUtils.readURL(new FileInputStream(f))));
369         DerOutputStream dos = new DerOutputStream();
370         dos.putInteger(0);
371         new AlgorithmId(new ObjectIdentifier(new int[] {
372                 1, 2, 840, 113549, 1, 1, 1
373         })).encode(dos);
374         dos.putOctetString(p8b);
375         byte[] ctx = dos.toByteArray();
376         dos.reset();
377         dos.write(DerValue.tag_Sequence, ctx);
378         PKCS8EncodedKeySpec p8 = new PKCS8EncodedKeySpec(dos.toByteArray());
379         PrivateKey i = KeyFactory.getInstance("RSA").generatePrivate(p8);
380         return i;
381     }
382
383     public static synchronized byte[] generateCert(PublicKey pk, PrivateKey prk, Map<String, String> subj, X500Principal issuer, List<SubjectAlternateName> altnames, Date fromDate, Date toDate, Digest digest, String eku) throws IOException, GeneralSecurityException {
384         File f = Paths.get("signer", "serial").toFile();
385         if ( !f.exists()) {
386             try (FileOutputStream fos = new FileOutputStream(f)) {
387                 fos.write("1".getBytes("UTF-8"));
388             }
389         }
390         try (FileInputStream fr = new FileInputStream(f)) {
391             byte[] serial = IOUtils.readURL(fr);
392             BigInteger ser = new BigInteger(new String(serial).trim());
393             ser = ser.add(BigInteger.ONE);
394
395             PrintWriter pw = new PrintWriter(f);
396             pw.println(ser);
397             pw.close();
398             if (digest != Digest.SHA256 && digest != Digest.SHA512) {
399                 System.err.println("assuming sha256 either way ;-): " + digest);
400                 digest = Digest.SHA256;
401             }
402             ObjectIdentifier sha512withrsa = new ObjectIdentifier(new int[] {
403                     1, 2, 840, 113549, 1, 1, digest == Digest.SHA256 ? 11 : 13
404             });
405             AlgorithmId aid = new AlgorithmId(sha512withrsa);
406             Signature s = Signature.getInstance(digest == Digest.SHA256 ? "SHA256withRSA" : "SHA512withRSA");
407
408             DerOutputStream cert = new DerOutputStream();
409             DerOutputStream content = new DerOutputStream();
410             {
411                 DerOutputStream version = new DerOutputStream();
412                 version.putInteger(2); // v3
413                 content.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0), version);
414             }
415             content.putInteger(ser); // Serial
416             aid.encode(content);
417
418             {
419                 content.write(issuer.getEncoded());
420             }
421             {
422                 DerOutputStream notAround = new DerOutputStream();
423                 notAround.putUTCTime(fromDate);
424                 notAround.putUTCTime(toDate);
425                 content.write(DerValue.tag_Sequence, notAround);
426             }
427             {
428
429                 X500Name xn = genX500Name(subj);
430                 content.write(xn.getEncoded());
431             }
432             {
433                 content.write(pk.getEncoded());
434             }
435             {
436                 DerOutputStream extensions = new DerOutputStream();
437                 {
438                     addExtension(extensions, new ObjectIdentifier(new int[] {
439                             2, 5, 29, 17
440                     }), generateSAN(altnames));
441                     addExtension(extensions, new ObjectIdentifier(new int[] {
442                             2, 5, 29, 15
443                     }), generateKU());
444                     addExtension(extensions, new ObjectIdentifier(new int[] {
445                             2, 5, 29, 37
446                     }), generateEKU(eku));
447                 }
448                 DerOutputStream extensionsSeq = new DerOutputStream();
449                 extensionsSeq.write(DerValue.tag_Sequence, extensions);
450                 content.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 3), extensionsSeq);
451             }
452
453             DerOutputStream contentSeq = new DerOutputStream();
454
455             contentSeq.write(DerValue.tag_Sequence, content.toByteArray());
456
457             s.initSign(prk);
458             s.update(contentSeq.toByteArray());
459
460             aid.encode(contentSeq);
461             contentSeq.putBitString(s.sign());
462             cert.write(DerValue.tag_Sequence, contentSeq);
463
464             // X509Certificate c = (X509Certificate)
465             // CertificateFactory.getInstance("X509").generateCertificate(new
466             // ByteArrayInputStream(cert.toByteArray()));
467             // c.verify(pk); only for self-signeds
468
469             byte[] res = cert.toByteArray();
470             cert.close();
471             return res;
472         }
473
474     }
475
476     private static byte[] generateKU() throws IOException {
477         try (DerOutputStream dos = new DerOutputStream()) {
478             dos.putBitString(new byte[] {
479                 (byte) 0b10101000
480             });
481             return dos.toByteArray();
482         }
483     }
484
485     private static byte[] generateEKU(String eku) throws IOException {
486
487         DerOutputStream dos = new DerOutputStream();
488         for (String name : eku.split(",")) {
489             ObjectIdentifier oid;
490             switch (name) {
491             case "serverAuth":
492                 oid = new ObjectIdentifier("1.3.6.1.5.5.7.3.1");
493                 break;
494             case "clientAuth":
495                 oid = new ObjectIdentifier("1.3.6.1.5.5.7.3.2");
496                 break;
497             case "codeSigning":
498                 oid = new ObjectIdentifier("1.3.6.1.5.5.7.3.3");
499                 break;
500             case "emailProtection":
501                 oid = new ObjectIdentifier("1.3.6.1.5.5.7.3.4");
502                 break;
503             case "OCSPSigning":
504                 oid = new ObjectIdentifier("1.3.6.1.5.5.7.3.9");
505                 break;
506
507             default:
508                 throw new Error(name);
509             }
510             dos.putOID(oid);
511         }
512         byte[] data = dos.toByteArray();
513         dos.reset();
514         dos.write(DerValue.tag_Sequence, data);
515         return dos.toByteArray();
516     }
517
518     public static X500Name genX500Name(Map<String, String> subj) throws IOException {
519         LinkedList<RDN> rdns = new LinkedList<>();
520         for (Entry<String, String> i : subj.entrySet()) {
521             RDN rdn = genRDN(i);
522             rdns.add(rdn);
523         }
524         return new X500Name(rdns.toArray(new RDN[rdns.size()]));
525     }
526
527     private static RDN genRDN(Entry<String, String> i) throws IOException {
528         DerOutputStream dos = new DerOutputStream();
529         dos.putUTF8String(i.getValue());
530         int[] oid;
531         String key = i.getKey();
532         switch (key) {
533         case "CN":
534             oid = new int[] {
535                     2, 5, 4, 3
536             };
537             break;
538         case "EMAIL":
539         case "emailAddress":
540             oid = new int[] {
541                     1, 2, 840, 113549, 1, 9, 1
542             };
543             break;
544         case "O":
545             oid = new int[] {
546                     2, 5, 4, 10
547             };
548             break;
549         case "OU":
550             oid = new int[] {
551                     2, 5, 4, 11
552             };
553             break;
554         case "ST":
555             oid = new int[] {
556                     2, 5, 4, 8
557             };
558             break;
559         case "L":
560             oid = new int[] {
561                     2, 5, 4, 7
562             };
563             break;
564         case "C":
565             oid = new int[] {
566                     2, 5, 4, 6
567             };
568             break;
569         default:
570             dos.close();
571             throw new Error("unknown RDN-type: " + key);
572         }
573         RDN rdn = new RDN(new AVA(new ObjectIdentifier(oid), new DerValue(dos.toByteArray())));
574         dos.close();
575         return rdn;
576     }
577
578     private static void addExtension(DerOutputStream extensions, ObjectIdentifier oid, byte[] extContent) throws IOException {
579         DerOutputStream SANs = new DerOutputStream();
580         SANs.putOID(oid);
581         SANs.putOctetString(extContent);
582
583         extensions.write(DerValue.tag_Sequence, SANs);
584     }
585
586     private static byte[] generateSAN(List<SubjectAlternateName> altnames) throws IOException {
587         DerOutputStream SANContent = new DerOutputStream();
588         for (SubjectAlternateName san : altnames) {
589             byte type = 0;
590             if (san.getType() == SANType.DNS) {
591                 type = (byte) GeneralNameInterface.NAME_DNS;
592             } else if (san.getType() == SANType.EMAIL) {
593                 type = (byte) GeneralNameInterface.NAME_RFC822;
594             } else {
595                 SANContent.close();
596                 throw new Error("" + san.getType());
597             }
598             SANContent.write(DerValue.createTag(DerValue.TAG_CONTEXT, false, type), san.getName().getBytes("UTF-8"));
599         }
600         DerOutputStream SANSeqContent = new DerOutputStream();
601         SANSeqContent.write(DerValue.tag_Sequence, SANContent);
602         byte[] byteArray = SANSeqContent.toByteArray();
603         SANContent.close();
604         SANSeqContent.close();
605         return byteArray;
606     }
607 }