• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 package software.amazon.awssdk.internal.http;
17 
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.nio.file.Files;
21 import java.nio.file.Path;
22 import java.security.KeyStore;
23 import java.security.KeyStoreException;
24 import java.security.NoSuchAlgorithmException;
25 import java.security.UnrecoverableKeyException;
26 import java.security.cert.CertificateException;
27 import javax.net.ssl.KeyManager;
28 import javax.net.ssl.KeyManagerFactory;
29 import software.amazon.awssdk.annotations.SdkInternalApi;
30 import software.amazon.awssdk.http.TlsKeyManagersProvider;
31 
32 /**
33  * Abstract {@link TlsKeyManagersProvider} that loads the key store from a
34  * a given file path.
35  * <p>
36  * This uses {@link KeyManagerFactory#getDefaultAlgorithm()} to determine the
37  * {@code KeyManagerFactory} algorithm to use.
38  */
39 @SdkInternalApi
40 public abstract class AbstractFileStoreTlsKeyManagersProvider implements TlsKeyManagersProvider {
41 
createKeyManagers(Path storePath, String storeType, char[] password)42     protected final KeyManager[] createKeyManagers(Path storePath, String storeType, char[] password)
43             throws CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException, UnrecoverableKeyException {
44         KeyStore ks = createKeyStore(storePath, storeType, password);
45         KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
46         kmf.init(ks, password);
47         return kmf.getKeyManagers();
48     }
49 
createKeyStore(Path storePath, String storeType, char[] password)50     private KeyStore createKeyStore(Path storePath, String storeType, char[] password)
51             throws KeyStoreException, IOException, CertificateException, NoSuchAlgorithmException {
52         KeyStore ks = KeyStore.getInstance(storeType);
53         try (InputStream storeIs = Files.newInputStream(storePath)) {
54             ks.load(storeIs, password);
55             return ks;
56         }
57     }
58 }
59