blob: 253bbdbfa683356931b80304ddb96f94c1cde484 [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 Willdenb6837e72015-05-16 09:20:59 -060083void AndroidKeymaster::SupportedAlgorithms(
Shawn Willden3809b932014-12-02 06:59:46 -070084 SupportedResponse<keymaster_algorithm_t>* response) const {
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 Willdenb6837e72015-05-16 09:20:59 -0600119void AndroidKeymaster::SupportedBlockModes(
Shawn Willden63ac0432014-12-29 14:07:08 -0700120 keymaster_algorithm_t algorithm, keymaster_purpose_t purpose,
121 SupportedResponse<keymaster_block_mode_t>* response) const {
Shawn Willden06298102015-05-25 23:12:48 -0600122 GetSupported(*context_, algorithm, purpose, &OperationFactory::SupportedBlockModes, response);
Shawn Willden63ac0432014-12-29 14:07:08 -0700123}
Shawn Willden3809b932014-12-02 06:59:46 -0700124
Shawn Willdenb6837e72015-05-16 09:20:59 -0600125void AndroidKeymaster::SupportedPaddingModes(
Shawn Willden4200f212014-12-02 07:01:21 -0700126 keymaster_algorithm_t algorithm, keymaster_purpose_t purpose,
Shawn Willden3809b932014-12-02 06:59:46 -0700127 SupportedResponse<keymaster_padding_t>* response) const {
Shawn Willden06298102015-05-25 23:12:48 -0600128 GetSupported(*context_, algorithm, purpose, &OperationFactory::SupportedPaddingModes, response);
Shawn Willden128ffe02014-08-06 12:31:33 -0600129}
130
Shawn Willdenb6837e72015-05-16 09:20:59 -0600131void AndroidKeymaster::SupportedDigests(keymaster_algorithm_t algorithm,
132 keymaster_purpose_t purpose,
133 SupportedResponse<keymaster_digest_t>* response) const {
Shawn Willden06298102015-05-25 23:12:48 -0600134 GetSupported(*context_, algorithm, purpose, &OperationFactory::SupportedDigests, response);
Shawn Willden128ffe02014-08-06 12:31:33 -0600135}
136
Shawn Willdenb6837e72015-05-16 09:20:59 -0600137void AndroidKeymaster::SupportedImportFormats(
Shawn Willden3809b932014-12-02 06:59:46 -0700138 keymaster_algorithm_t algorithm, SupportedResponse<keymaster_key_format_t>* response) const {
Shawn Willden06298102015-05-25 23:12:48 -0600139 if (response == NULL || !check_supported(*context_, 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 Willden06298102015-05-25 23:12:48 -0600144 context_->GetKeyFactory(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 Willdenb6837e72015-05-16 09:20:59 -0600148void AndroidKeymaster::SupportedExportFormats(
Shawn Willden3809b932014-12-02 06:59:46 -0700149 keymaster_algorithm_t algorithm, SupportedResponse<keymaster_key_format_t>* response) const {
Shawn Willden06298102015-05-25 23:12:48 -0600150 if (response == NULL || !check_supported(*context_, 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 Willden06298102015-05-25 23:12:48 -0600155 context_->GetKeyFactory(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 Willden0cb69422015-05-26 08:31:37 -0600159keymaster_error_t AndroidKeymaster::AddRngEntropy(const AddEntropyRequest& request) {
160 return context_->AddRngEntropy(request.random_data.peek_read(),
161 request.random_data.available_read());
162}
163
Shawn Willdenb6837e72015-05-16 09:20:59 -0600164void AndroidKeymaster::GenerateKey(const GenerateKeyRequest& request,
165 GenerateKeyResponse* response) {
Shawn Willden128ffe02014-08-06 12:31:33 -0600166 if (response == NULL)
167 return;
Shawn Willden128ffe02014-08-06 12:31:33 -0600168
Shawn Willdena278f612014-12-23 11:22:21 -0700169 keymaster_algorithm_t algorithm;
170 KeyFactory* factory = 0;
171 UniquePtr<Key> key;
172 if (!request.key_description.GetTagValue(TAG_ALGORITHM, &algorithm) ||
Shawn Willden06298102015-05-25 23:12:48 -0600173 !(factory = context_->GetKeyFactory(algorithm)))
Shawn Willdena278f612014-12-23 11:22:21 -0700174 response->error = KM_ERROR_UNSUPPORTED_ALGORITHM;
Shawn Willden0cb69422015-05-26 08:31:37 -0600175 else {
176 KeymasterKeyBlob key_blob;
Shawn Willden2beb6282015-05-20 16:36:24 -0600177 response->enforced.Clear();
178 response->unenforced.Clear();
Shawn Willden0cb69422015-05-26 08:31:37 -0600179 response->error = factory->GenerateKey(request.key_description, &key_blob,
180 &response->enforced, &response->unenforced);
181 if (response->error == KM_ERROR_OK)
182 response->key_blob = key_blob.release();
183 }
Shawn Willden128ffe02014-08-06 12:31:33 -0600184}
185
Shawn Willdenb6837e72015-05-16 09:20:59 -0600186void AndroidKeymaster::GetKeyCharacteristics(const GetKeyCharacteristicsRequest& request,
187 GetKeyCharacteristicsResponse* response) {
Shawn Willden1615f2e2014-08-13 10:37:40 -0600188 if (response == NULL)
189 return;
Shawn Willden76364712014-08-11 17:48:04 -0600190
Shawn Willden0cb69422015-05-26 08:31:37 -0600191 KeymasterKeyBlob key_material;
192 response->error =
193 context_->ParseKeyBlob(KeymasterKeyBlob(request.key_blob), request.additional_params,
194 &key_material, &response->enforced, &response->unenforced);
195 if (response->error != KM_ERROR_OK)
Shawn Willden1615f2e2014-08-13 10:37:40 -0600196 return;
Shawn Willden0cb69422015-05-26 08:31:37 -0600197}
Shawn Willden1615f2e2014-08-13 10:37:40 -0600198
Shawn Willden06298102015-05-25 23:12:48 -0600199static KeyFactory* GetKeyFactory(const KeymasterContext& context,
200 const AuthorizationSet& hw_enforced,
Shawn Willden0cb69422015-05-26 08:31:37 -0600201 const AuthorizationSet& sw_enforced,
202 keymaster_algorithm_t* algorithm, keymaster_error_t* error) {
203 *error = KM_ERROR_UNSUPPORTED_ALGORITHM;
204 if (!hw_enforced.GetTagValue(TAG_ALGORITHM, algorithm) &&
205 !sw_enforced.GetTagValue(TAG_ALGORITHM, algorithm))
206 return nullptr;
Shawn Willden06298102015-05-25 23:12:48 -0600207 KeyFactory* factory = context.GetKeyFactory(*algorithm);
Shawn Willden0cb69422015-05-26 08:31:37 -0600208 if (factory)
209 *error = KM_ERROR_OK;
210 return factory;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600211}
212
Shawn Willdenb6837e72015-05-16 09:20:59 -0600213void AndroidKeymaster::BeginOperation(const BeginOperationRequest& request,
214 BeginOperationResponse* response) {
Shawn Willden1615f2e2014-08-13 10:37:40 -0600215 if (response == NULL)
216 return;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600217 response->op_handle = 0;
218
Shawn Willden0cb69422015-05-26 08:31:37 -0600219 AuthorizationSet hw_enforced;
220 AuthorizationSet sw_enforced;
Shawn Willden06298102015-05-25 23:12:48 -0600221 const KeyFactory* key_factory;
Shawn Willden0cb69422015-05-26 08:31:37 -0600222 UniquePtr<Key> key;
223 response->error = LoadKey(request.key_blob, request.additional_params, &hw_enforced,
Shawn Willden06298102015-05-25 23:12:48 -0600224 &sw_enforced, &key_factory, &key);
Shawn Willden2beb6282015-05-20 16:36:24 -0600225 if (response->error != KM_ERROR_OK)
226 return;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600227
Shawn Willdenedb79942015-05-08 06:46:44 -0600228 // TODO(swillden): Move this check to a general authorization checker.
Shawn Willden0cb69422015-05-26 08:31:37 -0600229 // TODO(swillden): Consider introducing error codes for unauthorized usages.
230 response->error = KM_ERROR_INCOMPATIBLE_PURPOSE;
231 if (!hw_enforced.Contains(TAG_PURPOSE, request.purpose) &&
232 !sw_enforced.Contains(TAG_PURPOSE, request.purpose))
Shawn Willdenedb79942015-05-08 06:46:44 -0600233 return;
Shawn Willdenedb79942015-05-08 06:46:44 -0600234
Shawn Willden0cb69422015-05-26 08:31:37 -0600235 response->error = KM_ERROR_UNSUPPORTED_PURPOSE;
Shawn Willden06298102015-05-25 23:12:48 -0600236 OperationFactory* factory = key_factory->GetOperationFactory(request.purpose);
Shawn Willden0cb69422015-05-26 08:31:37 -0600237 if (!factory)
Shawn Willden63ac0432014-12-29 14:07:08 -0700238 return;
Shawn Willden63ac0432014-12-29 14:07:08 -0700239
Shawn Willden3ed6d062015-04-15 13:39:38 -0600240 UniquePtr<Operation> operation(
241 factory->CreateOperation(*key, request.additional_params, &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
Shawn Willden23d4a742015-03-19 15:33:21 -0600250 response->error = operation_table_->Add(operation.release(), &response->op_handle);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600251}
252
Shawn Willdenb6837e72015-05-16 09:20:59 -0600253void AndroidKeymaster::UpdateOperation(const UpdateOperationRequest& request,
254 UpdateOperationResponse* response) {
Shawn Willdend6cd7e32014-12-17 08:01:26 -0700255 if (response == NULL)
256 return;
257
Shawn Willden23d4a742015-03-19 15:33:21 -0600258 response->error = KM_ERROR_INVALID_OPERATION_HANDLE;
259 Operation* operation = operation_table_->Find(request.op_handle);
260 if (operation == NULL)
Shawn Willden1615f2e2014-08-13 10:37:40 -0600261 return;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600262
Shawn Willden23d4a742015-03-19 15:33:21 -0600263 response->error = operation->Update(request.additional_params, request.input, &response->output,
264 &response->input_consumed);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600265 if (response->error != KM_ERROR_OK) {
266 // Any error invalidates the operation.
Shawn Willden23d4a742015-03-19 15:33:21 -0600267 operation_table_->Delete(request.op_handle);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600268 }
269}
270
Shawn Willdenb6837e72015-05-16 09:20:59 -0600271void AndroidKeymaster::FinishOperation(const FinishOperationRequest& request,
272 FinishOperationResponse* response) {
Shawn Willdend6cd7e32014-12-17 08:01:26 -0700273 if (response == NULL)
274 return;
275
Shawn Willden23d4a742015-03-19 15:33:21 -0600276 response->error = KM_ERROR_INVALID_OPERATION_HANDLE;
277 Operation* operation = operation_table_->Find(request.op_handle);
278 if (operation == NULL)
Shawn Willden1615f2e2014-08-13 10:37:40 -0600279 return;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600280
Shawn Willden6bfbff02015-02-06 19:48:24 -0700281 response->error =
Shawn Willden23d4a742015-03-19 15:33:21 -0600282 operation->Finish(request.additional_params, request.signature, &response->output);
283 operation_table_->Delete(request.op_handle);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600284}
285
Shawn Willdenb6837e72015-05-16 09:20:59 -0600286keymaster_error_t AndroidKeymaster::AbortOperation(const keymaster_operation_handle_t op_handle) {
Shawn Willden23d4a742015-03-19 15:33:21 -0600287 Operation* operation = operation_table_->Find(op_handle);
288 if (operation == NULL)
Shawn Willden1615f2e2014-08-13 10:37:40 -0600289 return KM_ERROR_INVALID_OPERATION_HANDLE;
Shawn Willden23d4a742015-03-19 15:33:21 -0600290
291 keymaster_error_t error = operation->Abort();
292 operation_table_->Delete(op_handle);
Shawn Willden7ec74f92014-12-11 13:57:59 -0700293 if (error != KM_ERROR_OK)
294 return error;
Shawn Willden1615f2e2014-08-13 10:37:40 -0600295 return KM_ERROR_OK;
Shawn Willden76364712014-08-11 17:48:04 -0600296}
297
Shawn Willdenb6837e72015-05-16 09:20:59 -0600298void AndroidKeymaster::ExportKey(const ExportKeyRequest& request, ExportKeyResponse* response) {
Shawn Willdenffd790c2014-08-18 21:20:06 -0600299 if (response == NULL)
300 return;
301
Shawn Willden0cb69422015-05-26 08:31:37 -0600302 AuthorizationSet hw_enforced;
303 AuthorizationSet sw_enforced;
304 KeymasterKeyBlob key_material;
305 response->error =
306 context_->ParseKeyBlob(KeymasterKeyBlob(request.key_blob), request.additional_params,
307 &key_material, &hw_enforced, &sw_enforced);
308 if (response->error != KM_ERROR_OK)
309 return;
310
Shawn Willden63ac0432014-12-29 14:07:08 -0700311 keymaster_algorithm_t algorithm;
Shawn Willden06298102015-05-25 23:12:48 -0600312 KeyFactory* key_factory =
313 GetKeyFactory(*context_, hw_enforced, sw_enforced, &algorithm, &response->error);
Shawn Willden0cb69422015-05-26 08:31:37 -0600314 if (!key_factory)
315 return;
316
317 UniquePtr<Key> key;
318 response->error = key_factory->LoadKey(key_material, hw_enforced, sw_enforced, &key);
319 if (response->error != KM_ERROR_OK)
Shawn Willdenffd790c2014-08-18 21:20:06 -0600320 return;
321
Shawn Willdenf268d742014-08-19 15:36:26 -0600322 UniquePtr<uint8_t[]> out_key;
323 size_t size;
Shawn Willden0cb69422015-05-26 08:31:37 -0600324 response->error = key->formatted_key_material(request.key_format, &out_key, &size);
Shawn Willdenf268d742014-08-19 15:36:26 -0600325 if (response->error == KM_ERROR_OK) {
326 response->key_data = out_key.release();
327 response->key_data_length = size;
Shawn Willdenffd790c2014-08-18 21:20:06 -0600328 }
Shawn Willdenffd790c2014-08-18 21:20:06 -0600329}
330
Shawn Willdenb6837e72015-05-16 09:20:59 -0600331void AndroidKeymaster::ImportKey(const ImportKeyRequest& request, ImportKeyResponse* response) {
Shawn Willden437fbd12014-08-20 11:59:49 -0600332 if (response == NULL)
Shawn Willdenffd790c2014-08-18 21:20:06 -0600333 return;
Shawn Willdenffd790c2014-08-18 21:20:06 -0600334
Shawn Willdena278f612014-12-23 11:22:21 -0700335 keymaster_algorithm_t algorithm;
336 KeyFactory* factory = 0;
337 UniquePtr<Key> key;
338 if (!request.key_description.GetTagValue(TAG_ALGORITHM, &algorithm) ||
Shawn Willden06298102015-05-25 23:12:48 -0600339 !(factory = context_->GetKeyFactory(algorithm)))
Shawn Willdena278f612014-12-23 11:22:21 -0700340 response->error = KM_ERROR_UNSUPPORTED_ALGORITHM;
Shawn Willden0cb69422015-05-26 08:31:37 -0600341 else {
342 keymaster_key_blob_t key_material = {request.key_data, request.key_data_length};
343 KeymasterKeyBlob key_blob;
344 response->error = factory->ImportKey(request.key_description, request.key_format,
345 KeymasterKeyBlob(key_material), &key_blob,
346 &response->enforced, &response->unenforced);
347 if (response->error == KM_ERROR_OK)
348 response->key_blob = key_blob.release();
349 }
Shawn Willdenffd790c2014-08-18 21:20:06 -0600350}
351
Shawn Willden2beb6282015-05-20 16:36:24 -0600352keymaster_error_t AndroidKeymaster::DeleteKey(const DeleteKeyRequest& request) {
353 return context_->DeleteKey(KeymasterKeyBlob(request.key_blob));
354}
355
356keymaster_error_t AndroidKeymaster::DeleteAllKeys() {
357 return context_->DeleteAllKeys();
358}
359
Shawn Willden0cb69422015-05-26 08:31:37 -0600360keymaster_error_t AndroidKeymaster::LoadKey(const keymaster_key_blob_t& key_blob,
361 const AuthorizationSet& additional_params,
362 AuthorizationSet* hw_enforced,
363 AuthorizationSet* sw_enforced,
Shawn Willden06298102015-05-25 23:12:48 -0600364 const KeyFactory** factory, UniquePtr<Key>* key) {
Shawn Willden0cb69422015-05-26 08:31:37 -0600365 KeymasterKeyBlob key_material;
366 keymaster_error_t error = context_->ParseKeyBlob(KeymasterKeyBlob(key_blob), additional_params,
367 &key_material, hw_enforced, sw_enforced);
Shawn Willden437fbd12014-08-20 11:59:49 -0600368 if (error != KM_ERROR_OK)
369 return error;
370
Shawn Willden06298102015-05-25 23:12:48 -0600371 keymaster_algorithm_t algorithm;
372 *factory = GetKeyFactory(*context_, *hw_enforced, *sw_enforced, &algorithm, &error);
373 if (error != KM_ERROR_OK)
Shawn Willden437fbd12014-08-20 11:59:49 -0600374 return error;
Shawn Willden128ffe02014-08-06 12:31:33 -0600375
Shawn Willden06298102015-05-25 23:12:48 -0600376 return (*factory)->LoadKey(key_material, *hw_enforced, *sw_enforced, key);
Shawn Willden128ffe02014-08-06 12:31:33 -0600377}
378
Shawn Willden128ffe02014-08-06 12:31:33 -0600379} // namespace keymaster