Robert Berry | a9fae14 | 2017-12-11 14:34:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 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 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.locksettings.recoverablekeystore; |
| 18 | |
Dmitry Dementyev | 29b9de5 | 2018-01-31 16:09:32 -0800 | [diff] [blame] | 19 | import java.io.IOException; |
| 20 | import java.security.cert.CertificateException; |
Robert Berry | a9fae14 | 2017-12-11 14:34:29 +0000 | [diff] [blame] | 21 | import java.security.Key; |
| 22 | import java.security.KeyStore; |
| 23 | import java.security.KeyStoreException; |
| 24 | import java.security.NoSuchAlgorithmException; |
Dmitry Dementyev | 29b9de5 | 2018-01-31 16:09:32 -0800 | [diff] [blame] | 25 | import java.security.NoSuchAlgorithmException; |
Robert Berry | a9fae14 | 2017-12-11 14:34:29 +0000 | [diff] [blame] | 26 | import java.security.UnrecoverableKeyException; |
| 27 | |
| 28 | /** |
| 29 | * Implementation of {@link KeyStoreProxy} that delegates all method calls to the {@link KeyStore}. |
| 30 | */ |
| 31 | public class KeyStoreProxyImpl implements KeyStoreProxy { |
| 32 | |
Dmitry Dementyev | 29b9de5 | 2018-01-31 16:09:32 -0800 | [diff] [blame] | 33 | private static final String ANDROID_KEY_STORE_PROVIDER = "AndroidKeyStore"; |
Robert Berry | a9fae14 | 2017-12-11 14:34:29 +0000 | [diff] [blame] | 34 | private final KeyStore mKeyStore; |
| 35 | |
| 36 | /** |
| 37 | * A new instance, delegating to {@code keyStore}. |
| 38 | */ |
| 39 | public KeyStoreProxyImpl(KeyStore keyStore) { |
| 40 | mKeyStore = keyStore; |
| 41 | } |
| 42 | |
| 43 | @Override |
| 44 | public boolean containsAlias(String alias) throws KeyStoreException { |
| 45 | return mKeyStore.containsAlias(alias); |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public Key getKey(String alias, char[] password) |
| 50 | throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException { |
| 51 | return mKeyStore.getKey(alias, password); |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public void setEntry(String alias, KeyStore.Entry entry, KeyStore.ProtectionParameter protParam) |
| 56 | throws KeyStoreException { |
| 57 | mKeyStore.setEntry(alias, entry, protParam); |
| 58 | } |
Robert Berry | a244b2e | 2017-12-19 10:44:56 +0000 | [diff] [blame] | 59 | |
| 60 | @Override |
| 61 | public void deleteEntry(String alias) throws KeyStoreException { |
| 62 | mKeyStore.deleteEntry(alias); |
| 63 | } |
Dmitry Dementyev | 29b9de5 | 2018-01-31 16:09:32 -0800 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * Returns AndroidKeyStore-provided {@link KeyStore}, having already invoked |
| 67 | * {@link KeyStore#load(KeyStore.LoadStoreParameter)}. |
| 68 | * |
| 69 | * @throws KeyStoreException if there was a problem getting or initializing the key store. |
| 70 | */ |
| 71 | public static KeyStore getAndLoadAndroidKeyStore() throws KeyStoreException { |
| 72 | KeyStore keyStore = KeyStore.getInstance(ANDROID_KEY_STORE_PROVIDER); |
| 73 | try { |
| 74 | keyStore.load(/*param=*/ null); |
| 75 | } catch (CertificateException | IOException | NoSuchAlgorithmException e) { |
| 76 | // Should never happen. |
| 77 | throw new KeyStoreException("Unable to load keystore.", e); |
| 78 | } |
| 79 | return keyStore; |
| 80 | } |
Robert Berry | a9fae14 | 2017-12-11 14:34:29 +0000 | [diff] [blame] | 81 | } |