@@ -29,6 +29,8 @@ static SEC_BOOL g_sec_openssl_inited = SEC_FALSE;
2929static RSA_METHOD * rsa_method = NULL ;
3030#endif
3131
32+ static ENGINE * engine = NULL ;
33+
3234static void Sec_ShutdownOpenSSL () {
3335#if OPENSSL_VERSION_NUMBER >= 0x10100000L
3436 if (rsa_method != NULL ) {
@@ -37,11 +39,10 @@ static void Sec_ShutdownOpenSSL() {
3739 }
3840#endif
3941
40- ENGINE * engine = ENGINE_by_id (SECAPI_ENGINE_ID );
4142 if (engine != NULL ) {
42- ENGINE_remove (engine );
4343 ENGINE_finish (engine );
4444 ENGINE_free (engine );
45+ engine = NULL ;
4546 }
4647}
4748
@@ -62,7 +63,8 @@ static int Sec_OpenSSLPrivSign(int type, const unsigned char* m, unsigned int m_
6263 SEC_LOG_ERROR ("Unknown type %d" , type );
6364 return -1 ;
6465 }
65-
66+
67+ SEC_PRINT ("Calling Sec_OpenSSLPrivSign with %s" , OPENSSL_VERSION_TEXT );
6668 keyHandle = (Sec_KeyHandle * ) RSA_get_app_data (rsa );
6769 if (keyHandle == NULL ) {
6870 SEC_LOG_ERROR ("NULL keyHandle encountered" );
@@ -96,6 +98,7 @@ static int Sec_OpenSSLPubVerify(int type, const unsigned char* m, unsigned int m
9698 return -1 ;
9799 }
98100
101+ SEC_PRINT ("Calling Sec_OpenSSLPubVerify with %s" , OPENSSL_VERSION_TEXT );
99102 keyHandle = (Sec_KeyHandle * ) RSA_get_app_data (rsa );
100103 if (keyHandle == NULL ) {
101104 SEC_LOG_ERROR ("NULL keyHandle encountered" );
@@ -129,6 +132,7 @@ static int Sec_OpenSSLPubEncrypt(int flen, const unsigned char* from, unsigned c
129132 return -1 ;
130133 }
131134
135+ SEC_PRINT ("Calling Sec_OpenSSLPubEncrypt with %s" , OPENSSL_VERSION_TEXT );
132136 keyHandle = (Sec_KeyHandle * ) RSA_get_app_data (rsa );
133137 if (keyHandle == NULL ) {
134138 SEC_LOG_ERROR ("NULL keyHandle encountered" );
@@ -162,6 +166,7 @@ static int Sec_OpenSSLPrivDecrypt(int flen, const unsigned char* from, unsigned
162166 return -1 ;
163167 }
164168
169+ SEC_PRINT ("Calling Sec_OpenSSLPrivDecrypt with %s" , OPENSSL_VERSION_TEXT );
165170 keyHandle = (Sec_KeyHandle * ) RSA_get_app_data (rsa );
166171 if (keyHandle == NULL ) {
167172 SEC_LOG_ERROR ("NULL keyHandle encountered" );
@@ -199,7 +204,8 @@ static RSA_METHOD g_sec_openssl_rsamethod = {
199204#endif
200205
201206static void ENGINE_load_securityapi (void ) {
202- ENGINE * engine = ENGINE_new ();
207+ engine = ENGINE_new ();
208+ SEC_PRINT ("*****ENGINE_load_securityapi***** \n" );
203209 if (engine == NULL ) {
204210 SEC_LOG_ERROR ("ENGINE_new failed" );
205211 return ;
@@ -208,28 +214,34 @@ static void ENGINE_load_securityapi(void) {
208214 if (!ENGINE_set_id (engine , SECAPI_ENGINE_ID )) {
209215 SEC_LOG_ERROR ("ENGINE_set_id failed" );
210216 ENGINE_free (engine );
217+ engine = NULL ;
211218 return ;
212219 }
213220 if (!ENGINE_set_name (engine , "SecurityApi engine" )) {
214221 SEC_LOG_ERROR ("ENGINE_set_name failed" );
215222 ENGINE_free (engine );
223+ engine = NULL ;
216224 return ;
217225 }
218226
219227 if (!ENGINE_init (engine )) {
220228 SEC_LOG_ERROR ("ENGINE_init failed" );
221229 ENGINE_free (engine );
230+ engine = NULL ;
222231 return ;
223232 }
224233
225234#if OPENSSL_VERSION_NUMBER < 0x10100000L
235+ SEC_PRINT ("******Calling ENGINE_set_RSA 1 **** \n" );
226236 if (!ENGINE_set_RSA (engine , & g_sec_openssl_rsamethod )) {
227237#else
228238 if (rsa_method == NULL ) {
239+ SEC_PRINT ("*****Sec_InitOpenSSL creating RSA method****** \n" );
229240 rsa_method = RSA_meth_new ("securityapi RSA method" , RSA_METHOD_FLAG_NO_CHECK | RSA_FLAG_EXT_PKEY );
230241 if (rsa_method == NULL ) {
231242 SEC_LOG_ERROR ("RSA_meth_new failed" );
232243 ENGINE_free (engine );
244+ engine = NULL ;
233245 return ;
234246 }
235247
@@ -238,26 +250,27 @@ static void ENGINE_load_securityapi(void) {
238250 RSA_meth_set_sign (rsa_method , Sec_OpenSSLPrivSign );
239251 RSA_meth_set_verify (rsa_method , Sec_OpenSSLPubVerify );
240252 }
241-
253+
254+ SEC_PRINT ("*****Calling ENGINE_set_RSA 2***** \n" );
242255 if (!ENGINE_set_RSA (engine , rsa_method )) {
243256#endif
244- ENGINE_remove (engine );
245257 ENGINE_free (engine );
258+ engine = NULL ;
246259 return ;
247260 }
248261
249- ENGINE_add (engine );
250- ENGINE_free (engine );
251262 ERR_clear_error ();
252263}
253264
254265void Sec_InitOpenSSL () {
255266 static pthread_mutex_t init_openssl_mutex = PTHREAD_MUTEX_INITIALIZER ;
256267
257268 pthread_mutex_lock (& init_openssl_mutex );
269+ SEC_PRINT ("***** Sec_InitOpenSSL****** \n" );
258270
259271 if (g_sec_openssl_inited != SEC_TRUE ) {
260272#if OPENSSL_VERSION_NUMBER < 0x10100000L
273+ SEC_PRINT ("*****Sec_InitOpenSSL OPenssl < 1.1.0****** \n" );
261274 ERR_load_crypto_strings ();
262275 OpenSSL_add_all_algorithms ();
263276 OpenSSL_add_all_ciphers ();
@@ -276,9 +289,9 @@ void Sec_InitOpenSSL() {
276289
277290 ENGINE_set_default (engine , ENGINE_METHOD_ALL );
278291 ENGINE_free (engine );
292+ engine = NULL ;
279293 }
280294
281- ENGINE_load_securityapi ();
282295
283296 if (atexit (Sec_ShutdownOpenSSL ) != 0 ) {
284297 SEC_LOG_ERROR ("atexit failed" );
@@ -288,6 +301,10 @@ void Sec_InitOpenSSL() {
288301 g_sec_openssl_inited = SEC_TRUE ;
289302 }
290303
304+ if (engine == NULL ) {
305+ ENGINE_load_securityapi ();
306+ }
307+
291308 pthread_mutex_unlock (& init_openssl_mutex );
292309}
293310
@@ -300,21 +317,19 @@ RSA* SecKey_ToEngineRSA(Sec_KeyHandle* keyHandle) {
300317 Sec_RSARawPublicKey pubKey ;
301318 RSA * rsa = NULL ;
302319
303- ENGINE * engine = ENGINE_by_id (SECAPI_ENGINE_ID );
304320 if (engine == NULL ) {
305321 SEC_LOG_ERROR ("ENGINE_by_id failed" );
322+ SEC_LOG_ERROR ("engine not initialized" );
306323 return NULL ;
307324 }
308325
309326 if (SEC_RESULT_SUCCESS != SecKey_ExtractRSAPublicKey (keyHandle , & pubKey )) {
310- ENGINE_free (engine );
311327 SEC_LOG_ERROR ("SecKey_ExtractRSAPublicKey failed" );
312328 return NULL ;
313329 }
314330
315331 rsa = RSA_new_method (engine );
316332 if (rsa == NULL ) {
317- ENGINE_free (engine );
318333 SEC_LOG_ERROR ("RSA_new_method failed" );
319334 return NULL ;
320335 }
@@ -329,28 +344,26 @@ RSA* SecKey_ToEngineRSA(Sec_KeyHandle* keyHandle) {
329344
330345 RSA_set_app_data (rsa , keyHandle );
331346 ENGINE_free (engine );
347+ engine = NULL ;
332348 return rsa ;
333349}
334350
335351RSA * SecKey_ToEngineRSAWithCert (Sec_KeyHandle * keyHandle , Sec_CertificateHandle * certificateHandle ) {
336352 Sec_RSARawPublicKey pubKey ;
337353 RSA * rsa = NULL ;
338354
339- ENGINE * engine = ENGINE_by_id (SECAPI_ENGINE_ID );
340355 if (engine == NULL ) {
341356 SEC_LOG_ERROR ("ENGINE_by_id failed" );
342357 return NULL ;
343358 }
344359
345360 if (SEC_RESULT_SUCCESS != SecCertificate_ExtractRSAPublicKey (certificateHandle , & pubKey )) {
346- ENGINE_free (engine );
347361 SEC_LOG_ERROR ("SecKey_ExtractRSAPublicKey failed" );
348362 return NULL ;
349363 }
350364
351365 rsa = RSA_new_method (engine );
352366 if (rsa == NULL ) {
353- ENGINE_free (engine );
354367 SEC_LOG_ERROR ("RSA_new_method failed" );
355368 return NULL ;
356369 }
@@ -362,9 +375,8 @@ RSA* SecKey_ToEngineRSAWithCert(Sec_KeyHandle* keyHandle, Sec_CertificateHandle*
362375 RSA_set0_key (rsa , BN_bin2bn (pubKey .n , (int ) Sec_BEBytesToUint32 (pubKey .modulus_len_be ), NULL ),
363376 BN_bin2bn (pubKey .e , 4 , NULL ), NULL );
364377#endif
365-
378+ SEC_PRINT ( "Calling SecKey_ToEngineRSA with %s" , OPENSSL_VERSION_TEXT );
366379 RSA_set_app_data (rsa , keyHandle );
367- ENGINE_free (engine );
368380 return rsa ;
369381}
370382
0 commit comments