Skip to content

Commit 2da5097

Browse files
committed
[api] Option to load keys as usual when using OpenSSL engine
1 parent 7a44d00 commit 2da5097

File tree

2 files changed

+9
-20
lines changed

2 files changed

+9
-20
lines changed

src/api/crypto.cpp

+8-20
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ auto Crypto::get_openssl_engine() -> ENGINE* {
7878
}
7979

8080
void Crypto::init(const std::string &engine_id) {
81+
OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, nullptr);
82+
8183
if (!engine_id.empty()) {
8284
ENGINE_load_builtin_engines();
8385

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

151153
PublicKey::PublicKey(Data *data) {
152154
if (!data->size()) throw std::runtime_error("Data size is zero");
153-
if (s_openssl_engine) {
154-
m_pkey = load_by_engine(data->to_string());
155-
} else {
156-
auto buf = data->to_bytes();
157-
m_pkey = read_pem(&buf[0], buf.size());
158-
}
155+
auto buf = data->to_bytes();
156+
m_pkey = read_pem(&buf[0], buf.size());
159157
}
160158

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

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

227222
PrivateKey::PrivateKey(Data *data) {
228223
if (!data->size()) throw std::runtime_error("Data size is zero");
229-
if (s_openssl_engine) {
230-
m_pkey = load_by_engine(data->to_string());
231-
} else {
232-
auto buf = data->to_bytes();
233-
m_pkey = read_pem(&buf[0], buf.size());
234-
}
224+
auto buf = data->to_bytes();
225+
m_pkey = read_pem(&buf[0], buf.size());
235226
}
236227

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

309300
auto PrivateKey::load_by_engine(const std::string &id) -> EVP_PKEY* {
310301
auto pkey = ENGINE_load_private_key(s_openssl_engine, id.c_str(), nullptr, nullptr);
311-
if (!pkey) {
312-
std::string msg("cannot load private key from OpenSSL engine: ");
313-
throw std::runtime_error(msg + id);
314-
}
302+
if (!pkey) throw_error();
315303
EVP_PKEY_set1_engine(pkey, s_openssl_engine);
316304
return pkey;
317305
}

src/main.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ static void show_version() {
7979
std::cout << "Tongsuo : " << TONGSUO_VERSION_TEXT << std::endl;
8080
#else
8181
std::cout << "OpenSSL : " << OPENSSL_VERSION_TEXT << std::endl;
82+
std::cout << "OpenSSL Conf : " << CONF_get1_default_config_file() << std::endl;
8283
#endif
8384

8485
#ifdef PIPY_USE_GUI

0 commit comments

Comments
 (0)