- private Certificate(String serial) {
- //
- String concat = "group_concat(concat('/', `name`, '=', REPLACE(REPLACE(value, '\\\\', '\\\\\\\\'), '/', '\\\\/')))";
- GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT certs.id, " + concat + " as subject, md, csr_name, crt_name,memid, profile FROM `certs` LEFT JOIN certAvas ON certAvas.certid=certs.id WHERE serial=? GROUP BY certs.id");
- ps.setString(1, serial);
- GigiResultSet rs = ps.executeQuery();
- if ( !rs.next()) {
- throw new IllegalArgumentException("Invalid mid " + serial);
- }
- this.id = rs.getInt(1);
- dnString = rs.getString(2);
- md = rs.getString(3);
- csrName = rs.getString(4);
- crtName = rs.getString(5);
- owner = User.getById(rs.getInt(6));
- profile = CertificateProfile.getById(rs.getInt(7));
- this.serial = serial;
-
- GigiPreparedStatement ps2 = DatabaseConnection.getInstance().prepare("SELECT contents, type FROM `subjectAlternativeNames` WHERE certId=?");
- ps2.setInt(1, id);
- GigiResultSet rs2 = ps2.executeQuery();
- sans = new LinkedList<>();
- while (rs2.next()) {
- sans.add(new SubjectAlternateName(SANType.valueOf(rs2.getString("type").toUpperCase()), rs2.getString("contents")));
+ try (GigiPreparedStatement san = new GigiPreparedStatement("INSERT INTO `subjectAlternativeNames` SET `certId`=?, contents=?, type=?::`SANType`")) {
+ for (SubjectAlternateName subjectAlternateName : sans) {
+ san.setInt(1, id);
+ san.setString(2, subjectAlternateName.getName());
+ san.setString(3, subjectAlternateName.getType().getOpensslName());
+ san.execute();
+ }
+ }
+
+ try (GigiPreparedStatement insertAVA = new GigiPreparedStatement("INSERT INTO `certAvas` SET `certId`=?, name=?, value=?")) {
+ insertAVA.setInt(1, id);
+ for (Entry<String, String> e : dn.entrySet()) {
+ insertAVA.setString(2, e.getKey());
+ insertAVA.setString(3, e.getValue());
+ insertAVA.execute();
+ }
+ }
+ File csrFile = KeyStorage.locateCsr(id);
+ csrName = csrFile.getPath();
+ try (FileOutputStream fos = new FileOutputStream(csrFile)) {
+ fos.write(csr.getBytes("UTF-8"));
+ }
+ try (GigiPreparedStatement updater = new GigiPreparedStatement("UPDATE `certs` SET `csr_name`=? WHERE id=?")) {
+ updater.setString(1, csrName);
+ updater.setInt(2, id);
+ updater.execute();
+ }
+
+ cache.put(this);