/** * 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 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]; }