aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_http_service/src/main/java/com/yahoo/jdisc/http/ssl/JKSKeyStore.java
blob: ce3c6f6ea4afb720bec671e2de68924a38083806 (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
// 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;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;

/**
 * @author tonytv
 */
public class JKSKeyStore extends SslKeyStore {

    private static final String keyStoreType = "JKS";
    private final Path keyStoreFile;

    public JKSKeyStore(Path keyStoreFile) {
        this.keyStoreFile = keyStoreFile;
    }

    @Override
    public KeyStore loadJavaKeyStore() throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException {
        try(InputStream stream = Files.newInputStream(keyStoreFile)) {
            KeyStore keystore = KeyStore.getInstance(keyStoreType);
            keystore.load(stream, getKeyStorePassword().map(String::toCharArray).orElse(null));
            return keystore;
        }
    }

}