@@ -139,20 +139,26 @@ static mi_response_t *tls_trace_mi_1(const mi_params_t *params,
139
139
140
140
trace_dest t_dst ;
141
141
142
+ #ifndef NO_SSL_GLOBAL_LOCK
143
+ gen_lock_t * ssl_global_lock ;
144
+ #else
145
+ #define ssl_global_lock NULL
146
+ #endif
147
+
142
148
static int w_tls_blocking_write (struct tcp_connection * c , int fd , const char * buf ,
143
149
size_t len )
144
150
{
145
151
int ret ;
146
152
147
153
lock_get (& c -> write_lock );
148
154
ret = tls_blocking_write (c , fd , buf , len ,
149
- tls_handshake_tout , tls_send_tout , t_dst );
155
+ tls_handshake_tout , tls_send_tout , t_dst , ssl_global_lock );
150
156
lock_release (& c -> write_lock );
151
157
return ret ;
152
158
}
153
159
154
160
static int tls_write_on_socket (struct tcp_connection * c , int fd ,
155
- char * buf , int len )
161
+ char * buf , int len , gen_lock_t * ssl_global_lock )
156
162
{
157
163
int n ;
158
164
@@ -168,7 +174,7 @@ static int tls_write_on_socket(struct tcp_connection* c, int fd,
168
174
goto release ;
169
175
}
170
176
171
- n = tls_write (c , fd , buf , len , NULL );
177
+ n = tls_write (c , fd , buf , len , NULL , ssl_global_lock );
172
178
if (n >= 0 && len - n ) {
173
179
/* if could not write entire buffer, delay it */
174
180
n = tcp_async_add_chunk (c , buf + n , len - n , 0 );
@@ -178,7 +184,7 @@ static int tls_write_on_socket(struct tcp_connection* c, int fd,
178
184
}
179
185
} else {
180
186
n = tls_blocking_write (c , fd , buf , len ,
181
- tls_handshake_tout , tls_send_tout , t_dst );
187
+ tls_handshake_tout , tls_send_tout , t_dst , ssl_global_lock );
182
188
}
183
189
release :
184
190
lock_release (& c -> write_lock );
@@ -328,6 +334,14 @@ static int mod_init(void)
328
334
sroutes -> request , RT_NO );
329
335
}
330
336
337
+ #ifndef NO_SSL_GLOBAL_LOCK
338
+ ssl_global_lock = lock_alloc ();
339
+ if (!ssl_global_lock || !lock_init (ssl_global_lock )) {
340
+ LM_ERR ("could not initialize openssl lock!\n" );
341
+ return -1 ;
342
+ }
343
+ #endif
344
+
331
345
return 0 ;
332
346
}
333
347
@@ -337,6 +351,11 @@ static int mod_init(void)
337
351
*/
338
352
static void mod_destroy (void )
339
353
{
354
+ #ifndef NO_SSL_GLOBAL_LOCK
355
+ lock_destroy (ssl_global_lock );
356
+ lock_dealloc (ssl_global_lock );
357
+ #endif
358
+
340
359
/* library destroy */
341
360
ERR_free_strings ();
342
361
/*SSL_free_comp_methods(); - this function is not on std. openssl*/
@@ -440,7 +459,7 @@ static void proto_tls_conn_clean(struct tcp_connection* c)
440
459
c -> proto_data = NULL ;
441
460
}
442
461
443
- tls_conn_clean (c , & tls_mgm_api );
462
+ tls_conn_clean (c , ssl_global_lock , & tls_mgm_api );
444
463
}
445
464
446
465
@@ -537,7 +556,8 @@ static int proto_tls_send(struct socket_info* send_sock,
537
556
/* we connect under lock to make sure no one else is reading our
538
557
* connect status */
539
558
tls_update_fd (c , fd );
540
- n = tls_async_connect (c , fd , tls_async_handshake_connect_timeout , t_dst );
559
+ n = tls_async_connect (c , fd , tls_async_handshake_connect_timeout , t_dst ,
560
+ ssl_global_lock );
541
561
lock_release (& c -> write_lock );
542
562
if (n < 0 ) {
543
563
LM_ERR ("failed async TLS connect\n" );
@@ -581,7 +601,7 @@ static int proto_tls_send(struct socket_info* send_sock,
581
601
send_it :
582
602
LM_DBG ("sending via fd %d...\n" ,fd );
583
603
584
- rlen = tls_write_on_socket (c , fd , buf , len );
604
+ rlen = tls_write_on_socket (c , fd , buf , len , ssl_global_lock );
585
605
tcp_conn_set_lifetime ( c , tcp_con_lifetime );
586
606
587
607
LM_DBG ("after write: c=%p n=%d fd=%d\n" ,c , rlen , fd );
@@ -635,7 +655,7 @@ static int tls_read_req(struct tcp_connection* con, int* bytes_read)
635
655
}
636
656
637
657
/* do this trick in order to trace whether if it's an error or not */
638
- ret = tls_fix_read_conn (con , tls_handshake_tout , t_dst );
658
+ ret = tls_fix_read_conn (con , tls_handshake_tout , t_dst , ssl_global_lock );
639
659
if (ret < 0 ) {
640
660
LM_ERR ("failed to do pre-tls handshake!\n" );
641
661
return -1 ;
@@ -677,7 +697,7 @@ static int tls_read_req(struct tcp_connection* con, int* bytes_read)
677
697
if (req -> parsed < req -> pos ){
678
698
bytes = 0 ;
679
699
}else {
680
- bytes = tls_read (con ,req );
700
+ bytes = tls_read (con ,req , ssl_global_lock );
681
701
if (bytes < 0 ) {
682
702
LM_ERR ("failed to read \n" );
683
703
goto error ;
@@ -740,7 +760,8 @@ static int tls_async_write(struct tcp_connection* con, int fd)
740
760
struct tcp_async_chunk * chunk ;
741
761
SSL * ssl = (SSL * )con -> extra_data ;
742
762
743
- err = tls_fix_read_conn_unlocked (con , fd , tls_handshake_tout , t_dst );
763
+ err = tls_fix_read_conn_unlocked (con , fd , tls_handshake_tout , t_dst ,
764
+ ssl_global_lock );
744
765
if (err < 0 ) {
745
766
LM_ERR ("failed to do pre-tls handshake!\n" );
746
767
return -1 ;
@@ -753,15 +774,28 @@ static int tls_async_write(struct tcp_connection* con, int fd)
753
774
while ((chunk = tcp_async_get_chunk (con )) != NULL ) {
754
775
LM_DBG ("Trying to send %d bytes from chunk %p in conn %p - %d %d \n" ,
755
776
chunk -> len , chunk , con , chunk -> ticks , get_ticks ());
777
+
778
+ #ifndef NO_SSL_GLOBAL_LOCK
779
+ lock_get (ssl_global_lock );
780
+ #endif
781
+
756
782
n = SSL_write (con -> extra_data , chunk -> buf , chunk -> len );
757
783
if (n < 0 ) {
758
784
err = SSL_get_error (ssl , n );
759
785
switch (err ) {
760
786
case SSL_ERROR_ZERO_RETURN :
787
+ #ifndef NO_SSL_GLOBAL_LOCK
788
+ lock_release (ssl_global_lock );
789
+ #endif
790
+
761
791
LM_DBG ("connection closed cleanly\n" );
762
792
return -1 ;
763
793
case SSL_ERROR_WANT_READ :
764
794
case SSL_ERROR_WANT_WRITE :
795
+ #ifndef NO_SSL_GLOBAL_LOCK
796
+ lock_release (ssl_global_lock );
797
+ #endif
798
+
765
799
LM_DBG ("Can't finish to write chunk %p on conn %p\n" ,
766
800
chunk ,con );
767
801
/* report back we have more writting to be done */
@@ -770,6 +804,11 @@ static int tls_async_write(struct tcp_connection* con, int fd)
770
804
LM_ERR ("Error occurred while sending async chunk %d:%d (%s)\n" ,
771
805
err , errno ,strerror (errno ));
772
806
tls_print_errstack ();
807
+
808
+ #ifndef NO_SSL_GLOBAL_LOCK
809
+ lock_release (ssl_global_lock );
810
+ #endif
811
+
773
812
/* report the conn as broken */
774
813
return -1 ;
775
814
}
0 commit comments