как использовать хранилище ключей jks для HttpsServer с SNI?

У меня есть HttpsServer, который использует jks хранилище для сертификатов:

    httpsServer = com.sun.net.httpserver.HttpsServer.create();
    httpsServer.bind(new InetSocketAddress(httpsPort), 0);
    httpsServer.createContext("/getVersion", new VersionHandler());

    KeyStore ks = KeyStore.getInstance("JKS");
    KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");

    if (certPath!=null && keyPass !=null && storePass !=null) {
        File initialFile = new File(certPath);

        ks.load(new FileInputStream(initialFile), storePass.toCharArray());
        kmf.init(ks, keyPass.toCharArray());
    } else {
        System.out.println("There are no parameters to run https server. Only the http server is running.");
        return;
    }

    TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
    tmf.init(ks);
    SSLContext ssl = SSLContext.getInstance("TLS");
    ssl.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);

Мне нужно добавить SNI, для этого я добавляю SNIMatcher:

httpsServer.setHttpsConfigurator(new HttpsConfigurator(ssl) {
                public void configure (HttpsParameters params) {
                    SSLContext context = getSSLContext();
                    SSLEngine engine = context.createSSLEngine();
                    params.setNeedClientAuth(false);
                    params.setCipherSuites(engine.getEnabledCipherSuites());
                    params.setProtocols(engine.getEnabledProtocols());

                    // Set SNIMatcher
                    SNIMatcher matcher = SNIHostName.createSNIMatcher("www\\.example\\.(ns.work|ns.ua)");
                    Collection<SNIMatcher> matchers = new ArrayList<>(1);
                    matchers.add(matcher);

                    // Set the SSL parameters
                    SSLParameters sslParameters = context.getSupportedSSLParameters();
                    sslParameters.setSNIMatchers(matchers);
                    params.setSSLParameters(sslParameters);
                }
            });

Не могу понять, как для SNI правильно использовать хранилище сертификатов. Это должно быть два хранилища для сертификатов/ключей разных доменов или один jks? Как будет определяться какой сертификат/ключ использовать?


Ответы (0 шт):