blob: f360bbe99ba1408375f94d19211769f0bf94403e [file] [log] [blame]
Robert Berry81ee34b2018-01-23 11:59:59 +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
17package android.security.keystore.recovery;
18
19import android.annotation.NonNull;
Dmitry Dementyevf8ae5de2018-01-08 18:08:23 -080020import android.annotation.SystemApi;
21
Robert Berry81ee34b2018-01-23 11:59:59 +000022import android.os.Parcel;
23import android.os.Parcelable;
24
25import com.android.internal.util.Preconditions;
26
27/**
28 * Helper class with data necessary recover a single application key, given a recovery key.
29 *
30 * <ul>
31 * <li>Alias - Keystore alias of the key.
Dmitry Dementyev0916e7c2018-01-23 13:02:08 -080032 * <li>Account Recovery Agent specific account associated with the key.
Robert Berry81ee34b2018-01-23 11:59:59 +000033 * <li>Encrypted key material.
34 * </ul>
35 *
36 * Note that Application info is not included. Recovery Agent can only make its own keys
37 * recoverable.
38 *
39 * @hide
40 */
Dmitry Dementyevf8ae5de2018-01-08 18:08:23 -080041@SystemApi
Robert Berry81ee34b2018-01-23 11:59:59 +000042public final class WrappedApplicationKey implements Parcelable {
43 private String mAlias;
44 // The only supported format is AES-256 symmetric key.
45 private byte[] mEncryptedKeyMaterial;
Dmitry Dementyev0916e7c2018-01-23 13:02:08 -080046 private byte[] mAccount;
Robert Berry81ee34b2018-01-23 11:59:59 +000047
48 /**
49 * Builder for creating {@link WrappedApplicationKey}.
50 */
51 public static class Builder {
Dmitry Dementyev0916e7c2018-01-23 13:02:08 -080052 private WrappedApplicationKey mInstance = new WrappedApplicationKey();
Robert Berry81ee34b2018-01-23 11:59:59 +000053
54 /**
55 * Sets Application-specific alias of the key.
56 *
57 * @param alias The alias.
58 * @return This builder.
59 */
60 public Builder setAlias(@NonNull String alias) {
61 mInstance.mAlias = alias;
62 return this;
63 }
64
65 /**
Dmitry Dementyev0916e7c2018-01-23 13:02:08 -080066 * Sets Recovery agent specific account.
67 *
68 * @param account The account.
69 * @return This builder.
70 */
71 public Builder setAccount(@NonNull byte[] account) {
72 mInstance.mAccount = account;
73 return this;
74 }
75
76 /**
Robert Berry81ee34b2018-01-23 11:59:59 +000077 * Sets key material encrypted by recovery key.
78 *
79 * @param encryptedKeyMaterial The key material
80 * @return This builder
81 */
82
83 public Builder setEncryptedKeyMaterial(@NonNull byte[] encryptedKeyMaterial) {
84 mInstance.mEncryptedKeyMaterial = encryptedKeyMaterial;
85 return this;
86 }
87
88 /**
89 * Creates a new {@link WrappedApplicationKey} instance.
90 *
91 * @return new instance
92 * @throws NullPointerException if some required fields were not set.
93 */
94 @NonNull public WrappedApplicationKey build() {
95 Preconditions.checkNotNull(mInstance.mAlias);
96 Preconditions.checkNotNull(mInstance.mEncryptedKeyMaterial);
Dmitry Dementyev0916e7c2018-01-23 13:02:08 -080097 if (mInstance.mAccount == null) {
98 mInstance.mAccount = new byte[]{};
99 }
Robert Berry81ee34b2018-01-23 11:59:59 +0000100 return mInstance;
101 }
102 }
103
104 private WrappedApplicationKey() {
Robert Berry81ee34b2018-01-23 11:59:59 +0000105 }
106
107 /**
108 * Deprecated - consider using Builder.
109 * @hide
110 */
111 public WrappedApplicationKey(@NonNull String alias, @NonNull byte[] encryptedKeyMaterial) {
112 mAlias = Preconditions.checkNotNull(alias);
113 mEncryptedKeyMaterial = Preconditions.checkNotNull(encryptedKeyMaterial);
114 }
115
116 /**
117 * Application-specific alias of the key.
118 *
119 * @see java.security.KeyStore.aliases
120 */
121 public @NonNull String getAlias() {
122 return mAlias;
123 }
124
125 /** Key material encrypted by recovery key. */
126 public @NonNull byte[] getEncryptedKeyMaterial() {
127 return mEncryptedKeyMaterial;
128 }
129
Dmitry Dementyev0916e7c2018-01-23 13:02:08 -0800130 /** Account, default value is empty array */
131 public @NonNull byte[] getAccount() {
132 if (mAccount == null) {
133 return new byte[]{};
134 }
135 return mAccount;
136 }
137
138 public static final Parcelable.Creator<WrappedApplicationKey> CREATOR =
139 new Parcelable.Creator<WrappedApplicationKey>() {
Robert Berry81ee34b2018-01-23 11:59:59 +0000140 public WrappedApplicationKey createFromParcel(Parcel in) {
141 return new WrappedApplicationKey(in);
142 }
143
144 public WrappedApplicationKey[] newArray(int length) {
145 return new WrappedApplicationKey[length];
146 }
147 };
148
Robert Berry81ee34b2018-01-23 11:59:59 +0000149 @Override
150 public void writeToParcel(Parcel out, int flags) {
151 out.writeString(mAlias);
152 out.writeByteArray(mEncryptedKeyMaterial);
Dmitry Dementyev0916e7c2018-01-23 13:02:08 -0800153 out.writeByteArray(mAccount);
Robert Berry81ee34b2018-01-23 11:59:59 +0000154 }
155
156 /**
157 * @hide
158 */
159 protected WrappedApplicationKey(Parcel in) {
160 mAlias = in.readString();
161 mEncryptedKeyMaterial = in.createByteArray();
Dmitry Dementyev0916e7c2018-01-23 13:02:08 -0800162 mAccount = in.createByteArray();
Robert Berry81ee34b2018-01-23 11:59:59 +0000163 }
164
165 @Override
166 public int describeContents() {
167 return 0;
168 }
169}