aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/main/java/com/yahoo/jdisc/http/ssl/impl/SslContextFactoryUtils.java
blob: e7c9e4f0bee4d58e07549c3111fbe124d70d1294 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.http.ssl.impl;

import org.eclipse.jetty.util.ssl.SslContextFactory;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLParameters;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.List;

/**
 * @author bjorncs
 */
class SslContextFactoryUtils {

    static void setEnabledCipherSuites(SslContextFactory factory, SSLContext sslContextOrNull, List<String> enabledCiphers) {
        String[] supportedCiphers = supportedSslParams(sslContextOrNull).getCipherSuites();
        factory.setIncludeCipherSuites(enabledCiphers.toArray(String[]::new));
        factory.setExcludeCipherSuites(createExclusionList(enabledCiphers, supportedCiphers));
    }

    static void setEnabledProtocols(SslContextFactory factory, SSLContext sslContextOrNull, List<String> enabledProtocols) {
        String[] supportedProtocols = supportedSslParams(sslContextOrNull).getProtocols();
        factory.setIncludeProtocols(enabledProtocols.toArray(String[]::new));
        factory.setExcludeProtocols(createExclusionList(enabledProtocols, supportedProtocols));
    }

    private static String[] createExclusionList(List<String> enabledValues, String[] supportedValues) {
        return Arrays.stream(supportedValues)
                .filter(supportedValue -> !enabledValues.contains(supportedValue))
                .toArray(String[]::new);
    }

    private static SSLParameters supportedSslParams(SSLContext ctx) {
        try {
            return ctx != null
                    ? ctx.getSupportedSSLParameters()
                    : SSLContext.getDefault().getSupportedSSLParameters();
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalStateException(e);
        }
    }
}