1 /*
2 **
3 ** Copyright 2017, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 ** http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17
18 #include <keymaster/legacy_support/keymaster_passthrough_key.h>
19
20 #include <utility>
21
22 namespace keymaster {
23
LoadKey(KeymasterKeyBlob && key_material,const AuthorizationSet & additional_params,AuthorizationSet && hw_enforced,AuthorizationSet && sw_enforced,UniquePtr<Key> * key) const24 keymaster_error_t KeymasterPassthroughKeyFactory::LoadKey(KeymasterKeyBlob&& key_material,
25 const AuthorizationSet& additional_params,
26 AuthorizationSet&& hw_enforced,
27 AuthorizationSet&& sw_enforced,
28 UniquePtr<Key>* key) const {
29 keymaster_error_t error = KM_ERROR_OK;
30 if (!key) return KM_ERROR_OUTPUT_PARAMETER_NULL;
31
32 key->reset(new (std::nothrow)
33 KeymasterPassthroughKey(std::move(key_material), std::move(hw_enforced),
34 std::move(sw_enforced), this, &error, additional_params,
35 engine_));
36 if (!key->get()) error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
37
38 return error;
39 }
40
41 const keymaster_key_format_t*
SupportedImportFormats(size_t * format_count) const42 KeymasterPassthroughKeyFactory::SupportedImportFormats(size_t* format_count) const {
43 if (format_count) *format_count = 0;
44 return nullptr;
45 }
46 const keymaster_key_format_t*
SupportedExportFormats(size_t * format_count) const47 KeymasterPassthroughKeyFactory::SupportedExportFormats(size_t* format_count) const {
48 if (format_count) *format_count = 0;
49 return nullptr;
50 }
51
formatted_key_material(keymaster_key_format_t format,UniquePtr<uint8_t[]> * material,size_t * size) const52 keymaster_error_t KeymasterPassthroughKey::formatted_key_material(keymaster_key_format_t format,
53 UniquePtr<uint8_t[]>* material,
54 size_t* size) const {
55 if (!material || !size) {
56 return KM_ERROR_OUTPUT_PARAMETER_NULL;
57 }
58 keymaster_blob_t km_app_data = {};
59 KeymasterBlob app_data;
60 if (additional_parameters_.GetTagValue(TAG_APPLICATION_DATA, &km_app_data)) {
61 app_data = KeymasterBlob(km_app_data);
62 }
63
64 keymaster_blob_t km_client_id = {};
65 KeymasterBlob client_id;
66 if (additional_parameters_.GetTagValue(TAG_APPLICATION_ID, &km_client_id)) {
67 client_id = KeymasterBlob(km_client_id);
68 }
69
70 KeymasterBlob export_data;
71
72 keymaster_error_t error =
73 engine_->ExportKey(format, key_material(), client_id, app_data, &export_data);
74 if (error == KM_ERROR_OK) {
75 keymaster_blob_t export_blob = export_data.release();
76 material->reset(const_cast<uint8_t*>(export_blob.data));
77 *size = export_blob.data_length;
78 }
79 return error;
80 }
81
82 } // namespace keymaster
83