blob: 25ea7ab201ac0c82a0859d56ada01cb410a573dd [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 Willden437fbd12014-08-20 11:59:49 -0600194 response->error = SerializeKey(key.get(), origin(), &response->key_blob, &response->enforced,
195 &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 Willden5fad7852015-01-26 16:10:56 -0700224 if (key->rescopable()) {
Shawn Willdenf01329d2015-03-11 21:51:38 -0600225 response->error = KM_ERROR_RESCOPABLE_KEY_NOT_USABLE;
Shawn Willden5fad7852015-01-26 16:10:56 -0700226 return;
227 }
228
Shawn Willden63ac0432014-12-29 14:07:08 -0700229 OperationFactory::KeyType op_type(algorithm, request.purpose);
230 OperationFactory* factory = OperationFactoryRegistry::Get(op_type);
231 if (!factory) {
232 response->error = KM_ERROR_UNSUPPORTED_PURPOSE;
233 return;
234 }
235
Shawn Willden567a4a02014-12-31 12:14:46 -0700236 UniquePtr<Operation> operation(factory->CreateOperation(*key, &response->error));
Shawn Willdend67afae2014-08-19 12:36:27 -0600237 if (operation.get() == NULL)
Shawn Willden76364712014-08-11 17:48:04 -0600238 return;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600239
Shawn Willden111edb32015-02-05 22:44:24 -0700240 response->output_params.Clear();
241 response->error = operation->Begin(request.additional_params, &response->output_params);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600242 if (response->error != KM_ERROR_OK)
243 return;
244
Shawn Willden23d4a742015-03-19 15:33:21 -0600245 response->error = operation_table_->Add(operation.release(), &response->op_handle);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600246}
247
248void GoogleKeymaster::UpdateOperation(const UpdateOperationRequest& request,
249 UpdateOperationResponse* response) {
Shawn Willdend6cd7e32014-12-17 08:01:26 -0700250 if (response == NULL)
251 return;
252
Shawn Willden23d4a742015-03-19 15:33:21 -0600253 response->error = KM_ERROR_INVALID_OPERATION_HANDLE;
254 Operation* operation = operation_table_->Find(request.op_handle);
255 if (operation == NULL)
Shawn Willden1615f2e2014-08-13 10:37:40 -0600256 return;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600257
Shawn Willden23d4a742015-03-19 15:33:21 -0600258 response->error = operation->Update(request.additional_params, request.input, &response->output,
259 &response->input_consumed);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600260 if (response->error != KM_ERROR_OK) {
261 // Any error invalidates the operation.
Shawn Willden23d4a742015-03-19 15:33:21 -0600262 operation_table_->Delete(request.op_handle);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600263 }
264}
265
Shawn Willden43e999e2014-08-13 13:29:50 -0600266void GoogleKeymaster::FinishOperation(const FinishOperationRequest& request,
Shawn Willden1615f2e2014-08-13 10:37:40 -0600267 FinishOperationResponse* response) {
Shawn Willdend6cd7e32014-12-17 08:01:26 -0700268 if (response == NULL)
269 return;
270
Shawn Willden23d4a742015-03-19 15:33:21 -0600271 response->error = KM_ERROR_INVALID_OPERATION_HANDLE;
272 Operation* operation = operation_table_->Find(request.op_handle);
273 if (operation == NULL)
Shawn Willden1615f2e2014-08-13 10:37:40 -0600274 return;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600275
Shawn Willden6bfbff02015-02-06 19:48:24 -0700276 response->error =
Shawn Willden23d4a742015-03-19 15:33:21 -0600277 operation->Finish(request.additional_params, request.signature, &response->output);
278 operation_table_->Delete(request.op_handle);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600279}
280
281keymaster_error_t GoogleKeymaster::AbortOperation(const keymaster_operation_handle_t op_handle) {
Shawn Willden23d4a742015-03-19 15:33:21 -0600282 Operation* operation = operation_table_->Find(op_handle);
283 if (operation == NULL)
Shawn Willden1615f2e2014-08-13 10:37:40 -0600284 return KM_ERROR_INVALID_OPERATION_HANDLE;
Shawn Willden23d4a742015-03-19 15:33:21 -0600285
286 keymaster_error_t error = operation->Abort();
287 operation_table_->Delete(op_handle);
Shawn Willden7ec74f92014-12-11 13:57:59 -0700288 if (error != KM_ERROR_OK)
289 return error;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600290 return KM_ERROR_OK;
Shawn Willden76364712014-08-11 17:48:04 -0600291}
292
Shawn Willdenffd790c2014-08-18 21:20:06 -0600293void GoogleKeymaster::ExportKey(const ExportKeyRequest& request, ExportKeyResponse* response) {
294 if (response == NULL)
295 return;
296
Shawn Willden63ac0432014-12-29 14:07:08 -0700297 keymaster_algorithm_t algorithm;
Shawn Willdenf268d742014-08-19 15:36:26 -0600298 UniquePtr<Key> to_export(
Shawn Willden63ac0432014-12-29 14:07:08 -0700299 LoadKey(request.key_blob, request.additional_params, &algorithm, &response->error));
Shawn Willdenffd790c2014-08-18 21:20:06 -0600300 if (to_export.get() == NULL)
301 return;
302
Shawn Willdenf268d742014-08-19 15:36:26 -0600303 UniquePtr<uint8_t[]> out_key;
304 size_t size;
305 response->error = to_export->formatted_key_material(request.key_format, &out_key, &size);
306 if (response->error == KM_ERROR_OK) {
307 response->key_data = out_key.release();
308 response->key_data_length = size;
Shawn Willdenffd790c2014-08-18 21:20:06 -0600309 }
Shawn Willdenffd790c2014-08-18 21:20:06 -0600310}
311
Shawn Willden5fad7852015-01-26 16:10:56 -0700312void GoogleKeymaster::Rescope(const RescopeRequest& request, RescopeResponse* response) {
313 if (response == NULL)
314 return;
315
316 UniquePtr<UnencryptedKeyBlob> blob(
317 LoadKeyBlob(request.key_blob, request.additional_params, &response->error));
318 if (response->error != KM_ERROR_OK)
319 return;
320
321 KeyFactory* factory = KeyFactoryRegistry::Get(blob->algorithm());
322 if (!factory) {
323 response->error = KM_ERROR_UNSUPPORTED_ALGORITHM;
324 return;
325 }
326
327 UniquePtr<Key> key;
328 key.reset(factory->RescopeKey(*blob, request.new_authorizations, &response->error));
329 if (response->error != KM_ERROR_OK)
330 return;
331
332 response->error = SerializeKey(key.get(), blob->origin(), &response->key_blob,
333 &response->enforced, &response->unenforced);
334}
Shawn Willdenffd790c2014-08-18 21:20:06 -0600335void GoogleKeymaster::ImportKey(const ImportKeyRequest& request, ImportKeyResponse* response) {
Shawn Willden437fbd12014-08-20 11:59:49 -0600336 if (response == NULL)
Shawn Willdenffd790c2014-08-18 21:20:06 -0600337 return;
Shawn Willdenffd790c2014-08-18 21:20:06 -0600338
Shawn Willdena278f612014-12-23 11:22:21 -0700339 keymaster_algorithm_t algorithm;
340 KeyFactory* factory = 0;
341 UniquePtr<Key> key;
342 if (!request.key_description.GetTagValue(TAG_ALGORITHM, &algorithm) ||
343 !(factory = KeyFactoryRegistry::Get(algorithm)))
344 response->error = KM_ERROR_UNSUPPORTED_ALGORITHM;
345 else
346 key.reset(factory->ImportKey(request.key_description, request.key_format, request.key_data,
Shawn Willden567a4a02014-12-31 12:14:46 -0700347 request.key_data_length, &response->error));
Shawn Willdena278f612014-12-23 11:22:21 -0700348
Shawn Willden437fbd12014-08-20 11:59:49 -0600349 if (response->error != KM_ERROR_OK)
Shawn Willdenffd790c2014-08-18 21:20:06 -0600350 return;
Shawn Willdenffd790c2014-08-18 21:20:06 -0600351
Shawn Willden437fbd12014-08-20 11:59:49 -0600352 response->error = SerializeKey(key.get(), KM_ORIGIN_IMPORTED, &response->key_blob,
353 &response->enforced, &response->unenforced);
Shawn Willdenffd790c2014-08-18 21:20:06 -0600354}
355
Shawn Willden437fbd12014-08-20 11:59:49 -0600356keymaster_error_t GoogleKeymaster::SerializeKey(const Key* key, keymaster_key_origin_t origin,
357 keymaster_key_blob_t* keymaster_blob,
358 AuthorizationSet* enforced,
359 AuthorizationSet* unenforced) {
Shawn Willden437fbd12014-08-20 11:59:49 -0600360 keymaster_error_t error;
361
362 error = SetAuthorizations(key->authorizations(), origin, enforced, unenforced);
363 if (error != KM_ERROR_OK)
364 return error;
365
366 AuthorizationSet hidden_auths;
367 error = BuildHiddenAuthorizations(key->authorizations(), &hidden_auths);
368 if (error != KM_ERROR_OK)
369 return error;
370
371 UniquePtr<uint8_t[]> key_material;
372 size_t key_material_size;
373 error = key->key_material(&key_material, &key_material_size);
374 if (error != KM_ERROR_OK)
375 return error;
376
Shawn Willden39b970b2014-08-11 09:11:21 -0600377 uint8_t nonce[KeyBlob::NONCE_LENGTH];
378 GenerateNonce(nonce, array_size(nonce));
379
Shawn Willden72014ad2014-09-17 13:04:10 -0600380 keymaster_key_blob_t master_key = MasterKey();
381 UniquePtr<KeyBlob> blob(new UnencryptedKeyBlob(
382 *enforced, *unenforced, hidden_auths, key_material.get(), key_material_size,
383 master_key.key_material, master_key.key_material_size, nonce));
Shawn Willdend67afae2014-08-19 12:36:27 -0600384 if (blob.get() == NULL)
385 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
Shawn Willdend67afae2014-08-19 12:36:27 -0600386 if (blob->error() != KM_ERROR_OK)
Shawn Willden39b970b2014-08-11 09:11:21 -0600387 return blob->error();
Shawn Willden39b970b2014-08-11 09:11:21 -0600388
389 size_t size = blob->SerializedSize();
390 UniquePtr<uint8_t[]> blob_bytes(new uint8_t[size]);
Shawn Willdend67afae2014-08-19 12:36:27 -0600391 if (blob_bytes.get() == NULL)
392 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
393
Shawn Willden39b970b2014-08-11 09:11:21 -0600394 blob->Serialize(blob_bytes.get(), blob_bytes.get() + size);
Shawn Willden437fbd12014-08-20 11:59:49 -0600395 keymaster_blob->key_material_size = size;
396 keymaster_blob->key_material = blob_bytes.release();
Shawn Willdend67afae2014-08-19 12:36:27 -0600397
398 return KM_ERROR_OK;
399}
400
401Key* GoogleKeymaster::LoadKey(const keymaster_key_blob_t& key,
Shawn Willden63ac0432014-12-29 14:07:08 -0700402 const AuthorizationSet& client_params,
403 keymaster_algorithm_t* algorithm, keymaster_error_t* error) {
Shawn Willden72014ad2014-09-17 13:04:10 -0600404 UniquePtr<UnencryptedKeyBlob> blob(LoadKeyBlob(key, client_params, error));
Shawn Willdend67afae2014-08-19 12:36:27 -0600405 if (*error != KM_ERROR_OK)
406 return NULL;
Shawn Willdena278f612014-12-23 11:22:21 -0700407
Shawn Willden63ac0432014-12-29 14:07:08 -0700408 *algorithm = blob->algorithm();
409
Shawn Willdena278f612014-12-23 11:22:21 -0700410 KeyFactory* factory = 0;
Shawn Willden63ac0432014-12-29 14:07:08 -0700411 if ((factory = KeyFactoryRegistry::Get(*algorithm)))
Shawn Willden567a4a02014-12-31 12:14:46 -0700412 return factory->LoadKey(*blob, error);
Shawn Willdena278f612014-12-23 11:22:21 -0700413 *error = KM_ERROR_UNSUPPORTED_ALGORITHM;
414 return NULL;
Shawn Willden128ffe02014-08-06 12:31:33 -0600415}
416
Shawn Willden72014ad2014-09-17 13:04:10 -0600417UnencryptedKeyBlob* GoogleKeymaster::LoadKeyBlob(const keymaster_key_blob_t& key,
418 const AuthorizationSet& client_params,
419 keymaster_error_t* error) {
Shawn Willden1615f2e2014-08-13 10:37:40 -0600420 AuthorizationSet hidden;
421 BuildHiddenAuthorizations(client_params, &hidden);
Shawn Willden72014ad2014-09-17 13:04:10 -0600422 keymaster_key_blob_t master_key = MasterKey();
423 UniquePtr<UnencryptedKeyBlob> blob(
424 new UnencryptedKeyBlob(key, hidden, master_key.key_material, master_key.key_material_size));
Shawn Willden1615f2e2014-08-13 10:37:40 -0600425 if (blob.get() == NULL) {
426 *error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
427 return NULL;
428 } else if (blob->error() != KM_ERROR_OK) {
429 *error = blob->error();
430 return NULL;
431 }
Shawn Willdend67afae2014-08-19 12:36:27 -0600432 *error = KM_ERROR_OK;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600433 return blob.release();
434}
435
Shawn Willden437fbd12014-08-20 11:59:49 -0600436static keymaster_error_t TranslateAuthorizationSetError(AuthorizationSet::Error err) {
437 switch (err) {
Shawn Willden58e1a542014-08-08 21:58:29 -0600438 case AuthorizationSet::OK:
Shawn Willden128ffe02014-08-06 12:31:33 -0600439 return KM_ERROR_OK;
440 case AuthorizationSet::ALLOCATION_FAILURE:
441 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
Shawn Willden128ffe02014-08-06 12:31:33 -0600442 case AuthorizationSet::MALFORMED_DATA:
443 return KM_ERROR_UNKNOWN_ERROR;
444 }
445 return KM_ERROR_OK;
446}
447
Shawn Willden437fbd12014-08-20 11:59:49 -0600448keymaster_error_t GoogleKeymaster::SetAuthorizations(const AuthorizationSet& key_description,
449 keymaster_key_origin_t origin,
450 AuthorizationSet* enforced,
451 AuthorizationSet* unenforced) {
Shawn Willden941d1c42014-12-11 13:57:02 -0700452 enforced->Clear();
453 unenforced->Clear();
Shawn Willden128ffe02014-08-06 12:31:33 -0600454 for (size_t i = 0; i < key_description.size(); ++i) {
455 switch (key_description[i].tag) {
Shawn Willden437fbd12014-08-20 11:59:49 -0600456 // These cannot be specified by the client.
Shawn Willden128ffe02014-08-06 12:31:33 -0600457 case KM_TAG_ROOT_OF_TRUST:
Shawn Willden128ffe02014-08-06 12:31:33 -0600458 case KM_TAG_ORIGIN:
Shawn Willdenf01329d2015-03-11 21:51:38 -0600459 LOG_E("Root of trust and origin tags may not be specified", 0);
Shawn Willden437fbd12014-08-20 11:59:49 -0600460 return KM_ERROR_INVALID_TAG;
461
462 // These don't work.
Shawn Willden128ffe02014-08-06 12:31:33 -0600463 case KM_TAG_ROLLBACK_RESISTANT:
Shawn Willdenf01329d2015-03-11 21:51:38 -0600464 LOG_E("KM_TAG_ROLLBACK_RESISTANT not supported", 0);
Shawn Willden437fbd12014-08-20 11:59:49 -0600465 return KM_ERROR_UNSUPPORTED_TAG;
466
467 // These are hidden.
468 case KM_TAG_APPLICATION_ID:
469 case KM_TAG_APPLICATION_DATA:
470 break;
471
472 // Everything else we just copy into the appropriate set.
Shawn Willden128ffe02014-08-06 12:31:33 -0600473 default:
Shawn Willden437fbd12014-08-20 11:59:49 -0600474 AddAuthorization(key_description[i], enforced, unenforced);
Shawn Willden128ffe02014-08-06 12:31:33 -0600475 break;
476 }
477 }
478
Shawn Willden437fbd12014-08-20 11:59:49 -0600479 AddAuthorization(Authorization(TAG_CREATION_DATETIME, java_time(time(NULL))), enforced,
480 unenforced);
481 AddAuthorization(Authorization(TAG_ORIGIN, origin), enforced, unenforced);
Shawn Willden128ffe02014-08-06 12:31:33 -0600482
Shawn Willden437fbd12014-08-20 11:59:49 -0600483 if (enforced->is_valid() != AuthorizationSet::OK)
484 return TranslateAuthorizationSetError(enforced->is_valid());
Shawn Willden128ffe02014-08-06 12:31:33 -0600485
Shawn Willden437fbd12014-08-20 11:59:49 -0600486 return TranslateAuthorizationSetError(unenforced->is_valid());
Shawn Willden128ffe02014-08-06 12:31:33 -0600487}
488
Shawn Willden76364712014-08-11 17:48:04 -0600489keymaster_error_t GoogleKeymaster::BuildHiddenAuthorizations(const AuthorizationSet& input_set,
Shawn Willden1615f2e2014-08-13 10:37:40 -0600490 AuthorizationSet* hidden) {
Shawn Willden76364712014-08-11 17:48:04 -0600491 keymaster_blob_t entry;
492 if (input_set.GetTagValue(TAG_APPLICATION_ID, &entry))
493 hidden->push_back(TAG_APPLICATION_ID, entry.data, entry.data_length);
494 if (input_set.GetTagValue(TAG_APPLICATION_DATA, &entry))
495 hidden->push_back(TAG_APPLICATION_DATA, entry.data, entry.data_length);
496 hidden->push_back(RootOfTrustTag());
497
Shawn Willden437fbd12014-08-20 11:59:49 -0600498 return TranslateAuthorizationSetError(hidden->is_valid());
Shawn Willden76364712014-08-11 17:48:04 -0600499}
500
Shawn Willden437fbd12014-08-20 11:59:49 -0600501void GoogleKeymaster::AddAuthorization(const keymaster_key_param_t& auth,
502 AuthorizationSet* enforced, AuthorizationSet* unenforced) {
503 if (is_enforced(auth.tag))
504 enforced->push_back(auth);
505 else
506 unenforced->push_back(auth);
Shawn Willden128ffe02014-08-06 12:31:33 -0600507}
508
Shawn Willden128ffe02014-08-06 12:31:33 -0600509} // namespace keymaster