blob: 12fdc18f17cf2129dec90dc506f417d01e4bd790 [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 Willden567a4a02014-12-31 12:14:46 -070017#include <keymaster/google_keymaster.h>
18
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 Willden98d9b922014-08-26 08:14:10 -060029#include <keymaster/google_keymaster_utils.h>
30
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 Willden567a4a02014-12-31 12:14:46 -070044GoogleKeymaster::GoogleKeymaster(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 Willden39b970b2014-08-11 09:11:21 -060048GoogleKeymaster::~GoogleKeymaster() {
49}
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
57// specific modes, padding, etc. are supported for that key type, and GoogleKeymaster also has
58// methods that return the same information. They'll get out of sync. Best to put the knowledge in
59// the keytypes and provide some mechanism for GoogleKeymaster to query the keytypes for the
60// 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 Willden2665e862014-11-24 14:46:21 -070071void GoogleKeymaster::GetVersion(const GetVersionRequest&, GetVersionResponse* rsp) {
72 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 Willden3809b932014-12-02 06:59:46 -070081void GoogleKeymaster::SupportedAlgorithms(
82 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
138void GoogleKeymaster::SupportedBlockModes(
139 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
144void GoogleKeymaster::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 Willden63ac0432014-12-29 14:07:08 -0700150void GoogleKeymaster::SupportedDigests(keymaster_algorithm_t algorithm, keymaster_purpose_t purpose,
Shawn Willden128ffe02014-08-06 12:31:33 -0600151 SupportedResponse<keymaster_digest_t>* response) const {
Shawn Willden63ac0432014-12-29 14:07:08 -0700152 GetSupported(algorithm, purpose, &OperationFactory::SupportedDigests, response);
Shawn Willden128ffe02014-08-06 12:31:33 -0600153}
154
Shawn Willden3809b932014-12-02 06:59:46 -0700155void GoogleKeymaster::SupportedImportFormats(
156 keymaster_algorithm_t algorithm, SupportedResponse<keymaster_key_format_t>* response) const {
Shawn Willden128ffe02014-08-06 12:31:33 -0600157 if (response == NULL || !check_supported(algorithm, response))
158 return;
159
Shawn Willdena278f612014-12-23 11:22:21 -0700160 size_t count;
161 const keymaster_key_format_t* formats =
162 KeyFactoryRegistry::Get(algorithm)->SupportedImportFormats(&count);
163 response->SetResults(formats, count);
Shawn Willden128ffe02014-08-06 12:31:33 -0600164}
165
Shawn Willden3809b932014-12-02 06:59:46 -0700166void GoogleKeymaster::SupportedExportFormats(
167 keymaster_algorithm_t algorithm, SupportedResponse<keymaster_key_format_t>* response) const {
Shawn Willden128ffe02014-08-06 12:31:33 -0600168 if (response == NULL || !check_supported(algorithm, response))
169 return;
170
Shawn Willdena278f612014-12-23 11:22:21 -0700171 size_t count;
172 const keymaster_key_format_t* formats =
173 KeyFactoryRegistry::Get(algorithm)->SupportedExportFormats(&count);
174 response->SetResults(formats, count);
Shawn Willden128ffe02014-08-06 12:31:33 -0600175}
176
Shawn Willden128ffe02014-08-06 12:31:33 -0600177void GoogleKeymaster::GenerateKey(const GenerateKeyRequest& request,
178 GenerateKeyResponse* response) {
179 if (response == NULL)
180 return;
Shawn Willden128ffe02014-08-06 12:31:33 -0600181
Shawn Willdena278f612014-12-23 11:22:21 -0700182 keymaster_algorithm_t algorithm;
183 KeyFactory* factory = 0;
184 UniquePtr<Key> key;
185 if (!request.key_description.GetTagValue(TAG_ALGORITHM, &algorithm) ||
186 !(factory = KeyFactoryRegistry::Get(algorithm)))
187 response->error = KM_ERROR_UNSUPPORTED_ALGORITHM;
188 else
Shawn Willden567a4a02014-12-31 12:14:46 -0700189 key.reset(factory->GenerateKey(request.key_description, &response->error));
Shawn Willdena278f612014-12-23 11:22:21 -0700190
Shawn Willden76364712014-08-11 17:48:04 -0600191 if (response->error != KM_ERROR_OK)
Shawn Willden128ffe02014-08-06 12:31:33 -0600192 return;
193
Shawn Willden0b2d3332015-04-07 17:46:18 -0600194 response->error = SerializeKey(key.get(), KM_ORIGIN_GENERATED, &response->key_blob,
195 &response->enforced, &response->unenforced);
Shawn Willden128ffe02014-08-06 12:31:33 -0600196}
197
Shawn Willden76364712014-08-11 17:48:04 -0600198void GoogleKeymaster::GetKeyCharacteristics(const GetKeyCharacteristicsRequest& request,
199 GetKeyCharacteristicsResponse* response) {
Shawn Willden1615f2e2014-08-13 10:37:40 -0600200 if (response == NULL)
201 return;
Shawn Willden76364712014-08-11 17:48:04 -0600202
Shawn Willden1615f2e2014-08-13 10:37:40 -0600203 UniquePtr<KeyBlob> blob(
204 LoadKeyBlob(request.key_blob, request.additional_params, &(response->error)));
205 if (blob.get() == NULL)
206 return;
207
208 response->enforced.Reinitialize(blob->enforced());
209 response->unenforced.Reinitialize(blob->unenforced());
Shawn Willden1615f2e2014-08-13 10:37:40 -0600210}
211
212void GoogleKeymaster::BeginOperation(const BeginOperationRequest& request,
213 BeginOperationResponse* response) {
214 if (response == NULL)
215 return;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600216 response->op_handle = 0;
217
Shawn Willden63ac0432014-12-29 14:07:08 -0700218 keymaster_algorithm_t algorithm;
219 UniquePtr<Key> key(
220 LoadKey(request.key_blob, request.additional_params, &algorithm, &response->error));
Shawn Willden1615f2e2014-08-13 10:37:40 -0600221 if (key.get() == NULL)
222 return;
223
Shawn Willdenedb79942015-05-08 06:46:44 -0600224 // TODO(swillden): Move this check to a general authorization checker.
Shawn Willden5fad7852015-01-26 16:10:56 -0700225 if (key->rescopable()) {
Shawn Willdenf01329d2015-03-11 21:51:38 -0600226 response->error = KM_ERROR_RESCOPABLE_KEY_NOT_USABLE;
Shawn Willden5fad7852015-01-26 16:10:56 -0700227 return;
228 }
229
Shawn Willdenedb79942015-05-08 06:46:44 -0600230 // TODO(swillden): Move this check to a general authorization checker.
231 if (!key->authorizations().Contains(TAG_PURPOSE, request.purpose)) {
232 // TODO(swillden): Consider introducing error codes for unauthorized usages.
233 response->error = KM_ERROR_INCOMPATIBLE_PURPOSE;
234 return;
235 }
236
Shawn Willden63ac0432014-12-29 14:07:08 -0700237 OperationFactory::KeyType op_type(algorithm, request.purpose);
238 OperationFactory* factory = OperationFactoryRegistry::Get(op_type);
239 if (!factory) {
240 response->error = KM_ERROR_UNSUPPORTED_PURPOSE;
241 return;
242 }
243
Shawn Willden3ed6d062015-04-15 13:39:38 -0600244 UniquePtr<Operation> operation(
245 factory->CreateOperation(*key, request.additional_params, &response->error));
Shawn Willdend67afae2014-08-19 12:36:27 -0600246 if (operation.get() == NULL)
Shawn Willden76364712014-08-11 17:48:04 -0600247 return;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600248
Shawn Willden111edb32015-02-05 22:44:24 -0700249 response->output_params.Clear();
250 response->error = operation->Begin(request.additional_params, &response->output_params);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600251 if (response->error != KM_ERROR_OK)
252 return;
253
Shawn Willden23d4a742015-03-19 15:33:21 -0600254 response->error = operation_table_->Add(operation.release(), &response->op_handle);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600255}
256
257void GoogleKeymaster::UpdateOperation(const UpdateOperationRequest& request,
258 UpdateOperationResponse* response) {
Shawn Willdend6cd7e32014-12-17 08:01:26 -0700259 if (response == NULL)
260 return;
261
Shawn Willden23d4a742015-03-19 15:33:21 -0600262 response->error = KM_ERROR_INVALID_OPERATION_HANDLE;
263 Operation* operation = operation_table_->Find(request.op_handle);
264 if (operation == NULL)
Shawn Willden1615f2e2014-08-13 10:37:40 -0600265 return;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600266
Shawn Willden23d4a742015-03-19 15:33:21 -0600267 response->error = operation->Update(request.additional_params, request.input, &response->output,
268 &response->input_consumed);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600269 if (response->error != KM_ERROR_OK) {
270 // Any error invalidates the operation.
Shawn Willden23d4a742015-03-19 15:33:21 -0600271 operation_table_->Delete(request.op_handle);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600272 }
273}
274
Shawn Willden43e999e2014-08-13 13:29:50 -0600275void GoogleKeymaster::FinishOperation(const FinishOperationRequest& request,
Shawn Willden1615f2e2014-08-13 10:37:40 -0600276 FinishOperationResponse* response) {
Shawn Willdend6cd7e32014-12-17 08:01:26 -0700277 if (response == NULL)
278 return;
279
Shawn Willden23d4a742015-03-19 15:33:21 -0600280 response->error = KM_ERROR_INVALID_OPERATION_HANDLE;
281 Operation* operation = operation_table_->Find(request.op_handle);
282 if (operation == NULL)
Shawn Willden1615f2e2014-08-13 10:37:40 -0600283 return;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600284
Shawn Willden6bfbff02015-02-06 19:48:24 -0700285 response->error =
Shawn Willden23d4a742015-03-19 15:33:21 -0600286 operation->Finish(request.additional_params, request.signature, &response->output);
287 operation_table_->Delete(request.op_handle);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600288}
289
290keymaster_error_t GoogleKeymaster::AbortOperation(const keymaster_operation_handle_t op_handle) {
Shawn Willden23d4a742015-03-19 15:33:21 -0600291 Operation* operation = operation_table_->Find(op_handle);
292 if (operation == NULL)
Shawn Willden1615f2e2014-08-13 10:37:40 -0600293 return KM_ERROR_INVALID_OPERATION_HANDLE;
Shawn Willden23d4a742015-03-19 15:33:21 -0600294
295 keymaster_error_t error = operation->Abort();
296 operation_table_->Delete(op_handle);
Shawn Willden7ec74f92014-12-11 13:57:59 -0700297 if (error != KM_ERROR_OK)
298 return error;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600299 return KM_ERROR_OK;
Shawn Willden76364712014-08-11 17:48:04 -0600300}
301
Shawn Willdenffd790c2014-08-18 21:20:06 -0600302void GoogleKeymaster::ExportKey(const ExportKeyRequest& request, ExportKeyResponse* response) {
303 if (response == NULL)
304 return;
305
Shawn Willden63ac0432014-12-29 14:07:08 -0700306 keymaster_algorithm_t algorithm;
Shawn Willdenf268d742014-08-19 15:36:26 -0600307 UniquePtr<Key> to_export(
Shawn Willden63ac0432014-12-29 14:07:08 -0700308 LoadKey(request.key_blob, request.additional_params, &algorithm, &response->error));
Shawn Willdenffd790c2014-08-18 21:20:06 -0600309 if (to_export.get() == NULL)
310 return;
311
Shawn Willdenf268d742014-08-19 15:36:26 -0600312 UniquePtr<uint8_t[]> out_key;
313 size_t size;
314 response->error = to_export->formatted_key_material(request.key_format, &out_key, &size);
315 if (response->error == KM_ERROR_OK) {
316 response->key_data = out_key.release();
317 response->key_data_length = size;
Shawn Willdenffd790c2014-08-18 21:20:06 -0600318 }
Shawn Willdenffd790c2014-08-18 21:20:06 -0600319}
320
Shawn Willden5fad7852015-01-26 16:10:56 -0700321void GoogleKeymaster::Rescope(const RescopeRequest& request, RescopeResponse* response) {
322 if (response == NULL)
323 return;
324
325 UniquePtr<UnencryptedKeyBlob> blob(
326 LoadKeyBlob(request.key_blob, request.additional_params, &response->error));
327 if (response->error != KM_ERROR_OK)
328 return;
329
330 KeyFactory* factory = KeyFactoryRegistry::Get(blob->algorithm());
331 if (!factory) {
332 response->error = KM_ERROR_UNSUPPORTED_ALGORITHM;
333 return;
334 }
335
336 UniquePtr<Key> key;
337 key.reset(factory->RescopeKey(*blob, request.new_authorizations, &response->error));
338 if (response->error != KM_ERROR_OK)
339 return;
340
Shawn Willden0b2d3332015-04-07 17:46:18 -0600341 assert(is_hardware() == blob->is_hardware());
Shawn Willden5fad7852015-01-26 16:10:56 -0700342 response->error = SerializeKey(key.get(), blob->origin(), &response->key_blob,
343 &response->enforced, &response->unenforced);
344}
Shawn Willdenffd790c2014-08-18 21:20:06 -0600345void GoogleKeymaster::ImportKey(const ImportKeyRequest& request, ImportKeyResponse* response) {
Shawn Willden437fbd12014-08-20 11:59:49 -0600346 if (response == NULL)
Shawn Willdenffd790c2014-08-18 21:20:06 -0600347 return;
Shawn Willdenffd790c2014-08-18 21:20:06 -0600348
Shawn Willdena278f612014-12-23 11:22:21 -0700349 keymaster_algorithm_t algorithm;
350 KeyFactory* factory = 0;
351 UniquePtr<Key> key;
352 if (!request.key_description.GetTagValue(TAG_ALGORITHM, &algorithm) ||
353 !(factory = KeyFactoryRegistry::Get(algorithm)))
354 response->error = KM_ERROR_UNSUPPORTED_ALGORITHM;
355 else
356 key.reset(factory->ImportKey(request.key_description, request.key_format, request.key_data,
Shawn Willden567a4a02014-12-31 12:14:46 -0700357 request.key_data_length, &response->error));
Shawn Willdena278f612014-12-23 11:22:21 -0700358
Shawn Willden437fbd12014-08-20 11:59:49 -0600359 if (response->error != KM_ERROR_OK)
Shawn Willdenffd790c2014-08-18 21:20:06 -0600360 return;
Shawn Willdenffd790c2014-08-18 21:20:06 -0600361
Shawn Willden437fbd12014-08-20 11:59:49 -0600362 response->error = SerializeKey(key.get(), KM_ORIGIN_IMPORTED, &response->key_blob,
363 &response->enforced, &response->unenforced);
Shawn Willdenffd790c2014-08-18 21:20:06 -0600364}
365
Shawn Willden437fbd12014-08-20 11:59:49 -0600366keymaster_error_t GoogleKeymaster::SerializeKey(const Key* key, keymaster_key_origin_t origin,
367 keymaster_key_blob_t* keymaster_blob,
368 AuthorizationSet* enforced,
369 AuthorizationSet* unenforced) {
Shawn Willden437fbd12014-08-20 11:59:49 -0600370 keymaster_error_t error;
371
372 error = SetAuthorizations(key->authorizations(), origin, enforced, unenforced);
373 if (error != KM_ERROR_OK)
374 return error;
375
376 AuthorizationSet hidden_auths;
377 error = BuildHiddenAuthorizations(key->authorizations(), &hidden_auths);
378 if (error != KM_ERROR_OK)
379 return error;
380
381 UniquePtr<uint8_t[]> key_material;
382 size_t key_material_size;
383 error = key->key_material(&key_material, &key_material_size);
384 if (error != KM_ERROR_OK)
385 return error;
386
Shawn Willden39b970b2014-08-11 09:11:21 -0600387 uint8_t nonce[KeyBlob::NONCE_LENGTH];
388 GenerateNonce(nonce, array_size(nonce));
389
Shawn Willden72014ad2014-09-17 13:04:10 -0600390 keymaster_key_blob_t master_key = MasterKey();
391 UniquePtr<KeyBlob> blob(new UnencryptedKeyBlob(
392 *enforced, *unenforced, hidden_auths, key_material.get(), key_material_size,
393 master_key.key_material, master_key.key_material_size, nonce));
Shawn Willdend67afae2014-08-19 12:36:27 -0600394 if (blob.get() == NULL)
395 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
Shawn Willdend67afae2014-08-19 12:36:27 -0600396 if (blob->error() != KM_ERROR_OK)
Shawn Willden39b970b2014-08-11 09:11:21 -0600397 return blob->error();
Shawn Willden39b970b2014-08-11 09:11:21 -0600398
399 size_t size = blob->SerializedSize();
400 UniquePtr<uint8_t[]> blob_bytes(new uint8_t[size]);
Shawn Willdend67afae2014-08-19 12:36:27 -0600401 if (blob_bytes.get() == NULL)
402 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
403
Shawn Willden39b970b2014-08-11 09:11:21 -0600404 blob->Serialize(blob_bytes.get(), blob_bytes.get() + size);
Shawn Willden437fbd12014-08-20 11:59:49 -0600405 keymaster_blob->key_material_size = size;
406 keymaster_blob->key_material = blob_bytes.release();
Shawn Willdend67afae2014-08-19 12:36:27 -0600407
408 return KM_ERROR_OK;
409}
410
411Key* GoogleKeymaster::LoadKey(const keymaster_key_blob_t& key,
Shawn Willden63ac0432014-12-29 14:07:08 -0700412 const AuthorizationSet& client_params,
413 keymaster_algorithm_t* algorithm, keymaster_error_t* error) {
Shawn Willden72014ad2014-09-17 13:04:10 -0600414 UniquePtr<UnencryptedKeyBlob> blob(LoadKeyBlob(key, client_params, error));
Shawn Willdend67afae2014-08-19 12:36:27 -0600415 if (*error != KM_ERROR_OK)
416 return NULL;
Shawn Willdena278f612014-12-23 11:22:21 -0700417
Shawn Willden63ac0432014-12-29 14:07:08 -0700418 *algorithm = blob->algorithm();
419
Shawn Willdena278f612014-12-23 11:22:21 -0700420 KeyFactory* factory = 0;
Shawn Willden63ac0432014-12-29 14:07:08 -0700421 if ((factory = KeyFactoryRegistry::Get(*algorithm)))
Shawn Willden567a4a02014-12-31 12:14:46 -0700422 return factory->LoadKey(*blob, error);
Shawn Willdena278f612014-12-23 11:22:21 -0700423 *error = KM_ERROR_UNSUPPORTED_ALGORITHM;
424 return NULL;
Shawn Willden128ffe02014-08-06 12:31:33 -0600425}
426
Shawn Willden72014ad2014-09-17 13:04:10 -0600427UnencryptedKeyBlob* GoogleKeymaster::LoadKeyBlob(const keymaster_key_blob_t& key,
428 const AuthorizationSet& client_params,
429 keymaster_error_t* error) {
Shawn Willden1615f2e2014-08-13 10:37:40 -0600430 AuthorizationSet hidden;
431 BuildHiddenAuthorizations(client_params, &hidden);
Shawn Willden72014ad2014-09-17 13:04:10 -0600432 keymaster_key_blob_t master_key = MasterKey();
433 UniquePtr<UnencryptedKeyBlob> blob(
434 new UnencryptedKeyBlob(key, hidden, master_key.key_material, master_key.key_material_size));
Shawn Willden1615f2e2014-08-13 10:37:40 -0600435 if (blob.get() == NULL) {
436 *error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
437 return NULL;
438 } else if (blob->error() != KM_ERROR_OK) {
439 *error = blob->error();
440 return NULL;
441 }
Shawn Willdend67afae2014-08-19 12:36:27 -0600442 *error = KM_ERROR_OK;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600443 return blob.release();
444}
445
Shawn Willden437fbd12014-08-20 11:59:49 -0600446static keymaster_error_t TranslateAuthorizationSetError(AuthorizationSet::Error err) {
447 switch (err) {
Shawn Willden58e1a542014-08-08 21:58:29 -0600448 case AuthorizationSet::OK:
Shawn Willden128ffe02014-08-06 12:31:33 -0600449 return KM_ERROR_OK;
450 case AuthorizationSet::ALLOCATION_FAILURE:
451 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
Shawn Willden128ffe02014-08-06 12:31:33 -0600452 case AuthorizationSet::MALFORMED_DATA:
453 return KM_ERROR_UNKNOWN_ERROR;
454 }
455 return KM_ERROR_OK;
456}
457
Shawn Willden437fbd12014-08-20 11:59:49 -0600458keymaster_error_t GoogleKeymaster::SetAuthorizations(const AuthorizationSet& key_description,
459 keymaster_key_origin_t origin,
460 AuthorizationSet* enforced,
461 AuthorizationSet* unenforced) {
Shawn Willden941d1c42014-12-11 13:57:02 -0700462 enforced->Clear();
463 unenforced->Clear();
Shawn Willden128ffe02014-08-06 12:31:33 -0600464 for (size_t i = 0; i < key_description.size(); ++i) {
465 switch (key_description[i].tag) {
Shawn Willden437fbd12014-08-20 11:59:49 -0600466 // These cannot be specified by the client.
Shawn Willden128ffe02014-08-06 12:31:33 -0600467 case KM_TAG_ROOT_OF_TRUST:
Shawn Willden128ffe02014-08-06 12:31:33 -0600468 case KM_TAG_ORIGIN:
Shawn Willdenf01329d2015-03-11 21:51:38 -0600469 LOG_E("Root of trust and origin tags may not be specified", 0);
Shawn Willden437fbd12014-08-20 11:59:49 -0600470 return KM_ERROR_INVALID_TAG;
471
472 // These don't work.
Shawn Willden128ffe02014-08-06 12:31:33 -0600473 case KM_TAG_ROLLBACK_RESISTANT:
Shawn Willdenf01329d2015-03-11 21:51:38 -0600474 LOG_E("KM_TAG_ROLLBACK_RESISTANT not supported", 0);
Shawn Willden437fbd12014-08-20 11:59:49 -0600475 return KM_ERROR_UNSUPPORTED_TAG;
476
477 // These are hidden.
478 case KM_TAG_APPLICATION_ID:
479 case KM_TAG_APPLICATION_DATA:
480 break;
481
482 // Everything else we just copy into the appropriate set.
Shawn Willden128ffe02014-08-06 12:31:33 -0600483 default:
Shawn Willden437fbd12014-08-20 11:59:49 -0600484 AddAuthorization(key_description[i], enforced, unenforced);
Shawn Willden128ffe02014-08-06 12:31:33 -0600485 break;
486 }
487 }
488
Shawn Willden437fbd12014-08-20 11:59:49 -0600489 AddAuthorization(Authorization(TAG_CREATION_DATETIME, java_time(time(NULL))), enforced,
490 unenforced);
491 AddAuthorization(Authorization(TAG_ORIGIN, origin), enforced, unenforced);
Shawn Willden128ffe02014-08-06 12:31:33 -0600492
Shawn Willden437fbd12014-08-20 11:59:49 -0600493 if (enforced->is_valid() != AuthorizationSet::OK)
494 return TranslateAuthorizationSetError(enforced->is_valid());
Shawn Willden128ffe02014-08-06 12:31:33 -0600495
Shawn Willden437fbd12014-08-20 11:59:49 -0600496 return TranslateAuthorizationSetError(unenforced->is_valid());
Shawn Willden128ffe02014-08-06 12:31:33 -0600497}
498
Shawn Willden76364712014-08-11 17:48:04 -0600499keymaster_error_t GoogleKeymaster::BuildHiddenAuthorizations(const AuthorizationSet& input_set,
Shawn Willden1615f2e2014-08-13 10:37:40 -0600500 AuthorizationSet* hidden) {
Shawn Willden76364712014-08-11 17:48:04 -0600501 keymaster_blob_t entry;
502 if (input_set.GetTagValue(TAG_APPLICATION_ID, &entry))
503 hidden->push_back(TAG_APPLICATION_ID, entry.data, entry.data_length);
504 if (input_set.GetTagValue(TAG_APPLICATION_DATA, &entry))
505 hidden->push_back(TAG_APPLICATION_DATA, entry.data, entry.data_length);
506 hidden->push_back(RootOfTrustTag());
507
Shawn Willden437fbd12014-08-20 11:59:49 -0600508 return TranslateAuthorizationSetError(hidden->is_valid());
Shawn Willden76364712014-08-11 17:48:04 -0600509}
510
Shawn Willden437fbd12014-08-20 11:59:49 -0600511void GoogleKeymaster::AddAuthorization(const keymaster_key_param_t& auth,
512 AuthorizationSet* enforced, AuthorizationSet* unenforced) {
513 if (is_enforced(auth.tag))
514 enforced->push_back(auth);
515 else
516 unenforced->push_back(auth);
Shawn Willden128ffe02014-08-06 12:31:33 -0600517}
518
Shawn Willden128ffe02014-08-06 12:31:33 -0600519} // namespace keymaster