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