]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/pages/account/CertificateIssueForm.java
Merge branch 'issuePeriod'
[gigi.git] / src / org / cacert / gigi / pages / account / CertificateIssueForm.java
index 3f5c1e5c42bdf80ac24b7a31576ee5f75416906c..ff659225976a77a21106597e1ea23cf4e8655382 100644 (file)
@@ -27,6 +27,7 @@ import org.cacert.gigi.GigiApiException;
 import org.cacert.gigi.User;
 import org.cacert.gigi.crypto.SPKAC;
 import org.cacert.gigi.localisation.Language;
+import org.cacert.gigi.output.CertificateValiditySelector;
 import org.cacert.gigi.output.Form;
 import org.cacert.gigi.output.template.HashAlgorithms;
 import org.cacert.gigi.output.template.IterableDataset;
@@ -42,6 +43,7 @@ import sun.security.pkcs10.PKCS10Attribute;
 import sun.security.pkcs10.PKCS10Attributes;
 import sun.security.util.DerInputStream;
 import sun.security.util.DerValue;
+import sun.security.util.ObjectIdentifier;
 import sun.security.x509.AVA;
 import sun.security.x509.AlgorithmId;
 import sun.security.x509.CertificateExtensions;
@@ -51,6 +53,7 @@ import sun.security.x509.Extension;
 import sun.security.x509.GeneralName;
 import sun.security.x509.GeneralNameInterface;
 import sun.security.x509.GeneralNames;
+import sun.security.x509.PKIXExtensions;
 import sun.security.x509.RDN;
 import sun.security.x509.RFC822Name;
 import sun.security.x509.SubjectAlternativeNameExtension;
@@ -62,27 +65,55 @@ import sun.security.x509.X500Name;
  */
 public class CertificateIssueForm extends Form {
 
-    private static final String DEFAULT_CN = "CAcert WoT User";
+    public static final String DEFAULT_CN = "CAcert WoT User";
 
     private final static Template t = new Template(CertificateIssueForm.class.getResource("CertificateIssueForm.templ"));
 
     private final static Template tIni = new Template(CertificateAdd.class.getResource("RequestCertificate.templ"));
 
-    User u;
+    public static final ObjectIdentifier OID_KEY_USAGE_SSL_SERVER = ObjectIdentifier.newInternal(new int[] {
+            1, 3, 6, 1, 5, 5, 7, 3, 1
+    });
+
+    public static final ObjectIdentifier OID_KEY_USAGE_SSL_CLIENT = ObjectIdentifier.newInternal(new int[] {
+            1, 3, 6, 1, 5, 5, 7, 3, 2
+    });
+
+    public static final ObjectIdentifier OID_KEY_USAGE_CODESIGN = ObjectIdentifier.newInternal(new int[] {
+            1, 3, 6, 1, 5, 5, 7, 3, 3
+    });
+
+    public static final ObjectIdentifier OID_KEY_USAGE_EMAIL_PROTECTION = ObjectIdentifier.newInternal(new int[] {
+            1, 3, 6, 1, 5, 5, 7, 3, 4
+    });
+
+    public static final ObjectIdentifier OID_KEY_USAGE_TIMESTAMP = ObjectIdentifier.newInternal(new int[] {
+            1, 3, 6, 1, 5, 5, 7, 3, 8
+    });
+
+    public static final ObjectIdentifier OID_KEY_USAGE_OCSP = ObjectIdentifier.newInternal(new int[] {
+            1, 3, 6, 1, 5, 5, 7, 3, 9
+    });
+
+    private User u;
 
     private CSRType csrType;
 
-    String csr;
+    private String csr;
+
+    private String spkacChallenge;
 
-    String spkacChallenge;
+    public String CN = DEFAULT_CN;
 
-    String CN = DEFAULT_CN;
+    private Set<SubjectAlternateName> SANs = new LinkedHashSet<>();
 
-    Set<SubjectAlternateName> SANs = new LinkedHashSet<>();
+    private Digest selectedDigest = Digest.getDefault();
 
-    Digest selectedDigest = Digest.getDefault();
+    CertificateValiditySelector issueDate = new CertificateValiditySelector();
 
-    boolean login;
+    private boolean login;
+
+    private CertificateProfile profile = CertificateProfile.getById(1);
 
     public CertificateIssueForm(HttpServletRequest hsr) {
         super(hsr);
@@ -90,7 +121,7 @@ public class CertificateIssueForm extends Form {
         spkacChallenge = RandomToken.generateToken(16);
     }
 
-    Certificate result;
+    private Certificate result;
 
     public Certificate getResult() {
         return result;
@@ -119,7 +150,14 @@ public class CertificateIssueForm extends Form {
                                 if (a.getObjectIdentifier().equals((Object) PKCS9Attribute.EMAIL_ADDRESS_OID)) {
                                     SANs.add(new SubjectAlternateName(SANType.EMAIL, a.getValueString()));
                                 } else if (a.getObjectIdentifier().equals((Object) X500Name.commonName_oid)) {
-                                    CN = a.getValueString();
+                                    String value = a.getValueString();
+                                    if (value.contains(".") && !value.contains(" ")) {
+                                        SANs.add(new SubjectAlternateName(SANType.DNS, value));
+                                    } else {
+                                        CN = value;
+                                    }
+                                } else if (a.getObjectIdentifier().equals((Object) PKIXExtensions.SubjectAlternativeName_Id)) {
+                                    // parse invalid SANs
                                 }
                             }
                         }
@@ -141,17 +179,20 @@ public class CertificateIssueForm extends Form {
                             } else if (c instanceof ExtendedKeyUsageExtension) {
                                 ExtendedKeyUsageExtension ekue = (ExtendedKeyUsageExtension) c;
                                 for (String s : ekue.getExtendedKeyUsage()) {
-                                    if (s.equals("1.3.6.1.5.5.7.3.1")) {
+                                    if (s.equals(OID_KEY_USAGE_SSL_SERVER.toString())) {
                                         // server
-                                    } else if (s.equals("1.3.6.1.5.5.7.3.2")) {
+                                        profile = CertificateProfile.getByName("server");
+                                    } else if (s.equals(OID_KEY_USAGE_SSL_CLIENT.toString())) {
                                         // client
-                                    } else if (s.equals("1.3.6.1.5.5.7.3.3")) {
+                                        profile = CertificateProfile.getByName("client");
+                                    } else if (s.equals(OID_KEY_USAGE_CODESIGN.toString())) {
                                         // code sign
-                                    } else if (s.equals("1.3.6.1.5.5.7.3.4")) {
+                                    } else if (s.equals(OID_KEY_USAGE_EMAIL_PROTECTION.toString())) {
                                         // emailProtection
-                                    } else if (s.equals("1.3.6.1.5.5.7.3.8")) {
+                                        profile = CertificateProfile.getByName("mail");
+                                    } else if (s.equals(OID_KEY_USAGE_TIMESTAMP.toString())) {
                                         // timestamp
-                                    } else if (s.equals("1.3.6.1.5.5.7.3.9")) {
+                                    } else if (s.equals(OID_KEY_USAGE_OCSP.toString())) {
                                         // OCSP
                                     }
                                 }
@@ -168,6 +209,8 @@ public class CertificateIssueForm extends Form {
                     PublicKey pk = parsed.getSubjectPublicKeyInfo();
                     checkKeyStrength(pk, out);
                     String sign = getSignatureAlgorithm(data);
+                    guessDigest(sign);
+
                     out.println("<br/>digest: " + sign + "<br/>");
 
                     this.csr = csr;
@@ -181,6 +224,7 @@ public class CertificateIssueForm extends Form {
                     }
                     checkKeyStrength(parsed.getPubkey(), out);
                     String sign = getSignatureAlgorithm(data);
+                    guessDigest(sign);
                     out.println("<br/>digest: " + sign + "<br/>");
 
                     // spkacChallenge
@@ -189,45 +233,83 @@ public class CertificateIssueForm extends Form {
 
                 } else {
                     login = "1".equals(req.getParameter("login"));
+                    issueDate.update(req);
                     CN = req.getParameter("CN");
                     String hashAlg = req.getParameter("hash_alg");
                     if (hashAlg != null) {
                         selectedDigest = Digest.valueOf(hashAlg);
                     }
-                    CertificateProfile profile = CertificateProfile.getByName(req.getParameter("profile"));
+                    profile = CertificateProfile.getByName(req.getParameter("profile"));
 
+                    String pDNS = null;
+                    String pMail = null;
                     Set<SubjectAlternateName> filteredSANs = new LinkedHashSet<>();
+                    boolean server = profile.getKeyName().equals("server");
                     for (SubjectAlternateName san : parseSANBox(req.getParameter("SANs"))) {
                         if (san.getType() == SANType.DNS) {
-                            if (u.isValidDomain(san.getName())) {
+                            if (u.isValidDomain(san.getName()) && server) {
+                                if (pDNS == null) {
+                                    pDNS = san.getName();
+                                }
                                 filteredSANs.add(san);
                                 continue;
                             }
                         } else if (san.getType() == SANType.EMAIL) {
-                            if (u.isValidEmail(san.getName())) {
+                            if (u.isValidEmail(san.getName()) && !server) {
+                                if (pMail == null) {
+                                    pMail = san.getName();
+                                }
                                 filteredSANs.add(san);
                                 continue;
                             }
                         }
-                        // SAN blocked
+                        outputError(out, req, "The requested Subject alternate name \"%s\" has been removed.",//
+                                san.getType().toString().toLowerCase() + ":" + san.getName());
                     }
                     SANs = filteredSANs;
+                    if ( !u.isValidName(CN) && !server && !CN.equals(DEFAULT_CN)) {
+                        CN = DEFAULT_CN;
+                        outputError(out, req, "The real name entered cannot be verified with your account.");
+                    }
 
+                    final StringBuffer subject = new StringBuffer();
+                    if (server && pDNS != null) {
+                        subject.append("/commonName=");
+                        subject.append(pDNS);
+                        if (pMail != null) {
+                            outputError(out, req, "No email is included in this certificate.");
+                        }
+                        if (CN.equals("")) {
+                            CN = "";
+                            outputError(out, req, "No real name is included in this certificate.");
+                        }
+                    } else {
+                        subject.append("/commonName=");
+                        subject.append(CN);
+                        if (pMail != null) {
+                            subject.append("/emailAddress=");
+                            subject.append(pMail);
+                        }
+                    }
                     if (req.getParameter("CCA") == null) {
                         outputError(out, req, "You need to accept the CCA.");
+                    }
+                    if (isFailed(out)) {
                         return false;
                     }
 
-                    result = new Certificate(LoginPage.getUser(req).getId(), "/commonName=CAcert WoT User", selectedDigest.toString(), //
+                    result = new Certificate(LoginPage.getUser(req).getId(), subject.toString(), selectedDigest.toString(), //
                             this.csr, this.csrType, profile, SANs.toArray(new SubjectAlternateName[SANs.size()]));
-                    result.issue().waitFor(60000);
+                    result.issue(issueDate.getFrom(), issueDate.getTo()).waitFor(60000);
                     return true;
                 }
             } catch (IOException e) {
                 e.printStackTrace();
             } catch (IllegalArgumentException e) {
+                e.printStackTrace();
                 throw new GigiApiException("Certificate Request format is invalid.");
             } catch (GeneralSecurityException e) {
+                e.printStackTrace();
                 throw new GigiApiException("Certificate Request format is invalid.");
             } catch (InterruptedException e) {
                 e.printStackTrace();
@@ -240,16 +322,40 @@ public class CertificateIssueForm extends Form {
         return false;
     }
 
+    private void guessDigest(String sign) {
+        if (sign.toLowerCase().startsWith("sha512")) {
+            selectedDigest = Digest.SHA512;
+        } else if (sign.toLowerCase().startsWith("sha384")) {
+            selectedDigest = Digest.SHA384;
+        }
+    }
+
     private TreeSet<SubjectAlternateName> parseSANBox(String SANs) {
-        String[] SANparts = SANs.split("[\r\n]+");
+        String[] SANparts = SANs.split("[\r\n]+|, *");
         TreeSet<SubjectAlternateName> parsedNames = new TreeSet<>();
         for (String SANline : SANparts) {
             String[] parts = SANline.split(":", 2);
-            SANType t = Certificate.SANType.valueOf(parts[0].toUpperCase());
-            if (t == null || parts.length == 1) {
+            if (parts.length == 1) {
+                if (parts[0].trim().equals("")) {
+                    continue;
+                }
+                if (parts[0].contains("@")) {
+                    parsedNames.add(new SubjectAlternateName(SANType.EMAIL, parts[0]));
+                } else {
+                    parsedNames.add(new SubjectAlternateName(SANType.DNS, parts[0]));
+                }
+                continue;
+            }
+            try {
+                SANType t = Certificate.SANType.valueOf(parts[0].toUpperCase());
+                if (t == null) {
+                    continue;
+                }
+                parsedNames.add(new SubjectAlternateName(t, parts[1]));
+            } catch (IllegalArgumentException e) {
+                // invalid enum type
                 continue;
             }
-            parsedNames.add(new SubjectAlternateName(t, parts[1]));
         }
         return parsedNames;
     }
@@ -305,6 +411,7 @@ public class CertificateIssueForm extends Form {
         }
 
         vars2.put("CN", CN);
+        vars2.put("validity", issueDate);
         vars2.put("emails", content.toString());
         vars2.put("hashs", new HashAlgorithms(selectedDigest));
         vars2.put("profiles", new IterableDataset() {
@@ -317,6 +424,11 @@ public class CertificateIssueForm extends Form {
                 if (cp == null) {
                     return false;
                 }
+                if (cp.getId() == profile.getId()) {
+                    vars.put("selected", " selected");
+                } else {
+                    vars.put("selected", "");
+                }
                 vars.put("key", cp.getKeyName());
                 vars.put("name", cp.getVisibleName());
                 return true;