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 <keymaster/km_openssl/openssl_utils.h> 18 19 #include <openssl/rand.h> 20 #include <keymaster/android_keymaster_utils.h> 21 22 #include <keymaster/km_openssl/openssl_err.h> 23 24 namespace keymaster { 25 26 keymaster_error_t ec_get_group_size(const EC_GROUP* group, size_t* key_size_bits) { 27 switch (EC_GROUP_get_curve_name(group)) { 28 case NID_secp224r1: 29 *key_size_bits = 224; 30 break; 31 case NID_X9_62_prime256v1: 32 *key_size_bits = 256; 33 break; 34 case NID_secp384r1: 35 *key_size_bits = 384; 36 break; 37 case NID_secp521r1: 38 *key_size_bits = 521; 39 break; 40 default: 41 return KM_ERROR_UNSUPPORTED_EC_FIELD; 42 } 43 return KM_ERROR_OK; 44 } 45 46 EC_GROUP* ec_get_group(keymaster_ec_curve_t curve) { 47 switch (curve) { 48 case KM_EC_CURVE_P_224: 49 return EC_GROUP_new_by_curve_name(NID_secp224r1); 50 break; 51 case KM_EC_CURVE_P_256: 52 return EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1); 53 break; 54 case KM_EC_CURVE_P_384: 55 return EC_GROUP_new_by_curve_name(NID_secp384r1); 56 break; 57 case KM_EC_CURVE_P_521: 58 return EC_GROUP_new_by_curve_name(NID_secp521r1); 59 break; 60 default: 61 return nullptr; 62 break; 63 } 64 } 65 66 static int convert_to_evp(keymaster_algorithm_t algorithm) { 67 switch (algorithm) { 68 case KM_ALGORITHM_RSA: 69 return EVP_PKEY_RSA; 70 case KM_ALGORITHM_EC: 71 return EVP_PKEY_EC; 72 default: 73 return -1; 74 }; 75 } 76 77 keymaster_error_t convert_pkcs8_blob_to_evp(const uint8_t* key_data, size_t key_length, 78 keymaster_algorithm_t expected_algorithm, 79 UniquePtr<EVP_PKEY, EVP_PKEY_Delete>* pkey) { 80 if (key_data == nullptr || key_length <= 0) 81 return KM_ERROR_INVALID_KEY_BLOB; 82 83 UniquePtr<PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_Delete> pkcs8( 84 d2i_PKCS8_PRIV_KEY_INFO(nullptr, &key_data, key_length)); 85 if (pkcs8.get() == nullptr) 86 return TranslateLastOpenSslError(true /* log_message */); 87 88 pkey->reset(EVP_PKCS82PKEY(pkcs8.get())); 89 if (!pkey->get()) 90 return TranslateLastOpenSslError(true /* log_message */); 91 92 if (EVP_PKEY_type((*pkey)->type) != convert_to_evp(expected_algorithm)) { 93 LOG_E("EVP key algorithm was %d, not the expected %d", EVP_PKEY_type((*pkey)->type), 94 convert_to_evp(expected_algorithm)); 95 return KM_ERROR_INVALID_KEY_BLOB; 96 } 97 98 return KM_ERROR_OK; 99 } 100 101 keymaster_error_t KeyMaterialToEvpKey(keymaster_key_format_t key_format, 102 const KeymasterKeyBlob& key_material, 103 keymaster_algorithm_t expected_algorithm, 104 UniquePtr<EVP_PKEY, EVP_PKEY_Delete>* pkey) { 105 if (key_format != KM_KEY_FORMAT_PKCS8) 106 return KM_ERROR_UNSUPPORTED_KEY_FORMAT; 107 108 return convert_pkcs8_blob_to_evp(key_material.key_material, key_material.key_material_size, 109 expected_algorithm, pkey); 110 } 111 112 keymaster_error_t EvpKeyToKeyMaterial(const EVP_PKEY* pkey, KeymasterKeyBlob* key_blob) { 113 int key_data_size = i2d_PrivateKey(pkey, nullptr /* key_data*/); 114 if (key_data_size <= 0) 115 return TranslateLastOpenSslError(); 116 117 if (!key_blob->Reset(key_data_size)) 118 return KM_ERROR_MEMORY_ALLOCATION_FAILED; 119 120 uint8_t* tmp = key_blob->writable_data(); 121 i2d_PrivateKey(pkey, &tmp); 122 123 return KM_ERROR_OK; 124 } 125 126 size_t ec_group_size_bits(EC_KEY* ec_key) { 127 const EC_GROUP* group = EC_KEY_get0_group(ec_key); 128 UniquePtr<BN_CTX, BN_CTX_Delete> bn_ctx(BN_CTX_new()); 129 UniquePtr<BIGNUM, BIGNUM_Delete> order(BN_new()); 130 if (!EC_GROUP_get_order(group, order.get(), bn_ctx.get())) { 131 LOG_E("Failed to get EC group order", 0); 132 return 0; 133 } 134 return BN_num_bits(order.get()); 135 } 136 137 keymaster_error_t GenerateRandom(uint8_t* buf, size_t length) { 138 if (RAND_bytes(buf, length) != 1) 139 return KM_ERROR_UNKNOWN_ERROR; 140 return KM_ERROR_OK; 141 } 142 143 } // namespace keymaster 144