private static boolean keyStoreContainsCertificate(KeyStore ks, String hostname) throws Exception { SubjectDnX509PrincipalExtractor ex = new SubjectDnX509PrincipalExtractor(); Enumeration<String> e = ks.aliases(); while (e.hasMoreElements()) { String alias = e.nextElement(); if (ks.isCertificateEntry(alias)) { Certificate c = ks.getCertificate(alias); if (c instanceof X509Certificate) { X500Principal p = (X500Principal) ((X509Certificate) c) .getSubjectX500Principal(); if (p.getName().contains(hostname)) return true; } } } return false; }