1
1
/**
2
- * Copyright 2020-2022 Comcast Cable Communications Management, LLC
2
+ * Copyright 2020-2023 Comcast Cable Communications Management, LLC
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -28,6 +28,8 @@ static SEC_BOOL g_sec_openssl_inited = SEC_FALSE;
28
28
static RSA_METHOD * rsa_method = NULL ;
29
29
#endif
30
30
31
+ static ENGINE * engine = NULL ;
32
+
31
33
static void Sec_ShutdownOpenSSL () {
32
34
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
33
35
if (rsa_method != NULL ) {
@@ -36,11 +38,10 @@ static void Sec_ShutdownOpenSSL() {
36
38
}
37
39
#endif
38
40
39
- ENGINE * engine = ENGINE_by_id (ENGINE_ID );
40
41
if (engine != NULL ) {
41
- ENGINE_remove (engine );
42
42
ENGINE_finish (engine );
43
43
ENGINE_free (engine );
44
+ engine = NULL ;
44
45
}
45
46
}
46
47
@@ -198,38 +199,48 @@ static RSA_METHOD g_sec_openssl_rsamethod = {
198
199
#endif
199
200
200
201
static void ENGINE_load_securityapi (void ) {
201
- ENGINE * engine = ENGINE_new ();
202
+ engine = ENGINE_new ();
202
203
if (engine == NULL ) {
203
204
SEC_LOG_ERROR ("ENGINE_new failed" );
204
205
return ;
205
206
}
206
207
207
208
if (!ENGINE_set_id (engine , ENGINE_ID )) {
208
209
ENGINE_free (engine );
210
+ engine = NULL ;
209
211
return ;
210
212
}
211
213
if (!ENGINE_set_name (engine , "SecurityApi engine" )) {
212
214
ENGINE_free (engine );
215
+ engine = NULL ;
213
216
return ;
214
217
}
215
218
216
219
if (!ENGINE_init (engine )) {
217
220
ENGINE_free (engine );
221
+ engine = NULL ;
218
222
return ;
219
223
}
220
224
221
225
#if OPENSSL_VERSION_NUMBER < 0x10100000L
222
226
if (!ENGINE_set_RSA (engine , & g_sec_openssl_rsamethod )) {
223
227
#else
228
+ if (rsa_method == NULL ) {
229
+ rsa_method = RSA_meth_new ("securityapi RSA method" , RSA_METHOD_FLAG_NO_CHECK | RSA_FLAG_EXT_PKEY );
230
+ RSA_meth_set_pub_enc (rsa_method , Sec_OpenSSLPubEncrypt );
231
+ RSA_meth_set_priv_dec (rsa_method , Sec_OpenSSLPrivDecrypt );
232
+ RSA_meth_set_sign (rsa_method , Sec_OpenSSLPrivSign );
233
+ RSA_meth_set_verify (rsa_method , Sec_OpenSSLPubVerify );
234
+ }
235
+
224
236
if (!ENGINE_set_RSA (engine , rsa_method )) {
225
237
#endif
226
- ENGINE_remove (engine );
238
+ ENGINE_finish (engine );
227
239
ENGINE_free (engine );
240
+ engine = NULL ;
228
241
return ;
229
242
}
230
243
231
- ENGINE_add (engine );
232
- ENGINE_free (engine );
233
244
ERR_clear_error ();
234
245
}
235
246
@@ -239,16 +250,7 @@ void Sec_InitOpenSSL() {
239
250
pthread_mutex_lock (& init_openssl_mutex );
240
251
241
252
if (g_sec_openssl_inited != SEC_TRUE ) {
242
- #if OPENSSL_VERSION_NUMBER >= 0x10100000L
243
- if (rsa_method == NULL ) {
244
- rsa_method = RSA_meth_new ("securityapi RSA method" , RSA_METHOD_FLAG_NO_CHECK | RSA_FLAG_EXT_PKEY );
245
- RSA_meth_set_pub_enc (rsa_method , Sec_OpenSSLPubEncrypt );
246
- RSA_meth_set_priv_dec (rsa_method , Sec_OpenSSLPrivDecrypt );
247
- RSA_meth_set_sign (rsa_method , Sec_OpenSSLPrivSign );
248
- RSA_meth_set_verify (rsa_method , Sec_OpenSSLPubVerify );
249
- }
250
-
251
- #else
253
+ #if OPENSSL_VERSION_NUMBER < 0x10100000L
252
254
ERR_load_crypto_strings ();
253
255
OpenSSL_add_all_algorithms ();
254
256
OpenSSL_add_all_ciphers ();
@@ -257,7 +259,6 @@ void Sec_InitOpenSSL() {
257
259
258
260
ENGINE_load_builtin_engines ();
259
261
ENGINE_register_all_complete ();
260
- ENGINE_load_securityapi ();
261
262
262
263
if (atexit (Sec_ShutdownOpenSSL ) != 0 ) {
263
264
SEC_LOG_ERROR ("atexit failed" );
@@ -267,6 +268,10 @@ void Sec_InitOpenSSL() {
267
268
g_sec_openssl_inited = SEC_TRUE ;
268
269
}
269
270
271
+ if (engine == NULL ) {
272
+ ENGINE_load_securityapi ();
273
+ }
274
+
270
275
pthread_mutex_unlock (& init_openssl_mutex );
271
276
}
272
277
@@ -278,23 +283,19 @@ void Sec_PrintOpenSSLVersion() {
278
283
RSA * SecKey_ToEngineRSA (Sec_KeyHandle * keyHandle ) {
279
284
Sec_RSARawPublicKey pubKey ;
280
285
RSA * rsa = NULL ;
281
- ENGINE * engine = NULL ;
282
286
283
- engine = ENGINE_by_id (ENGINE_ID );
284
287
if (engine == NULL ) {
285
- SEC_LOG_ERROR ("ENGINE_by_id failed " );
288
+ SEC_LOG_ERROR ("engine not initialized " );
286
289
return NULL ;
287
290
}
288
291
289
292
if (SEC_RESULT_SUCCESS != SecKey_ExtractRSAPublicKey (keyHandle , & pubKey )) {
290
- ENGINE_free (engine );
291
293
SEC_LOG_ERROR ("SecKey_ExtractRSAPublicKey failed" );
292
294
return NULL ;
293
295
}
294
296
295
297
rsa = RSA_new_method (engine );
296
298
if (rsa == NULL ) {
297
- ENGINE_free (engine );
298
299
SEC_LOG_ERROR ("RSA_new_method failed" );
299
300
return NULL ;
300
301
}
@@ -308,30 +309,25 @@ RSA* SecKey_ToEngineRSA(Sec_KeyHandle* keyHandle) {
308
309
#endif
309
310
310
311
RSA_set_app_data (rsa , keyHandle );
311
- ENGINE_free (engine );
312
312
return rsa ;
313
313
}
314
314
315
315
RSA * SecKey_ToEngineRSAWithCert (Sec_KeyHandle * keyHandle , Sec_CertificateHandle * certificateHandle ) {
316
316
Sec_RSARawPublicKey pubKey ;
317
317
RSA * rsa = NULL ;
318
- ENGINE * engine = NULL ;
319
318
320
- engine = ENGINE_by_id (ENGINE_ID );
321
319
if (engine == NULL ) {
322
320
SEC_LOG_ERROR ("ENGINE_by_id failed" );
323
321
return NULL ;
324
322
}
325
323
326
324
if (SEC_RESULT_SUCCESS != SecCertificate_ExtractRSAPublicKey (certificateHandle , & pubKey )) {
327
- ENGINE_free (engine );
328
325
SEC_LOG_ERROR ("SecKey_ExtractRSAPublicKey failed" );
329
326
return NULL ;
330
327
}
331
328
332
329
rsa = RSA_new_method (engine );
333
330
if (rsa == NULL ) {
334
- ENGINE_free (engine );
335
331
SEC_LOG_ERROR ("RSA_new_method failed" );
336
332
return NULL ;
337
333
}
@@ -345,7 +341,6 @@ RSA* SecKey_ToEngineRSAWithCert(Sec_KeyHandle* keyHandle, Sec_CertificateHandle*
345
341
#endif
346
342
347
343
RSA_set_app_data (rsa , keyHandle );
348
- ENGINE_free (engine );
349
344
return rsa ;
350
345
}
351
346
0 commit comments