blob: d25522751572a54877491f14259a760cbe911c83 [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 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 Willden3ed6d062015-04-15 13:39:38 -0600236 UniquePtr<Operation> operation(
237 factory->CreateOperation(*key, request.additional_params, &response->error));
Shawn Willdend67afae2014-08-19 12:36:27 -0600238 if (operation.get() == NULL)
Shawn Willden76364712014-08-11 17:48:04 -0600239 return;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600240
Shawn Willden111edb32015-02-05 22:44:24 -0700241 response->output_params.Clear();
242 response->error = operation->Begin(request.additional_params, &response->output_params);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600243 if (response->error != KM_ERROR_OK)
244 return;
245
Shawn Willden23d4a742015-03-19 15:33:21 -0600246 response->error = operation_table_->Add(operation.release(), &response->op_handle);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600247}
248
249void GoogleKeymaster::UpdateOperation(const UpdateOperationRequest& request,
250 UpdateOperationResponse* response) {
Shawn Willdend6cd7e32014-12-17 08:01:26 -0700251 if (response == NULL)
252 return;
253
Shawn Willden23d4a742015-03-19 15:33:21 -0600254 response->error = KM_ERROR_INVALID_OPERATION_HANDLE;
255 Operation* operation = operation_table_->Find(request.op_handle);
256 if (operation == NULL)
Shawn Willden1615f2e2014-08-13 10:37:40 -0600257 return;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600258
Shawn Willden23d4a742015-03-19 15:33:21 -0600259 response->error = operation->Update(request.additional_params, request.input, &response->output,
260 &response->input_consumed);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600261 if (response->error != KM_ERROR_OK) {
262 // Any error invalidates the operation.
Shawn Willden23d4a742015-03-19 15:33:21 -0600263 operation_table_->Delete(request.op_handle);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600264 }
265}
266
Shawn Willden43e999e2014-08-13 13:29:50 -0600267void GoogleKeymaster::FinishOperation(const FinishOperationRequest& request,
Shawn Willden1615f2e2014-08-13 10:37:40 -0600268 FinishOperationResponse* response) {
Shawn Willdend6cd7e32014-12-17 08:01:26 -0700269 if (response == NULL)
270 return;
271
Shawn Willden23d4a742015-03-19 15:33:21 -0600272 response->error = KM_ERROR_INVALID_OPERATION_HANDLE;
273 Operation* operation = operation_table_->Find(request.op_handle);
274 if (operation == NULL)
Shawn Willden1615f2e2014-08-13 10:37:40 -0600275 return;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600276
Shawn Willden6bfbff02015-02-06 19:48:24 -0700277 response->error =
Shawn Willden23d4a742015-03-19 15:33:21 -0600278 operation->Finish(request.additional_params, request.signature, &response->output);
279 operation_table_->Delete(request.op_handle);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600280}
281
282keymaster_error_t GoogleKeymaster::AbortOperation(const keymaster_operation_handle_t op_handle) {
Shawn Willden23d4a742015-03-19 15:33:21 -0600283 Operation* operation = operation_table_->Find(op_handle);
284 if (operation == NULL)
Shawn Willden1615f2e2014-08-13 10:37:40 -0600285 return KM_ERROR_INVALID_OPERATION_HANDLE;
Shawn Willden23d4a742015-03-19 15:33:21 -0600286
287 keymaster_error_t error = operation->Abort();
288 operation_table_->Delete(op_handle);
Shawn Willden7ec74f92014-12-11 13:57:59 -0700289 if (error != KM_ERROR_OK)
290 return error;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600291 return KM_ERROR_OK;
Shawn Willden76364712014-08-11 17:48:04 -0600292}
293
Shawn Willdenffd790c2014-08-18 21:20:06 -0600294void GoogleKeymaster::ExportKey(const ExportKeyRequest& request, ExportKeyResponse* response) {
295 if (response == NULL)
296 return;
297
Shawn Willden63ac0432014-12-29 14:07:08 -0700298 keymaster_algorithm_t algorithm;
Shawn Willdenf268d742014-08-19 15:36:26 -0600299 UniquePtr<Key> to_export(
Shawn Willden63ac0432014-12-29 14:07:08 -0700300 LoadKey(request.key_blob, request.additional_params, &algorithm, &response->error));
Shawn Willdenffd790c2014-08-18 21:20:06 -0600301 if (to_export.get() == NULL)
302 return;
303
Shawn Willdenf268d742014-08-19 15:36:26 -0600304 UniquePtr<uint8_t[]> out_key;
305 size_t size;
306 response->error = to_export->formatted_key_material(request.key_format, &out_key, &size);
307 if (response->error == KM_ERROR_OK) {
308 response->key_data = out_key.release();
309 response->key_data_length = size;
Shawn Willdenffd790c2014-08-18 21:20:06 -0600310 }
Shawn Willdenffd790c2014-08-18 21:20:06 -0600311}
312
Shawn Willden5fad7852015-01-26 16:10:56 -0700313void GoogleKeymaster::Rescope(const RescopeRequest& request, RescopeResponse* response) {
314 if (response == NULL)
315 return;
316
317 UniquePtr<UnencryptedKeyBlob> blob(
318 LoadKeyBlob(request.key_blob, request.additional_params, &response->error));
319 if (response->error != KM_ERROR_OK)
320 return;
321
322 KeyFactory* factory = KeyFactoryRegistry::Get(blob->algorithm());
323 if (!factory) {
324 response->error = KM_ERROR_UNSUPPORTED_ALGORITHM;
325 return;
326 }
327
328 UniquePtr<Key> key;
329 key.reset(factory->RescopeKey(*blob, request.new_authorizations, &response->error));
330 if (response->error != KM_ERROR_OK)
331 return;
332
Shawn Willden0b2d3332015-04-07 17:46:18 -0600333 assert(is_hardware() == blob->is_hardware());
Shawn Willden5fad7852015-01-26 16:10:56 -0700334 response->error = SerializeKey(key.get(), blob->origin(), &response->key_blob,
335 &response->enforced, &response->unenforced);
336}
Shawn Willdenffd790c2014-08-18 21:20:06 -0600337void GoogleKeymaster::ImportKey(const ImportKeyRequest& request, ImportKeyResponse* response) {
Shawn Willden437fbd12014-08-20 11:59:49 -0600338 if (response == NULL)
Shawn Willdenffd790c2014-08-18 21:20:06 -0600339 return;
Shawn Willdenffd790c2014-08-18 21:20:06 -0600340
Shawn Willdena278f612014-12-23 11:22:21 -0700341 keymaster_algorithm_t algorithm;
342 KeyFactory* factory = 0;
343 UniquePtr<Key> key;
344 if (!request.key_description.GetTagValue(TAG_ALGORITHM, &algorithm) ||
345 !(factory = KeyFactoryRegistry::Get(algorithm)))
346 response->error = KM_ERROR_UNSUPPORTED_ALGORITHM;
347 else
348 key.reset(factory->ImportKey(request.key_description, request.key_format, request.key_data,
Shawn Willden567a4a02014-12-31 12:14:46 -0700349 request.key_data_length, &response->error));
Shawn Willdena278f612014-12-23 11:22:21 -0700350
Shawn Willden437fbd12014-08-20 11:59:49 -0600351 if (response->error != KM_ERROR_OK)
Shawn Willdenffd790c2014-08-18 21:20:06 -0600352 return;
Shawn Willdenffd790c2014-08-18 21:20:06 -0600353
Shawn Willden437fbd12014-08-20 11:59:49 -0600354 response->error = SerializeKey(key.get(), KM_ORIGIN_IMPORTED, &response->key_blob,
355 &response->enforced, &response->unenforced);
Shawn Willdenffd790c2014-08-18 21:20:06 -0600356}
357
Shawn Willden437fbd12014-08-20 11:59:49 -0600358keymaster_error_t GoogleKeymaster::SerializeKey(const Key* key, keymaster_key_origin_t origin,
359 keymaster_key_blob_t* keymaster_blob,
360 AuthorizationSet* enforced,
361 AuthorizationSet* unenforced) {
Shawn Willden437fbd12014-08-20 11:59:49 -0600362 keymaster_error_t error;
363
364 error = SetAuthorizations(key->authorizations(), origin, enforced, unenforced);
365 if (error != KM_ERROR_OK)
366 return error;
367
368 AuthorizationSet hidden_auths;
369 error = BuildHiddenAuthorizations(key->authorizations(), &hidden_auths);
370 if (error != KM_ERROR_OK)
371 return error;
372
373 UniquePtr<uint8_t[]> key_material;
374 size_t key_material_size;
375 error = key->key_material(&key_material, &key_material_size);
376 if (error != KM_ERROR_OK)
377 return error;
378
Shawn Willden39b970b2014-08-11 09:11:21 -0600379 uint8_t nonce[KeyBlob::NONCE_LENGTH];
380 GenerateNonce(nonce, array_size(nonce));
381
Shawn Willden72014ad2014-09-17 13:04:10 -0600382 keymaster_key_blob_t master_key = MasterKey();
383 UniquePtr<KeyBlob> blob(new UnencryptedKeyBlob(
384 *enforced, *unenforced, hidden_auths, key_material.get(), key_material_size,
385 master_key.key_material, master_key.key_material_size, nonce));
Shawn Willdend67afae2014-08-19 12:36:27 -0600386 if (blob.get() == NULL)
387 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
Shawn Willdend67afae2014-08-19 12:36:27 -0600388 if (blob->error() != KM_ERROR_OK)
Shawn Willden39b970b2014-08-11 09:11:21 -0600389 return blob->error();
Shawn Willden39b970b2014-08-11 09:11:21 -0600390
391 size_t size = blob->SerializedSize();
392 UniquePtr<uint8_t[]> blob_bytes(new uint8_t[size]);
Shawn Willdend67afae2014-08-19 12:36:27 -0600393 if (blob_bytes.get() == NULL)
394 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
395
Shawn Willden39b970b2014-08-11 09:11:21 -0600396 blob->Serialize(blob_bytes.get(), blob_bytes.get() + size);
Shawn Willden437fbd12014-08-20 11:59:49 -0600397 keymaster_blob->key_material_size = size;
398 keymaster_blob->key_material = blob_bytes.release();
Shawn Willdend67afae2014-08-19 12:36:27 -0600399
400 return KM_ERROR_OK;
401}
402
403Key* GoogleKeymaster::LoadKey(const keymaster_key_blob_t& key,
Shawn Willden63ac0432014-12-29 14:07:08 -0700404 const AuthorizationSet& client_params,
405 keymaster_algorithm_t* algorithm, keymaster_error_t* error) {
Shawn Willden72014ad2014-09-17 13:04:10 -0600406 UniquePtr<UnencryptedKeyBlob> blob(LoadKeyBlob(key, client_params, error));
Shawn Willdend67afae2014-08-19 12:36:27 -0600407 if (*error != KM_ERROR_OK)
408 return NULL;
Shawn Willdena278f612014-12-23 11:22:21 -0700409
Shawn Willden63ac0432014-12-29 14:07:08 -0700410 *algorithm = blob->algorithm();
411
Shawn Willdena278f612014-12-23 11:22:21 -0700412 KeyFactory* factory = 0;
Shawn Willden63ac0432014-12-29 14:07:08 -0700413 if ((factory = KeyFactoryRegistry::Get(*algorithm)))
Shawn Willden567a4a02014-12-31 12:14:46 -0700414 return factory->LoadKey(*blob, error);
Shawn Willdena278f612014-12-23 11:22:21 -0700415 *error = KM_ERROR_UNSUPPORTED_ALGORITHM;
416 return NULL;
Shawn Willden128ffe02014-08-06 12:31:33 -0600417}
418
Shawn Willden72014ad2014-09-17 13:04:10 -0600419UnencryptedKeyBlob* GoogleKeymaster::LoadKeyBlob(const keymaster_key_blob_t& key,
420 const AuthorizationSet& client_params,
421 keymaster_error_t* error) {
Shawn Willden1615f2e2014-08-13 10:37:40 -0600422 AuthorizationSet hidden;
423 BuildHiddenAuthorizations(client_params, &hidden);
Shawn Willden72014ad2014-09-17 13:04:10 -0600424 keymaster_key_blob_t master_key = MasterKey();
425 UniquePtr<UnencryptedKeyBlob> blob(
426 new UnencryptedKeyBlob(key, hidden, master_key.key_material, master_key.key_material_size));
Shawn Willden1615f2e2014-08-13 10:37:40 -0600427 if (blob.get() == NULL) {
428 *error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
429 return NULL;
430 } else if (blob->error() != KM_ERROR_OK) {
431 *error = blob->error();
432 return NULL;
433 }
Shawn Willdend67afae2014-08-19 12:36:27 -0600434 *error = KM_ERROR_OK;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600435 return blob.release();
436}
437
Shawn Willden437fbd12014-08-20 11:59:49 -0600438static keymaster_error_t TranslateAuthorizationSetError(AuthorizationSet::Error err) {
439 switch (err) {
Shawn Willden58e1a542014-08-08 21:58:29 -0600440 case AuthorizationSet::OK:
Shawn Willden128ffe02014-08-06 12:31:33 -0600441 return KM_ERROR_OK;
442 case AuthorizationSet::ALLOCATION_FAILURE:
443 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
Shawn Willden128ffe02014-08-06 12:31:33 -0600444 case AuthorizationSet::MALFORMED_DATA:
445 return KM_ERROR_UNKNOWN_ERROR;
446 }
447 return KM_ERROR_OK;
448}
449
Shawn Willden437fbd12014-08-20 11:59:49 -0600450keymaster_error_t GoogleKeymaster::SetAuthorizations(const AuthorizationSet& key_description,
451 keymaster_key_origin_t origin,
452 AuthorizationSet* enforced,
453 AuthorizationSet* unenforced) {
Shawn Willden941d1c42014-12-11 13:57:02 -0700454 enforced->Clear();
455 unenforced->Clear();
Shawn Willden128ffe02014-08-06 12:31:33 -0600456 for (size_t i = 0; i < key_description.size(); ++i) {
457 switch (key_description[i].tag) {
Shawn Willden437fbd12014-08-20 11:59:49 -0600458 // These cannot be specified by the client.
Shawn Willden128ffe02014-08-06 12:31:33 -0600459 case KM_TAG_ROOT_OF_TRUST:
Shawn Willden128ffe02014-08-06 12:31:33 -0600460 case KM_TAG_ORIGIN:
Shawn Willdenf01329d2015-03-11 21:51:38 -0600461 LOG_E("Root of trust and origin tags may not be specified", 0);
Shawn Willden437fbd12014-08-20 11:59:49 -0600462 return KM_ERROR_INVALID_TAG;
463
464 // These don't work.
Shawn Willden128ffe02014-08-06 12:31:33 -0600465 case KM_TAG_ROLLBACK_RESISTANT:
Shawn Willdenf01329d2015-03-11 21:51:38 -0600466 LOG_E("KM_TAG_ROLLBACK_RESISTANT not supported", 0);
Shawn Willden437fbd12014-08-20 11:59:49 -0600467 return KM_ERROR_UNSUPPORTED_TAG;
468
469 // These are hidden.
470 case KM_TAG_APPLICATION_ID:
471 case KM_TAG_APPLICATION_DATA:
472 break;
473
474 // Everything else we just copy into the appropriate set.
Shawn Willden128ffe02014-08-06 12:31:33 -0600475 default:
Shawn Willden437fbd12014-08-20 11:59:49 -0600476 AddAuthorization(key_description[i], enforced, unenforced);
Shawn Willden128ffe02014-08-06 12:31:33 -0600477 break;
478 }
479 }
480
Shawn Willden437fbd12014-08-20 11:59:49 -0600481 AddAuthorization(Authorization(TAG_CREATION_DATETIME, java_time(time(NULL))), enforced,
482 unenforced);
483 AddAuthorization(Authorization(TAG_ORIGIN, origin), enforced, unenforced);
Shawn Willden128ffe02014-08-06 12:31:33 -0600484
Shawn Willden437fbd12014-08-20 11:59:49 -0600485 if (enforced->is_valid() != AuthorizationSet::OK)
486 return TranslateAuthorizationSetError(enforced->is_valid());
Shawn Willden128ffe02014-08-06 12:31:33 -0600487
Shawn Willden437fbd12014-08-20 11:59:49 -0600488 return TranslateAuthorizationSetError(unenforced->is_valid());
Shawn Willden128ffe02014-08-06 12:31:33 -0600489}
490
Shawn Willden76364712014-08-11 17:48:04 -0600491keymaster_error_t GoogleKeymaster::BuildHiddenAuthorizations(const AuthorizationSet& input_set,
Shawn Willden1615f2e2014-08-13 10:37:40 -0600492 AuthorizationSet* hidden) {
Shawn Willden76364712014-08-11 17:48:04 -0600493 keymaster_blob_t entry;
494 if (input_set.GetTagValue(TAG_APPLICATION_ID, &entry))
495 hidden->push_back(TAG_APPLICATION_ID, entry.data, entry.data_length);
496 if (input_set.GetTagValue(TAG_APPLICATION_DATA, &entry))
497 hidden->push_back(TAG_APPLICATION_DATA, entry.data, entry.data_length);
498 hidden->push_back(RootOfTrustTag());
499
Shawn Willden437fbd12014-08-20 11:59:49 -0600500 return TranslateAuthorizationSetError(hidden->is_valid());
Shawn Willden76364712014-08-11 17:48:04 -0600501}
502
Shawn Willden437fbd12014-08-20 11:59:49 -0600503void GoogleKeymaster::AddAuthorization(const keymaster_key_param_t& auth,
504 AuthorizationSet* enforced, AuthorizationSet* unenforced) {
505 if (is_enforced(auth.tag))
506 enforced->push_back(auth);
507 else
508 unenforced->push_back(auth);
Shawn Willden128ffe02014-08-06 12:31:33 -0600509}
510
Shawn Willden128ffe02014-08-06 12:31:33 -0600511} // namespace keymaster