javakeystoretruststorejava-securitysslcontext

How to retrieve keyManager details from SSLContext?


I would like to retrieve credentialMap belongs to keyManager from sslContext object.

 public static void main(String[] args) throws UnrecoverableKeyException, CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException, NoSuchProviderException, KeyManagementException {
        SSLContext sslContext = newServerContext(createKeyManagers());
        // how to get keyManager from sslContext?
    }

public static SSLContext newServerContext(KeyManager[] keyManagers)
            throws NoSuchAlgorithmException, NoSuchProviderException,
            KeyManagementException {
        SSLContext ctx = SSLContext.getInstance("TLS");
        ctx.init(keyManagers, null, new SecureRandom());
        return ctx;
    }

public static KeyManager[] createKeyManagers()
            throws KeyStoreException, IOException, NoSuchAlgorithmException,
            CertificateException, UnrecoverableKeyException
    {
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        FileInputStream keyStoreFile = new FileInputStream("F:\\IdeaProjects\\NewFeature\\keystore.jks");
        String keyStorePassword = "changeit";
        keyStore.load(keyStoreFile, keyStorePassword.toCharArray());

        KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmf.init(keyStore, keyStorePassword.toCharArray());
        return kmf.getKeyManagers();
    }

How can I retrieve keyManager details from ctx(SSLContext) object?


Solution

  • It seems you cannot get the KeyManager array through some methods of SSLContext. Why don't you hold the KeyManager array in a variable?

    KeyManager[] keyManagers = createKeyManagers();
    SSLContext sslContext = newServerContext(keyManagers);
    

    By the way, why do you want to obtain the KeyManager[] through SSLContext? What are you going to do with it? Where are you going to use it? The SSLContext acts as a factory for secure socket factories or SSLEngines. The KeyManager should be internally used by SSLServerSocketFactory or SSLEngine.