blob: 68cd39aedb71355dd884c1553e581a6975262875 [file] [log] [blame]
Shawn Willden128ffe02014-08-06 12:31:33 -06001/*
2 * Copyright 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Shawn Willdenb6837e72015-05-16 09:20:59 -060017#include <keymaster/android_keymaster.h>
Shawn Willden567a4a02014-12-31 12:14:46 -070018
Shawn Willden128ffe02014-08-06 12:31:33 -060019#include <assert.h>
20#include <string.h>
21
Shawn Willden4db3fbd2014-08-08 22:13:44 -060022#include <cstddef>
23
Shawn Willden1615f2e2014-08-13 10:37:40 -060024#include <openssl/rand.h>
Shawn Willdenffd790c2014-08-18 21:20:06 -060025#include <openssl/x509.h>
Shawn Willden128ffe02014-08-06 12:31:33 -060026
27#include <UniquePtr.h>
28
Shawn Willdenb6837e72015-05-16 09:20:59 -060029#include <keymaster/android_keymaster_utils.h>
Shawn Willden398c1582015-05-28 00:04:06 -060030#include <keymaster/key_factory.h>
Shawn Willden0cb69422015-05-26 08:31:37 -060031#include <keymaster/keymaster_context.h>
Shawn Willden98d9b922014-08-26 08:14:10 -060032
Shawn Willden3879f862014-08-06 14:40:48 -060033#include "ae.h"
Shawn Willdend67afae2014-08-19 12:36:27 -060034#include "key.h"
Shawn Willden567a4a02014-12-31 12:14:46 -070035#include "openssl_err.h"
Shawn Willdend67afae2014-08-19 12:36:27 -060036#include "operation.h"
Shawn Willden23d4a742015-03-19 15:33:21 -060037#include "operation_table.h"
Shawn Willden128ffe02014-08-06 12:31:33 -060038
39namespace keymaster {
40
Shawn Willden2665e862014-11-24 14:46:21 -070041const uint8_t MAJOR_VER = 1;
Shawn Willdenac69d952015-06-01 14:40:19 -060042const uint8_t MINOR_VER = 1;
Shawn Willden2665e862014-11-24 14:46:21 -070043const uint8_t SUBMINOR_VER = 0;
44
Shawn Willden0cb69422015-05-26 08:31:37 -060045AndroidKeymaster::AndroidKeymaster(KeymasterContext* context, size_t operation_table_size)
46 : context_(context), operation_table_(new OperationTable(operation_table_size)) {
Shawn Willden39b970b2014-08-11 09:11:21 -060047}
Shawn Willdena278f612014-12-23 11:22:21 -070048
Shawn Willdenb6837e72015-05-16 09:20:59 -060049AndroidKeymaster::~AndroidKeymaster() {
Shawn Willden39b970b2014-08-11 09:11:21 -060050}
Shawn Willden128ffe02014-08-06 12:31:33 -060051
Shawn Willden128ffe02014-08-06 12:31:33 -060052struct AE_CTX_Delete {
Shawn Willden802bb292014-08-18 10:46:29 -060053 void operator()(ae_ctx* ctx) const { ae_free(ctx); }
Shawn Willden128ffe02014-08-06 12:31:33 -060054};
55typedef UniquePtr<ae_ctx, AE_CTX_Delete> Unique_ae_ctx;
56
Shawn Willden19fca882015-01-22 16:35:30 -070057// TODO(swillden): Unify support analysis. Right now, we have per-keytype methods that determine if
Shawn Willdenb6837e72015-05-16 09:20:59 -060058// specific modes, padding, etc. are supported for that key type, and AndroidKeymaster also has
Shawn Willden19fca882015-01-22 16:35:30 -070059// methods that return the same information. They'll get out of sync. Best to put the knowledge in
Shawn Willdenb6837e72015-05-16 09:20:59 -060060// the keytypes and provide some mechanism for AndroidKeymaster to query the keytypes for the
Shawn Willden19fca882015-01-22 16:35:30 -070061// information.
Shawn Willden128ffe02014-08-06 12:31:33 -060062
63template <typename T>
Shawn Willden06298102015-05-25 23:12:48 -060064bool check_supported(const KeymasterContext& context, keymaster_algorithm_t algorithm,
65 SupportedResponse<T>* response) {
66 if (context.GetKeyFactory(algorithm) == NULL) {
Shawn Willden128ffe02014-08-06 12:31:33 -060067 response->error = KM_ERROR_UNSUPPORTED_ALGORITHM;
68 return false;
69 }
70 return true;
71}
72
Shawn Willdenb6837e72015-05-16 09:20:59 -060073void AndroidKeymaster::GetVersion(const GetVersionRequest&, GetVersionResponse* rsp) {
Shawn Willden2665e862014-11-24 14:46:21 -070074 if (rsp == NULL)
75 return;
76
77 rsp->major_ver = MAJOR_VER;
78 rsp->minor_ver = MINOR_VER;
79 rsp->subminor_ver = SUBMINOR_VER;
80 rsp->error = KM_ERROR_OK;
81}
82
Shawn Willden36d41e22015-06-17 06:39:48 -060083void AndroidKeymaster::SupportedAlgorithms(const SupportedAlgorithmsRequest& /* request */,
84 SupportedAlgorithmsResponse* response) {
Shawn Willden128ffe02014-08-06 12:31:33 -060085 if (response == NULL)
86 return;
Shawn Willdena278f612014-12-23 11:22:21 -070087
Shawn Willdena278f612014-12-23 11:22:21 -070088 response->error = KM_ERROR_OK;
Shawn Willden128ffe02014-08-06 12:31:33 -060089
Shawn Willden06298102015-05-25 23:12:48 -060090 size_t algorithm_count = 0;
91 const keymaster_algorithm_t* algorithms = context_->GetSupportedAlgorithms(&algorithm_count);
92 if (algorithm_count == 0)
93 return;
94 response->results_length = algorithm_count;
95 response->results = dup_array(algorithms, algorithm_count);
96 if (!response->results)
97 response->error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
Shawn Willden128ffe02014-08-06 12:31:33 -060098}
99
Shawn Willden63ac0432014-12-29 14:07:08 -0700100template <typename T>
Shawn Willden06298102015-05-25 23:12:48 -0600101void GetSupported(const KeymasterContext& context, keymaster_algorithm_t algorithm,
102 keymaster_purpose_t purpose,
Shawn Willden63ac0432014-12-29 14:07:08 -0700103 const T* (OperationFactory::*get_supported_method)(size_t* count) const,
104 SupportedResponse<T>* response) {
Shawn Willden06298102015-05-25 23:12:48 -0600105 if (response == NULL || !check_supported(context, algorithm, response))
Shawn Willden63ac0432014-12-29 14:07:08 -0700106 return;
107
Shawn Willden06298102015-05-25 23:12:48 -0600108 const OperationFactory* factory = context.GetOperationFactory(algorithm, purpose);
Shawn Willden63ac0432014-12-29 14:07:08 -0700109 if (!factory) {
110 response->error = KM_ERROR_UNSUPPORTED_PURPOSE;
111 return;
112 }
113
114 size_t count;
115 const T* supported = (factory->*get_supported_method)(&count);
116 response->SetResults(supported, count);
117}
118
Shawn Willden36d41e22015-06-17 06:39:48 -0600119void AndroidKeymaster::SupportedBlockModes(const SupportedBlockModesRequest& request,
120 SupportedBlockModesResponse* response) {
121 GetSupported(*context_, request.algorithm, request.purpose,
122 &OperationFactory::SupportedBlockModes, response);
Shawn Willden63ac0432014-12-29 14:07:08 -0700123}
Shawn Willden3809b932014-12-02 06:59:46 -0700124
Shawn Willden36d41e22015-06-17 06:39:48 -0600125void AndroidKeymaster::SupportedPaddingModes(const SupportedPaddingModesRequest& request,
126 SupportedPaddingModesResponse* response) {
127 GetSupported(*context_, request.algorithm, request.purpose,
128 &OperationFactory::SupportedPaddingModes, response);
Shawn Willden128ffe02014-08-06 12:31:33 -0600129}
130
Shawn Willden36d41e22015-06-17 06:39:48 -0600131void AndroidKeymaster::SupportedDigests(const SupportedDigestsRequest& request,
132 SupportedDigestsResponse* response) {
133 GetSupported(*context_, request.algorithm, request.purpose, &OperationFactory::SupportedDigests,
134 response);
Shawn Willden128ffe02014-08-06 12:31:33 -0600135}
136
Shawn Willden36d41e22015-06-17 06:39:48 -0600137void AndroidKeymaster::SupportedImportFormats(const SupportedImportFormatsRequest& request,
138 SupportedImportFormatsResponse* response) {
139 if (response == NULL || !check_supported(*context_, request.algorithm, response))
Shawn Willden128ffe02014-08-06 12:31:33 -0600140 return;
141
Shawn Willdena278f612014-12-23 11:22:21 -0700142 size_t count;
143 const keymaster_key_format_t* formats =
Shawn Willden36d41e22015-06-17 06:39:48 -0600144 context_->GetKeyFactory(request.algorithm)->SupportedImportFormats(&count);
Shawn Willdena278f612014-12-23 11:22:21 -0700145 response->SetResults(formats, count);
Shawn Willden128ffe02014-08-06 12:31:33 -0600146}
147
Shawn Willden36d41e22015-06-17 06:39:48 -0600148void AndroidKeymaster::SupportedExportFormats(const SupportedExportFormatsRequest& request,
149 SupportedExportFormatsResponse* response) {
150 if (response == NULL || !check_supported(*context_, request.algorithm, response))
Shawn Willden128ffe02014-08-06 12:31:33 -0600151 return;
152
Shawn Willdena278f612014-12-23 11:22:21 -0700153 size_t count;
154 const keymaster_key_format_t* formats =
Shawn Willden36d41e22015-06-17 06:39:48 -0600155 context_->GetKeyFactory(request.algorithm)->SupportedExportFormats(&count);
Shawn Willdena278f612014-12-23 11:22:21 -0700156 response->SetResults(formats, count);
Shawn Willden128ffe02014-08-06 12:31:33 -0600157}
158
Shawn Willden36d41e22015-06-17 06:39:48 -0600159void AndroidKeymaster::AddRngEntropy(const AddEntropyRequest& request,
160 AddEntropyResponse* response) {
161 response->error = context_->AddRngEntropy(request.random_data.peek_read(),
162 request.random_data.available_read());
Shawn Willden0cb69422015-05-26 08:31:37 -0600163}
164
Shawn Willdenb6837e72015-05-16 09:20:59 -0600165void AndroidKeymaster::GenerateKey(const GenerateKeyRequest& request,
166 GenerateKeyResponse* response) {
Shawn Willden128ffe02014-08-06 12:31:33 -0600167 if (response == NULL)
168 return;
Shawn Willden128ffe02014-08-06 12:31:33 -0600169
Shawn Willdena278f612014-12-23 11:22:21 -0700170 keymaster_algorithm_t algorithm;
171 KeyFactory* factory = 0;
172 UniquePtr<Key> key;
173 if (!request.key_description.GetTagValue(TAG_ALGORITHM, &algorithm) ||
Shawn Willden06298102015-05-25 23:12:48 -0600174 !(factory = context_->GetKeyFactory(algorithm)))
Shawn Willdena278f612014-12-23 11:22:21 -0700175 response->error = KM_ERROR_UNSUPPORTED_ALGORITHM;
Shawn Willden0cb69422015-05-26 08:31:37 -0600176 else {
177 KeymasterKeyBlob key_blob;
Shawn Willden2beb6282015-05-20 16:36:24 -0600178 response->enforced.Clear();
179 response->unenforced.Clear();
Shawn Willden0cb69422015-05-26 08:31:37 -0600180 response->error = factory->GenerateKey(request.key_description, &key_blob,
181 &response->enforced, &response->unenforced);
182 if (response->error == KM_ERROR_OK)
183 response->key_blob = key_blob.release();
184 }
Shawn Willden128ffe02014-08-06 12:31:33 -0600185}
186
Shawn Willdenb6837e72015-05-16 09:20:59 -0600187void AndroidKeymaster::GetKeyCharacteristics(const GetKeyCharacteristicsRequest& request,
188 GetKeyCharacteristicsResponse* response) {
Shawn Willden1615f2e2014-08-13 10:37:40 -0600189 if (response == NULL)
190 return;
Shawn Willden76364712014-08-11 17:48:04 -0600191
Shawn Willden0cb69422015-05-26 08:31:37 -0600192 KeymasterKeyBlob key_material;
193 response->error =
194 context_->ParseKeyBlob(KeymasterKeyBlob(request.key_blob), request.additional_params,
195 &key_material, &response->enforced, &response->unenforced);
196 if (response->error != KM_ERROR_OK)
Shawn Willden1615f2e2014-08-13 10:37:40 -0600197 return;
Shawn Willden0cb69422015-05-26 08:31:37 -0600198}
Shawn Willden1615f2e2014-08-13 10:37:40 -0600199
Shawn Willden06298102015-05-25 23:12:48 -0600200static KeyFactory* GetKeyFactory(const KeymasterContext& context,
201 const AuthorizationSet& hw_enforced,
Shawn Willden0cb69422015-05-26 08:31:37 -0600202 const AuthorizationSet& sw_enforced,
203 keymaster_algorithm_t* algorithm, keymaster_error_t* error) {
204 *error = KM_ERROR_UNSUPPORTED_ALGORITHM;
205 if (!hw_enforced.GetTagValue(TAG_ALGORITHM, algorithm) &&
206 !sw_enforced.GetTagValue(TAG_ALGORITHM, algorithm))
207 return nullptr;
Shawn Willden06298102015-05-25 23:12:48 -0600208 KeyFactory* factory = context.GetKeyFactory(*algorithm);
Shawn Willden0cb69422015-05-26 08:31:37 -0600209 if (factory)
210 *error = KM_ERROR_OK;
211 return factory;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600212}
213
Shawn Willdenb6837e72015-05-16 09:20:59 -0600214void AndroidKeymaster::BeginOperation(const BeginOperationRequest& request,
215 BeginOperationResponse* response) {
Shawn Willden1615f2e2014-08-13 10:37:40 -0600216 if (response == NULL)
217 return;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600218 response->op_handle = 0;
219
Shawn Willden0cb69422015-05-26 08:31:37 -0600220 AuthorizationSet hw_enforced;
221 AuthorizationSet sw_enforced;
Shawn Willden06298102015-05-25 23:12:48 -0600222 const KeyFactory* key_factory;
Shawn Willden0cb69422015-05-26 08:31:37 -0600223 UniquePtr<Key> key;
224 response->error = LoadKey(request.key_blob, request.additional_params, &hw_enforced,
Shawn Willden06298102015-05-25 23:12:48 -0600225 &sw_enforced, &key_factory, &key);
Shawn Willden2beb6282015-05-20 16:36:24 -0600226 if (response->error != KM_ERROR_OK)
227 return;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600228
Shawn Willdenedb79942015-05-08 06:46:44 -0600229 // TODO(swillden): Move this check to a general authorization checker.
Shawn Willden0cb69422015-05-26 08:31:37 -0600230 // TODO(swillden): Consider introducing error codes for unauthorized usages.
231 response->error = KM_ERROR_INCOMPATIBLE_PURPOSE;
232 if (!hw_enforced.Contains(TAG_PURPOSE, request.purpose) &&
233 !sw_enforced.Contains(TAG_PURPOSE, request.purpose))
Shawn Willdenedb79942015-05-08 06:46:44 -0600234 return;
Shawn Willdenedb79942015-05-08 06:46:44 -0600235
Shawn Willden0cb69422015-05-26 08:31:37 -0600236 response->error = KM_ERROR_UNSUPPORTED_PURPOSE;
Shawn Willden06298102015-05-25 23:12:48 -0600237 OperationFactory* factory = key_factory->GetOperationFactory(request.purpose);
Shawn Willden0cb69422015-05-26 08:31:37 -0600238 if (!factory)
Shawn Willden63ac0432014-12-29 14:07:08 -0700239 return;
Shawn Willden63ac0432014-12-29 14:07:08 -0700240
Shawn Willden3ed6d062015-04-15 13:39:38 -0600241 UniquePtr<Operation> operation(
242 factory->CreateOperation(*key, request.additional_params, &response->error));
Shawn Willdend67afae2014-08-19 12:36:27 -0600243 if (operation.get() == NULL)
Shawn Willden76364712014-08-11 17:48:04 -0600244 return;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600245
Shawn Willden111edb32015-02-05 22:44:24 -0700246 response->output_params.Clear();
247 response->error = operation->Begin(request.additional_params, &response->output_params);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600248 if (response->error != KM_ERROR_OK)
249 return;
250
Shawn Willden23d4a742015-03-19 15:33:21 -0600251 response->error = operation_table_->Add(operation.release(), &response->op_handle);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600252}
253
Shawn Willdenb6837e72015-05-16 09:20:59 -0600254void AndroidKeymaster::UpdateOperation(const UpdateOperationRequest& request,
255 UpdateOperationResponse* response) {
Shawn Willdend6cd7e32014-12-17 08:01:26 -0700256 if (response == NULL)
257 return;
258
Shawn Willden23d4a742015-03-19 15:33:21 -0600259 response->error = KM_ERROR_INVALID_OPERATION_HANDLE;
260 Operation* operation = operation_table_->Find(request.op_handle);
261 if (operation == NULL)
Shawn Willden1615f2e2014-08-13 10:37:40 -0600262 return;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600263
Shawn Willdended8e7d2015-06-01 15:29:12 -0600264 response->error =
265 operation->Update(request.additional_params, request.input, &response->output_params,
266 &response->output, &response->input_consumed);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600267 if (response->error != KM_ERROR_OK) {
268 // Any error invalidates the operation.
Shawn Willden23d4a742015-03-19 15:33:21 -0600269 operation_table_->Delete(request.op_handle);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600270 }
271}
272
Shawn Willdenb6837e72015-05-16 09:20:59 -0600273void AndroidKeymaster::FinishOperation(const FinishOperationRequest& request,
274 FinishOperationResponse* response) {
Shawn Willdend6cd7e32014-12-17 08:01:26 -0700275 if (response == NULL)
276 return;
277
Shawn Willden23d4a742015-03-19 15:33:21 -0600278 response->error = KM_ERROR_INVALID_OPERATION_HANDLE;
279 Operation* operation = operation_table_->Find(request.op_handle);
280 if (operation == NULL)
Shawn Willden1615f2e2014-08-13 10:37:40 -0600281 return;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600282
Shawn Willdended8e7d2015-06-01 15:29:12 -0600283 response->error = operation->Finish(request.additional_params, request.signature,
284 &response->output_params, &response->output);
Shawn Willden23d4a742015-03-19 15:33:21 -0600285 operation_table_->Delete(request.op_handle);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600286}
287
Shawn Willden36d41e22015-06-17 06:39:48 -0600288void AndroidKeymaster::AbortOperation(const AbortOperationRequest& request,
289 AbortOperationResponse* response) {
290 if (!response)
291 return;
Shawn Willden23d4a742015-03-19 15:33:21 -0600292
Shawn Willden36d41e22015-06-17 06:39:48 -0600293 Operation* operation = operation_table_->Find(request.op_handle);
294 if (!operation) {
295 response->error = KM_ERROR_INVALID_OPERATION_HANDLE;
296 return;
297 }
298
299 response->error = operation->Abort();
300 operation_table_->Delete(request.op_handle);
Shawn Willden76364712014-08-11 17:48:04 -0600301}
302
Shawn Willdenb6837e72015-05-16 09:20:59 -0600303void AndroidKeymaster::ExportKey(const ExportKeyRequest& request, ExportKeyResponse* response) {
Shawn Willdenffd790c2014-08-18 21:20:06 -0600304 if (response == NULL)
305 return;
306
Shawn Willden0cb69422015-05-26 08:31:37 -0600307 AuthorizationSet hw_enforced;
308 AuthorizationSet sw_enforced;
309 KeymasterKeyBlob key_material;
310 response->error =
311 context_->ParseKeyBlob(KeymasterKeyBlob(request.key_blob), request.additional_params,
312 &key_material, &hw_enforced, &sw_enforced);
313 if (response->error != KM_ERROR_OK)
314 return;
315
Shawn Willden63ac0432014-12-29 14:07:08 -0700316 keymaster_algorithm_t algorithm;
Shawn Willden06298102015-05-25 23:12:48 -0600317 KeyFactory* key_factory =
318 GetKeyFactory(*context_, hw_enforced, sw_enforced, &algorithm, &response->error);
Shawn Willden0cb69422015-05-26 08:31:37 -0600319 if (!key_factory)
320 return;
321
322 UniquePtr<Key> key;
323 response->error = key_factory->LoadKey(key_material, hw_enforced, sw_enforced, &key);
324 if (response->error != KM_ERROR_OK)
Shawn Willdenffd790c2014-08-18 21:20:06 -0600325 return;
326
Shawn Willdenf268d742014-08-19 15:36:26 -0600327 UniquePtr<uint8_t[]> out_key;
328 size_t size;
Shawn Willden0cb69422015-05-26 08:31:37 -0600329 response->error = key->formatted_key_material(request.key_format, &out_key, &size);
Shawn Willdenf268d742014-08-19 15:36:26 -0600330 if (response->error == KM_ERROR_OK) {
331 response->key_data = out_key.release();
332 response->key_data_length = size;
Shawn Willdenffd790c2014-08-18 21:20:06 -0600333 }
Shawn Willdenffd790c2014-08-18 21:20:06 -0600334}
335
Shawn Willdenb6837e72015-05-16 09:20:59 -0600336void AndroidKeymaster::ImportKey(const ImportKeyRequest& request, ImportKeyResponse* response) {
Shawn Willden437fbd12014-08-20 11:59:49 -0600337 if (response == NULL)
Shawn Willdenffd790c2014-08-18 21:20:06 -0600338 return;
Shawn Willdenffd790c2014-08-18 21:20:06 -0600339
Shawn Willdena278f612014-12-23 11:22:21 -0700340 keymaster_algorithm_t algorithm;
341 KeyFactory* factory = 0;
342 UniquePtr<Key> key;
343 if (!request.key_description.GetTagValue(TAG_ALGORITHM, &algorithm) ||
Shawn Willden06298102015-05-25 23:12:48 -0600344 !(factory = context_->GetKeyFactory(algorithm)))
Shawn Willdena278f612014-12-23 11:22:21 -0700345 response->error = KM_ERROR_UNSUPPORTED_ALGORITHM;
Shawn Willden0cb69422015-05-26 08:31:37 -0600346 else {
347 keymaster_key_blob_t key_material = {request.key_data, request.key_data_length};
348 KeymasterKeyBlob key_blob;
349 response->error = factory->ImportKey(request.key_description, request.key_format,
350 KeymasterKeyBlob(key_material), &key_blob,
351 &response->enforced, &response->unenforced);
352 if (response->error == KM_ERROR_OK)
353 response->key_blob = key_blob.release();
354 }
Shawn Willdenffd790c2014-08-18 21:20:06 -0600355}
356
Shawn Willden36d41e22015-06-17 06:39:48 -0600357void AndroidKeymaster::DeleteKey(const DeleteKeyRequest& request, DeleteKeyResponse* response) {
358 if (!response)
359 return;
360 response->error = context_->DeleteKey(KeymasterKeyBlob(request.key_blob));
Shawn Willden2beb6282015-05-20 16:36:24 -0600361}
362
Shawn Willden36d41e22015-06-17 06:39:48 -0600363void AndroidKeymaster::DeleteAllKeys(const DeleteAllKeysRequest&, DeleteAllKeysResponse* response) {
364 if (!response)
365 return;
366 response->error = context_->DeleteAllKeys();
Shawn Willden2beb6282015-05-20 16:36:24 -0600367}
368
Shawn Willden0cb69422015-05-26 08:31:37 -0600369keymaster_error_t AndroidKeymaster::LoadKey(const keymaster_key_blob_t& key_blob,
370 const AuthorizationSet& additional_params,
371 AuthorizationSet* hw_enforced,
372 AuthorizationSet* sw_enforced,
Shawn Willden06298102015-05-25 23:12:48 -0600373 const KeyFactory** factory, UniquePtr<Key>* key) {
Shawn Willden0cb69422015-05-26 08:31:37 -0600374 KeymasterKeyBlob key_material;
375 keymaster_error_t error = context_->ParseKeyBlob(KeymasterKeyBlob(key_blob), additional_params,
376 &key_material, hw_enforced, sw_enforced);
Shawn Willden437fbd12014-08-20 11:59:49 -0600377 if (error != KM_ERROR_OK)
378 return error;
379
Shawn Willden06298102015-05-25 23:12:48 -0600380 keymaster_algorithm_t algorithm;
381 *factory = GetKeyFactory(*context_, *hw_enforced, *sw_enforced, &algorithm, &error);
382 if (error != KM_ERROR_OK)
Shawn Willden437fbd12014-08-20 11:59:49 -0600383 return error;
Shawn Willden128ffe02014-08-06 12:31:33 -0600384
Shawn Willden06298102015-05-25 23:12:48 -0600385 return (*factory)->LoadKey(key_material, *hw_enforced, *sw_enforced, key);
Shawn Willden128ffe02014-08-06 12:31:33 -0600386}
387
Shawn Willden128ffe02014-08-06 12:31:33 -0600388} // namespace keymaster