blob: f4aafbe24fb125cd562bfae98e604bd19f76e188 [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 Willden72014ad2014-09-17 13:04:10 -060035#include "unencrypted_key_blob.h"
Shawn Willden128ffe02014-08-06 12:31:33 -060036
37namespace keymaster {
38
Shawn Willden2665e862014-11-24 14:46:21 -070039const uint8_t MAJOR_VER = 1;
40const uint8_t MINOR_VER = 0;
41const uint8_t SUBMINOR_VER = 0;
42
Shawn Willden567a4a02014-12-31 12:14:46 -070043GoogleKeymaster::GoogleKeymaster(size_t operation_table_size)
Shawn Willden1615f2e2014-08-13 10:37:40 -060044 : operation_table_(new OpTableEntry[operation_table_size]),
Shawn Willden567a4a02014-12-31 12:14:46 -070045 operation_table_size_(operation_table_size) {
Shawn Willden1615f2e2014-08-13 10:37:40 -060046 if (operation_table_.get() == NULL)
47 operation_table_size_ = 0;
Shawn Willden39b970b2014-08-11 09:11:21 -060048}
Shawn Willdena278f612014-12-23 11:22:21 -070049
Shawn Willden39b970b2014-08-11 09:11:21 -060050GoogleKeymaster::~GoogleKeymaster() {
Shawn Willden1615f2e2014-08-13 10:37:40 -060051 for (size_t i = 0; i < operation_table_size_; ++i)
52 if (operation_table_[i].operation != NULL)
53 delete operation_table_[i].operation;
Shawn Willden39b970b2014-08-11 09:11:21 -060054}
Shawn Willden128ffe02014-08-06 12:31:33 -060055
Shawn Willden128ffe02014-08-06 12:31:33 -060056struct AE_CTX_Delete {
Shawn Willden802bb292014-08-18 10:46:29 -060057 void operator()(ae_ctx* ctx) const { ae_free(ctx); }
Shawn Willden128ffe02014-08-06 12:31:33 -060058};
59typedef UniquePtr<ae_ctx, AE_CTX_Delete> Unique_ae_ctx;
60
Shawn Willden19fca882015-01-22 16:35:30 -070061// TODO(swillden): Unify support analysis. Right now, we have per-keytype methods that determine if
62// specific modes, padding, etc. are supported for that key type, and GoogleKeymaster also has
63// methods that return the same information. They'll get out of sync. Best to put the knowledge in
64// the keytypes and provide some mechanism for GoogleKeymaster to query the keytypes for the
65// information.
Shawn Willdena278f612014-12-23 11:22:21 -070066//
67// UPDATE: This TODO has been completed for supported algorithms. It still needs to be done for
68// modes, padding, etc. This will be done with a registry of operation factories.
Shawn Willden128ffe02014-08-06 12:31:33 -060069
70template <typename T>
71bool check_supported(keymaster_algorithm_t algorithm, SupportedResponse<T>* response) {
Shawn Willdena278f612014-12-23 11:22:21 -070072 if (KeyFactoryRegistry::Get(algorithm) == NULL) {
Shawn Willden128ffe02014-08-06 12:31:33 -060073 response->error = KM_ERROR_UNSUPPORTED_ALGORITHM;
74 return false;
75 }
76 return true;
77}
78
Shawn Willden2665e862014-11-24 14:46:21 -070079void GoogleKeymaster::GetVersion(const GetVersionRequest&, GetVersionResponse* rsp) {
80 if (rsp == NULL)
81 return;
82
83 rsp->major_ver = MAJOR_VER;
84 rsp->minor_ver = MINOR_VER;
85 rsp->subminor_ver = SUBMINOR_VER;
86 rsp->error = KM_ERROR_OK;
87}
88
Shawn Willden3809b932014-12-02 06:59:46 -070089void GoogleKeymaster::SupportedAlgorithms(
90 SupportedResponse<keymaster_algorithm_t>* response) const {
Shawn Willden128ffe02014-08-06 12:31:33 -060091 if (response == NULL)
92 return;
Shawn Willdena278f612014-12-23 11:22:21 -070093
94 size_t factory_count = 0;
95 const KeyFactory** factories = KeyFactoryRegistry::GetAll(&factory_count);
96 assert(factories != NULL);
97 assert(factory_count > 0);
98
99 UniquePtr<keymaster_algorithm_t[]> algorithms(new keymaster_algorithm_t[factory_count]);
100 if (!algorithms.get()) {
101 response->error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
102 return;
103 }
104
105 for (size_t i = 0; i < factory_count; ++i)
106 algorithms[i] = factories[i]->registry_key();
107
108 response->results = algorithms.release();
109 response->results_length = factory_count;
110 response->error = KM_ERROR_OK;
Shawn Willden128ffe02014-08-06 12:31:33 -0600111}
112
Shawn Willden63ac0432014-12-29 14:07:08 -0700113static OperationFactory* GetOperationFactory(keymaster_algorithm_t algorithm,
114 keymaster_purpose_t purpose,
115 keymaster_error_t* error) {
116 assert(error);
117 if (error)
118 *error = KM_ERROR_OK;
119
120 OperationFactory* factory =
121 OperationFactoryRegistry::Get(OperationFactory::KeyType(algorithm, purpose));
122 if (factory == NULL && error)
123 *error = KM_ERROR_UNSUPPORTED_PURPOSE;
124
125 return factory;
Shawn Willden128ffe02014-08-06 12:31:33 -0600126}
127
Shawn Willden63ac0432014-12-29 14:07:08 -0700128template <typename T>
129void GetSupported(keymaster_algorithm_t algorithm, keymaster_purpose_t purpose,
130 const T* (OperationFactory::*get_supported_method)(size_t* count) const,
131 SupportedResponse<T>* response) {
132 if (response == NULL || !check_supported(algorithm, response))
133 return;
134
135 OperationFactory* factory = GetOperationFactory(algorithm, purpose, &response->error);
136 if (!factory) {
137 response->error = KM_ERROR_UNSUPPORTED_PURPOSE;
138 return;
139 }
140
141 size_t count;
142 const T* supported = (factory->*get_supported_method)(&count);
143 response->SetResults(supported, count);
144}
145
146void GoogleKeymaster::SupportedBlockModes(
147 keymaster_algorithm_t algorithm, keymaster_purpose_t purpose,
148 SupportedResponse<keymaster_block_mode_t>* response) const {
149 GetSupported(algorithm, purpose, &OperationFactory::SupportedBlockModes, response);
150}
Shawn Willden3809b932014-12-02 06:59:46 -0700151
152void GoogleKeymaster::SupportedPaddingModes(
Shawn Willden4200f212014-12-02 07:01:21 -0700153 keymaster_algorithm_t algorithm, keymaster_purpose_t purpose,
Shawn Willden3809b932014-12-02 06:59:46 -0700154 SupportedResponse<keymaster_padding_t>* response) const {
Shawn Willden63ac0432014-12-29 14:07:08 -0700155 GetSupported(algorithm, purpose, &OperationFactory::SupportedPaddingModes, response);
Shawn Willden128ffe02014-08-06 12:31:33 -0600156}
157
Shawn Willden63ac0432014-12-29 14:07:08 -0700158void GoogleKeymaster::SupportedDigests(keymaster_algorithm_t algorithm, keymaster_purpose_t purpose,
Shawn Willden128ffe02014-08-06 12:31:33 -0600159 SupportedResponse<keymaster_digest_t>* response) const {
Shawn Willden63ac0432014-12-29 14:07:08 -0700160 GetSupported(algorithm, purpose, &OperationFactory::SupportedDigests, response);
Shawn Willden128ffe02014-08-06 12:31:33 -0600161}
162
Shawn Willden3809b932014-12-02 06:59:46 -0700163void GoogleKeymaster::SupportedImportFormats(
164 keymaster_algorithm_t algorithm, SupportedResponse<keymaster_key_format_t>* response) const {
Shawn Willden128ffe02014-08-06 12:31:33 -0600165 if (response == NULL || !check_supported(algorithm, response))
166 return;
167
Shawn Willdena278f612014-12-23 11:22:21 -0700168 size_t count;
169 const keymaster_key_format_t* formats =
170 KeyFactoryRegistry::Get(algorithm)->SupportedImportFormats(&count);
171 response->SetResults(formats, count);
Shawn Willden128ffe02014-08-06 12:31:33 -0600172}
173
Shawn Willden3809b932014-12-02 06:59:46 -0700174void GoogleKeymaster::SupportedExportFormats(
175 keymaster_algorithm_t algorithm, SupportedResponse<keymaster_key_format_t>* response) const {
Shawn Willden128ffe02014-08-06 12:31:33 -0600176 if (response == NULL || !check_supported(algorithm, response))
177 return;
178
Shawn Willdena278f612014-12-23 11:22:21 -0700179 size_t count;
180 const keymaster_key_format_t* formats =
181 KeyFactoryRegistry::Get(algorithm)->SupportedExportFormats(&count);
182 response->SetResults(formats, count);
Shawn Willden128ffe02014-08-06 12:31:33 -0600183}
184
Shawn Willden128ffe02014-08-06 12:31:33 -0600185void GoogleKeymaster::GenerateKey(const GenerateKeyRequest& request,
186 GenerateKeyResponse* response) {
187 if (response == NULL)
188 return;
Shawn Willden128ffe02014-08-06 12:31:33 -0600189
Shawn Willdena278f612014-12-23 11:22:21 -0700190 keymaster_algorithm_t algorithm;
191 KeyFactory* factory = 0;
192 UniquePtr<Key> key;
193 if (!request.key_description.GetTagValue(TAG_ALGORITHM, &algorithm) ||
194 !(factory = KeyFactoryRegistry::Get(algorithm)))
195 response->error = KM_ERROR_UNSUPPORTED_ALGORITHM;
196 else
Shawn Willden567a4a02014-12-31 12:14:46 -0700197 key.reset(factory->GenerateKey(request.key_description, &response->error));
Shawn Willdena278f612014-12-23 11:22:21 -0700198
Shawn Willden76364712014-08-11 17:48:04 -0600199 if (response->error != KM_ERROR_OK)
Shawn Willden128ffe02014-08-06 12:31:33 -0600200 return;
201
Shawn Willden437fbd12014-08-20 11:59:49 -0600202 response->error = SerializeKey(key.get(), origin(), &response->key_blob, &response->enforced,
203 &response->unenforced);
Shawn Willden128ffe02014-08-06 12:31:33 -0600204}
205
Shawn Willden76364712014-08-11 17:48:04 -0600206void GoogleKeymaster::GetKeyCharacteristics(const GetKeyCharacteristicsRequest& request,
207 GetKeyCharacteristicsResponse* response) {
Shawn Willden1615f2e2014-08-13 10:37:40 -0600208 if (response == NULL)
209 return;
210 response->error = KM_ERROR_UNKNOWN_ERROR;
Shawn Willden76364712014-08-11 17:48:04 -0600211
Shawn Willden1615f2e2014-08-13 10:37:40 -0600212 UniquePtr<KeyBlob> blob(
213 LoadKeyBlob(request.key_blob, request.additional_params, &(response->error)));
214 if (blob.get() == NULL)
215 return;
216
217 response->enforced.Reinitialize(blob->enforced());
218 response->unenforced.Reinitialize(blob->unenforced());
219 response->error = KM_ERROR_OK;
220}
221
222void GoogleKeymaster::BeginOperation(const BeginOperationRequest& request,
223 BeginOperationResponse* response) {
224 if (response == NULL)
225 return;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600226 response->op_handle = 0;
227
Shawn Willden63ac0432014-12-29 14:07:08 -0700228 keymaster_algorithm_t algorithm;
229 UniquePtr<Key> key(
230 LoadKey(request.key_blob, request.additional_params, &algorithm, &response->error));
Shawn Willden1615f2e2014-08-13 10:37:40 -0600231 if (key.get() == NULL)
232 return;
233
Shawn Willden63ac0432014-12-29 14:07:08 -0700234 OperationFactory::KeyType op_type(algorithm, request.purpose);
235 OperationFactory* factory = OperationFactoryRegistry::Get(op_type);
236 if (!factory) {
237 response->error = KM_ERROR_UNSUPPORTED_PURPOSE;
238 return;
239 }
240
Shawn Willden567a4a02014-12-31 12:14:46 -0700241 UniquePtr<Operation> operation(factory->CreateOperation(*key, &response->error));
Shawn Willdend67afae2014-08-19 12:36:27 -0600242 if (operation.get() == NULL)
Shawn Willden76364712014-08-11 17:48:04 -0600243 return;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600244
Shawn Willden111edb32015-02-05 22:44:24 -0700245 response->output_params.Clear();
246 response->error = operation->Begin(request.additional_params, &response->output_params);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600247 if (response->error != KM_ERROR_OK)
248 return;
249
250 response->error = AddOperation(operation.release(), &response->op_handle);
251}
252
253void GoogleKeymaster::UpdateOperation(const UpdateOperationRequest& request,
254 UpdateOperationResponse* response) {
Shawn Willdend6cd7e32014-12-17 08:01:26 -0700255 if (response == NULL)
256 return;
257
Shawn Willden1615f2e2014-08-13 10:37:40 -0600258 OpTableEntry* entry = FindOperation(request.op_handle);
259 if (entry == NULL) {
260 response->error = KM_ERROR_INVALID_OPERATION_HANDLE;
261 return;
262 }
263
Shawn Willden6bfbff02015-02-06 19:48:24 -0700264 response->error = entry->operation->Update(request.additional_params, request.input,
265 &response->output, &response->input_consumed);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600266 if (response->error != KM_ERROR_OK) {
267 // Any error invalidates the operation.
268 DeleteOperation(entry);
269 }
270}
271
Shawn Willden43e999e2014-08-13 13:29:50 -0600272void GoogleKeymaster::FinishOperation(const FinishOperationRequest& request,
Shawn Willden1615f2e2014-08-13 10:37:40 -0600273 FinishOperationResponse* response) {
Shawn Willdend6cd7e32014-12-17 08:01:26 -0700274 if (response == NULL)
275 return;
276
Shawn Willden43e999e2014-08-13 13:29:50 -0600277 OpTableEntry* entry = FindOperation(request.op_handle);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600278 if (entry == NULL) {
279 response->error = KM_ERROR_INVALID_OPERATION_HANDLE;
280 return;
281 }
282
Shawn Willden6bfbff02015-02-06 19:48:24 -0700283 response->error =
284 entry->operation->Finish(request.additional_params, request.signature, &response->output);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600285 DeleteOperation(entry);
286}
287
288keymaster_error_t GoogleKeymaster::AbortOperation(const keymaster_operation_handle_t op_handle) {
289 OpTableEntry* entry = FindOperation(op_handle);
290 if (entry == NULL)
291 return KM_ERROR_INVALID_OPERATION_HANDLE;
Shawn Willden7ec74f92014-12-11 13:57:59 -0700292 keymaster_error_t error = entry->operation->Abort();
Shawn Willden1615f2e2014-08-13 10:37:40 -0600293 DeleteOperation(entry);
Shawn Willden7ec74f92014-12-11 13:57:59 -0700294 if (error != KM_ERROR_OK)
295 return error;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600296 return KM_ERROR_OK;
Shawn Willden76364712014-08-11 17:48:04 -0600297}
298
Shawn Willdenffd790c2014-08-18 21:20:06 -0600299void GoogleKeymaster::ExportKey(const ExportKeyRequest& request, ExportKeyResponse* response) {
300 if (response == NULL)
301 return;
302
Shawn Willden63ac0432014-12-29 14:07:08 -0700303 keymaster_algorithm_t algorithm;
Shawn Willdenf268d742014-08-19 15:36:26 -0600304 UniquePtr<Key> to_export(
Shawn Willden63ac0432014-12-29 14:07:08 -0700305 LoadKey(request.key_blob, request.additional_params, &algorithm, &response->error));
Shawn Willdenffd790c2014-08-18 21:20:06 -0600306 if (to_export.get() == NULL)
307 return;
308
Shawn Willdenf268d742014-08-19 15:36:26 -0600309 UniquePtr<uint8_t[]> out_key;
310 size_t size;
311 response->error = to_export->formatted_key_material(request.key_format, &out_key, &size);
312 if (response->error == KM_ERROR_OK) {
313 response->key_data = out_key.release();
314 response->key_data_length = size;
Shawn Willdenffd790c2014-08-18 21:20:06 -0600315 }
Shawn Willdenffd790c2014-08-18 21:20:06 -0600316}
317
318void GoogleKeymaster::ImportKey(const ImportKeyRequest& request, ImportKeyResponse* response) {
Shawn Willden437fbd12014-08-20 11:59:49 -0600319 if (response == NULL)
Shawn Willdenffd790c2014-08-18 21:20:06 -0600320 return;
Shawn Willdenffd790c2014-08-18 21:20:06 -0600321
Shawn Willdena278f612014-12-23 11:22:21 -0700322 keymaster_algorithm_t algorithm;
323 KeyFactory* factory = 0;
324 UniquePtr<Key> key;
325 if (!request.key_description.GetTagValue(TAG_ALGORITHM, &algorithm) ||
326 !(factory = KeyFactoryRegistry::Get(algorithm)))
327 response->error = KM_ERROR_UNSUPPORTED_ALGORITHM;
328 else
329 key.reset(factory->ImportKey(request.key_description, request.key_format, request.key_data,
Shawn Willden567a4a02014-12-31 12:14:46 -0700330 request.key_data_length, &response->error));
Shawn Willdena278f612014-12-23 11:22:21 -0700331
Shawn Willden437fbd12014-08-20 11:59:49 -0600332 if (response->error != KM_ERROR_OK)
Shawn Willdenffd790c2014-08-18 21:20:06 -0600333 return;
Shawn Willdenffd790c2014-08-18 21:20:06 -0600334
Shawn Willden437fbd12014-08-20 11:59:49 -0600335 response->error = SerializeKey(key.get(), KM_ORIGIN_IMPORTED, &response->key_blob,
336 &response->enforced, &response->unenforced);
Shawn Willdenffd790c2014-08-18 21:20:06 -0600337}
338
Shawn Willden437fbd12014-08-20 11:59:49 -0600339keymaster_error_t GoogleKeymaster::SerializeKey(const Key* key, keymaster_key_origin_t origin,
340 keymaster_key_blob_t* keymaster_blob,
341 AuthorizationSet* enforced,
342 AuthorizationSet* unenforced) {
Shawn Willden437fbd12014-08-20 11:59:49 -0600343 keymaster_error_t error;
344
345 error = SetAuthorizations(key->authorizations(), origin, enforced, unenforced);
346 if (error != KM_ERROR_OK)
347 return error;
348
349 AuthorizationSet hidden_auths;
350 error = BuildHiddenAuthorizations(key->authorizations(), &hidden_auths);
351 if (error != KM_ERROR_OK)
352 return error;
353
354 UniquePtr<uint8_t[]> key_material;
355 size_t key_material_size;
356 error = key->key_material(&key_material, &key_material_size);
357 if (error != KM_ERROR_OK)
358 return error;
359
Shawn Willden39b970b2014-08-11 09:11:21 -0600360 uint8_t nonce[KeyBlob::NONCE_LENGTH];
361 GenerateNonce(nonce, array_size(nonce));
362
Shawn Willden72014ad2014-09-17 13:04:10 -0600363 keymaster_key_blob_t master_key = MasterKey();
364 UniquePtr<KeyBlob> blob(new UnencryptedKeyBlob(
365 *enforced, *unenforced, hidden_auths, key_material.get(), key_material_size,
366 master_key.key_material, master_key.key_material_size, nonce));
Shawn Willdend67afae2014-08-19 12:36:27 -0600367 if (blob.get() == NULL)
368 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
Shawn Willdend67afae2014-08-19 12:36:27 -0600369 if (blob->error() != KM_ERROR_OK)
Shawn Willden39b970b2014-08-11 09:11:21 -0600370 return blob->error();
Shawn Willden39b970b2014-08-11 09:11:21 -0600371
372 size_t size = blob->SerializedSize();
373 UniquePtr<uint8_t[]> blob_bytes(new uint8_t[size]);
Shawn Willdend67afae2014-08-19 12:36:27 -0600374 if (blob_bytes.get() == NULL)
375 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
376
Shawn Willden39b970b2014-08-11 09:11:21 -0600377 blob->Serialize(blob_bytes.get(), blob_bytes.get() + size);
Shawn Willden437fbd12014-08-20 11:59:49 -0600378 keymaster_blob->key_material_size = size;
379 keymaster_blob->key_material = blob_bytes.release();
Shawn Willdend67afae2014-08-19 12:36:27 -0600380
381 return KM_ERROR_OK;
382}
383
384Key* GoogleKeymaster::LoadKey(const keymaster_key_blob_t& key,
Shawn Willden63ac0432014-12-29 14:07:08 -0700385 const AuthorizationSet& client_params,
386 keymaster_algorithm_t* algorithm, keymaster_error_t* error) {
Shawn Willden72014ad2014-09-17 13:04:10 -0600387 UniquePtr<UnencryptedKeyBlob> blob(LoadKeyBlob(key, client_params, error));
Shawn Willdend67afae2014-08-19 12:36:27 -0600388 if (*error != KM_ERROR_OK)
389 return NULL;
Shawn Willdena278f612014-12-23 11:22:21 -0700390
Shawn Willden63ac0432014-12-29 14:07:08 -0700391 *algorithm = blob->algorithm();
392
Shawn Willdena278f612014-12-23 11:22:21 -0700393 KeyFactory* factory = 0;
Shawn Willden63ac0432014-12-29 14:07:08 -0700394 if ((factory = KeyFactoryRegistry::Get(*algorithm)))
Shawn Willden567a4a02014-12-31 12:14:46 -0700395 return factory->LoadKey(*blob, error);
Shawn Willdena278f612014-12-23 11:22:21 -0700396 *error = KM_ERROR_UNSUPPORTED_ALGORITHM;
397 return NULL;
Shawn Willden128ffe02014-08-06 12:31:33 -0600398}
399
Shawn Willden72014ad2014-09-17 13:04:10 -0600400UnencryptedKeyBlob* GoogleKeymaster::LoadKeyBlob(const keymaster_key_blob_t& key,
401 const AuthorizationSet& client_params,
402 keymaster_error_t* error) {
Shawn Willden1615f2e2014-08-13 10:37:40 -0600403 AuthorizationSet hidden;
404 BuildHiddenAuthorizations(client_params, &hidden);
Shawn Willden72014ad2014-09-17 13:04:10 -0600405 keymaster_key_blob_t master_key = MasterKey();
406 UniquePtr<UnencryptedKeyBlob> blob(
407 new UnencryptedKeyBlob(key, hidden, master_key.key_material, master_key.key_material_size));
Shawn Willden1615f2e2014-08-13 10:37:40 -0600408 if (blob.get() == NULL) {
409 *error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
410 return NULL;
411 } else if (blob->error() != KM_ERROR_OK) {
412 *error = blob->error();
413 return NULL;
414 }
Shawn Willdend67afae2014-08-19 12:36:27 -0600415 *error = KM_ERROR_OK;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600416 return blob.release();
417}
418
Shawn Willden437fbd12014-08-20 11:59:49 -0600419static keymaster_error_t TranslateAuthorizationSetError(AuthorizationSet::Error err) {
420 switch (err) {
Shawn Willden58e1a542014-08-08 21:58:29 -0600421 case AuthorizationSet::OK:
Shawn Willden128ffe02014-08-06 12:31:33 -0600422 return KM_ERROR_OK;
423 case AuthorizationSet::ALLOCATION_FAILURE:
424 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
Shawn Willden128ffe02014-08-06 12:31:33 -0600425 case AuthorizationSet::MALFORMED_DATA:
426 return KM_ERROR_UNKNOWN_ERROR;
427 }
428 return KM_ERROR_OK;
429}
430
Shawn Willden437fbd12014-08-20 11:59:49 -0600431keymaster_error_t GoogleKeymaster::SetAuthorizations(const AuthorizationSet& key_description,
432 keymaster_key_origin_t origin,
433 AuthorizationSet* enforced,
434 AuthorizationSet* unenforced) {
Shawn Willden941d1c42014-12-11 13:57:02 -0700435 enforced->Clear();
436 unenforced->Clear();
Shawn Willden128ffe02014-08-06 12:31:33 -0600437 for (size_t i = 0; i < key_description.size(); ++i) {
438 switch (key_description[i].tag) {
Shawn Willden437fbd12014-08-20 11:59:49 -0600439 // These cannot be specified by the client.
Shawn Willden128ffe02014-08-06 12:31:33 -0600440 case KM_TAG_ROOT_OF_TRUST:
Shawn Willden128ffe02014-08-06 12:31:33 -0600441 case KM_TAG_ORIGIN:
Shawn Willden437fbd12014-08-20 11:59:49 -0600442 return KM_ERROR_INVALID_TAG;
443
444 // These don't work.
Shawn Willden128ffe02014-08-06 12:31:33 -0600445 case KM_TAG_ROLLBACK_RESISTANT:
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 Willden76364712014-08-11 17:48:04 -0600470keymaster_error_t GoogleKeymaster::BuildHiddenAuthorizations(const AuthorizationSet& input_set,
Shawn Willden1615f2e2014-08-13 10:37:40 -0600471 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 Willden437fbd12014-08-20 11:59:49 -0600482void GoogleKeymaster::AddAuthorization(const keymaster_key_param_t& auth,
483 AuthorizationSet* enforced, AuthorizationSet* unenforced) {
484 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 Willden1615f2e2014-08-13 10:37:40 -0600490keymaster_error_t GoogleKeymaster::AddOperation(Operation* operation,
491 keymaster_operation_handle_t* op_handle) {
492 UniquePtr<Operation> op(operation);
493 if (RAND_bytes(reinterpret_cast<uint8_t*>(op_handle), sizeof(*op_handle)) == 0)
Shawn Willden567a4a02014-12-31 12:14:46 -0700494 return TranslateLastOpenSslError();
Shawn Willden802bb292014-08-18 10:46:29 -0600495 if (*op_handle == 0) {
Shawn Willden1615f2e2014-08-13 10:37:40 -0600496 // Statistically this is vanishingly unlikely, which means if it ever happens in practice,
497 // it indicates a broken RNG.
498 return KM_ERROR_UNKNOWN_ERROR;
499 }
500 for (size_t i = 0; i < operation_table_size_; ++i) {
501 if (operation_table_[i].operation == NULL) {
502 operation_table_[i].operation = op.release();
503 operation_table_[i].handle = *op_handle;
504 return KM_ERROR_OK;
505 }
506 }
507 return KM_ERROR_TOO_MANY_OPERATIONS;
508}
509
510GoogleKeymaster::OpTableEntry*
511GoogleKeymaster::FindOperation(keymaster_operation_handle_t op_handle) {
512 if (op_handle == 0)
513 return NULL;
514
515 for (size_t i = 0; i < operation_table_size_; ++i) {
516 if (operation_table_[i].handle == op_handle)
517 return operation_table_.get() + i;
518 }
519 return NULL;
520}
521
522void GoogleKeymaster::DeleteOperation(OpTableEntry* entry) {
523 delete entry->operation;
524 entry->operation = NULL;
525 entry->handle = 0;
526}
527
Shawn Willden128ffe02014-08-06 12:31:33 -0600528} // namespace keymaster