|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 OR ISC |
| 3 | + |
| 4 | +#include <gtest/gtest.h> |
| 5 | +#include <openssl/pem.h> |
| 6 | +#include <openssl/ec.h> |
| 7 | +#include "internal.h" |
| 8 | +#include "test_util.h" |
| 9 | +#include "../crypto/test/test_util.h" |
| 10 | + |
| 11 | +static EC_KEY* CreateTestECKey() { |
| 12 | + bssl::UniquePtr<EC_KEY> ec_key(EC_KEY_new_by_curve_name(NID_X9_62_prime256v1)); |
| 13 | + if (!ec_key || !EC_KEY_generate_key(ec_key.get())) { |
| 14 | + return nullptr; |
| 15 | + } |
| 16 | + return ec_key.release(); |
| 17 | +} |
| 18 | + |
| 19 | +class ECTest : public ::testing::Test { |
| 20 | +protected: |
| 21 | + void SetUp() override { |
| 22 | + ASSERT_GT(createTempFILEpath(pem_key_path), 0u); |
| 23 | + ASSERT_GT(createTempFILEpath(der_key_path), 0u); |
| 24 | + ASSERT_GT(createTempFILEpath(out_path), 0u); |
| 25 | + |
| 26 | + tool_executable_path = getenv("AWSLC_TOOL_PATH"); |
| 27 | + openssl_executable_path = getenv("OPENSSL_TOOL_PATH"); |
| 28 | + |
| 29 | + if (tool_executable_path != nullptr && openssl_executable_path != nullptr) { |
| 30 | + ASSERT_GT(createTempFILEpath(out_path_openssl), 0u); |
| 31 | + |
| 32 | + // Use OpenSSL to generate test keys for better cross-compatibility |
| 33 | + std::string pem_cmd = std::string(openssl_executable_path) + " ecparam -genkey -name prime256v1 -out " + pem_key_path; |
| 34 | + std::string der_cmd = std::string(openssl_executable_path) + " ecparam -genkey -name prime256v1 | " + |
| 35 | + std::string(openssl_executable_path) + " ec -outform DER -out " + der_key_path; |
| 36 | + |
| 37 | + ASSERT_EQ(system(pem_cmd.c_str()), 0) << "Failed to generate PEM key with OpenSSL"; |
| 38 | + ASSERT_EQ(system(der_cmd.c_str()), 0) << "Failed to generate DER key with OpenSSL"; |
| 39 | + } else { |
| 40 | + // Fallback to AWS-LC key generation |
| 41 | + ec_key.reset(CreateTestECKey()); |
| 42 | + ASSERT_TRUE(ec_key); |
| 43 | + |
| 44 | + bssl::UniquePtr<BIO> pem_bio(BIO_new_file(pem_key_path, "wb")); |
| 45 | + ASSERT_TRUE(pem_bio); |
| 46 | + ASSERT_TRUE(PEM_write_bio_ECPrivateKey(pem_bio.get(), ec_key.get(), nullptr, nullptr, 0, nullptr, nullptr)); |
| 47 | + BIO_flush(pem_bio.get()); |
| 48 | + |
| 49 | + bssl::UniquePtr<BIO> der_bio(BIO_new_file(der_key_path, "wb")); |
| 50 | + ASSERT_TRUE(der_bio); |
| 51 | + ASSERT_TRUE(i2d_ECPrivateKey_bio(der_bio.get(), ec_key.get())); |
| 52 | + BIO_flush(der_bio.get()); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + void TearDown() override { |
| 57 | + RemoveFile(pem_key_path); |
| 58 | + RemoveFile(der_key_path); |
| 59 | + RemoveFile(out_path); |
| 60 | + if (tool_executable_path != nullptr && openssl_executable_path != nullptr) { |
| 61 | + RemoveFile(out_path_openssl); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + char pem_key_path[PATH_MAX]; |
| 66 | + char der_key_path[PATH_MAX]; |
| 67 | + char out_path[PATH_MAX]; |
| 68 | + char out_path_openssl[PATH_MAX]; |
| 69 | + const char* tool_executable_path; |
| 70 | + const char* openssl_executable_path; |
| 71 | + bssl::UniquePtr<EC_KEY> ec_key; |
| 72 | +}; |
| 73 | + |
| 74 | +TEST_F(ECTest, ReadPEMOutputPEM) { |
| 75 | + args_list_t args = {"-in", pem_key_path, "-out", out_path}; |
| 76 | + ASSERT_TRUE(ecTool(args)); |
| 77 | + |
| 78 | + bssl::UniquePtr<BIO> out_bio(BIO_new_file(out_path, "rb")); |
| 79 | + ASSERT_TRUE(out_bio); |
| 80 | + bssl::UniquePtr<EC_KEY> parsed_key(PEM_read_bio_ECPrivateKey(out_bio.get(), nullptr, nullptr, nullptr)); |
| 81 | + ASSERT_TRUE(parsed_key); |
| 82 | +} |
| 83 | + |
| 84 | +TEST_F(ECTest, ReadPEMOutputDER) { |
| 85 | + args_list_t args = {"-in", pem_key_path, "-outform", "DER", "-out", out_path}; |
| 86 | + ASSERT_TRUE(ecTool(args)); |
| 87 | + |
| 88 | + bssl::UniquePtr<BIO> out_bio(BIO_new_file(out_path, "rb")); |
| 89 | + ASSERT_TRUE(out_bio); |
| 90 | + bssl::UniquePtr<EC_KEY> parsed_key(d2i_ECPrivateKey_bio(out_bio.get(), nullptr)); |
| 91 | + ASSERT_TRUE(parsed_key); |
| 92 | +} |
| 93 | + |
| 94 | +TEST_F(ECTest, ReadDEROutputPEM) { |
| 95 | + args_list_t args = {"-in", der_key_path, "-inform", "DER", "-out", out_path}; |
| 96 | + ASSERT_TRUE(ecTool(args)); |
| 97 | + |
| 98 | + bssl::UniquePtr<BIO> out_bio(BIO_new_file(out_path, "rb")); |
| 99 | + ASSERT_TRUE(out_bio); |
| 100 | + bssl::UniquePtr<EC_KEY> parsed_key(PEM_read_bio_ECPrivateKey(out_bio.get(), nullptr, nullptr, nullptr)); |
| 101 | + ASSERT_TRUE(parsed_key); |
| 102 | +} |
| 103 | + |
| 104 | +TEST_F(ECTest, ReadDEROutputDER) { |
| 105 | + args_list_t args = {"-in", der_key_path, "-inform", "DER", "-outform", "DER", "-out", out_path}; |
| 106 | + ASSERT_TRUE(ecTool(args)); |
| 107 | + |
| 108 | + bssl::UniquePtr<BIO> out_bio(BIO_new_file(out_path, "rb")); |
| 109 | + ASSERT_TRUE(out_bio); |
| 110 | + bssl::UniquePtr<EC_KEY> parsed_key(d2i_ECPrivateKey_bio(out_bio.get(), nullptr)); |
| 111 | + ASSERT_TRUE(parsed_key); |
| 112 | +} |
| 113 | + |
| 114 | +TEST_F(ECTest, PublicKeyExtractionPEM) { |
| 115 | + args_list_t args = {"-in", pem_key_path, "-pubout", "-out", out_path}; |
| 116 | + ASSERT_TRUE(ecTool(args)); |
| 117 | + |
| 118 | + bssl::UniquePtr<BIO> out_bio(BIO_new_file(out_path, "rb")); |
| 119 | + ASSERT_TRUE(out_bio); |
| 120 | + bssl::UniquePtr<EC_KEY> parsed_key(PEM_read_bio_EC_PUBKEY(out_bio.get(), nullptr, nullptr, nullptr)); |
| 121 | + ASSERT_TRUE(parsed_key); |
| 122 | +} |
| 123 | + |
| 124 | +TEST_F(ECTest, PublicKeyExtractionDER) { |
| 125 | + args_list_t args = {"-in", der_key_path, "-inform", "DER", "-pubout", "-outform", "DER", "-out", out_path}; |
| 126 | + ASSERT_TRUE(ecTool(args)); |
| 127 | + |
| 128 | + bssl::UniquePtr<BIO> out_bio(BIO_new_file(out_path, "rb")); |
| 129 | + ASSERT_TRUE(out_bio); |
| 130 | + bssl::UniquePtr<EC_KEY> parsed_key(d2i_EC_PUBKEY_bio(out_bio.get(), nullptr)); |
| 131 | + ASSERT_TRUE(parsed_key); |
| 132 | +} |
| 133 | + |
| 134 | +TEST_F(ECTest, RoundTripPEMtoDERtoPEM) { |
| 135 | + char temp_der[PATH_MAX]; |
| 136 | + ASSERT_GT(createTempFILEpath(temp_der), 0u); |
| 137 | + |
| 138 | + // Load original key for comparison |
| 139 | + bssl::UniquePtr<BIO> orig_bio(BIO_new_file(pem_key_path, "rb")); |
| 140 | + ASSERT_TRUE(orig_bio); |
| 141 | + bssl::UniquePtr<EC_KEY> orig_key(PEM_read_bio_ECPrivateKey(orig_bio.get(), nullptr, nullptr, nullptr)); |
| 142 | + ASSERT_TRUE(orig_key); |
| 143 | + |
| 144 | + args_list_t args1 = {"-in", pem_key_path, "-outform", "DER", "-out", temp_der}; |
| 145 | + ASSERT_TRUE(ecTool(args1)); |
| 146 | + |
| 147 | + args_list_t args2 = {"-in", temp_der, "-inform", "DER", "-out", out_path}; |
| 148 | + ASSERT_TRUE(ecTool(args2)); |
| 149 | + |
| 150 | + bssl::UniquePtr<BIO> out_bio(BIO_new_file(out_path, "rb")); |
| 151 | + ASSERT_TRUE(out_bio); |
| 152 | + bssl::UniquePtr<EC_KEY> parsed_key(PEM_read_bio_ECPrivateKey(out_bio.get(), nullptr, nullptr, nullptr)); |
| 153 | + ASSERT_TRUE(parsed_key); |
| 154 | + |
| 155 | + // Validate key content matches |
| 156 | + const BIGNUM *orig_priv = EC_KEY_get0_private_key(orig_key.get()); |
| 157 | + const BIGNUM *parsed_priv = EC_KEY_get0_private_key(parsed_key.get()); |
| 158 | + ASSERT_EQ(BN_cmp(orig_priv, parsed_priv), 0); |
| 159 | + |
| 160 | + RemoveFile(temp_der); |
| 161 | +} |
| 162 | + |
| 163 | +TEST_F(ECTest, RoundTripDERtoPEMtoDER) { |
| 164 | + char temp_pem[PATH_MAX]; |
| 165 | + ASSERT_GT(createTempFILEpath(temp_pem), 0u); |
| 166 | + |
| 167 | + // Load original key for comparison |
| 168 | + bssl::UniquePtr<BIO> orig_bio(BIO_new_file(der_key_path, "rb")); |
| 169 | + ASSERT_TRUE(orig_bio); |
| 170 | + bssl::UniquePtr<EC_KEY> orig_key(d2i_ECPrivateKey_bio(orig_bio.get(), nullptr)); |
| 171 | + ASSERT_TRUE(orig_key); |
| 172 | + |
| 173 | + args_list_t args1 = {"-in", der_key_path, "-inform", "DER", "-out", temp_pem}; |
| 174 | + ASSERT_TRUE(ecTool(args1)); |
| 175 | + |
| 176 | + args_list_t args2 = {"-in", temp_pem, "-outform", "DER", "-out", out_path}; |
| 177 | + ASSERT_TRUE(ecTool(args2)); |
| 178 | + |
| 179 | + bssl::UniquePtr<BIO> out_bio(BIO_new_file(out_path, "rb")); |
| 180 | + ASSERT_TRUE(out_bio); |
| 181 | + bssl::UniquePtr<EC_KEY> parsed_key(d2i_ECPrivateKey_bio(out_bio.get(), nullptr)); |
| 182 | + ASSERT_TRUE(parsed_key); |
| 183 | + |
| 184 | + // Validate key content matches |
| 185 | + const BIGNUM *orig_priv = EC_KEY_get0_private_key(orig_key.get()); |
| 186 | + const BIGNUM *parsed_priv = EC_KEY_get0_private_key(parsed_key.get()); |
| 187 | + ASSERT_EQ(BN_cmp(orig_priv, parsed_priv), 0); |
| 188 | + |
| 189 | + RemoveFile(temp_pem); |
| 190 | +} |
| 191 | + |
| 192 | +TEST_F(ECTest, HelpOption) { |
| 193 | + args_list_t args = {"-help"}; |
| 194 | + ASSERT_TRUE(ecTool(args)); |
| 195 | +} |
| 196 | + |
| 197 | +TEST_F(ECTest, InvalidInputFile) { |
| 198 | + args_list_t args = {"-in", "/nonexistent/file.pem", "-out", out_path}; |
| 199 | + ASSERT_FALSE(ecTool(args)); |
| 200 | +} |
| 201 | + |
| 202 | +TEST_F(ECTest, InvalidOutputPath) { |
| 203 | + args_list_t args = {"-in", pem_key_path, "-out", "/nonexistent/dir/output.pem"}; |
| 204 | + ASSERT_FALSE(ecTool(args)); |
| 205 | +} |
| 206 | + |
| 207 | +TEST_F(ECTest, CompareWithOpenSSLPEMOutput) { |
| 208 | + if (tool_executable_path == nullptr || openssl_executable_path == nullptr) { |
| 209 | + GTEST_SKIP() << "Skipping test: AWSLC_TOOL_PATH and/or OPENSSL_TOOL_PATH environment variables are not set"; |
| 210 | + } |
| 211 | + |
| 212 | + std::string tool_cmd = std::string(tool_executable_path) + " ec -in " + pem_key_path + " -out " + out_path; |
| 213 | + std::string openssl_cmd = std::string(openssl_executable_path) + " ec -in " + pem_key_path + " -out " + out_path_openssl; |
| 214 | + |
| 215 | + ASSERT_EQ(system(tool_cmd.c_str()), 0); |
| 216 | + ASSERT_EQ(system(openssl_cmd.c_str()), 0); |
| 217 | + |
| 218 | + bssl::UniquePtr<BIO> tool_bio(BIO_new_file(out_path, "rb")); |
| 219 | + bssl::UniquePtr<BIO> openssl_bio(BIO_new_file(out_path_openssl, "rb")); |
| 220 | + ASSERT_TRUE(tool_bio); |
| 221 | + ASSERT_TRUE(openssl_bio); |
| 222 | + |
| 223 | + bssl::UniquePtr<EC_KEY> tool_key(PEM_read_bio_ECPrivateKey(tool_bio.get(), nullptr, nullptr, nullptr)); |
| 224 | + bssl::UniquePtr<EC_KEY> openssl_key(PEM_read_bio_ECPrivateKey(openssl_bio.get(), nullptr, nullptr, nullptr)); |
| 225 | + ASSERT_TRUE(tool_key); |
| 226 | + ASSERT_TRUE(openssl_key); |
| 227 | +} |
| 228 | + |
| 229 | +TEST_F(ECTest, CompareWithOpenSSLDEROutput) { |
| 230 | + if (tool_executable_path == nullptr || openssl_executable_path == nullptr) { |
| 231 | + GTEST_SKIP() << "Skipping test: AWSLC_TOOL_PATH and/or OPENSSL_TOOL_PATH environment variables are not set"; |
| 232 | + } |
| 233 | + |
| 234 | + std::string tool_cmd = std::string(tool_executable_path) + " ec -in " + pem_key_path + " -outform DER -out " + out_path; |
| 235 | + std::string openssl_cmd = std::string(openssl_executable_path) + " ec -in " + pem_key_path + " -outform DER -out " + out_path_openssl; |
| 236 | + |
| 237 | + ASSERT_EQ(system(tool_cmd.c_str()), 0); |
| 238 | + ASSERT_EQ(system(openssl_cmd.c_str()), 0); |
| 239 | + |
| 240 | + bssl::UniquePtr<BIO> tool_bio(BIO_new_file(out_path, "rb")); |
| 241 | + bssl::UniquePtr<BIO> openssl_bio(BIO_new_file(out_path_openssl, "rb")); |
| 242 | + ASSERT_TRUE(tool_bio); |
| 243 | + ASSERT_TRUE(openssl_bio); |
| 244 | + |
| 245 | + bssl::UniquePtr<EC_KEY> tool_key(d2i_ECPrivateKey_bio(tool_bio.get(), nullptr)); |
| 246 | + bssl::UniquePtr<EC_KEY> openssl_key(d2i_ECPrivateKey_bio(openssl_bio.get(), nullptr)); |
| 247 | + ASSERT_TRUE(tool_key); |
| 248 | + ASSERT_TRUE(openssl_key); |
| 249 | +} |
0 commit comments