blob: 66c87ed2ec1e7e6654187f41afa453df960efc77 [file] [log] [blame]
Kenny Root2eeda722013-04-10 11:30:58 -07001/*
2 * Copyright (C) 2013 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
17package android.security;
18
Alex Klyubin54bb1592015-05-11 12:30:03 -070019import android.annotation.NonNull;
Alex Klyubineedda452015-05-07 17:34:24 -070020import android.app.KeyguardManager;
Kenny Root2eeda722013-04-10 11:30:58 -070021import android.content.Context;
Alex Klyubin3f8d4d82015-05-13 09:15:00 -070022import android.security.keystore.KeyProtection;
Kenny Root2eeda722013-04-10 11:30:58 -070023
Alex Klyubin3f8d4d82015-05-13 09:15:00 -070024import java.security.KeyPairGenerator;
Kenny Root2eeda722013-04-10 11:30:58 -070025import java.security.KeyStore.ProtectionParameter;
Alex Klyubinf853f642015-04-08 13:36:22 -070026
Kenny Root2eeda722013-04-10 11:30:58 -070027/**
Alex Klyubin3f8d4d82015-05-13 09:15:00 -070028 * This provides the optional parameters that can be specified for
29 * {@code KeyStore} entries that work with
30 * <a href="{@docRoot}training/articles/keystore.html">Android KeyStore
31 * facility</a>. The Android KeyStore facility is accessed through a
32 * {@link java.security.KeyStore} API using the {@code AndroidKeyStore}
33 * provider. The {@code context} passed in may be used to pop up some UI to ask
34 * the user to unlock or initialize the Android KeyStore facility.
35 * <p>
36 * Any entries placed in the {@code KeyStore} may be retrieved later. Note that
37 * there is only one logical instance of the {@code KeyStore} per application
38 * UID so apps using the {@code sharedUid} facility will also share a
39 * {@code KeyStore}.
40 * <p>
41 * Keys may be generated using the {@link KeyPairGenerator} facility with a
42 * {@link KeyPairGeneratorSpec} to specify the entry's {@code alias}. A
43 * self-signed X.509 certificate will be attached to generated entries, but that
44 * may be replaced at a later time by a certificate signed by a real Certificate
45 * Authority.
Alex Klyubineedda452015-05-07 17:34:24 -070046 *
Alex Klyubin3f8d4d82015-05-13 09:15:00 -070047 * @deprecated Use {@link KeyProtection} instead.
Kenny Root2eeda722013-04-10 11:30:58 -070048 */
Alex Klyubin3f8d4d82015-05-13 09:15:00 -070049@Deprecated
Kenny Root1c219f62013-04-18 17:57:03 -070050public final class KeyStoreParameter implements ProtectionParameter {
Alex Klyubin1eda77a2015-04-28 14:21:01 -070051 private final int mFlags;
Kenny Root2eeda722013-04-10 11:30:58 -070052
Alex Klyubin1eda77a2015-04-28 14:21:01 -070053 private KeyStoreParameter(
Alex Klyubin3f8d4d82015-05-13 09:15:00 -070054 int flags) {
Kenny Root2eeda722013-04-10 11:30:58 -070055 mFlags = flags;
Alex Klyubin1eda77a2015-04-28 14:21:01 -070056 }
57
58 /**
Kenny Root2eeda722013-04-10 11:30:58 -070059 * @hide
60 */
61 public int getFlags() {
62 return mFlags;
63 }
64
65 /**
Alex Klyubineedda452015-05-07 17:34:24 -070066 * Returns {@code true} if the {@link java.security.KeyStore} entry must be encrypted at rest.
67 * This will protect the entry with the secure lock screen credential (e.g., password, PIN, or
68 * pattern).
Alex Klyubin3f8d4d82015-05-13 09:15:00 -070069 *
70 * <p>Note that encrypting the key at rest requires that the secure lock screen (e.g., password,
71 * PIN, pattern) is set up, otherwise key generation will fail. Moreover, this key will be
72 * deleted when the secure lock screen is disabled or reset (e.g., by the user or a Device
73 * Administrator). Finally, this key cannot be used until the user unlocks the secure lock
74 * screen after boot.
75 *
76 * @see KeyguardManager#isDeviceSecure()
Kenny Root2eeda722013-04-10 11:30:58 -070077 */
78 public boolean isEncryptionRequired() {
79 return (mFlags & KeyStore.FLAG_ENCRYPTED) != 0;
80 }
81
82 /**
Kenny Root1c219f62013-04-18 17:57:03 -070083 * Builder class for {@link KeyStoreParameter} objects.
Kenny Root2eeda722013-04-10 11:30:58 -070084 * <p>
Kenny Root1c219f62013-04-18 17:57:03 -070085 * This will build protection parameters for use with the
Robert Ly716cc7d2014-05-07 21:16:15 -070086 * <a href="{@docRoot}training/articles/keystore.html">Android KeyStore
Kenny Root2eeda722013-04-10 11:30:58 -070087 * facility</a>.
88 * <p>
89 * This can be used to require that KeyStore entries be stored encrypted.
90 * <p>
91 * Example:
92 *
93 * <pre class="prettyprint">
Kenny Root1c219f62013-04-18 17:57:03 -070094 * KeyStoreParameter params = new KeyStoreParameter.Builder(mContext)
Alex Klyubin3f8d4d82015-05-13 09:15:00 -070095 * .setEncryptionRequired()
Kenny Root1c219f62013-04-18 17:57:03 -070096 * .build();
Kenny Root2eeda722013-04-10 11:30:58 -070097 * </pre>
Alex Klyubin3f8d4d82015-05-13 09:15:00 -070098 *
99 * @deprecated Use {@link KeyProtection.Builder} instead.
Kenny Root2eeda722013-04-10 11:30:58 -0700100 */
Alex Klyubin3f8d4d82015-05-13 09:15:00 -0700101 @Deprecated
Kenny Root2eeda722013-04-10 11:30:58 -0700102 public final static class Builder {
103 private int mFlags;
104
105 /**
106 * Creates a new instance of the {@code Builder} with the given
107 * {@code context}. The {@code context} passed in may be used to pop up
108 * some UI to ask the user to unlock or initialize the Android KeyStore
109 * facility.
110 */
Alex Klyubin54bb1592015-05-11 12:30:03 -0700111 public Builder(@NonNull Context context) {
Kenny Root2eeda722013-04-10 11:30:58 -0700112 if (context == null) {
113 throw new NullPointerException("context == null");
114 }
Kenny Root2eeda722013-04-10 11:30:58 -0700115 }
116
117 /**
Alex Klyubin54183932015-05-08 15:25:48 -0700118 * Sets whether this {@link java.security.KeyStore} entry must be encrypted at rest.
119 * Encryption at rest will protect the entry with the secure lock screen credential (e.g.,
120 * password, PIN, or pattern).
Alex Klyubineedda452015-05-07 17:34:24 -0700121 *
122 * <p>Note that enabling this feature requires that the secure lock screen (e.g., password,
Alex Klyubin54183932015-05-08 15:25:48 -0700123 * PIN, pattern) is set up, otherwise setting the {@code KeyStore} entry will fail.
124 * Moreover, this entry will be deleted when the secure lock screen is disabled or reset
125 * (e.g., by the user or a Device Administrator). Finally, this entry cannot be used until
126 * the user unlocks the secure lock screen after boot.
Alex Klyubineedda452015-05-07 17:34:24 -0700127 *
128 * @see KeyguardManager#isDeviceSecure()
Kenny Root2eeda722013-04-10 11:30:58 -0700129 */
Alex Klyubin54bb1592015-05-11 12:30:03 -0700130 @NonNull
Kenny Root1c219f62013-04-18 17:57:03 -0700131 public Builder setEncryptionRequired(boolean required) {
132 if (required) {
133 mFlags |= KeyStore.FLAG_ENCRYPTED;
134 } else {
135 mFlags &= ~KeyStore.FLAG_ENCRYPTED;
136 }
Kenny Root2eeda722013-04-10 11:30:58 -0700137 return this;
138 }
139
140 /**
Alex Klyubinbaf28382015-03-26 14:46:55 -0700141 * Builds the instance of the {@code KeyStoreParameter}.
Kenny Root2eeda722013-04-10 11:30:58 -0700142 *
143 * @throws IllegalArgumentException if a required field is missing
Alex Klyubinbaf28382015-03-26 14:46:55 -0700144 * @return built instance of {@code KeyStoreParameter}
Kenny Root2eeda722013-04-10 11:30:58 -0700145 */
Alex Klyubin54bb1592015-05-11 12:30:03 -0700146 @NonNull
Kenny Root1c219f62013-04-18 17:57:03 -0700147 public KeyStoreParameter build() {
Alex Klyubin1eda77a2015-04-28 14:21:01 -0700148 return new KeyStoreParameter(
Alex Klyubin3f8d4d82015-05-13 09:15:00 -0700149 mFlags);
Kenny Root2eeda722013-04-10 11:30:58 -0700150 }
151 }
152}