Skip to content

Commit

Permalink
Better certificate handling when organization is blank (#804)
Browse files Browse the repository at this point in the history
  • Loading branch information
tresf authored May 7, 2021
1 parent 878d156 commit 1b30b37
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/qz/auth/Certificate.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.apache.commons.ssl.Base64;
import org.apache.commons.ssl.X509CertificateChainBuilder;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.x500.style.BCStyle;
import org.bouncycastle.asn1.x509.X509Name;
import org.bouncycastle.jce.PrincipalUtil;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
Expand Down Expand Up @@ -58,6 +59,8 @@ public enum Algorithm {
private static CertPathValidator validator;
private static CertificateFactory factory;
private static boolean trustBuiltIn = false;
// id-at-description used for storing renewal information
private static ASN1ObjectIdentifier RENEWAL_OF = new ASN1ObjectIdentifier("2.5.4.13");

public static final String[] saveFields = new String[] {"fingerprint", "commonName", "organization", "validFrom", "validTo", "valid"};

Expand Down Expand Up @@ -178,7 +181,6 @@ public Certificate(Path path) throws IOException, CertificateException {
}

/** Decodes a certificate and intermediate certificate from the given string */
@SuppressWarnings("deprecation")
public Certificate(String in) throws CertificateException {
try {
//Strip beginning and end
Expand All @@ -195,9 +197,12 @@ public Certificate(String in) throws CertificateException {

//Generate cert
theCertificate = (X509Certificate)factory.generateCertificate(new ByteArrayInputStream(serverCertificate));
commonName = String.valueOf(PrincipalUtil.getSubjectX509Principal(theCertificate).getValues(X509Name.CN).get(0));
commonName = getSubjectX509Principal(theCertificate, BCStyle.CN);
if(commonName.isEmpty()) {
throw new CertificateException("Common Name cannot be blank.");
}
fingerprint = makeThumbPrint(theCertificate);
organization = String.valueOf(PrincipalUtil.getSubjectX509Principal(theCertificate).getValues(X509Name.O).get(0));
organization = getSubjectX509Principal(theCertificate, BCStyle.O);
validFrom = theCertificate.getNotBefore().toInstant();
validTo = theCertificate.getNotAfter().toInstant();

Expand Down Expand Up @@ -269,8 +274,7 @@ public Certificate(String in) throws CertificateException {
}

private void readRenewalInfo() throws Exception {
// "id-at-description" = "2.5.4.13"
Vector values = PrincipalUtil.getSubjectX509Principal(theCertificate).getValues(new ASN1ObjectIdentifier("2.5.4.13"));
Vector values = PrincipalUtil.getSubjectX509Principal(theCertificate).getValues(RENEWAL_OF);
Iterator renewals = values.iterator();

while(renewals.hasNext()) {
Expand Down Expand Up @@ -500,4 +504,16 @@ public static boolean hasAdditionalCAs() {
return rootCAs.size() > (isTrustBuiltIn() ? 1 : 0);
}

private static String getSubjectX509Principal(X509Certificate cert, ASN1ObjectIdentifier key) {
try {
Vector v = PrincipalUtil.getSubjectX509Principal(cert).getValues(key);
if(v.size() > 0) {
return String.valueOf(v.get(0));
}
} catch(CertificateEncodingException e) {
log.warn("Certificate encoding exception occurred", e);
}
return "";
}

}
2 changes: 2 additions & 0 deletions src/qz/ui/SiteManagerDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class SiteManagerDialog extends BasicDialog implements Runnable {
private static final String IMPORT_NEEDED = "The provided certificate \"%s\" is unrecognized and not yet trusted.\n" +
"Would you like to automatically copy it to \"%s\"?";
private static final String IMPORT_FAILED = "Failed to import certificate. Please import manually.";
private static final String INVALID_CERTIFICATE = "An exception occurred importing the certificate. Please check the logs for details.";
private static final String IMPORT_QUESTION = "Successfully created a new demo keypair. Automatically install?";

private static final String DEMO_CERT_QUESTION = "Create a new demo keypair for %s?\n" +
Expand Down Expand Up @@ -449,6 +450,7 @@ private void addCertificates(File[] certFiles, ContainerList<CertificateDisplay>
}
catch(CertificateException | IOException e) {
log.warn("Unable to import cert {}", file, e);
JOptionPane.showMessageDialog(this, String.format(INVALID_CERTIFICATE), "Import failed", JOptionPane.ERROR_MESSAGE);
}
}
}
Expand Down

0 comments on commit 1b30b37

Please sign in to comment.