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.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;
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 {
return CertificateStatus.BEEING_ISSUED;
}
crtName = rs.getString(1);
+ System.out.println(crtName);
if (rs.getTime(2) != null && rs.getTime(3) == null) {
return CertificateStatus.ISSUED;
}
return CertificateStatus.REVOKED;
}
- public void issue() {
+ public void issue() throws IOException {
try {
if (getStatus() != CertificateStatus.DRAFT) {
throw new IllegalStateException();
PreparedStatement inserter = DatabaseConnection
.getInstance()
.prepare(
- "INSERT INTO emailcerts SET csr_name =?, md=?, subject=?, coll_found=0, crt_name=''");
- inserter.setString(1, csrName);
- inserter.setString(2, md);
- inserter.setString(3, dn);
+ "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();
}
}
- public X509Certificate cert() throws IOException, GeneralSecurityException {
+ 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 {
import java.io.IOException;
import java.io.PrintWriter;
+import java.security.GeneralSecurityException;
+import java.sql.SQLException;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import org.cacert.gigi.Certificate;
import org.cacert.gigi.output.ClientCSRGenerate;
import org.cacert.gigi.pages.Page;
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
HashMap<String, Object> vars = new HashMap<String, Object>();
+ vars.put("CCA",
+ "<a href='/policy/CAcertCommunityAgreement.html'>CCA</a>");
getDefaultTemplate().output(resp.getWriter(), getLanguage(req), vars);
}
@Override
out.println("csr missing");
ClientCSRGenerate.output(req, resp);
}
- out.println("could now start processing the cert request");
+ String csr = req.getParameter("optionalCSR");
+ if (!"on".equals(req.getParameter("CCA"))) {
+ // Error.
+ return;
+ }
+ Certificate c = new Certificate("/commonName=felix@dogcraft.de",
+ "sha256", csr);
+ c.issue();
+ try {
+ c.waitFor(60000);
+ out.println(c.getStatus());
+ out.println(c.cert());
+ } catch (SQLException e1) {
+ e1.printStackTrace();
+ } catch (GeneralSecurityException e) {
+ e.printStackTrace();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
}
}