• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 #include <keymaster/asymmetric_key_factory.h>
18 
19 #include <utility>
20 
21 #include <keymaster/android_keymaster_utils.h>
22 
23 #include <keymaster/km_openssl/asymmetric_key.h>
24 #include <keymaster/km_openssl/openssl_err.h>
25 #include <keymaster/km_openssl/openssl_utils.h>
26 
27 namespace keymaster {
28 
29 static const keymaster_key_format_t supported_import_formats[] = {KM_KEY_FORMAT_PKCS8};
30 const keymaster_key_format_t*
SupportedImportFormats(size_t * format_count) const31 AsymmetricKeyFactory::SupportedImportFormats(size_t* format_count) const {
32     *format_count = array_length(supported_import_formats);
33     return supported_import_formats;
34 }
35 
36 static const keymaster_key_format_t supported_export_formats[] = {KM_KEY_FORMAT_X509};
37 const keymaster_key_format_t*
SupportedExportFormats(size_t * format_count) const38 AsymmetricKeyFactory::SupportedExportFormats(size_t* format_count) const {
39     *format_count = array_length(supported_export_formats);
40     return supported_export_formats;
41 }
42 
LoadKey(KeymasterKeyBlob && key_material,const AuthorizationSet &,AuthorizationSet && hw_enforced,AuthorizationSet && sw_enforced,UniquePtr<Key> * key) const43 keymaster_error_t AsymmetricKeyFactory::LoadKey(KeymasterKeyBlob&& key_material,
44                                                 const AuthorizationSet& /* additional_params */,
45                                                 AuthorizationSet&& hw_enforced,
46                                                 AuthorizationSet&& sw_enforced,
47                                                 UniquePtr<Key>* key) const {
48     UniquePtr<AsymmetricKey> asym_key;
49     keymaster_error_t error = CreateEmptyKey(std::move(hw_enforced), std::move(sw_enforced),
50                                              &asym_key);
51     if (error != KM_ERROR_OK) return error;
52 
53     const uint8_t* tmp = key_material.key_material;
54     asym_key->key_material() = std::move(key_material);
55 
56     EVP_PKEY* pkey = d2i_PrivateKey(asym_key->evp_key_type(), nullptr /* pkey */, &tmp,
57                                     asym_key->key_material().key_material_size);
58     if (!pkey) return TranslateLastOpenSslError();
59     UniquePtr<EVP_PKEY, EVP_PKEY_Delete> pkey_deleter(pkey);
60 
61     if (!asym_key->EvpToInternal(pkey)) {
62         error = TranslateLastOpenSslError();
63     } else {
64         *key = std::move(asym_key);
65     }
66 
67     return error;
68 }
69 
70 }  // namespace keymaster
71