aboutsummaryrefslogtreecommitdiffstats
path: root/security-utils/src/test/java/com/yahoo/security/tls/ConfigFileBasedTlsContextTest.java
blob: 7b70c842a4c7ac6d805c742e29febcdb342a9009 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.security.tls;

import com.yahoo.security.KeyUtils;
import com.yahoo.security.X509CertificateBuilder;
import com.yahoo.security.X509CertificateUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import javax.net.ssl.SSLEngine;
import javax.security.auth.x500.X500Principal;

import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.KeyPair;
import java.security.cert.X509Certificate;

import static com.yahoo.security.KeyAlgorithm.EC;
import static com.yahoo.security.SignatureAlgorithm.SHA256_WITH_ECDSA;
import static java.time.Instant.EPOCH;
import static java.time.temporal.ChronoUnit.DAYS;
import static org.assertj.core.api.Assertions.assertThat;

/**
 * @author bjorncs
 */
public class ConfigFileBasedTlsContextTest {

    @TempDir
    public File tempDirectory;

    @Test
    void can_create_sslcontext_from_credentials() throws IOException, InterruptedException {
        KeyPair keyPair = KeyUtils.generateKeypair(EC);
        Path privateKeyFile = File.createTempFile("junit", null, tempDirectory).toPath();
        Files.write(privateKeyFile, KeyUtils.toPem(keyPair.getPrivate()).getBytes());

        X509Certificate certificate = X509CertificateBuilder
                .fromKeypair(keyPair, new X500Principal("CN=dummy"), EPOCH, EPOCH.plus(1, DAYS), SHA256_WITH_ECDSA, BigInteger.ONE)
                .build();
        Path certificateChainFile = File.createTempFile("junit", null, tempDirectory).toPath();
        String certificatePem = X509CertificateUtils.toPem(certificate);
        Files.write(certificateChainFile, certificatePem.getBytes());

        Path caCertificatesFile = File.createTempFile("junit", null, tempDirectory).toPath();
        Files.write(caCertificatesFile, certificatePem.getBytes());

        TransportSecurityOptions options = new TransportSecurityOptions.Builder()
                .withCertificates(certificateChainFile, privateKeyFile)
                .withCaCertificates(caCertificatesFile)
                .build();

        Path optionsFile = File.createTempFile("junit", null, tempDirectory).toPath();
        options.toJsonFile(optionsFile);

        try (TlsContext tlsContext = new ConfigFileBasedTlsContext(optionsFile, AuthorizationMode.ENFORCE)) {
            SSLEngine sslEngine = tlsContext.createSslEngine();
            assertThat(sslEngine).isNotNull();
            String[] enabledCiphers = sslEngine.getEnabledCipherSuites();
            assertThat(enabledCiphers).isNotEmpty();
            assertThat(enabledCiphers).isSubsetOf(TlsContext.ALLOWED_CIPHER_SUITES.toArray(new String[0]));

            String[] enabledProtocols = sslEngine.getEnabledProtocols();
            assertThat(enabledProtocols).contains("TLSv1.2");
        }
    }

}