public int compare(Object o1, Object o2) { X509Certificate c1 = (X509Certificate) o1; X509Certificate c2 = (X509Certificate) o2; if (c1 == c2) // this deals with case where both are null { return 0; } if (c1 == null) // non-null is always bigger than null { return -1; } if (c2 == null) { return 1; } if (c1.equals(c2)) { return 0; } Date d1 = c1.getNotAfter(); Date d2 = c2.getNotAfter(); int c = d1.compareTo(d2); if (c == 0) { String s1 = JavaImpl.getSubjectX500(c1); String s2 = JavaImpl.getSubjectX500(c2); c = s1.compareTo(s2); if (c == 0) { s1 = JavaImpl.getIssuerX500(c1); s2 = JavaImpl.getIssuerX500(c2); c = s1.compareTo(s2); if (c == 0) { BigInteger big1 = c1.getSerialNumber(); BigInteger big2 = c2.getSerialNumber(); c = big1.compareTo(big2); if (c == 0) { try { byte[] b1 = c1.getEncoded(); byte[] b2 = c2.getEncoded(); int len1 = b1.length; int len2 = b2.length; int i = 0; for (; i < len1 && i < len2; i++) { c = ((int) b1[i]) - ((int) b2[i]); if (c != 0) { break; } } if (c == 0) { c = b1.length - b2.length; } } catch (CertificateEncodingException cee) { // I give up. They can be equal if they // really want to be this badly. c = 0; } } } } } return c; }
public static BigInteger getFingerprint(byte[] x509) throws CertificateEncodingException { MessageDigest sha1; try { sha1 = MessageDigest.getInstance("SHA1"); } catch (NoSuchAlgorithmException nsae) { throw JavaImpl.newRuntimeException(nsae); } sha1.reset(); byte[] result = sha1.digest(x509); return new BigInteger(result); }
public static String toString(X509Certificate cert, boolean htmlStyle) { String cn = getCN(cert); String startStart = DF.format(cert.getNotBefore()); String endDate = DF.format(cert.getNotAfter()); String subject = JavaImpl.getSubjectX500(cert); String issuer = JavaImpl.getIssuerX500(cert); Iterator crls = getCRLs(cert).iterator(); if (subject.equals(issuer)) { issuer = "self-signed"; } StringBuffer buf = new StringBuffer(128); if (htmlStyle) { buf.append("<strong class=\"cn\">"); } buf.append(cn); if (htmlStyle) { buf.append("</strong>"); } buf.append(LINE_ENDING); buf.append("Valid: "); buf.append(startStart); buf.append(" - "); buf.append(endDate); buf.append(LINE_ENDING); buf.append("s: "); buf.append(subject); buf.append(LINE_ENDING); buf.append("i: "); buf.append(issuer); while (crls.hasNext()) { buf.append(LINE_ENDING); buf.append("CRL: "); buf.append((String) crls.next()); } buf.append(LINE_ENDING); return buf.toString(); }