]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/Certificate.java
Implement first issuing for email cert.
[gigi.git] / src / org / cacert / gigi / Certificate.java
index 43e6b87beead50bc97bebb664fc795d5de7b714d..c5aaf3fdd3f650f1f89f37aaaf9973bce36f09e1 100644 (file)
@@ -1,9 +1,19 @@
 package org.cacert.gigi;
 
 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 java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
+
 import org.cacert.gigi.database.DatabaseConnection;
 import org.cacert.gigi.database.DatabaseConnection;
+import org.cacert.gigi.util.KeyStorage;
 
 public class Certificate {
        int id;
 
 public class Certificate {
        int id;
@@ -12,6 +22,12 @@ public class Certificate {
        String md;
        String csrName;
        String crtName;
        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 {
 
        // created, modified, revoked, expire
        public enum CertificateStatus {
@@ -33,7 +49,7 @@ public class Certificate {
                        return CertificateStatus.DRAFT;
                }
                PreparedStatement searcher = DatabaseConnection.getInstance().prepare(
                        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()) {
                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;
                }
                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;
                }
                if (rs.getTime(2) != null && rs.getTime(3) == null) {
                        return CertificateStatus.ISSUED;
                }
@@ -53,7 +70,7 @@ public class Certificate {
                return CertificateStatus.REVOKED;
        }
 
                return CertificateStatus.REVOKED;
        }
 
-       public void issue() {
+       public void issue() throws IOException {
                try {
                        if (getStatus() != CertificateStatus.DRAFT) {
                                throw new IllegalStateException();
                try {
                        if (getStatus() != CertificateStatus.DRAFT) {
                                throw new IllegalStateException();
@@ -61,11 +78,22 @@ public class Certificate {
                        PreparedStatement inserter = DatabaseConnection
                                        .getInstance()
                                        .prepare(
                        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);
                        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();
                }
                } 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;
        }
        public Certificate renew() {
                return null;
        }