Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.security.keystore.recovery; |
| 18 | |
| 19 | import android.annotation.NonNull; |
Dmitry Dementyev | f8ae5de | 2018-01-08 18:08:23 -0800 | [diff] [blame^] | 20 | import android.annotation.SystemApi; |
| 21 | |
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. |
Dmitry Dementyev | 0916e7c | 2018-01-23 13:02:08 -0800 | [diff] [blame] | 32 | * <li>Account Recovery Agent specific account associated with the key. |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 33 | * <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 Dementyev | f8ae5de | 2018-01-08 18:08:23 -0800 | [diff] [blame^] | 41 | @SystemApi |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 42 | public final class WrappedApplicationKey implements Parcelable { |
| 43 | private String mAlias; |
| 44 | // The only supported format is AES-256 symmetric key. |
| 45 | private byte[] mEncryptedKeyMaterial; |
Dmitry Dementyev | 0916e7c | 2018-01-23 13:02:08 -0800 | [diff] [blame] | 46 | private byte[] mAccount; |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * Builder for creating {@link WrappedApplicationKey}. |
| 50 | */ |
| 51 | public static class Builder { |
Dmitry Dementyev | 0916e7c | 2018-01-23 13:02:08 -0800 | [diff] [blame] | 52 | private WrappedApplicationKey mInstance = new WrappedApplicationKey(); |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 53 | |
| 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 Dementyev | 0916e7c | 2018-01-23 13:02:08 -0800 | [diff] [blame] | 66 | * 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 Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 77 | * 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 Dementyev | 0916e7c | 2018-01-23 13:02:08 -0800 | [diff] [blame] | 97 | if (mInstance.mAccount == null) { |
| 98 | mInstance.mAccount = new byte[]{}; |
| 99 | } |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 100 | return mInstance; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | private WrappedApplicationKey() { |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 105 | } |
| 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 Dementyev | 0916e7c | 2018-01-23 13:02:08 -0800 | [diff] [blame] | 130 | /** 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 Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 140 | 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 Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 149 | @Override |
| 150 | public void writeToParcel(Parcel out, int flags) { |
| 151 | out.writeString(mAlias); |
| 152 | out.writeByteArray(mEncryptedKeyMaterial); |
Dmitry Dementyev | 0916e7c | 2018-01-23 13:02:08 -0800 | [diff] [blame] | 153 | out.writeByteArray(mAccount); |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /** |
| 157 | * @hide |
| 158 | */ |
| 159 | protected WrappedApplicationKey(Parcel in) { |
| 160 | mAlias = in.readString(); |
| 161 | mEncryptedKeyMaterial = in.createByteArray(); |
Dmitry Dementyev | 0916e7c | 2018-01-23 13:02:08 -0800 | [diff] [blame] | 162 | mAccount = in.createByteArray(); |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public int describeContents() { |
| 167 | return 0; |
| 168 | } |
| 169 | } |