aboutsummaryrefslogtreecommitdiffstats
path: root/vespa-athenz/src/test/java/com/yahoo/vespa/athenz/tls/AthenzSslContextBuilderTest.java
blob: 5aca1fc3116fd5e07222a365ecb0d98d50726b59 (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
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.athenz.tls;

import com.yahoo.athenz.auth.util.Crypto;
import org.bouncycastle.asn1.x500.X500Name;
import org.bouncycastle.operator.OperatorCreationException;
import org.bouncycastle.pkcs.PKCS10CertificationRequest;
import org.junit.Test;

import java.io.IOException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.KeyStore;
import java.security.NoSuchAlgorithmException;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;

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

    private static final char[] PASSWORD = new char[0];

    @Test
    public void can_build_sslcontext_with_truststore_only() throws Exception {
        new AthenzSslContextBuilder()
                .withTrustStore(createKeystore())
                .build();
    }

    @Test
    public void can_build_sslcontext_with_keystore_only() throws Exception {
        new AthenzSslContextBuilder()
                .withKeyStore(createKeystore(), PASSWORD)
                .build();
    }

    @Test
    public void can_build_sslcontext_with_truststore_and_keystore() throws Exception {
        new AthenzSslContextBuilder()
                .withKeyStore(createKeystore(), PASSWORD)
                .withTrustStore(createKeystore())
                .build();
    }

    private static KeyStore createKeystore() throws Exception {
        KeyPair keyPair = createKeyPair();
        KeyStore keystore = KeyStore.getInstance("JKS");
        keystore.load(null);
        keystore.setKeyEntry("entry-name", keyPair.getPrivate(), PASSWORD, new Certificate[]{createCertificate(keyPair)});
        return keystore;
    }

    private static X509Certificate createCertificate(KeyPair keyPair) throws
            OperatorCreationException, IOException {
        String x500Principal = "CN=mysubject";
        PKCS10CertificationRequest csr =
                Crypto.getPKCS10CertRequest(
                        Crypto.generateX509CSR(keyPair.getPrivate(), x500Principal, null));
        return Crypto.generateX509Certificate(csr, keyPair.getPrivate(), new X500Name(x500Principal), 3600, false);
    }

    private static KeyPair createKeyPair() throws NoSuchAlgorithmException {
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
        keyGen.initialize(512);
        return keyGen.genKeyPair();
    }
}