X-Git-Url: https://code.wpia.club/?p=gigi.git;a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2FCertificate.java;h=c5aaf3fdd3f650f1f89f37aaaf9973bce36f09e1;hp=43e6b87beead50bc97bebb664fc795d5de7b714d;hb=ab88602bac4ff5c5540765a85a2013ecadb070df;hpb=4774a98026a0f96130fd8d01e7279df514a5b888 diff --git a/src/org/cacert/gigi/Certificate.java b/src/org/cacert/gigi/Certificate.java index 43e6b87b..c5aaf3fd 100644 --- a/src/org/cacert/gigi/Certificate.java +++ b/src/org/cacert/gigi/Certificate.java @@ -1,9 +1,19 @@ package org.cacert.gigi; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.security.GeneralSecurityException; +import java.security.cert.CertificateFactory; +import java.security.cert.X509Certificate; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; + import org.cacert.gigi.database.DatabaseConnection; +import org.cacert.gigi.util.KeyStorage; public class Certificate { int id; @@ -12,6 +22,12 @@ public class Certificate { String md; String csrName; String crtName; + String csr = null; + public Certificate(String dn, String md, String csr) { + this.dn = dn; + this.md = md; + this.csr = csr; + } // created, modified, revoked, expire public enum CertificateStatus { @@ -33,7 +49,7 @@ public class Certificate { return CertificateStatus.DRAFT; } PreparedStatement searcher = DatabaseConnection.getInstance().prepare( - "SELECT csr_name, created, revoked FROM emailcerts WHERE id=?"); + "SELECT crt_name, created, revoked FROM emailcerts WHERE id=?"); searcher.setInt(1, id); ResultSet rs = searcher.executeQuery(); if (!rs.next()) { @@ -42,7 +58,8 @@ public class Certificate { if (rs.getString(2) == null) { return CertificateStatus.BEEING_ISSUED; } - csrName = rs.getString(1); + crtName = rs.getString(1); + System.out.println(crtName); if (rs.getTime(2) != null && rs.getTime(3) == null) { return CertificateStatus.ISSUED; } @@ -53,7 +70,7 @@ public class Certificate { return CertificateStatus.REVOKED; } - public void issue() { + public void issue() throws IOException { try { if (getStatus() != CertificateStatus.DRAFT) { throw new IllegalStateException(); @@ -61,11 +78,22 @@ public class Certificate { PreparedStatement inserter = DatabaseConnection .getInstance() .prepare( - "INSERT INTO emailcerts SET csr_name =?, md=?, subject='a', coll_found=0, crt_name=''"); - inserter.setString(1, csrName); - inserter.setString(2, md); + "INSERT INTO emailcerts SET md=?, subject=?, coll_found=0, crt_name=''"); + inserter.setString(1, md); + inserter.setString(2, dn); inserter.execute(); id = DatabaseConnection.lastInsertId(inserter); + File csrFile = KeyStorage.locateCsr(id); + csrName = csrFile.getPath(); + FileOutputStream fos = new FileOutputStream(csrFile); + fos.write(csr.getBytes()); + fos.close(); + + PreparedStatement updater = DatabaseConnection.getInstance() + .prepare("UPDATE emailcerts SET csr_name=? WHERE id=?"); + updater.setString(1, csrName); + updater.setInt(2, id); + updater.execute(); } catch (SQLException e) { e.printStackTrace(); } @@ -97,6 +125,26 @@ public class Certificate { } } + + public X509Certificate cert() throws IOException, GeneralSecurityException, + SQLException { + CertificateStatus status = getStatus(); + if (status != CertificateStatus.ISSUED) { + throw new IllegalStateException(status + " is not wanted here."); + } + InputStream is = null; + X509Certificate crt = null; + try { + is = new FileInputStream(crtName); + CertificateFactory cf = CertificateFactory.getInstance("X.509"); + crt = (X509Certificate) cf.generateCertificate(is); + } finally { + if (is != null) { + is.close(); + } + } + return crt; + } public Certificate renew() { return null; }