@@ -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,7 @@ 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+ SEC_PRINT ( "Calling Sec_OpenSSLPrivSign with %s" , OPENSSL_VERSION_TEXT );
6667 keyHandle = (Sec_KeyHandle * ) RSA_get_app_data (rsa );
6768 if (keyHandle == NULL ) {
6869 SEC_LOG_ERROR ("NULL keyHandle encountered" );
@@ -96,6 +97,7 @@ static int Sec_OpenSSLPubVerify(int type, const unsigned char* m, unsigned int m
9697 return -1 ;
9798 }
9899
100+ SEC_PRINT ("Calling Sec_OpenSSLPubVerify with %s" , OPENSSL_VERSION_TEXT );
99101 keyHandle = (Sec_KeyHandle * ) RSA_get_app_data (rsa );
100102 if (keyHandle == NULL ) {
101103 SEC_LOG_ERROR ("NULL keyHandle encountered" );
@@ -129,6 +131,7 @@ static int Sec_OpenSSLPubEncrypt(int flen, const unsigned char* from, unsigned c
129131 return -1 ;
130132 }
131133
134+ SEC_PRINT ("Calling Sec_OpenSSLPubEncrypt with %s" , OPENSSL_VERSION_TEXT );
132135 keyHandle = (Sec_KeyHandle * ) RSA_get_app_data (rsa );
133136 if (keyHandle == NULL ) {
134137 SEC_LOG_ERROR ("NULL keyHandle encountered" );
@@ -162,6 +165,7 @@ static int Sec_OpenSSLPrivDecrypt(int flen, const unsigned char* from, unsigned
162165 return -1 ;
163166 }
164167
168+ SEC_PRINT ("Calling Sec_OpenSSLPrivDecrypt with %s" , OPENSSL_VERSION_TEXT );
165169 keyHandle = (Sec_KeyHandle * ) RSA_get_app_data (rsa );
166170 if (keyHandle == NULL ) {
167171 SEC_LOG_ERROR ("NULL keyHandle encountered" );
@@ -199,7 +203,8 @@ static RSA_METHOD g_sec_openssl_rsamethod = {
199203#endif
200204
201205static void ENGINE_load_securityapi (void ) {
202- ENGINE * engine = ENGINE_new ();
206+ engine = ENGINE_new ();
207+ SEC_PRINT ("*****ENGINE_load_securityapi***** \n" );
203208 if (engine == NULL ) {
204209 SEC_LOG_ERROR ("ENGINE_new failed" );
205210 return ;
@@ -208,28 +213,34 @@ static void ENGINE_load_securityapi(void) {
208213 if (!ENGINE_set_id (engine , SECAPI_ENGINE_ID )) {
209214 SEC_LOG_ERROR ("ENGINE_set_id failed" );
210215 ENGINE_free (engine );
216+ engine = NULL ;
211217 return ;
212218 }
213219 if (!ENGINE_set_name (engine , "SecurityApi engine" )) {
214220 SEC_LOG_ERROR ("ENGINE_set_name failed" );
215221 ENGINE_free (engine );
222+ engine = NULL ;
216223 return ;
217224 }
218225
219226 if (!ENGINE_init (engine )) {
220227 SEC_LOG_ERROR ("ENGINE_init failed" );
221228 ENGINE_free (engine );
229+ engine = NULL ;
222230 return ;
223231 }
224232
225233#if OPENSSL_VERSION_NUMBER < 0x10100000L
234+ SEC_PRINT ("******Calling ENGINE_set_RSA 1 **** \n" );
226235 if (!ENGINE_set_RSA (engine , & g_sec_openssl_rsamethod )) {
227236#else
228237 if (rsa_method == NULL ) {
238+ SEC_PRINT ("*****Sec_InitOpenSSL creating RSA method****** \n" );
229239 rsa_method = RSA_meth_new ("securityapi RSA method" , RSA_METHOD_FLAG_NO_CHECK | RSA_FLAG_EXT_PKEY );
230240 if (rsa_method == NULL ) {
231241 SEC_LOG_ERROR ("RSA_meth_new failed" );
232242 ENGINE_free (engine );
243+ engine = NULL ;
233244 return ;
234245 }
235246
@@ -238,26 +249,27 @@ static void ENGINE_load_securityapi(void) {
238249 RSA_meth_set_sign (rsa_method , Sec_OpenSSLPrivSign );
239250 RSA_meth_set_verify (rsa_method , Sec_OpenSSLPubVerify );
240251 }
241-
252+
253+ SEC_PRINT ("*****Calling ENGINE_set_RSA 2***** \n" );
242254 if (!ENGINE_set_RSA (engine , rsa_method )) {
243255#endif
244- ENGINE_remove (engine );
245256 ENGINE_free (engine );
257+ engine = NULL ;
246258 return ;
247259 }
248260
249- ENGINE_add (engine );
250- ENGINE_free (engine );
251261 ERR_clear_error ();
252262}
253263
254264void Sec_InitOpenSSL () {
255265 static pthread_mutex_t init_openssl_mutex = PTHREAD_MUTEX_INITIALIZER ;
256266
257267 pthread_mutex_lock (& init_openssl_mutex );
268+ SEC_PRINT ("***** Sec_InitOpenSSL****** \n" );
258269
259270 if (g_sec_openssl_inited != SEC_TRUE ) {
260271#if OPENSSL_VERSION_NUMBER < 0x10100000L
272+ SEC_PRINT ("*****Sec_InitOpenSSL OPenssl < 1.1.0****** \n" );
261273 ERR_load_crypto_strings ();
262274 OpenSSL_add_all_algorithms ();
263275 OpenSSL_add_all_ciphers ();
@@ -276,9 +288,9 @@ void Sec_InitOpenSSL() {
276288
277289 ENGINE_set_default (engine , ENGINE_METHOD_ALL );
278290 ENGINE_free (engine );
291+ engine = NULL ;
279292 }
280293
281- ENGINE_load_securityapi ();
282294
283295 if (atexit (Sec_ShutdownOpenSSL ) != 0 ) {
284296 SEC_LOG_ERROR ("atexit failed" );
@@ -288,6 +300,10 @@ void Sec_InitOpenSSL() {
288300 g_sec_openssl_inited = SEC_TRUE ;
289301 }
290302
303+ if (engine == NULL ) {
304+ ENGINE_load_securityapi ();
305+ }
306+
291307 pthread_mutex_unlock (& init_openssl_mutex );
292308}
293309
@@ -300,21 +316,19 @@ RSA* SecKey_ToEngineRSA(Sec_KeyHandle* keyHandle) {
300316 Sec_RSARawPublicKey pubKey ;
301317 RSA * rsa = NULL ;
302318
303- ENGINE * engine = ENGINE_by_id (SECAPI_ENGINE_ID );
304319 if (engine == NULL ) {
305320 SEC_LOG_ERROR ("ENGINE_by_id failed" );
321+ SEC_LOG_ERROR ("engine not initialized" );
306322 return NULL ;
307323 }
308324
309325 if (SEC_RESULT_SUCCESS != SecKey_ExtractRSAPublicKey (keyHandle , & pubKey )) {
310- ENGINE_free (engine );
311326 SEC_LOG_ERROR ("SecKey_ExtractRSAPublicKey failed" );
312327 return NULL ;
313328 }
314329
315330 rsa = RSA_new_method (engine );
316331 if (rsa == NULL ) {
317- ENGINE_free (engine );
318332 SEC_LOG_ERROR ("RSA_new_method failed" );
319333 return NULL ;
320334 }
@@ -329,28 +343,26 @@ RSA* SecKey_ToEngineRSA(Sec_KeyHandle* keyHandle) {
329343
330344 RSA_set_app_data (rsa , keyHandle );
331345 ENGINE_free (engine );
346+ engine = NULL ;
332347 return rsa ;
333348}
334349
335350RSA * SecKey_ToEngineRSAWithCert (Sec_KeyHandle * keyHandle , Sec_CertificateHandle * certificateHandle ) {
336351 Sec_RSARawPublicKey pubKey ;
337352 RSA * rsa = NULL ;
338353
339- ENGINE * engine = ENGINE_by_id (SECAPI_ENGINE_ID );
340354 if (engine == NULL ) {
341355 SEC_LOG_ERROR ("ENGINE_by_id failed" );
342356 return NULL ;
343357 }
344358
345359 if (SEC_RESULT_SUCCESS != SecCertificate_ExtractRSAPublicKey (certificateHandle , & pubKey )) {
346- ENGINE_free (engine );
347360 SEC_LOG_ERROR ("SecKey_ExtractRSAPublicKey failed" );
348361 return NULL ;
349362 }
350363
351364 rsa = RSA_new_method (engine );
352365 if (rsa == NULL ) {
353- ENGINE_free (engine );
354366 SEC_LOG_ERROR ("RSA_new_method failed" );
355367 return NULL ;
356368 }
@@ -362,9 +374,8 @@ RSA* SecKey_ToEngineRSAWithCert(Sec_KeyHandle* keyHandle, Sec_CertificateHandle*
362374 RSA_set0_key (rsa , BN_bin2bn (pubKey .n , (int ) Sec_BEBytesToUint32 (pubKey .modulus_len_be ), NULL ),
363375 BN_bin2bn (pubKey .e , 4 , NULL ), NULL );
364376#endif
365-
377+ SEC_PRINT ( "Calling SecKey_ToEngineRSA with %s" , OPENSSL_VERSION_TEXT );
366378 RSA_set_app_data (rsa , keyHandle );
367- ENGINE_free (engine );
368379 return rsa ;
369380}
370381
0 commit comments