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 namespace keymaster {
21 
LoadKey(KeymasterKeyBlob && key_material,const AuthorizationSet & additional_params,AuthorizationSet && hw_enforced,AuthorizationSet && sw_enforced,UniquePtr<Key> * key) const22 keymaster_error_t KeymasterPassthroughKeyFactory::LoadKey(KeymasterKeyBlob&& key_material,
23                                                           const AuthorizationSet& additional_params,
24                                                           AuthorizationSet&& hw_enforced,
25                                                           AuthorizationSet&& sw_enforced,
26                                                           UniquePtr<Key>* key) const {
27     keymaster_error_t error = KM_ERROR_OK;
28     if (!key) return KM_ERROR_OUTPUT_PARAMETER_NULL;
29 
30     key->reset(new (std::nothrow)
31                    KeymasterPassthroughKey(move(key_material), move(hw_enforced), move(sw_enforced),
32                                            this, &error, additional_params, engine_));
33     if (!key->get()) error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
34 
35     return error;
36 }
37 
38 const keymaster_key_format_t*
SupportedImportFormats(size_t * format_count) const39 KeymasterPassthroughKeyFactory::SupportedImportFormats(size_t* format_count) const {
40     if (format_count) *format_count = 0;
41     return nullptr;
42 }
43 const keymaster_key_format_t*
SupportedExportFormats(size_t * format_count) const44 KeymasterPassthroughKeyFactory::SupportedExportFormats(size_t* format_count) const {
45     if (format_count) *format_count = 0;
46     return nullptr;
47 }
48 
formatted_key_material(keymaster_key_format_t format,UniquePtr<uint8_t[]> * material,size_t * size) const49 keymaster_error_t KeymasterPassthroughKey::formatted_key_material(keymaster_key_format_t format,
50                                                                   UniquePtr<uint8_t[]>* material,
51                                                                   size_t* size) const {
52     if (!material || !size) {
53         return KM_ERROR_OUTPUT_PARAMETER_NULL;
54     }
55     keymaster_blob_t km_app_data = {};
56     KeymasterBlob app_data;
57     if (additional_parameters_.GetTagValue(TAG_APPLICATION_DATA, &km_app_data)) {
58         app_data = KeymasterBlob(km_app_data);
59     }
60 
61     keymaster_blob_t km_client_id = {};
62     KeymasterBlob client_id;
63     if (additional_parameters_.GetTagValue(TAG_APPLICATION_ID, &km_client_id)) {
64         client_id = KeymasterBlob(km_client_id);
65     }
66 
67     KeymasterBlob export_data;
68 
69     keymaster_error_t error =
70         engine_->ExportKey(format, key_material(), client_id, app_data, &export_data);
71     if (error == KM_ERROR_OK) {
72         keymaster_blob_t export_blob = export_data.release();
73         material->reset(const_cast<uint8_t*>(export_blob.data));
74         *size = export_blob.data_length;
75     }
76     return error;
77 }
78 
79 }  // namespace keymaster
80