blob: bca03b389e439bf14309eea39dd725fc9cab7934 [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;
20import android.os.Parcel;
21import android.os.Parcelable;
22
23import com.android.internal.util.Preconditions;
24
25/**
26 * Helper class with data necessary recover a single application key, given a recovery key.
27 *
28 * <ul>
29 * <li>Alias - Keystore alias of the key.
30 * <li>Encrypted key material.
31 * </ul>
32 *
33 * Note that Application info is not included. Recovery Agent can only make its own keys
34 * recoverable.
35 *
36 * @hide
37 */
38public final class WrappedApplicationKey implements Parcelable {
39 private String mAlias;
40 // The only supported format is AES-256 symmetric key.
41 private byte[] mEncryptedKeyMaterial;
42
43 /**
44 * Builder for creating {@link WrappedApplicationKey}.
45 */
46 public static class Builder {
47 private WrappedApplicationKey
48 mInstance = new WrappedApplicationKey();
49
50 /**
51 * Sets Application-specific alias of the key.
52 *
53 * @param alias The alias.
54 * @return This builder.
55 */
56 public Builder setAlias(@NonNull String alias) {
57 mInstance.mAlias = alias;
58 return this;
59 }
60
61 /**
62 * Sets key material encrypted by recovery key.
63 *
64 * @param encryptedKeyMaterial The key material
65 * @return This builder
66 */
67
68 public Builder setEncryptedKeyMaterial(@NonNull byte[] encryptedKeyMaterial) {
69 mInstance.mEncryptedKeyMaterial = encryptedKeyMaterial;
70 return this;
71 }
72
73 /**
74 * Creates a new {@link WrappedApplicationKey} instance.
75 *
76 * @return new instance
77 * @throws NullPointerException if some required fields were not set.
78 */
79 @NonNull public WrappedApplicationKey build() {
80 Preconditions.checkNotNull(mInstance.mAlias);
81 Preconditions.checkNotNull(mInstance.mEncryptedKeyMaterial);
82 return mInstance;
83 }
84 }
85
86 private WrappedApplicationKey() {
87
88 }
89
90 /**
91 * Deprecated - consider using Builder.
92 * @hide
93 */
94 public WrappedApplicationKey(@NonNull String alias, @NonNull byte[] encryptedKeyMaterial) {
95 mAlias = Preconditions.checkNotNull(alias);
96 mEncryptedKeyMaterial = Preconditions.checkNotNull(encryptedKeyMaterial);
97 }
98
99 /**
100 * Application-specific alias of the key.
101 *
102 * @see java.security.KeyStore.aliases
103 */
104 public @NonNull String getAlias() {
105 return mAlias;
106 }
107
108 /** Key material encrypted by recovery key. */
109 public @NonNull byte[] getEncryptedKeyMaterial() {
110 return mEncryptedKeyMaterial;
111 }
112
113 public static final Creator<WrappedApplicationKey> CREATOR =
114 new Creator<WrappedApplicationKey>() {
115 public WrappedApplicationKey createFromParcel(Parcel in) {
116 return new WrappedApplicationKey(in);
117 }
118
119 public WrappedApplicationKey[] newArray(int length) {
120 return new WrappedApplicationKey[length];
121 }
122 };
123
124 /**
125 * @hide
126 */
127 @Override
128 public void writeToParcel(Parcel out, int flags) {
129 out.writeString(mAlias);
130 out.writeByteArray(mEncryptedKeyMaterial);
131 }
132
133 /**
134 * @hide
135 */
136 protected WrappedApplicationKey(Parcel in) {
137 mAlias = in.readString();
138 mEncryptedKeyMaterial = in.createByteArray();
139 }
140
141 @Override
142 public int describeContents() {
143 return 0;
144 }
145}