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;
String spkacChallenge;
- String CN = DEFAULT_CN;
+ public String CN = DEFAULT_CN;
Set<SubjectAlternateName> SANs = new LinkedHashSet<>();
boolean login;
+ CertificateProfile profile = CertificateProfile.getById(1);
+
public CertificateIssueForm(HttpServletRequest hsr) {
super(hsr);
u = Page.getUser(hsr);
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
}
}
}
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);
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();
}
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;
}
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;