blob: df1d1009b95d1159b93055af93d0938ec57f2255 [file] [log] [blame]
Shawn Willden128ffe02014-08-06 12:31:33 -06001/*
2 * Copyright 2014 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
Shawn Willdenb6837e72015-05-16 09:20:59 -060017#include <keymaster/android_keymaster.h>
Shawn Willden567a4a02014-12-31 12:14:46 -070018
Shawn Willden128ffe02014-08-06 12:31:33 -060019#include <assert.h>
20#include <string.h>
21
Shawn Willden4db3fbd2014-08-08 22:13:44 -060022#include <cstddef>
23
Shawn Willden1615f2e2014-08-13 10:37:40 -060024#include <openssl/rand.h>
Shawn Willdenffd790c2014-08-18 21:20:06 -060025#include <openssl/x509.h>
Shawn Willden128ffe02014-08-06 12:31:33 -060026
27#include <UniquePtr.h>
28
Shawn Willdenb6837e72015-05-16 09:20:59 -060029#include <keymaster/android_keymaster_utils.h>
Shawn Willden98d9b922014-08-26 08:14:10 -060030
Shawn Willden3879f862014-08-06 14:40:48 -060031#include "ae.h"
Shawn Willdend67afae2014-08-19 12:36:27 -060032#include "key.h"
Shawn Willden567a4a02014-12-31 12:14:46 -070033#include "openssl_err.h"
Shawn Willdend67afae2014-08-19 12:36:27 -060034#include "operation.h"
Shawn Willden23d4a742015-03-19 15:33:21 -060035#include "operation_table.h"
Shawn Willden72014ad2014-09-17 13:04:10 -060036#include "unencrypted_key_blob.h"
Shawn Willden128ffe02014-08-06 12:31:33 -060037
38namespace keymaster {
39
Shawn Willden2665e862014-11-24 14:46:21 -070040const uint8_t MAJOR_VER = 1;
41const uint8_t MINOR_VER = 0;
42const uint8_t SUBMINOR_VER = 0;
43
Shawn Willdenb6837e72015-05-16 09:20:59 -060044AndroidKeymaster::AndroidKeymaster(size_t operation_table_size)
Shawn Willden23d4a742015-03-19 15:33:21 -060045 : operation_table_(new OperationTable(operation_table_size)) {
Shawn Willden39b970b2014-08-11 09:11:21 -060046}
Shawn Willdena278f612014-12-23 11:22:21 -070047
Shawn Willdenb6837e72015-05-16 09:20:59 -060048AndroidKeymaster::~AndroidKeymaster() {
Shawn Willden39b970b2014-08-11 09:11:21 -060049}
Shawn Willden128ffe02014-08-06 12:31:33 -060050
Shawn Willden128ffe02014-08-06 12:31:33 -060051struct AE_CTX_Delete {
Shawn Willden802bb292014-08-18 10:46:29 -060052 void operator()(ae_ctx* ctx) const { ae_free(ctx); }
Shawn Willden128ffe02014-08-06 12:31:33 -060053};
54typedef UniquePtr<ae_ctx, AE_CTX_Delete> Unique_ae_ctx;
55
Shawn Willden19fca882015-01-22 16:35:30 -070056// TODO(swillden): Unify support analysis. Right now, we have per-keytype methods that determine if
Shawn Willdenb6837e72015-05-16 09:20:59 -060057// specific modes, padding, etc. are supported for that key type, and AndroidKeymaster also has
Shawn Willden19fca882015-01-22 16:35:30 -070058// methods that return the same information. They'll get out of sync. Best to put the knowledge in
Shawn Willdenb6837e72015-05-16 09:20:59 -060059// the keytypes and provide some mechanism for AndroidKeymaster to query the keytypes for the
Shawn Willden19fca882015-01-22 16:35:30 -070060// information.
Shawn Willden128ffe02014-08-06 12:31:33 -060061
62template <typename T>
63bool check_supported(keymaster_algorithm_t algorithm, SupportedResponse<T>* response) {
Shawn Willdena278f612014-12-23 11:22:21 -070064 if (KeyFactoryRegistry::Get(algorithm) == NULL) {
Shawn Willden128ffe02014-08-06 12:31:33 -060065 response->error = KM_ERROR_UNSUPPORTED_ALGORITHM;
66 return false;
67 }
68 return true;
69}
70
Shawn Willdenb6837e72015-05-16 09:20:59 -060071void AndroidKeymaster::GetVersion(const GetVersionRequest&, GetVersionResponse* rsp) {
Shawn Willden2665e862014-11-24 14:46:21 -070072 if (rsp == NULL)
73 return;
74
75 rsp->major_ver = MAJOR_VER;
76 rsp->minor_ver = MINOR_VER;
77 rsp->subminor_ver = SUBMINOR_VER;
78 rsp->error = KM_ERROR_OK;
79}
80
Shawn Willdenb6837e72015-05-16 09:20:59 -060081void AndroidKeymaster::SupportedAlgorithms(
Shawn Willden3809b932014-12-02 06:59:46 -070082 SupportedResponse<keymaster_algorithm_t>* response) const {
Shawn Willden128ffe02014-08-06 12:31:33 -060083 if (response == NULL)
84 return;
Shawn Willdena278f612014-12-23 11:22:21 -070085
86 size_t factory_count = 0;
87 const KeyFactory** factories = KeyFactoryRegistry::GetAll(&factory_count);
88 assert(factories != NULL);
89 assert(factory_count > 0);
90
91 UniquePtr<keymaster_algorithm_t[]> algorithms(new keymaster_algorithm_t[factory_count]);
92 if (!algorithms.get()) {
93 response->error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
94 return;
95 }
96
97 for (size_t i = 0; i < factory_count; ++i)
98 algorithms[i] = factories[i]->registry_key();
99
100 response->results = algorithms.release();
101 response->results_length = factory_count;
102 response->error = KM_ERROR_OK;
Shawn Willden128ffe02014-08-06 12:31:33 -0600103}
104
Shawn Willden63ac0432014-12-29 14:07:08 -0700105static OperationFactory* GetOperationFactory(keymaster_algorithm_t algorithm,
106 keymaster_purpose_t purpose,
107 keymaster_error_t* error) {
108 assert(error);
109 if (error)
110 *error = KM_ERROR_OK;
111
112 OperationFactory* factory =
113 OperationFactoryRegistry::Get(OperationFactory::KeyType(algorithm, purpose));
114 if (factory == NULL && error)
115 *error = KM_ERROR_UNSUPPORTED_PURPOSE;
116
117 return factory;
Shawn Willden128ffe02014-08-06 12:31:33 -0600118}
119
Shawn Willden63ac0432014-12-29 14:07:08 -0700120template <typename T>
121void GetSupported(keymaster_algorithm_t algorithm, keymaster_purpose_t purpose,
122 const T* (OperationFactory::*get_supported_method)(size_t* count) const,
123 SupportedResponse<T>* response) {
124 if (response == NULL || !check_supported(algorithm, response))
125 return;
126
127 OperationFactory* factory = GetOperationFactory(algorithm, purpose, &response->error);
128 if (!factory) {
129 response->error = KM_ERROR_UNSUPPORTED_PURPOSE;
130 return;
131 }
132
133 size_t count;
134 const T* supported = (factory->*get_supported_method)(&count);
135 response->SetResults(supported, count);
136}
137
Shawn Willdenb6837e72015-05-16 09:20:59 -0600138void AndroidKeymaster::SupportedBlockModes(
Shawn Willden63ac0432014-12-29 14:07:08 -0700139 keymaster_algorithm_t algorithm, keymaster_purpose_t purpose,
140 SupportedResponse<keymaster_block_mode_t>* response) const {
141 GetSupported(algorithm, purpose, &OperationFactory::SupportedBlockModes, response);
142}
Shawn Willden3809b932014-12-02 06:59:46 -0700143
Shawn Willdenb6837e72015-05-16 09:20:59 -0600144void AndroidKeymaster::SupportedPaddingModes(
Shawn Willden4200f212014-12-02 07:01:21 -0700145 keymaster_algorithm_t algorithm, keymaster_purpose_t purpose,
Shawn Willden3809b932014-12-02 06:59:46 -0700146 SupportedResponse<keymaster_padding_t>* response) const {
Shawn Willden63ac0432014-12-29 14:07:08 -0700147 GetSupported(algorithm, purpose, &OperationFactory::SupportedPaddingModes, response);
Shawn Willden128ffe02014-08-06 12:31:33 -0600148}
149
Shawn Willdenb6837e72015-05-16 09:20:59 -0600150void AndroidKeymaster::SupportedDigests(keymaster_algorithm_t algorithm,
151 keymaster_purpose_t purpose,
152 SupportedResponse<keymaster_digest_t>* response) const {
Shawn Willden63ac0432014-12-29 14:07:08 -0700153 GetSupported(algorithm, purpose, &OperationFactory::SupportedDigests, response);
Shawn Willden128ffe02014-08-06 12:31:33 -0600154}
155
Shawn Willdenb6837e72015-05-16 09:20:59 -0600156void AndroidKeymaster::SupportedImportFormats(
Shawn Willden3809b932014-12-02 06:59:46 -0700157 keymaster_algorithm_t algorithm, SupportedResponse<keymaster_key_format_t>* response) const {
Shawn Willden128ffe02014-08-06 12:31:33 -0600158 if (response == NULL || !check_supported(algorithm, response))
159 return;
160
Shawn Willdena278f612014-12-23 11:22:21 -0700161 size_t count;
162 const keymaster_key_format_t* formats =
163 KeyFactoryRegistry::Get(algorithm)->SupportedImportFormats(&count);
164 response->SetResults(formats, count);
Shawn Willden128ffe02014-08-06 12:31:33 -0600165}
166
Shawn Willdenb6837e72015-05-16 09:20:59 -0600167void AndroidKeymaster::SupportedExportFormats(
Shawn Willden3809b932014-12-02 06:59:46 -0700168 keymaster_algorithm_t algorithm, SupportedResponse<keymaster_key_format_t>* response) const {
Shawn Willden128ffe02014-08-06 12:31:33 -0600169 if (response == NULL || !check_supported(algorithm, response))
170 return;
171
Shawn Willdena278f612014-12-23 11:22:21 -0700172 size_t count;
173 const keymaster_key_format_t* formats =
174 KeyFactoryRegistry::Get(algorithm)->SupportedExportFormats(&count);
175 response->SetResults(formats, count);
Shawn Willden128ffe02014-08-06 12:31:33 -0600176}
177
Shawn Willdenb6837e72015-05-16 09:20:59 -0600178void AndroidKeymaster::GenerateKey(const GenerateKeyRequest& request,
179 GenerateKeyResponse* response) {
Shawn Willden128ffe02014-08-06 12:31:33 -0600180 if (response == NULL)
181 return;
Shawn Willden128ffe02014-08-06 12:31:33 -0600182
Shawn Willdena278f612014-12-23 11:22:21 -0700183 keymaster_algorithm_t algorithm;
184 KeyFactory* factory = 0;
185 UniquePtr<Key> key;
186 if (!request.key_description.GetTagValue(TAG_ALGORITHM, &algorithm) ||
187 !(factory = KeyFactoryRegistry::Get(algorithm)))
188 response->error = KM_ERROR_UNSUPPORTED_ALGORITHM;
189 else
Shawn Willden567a4a02014-12-31 12:14:46 -0700190 key.reset(factory->GenerateKey(request.key_description, &response->error));
Shawn Willdena278f612014-12-23 11:22:21 -0700191
Shawn Willden76364712014-08-11 17:48:04 -0600192 if (response->error != KM_ERROR_OK)
Shawn Willden128ffe02014-08-06 12:31:33 -0600193 return;
194
Shawn Willden0b2d3332015-04-07 17:46:18 -0600195 response->error = SerializeKey(key.get(), KM_ORIGIN_GENERATED, &response->key_blob,
196 &response->enforced, &response->unenforced);
Shawn Willden128ffe02014-08-06 12:31:33 -0600197}
198
Shawn Willdenb6837e72015-05-16 09:20:59 -0600199void AndroidKeymaster::GetKeyCharacteristics(const GetKeyCharacteristicsRequest& request,
200 GetKeyCharacteristicsResponse* response) {
Shawn Willden1615f2e2014-08-13 10:37:40 -0600201 if (response == NULL)
202 return;
Shawn Willden76364712014-08-11 17:48:04 -0600203
Shawn Willden1615f2e2014-08-13 10:37:40 -0600204 UniquePtr<KeyBlob> blob(
205 LoadKeyBlob(request.key_blob, request.additional_params, &(response->error)));
206 if (blob.get() == NULL)
207 return;
208
209 response->enforced.Reinitialize(blob->enforced());
210 response->unenforced.Reinitialize(blob->unenforced());
Shawn Willden1615f2e2014-08-13 10:37:40 -0600211}
212
Shawn Willdenb6837e72015-05-16 09:20:59 -0600213void AndroidKeymaster::BeginOperation(const BeginOperationRequest& request,
214 BeginOperationResponse* response) {
Shawn Willden1615f2e2014-08-13 10:37:40 -0600215 if (response == NULL)
216 return;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600217 response->op_handle = 0;
218
Shawn Willden63ac0432014-12-29 14:07:08 -0700219 keymaster_algorithm_t algorithm;
220 UniquePtr<Key> key(
221 LoadKey(request.key_blob, request.additional_params, &algorithm, &response->error));
Shawn Willden1615f2e2014-08-13 10:37:40 -0600222 if (key.get() == NULL)
223 return;
224
Shawn Willdenedb79942015-05-08 06:46:44 -0600225 // TODO(swillden): Move this check to a general authorization checker.
Shawn Willdenedb79942015-05-08 06:46:44 -0600226 if (!key->authorizations().Contains(TAG_PURPOSE, request.purpose)) {
227 // TODO(swillden): Consider introducing error codes for unauthorized usages.
228 response->error = KM_ERROR_INCOMPATIBLE_PURPOSE;
229 return;
230 }
231
Shawn Willden63ac0432014-12-29 14:07:08 -0700232 OperationFactory::KeyType op_type(algorithm, request.purpose);
233 OperationFactory* factory = OperationFactoryRegistry::Get(op_type);
234 if (!factory) {
235 response->error = KM_ERROR_UNSUPPORTED_PURPOSE;
236 return;
237 }
238
Shawn Willden3ed6d062015-04-15 13:39:38 -0600239 UniquePtr<Operation> operation(
240 factory->CreateOperation(*key, request.additional_params, &response->error));
Shawn Willdend67afae2014-08-19 12:36:27 -0600241 if (operation.get() == NULL)
Shawn Willden76364712014-08-11 17:48:04 -0600242 return;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600243
Shawn Willden111edb32015-02-05 22:44:24 -0700244 response->output_params.Clear();
245 response->error = operation->Begin(request.additional_params, &response->output_params);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600246 if (response->error != KM_ERROR_OK)
247 return;
248
Shawn Willden23d4a742015-03-19 15:33:21 -0600249 response->error = operation_table_->Add(operation.release(), &response->op_handle);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600250}
251
Shawn Willdenb6837e72015-05-16 09:20:59 -0600252void AndroidKeymaster::UpdateOperation(const UpdateOperationRequest& request,
253 UpdateOperationResponse* response) {
Shawn Willdend6cd7e32014-12-17 08:01:26 -0700254 if (response == NULL)
255 return;
256
Shawn Willden23d4a742015-03-19 15:33:21 -0600257 response->error = KM_ERROR_INVALID_OPERATION_HANDLE;
258 Operation* operation = operation_table_->Find(request.op_handle);
259 if (operation == NULL)
Shawn Willden1615f2e2014-08-13 10:37:40 -0600260 return;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600261
Shawn Willden23d4a742015-03-19 15:33:21 -0600262 response->error = operation->Update(request.additional_params, request.input, &response->output,
263 &response->input_consumed);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600264 if (response->error != KM_ERROR_OK) {
265 // Any error invalidates the operation.
Shawn Willden23d4a742015-03-19 15:33:21 -0600266 operation_table_->Delete(request.op_handle);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600267 }
268}
269
Shawn Willdenb6837e72015-05-16 09:20:59 -0600270void AndroidKeymaster::FinishOperation(const FinishOperationRequest& request,
271 FinishOperationResponse* response) {
Shawn Willdend6cd7e32014-12-17 08:01:26 -0700272 if (response == NULL)
273 return;
274
Shawn Willden23d4a742015-03-19 15:33:21 -0600275 response->error = KM_ERROR_INVALID_OPERATION_HANDLE;
276 Operation* operation = operation_table_->Find(request.op_handle);
277 if (operation == NULL)
Shawn Willden1615f2e2014-08-13 10:37:40 -0600278 return;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600279
Shawn Willden6bfbff02015-02-06 19:48:24 -0700280 response->error =
Shawn Willden23d4a742015-03-19 15:33:21 -0600281 operation->Finish(request.additional_params, request.signature, &response->output);
282 operation_table_->Delete(request.op_handle);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600283}
284
Shawn Willdenb6837e72015-05-16 09:20:59 -0600285keymaster_error_t AndroidKeymaster::AbortOperation(const keymaster_operation_handle_t op_handle) {
Shawn Willden23d4a742015-03-19 15:33:21 -0600286 Operation* operation = operation_table_->Find(op_handle);
287 if (operation == NULL)
Shawn Willden1615f2e2014-08-13 10:37:40 -0600288 return KM_ERROR_INVALID_OPERATION_HANDLE;
Shawn Willden23d4a742015-03-19 15:33:21 -0600289
290 keymaster_error_t error = operation->Abort();
291 operation_table_->Delete(op_handle);
Shawn Willden7ec74f92014-12-11 13:57:59 -0700292 if (error != KM_ERROR_OK)
293 return error;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600294 return KM_ERROR_OK;
Shawn Willden76364712014-08-11 17:48:04 -0600295}
296
Shawn Willdenb6837e72015-05-16 09:20:59 -0600297void AndroidKeymaster::ExportKey(const ExportKeyRequest& request, ExportKeyResponse* response) {
Shawn Willdenffd790c2014-08-18 21:20:06 -0600298 if (response == NULL)
299 return;
300
Shawn Willden63ac0432014-12-29 14:07:08 -0700301 keymaster_algorithm_t algorithm;
Shawn Willdenf268d742014-08-19 15:36:26 -0600302 UniquePtr<Key> to_export(
Shawn Willden63ac0432014-12-29 14:07:08 -0700303 LoadKey(request.key_blob, request.additional_params, &algorithm, &response->error));
Shawn Willdenffd790c2014-08-18 21:20:06 -0600304 if (to_export.get() == NULL)
305 return;
306
Shawn Willdenf268d742014-08-19 15:36:26 -0600307 UniquePtr<uint8_t[]> out_key;
308 size_t size;
309 response->error = to_export->formatted_key_material(request.key_format, &out_key, &size);
310 if (response->error == KM_ERROR_OK) {
311 response->key_data = out_key.release();
312 response->key_data_length = size;
Shawn Willdenffd790c2014-08-18 21:20:06 -0600313 }
Shawn Willdenffd790c2014-08-18 21:20:06 -0600314}
315
Shawn Willdenb6837e72015-05-16 09:20:59 -0600316void AndroidKeymaster::ImportKey(const ImportKeyRequest& request, ImportKeyResponse* response) {
Shawn Willden437fbd12014-08-20 11:59:49 -0600317 if (response == NULL)
Shawn Willdenffd790c2014-08-18 21:20:06 -0600318 return;
Shawn Willdenffd790c2014-08-18 21:20:06 -0600319
Shawn Willdena278f612014-12-23 11:22:21 -0700320 keymaster_algorithm_t algorithm;
321 KeyFactory* factory = 0;
322 UniquePtr<Key> key;
323 if (!request.key_description.GetTagValue(TAG_ALGORITHM, &algorithm) ||
324 !(factory = KeyFactoryRegistry::Get(algorithm)))
325 response->error = KM_ERROR_UNSUPPORTED_ALGORITHM;
326 else
327 key.reset(factory->ImportKey(request.key_description, request.key_format, request.key_data,
Shawn Willden567a4a02014-12-31 12:14:46 -0700328 request.key_data_length, &response->error));
Shawn Willdena278f612014-12-23 11:22:21 -0700329
Shawn Willden437fbd12014-08-20 11:59:49 -0600330 if (response->error != KM_ERROR_OK)
Shawn Willdenffd790c2014-08-18 21:20:06 -0600331 return;
Shawn Willdenffd790c2014-08-18 21:20:06 -0600332
Shawn Willden437fbd12014-08-20 11:59:49 -0600333 response->error = SerializeKey(key.get(), KM_ORIGIN_IMPORTED, &response->key_blob,
334 &response->enforced, &response->unenforced);
Shawn Willdenffd790c2014-08-18 21:20:06 -0600335}
336
Shawn Willdenb6837e72015-05-16 09:20:59 -0600337keymaster_error_t AndroidKeymaster::SerializeKey(const Key* key, keymaster_key_origin_t origin,
338 keymaster_key_blob_t* keymaster_blob,
339 AuthorizationSet* enforced,
340 AuthorizationSet* unenforced) {
Shawn Willden437fbd12014-08-20 11:59:49 -0600341 keymaster_error_t error;
342
343 error = SetAuthorizations(key->authorizations(), origin, enforced, unenforced);
344 if (error != KM_ERROR_OK)
345 return error;
346
347 AuthorizationSet hidden_auths;
348 error = BuildHiddenAuthorizations(key->authorizations(), &hidden_auths);
349 if (error != KM_ERROR_OK)
350 return error;
351
352 UniquePtr<uint8_t[]> key_material;
353 size_t key_material_size;
354 error = key->key_material(&key_material, &key_material_size);
355 if (error != KM_ERROR_OK)
356 return error;
357
Shawn Willden39b970b2014-08-11 09:11:21 -0600358 uint8_t nonce[KeyBlob::NONCE_LENGTH];
359 GenerateNonce(nonce, array_size(nonce));
360
Shawn Willden72014ad2014-09-17 13:04:10 -0600361 keymaster_key_blob_t master_key = MasterKey();
362 UniquePtr<KeyBlob> blob(new UnencryptedKeyBlob(
363 *enforced, *unenforced, hidden_auths, key_material.get(), key_material_size,
364 master_key.key_material, master_key.key_material_size, nonce));
Shawn Willdend67afae2014-08-19 12:36:27 -0600365 if (blob.get() == NULL)
366 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
Shawn Willdend67afae2014-08-19 12:36:27 -0600367 if (blob->error() != KM_ERROR_OK)
Shawn Willden39b970b2014-08-11 09:11:21 -0600368 return blob->error();
Shawn Willden39b970b2014-08-11 09:11:21 -0600369
370 size_t size = blob->SerializedSize();
371 UniquePtr<uint8_t[]> blob_bytes(new uint8_t[size]);
Shawn Willdend67afae2014-08-19 12:36:27 -0600372 if (blob_bytes.get() == NULL)
373 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
374
Shawn Willden39b970b2014-08-11 09:11:21 -0600375 blob->Serialize(blob_bytes.get(), blob_bytes.get() + size);
Shawn Willden437fbd12014-08-20 11:59:49 -0600376 keymaster_blob->key_material_size = size;
377 keymaster_blob->key_material = blob_bytes.release();
Shawn Willdend67afae2014-08-19 12:36:27 -0600378
379 return KM_ERROR_OK;
380}
381
Shawn Willdenb6837e72015-05-16 09:20:59 -0600382Key* AndroidKeymaster::LoadKey(const keymaster_key_blob_t& key,
383 const AuthorizationSet& client_params,
384 keymaster_algorithm_t* algorithm, keymaster_error_t* error) {
Shawn Willden72014ad2014-09-17 13:04:10 -0600385 UniquePtr<UnencryptedKeyBlob> blob(LoadKeyBlob(key, client_params, error));
Shawn Willdend67afae2014-08-19 12:36:27 -0600386 if (*error != KM_ERROR_OK)
387 return NULL;
Shawn Willdena278f612014-12-23 11:22:21 -0700388
Shawn Willden63ac0432014-12-29 14:07:08 -0700389 *algorithm = blob->algorithm();
390
Shawn Willdena278f612014-12-23 11:22:21 -0700391 KeyFactory* factory = 0;
Shawn Willden63ac0432014-12-29 14:07:08 -0700392 if ((factory = KeyFactoryRegistry::Get(*algorithm)))
Shawn Willden567a4a02014-12-31 12:14:46 -0700393 return factory->LoadKey(*blob, error);
Shawn Willdena278f612014-12-23 11:22:21 -0700394 *error = KM_ERROR_UNSUPPORTED_ALGORITHM;
395 return NULL;
Shawn Willden128ffe02014-08-06 12:31:33 -0600396}
397
Shawn Willdenb6837e72015-05-16 09:20:59 -0600398UnencryptedKeyBlob* AndroidKeymaster::LoadKeyBlob(const keymaster_key_blob_t& key,
399 const AuthorizationSet& client_params,
400 keymaster_error_t* error) {
Shawn Willden1615f2e2014-08-13 10:37:40 -0600401 AuthorizationSet hidden;
402 BuildHiddenAuthorizations(client_params, &hidden);
Shawn Willden72014ad2014-09-17 13:04:10 -0600403 keymaster_key_blob_t master_key = MasterKey();
404 UniquePtr<UnencryptedKeyBlob> blob(
405 new UnencryptedKeyBlob(key, hidden, master_key.key_material, master_key.key_material_size));
Shawn Willden1615f2e2014-08-13 10:37:40 -0600406 if (blob.get() == NULL) {
407 *error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
408 return NULL;
409 } else if (blob->error() != KM_ERROR_OK) {
410 *error = blob->error();
411 return NULL;
412 }
Shawn Willdend67afae2014-08-19 12:36:27 -0600413 *error = KM_ERROR_OK;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600414 return blob.release();
415}
416
Shawn Willden437fbd12014-08-20 11:59:49 -0600417static keymaster_error_t TranslateAuthorizationSetError(AuthorizationSet::Error err) {
418 switch (err) {
Shawn Willden58e1a542014-08-08 21:58:29 -0600419 case AuthorizationSet::OK:
Shawn Willden128ffe02014-08-06 12:31:33 -0600420 return KM_ERROR_OK;
421 case AuthorizationSet::ALLOCATION_FAILURE:
422 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
Shawn Willden128ffe02014-08-06 12:31:33 -0600423 case AuthorizationSet::MALFORMED_DATA:
424 return KM_ERROR_UNKNOWN_ERROR;
425 }
426 return KM_ERROR_OK;
427}
428
Shawn Willdenb6837e72015-05-16 09:20:59 -0600429keymaster_error_t AndroidKeymaster::SetAuthorizations(const AuthorizationSet& key_description,
430 keymaster_key_origin_t origin,
431 AuthorizationSet* enforced,
432 AuthorizationSet* unenforced) {
Shawn Willden941d1c42014-12-11 13:57:02 -0700433 enforced->Clear();
434 unenforced->Clear();
Shawn Willden128ffe02014-08-06 12:31:33 -0600435 for (size_t i = 0; i < key_description.size(); ++i) {
436 switch (key_description[i].tag) {
Shawn Willden437fbd12014-08-20 11:59:49 -0600437 // These cannot be specified by the client.
Shawn Willden128ffe02014-08-06 12:31:33 -0600438 case KM_TAG_ROOT_OF_TRUST:
Shawn Willden128ffe02014-08-06 12:31:33 -0600439 case KM_TAG_ORIGIN:
Shawn Willdenf01329d2015-03-11 21:51:38 -0600440 LOG_E("Root of trust and origin tags may not be specified", 0);
Shawn Willden437fbd12014-08-20 11:59:49 -0600441 return KM_ERROR_INVALID_TAG;
442
443 // These don't work.
Shawn Willden128ffe02014-08-06 12:31:33 -0600444 case KM_TAG_ROLLBACK_RESISTANT:
Shawn Willdenf01329d2015-03-11 21:51:38 -0600445 LOG_E("KM_TAG_ROLLBACK_RESISTANT not supported", 0);
Shawn Willden437fbd12014-08-20 11:59:49 -0600446 return KM_ERROR_UNSUPPORTED_TAG;
447
448 // These are hidden.
449 case KM_TAG_APPLICATION_ID:
450 case KM_TAG_APPLICATION_DATA:
451 break;
452
453 // Everything else we just copy into the appropriate set.
Shawn Willden128ffe02014-08-06 12:31:33 -0600454 default:
Shawn Willden437fbd12014-08-20 11:59:49 -0600455 AddAuthorization(key_description[i], enforced, unenforced);
Shawn Willden128ffe02014-08-06 12:31:33 -0600456 break;
457 }
458 }
459
Shawn Willden437fbd12014-08-20 11:59:49 -0600460 AddAuthorization(Authorization(TAG_CREATION_DATETIME, java_time(time(NULL))), enforced,
461 unenforced);
462 AddAuthorization(Authorization(TAG_ORIGIN, origin), enforced, unenforced);
Shawn Willden128ffe02014-08-06 12:31:33 -0600463
Shawn Willden437fbd12014-08-20 11:59:49 -0600464 if (enforced->is_valid() != AuthorizationSet::OK)
465 return TranslateAuthorizationSetError(enforced->is_valid());
Shawn Willden128ffe02014-08-06 12:31:33 -0600466
Shawn Willden437fbd12014-08-20 11:59:49 -0600467 return TranslateAuthorizationSetError(unenforced->is_valid());
Shawn Willden128ffe02014-08-06 12:31:33 -0600468}
469
Shawn Willdenb6837e72015-05-16 09:20:59 -0600470keymaster_error_t AndroidKeymaster::BuildHiddenAuthorizations(const AuthorizationSet& input_set,
471 AuthorizationSet* hidden) {
Shawn Willden76364712014-08-11 17:48:04 -0600472 keymaster_blob_t entry;
473 if (input_set.GetTagValue(TAG_APPLICATION_ID, &entry))
474 hidden->push_back(TAG_APPLICATION_ID, entry.data, entry.data_length);
475 if (input_set.GetTagValue(TAG_APPLICATION_DATA, &entry))
476 hidden->push_back(TAG_APPLICATION_DATA, entry.data, entry.data_length);
477 hidden->push_back(RootOfTrustTag());
478
Shawn Willden437fbd12014-08-20 11:59:49 -0600479 return TranslateAuthorizationSetError(hidden->is_valid());
Shawn Willden76364712014-08-11 17:48:04 -0600480}
481
Shawn Willdenb6837e72015-05-16 09:20:59 -0600482void AndroidKeymaster::AddAuthorization(const keymaster_key_param_t& auth,
483 AuthorizationSet* enforced, AuthorizationSet* unenforced) {
Shawn Willden437fbd12014-08-20 11:59:49 -0600484 if (is_enforced(auth.tag))
485 enforced->push_back(auth);
486 else
487 unenforced->push_back(auth);
Shawn Willden128ffe02014-08-06 12:31:33 -0600488}
489
Shawn Willden128ffe02014-08-06 12:31:33 -0600490} // namespace keymaster