1 /*
2  * Copyright 2015 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 "openssl_utils.h"
18 
19 #include <keymaster/android_keymaster_utils.h>
20 
21 #include "openssl_err.h"
22 
23 namespace keymaster {
24 
convert_to_evp(keymaster_algorithm_t algorithm)25 static int convert_to_evp(keymaster_algorithm_t algorithm) {
26     switch (algorithm) {
27     case KM_ALGORITHM_RSA:
28         return EVP_PKEY_RSA;
29     case KM_ALGORITHM_EC:
30         return EVP_PKEY_EC;
31     default:
32         return -1;
33     };
34 }
35 
convert_pkcs8_blob_to_evp(const uint8_t * key_data,size_t key_length,keymaster_algorithm_t expected_algorithm,UniquePtr<EVP_PKEY,EVP_PKEY_Delete> * pkey)36 keymaster_error_t convert_pkcs8_blob_to_evp(const uint8_t* key_data, size_t key_length,
37                                             keymaster_algorithm_t expected_algorithm,
38                                             UniquePtr<EVP_PKEY, EVP_PKEY_Delete>* pkey) {
39     if (key_data == NULL || key_length <= 0)
40         return KM_ERROR_INVALID_KEY_BLOB;
41 
42     UniquePtr<PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_Delete> pkcs8(
43         d2i_PKCS8_PRIV_KEY_INFO(NULL, &key_data, key_length));
44     if (pkcs8.get() == NULL)
45         return TranslateLastOpenSslError(true /* log_message */);
46 
47     pkey->reset(EVP_PKCS82PKEY(pkcs8.get()));
48     if (!pkey->get())
49         return TranslateLastOpenSslError(true /* log_message */);
50 
51     if (EVP_PKEY_type((*pkey)->type) != convert_to_evp(expected_algorithm)) {
52         LOG_E("EVP key algorithm was %d, not the expected %d", EVP_PKEY_type((*pkey)->type),
53               convert_to_evp(expected_algorithm));
54         return KM_ERROR_INVALID_KEY_BLOB;
55     }
56 
57     return KM_ERROR_OK;
58 }
59 
KeyMaterialToEvpKey(keymaster_key_format_t key_format,const KeymasterKeyBlob & key_material,keymaster_algorithm_t expected_algorithm,UniquePtr<EVP_PKEY,EVP_PKEY_Delete> * pkey)60 keymaster_error_t KeyMaterialToEvpKey(keymaster_key_format_t key_format,
61                                       const KeymasterKeyBlob& key_material,
62                                       keymaster_algorithm_t expected_algorithm,
63                                       UniquePtr<EVP_PKEY, EVP_PKEY_Delete>* pkey) {
64     if (key_format != KM_KEY_FORMAT_PKCS8)
65         return KM_ERROR_UNSUPPORTED_KEY_FORMAT;
66 
67     return convert_pkcs8_blob_to_evp(key_material.key_material, key_material.key_material_size,
68                                      expected_algorithm, pkey);
69 }
70 
EvpKeyToKeyMaterial(const EVP_PKEY * pkey,KeymasterKeyBlob * key_blob)71 keymaster_error_t EvpKeyToKeyMaterial(const EVP_PKEY* pkey, KeymasterKeyBlob* key_blob) {
72     int key_data_size = i2d_PrivateKey(pkey, NULL /* key_data*/);
73     if (key_data_size <= 0)
74         return TranslateLastOpenSslError();
75 
76     if (!key_blob->Reset(key_data_size))
77         return KM_ERROR_MEMORY_ALLOCATION_FAILED;
78 
79     uint8_t* tmp = key_blob->writable_data();
80     i2d_PrivateKey(pkey, &tmp);
81 
82     return KM_ERROR_OK;
83 }
84 
85 }  // namespace keymaster
86