Robert Berry | ce50cd3 | 2017-12-07 14:33:54 +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 | |
Robert Berry | 5397d4d | 2017-12-12 13:22:00 +0000 | [diff] [blame] | 17 | package com.android.server.locksettings.recoverablekeystore; |
Robert Berry | ce50cd3 | 2017-12-07 14:33:54 +0000 | [diff] [blame] | 18 | |
Robert Berry | a244b2e | 2017-12-19 10:44:56 +0000 | [diff] [blame] | 19 | import com.android.server.locksettings.recoverablekeystore.storage.RecoverableKeyStoreDb; |
| 20 | |
Robert Berry | ce50cd3 | 2017-12-07 14:33:54 +0000 | [diff] [blame] | 21 | import java.security.InvalidKeyException; |
| 22 | import java.security.KeyStoreException; |
| 23 | import java.security.NoSuchAlgorithmException; |
Robert Berry | a244b2e | 2017-12-19 10:44:56 +0000 | [diff] [blame] | 24 | import java.util.Locale; |
Robert Berry | ce50cd3 | 2017-12-07 14:33:54 +0000 | [diff] [blame] | 25 | |
| 26 | import javax.crypto.KeyGenerator; |
| 27 | import javax.crypto.SecretKey; |
Robert Berry | ce50cd3 | 2017-12-07 14:33:54 +0000 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Generates keys and stores them both in AndroidKeyStore and on disk, in wrapped form. |
| 31 | * |
| 32 | * <p>Generates 256-bit AES keys, which can be used for encrypt / decrypt with AES/GCM/NoPadding. |
| 33 | * They are synced to disk wrapped by a platform key. This allows them to be exported to a remote |
| 34 | * service. |
| 35 | * |
| 36 | * @hide |
| 37 | */ |
| 38 | public class RecoverableKeyGenerator { |
Robert Berry | a244b2e | 2017-12-19 10:44:56 +0000 | [diff] [blame] | 39 | private static final int RESULT_CANNOT_INSERT_ROW = -1; |
Robert Berry | ce50cd3 | 2017-12-07 14:33:54 +0000 | [diff] [blame] | 40 | private static final String KEY_GENERATOR_ALGORITHM = "AES"; |
| 41 | private static final int KEY_SIZE_BITS = 256; |
| 42 | |
| 43 | /** |
| 44 | * A new {@link RecoverableKeyGenerator} instance. |
| 45 | * |
Robert Berry | ce50cd3 | 2017-12-07 14:33:54 +0000 | [diff] [blame] | 46 | * @throws NoSuchAlgorithmException if "AES" key generation or "AES/GCM/NoPadding" cipher is |
| 47 | * unavailable. Should never happen. |
| 48 | * |
| 49 | * @hide |
| 50 | */ |
Robert Berry | a244b2e | 2017-12-19 10:44:56 +0000 | [diff] [blame] | 51 | public static RecoverableKeyGenerator newInstance(RecoverableKeyStoreDb database) |
Robert Berry | ce50cd3 | 2017-12-07 14:33:54 +0000 | [diff] [blame] | 52 | throws NoSuchAlgorithmException { |
| 53 | // NB: This cannot use AndroidKeyStore as the provider, as we need access to the raw key |
| 54 | // material, so that it can be synced to disk in encrypted form. |
| 55 | KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_GENERATOR_ALGORITHM); |
Robert Berry | cfc990a | 2017-12-22 15:54:30 +0000 | [diff] [blame] | 56 | return new RecoverableKeyGenerator(keyGenerator, database); |
Robert Berry | ce50cd3 | 2017-12-07 14:33:54 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | private final KeyGenerator mKeyGenerator; |
Robert Berry | a244b2e | 2017-12-19 10:44:56 +0000 | [diff] [blame] | 60 | private final RecoverableKeyStoreDb mDatabase; |
Robert Berry | ce50cd3 | 2017-12-07 14:33:54 +0000 | [diff] [blame] | 61 | |
| 62 | private RecoverableKeyGenerator( |
| 63 | KeyGenerator keyGenerator, |
Robert Berry | cfc990a | 2017-12-22 15:54:30 +0000 | [diff] [blame] | 64 | RecoverableKeyStoreDb recoverableKeyStoreDb) { |
Robert Berry | ce50cd3 | 2017-12-07 14:33:54 +0000 | [diff] [blame] | 65 | mKeyGenerator = keyGenerator; |
Robert Berry | a244b2e | 2017-12-19 10:44:56 +0000 | [diff] [blame] | 66 | mDatabase = recoverableKeyStoreDb; |
Robert Berry | ce50cd3 | 2017-12-07 14:33:54 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Generates a 256-bit AES key with the given alias. |
| 71 | * |
| 72 | * <p>Stores in the AndroidKeyStore, as well as persisting in wrapped form to disk. It is |
| 73 | * persisted to disk so that it can be synced remotely, and then recovered on another device. |
| 74 | * The generated key allows encrypt/decrypt only using AES/GCM/NoPadding. |
| 75 | * |
Robert Berry | a244b2e | 2017-12-19 10:44:56 +0000 | [diff] [blame] | 76 | * @param platformKey The user's platform key, with which to wrap the generated key. |
Robert Berry | b7c06ea | 2017-12-21 13:37:23 +0000 | [diff] [blame] | 77 | * @param userId The user ID of the profile to which the calling app belongs. |
Robert Berry | a244b2e | 2017-12-19 10:44:56 +0000 | [diff] [blame] | 78 | * @param uid The uid of the application that will own the key. |
Robert Berry | cfc990a | 2017-12-22 15:54:30 +0000 | [diff] [blame] | 79 | * @param alias The alias by which the key will be known in the recoverable key store. |
Robert Berry | a244b2e | 2017-12-19 10:44:56 +0000 | [diff] [blame] | 80 | * @throws RecoverableKeyStorageException if there is some error persisting the key either to |
Robert Berry | cfc990a | 2017-12-22 15:54:30 +0000 | [diff] [blame] | 81 | * the database. |
Robert Berry | a244b2e | 2017-12-19 10:44:56 +0000 | [diff] [blame] | 82 | * @throws KeyStoreException if there is a KeyStore error wrapping the generated key. |
Robert Berry | ce50cd3 | 2017-12-07 14:33:54 +0000 | [diff] [blame] | 83 | * @throws InvalidKeyException if the platform key cannot be used to wrap keys. |
Robert Berry | ce50cd3 | 2017-12-07 14:33:54 +0000 | [diff] [blame] | 84 | * |
| 85 | * @hide |
| 86 | */ |
Robert Berry | cfc990a | 2017-12-22 15:54:30 +0000 | [diff] [blame] | 87 | public byte[] generateAndStoreKey( |
Robert Berry | b7c06ea | 2017-12-21 13:37:23 +0000 | [diff] [blame] | 88 | PlatformEncryptionKey platformKey, int userId, int uid, String alias) |
Robert Berry | a244b2e | 2017-12-19 10:44:56 +0000 | [diff] [blame] | 89 | throws RecoverableKeyStorageException, KeyStoreException, InvalidKeyException { |
Robert Berry | ce50cd3 | 2017-12-07 14:33:54 +0000 | [diff] [blame] | 90 | mKeyGenerator.init(KEY_SIZE_BITS); |
| 91 | SecretKey key = mKeyGenerator.generateKey(); |
| 92 | |
Robert Berry | a244b2e | 2017-12-19 10:44:56 +0000 | [diff] [blame] | 93 | WrappedKey wrappedKey = WrappedKey.fromSecretKey(platformKey, key); |
Robert Berry | b7c06ea | 2017-12-21 13:37:23 +0000 | [diff] [blame] | 94 | long result = mDatabase.insertKey(userId, uid, alias, wrappedKey); |
Robert Berry | ce50cd3 | 2017-12-07 14:33:54 +0000 | [diff] [blame] | 95 | |
Robert Berry | a244b2e | 2017-12-19 10:44:56 +0000 | [diff] [blame] | 96 | if (result == RESULT_CANNOT_INSERT_ROW) { |
Robert Berry | a244b2e | 2017-12-19 10:44:56 +0000 | [diff] [blame] | 97 | throw new RecoverableKeyStorageException( |
| 98 | String.format( |
| 99 | Locale.US, "Failed writing (%d, %s) to database.", uid, alias)); |
Robert Berry | ce50cd3 | 2017-12-07 14:33:54 +0000 | [diff] [blame] | 100 | } |
Robert Berry | cfc990a | 2017-12-22 15:54:30 +0000 | [diff] [blame] | 101 | |
| 102 | return key.getEncoded(); |
Robert Berry | ce50cd3 | 2017-12-07 14:33:54 +0000 | [diff] [blame] | 103 | } |
| 104 | } |