|
| 1 | +#include <NitroModules/ArrayBuffer.hpp> |
| 2 | +#include <NitroModules/Promise.hpp> |
| 3 | +#include <memory> |
| 4 | +#include <openssl/bio.h> |
| 5 | +#include <openssl/buffer.h> |
| 6 | +#include <openssl/ec.h> |
| 7 | +#include <openssl/err.h> |
| 8 | +#include <openssl/evp.h> |
| 9 | +#include <openssl/obj_mac.h> |
| 10 | +#include <openssl/pem.h> |
| 11 | +#include <stdexcept> |
| 12 | +#include <string> |
| 13 | + |
| 14 | +// OpenSSL EC parameter encoding constants |
| 15 | +#ifndef OPENSSL_EC_EXPLICIT_CURVE |
| 16 | +#define OPENSSL_EC_EXPLICIT_CURVE 0x000 |
| 17 | +#endif |
| 18 | +#ifndef OPENSSL_EC_NAMED_CURVE |
| 19 | +#define OPENSSL_EC_NAMED_CURVE 0x001 |
| 20 | +#endif |
| 21 | + |
| 22 | +#include "HybridEcKeyPair.hpp" |
| 23 | +#include "Utils.hpp" |
| 24 | + |
| 25 | +namespace margelo::nitro::crypto { |
| 26 | + |
| 27 | +std::shared_ptr<Promise<void>> HybridEcKeyPair::generateKeyPair() { |
| 28 | + return Promise<void>::async([this]() { this->generateKeyPairSync(); }); |
| 29 | +} |
| 30 | + |
| 31 | +void HybridEcKeyPair::generateKeyPairSync() { |
| 32 | + if (this->curve.empty()) { |
| 33 | + throw std::runtime_error("EC curve not set. Call setCurve() first."); |
| 34 | + } |
| 35 | + |
| 36 | + // Clean up existing key if any |
| 37 | + if (this->pkey != nullptr) { |
| 38 | + EVP_PKEY_free(this->pkey); |
| 39 | + this->pkey = nullptr; |
| 40 | + } |
| 41 | + |
| 42 | + // Get curve NID from curve name |
| 43 | + int curve_nid = GetCurveFromName(this->curve.c_str()); |
| 44 | + if (curve_nid == NID_undef) { |
| 45 | + throw std::runtime_error("Invalid or unsupported curve: " + this->curve); |
| 46 | + } |
| 47 | + |
| 48 | + std::unique_ptr<EVP_PKEY_CTX, decltype(&EVP_PKEY_CTX_free)> key_ctx(nullptr, EVP_PKEY_CTX_free); |
| 49 | + |
| 50 | + // Handle special curves (Ed25519, X25519, etc.) |
| 51 | + switch (curve_nid) { |
| 52 | + case EVP_PKEY_ED25519: |
| 53 | + case EVP_PKEY_ED448: |
| 54 | + case EVP_PKEY_X25519: |
| 55 | + case EVP_PKEY_X448: |
| 56 | + key_ctx.reset(EVP_PKEY_CTX_new_id(curve_nid, nullptr)); |
| 57 | + break; |
| 58 | + default: { |
| 59 | + // Standard EC curves |
| 60 | + std::unique_ptr<EVP_PKEY_CTX, decltype(&EVP_PKEY_CTX_free)> param_ctx(EVP_PKEY_CTX_new_id(EVP_PKEY_EC, nullptr), EVP_PKEY_CTX_free); |
| 61 | + |
| 62 | + if (!param_ctx) { |
| 63 | + throw std::runtime_error("Failed to create parameter context"); |
| 64 | + } |
| 65 | + |
| 66 | + if (EVP_PKEY_paramgen_init(param_ctx.get()) <= 0) { |
| 67 | + throw std::runtime_error("Failed to initialize parameter generation"); |
| 68 | + } |
| 69 | + |
| 70 | + if (EVP_PKEY_CTX_set_ec_paramgen_curve_nid(param_ctx.get(), curve_nid) <= 0) { |
| 71 | + throw std::runtime_error("Failed to set curve NID"); |
| 72 | + } |
| 73 | + |
| 74 | + if (EVP_PKEY_CTX_set_ec_param_enc(param_ctx.get(), OPENSSL_EC_NAMED_CURVE) <= 0) { |
| 75 | + throw std::runtime_error("Failed to set parameter encoding"); |
| 76 | + } |
| 77 | + |
| 78 | + EVP_PKEY* raw_params = nullptr; |
| 79 | + if (EVP_PKEY_paramgen(param_ctx.get(), &raw_params) <= 0) { |
| 80 | + throw std::runtime_error("Failed to generate parameters"); |
| 81 | + } |
| 82 | + |
| 83 | + std::unique_ptr<EVP_PKEY, decltype(&EVP_PKEY_free)> key_params(raw_params, EVP_PKEY_free); |
| 84 | + key_ctx.reset(EVP_PKEY_CTX_new(key_params.get(), nullptr)); |
| 85 | + break; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + if (!key_ctx) { |
| 90 | + throw std::runtime_error("Failed to create key generation context"); |
| 91 | + } |
| 92 | + |
| 93 | + if (EVP_PKEY_keygen_init(key_ctx.get()) <= 0) { |
| 94 | + throw std::runtime_error("Failed to initialize key generation"); |
| 95 | + } |
| 96 | + |
| 97 | + EVP_PKEY* raw_pkey = nullptr; |
| 98 | + if (EVP_PKEY_keygen(key_ctx.get(), &raw_pkey) <= 0) { |
| 99 | + throw std::runtime_error("Failed to generate EC key pair"); |
| 100 | + } |
| 101 | + |
| 102 | + this->pkey = raw_pkey; |
| 103 | +} |
| 104 | + |
| 105 | +KeyObject HybridEcKeyPair::importKey(const std::string& format, const std::shared_ptr<ArrayBuffer>& keyData, const std::string& algorithm, |
| 106 | + bool extractable, const std::vector<std::string>& keyUsages) { |
| 107 | + throw std::runtime_error("HybridEcKeyPair::importKey() is not yet implemented"); |
| 108 | +} |
| 109 | + |
| 110 | +std::shared_ptr<ArrayBuffer> HybridEcKeyPair::exportKey(const KeyObject& key, const std::string& format) { |
| 111 | + throw std::runtime_error("HybridEcKeyPair::exportKey() is not yet implemented"); |
| 112 | +} |
| 113 | + |
| 114 | +std::shared_ptr<ArrayBuffer> HybridEcKeyPair::getPublicKey() { |
| 115 | + this->checkKeyPair(); |
| 116 | + |
| 117 | + // Export as DER format using direct OpenSSL calls |
| 118 | + BIO* bio = BIO_new(BIO_s_mem()); |
| 119 | + if (!bio) { |
| 120 | + throw std::runtime_error("Failed to create BIO for public key export"); |
| 121 | + } |
| 122 | + |
| 123 | + if (i2d_PUBKEY_bio(bio, this->pkey) != 1) { |
| 124 | + BIO_free(bio); |
| 125 | + throw std::runtime_error("Failed to export public key to DER format"); |
| 126 | + } |
| 127 | + |
| 128 | + BUF_MEM* mem; |
| 129 | + BIO_get_mem_ptr(bio, &mem); |
| 130 | + |
| 131 | + // Create a string from the DER data and use ToNativeArrayBuffer utility |
| 132 | + std::string derData(mem->data, mem->length); |
| 133 | + BIO_free(bio); |
| 134 | + |
| 135 | + return ToNativeArrayBuffer(derData); |
| 136 | +} |
| 137 | + |
| 138 | +std::shared_ptr<ArrayBuffer> HybridEcKeyPair::getPrivateKey() { |
| 139 | + this->checkKeyPair(); |
| 140 | + |
| 141 | + // Export as DER format in PKCS8 format using direct OpenSSL calls |
| 142 | + BIO* bio = BIO_new(BIO_s_mem()); |
| 143 | + if (!bio) { |
| 144 | + throw std::runtime_error("Failed to create BIO for private key export"); |
| 145 | + } |
| 146 | + |
| 147 | + if (i2d_PKCS8PrivateKey_bio(bio, this->pkey, nullptr, nullptr, 0, nullptr, nullptr) != 1) { |
| 148 | + BIO_free(bio); |
| 149 | + throw std::runtime_error("Failed to export private key to DER PKCS8 format"); |
| 150 | + } |
| 151 | + |
| 152 | + BUF_MEM* mem; |
| 153 | + BIO_get_mem_ptr(bio, &mem); |
| 154 | + |
| 155 | + // Create a string from the DER data and use ToNativeArrayBuffer utility |
| 156 | + std::string derData(mem->data, mem->length); |
| 157 | + BIO_free(bio); |
| 158 | + |
| 159 | + return ToNativeArrayBuffer(derData); |
| 160 | +} |
| 161 | + |
| 162 | +void HybridEcKeyPair::setCurve(const std::string& curve) { |
| 163 | + this->curve = curve; |
| 164 | +} |
| 165 | + |
| 166 | +int HybridEcKeyPair::GetCurveFromName(const char* name) { |
| 167 | + // Handle NIST curve name mappings first |
| 168 | + std::string curve_name(name); |
| 169 | + if (curve_name == "P-256") { |
| 170 | + return NID_X9_62_prime256v1; |
| 171 | + } else if (curve_name == "P-384") { |
| 172 | + return NID_secp384r1; |
| 173 | + } else if (curve_name == "P-521") { |
| 174 | + return NID_secp521r1; |
| 175 | + } else if (curve_name == "secp256k1") { |
| 176 | + return NID_secp256k1; |
| 177 | + } |
| 178 | + |
| 179 | + // Try standard OpenSSL name resolution |
| 180 | + int nid = OBJ_txt2nid(name); |
| 181 | + if (nid == NID_undef) { |
| 182 | + // Try short names |
| 183 | + nid = OBJ_sn2nid(name); |
| 184 | + } |
| 185 | + if (nid == NID_undef) { |
| 186 | + // Try long names |
| 187 | + nid = OBJ_ln2nid(name); |
| 188 | + } |
| 189 | + return nid; |
| 190 | +} |
| 191 | + |
| 192 | +void HybridEcKeyPair::checkKeyPair() { |
| 193 | + if (this->pkey == nullptr) { |
| 194 | + throw std::runtime_error("EC KeyPair not initialized"); |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +} // namespace margelo::nitro::crypto |
0 commit comments