8383#define parsed_ctx_set (_ptr ) \
8484 context_put_ptr(CONTEXT_GLOBAL, current_processing_ctx, parsed_ctx_idx, _ptr)
8585
86+ #define SS_LOCK \
87+ do { \
88+ if (ss_openssl_lock) \
89+ lock_get(ss_openssl_lock); \
90+ else \
91+ LM_DBG("cannot lock openssl\n"); \
92+ } while (0)
93+ #define SS_UNLOCK \
94+ do { \
95+ if (ss_openssl_lock) \
96+ lock_release(ss_openssl_lock); \
97+ } while (0)
98+
99+ /*
100+ * Module core functions
101+ */
86102static int mod_init (void );
87103static void mod_destroy (void );
88104
@@ -118,6 +134,7 @@ static int tn_authlist_nid;
118134static int parsed_ctx_idx = -1 ;
119135
120136static X509_STORE * store ;
137+ static gen_lock_t * ss_openssl_lock ;
121138
122139static const param_export_t params [] = {
123140 {"auth_date_freshness" , INT_PARAM , & auth_date_freshness },
@@ -231,6 +248,12 @@ static int init_cert_validation(void)
231248 X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL );
232249 }
233250
251+ ss_openssl_lock = lock_alloc ();
252+ if (!ss_openssl_lock || !lock_init (ss_openssl_lock )) {
253+ LM_ERR ("could not allocate openssl lock\n" );
254+ ss_openssl_lock = NULL ;
255+ }
256+
234257 return 0 ;
235258}
236259
@@ -853,12 +876,14 @@ static int load_cert(X509 **cert, STACK_OF(X509) **certchain, str *cert_buf)
853876 LM_ERR ("Unable to create BIO buf\n" );
854877 return -1 ;
855878 }
879+ SS_LOCK ;
856880
857881 /* parse end-entity certificate */
858882 * cert = PEM_read_bio_X509 (cbio , NULL , 0 , NULL );
859883 if (* cert == NULL ) {
860884 LM_ERR ("Failed to parse certificate\n" );
861885 BIO_free (cbio );
886+ SS_UNLOCK ;
862887 return -1 ;
863888 }
864889
@@ -870,6 +895,7 @@ static int load_cert(X509 **cert, STACK_OF(X509) **certchain, str *cert_buf)
870895 X509_free (* cert );
871896 * cert = NULL ;
872897 BIO_free (cbio );
898+ SS_UNLOCK ;
873899 return -1 ;
874900 }
875901
@@ -880,6 +906,7 @@ static int load_cert(X509 **cert, STACK_OF(X509) **certchain, str *cert_buf)
880906 * cert = NULL ;
881907 BIO_free (cbio );
882908 sk_X509_free (stack );
909+ SS_UNLOCK ;
883910 return -1 ;
884911 }
885912
@@ -902,6 +929,7 @@ static int load_cert(X509 **cert, STACK_OF(X509) **certchain, str *cert_buf)
902929 } else {
903930 BIO_free (cbio );
904931 }
932+ SS_UNLOCK ;
905933
906934 return 0 ;
907935}
@@ -916,14 +944,17 @@ static int load_pkey(EVP_PKEY **pkey, str *pkey_buf)
916944 return -1 ;
917945 }
918946
947+ SS_LOCK ;
919948 * pkey = PEM_read_bio_PrivateKey (kbio , NULL , NULL , NULL );
920949 if (* pkey == NULL ) {
921950 LM_ERR ("Failed to load private key from buffer\n" );
922951 BIO_free (kbio );
952+ SS_UNLOCK ;
923953 return -1 ;
924954 }
925955
926956 BIO_free (kbio );
957+ SS_UNLOCK ;
927958
928959 return 0 ;
929960}
0 commit comments