Skip to content

Commit

Permalink
[api] Option to load keys as usual when using OpenSSL engine
Browse files Browse the repository at this point in the history
  • Loading branch information
pajama-coder committed Jan 10, 2025
1 parent 7a44d00 commit 2da5097
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 20 deletions.
28 changes: 8 additions & 20 deletions src/api/crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ auto Crypto::get_openssl_engine() -> ENGINE* {
}

void Crypto::init(const std::string &engine_id) {
OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, nullptr);

if (!engine_id.empty()) {
ENGINE_load_builtin_engines();

Expand Down Expand Up @@ -150,12 +152,8 @@ PublicKey::PublicKey(EVP_PKEY *pkey) : m_pkey(pkey) {

PublicKey::PublicKey(Data *data) {
if (!data->size()) throw std::runtime_error("Data size is zero");
if (s_openssl_engine) {
m_pkey = load_by_engine(data->to_string());
} else {
auto buf = data->to_bytes();
m_pkey = read_pem(&buf[0], buf.size());
}
auto buf = data->to_bytes();
m_pkey = read_pem(&buf[0], buf.size());
}

PublicKey::PublicKey(pjs::Str *data) {
Expand Down Expand Up @@ -203,10 +201,7 @@ auto PublicKey::read_pem(const void *data, size_t size) -> EVP_PKEY* {

auto PublicKey::load_by_engine(const std::string &id) -> EVP_PKEY* {
auto pkey = ENGINE_load_public_key(s_openssl_engine, id.c_str(), nullptr, nullptr);
if (!pkey) {
std::string msg("cannot load public key from OpenSSL engine: ");
throw std::runtime_error(msg + id);
}
if (!pkey) throw_error();
EVP_PKEY_set1_engine(pkey, s_openssl_engine);
return pkey;
}
Expand All @@ -226,12 +221,8 @@ PrivateKey::GenerateOptions::GenerateOptions(pjs::Object *options) {

PrivateKey::PrivateKey(Data *data) {
if (!data->size()) throw std::runtime_error("Data size is zero");
if (s_openssl_engine) {
m_pkey = load_by_engine(data->to_string());
} else {
auto buf = data->to_bytes();
m_pkey = read_pem(&buf[0], buf.size());
}
auto buf = data->to_bytes();
m_pkey = read_pem(&buf[0], buf.size());
}

PrivateKey::PrivateKey(pjs::Str *data) {
Expand Down Expand Up @@ -308,10 +299,7 @@ auto PrivateKey::read_pem(const void *data, size_t size) -> EVP_PKEY* {

auto PrivateKey::load_by_engine(const std::string &id) -> EVP_PKEY* {
auto pkey = ENGINE_load_private_key(s_openssl_engine, id.c_str(), nullptr, nullptr);
if (!pkey) {
std::string msg("cannot load private key from OpenSSL engine: ");
throw std::runtime_error(msg + id);
}
if (!pkey) throw_error();
EVP_PKEY_set1_engine(pkey, s_openssl_engine);
return pkey;
}
Expand Down
1 change: 1 addition & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ static void show_version() {
std::cout << "Tongsuo : " << TONGSUO_VERSION_TEXT << std::endl;
#else
std::cout << "OpenSSL : " << OPENSSL_VERSION_TEXT << std::endl;
std::cout << "OpenSSL Conf : " << CONF_get1_default_config_file() << std::endl;
#endif

#ifdef PIPY_USE_GUI
Expand Down

0 comments on commit 2da5097

Please sign in to comment.