aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/main/java/com/yahoo/jdisc/http/ssl/impl/DefaultConnectorSsl.java
blob: 5b7f5cb1c0282105d1a001a7ebae1d7639dcdf53 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright Vespa.ai. 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.jdisc.http.SslProvider;
import org.eclipse.jetty.util.ssl.SslContextFactory;

import javax.net.ssl.SSLContext;
import java.security.KeyStore;
import java.util.List;

/**
 * Default implementation of {@link SslProvider} backed by {@link SslContextFactory.Server}
 *
 * @author bjorncs
 */
public class DefaultConnectorSsl implements SslProvider.ConnectorSsl {

    private SSLContext sslContext;
    private ClientAuth clientAuth;
    private List<String> cipherSuites = List.of();
    private List<String> protocolVersions = List.of();
    private KeyStore keystore;
    private char[] keystorePassword;
    private KeyStore truststore;
    private char[] truststorePassword;

    @Override
    public SslProvider.ConnectorSsl setSslContext(SSLContext ctx) {
        this.sslContext = ctx; return this;
    }

    @Override
    public SslProvider.ConnectorSsl setClientAuth(SslProvider.ConnectorSsl.ClientAuth auth) {
        this.clientAuth = auth; return this;
    }

    @Override
    public SslProvider.ConnectorSsl setEnabledCipherSuites(List<String> ciphers) {
        this.cipherSuites = ciphers; return this;
    }

    @Override
    public SslProvider.ConnectorSsl setEnabledProtocolVersions(List<String> versions) {
        this.protocolVersions = versions; return this;
    }

    @Override
    public SslProvider.ConnectorSsl setKeystore(KeyStore keystore, char[] password) {
        this.keystore = keystore; this.keystorePassword = password; return this;
    }

    @Override
    public SslProvider.ConnectorSsl setKeystore(KeyStore keystore) {
        this.keystore = keystore; return this;
    }

    @Override
    public SslProvider.ConnectorSsl setTruststore(KeyStore truststore, char[] password) {
        this.truststore = truststore; this.truststorePassword = password; return this;
    }

    @Override
    public SslProvider.ConnectorSsl setTruststore(KeyStore truststore) {
        this.truststore = truststore; return this;
    }

    public SslContextFactory.Server createSslContextFactory() {
        SslContextFactory.Server ssl = new SslContextFactory.Server();
        if (sslContext != null) ssl.setSslContext(sslContext);
        if (keystore != null) ssl.setKeyStore(keystore);
        if (keystorePassword != null) ssl.setKeyStorePassword(new String(keystorePassword));
        if (truststore != null) ssl.setTrustStore(truststore);
        if (truststorePassword != null) ssl.setTrustStorePassword(new String(truststorePassword));
        switch (clientAuth) {
            case DISABLED:
                ssl.setWantClientAuth(false);
                ssl.setNeedClientAuth(false);
                break;
            case NEED:
                ssl.setWantClientAuth(false);
                ssl.setNeedClientAuth(true);
                break;
            case WANT:
                ssl.setWantClientAuth(true);
                ssl.setNeedClientAuth(false);
                break;
            default:
                throw new IllegalArgumentException(clientAuth.name());
        }
        if (!cipherSuites.isEmpty()) SslContextFactoryUtils.setEnabledCipherSuites(ssl, sslContext, cipherSuites);
        if (!protocolVersions.isEmpty()) SslContextFactoryUtils.setEnabledProtocols(ssl, sslContext, protocolVersions);
        return ssl;
    }
}