Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 1 | /* |
Robert Berry | 291bd32 | 2018-02-25 22:19:08 +0000 | [diff] [blame] | 2 | * Copyright (C) 2018 The Android Open Source Project |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 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 | |
| 17 | package android.security.keystore.recovery; |
| 18 | |
| 19 | import android.annotation.NonNull; |
Bo Zhu | c704834 | 2019-01-03 14:04:58 -0800 | [diff] [blame] | 20 | import android.annotation.Nullable; |
Dmitry Dementyev | f8ae5de | 2018-01-08 18:08:23 -0800 | [diff] [blame] | 21 | import android.annotation.SystemApi; |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 22 | import android.os.Parcel; |
| 23 | import android.os.Parcelable; |
| 24 | |
| 25 | import 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. |
| 32 | * <li>Encrypted key material. |
| 33 | * </ul> |
| 34 | * |
| 35 | * Note that Application info is not included. Recovery Agent can only make its own keys |
| 36 | * recoverable. |
| 37 | * |
| 38 | * @hide |
| 39 | */ |
Dmitry Dementyev | f8ae5de | 2018-01-08 18:08:23 -0800 | [diff] [blame] | 40 | @SystemApi |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 41 | public final class WrappedApplicationKey implements Parcelable { |
| 42 | private String mAlias; |
| 43 | // The only supported format is AES-256 symmetric key. |
| 44 | private byte[] mEncryptedKeyMaterial; |
Bo Zhu | c704834 | 2019-01-03 14:04:58 -0800 | [diff] [blame] | 45 | // The optional metadata that's authenticated (but unencrypted) with the key material. |
| 46 | private byte[] mMetadata; |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 47 | |
Robert Berry | 52c15f1 | 2018-03-29 10:21:50 +0100 | [diff] [blame] | 48 | // IMPORTANT! PLEASE READ! |
| 49 | // ----------------------- |
| 50 | // If you edit this file (e.g., to add new fields), please MAKE SURE to also do the following: |
| 51 | // - Update the #writeToParcel(Parcel) method below |
| 52 | // - Update the #(Parcel) constructor below |
| 53 | // - Update android.security.keystore.recovery.KeyChainSnapshotTest to make sure nobody |
| 54 | // accidentally breaks your fields in the Parcel in the future. |
| 55 | // - Update com.android.server.locksettings.recoverablekeystore.serialization |
| 56 | // .KeyChainSnapshotSerializer to correctly serialize your new field |
| 57 | // - Update com.android.server.locksettings.recoverablekeystore.serialization |
| 58 | // .KeyChainSnapshotSerializer to correctly deserialize your new field |
| 59 | // - Update com.android.server.locksettings.recoverablekeystore.serialization |
| 60 | // .KeychainSnapshotSerializerTest to make sure nobody breaks serialization of your field |
| 61 | // in the future. |
| 62 | |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 63 | /** |
| 64 | * Builder for creating {@link WrappedApplicationKey}. |
| 65 | */ |
| 66 | public static class Builder { |
Dmitry Dementyev | 0916e7c | 2018-01-23 13:02:08 -0800 | [diff] [blame] | 67 | private WrappedApplicationKey mInstance = new WrappedApplicationKey(); |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * Sets Application-specific alias of the key. |
| 71 | * |
| 72 | * @param alias The alias. |
| 73 | * @return This builder. |
| 74 | */ |
Dmitry Dementyev | ebe5327 | 2019-03-05 13:33:24 -0800 | [diff] [blame] | 75 | public @NonNull Builder setAlias(@NonNull String alias) { |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 76 | mInstance.mAlias = alias; |
| 77 | return this; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Sets key material encrypted by recovery key. |
| 82 | * |
| 83 | * @param encryptedKeyMaterial The key material |
| 84 | * @return This builder |
| 85 | */ |
Dmitry Dementyev | ebe5327 | 2019-03-05 13:33:24 -0800 | [diff] [blame] | 86 | public @NonNull Builder setEncryptedKeyMaterial(@NonNull byte[] encryptedKeyMaterial) { |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 87 | mInstance.mEncryptedKeyMaterial = encryptedKeyMaterial; |
| 88 | return this; |
| 89 | } |
| 90 | |
| 91 | /** |
Bo Zhu | c704834 | 2019-01-03 14:04:58 -0800 | [diff] [blame] | 92 | * Sets the metadata that is authenticated (but unecrypted) with the key material. |
| 93 | * |
| 94 | * @param metadata The metadata |
| 95 | * @return This builder |
| 96 | */ |
Dmitry Dementyev | ebe5327 | 2019-03-05 13:33:24 -0800 | [diff] [blame] | 97 | public @NonNull Builder setMetadata(@Nullable byte[] metadata) { |
Bo Zhu | c704834 | 2019-01-03 14:04:58 -0800 | [diff] [blame] | 98 | mInstance.mMetadata = metadata; |
| 99 | return this; |
| 100 | } |
| 101 | |
| 102 | /** |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 103 | * Creates a new {@link WrappedApplicationKey} instance. |
| 104 | * |
| 105 | * @return new instance |
| 106 | * @throws NullPointerException if some required fields were not set. |
| 107 | */ |
Dmitry Dementyev | ebe5327 | 2019-03-05 13:33:24 -0800 | [diff] [blame] | 108 | public @NonNull WrappedApplicationKey build() { |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 109 | Preconditions.checkNotNull(mInstance.mAlias); |
| 110 | Preconditions.checkNotNull(mInstance.mEncryptedKeyMaterial); |
| 111 | return mInstance; |
| 112 | } |
| 113 | } |
| 114 | |
Robert Berry | 291bd32 | 2018-02-25 22:19:08 +0000 | [diff] [blame] | 115 | private WrappedApplicationKey() { } |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 116 | |
| 117 | /** |
Bo Zhu | c704834 | 2019-01-03 14:04:58 -0800 | [diff] [blame] | 118 | * @deprecated Use the builder instead. |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 119 | * @hide |
| 120 | */ |
Bo Zhu | c704834 | 2019-01-03 14:04:58 -0800 | [diff] [blame] | 121 | @Deprecated |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 122 | public WrappedApplicationKey(@NonNull String alias, @NonNull byte[] encryptedKeyMaterial) { |
| 123 | mAlias = Preconditions.checkNotNull(alias); |
| 124 | mEncryptedKeyMaterial = Preconditions.checkNotNull(encryptedKeyMaterial); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Application-specific alias of the key. |
| 129 | * |
| 130 | * @see java.security.KeyStore.aliases |
| 131 | */ |
| 132 | public @NonNull String getAlias() { |
| 133 | return mAlias; |
| 134 | } |
| 135 | |
| 136 | /** Key material encrypted by recovery key. */ |
| 137 | public @NonNull byte[] getEncryptedKeyMaterial() { |
| 138 | return mEncryptedKeyMaterial; |
| 139 | } |
| 140 | |
Bo Zhu | c704834 | 2019-01-03 14:04:58 -0800 | [diff] [blame] | 141 | /** The metadata with the key. */ |
| 142 | public @Nullable byte[] getMetadata() { |
| 143 | return mMetadata; |
| 144 | } |
| 145 | |
Dmitry Dementyev | ebe5327 | 2019-03-05 13:33:24 -0800 | [diff] [blame] | 146 | public static final @NonNull Parcelable.Creator<WrappedApplicationKey> CREATOR = |
Dmitry Dementyev | 0916e7c | 2018-01-23 13:02:08 -0800 | [diff] [blame] | 147 | new Parcelable.Creator<WrappedApplicationKey>() { |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 148 | public WrappedApplicationKey createFromParcel(Parcel in) { |
| 149 | return new WrappedApplicationKey(in); |
| 150 | } |
| 151 | |
| 152 | public WrappedApplicationKey[] newArray(int length) { |
| 153 | return new WrappedApplicationKey[length]; |
| 154 | } |
| 155 | }; |
| 156 | |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 157 | @Override |
| 158 | public void writeToParcel(Parcel out, int flags) { |
| 159 | out.writeString(mAlias); |
| 160 | out.writeByteArray(mEncryptedKeyMaterial); |
Bo Zhu | c704834 | 2019-01-03 14:04:58 -0800 | [diff] [blame] | 161 | out.writeByteArray(mMetadata); |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /** |
| 165 | * @hide |
| 166 | */ |
| 167 | protected WrappedApplicationKey(Parcel in) { |
| 168 | mAlias = in.readString(); |
| 169 | mEncryptedKeyMaterial = in.createByteArray(); |
Bo Zhu | c704834 | 2019-01-03 14:04:58 -0800 | [diff] [blame] | 170 | // Check if there is still data to be read. |
| 171 | if (in.dataAvail() > 0) { |
| 172 | mMetadata = in.createByteArray(); |
| 173 | } |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public int describeContents() { |
| 178 | return 0; |
| 179 | } |
| 180 | } |