aboutsummaryrefslogtreecommitdiffstats
path: root/jrt/src/com/yahoo/jrt/TlsCryptoEngine.java
blob: bce93eb4537c37802622eef0d83cd3a8bc9331a5 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jrt;

import com.yahoo.security.tls.TlsContext;

import javax.net.ssl.SSLEngine;
import java.nio.channels.SocketChannel;

/**
 * A {@link CryptoSocket} that creates {@link TlsCryptoSocket} instances.
 *
 * @author bjorncs
 */
public class TlsCryptoEngine implements CryptoEngine {

    private final TlsContext tlsContext;

    public TlsCryptoEngine(TlsContext tlsContext) {
        this.tlsContext = tlsContext;
    }

    @Override
    public TlsCryptoSocket createClientCryptoSocket(SocketChannel channel, Spec spec)  {
        String peerHost = spec.host() != null ? spec.host() : "localhost"; // Use localhost for wildcard address
        SSLEngine sslEngine = tlsContext.createSslEngine(peerHost, spec.port());
        sslEngine.setUseClientMode(true);
        return new TlsCryptoSocket(channel, sslEngine);
    }

    @Override
    public TlsCryptoSocket createServerCryptoSocket(SocketChannel channel)  {
        SSLEngine sslEngine = tlsContext.createSslEngine();
        sslEngine.setUseClientMode(false);
        return new TlsCryptoSocket(channel, sslEngine);
    }

    @Override
    public void close() {
        tlsContext.close();
    }

}