blob: 8c23d9b4f8e1e87a6d9ff6103b5bae5c56a35683 [file] [log] [blame]
Robert Berryce50cd32017-12-07 14:33:54 +00001/*
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 Berry5397d4d2017-12-12 13:22:00 +000017package com.android.server.locksettings.recoverablekeystore;
Robert Berryce50cd32017-12-07 14:33:54 +000018
Robert Berrya244b2e2017-12-19 10:44:56 +000019import com.android.server.locksettings.recoverablekeystore.storage.RecoverableKeyStoreDb;
20
Robert Berryce50cd32017-12-07 14:33:54 +000021import java.security.InvalidKeyException;
22import java.security.KeyStoreException;
23import java.security.NoSuchAlgorithmException;
Robert Berrya244b2e2017-12-19 10:44:56 +000024import java.util.Locale;
Robert Berryce50cd32017-12-07 14:33:54 +000025
26import javax.crypto.KeyGenerator;
27import javax.crypto.SecretKey;
Robert Berryce50cd32017-12-07 14:33:54 +000028
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 */
38public class RecoverableKeyGenerator {
Robert Berrya244b2e2017-12-19 10:44:56 +000039 private static final int RESULT_CANNOT_INSERT_ROW = -1;
Robert Berryce50cd32017-12-07 14:33:54 +000040 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 Berryce50cd32017-12-07 14:33:54 +000046 * @throws NoSuchAlgorithmException if "AES" key generation or "AES/GCM/NoPadding" cipher is
47 * unavailable. Should never happen.
48 *
49 * @hide
50 */
Robert Berrya244b2e2017-12-19 10:44:56 +000051 public static RecoverableKeyGenerator newInstance(RecoverableKeyStoreDb database)
Robert Berryce50cd32017-12-07 14:33:54 +000052 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 Berrycfc990a2017-12-22 15:54:30 +000056 return new RecoverableKeyGenerator(keyGenerator, database);
Robert Berryce50cd32017-12-07 14:33:54 +000057 }
58
59 private final KeyGenerator mKeyGenerator;
Robert Berrya244b2e2017-12-19 10:44:56 +000060 private final RecoverableKeyStoreDb mDatabase;
Robert Berryce50cd32017-12-07 14:33:54 +000061
62 private RecoverableKeyGenerator(
63 KeyGenerator keyGenerator,
Robert Berrycfc990a2017-12-22 15:54:30 +000064 RecoverableKeyStoreDb recoverableKeyStoreDb) {
Robert Berryce50cd32017-12-07 14:33:54 +000065 mKeyGenerator = keyGenerator;
Robert Berrya244b2e2017-12-19 10:44:56 +000066 mDatabase = recoverableKeyStoreDb;
Robert Berryce50cd32017-12-07 14:33:54 +000067 }
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 Berrya244b2e2017-12-19 10:44:56 +000076 * @param platformKey The user's platform key, with which to wrap the generated key.
Robert Berryb7c06ea2017-12-21 13:37:23 +000077 * @param userId The user ID of the profile to which the calling app belongs.
Robert Berrya244b2e2017-12-19 10:44:56 +000078 * @param uid The uid of the application that will own the key.
Robert Berrycfc990a2017-12-22 15:54:30 +000079 * @param alias The alias by which the key will be known in the recoverable key store.
Robert Berrya244b2e2017-12-19 10:44:56 +000080 * @throws RecoverableKeyStorageException if there is some error persisting the key either to
Robert Berrycfc990a2017-12-22 15:54:30 +000081 * the database.
Robert Berrya244b2e2017-12-19 10:44:56 +000082 * @throws KeyStoreException if there is a KeyStore error wrapping the generated key.
Robert Berryce50cd32017-12-07 14:33:54 +000083 * @throws InvalidKeyException if the platform key cannot be used to wrap keys.
Robert Berryce50cd32017-12-07 14:33:54 +000084 *
85 * @hide
86 */
Robert Berrycfc990a2017-12-22 15:54:30 +000087 public byte[] generateAndStoreKey(
Robert Berryb7c06ea2017-12-21 13:37:23 +000088 PlatformEncryptionKey platformKey, int userId, int uid, String alias)
Robert Berrya244b2e2017-12-19 10:44:56 +000089 throws RecoverableKeyStorageException, KeyStoreException, InvalidKeyException {
Robert Berryce50cd32017-12-07 14:33:54 +000090 mKeyGenerator.init(KEY_SIZE_BITS);
91 SecretKey key = mKeyGenerator.generateKey();
92
Robert Berrya244b2e2017-12-19 10:44:56 +000093 WrappedKey wrappedKey = WrappedKey.fromSecretKey(platformKey, key);
Robert Berryb7c06ea2017-12-21 13:37:23 +000094 long result = mDatabase.insertKey(userId, uid, alias, wrappedKey);
Robert Berryce50cd32017-12-07 14:33:54 +000095
Robert Berrya244b2e2017-12-19 10:44:56 +000096 if (result == RESULT_CANNOT_INSERT_ROW) {
Robert Berrya244b2e2017-12-19 10:44:56 +000097 throw new RecoverableKeyStorageException(
98 String.format(
99 Locale.US, "Failed writing (%d, %s) to database.", uid, alias));
Robert Berryce50cd32017-12-07 14:33:54 +0000100 }
Robert Berrycfc990a2017-12-22 15:54:30 +0000101
102 return key.getEncoded();
Robert Berryce50cd32017-12-07 14:33:54 +0000103 }
104}