Skip to content

Commit 9cf9262

Browse files
author
ericeberry
authored
Issue 28: Fix integration issue with OpenSSL 1.1.1l (#29)
1 parent 9cc8913 commit 9cf9262

File tree

2 files changed

+32
-29
lines changed

2 files changed

+32
-29
lines changed

include/sec_security_svp.h

+8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
#include "sec_security.h"
2525
#include <pthread.h>
2626

27+
#ifdef __cplusplus
28+
extern "C" {
29+
#endif
30+
2731
typedef struct svp_processor_buffer_struct {
2832
Sec_ProcessorHandle* processorHandle;
2933
sa_svp_buffer svp_buffer;
@@ -49,4 +53,8 @@ Sec_Result SecOpaqueBuffer_Create(Sec_OpaqueBufferHandle** opaqueBufferHandle, v
4953
sa_svp_buffer get_svp_buffer(Sec_ProcessorHandle* processorHandle, Sec_OpaqueBufferHandle* opaqueBufferHandle);
5054
void release_svp_buffer(Sec_ProcessorHandle* processorHandle, Sec_OpaqueBufferHandle* opaqueBufferHandle);
5155

56+
#ifdef __cplusplus
57+
}
58+
#endif
59+
5260
#endif // SEC_SECURITY_SVP_H

src/sec_adapter_engine.c

+24-29
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2020-2022 Comcast Cable Communications Management, LLC
2+
* Copyright 2020-2023 Comcast Cable Communications Management, LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* 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;
2828
static RSA_METHOD* rsa_method = NULL;
2929
#endif
3030

31+
static ENGINE* engine = NULL;
32+
3133
static void Sec_ShutdownOpenSSL() {
3234
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
3335
if (rsa_method != NULL) {
@@ -36,11 +38,10 @@ static void Sec_ShutdownOpenSSL() {
3638
}
3739
#endif
3840

39-
ENGINE* engine = ENGINE_by_id(ENGINE_ID);
4041
if (engine != NULL) {
41-
ENGINE_remove(engine);
4242
ENGINE_finish(engine);
4343
ENGINE_free(engine);
44+
engine = NULL;
4445
}
4546
}
4647

@@ -198,38 +199,48 @@ static RSA_METHOD g_sec_openssl_rsamethod = {
198199
#endif
199200

200201
static void ENGINE_load_securityapi(void) {
201-
ENGINE* engine = ENGINE_new();
202+
engine = ENGINE_new();
202203
if (engine == NULL) {
203204
SEC_LOG_ERROR("ENGINE_new failed");
204205
return;
205206
}
206207

207208
if (!ENGINE_set_id(engine, ENGINE_ID)) {
208209
ENGINE_free(engine);
210+
engine = NULL;
209211
return;
210212
}
211213
if (!ENGINE_set_name(engine, "SecurityApi engine")) {
212214
ENGINE_free(engine);
215+
engine = NULL;
213216
return;
214217
}
215218

216219
if (!ENGINE_init(engine)) {
217220
ENGINE_free(engine);
221+
engine = NULL;
218222
return;
219223
}
220224

221225
#if OPENSSL_VERSION_NUMBER < 0x10100000L
222226
if (!ENGINE_set_RSA(engine, &g_sec_openssl_rsamethod)) {
223227
#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+
224236
if (!ENGINE_set_RSA(engine, rsa_method)) {
225237
#endif
226-
ENGINE_remove(engine);
238+
ENGINE_finish(engine);
227239
ENGINE_free(engine);
240+
engine = NULL;
228241
return;
229242
}
230243

231-
ENGINE_add(engine);
232-
ENGINE_free(engine);
233244
ERR_clear_error();
234245
}
235246

@@ -239,16 +250,7 @@ void Sec_InitOpenSSL() {
239250
pthread_mutex_lock(&init_openssl_mutex);
240251

241252
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
252254
ERR_load_crypto_strings();
253255
OpenSSL_add_all_algorithms();
254256
OpenSSL_add_all_ciphers();
@@ -257,7 +259,6 @@ void Sec_InitOpenSSL() {
257259

258260
ENGINE_load_builtin_engines();
259261
ENGINE_register_all_complete();
260-
ENGINE_load_securityapi();
261262

262263
if (atexit(Sec_ShutdownOpenSSL) != 0) {
263264
SEC_LOG_ERROR("atexit failed");
@@ -267,6 +268,10 @@ void Sec_InitOpenSSL() {
267268
g_sec_openssl_inited = SEC_TRUE;
268269
}
269270

271+
if (engine == NULL) {
272+
ENGINE_load_securityapi();
273+
}
274+
270275
pthread_mutex_unlock(&init_openssl_mutex);
271276
}
272277

@@ -278,23 +283,19 @@ void Sec_PrintOpenSSLVersion() {
278283
RSA* SecKey_ToEngineRSA(Sec_KeyHandle* keyHandle) {
279284
Sec_RSARawPublicKey pubKey;
280285
RSA* rsa = NULL;
281-
ENGINE* engine = NULL;
282286

283-
engine = ENGINE_by_id(ENGINE_ID);
284287
if (engine == NULL) {
285-
SEC_LOG_ERROR("ENGINE_by_id failed");
288+
SEC_LOG_ERROR("engine not initialized");
286289
return NULL;
287290
}
288291

289292
if (SEC_RESULT_SUCCESS != SecKey_ExtractRSAPublicKey(keyHandle, &pubKey)) {
290-
ENGINE_free(engine);
291293
SEC_LOG_ERROR("SecKey_ExtractRSAPublicKey failed");
292294
return NULL;
293295
}
294296

295297
rsa = RSA_new_method(engine);
296298
if (rsa == NULL) {
297-
ENGINE_free(engine);
298299
SEC_LOG_ERROR("RSA_new_method failed");
299300
return NULL;
300301
}
@@ -308,30 +309,25 @@ RSA* SecKey_ToEngineRSA(Sec_KeyHandle* keyHandle) {
308309
#endif
309310

310311
RSA_set_app_data(rsa, keyHandle);
311-
ENGINE_free(engine);
312312
return rsa;
313313
}
314314

315315
RSA* SecKey_ToEngineRSAWithCert(Sec_KeyHandle* keyHandle, Sec_CertificateHandle* certificateHandle) {
316316
Sec_RSARawPublicKey pubKey;
317317
RSA* rsa = NULL;
318-
ENGINE* engine = NULL;
319318

320-
engine = ENGINE_by_id(ENGINE_ID);
321319
if (engine == NULL) {
322320
SEC_LOG_ERROR("ENGINE_by_id failed");
323321
return NULL;
324322
}
325323

326324
if (SEC_RESULT_SUCCESS != SecCertificate_ExtractRSAPublicKey(certificateHandle, &pubKey)) {
327-
ENGINE_free(engine);
328325
SEC_LOG_ERROR("SecKey_ExtractRSAPublicKey failed");
329326
return NULL;
330327
}
331328

332329
rsa = RSA_new_method(engine);
333330
if (rsa == NULL) {
334-
ENGINE_free(engine);
335331
SEC_LOG_ERROR("RSA_new_method failed");
336332
return NULL;
337333
}
@@ -345,7 +341,6 @@ RSA* SecKey_ToEngineRSAWithCert(Sec_KeyHandle* keyHandle, Sec_CertificateHandle*
345341
#endif
346342

347343
RSA_set_app_data(rsa, keyHandle);
348-
ENGINE_free(engine);
349344
return rsa;
350345
}
351346

0 commit comments

Comments
 (0)