summaryrefslogtreecommitdiffstats
path: root/jdisc_http_service/src/main/java/com/yahoo/jdisc/http/ssl/impl/TlsContextBasedProvider.java
blob: e8ae13e48be70bc62d090d88c919a0c87ef72e45 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.http.ssl.impl;

import com.yahoo.component.AbstractComponent;
import com.yahoo.jdisc.http.ssl.SslContextFactoryProvider;
import com.yahoo.security.tls.TlsContext;
import org.eclipse.jetty.util.ssl.SslContextFactory;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLParameters;
import java.util.Arrays;

/**
 * A {@link SslContextFactoryProvider} that creates {@link SslContextFactory} instances from {@link TlsContext} instances.
 *
 * @author bjorncs
 */
public abstract class TlsContextBasedProvider extends AbstractComponent implements SslContextFactoryProvider {

    protected abstract TlsContext getTlsContext(String containerId, int port);

    @Override
    public final SslContextFactory getInstance(String containerId, int port) {
        TlsContext tlsContext = getTlsContext(containerId, port);
        SSLContext sslContext = tlsContext.context();
        SSLParameters parameters = tlsContext.parameters();

        SslContextFactory.Server sslContextFactory = new SslContextFactory.Server();
        sslContextFactory.setSslContext(sslContext);

        sslContextFactory.setNeedClientAuth(parameters.getNeedClientAuth());
        sslContextFactory.setWantClientAuth(parameters.getWantClientAuth());

        String[] enabledProtocols = parameters.getProtocols();
        sslContextFactory.setIncludeProtocols(enabledProtocols);
        String[] supportedProtocols = sslContext.getSupportedSSLParameters().getProtocols();
        sslContextFactory.setExcludeProtocols(createExclusionList(enabledProtocols, supportedProtocols));

        String[] enabledCiphers = parameters.getCipherSuites();
        String[] supportedCiphers = sslContext.getSupportedSSLParameters().getCipherSuites();
        sslContextFactory.setIncludeCipherSuites(enabledCiphers);
        sslContextFactory.setExcludeCipherSuites(createExclusionList(enabledCiphers, supportedCiphers));
        return sslContextFactory;
    }

    private static String[] createExclusionList(String[] enabledValues, String[] supportedValues) {
        return Arrays.stream(supportedValues)
                .filter(supportedValue ->
                                Arrays.stream(enabledValues)
                                        .noneMatch(enabledValue -> enabledValue.equals(supportedValue)))
                .toArray(String[]::new);
    }

}