summaryrefslogtreecommitdiff
path: root/android/security/keystore/KeychainProtectionParams.java
blob: a940fdc778a9307719fcc7c64930c9d4a2b9e400 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 * Copyright (C) 2017 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.keystore;

import android.annotation.IntDef;
import android.annotation.NonNull;
import android.os.Parcel;
import android.os.Parcelable;

import com.android.internal.util.Preconditions;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Arrays;

/**
 * A {@link KeychainSnapshot} is protected with a key derived from the user's lock screen. This
 * class wraps all the data necessary to derive the same key on a recovering device:
 *
 * <ul>
 *     <li>UI parameters for the user's lock screen - so that if e.g., the user was using a pattern,
 *         the recovering device can display the pattern UI to the user when asking them to enter
 *         the lock screen from their previous device.
 *     <li>The algorithm used to derive a key from the user's lock screen, e.g. SHA-256 with a salt.
 * </ul>
 *
 * <p>As such, this data is sent along with the {@link KeychainSnapshot} when syncing the current
 * version of the keychain.
 *
 * <p>For now, the recoverable keychain only supports a single layer of protection, which is the
 * user's lock screen. In the future, the keychain will support multiple layers of protection
 * (e.g. an additional keychain password, along with the lock screen).
 *
 * @hide
 */
public final class KeychainProtectionParams implements Parcelable {
    /** @hide */
    @Retention(RetentionPolicy.SOURCE)
    @IntDef({TYPE_LOCKSCREEN, TYPE_CUSTOM_PASSWORD})
    public @interface UserSecretType {
    }

    /**
     * Lockscreen secret is required to recover KeyStore.
     */
    public static final int TYPE_LOCKSCREEN = 100;

    /**
     * Custom passphrase, unrelated to lock screen, is required to recover KeyStore.
     */
    public static final int TYPE_CUSTOM_PASSWORD = 101;

    /** @hide */
    @Retention(RetentionPolicy.SOURCE)
    @IntDef({TYPE_PIN, TYPE_PASSWORD, TYPE_PATTERN})
    public @interface LockScreenUiFormat {
    }

    /**
     * Pin with digits only.
     */
    public static final int TYPE_PIN = 1;

    /**
     * Password. String with latin-1 characters only.
     */
    public static final int TYPE_PASSWORD = 2;

    /**
     * Pattern with 3 by 3 grid.
     */
    public static final int TYPE_PATTERN = 3;

    @UserSecretType
    private Integer mUserSecretType;

    @LockScreenUiFormat
    private Integer mLockScreenUiFormat;

    /**
     * Parameters of the key derivation function, including algorithm, difficulty, salt.
     */
    private KeyDerivationParams mKeyDerivationParams;
    private byte[] mSecret; // Derived from user secret. The field must have limited visibility.

    /**
     * @param secret Constructor creates a reference to the secret. Caller must use
     * @link {#clearSecret} to overwrite its value in memory.
     * @hide
     */
    public KeychainProtectionParams(@UserSecretType int userSecretType,
            @LockScreenUiFormat int lockScreenUiFormat,
            @NonNull KeyDerivationParams keyDerivationParams,
            @NonNull byte[] secret) {
        mUserSecretType = userSecretType;
        mLockScreenUiFormat = lockScreenUiFormat;
        mKeyDerivationParams = Preconditions.checkNotNull(keyDerivationParams);
        mSecret = Preconditions.checkNotNull(secret);
    }

    private KeychainProtectionParams() {

    }

    /**
     * @see TYPE_LOCKSCREEN
     * @see TYPE_CUSTOM_PASSWORD
     */
    public @UserSecretType int getUserSecretType() {
        return mUserSecretType;
    }

    /**
     * Specifies UX shown to user during recovery.
     * Default value is {@code TYPE_LOCKSCREEN}
     *
     * @see TYPE_PIN
     * @see TYPE_PASSWORD
     * @see TYPE_PATTERN
     */
    public @LockScreenUiFormat int getLockScreenUiFormat() {
        return mLockScreenUiFormat;
    }

    /**
     * Specifies function used to derive symmetric key from user input
     * Format is defined in separate util class.
     */
    public @NonNull KeyDerivationParams getKeyDerivationParams() {
        return mKeyDerivationParams;
    }

    /**
     * Secret derived from user input.
     * Default value is empty array
     *
     * @return secret or empty array
     */
    public @NonNull byte[] getSecret() {
        return mSecret;
    }

    /**
     * Builder for creating {@link KeychainProtectionParams}.
     */
    public static class Builder {
        private KeychainProtectionParams mInstance = new KeychainProtectionParams();

        /**
         * Sets user secret type.
         *
         * @see TYPE_LOCKSCREEN
         * @see TYPE_CUSTOM_PASSWORD
         * @param userSecretType The secret type
         * @return This builder.
         */
        public Builder setUserSecretType(@UserSecretType int userSecretType) {
            mInstance.mUserSecretType = userSecretType;
            return this;
        }

        /**
         * Sets UI format.
         *
         * @see TYPE_PIN
         * @see TYPE_PASSWORD
         * @see TYPE_PATTERN
         * @param lockScreenUiFormat The UI format
         * @return This builder.
         */
        public Builder setLockScreenUiFormat(@LockScreenUiFormat int lockScreenUiFormat) {
            mInstance.mLockScreenUiFormat = lockScreenUiFormat;
            return this;
        }

        /**
         * Sets parameters of the key derivation function.
         *
         * @param keyDerivationParams Key derivation Params
         * @return This builder.
         */
        public Builder setKeyDerivationParams(@NonNull KeyDerivationParams
                keyDerivationParams) {
            mInstance.mKeyDerivationParams = keyDerivationParams;
            return this;
        }

        /**
         * Secret derived from user input, or empty array.
         *
         * @param secret The secret.
         * @return This builder.
         */
        public Builder setSecret(@NonNull byte[] secret) {
            mInstance.mSecret = secret;
            return this;
        }


        /**
         * Creates a new {@link KeychainProtectionParams} instance.
         * The instance will include default values, if {@link setSecret}
         * or {@link setUserSecretType} were not called.
         *
         * @return new instance
         * @throws NullPointerException if some required fields were not set.
         */
        @NonNull public KeychainProtectionParams build() {
            if (mInstance.mUserSecretType == null) {
                mInstance.mUserSecretType = TYPE_LOCKSCREEN;
            }
            Preconditions.checkNotNull(mInstance.mLockScreenUiFormat);
            Preconditions.checkNotNull(mInstance.mKeyDerivationParams);
            if (mInstance.mSecret == null) {
                mInstance.mSecret = new byte[]{};
            }
            return mInstance;
        }
    }

    /**
     * Removes secret from memory than object is no longer used.
     * Since finalizer call is not reliable, please use @link {#clearSecret} directly.
     */
    @Override
    protected void finalize() throws Throwable {
        clearSecret();
        super.finalize();
    }

    /**
     * Fills mSecret with zeroes.
     */
    public void clearSecret() {
        Arrays.fill(mSecret, (byte) 0);
    }

    public static final Parcelable.Creator<KeychainProtectionParams> CREATOR =
            new Parcelable.Creator<KeychainProtectionParams>() {
        public KeychainProtectionParams createFromParcel(Parcel in) {
            return new KeychainProtectionParams(in);
        }

        public KeychainProtectionParams[] newArray(int length) {
            return new KeychainProtectionParams[length];
        }
    };

    @Override
    public void writeToParcel(Parcel out, int flags) {
        out.writeInt(mUserSecretType);
        out.writeInt(mLockScreenUiFormat);
        out.writeTypedObject(mKeyDerivationParams, flags);
        out.writeByteArray(mSecret);
    }

    /**
     * @hide
     */
    protected KeychainProtectionParams(Parcel in) {
        mUserSecretType = in.readInt();
        mLockScreenUiFormat = in.readInt();
        mKeyDerivationParams = in.readTypedObject(KeyDerivationParams.CREATOR);
        mSecret = in.createByteArray();
    }

    @Override
    public int describeContents() {
        return 0;
    }
}