@@ -78,6 +78,8 @@ auto Crypto::get_openssl_engine() -> ENGINE* {
78
78
}
79
79
80
80
void Crypto::init (const std::string &engine_id) {
81
+ OPENSSL_init_crypto (OPENSSL_INIT_LOAD_CONFIG, nullptr );
82
+
81
83
if (!engine_id.empty ()) {
82
84
ENGINE_load_builtin_engines ();
83
85
@@ -150,12 +152,8 @@ PublicKey::PublicKey(EVP_PKEY *pkey) : m_pkey(pkey) {
150
152
151
153
PublicKey::PublicKey (Data *data) {
152
154
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 ());
159
157
}
160
158
161
159
PublicKey::PublicKey (pjs::Str *data) {
@@ -203,10 +201,7 @@ auto PublicKey::read_pem(const void *data, size_t size) -> EVP_PKEY* {
203
201
204
202
auto PublicKey::load_by_engine (const std::string &id) -> EVP_PKEY* {
205
203
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 ();
210
205
EVP_PKEY_set1_engine (pkey, s_openssl_engine);
211
206
return pkey;
212
207
}
@@ -226,12 +221,8 @@ PrivateKey::GenerateOptions::GenerateOptions(pjs::Object *options) {
226
221
227
222
PrivateKey::PrivateKey (Data *data) {
228
223
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 ());
235
226
}
236
227
237
228
PrivateKey::PrivateKey (pjs::Str *data) {
@@ -308,10 +299,7 @@ auto PrivateKey::read_pem(const void *data, size_t size) -> EVP_PKEY* {
308
299
309
300
auto PrivateKey::load_by_engine (const std::string &id) -> EVP_PKEY* {
310
301
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 ();
315
303
EVP_PKEY_set1_engine (pkey, s_openssl_engine);
316
304
return pkey;
317
305
}
0 commit comments