This CL addresses the comments from API Council about Android KeyStore KeyPairGeneratorSpec, KeyGeneratorSpec and KeyStoreParameter: 1. These abstractions should not take or hold references to Context. 2. The Builders of these abstractions should take all mandatory parameters in their constructors rather than expose them as setters -- only optional paratemers should be exposed via setters. These comments cannot be addressed without deprecation in the already launched KeyPairGeneratorSpec and KeyStoreParameter. Instead of deprecating just the getContext methods and Builder constructors, this CL goes for the nuclear option of deprecating KeyPairGeneratorSpec and KeyStoreParameter as a whole and exposing all of the AndroidKeyStore API in the new package android.security.keystore. This enables this CL to correct all of the accrued design issues with KeyPairGeneratorSpec (e.g., naming of certificate-related methods) and KeyStoreParameter. This also makes the transition to API Level M more clear for existing users of the AndroidKeyStore API. These users will only have to deal with the new always-mandatory parameters (e.g., purposes) and sometimes-mandatory (e.g., digests, block modes, paddings) if they switch to the new API. Prior to this CL they would've had to deal with this if they invoked any of the new methods of KeyPairGeneratorSpec or KeyStoreParameter introduced in API Level M. This CL rips out all the new API introduced into KeyPairGeneratorSpec and KeyStoreParameter classes for Android M, thus reverting these classes to the API launched in L MR1. This is because the new API is now in android.security.keystore.KeyGenParameterSpec and KeyProtection respectively. Bug: 21039983 Change-Id: I59672b3c6ef7bc25c40aa85f1c47d9d8a05d627c
171 lines
6.1 KiB
Java
171 lines
6.1 KiB
Java
/*
|
|
* Copyright (C) 2013 The Android Open Source Project
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
package android.security;
|
|
|
|
import android.annotation.NonNull;
|
|
import android.app.KeyguardManager;
|
|
import android.content.Context;
|
|
import android.security.keystore.KeyGenParameterSpec;
|
|
import android.security.keystore.KeyProtection;
|
|
|
|
import java.security.KeyPairGenerator;
|
|
import java.security.KeyStore.ProtectionParameter;
|
|
|
|
/**
|
|
* This provides the optional parameters that can be specified for
|
|
* {@code KeyStore} entries that work with
|
|
* <a href="{@docRoot}training/articles/keystore.html">Android KeyStore
|
|
* facility</a>. The Android KeyStore facility is accessed through a
|
|
* {@link java.security.KeyStore} API using the {@code AndroidKeyStore}
|
|
* provider. The {@code context} passed in may be used to pop up some UI to ask
|
|
* the user to unlock or initialize the Android KeyStore facility.
|
|
* <p>
|
|
* Any entries placed in the {@code KeyStore} may be retrieved later. Note that
|
|
* there is only one logical instance of the {@code KeyStore} per application
|
|
* UID so apps using the {@code sharedUid} facility will also share a
|
|
* {@code KeyStore}.
|
|
* <p>
|
|
* Keys may be generated using the {@link KeyPairGenerator} facility with a
|
|
* {@link KeyPairGeneratorSpec} to specify the entry's {@code alias}. A
|
|
* self-signed X.509 certificate will be attached to generated entries, but that
|
|
* may be replaced at a later time by a certificate signed by a real Certificate
|
|
* Authority.
|
|
*
|
|
* @deprecated Use {@link KeyProtection} instead.
|
|
*/
|
|
@Deprecated
|
|
public final class KeyStoreParameter implements ProtectionParameter {
|
|
private final Context mContext;
|
|
private final int mFlags;
|
|
|
|
private KeyStoreParameter(
|
|
Context context,
|
|
int flags) {
|
|
if (context == null) {
|
|
throw new IllegalArgumentException("context == null");
|
|
}
|
|
|
|
mContext = context;
|
|
mFlags = flags;
|
|
}
|
|
|
|
/**
|
|
* Gets the Android context used for operations with this instance.
|
|
*/
|
|
public Context getContext() {
|
|
return mContext;
|
|
}
|
|
|
|
/**
|
|
* @hide
|
|
*/
|
|
public int getFlags() {
|
|
return mFlags;
|
|
}
|
|
|
|
/**
|
|
* Returns {@code true} if the {@link java.security.KeyStore} entry must be encrypted at rest.
|
|
* This will protect the entry with the secure lock screen credential (e.g., password, PIN, or
|
|
* pattern).
|
|
*
|
|
* <p>Note that encrypting the key at rest requires that the secure lock screen (e.g., password,
|
|
* PIN, pattern) is set up, otherwise key generation will fail. Moreover, this key will be
|
|
* deleted when the secure lock screen is disabled or reset (e.g., by the user or a Device
|
|
* Administrator). Finally, this key cannot be used until the user unlocks the secure lock
|
|
* screen after boot.
|
|
*
|
|
* @see KeyguardManager#isDeviceSecure()
|
|
*/
|
|
public boolean isEncryptionRequired() {
|
|
return (mFlags & KeyStore.FLAG_ENCRYPTED) != 0;
|
|
}
|
|
|
|
/**
|
|
* Builder class for {@link KeyStoreParameter} objects.
|
|
* <p>
|
|
* This will build protection parameters for use with the
|
|
* <a href="{@docRoot}training/articles/keystore.html">Android KeyStore
|
|
* facility</a>.
|
|
* <p>
|
|
* This can be used to require that KeyStore entries be stored encrypted.
|
|
* <p>
|
|
* Example:
|
|
*
|
|
* <pre class="prettyprint">
|
|
* KeyStoreParameter params = new KeyStoreParameter.Builder(mContext)
|
|
* .setEncryptionRequired()
|
|
* .build();
|
|
* </pre>
|
|
*
|
|
* @deprecated Use {@link KeyProtection.Builder} instead.
|
|
*/
|
|
@Deprecated
|
|
public final static class Builder {
|
|
private final Context mContext;
|
|
private int mFlags;
|
|
|
|
/**
|
|
* Creates a new instance of the {@code Builder} with the given
|
|
* {@code context}. The {@code context} passed in may be used to pop up
|
|
* some UI to ask the user to unlock or initialize the Android KeyStore
|
|
* facility.
|
|
*/
|
|
public Builder(@NonNull Context context) {
|
|
if (context == null) {
|
|
throw new NullPointerException("context == null");
|
|
}
|
|
mContext = context;
|
|
}
|
|
|
|
/**
|
|
* Sets whether this {@link java.security.KeyStore} entry must be encrypted at rest.
|
|
* Encryption at rest will protect the entry with the secure lock screen credential (e.g.,
|
|
* password, PIN, or pattern).
|
|
*
|
|
* <p>Note that enabling this feature requires that the secure lock screen (e.g., password,
|
|
* PIN, pattern) is set up, otherwise setting the {@code KeyStore} entry will fail.
|
|
* Moreover, this entry will be deleted when the secure lock screen is disabled or reset
|
|
* (e.g., by the user or a Device Administrator). Finally, this entry cannot be used until
|
|
* the user unlocks the secure lock screen after boot.
|
|
*
|
|
* @see KeyguardManager#isDeviceSecure()
|
|
*/
|
|
@NonNull
|
|
public Builder setEncryptionRequired(boolean required) {
|
|
if (required) {
|
|
mFlags |= KeyStore.FLAG_ENCRYPTED;
|
|
} else {
|
|
mFlags &= ~KeyStore.FLAG_ENCRYPTED;
|
|
}
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Builds the instance of the {@code KeyStoreParameter}.
|
|
*
|
|
* @throws IllegalArgumentException if a required field is missing
|
|
* @return built instance of {@code KeyStoreParameter}
|
|
*/
|
|
@NonNull
|
|
public KeyStoreParameter build() {
|
|
return new KeyStoreParameter(
|
|
mContext,
|
|
mFlags);
|
|
}
|
|
}
|
|
}
|