aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_http_service/src/main/java/com/yahoo/jdisc/http/ssl/pem/PemSslKeyStore.java
blob: fe41d74f692782760142bbc444ad34fa565d4547 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.http.ssl.pem;

import com.yahoo.jdisc.http.ssl.pem.PemKeyStore.KeyStoreLoadParameter;
import com.yahoo.jdisc.http.ssl.pem.PemKeyStore.TrustStoreLoadParameter;

import java.io.IOException;
import java.nio.file.Path;
import java.security.KeyStore;
import java.security.KeyStore.LoadStoreParameter;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.Provider;
import java.security.Security;
import java.security.cert.CertificateException;

/**
 * Responsible for creating pem key stores.
 *
 * @author Tony Vaagenes
 * @author bjorncs
 */
public class PemSslKeyStore {

    static {
        Security.addProvider(new PemKeyStoreProvider());
    }

    private static final String KEY_STORE_TYPE = "PEM";

    private final LoadStoreParameter loadParameter;
    private KeyStore keyStore;

    public PemSslKeyStore(Path certificatePath, Path keyPath) {
        this.loadParameter = new KeyStoreLoadParameter(certificatePath, keyPath);
    }

    public PemSslKeyStore(Path certificatePath) {
        this.loadParameter = new TrustStoreLoadParameter(certificatePath);
    }

    public KeyStore loadJavaKeyStore()
            throws KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException {
        if (keyStore == null) {
            keyStore = KeyStore.getInstance(KEY_STORE_TYPE);
            keyStore.load(loadParameter);
        }
        return keyStore;
    }

    private static class PemKeyStoreProvider extends Provider {

        static final String NAME = "PEMKeyStoreProvider";
        static final double VERSION = 1;
        static final String DESCRIPTION = "Provides PEM keystore support";

        @SuppressWarnings("deprecation") // TODO: Remove annotation and use new super ctor when we don't need Java 8 support anymore.
        PemKeyStoreProvider() {
            super(NAME, VERSION, DESCRIPTION);
            putService(new Service(this, "KeyStore", "PEM", PemKeyStore. class.getName(), PemKeyStore.aliases, PemKeyStore.attributes));
        }
    }

}