wolfman
wolfman

Reputation: 185

In a Java HttpsServer, how does Java know what cert to pass back to client?

I have a keystore with a self-signed certificate, created like this:

keytool -genkeypair -keyalg RSA -alias self_signed -keypass mypass -keystore mystore.jks -storepass mypass

And I have a HttpsServer server initialized like this:

    // Initialise the HTTPS server
    HttpsServer httpsServer = HttpsServer.create(address, 0);
    SSLContext sslContext = SSLContext.getInstance("TLS");

    // Initialise the keystore
    String pw = "mypass";
    char[] keyStorePassword = pw.toCharArray();
    char[] keyPassword = pw.toCharArray();
    KeyStore ks = KeyStore.getInstance("JKS");
    File keyStoreFile = new File(Context.APP_STATE.getKeyStoreFile());
    FileInputStream fis = new FileInputStream(keyStoreFile);
    ks.load(fis, keyStorePassword);

    // Set up the key manager factory
    KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
    kmf.init(ks, keyPassword);

    // Set up the trust manager factory
    TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
    tmf.init(ks);

    // Set up the HTTPS context and parameters
    sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
    ...<setting HttpsConfigurator omitted for brevity...

The above code works, but when a browser connects to my server, how does the server know to return the self-signed certificate without an alias having been specified? Is it because when there's only one entry in the keystore it always uses that? What if I had multiple entries in my keystore - how, could the above code be modified to specify a specific entry?

Upvotes: 1

Views: 39

Answers (0)

Related Questions