/** Returns a trust manager that trusts {@code trustedCertificates}. */ public static X509TrustManager newTrustManager(String keyStoreType, List<X509Certificate> trustedCertificates) throws GeneralSecurityException { KeyStore trustStore = newEmptyKeyStore(keyStoreType); for (int i = 0; i < trustedCertificates.size(); i++) { trustStore.setCertificateEntry("cert_" + i, trustedCertificates.get(i)); } TrustManagerFactory factory = TrustManagerFactory.getInstance( TrustManagerFactory.getDefaultAlgorithm()); factory.init(trustStore); TrustManager[] result = factory.getTrustManagers(); if (result.length != 1 || !(result[0] instanceof X509TrustManager)) { throw new IllegalStateException("Unexpected trust managers:" + Arrays.toString(result)); } return (X509TrustManager) result[0]; }
/** * Returns a key manager for the held certificate and its chain. Returns an empty key manager if * {@code heldCertificate} is null. */ public static X509KeyManager newKeyManager(String keyStoreType, HeldCertificate heldCertificate, X509Certificate... intermediates) throws GeneralSecurityException { KeyStore keyStore = newEmptyKeyStore(keyStoreType); if (heldCertificate != null) { Certificate[] chain = new Certificate[1 + intermediates.length]; chain[0] = heldCertificate.certificate(); System.arraycopy(intermediates, 0, chain, 1, intermediates.length); keyStore.setKeyEntry("private", heldCertificate.keyPair().getPrivate(), password, chain); } KeyManagerFactory factory = KeyManagerFactory.getInstance( KeyManagerFactory.getDefaultAlgorithm()); factory.init(keyStore, password); KeyManager[] result = factory.getKeyManagers(); if (result.length != 1 || !(result[0] instanceof X509KeyManager)) { throw new IllegalStateException("Unexpected key managers:" + Arrays.toString(result)); } return (X509KeyManager) result[0]; }
/** Returns a trust manager that trusts {@code trustedCertificates}. */ public static X509TrustManager newTrustManager(String keyStoreType, List<X509Certificate> trustedCertificates) throws GeneralSecurityException { KeyStore trustStore = newEmptyKeyStore(keyStoreType); for (int i = 0; i < trustedCertificates.size(); i++) { trustStore.setCertificateEntry("cert_" + i, trustedCertificates.get(i)); } TrustManagerFactory factory = TrustManagerFactory.getInstance( TrustManagerFactory.getDefaultAlgorithm()); factory.init(trustStore); TrustManager[] result = factory.getTrustManagers(); if (result.length != 1 || !(result[0] instanceof X509TrustManager)) { throw new IllegalStateException("Unexpected trust managers:" + Arrays.toString(result)); } return (X509TrustManager) result[0]; }
/** * Returns a key manager for the held certificate and its chain. Returns an empty key manager if * {@code heldCertificate} is null. */ public static X509KeyManager newKeyManager(String keyStoreType, HeldCertificate heldCertificate, X509Certificate... intermediates) throws GeneralSecurityException { KeyStore keyStore = newEmptyKeyStore(keyStoreType); if (heldCertificate != null) { Certificate[] chain = new Certificate[1 + intermediates.length]; chain[0] = heldCertificate.certificate(); System.arraycopy(intermediates, 0, chain, 1, intermediates.length); keyStore.setKeyEntry("private", heldCertificate.keyPair().getPrivate(), password, chain); } KeyManagerFactory factory = KeyManagerFactory.getInstance( KeyManagerFactory.getDefaultAlgorithm()); factory.init(keyStore, password); KeyManager[] result = factory.getKeyManagers(); if (result.length != 1 || !(result[0] instanceof X509KeyManager)) { throw new IllegalStateException("Unexpected key managers:" + Arrays.toString(result)); } return (X509KeyManager) result[0]; }