-
Notifications
You must be signed in to change notification settings - Fork 590
/
Copy pathopenssl_conn_ops.c
1155 lines (999 loc) · 27.8 KB
/
openssl_conn_ops.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2015 - OpenSIPS Foundation
*
* This file is part of opensips, a free SIP server.
*
* opensips is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* opensips is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*
*/
#include <openssl/ui.h>
#include <openssl/ssl.h>
#include <openssl/opensslv.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include <openssl/x509v3.h>
#include <poll.h>
#include <errno.h>
#include <unistd.h>
#include <netinet/tcp.h>
#include "../../net/tcp_conn_defs.h"
#include "../../net/proto_tcp/tcp_common_defs.h"
#include "../tls_mgm/tls_helper.h"
#include "openssl_trace.h"
void tls_print_errstack(void);
void tls_dump_cert_info(char* s, X509* cert);
extern gen_lock_t *tls_global_lock;
#define TLS_ERR_MAX 256
static char tls_err_buf[TLS_ERR_MAX];
static int tls_get_errstack( char* result, int size )
{
int len = 0, new, code;
if ( !result || !size )
return 0;
while ((code = ERR_get_error())) {
/* in case we overflow the buffer we still need to report the error
* to syslog */
if ( len < size ) {
new = snprintf( result + len, size - len,
"%s\n", ERR_error_string( code, 0) );
LM_ERR("TLS errstack: %s\n", result + len);
} else {
/* even though there s no place in the buffer we still have
* to print the errors */
LM_ERR("TLS errstack: %s\n", ERR_error_string(code, 0));
continue;
}
if ( new < size ) {
len += new;
} else {
len = size;
}
}
return len;
}
static void tls_dump_verification_failure(long verification_result)
{
switch(verification_result) {
case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
LM_ERR("unable to get issuer certificate\n");
break;
case X509_V_ERR_UNABLE_TO_GET_CRL:
LM_ERR("unable to get certificate CRL\n");
break;
case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
LM_ERR("unable to decrypt certificate's signature\n");
break;
case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
LM_ERR("unable to decrypt CRL's signature\n");
break;
case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
LM_ERR("unable to decode issuer public key\n");
break;
case X509_V_ERR_CERT_SIGNATURE_FAILURE:
LM_ERR("certificate signature failure\n");
break;
case X509_V_ERR_CRL_SIGNATURE_FAILURE:
LM_ERR("CRL signature failure\n");
break;
case X509_V_ERR_CERT_NOT_YET_VALID:
LM_ERR("certificate is not yet valid\n");
break;
case X509_V_ERR_CERT_HAS_EXPIRED:
LM_ERR("certificate has expired\n");
break;
case X509_V_ERR_CRL_NOT_YET_VALID:
LM_ERR("CRL is not yet valid\n");
break;
case X509_V_ERR_CRL_HAS_EXPIRED:
LM_ERR("CRL has expired\n");
break;
case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
LM_ERR("format error in certificate's notBefore field\n");
break;
case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
LM_ERR("format error in certificate's notAfter field\n");
break;
case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
LM_ERR("format error in CRL's lastUpdate field\n");
break;
case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
LM_ERR("format error in CRL's nextUpdate field\n");
break;
case X509_V_ERR_OUT_OF_MEM:
LM_ERR("out of memory\n");
break;
case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
LM_ERR("self signed certificate\n");
break;
case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
LM_ERR("self signed certificate in certificate chain\n");
break;
case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
LM_ERR("unable to get local issuer certificate\n");
break;
case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
LM_ERR("unable to verify the first certificate\n");
break;
case X509_V_ERR_CERT_CHAIN_TOO_LONG:
LM_ERR("certificate chain too long\n");
break;
case X509_V_ERR_CERT_REVOKED:
LM_ERR("certificate revoked\n");
break;
case X509_V_ERR_INVALID_CA:
LM_ERR("invalid CA certificate\n");
break;
case X509_V_ERR_PATH_LENGTH_EXCEEDED:
LM_ERR("path length constraint exceeded\n");
break;
case X509_V_ERR_INVALID_PURPOSE:
LM_ERR("unsupported certificate purpose\n");
break;
case X509_V_ERR_CERT_UNTRUSTED:
LM_ERR("certificate not trusted\n");
break;
case X509_V_ERR_CERT_REJECTED:
LM_ERR("certificate rejected\n");
break;
case X509_V_ERR_SUBJECT_ISSUER_MISMATCH:
LM_ERR("subject issuer mismatch\n");
break;
case X509_V_ERR_AKID_SKID_MISMATCH:
LM_ERR("authority and subject key identifier mismatch\n");
break;
case X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH:
LM_ERR("authority and issuer serial number mismatch\n");
break;
case X509_V_ERR_KEYUSAGE_NO_CERTSIGN:
LM_ERR("key usage does not include certificate signing\n");
break;
case X509_V_ERR_APPLICATION_VERIFICATION:
LM_ERR("application verification failure\n");
break;
}
}
int openssl_tls_update_fd(struct tcp_connection *c, int fd)
{
/*
* must be run from within a lock
*/
SSL *ssl;
ssl = (SSL *) c->extra_data;
if (!SSL_set_fd(ssl, fd)) {
LM_ERR("failed to assign socket to ssl\n");
return -1;
}
LM_DBG("New fd is %d\n", fd);
return 0;
}
int openssl_tls_conn_init(struct tcp_connection* c, struct tls_domain *tls_dom)
{
X509_VERIFY_PARAM *param = NULL;
/*
* new connection within a single process, no lock necessary
*/
LM_DBG("Creating a whole new ssl connection\n");
if ( c->flags&F_CONN_ACCEPTED ) {
/* connection created as a result of an accept -> server */
c->proto_flags = F_TLS_DO_ACCEPT;
} else
/* connection created as a result of a connect -> client */
c->proto_flags = F_TLS_DO_CONNECT;
c->extra_data = SSL_new(((void**)tls_dom->ctx)[process_no]);
if (!c->extra_data) {
LM_ERR("failed to create SSL structure (%d:%s)\n", errno, strerror(errno));
tls_print_errstack();
return -1;
}
if (tls_dom->verify_hostname) {
param = SSL_get0_param(c->extra_data);
X509_VERIFY_PARAM_set_hostflags(param, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
if (!X509_VERIFY_PARAM_set1_host(param, c->hostname, strlen(c->hostname))) {
LM_ERR("failed to set hostname for SSL context\n");
return -1;
}
}
/* put pointers to the tcp_connection and tls_domain structs
* in the SSL struct as extra data */
if (!SSL_set_ex_data(c->extra_data, SSL_EX_CONN_IDX, c)) {
LM_ERR("Failed to store tcp_connection pointer in SSL struct\n");
return -1;
}
if (!SSL_set_ex_data(c->extra_data, SSL_EX_DOM_IDX, tls_dom)) {
LM_ERR("Failed to store tls_domain pointer in SSL struct\n");
return -1;
}
#if OPENSSL_VERSION_NUMBER < 0x10100000L
#ifndef OPENSSL_NO_KRB5
if ( ((SSL *)c->extra_data)->kssl_ctx ) {
kssl_ctx_free( ((SSL *)c->extra_data)->kssl_ctx );
((SSL *)c->extra_data)->kssl_ctx = 0;
}
#endif
#endif
if ( c->proto_flags & F_TLS_DO_ACCEPT ) {
LM_DBG("Setting in ACCEPT mode (server)\n");
SSL_set_accept_state((SSL *) c->extra_data);
} else {
LM_DBG("Setting in CONNECT mode (client)\n");
SSL_set_connect_state((SSL *) c->extra_data);
}
/* if the connection is asynchronous, allow partial writes */
if (c->async && !SSL_set_mode((SSL *)c->extra_data,
SSL_MODE_ENABLE_PARTIAL_WRITE|SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER))
LM_ERR("Failed to enable non-blocking write! Running in blocking mode!\n");
return 0;
}
static int openssl_tls_conn_shutdown(struct tcp_connection *c)
{
int ret,
err;
SSL *ssl;
/* If EOF or other error on connection, no point in attempting to
* do further writing & reading on the con */
if (c->state == S_CONN_BAD ||
c->state == S_CONN_ERROR ||
c->state == S_CONN_EOF)
return 0;
/*
* we do not implement full ssl shutdown
*/
ssl = (SSL *) c->extra_data;
if (ssl == 0) {
LM_ERR("no ssl data\n");
return -1;
}
#ifndef NO_SSL_GLOBAL_LOCK
lock_get(tls_global_lock);
#endif
ERR_clear_error();
ret = SSL_shutdown(ssl);
if (ret == 1) {
#ifndef NO_SSL_GLOBAL_LOCK
lock_release(tls_global_lock);
#endif
LM_DBG("shutdown successful\n");
return 0;
} else if (ret == 0) {
#ifndef NO_SSL_GLOBAL_LOCK
lock_release(tls_global_lock);
#endif
LM_DBG("first phase of 2-way handshake completed succesfuly\n");
return 0;
} else {
err = SSL_get_error(ssl, ret);
switch (err) {
case SSL_ERROR_ZERO_RETURN:
#ifndef NO_SSL_GLOBAL_LOCK
lock_release(tls_global_lock);
#endif
c->state = S_CONN_EOF;
return 0;
case SSL_ERROR_WANT_READ:
case SSL_ERROR_WANT_WRITE:
#ifndef NO_SSL_GLOBAL_LOCK
lock_release(tls_global_lock);
#endif
c->state = S_CONN_EOF;
return 0;
default:
LM_ERR("something wrong in SSL: %d, %d, %s\n",err,errno,strerror(errno));
/* fall through */
case SSL_ERROR_SYSCALL:
c->state = S_CONN_BAD;
tls_print_errstack();
#ifndef NO_SSL_GLOBAL_LOCK
lock_release(tls_global_lock);
#endif
return -1;
}
}
LM_ERR("bug\n");
return -1;
}
void openssl_tls_conn_clean(struct tcp_connection *c, struct tls_domain **tls_dom)
{
void *d = NULL;
if (c->extra_data) {
d = SSL_get_ex_data(c->extra_data, SSL_EX_DOM_IDX);
openssl_tls_update_fd(c,c->s);
openssl_tls_conn_shutdown(c);
SSL_free((SSL *) c->extra_data);
c->extra_data = 0;
}
*tls_dom = d;
}
static int openssl_tls_connect(struct tcp_connection *c, short *poll_events,
trace_dest t_dst)
{
int ret, err;
SSL *ssl;
X509* cert;
str tls_err_s;
if ( (c->proto_flags&F_TLS_DO_CONNECT)==0 ) {
LM_BUG("invalid connection state (bug in TLS code)\n");
return -1;
}
ssl = (SSL *) c->extra_data;
#ifndef NO_SSL_GLOBAL_LOCK
lock_get(tls_global_lock);
#endif
ERR_clear_error();
ret = SSL_connect(ssl);
if (ret > 0) {
#ifndef NO_SSL_GLOBAL_LOCK
lock_release(tls_global_lock);
#endif
LM_INFO("New TLS connection to %s:%d established\n",
ip_addr2a(&c->rcv.src_ip), c->rcv.src_port);
trace_tls( c, ssl, TRANS_TRACE_CONNECTED,
TRANS_TRACE_SUCCESS, &CONNECT_OK);
tls_send_trace_data(c, t_dst);
c->proto_flags &= ~F_TLS_DO_CONNECT;
LM_DBG("new TLS connection to %s:%d using %s %s %d\n",
ip_addr2a(&c->rcv.src_ip), c->rcv.src_port,
SSL_get_cipher_version(ssl), SSL_get_cipher_name(ssl),
SSL_get_cipher_bits(ssl, 0)
);
LM_DBG("sending socket: %s:%d \n",
ip_addr2a(&c->rcv.dst_ip), c->rcv.dst_port
);
cert = SSL_get_peer_certificate(ssl);
if (cert != 0) {
tls_dump_cert_info("tls_connect: server TLS certificate", cert);
if (SSL_get_verify_result(ssl) != X509_V_OK) {
LM_WARN("TLS server certificate verification failed\n");
tls_dump_verification_failure(SSL_get_verify_result(ssl));
}
X509_free(cert);
} else {
/* this should not happen, servers always present a cert */
LM_ERR("server did not present a TLS certificate\n");
}
cert = SSL_get_certificate(ssl);
if (cert != 0) {
tls_dump_cert_info("tls_connect: local TLS client certificate",
cert);
} else {
LM_INFO("local TLS client domain does not have a certificate\n");
}
return 1;
} else {
err = SSL_get_error(ssl, ret);
switch (err) {
case SSL_ERROR_ZERO_RETURN:
#ifndef NO_SSL_GLOBAL_LOCK
lock_release(tls_global_lock);
#endif
LM_INFO("New TLS connection to %s:%d failed cleanly\n",
ip_addr2a(&c->rcv.src_ip), c->rcv.src_port);
trace_tls( c, ssl, TRANS_TRACE_CONNECTED,
TRANS_TRACE_FAILURE, &CONNECT_FAIL);
tls_send_trace_data(c, t_dst);
c->state = S_CONN_BAD;
return -1;
case SSL_ERROR_WANT_READ:
#ifndef NO_SSL_GLOBAL_LOCK
lock_release(tls_global_lock);
#endif
if (poll_events)
*poll_events = POLLIN;
return 0;
case SSL_ERROR_WANT_WRITE:
#ifndef NO_SSL_GLOBAL_LOCK
lock_release(tls_global_lock);
#endif
if (poll_events)
*poll_events = POLLOUT;
return 0;
case SSL_ERROR_SYSCALL:
LM_ERR("SSL_ERROR_SYSCALL err=%s(%d)\n",
strerror(errno), errno);
/* fall through */
default:
LM_ERR("New TLS connection to %s:%d failed\n",
ip_addr2a(&c->rcv.src_ip), c->rcv.src_port);
LM_ERR("TLS error: %d (ret=%d) err=%s(%d)\n",
err,ret,strerror(errno), errno);
c->state = S_CONN_BAD;
if ( TLS_TRACE_IS_ON( c ) ) {
if ( ( tls_err_s.len =
tls_get_errstack( tls_err_buf, TLS_ERR_MAX ) ) == 0 ) {
tls_err_s.len = snprintf( tls_err_buf, TLS_ERR_MAX,
"New TLS connection failed to connect" );
}
tls_err_s.s = tls_err_buf;
trace_tls( c, ssl, TRANS_TRACE_CONNECTED,
TRANS_TRACE_FAILURE, &tls_err_s);
tls_send_trace_data(c, t_dst);
} else {
tls_print_errstack();
}
#ifndef NO_SSL_GLOBAL_LOCK
lock_release(tls_global_lock);
#endif
return -1;
}
}
LM_BUG("bug\n");
return -1;
}
static int openssl_tls_accept(struct tcp_connection *c, short *poll_events)
{
int ret, err;
SSL *ssl;
X509* cert;
str tls_err_s;
if ( (c->proto_flags&F_TLS_DO_ACCEPT)==0 ) {
LM_BUG("invalid connection state (bug in TLS code)\n");
return -1;
}
ssl = (SSL *) c->extra_data;
#if OPENSSL_VERSION_NUMBER < 0x10100000L
#ifndef OPENSSL_NO_KRB5
if ( ssl->kssl_ctx==NULL )
ssl->kssl_ctx = kssl_ctx_new( );
#endif
#endif
#ifndef NO_SSL_GLOBAL_LOCK
lock_get(tls_global_lock);
#endif
ERR_clear_error();
ret = SSL_accept(ssl);
#if OPENSSL_VERSION_NUMBER < 0x10100000L
#ifndef OPENSSL_NO_KRB5
if ( ssl->kssl_ctx ) {
kssl_ctx_free( ssl->kssl_ctx );
ssl->kssl_ctx = 0;
}
#endif
#endif
if (ret > 0) {
#ifndef NO_SSL_GLOBAL_LOCK
lock_release(tls_global_lock);
#endif
LM_INFO("New TLS connection from %s:%d accepted\n",
ip_addr2a(&c->rcv.src_ip), c->rcv.src_port);
trace_tls( c, ssl, TRANS_TRACE_ACCEPTED, TRANS_TRACE_SUCCESS, &ACCEPT_OK);
/* TLS accept done, reset the flag */
c->proto_flags &= ~F_TLS_DO_ACCEPT;
LM_DBG("new TLS connection from %s:%d using %s %s %d\n",
ip_addr2a(&c->rcv.src_ip), c->rcv.src_port,
SSL_get_cipher_version(ssl), SSL_get_cipher_name(ssl),
SSL_get_cipher_bits(ssl, 0) );
LM_DBG("local socket: %s:%d\n",
ip_addr2a(&c->rcv.dst_ip), c->rcv.dst_port );
cert = SSL_get_peer_certificate(ssl);
if (cert != 0) {
tls_dump_cert_info("tls_accept: client TLS certificate", cert);
if (SSL_get_verify_result(ssl) != X509_V_OK) {
LM_WARN("TLS client certificate verification failed\n");
tls_dump_verification_failure(SSL_get_verify_result(ssl));
}
X509_free(cert);
} else {
LM_INFO("Client did not present a TLS certificate\n");
}
cert = SSL_get_certificate(ssl);
if (cert != 0) {
tls_dump_cert_info("tls_accept: local TLS server certificate",
cert);
} else {
/* this should not happen, servers always present a cert */
LM_ERR("local TLS server domain has no certificate\n");
}
return 1;
} else {
err = SSL_get_error(ssl, ret);
switch (err) {
case SSL_ERROR_ZERO_RETURN:
#ifndef NO_SSL_GLOBAL_LOCK
lock_release(tls_global_lock);
#endif
LM_INFO("TLS connection from %s:%d accept failed cleanly\n",
ip_addr2a(&c->rcv.src_ip), c->rcv.src_port);
trace_tls( c, ssl, TRANS_TRACE_ACCEPTED,
TRANS_TRACE_FAILURE, &ACCEPT_FAIL);
c->state = S_CONN_BAD;
return -1;
case SSL_ERROR_WANT_READ:
#ifndef NO_SSL_GLOBAL_LOCK
lock_release(tls_global_lock);
#endif
if (poll_events)
*poll_events = POLLIN;
return 0;
case SSL_ERROR_WANT_WRITE:
#ifndef NO_SSL_GLOBAL_LOCK
lock_release(tls_global_lock);
#endif
if (poll_events)
*poll_events = POLLOUT;
return 0;
case SSL_ERROR_SYSCALL:
LM_ERR("SSL_ERROR_SYSCALL err=%s(%d)\n",
strerror(errno), errno);
default:
c->state = S_CONN_BAD;
LM_ERR("New TLS connection from %s:%d failed to accept\n",
ip_addr2a(&c->rcv.src_ip), c->rcv.src_port);
if (errno != 0) {
LM_ERR("TLS error: (ret=%d, err=%d, errno=%d/%s):\n",
ret, err, errno, strerror(errno));
}
if ( TLS_TRACE_IS_ON( c ) ) {
if ( ( tls_err_s.len =
tls_get_errstack( tls_err_buf, TLS_ERR_MAX ) ) == 0 ) {
if ( errno ) {
tls_err_s.len = snprintf( tls_err_buf, TLS_ERR_MAX,
"TLS error: (ret=%d, err=%d, errno=%d/%s)",
ret, err, errno, strerror(errno));
} else {
tls_err_s.len = snprintf( tls_err_buf, TLS_ERR_MAX,
"New TLS connection failed to accept" );
}
}
tls_err_s.s = tls_err_buf;
trace_tls( c, ssl, TRANS_TRACE_ACCEPTED,
TRANS_TRACE_FAILURE, &tls_err_s);
} else {
tls_print_errstack();
}
#ifndef NO_SSL_GLOBAL_LOCK
lock_release(tls_global_lock);
#endif
return -1;
}
}
LM_BUG("bug\n");
return -1;
}
int openssl_tls_async_connect(struct tcp_connection *con, int fd,
int timeout, trace_dest t_dst)
{
unsigned int elapsed,to;
unsigned int err_len;
int poll_err, n, err;
struct timeval begin;
str tls_err_s;
#if defined(HAVE_SELECT) && defined(BLOCKING_USE_SELECT)
fd_set sel_set;
fd_set orig_set;
struct timeval timeout_val;
#else
struct pollfd pf;
#endif
SSL *ssl = (SSL *)con->extra_data;
/* attempt to do connect and see if we do block or not */
poll_err=0;
elapsed = 0;
to = timeout*1000;
fd = con->fd;
#if defined(HAVE_SELECT) && defined(BLOCKING_USE_SELECT)
FD_ZERO(&orig_set);
FD_SET(fd, &orig_set);
#else
pf.fd=fd;
pf.events=POLLOUT|POLLIN;
#endif
if (gettimeofday(&(begin), NULL)) {
LM_ERR("Failed to get TLS connect start time\n");
goto failure;
}
while (1) {
#ifndef NO_SSL_GLOBAL_LOCK
lock_get(tls_global_lock);
#endif
n = SSL_connect(ssl);
if (n > 0) {
#ifndef NO_SSL_GLOBAL_LOCK
lock_release(tls_global_lock);
#endif
LM_INFO("new TLS connection to %s:%d established\n",
ip_addr2a(&con->rcv.src_ip), con->rcv.src_port);
trace_tls(con, ssl, TRANS_TRACE_CONNECTED,
TRANS_TRACE_SUCCESS, &ASYNC_CONNECT_OK);
tls_send_trace_data(con, t_dst);
con->proto_flags &= ~F_TLS_DO_CONNECT;
return 1;
} else if (n == 0) {
err = SSL_get_error(ssl, n);
#ifndef NO_SSL_GLOBAL_LOCK
lock_release(tls_global_lock);
#endif
LM_ERR("Failed to connect to %s:%d %d:%d (%s)\n",
ip_addr2a(&con->rcv.src_ip), con->rcv.src_port,
err, errno, strerror(errno));
goto failure;
}
err = SSL_get_error(ssl, n);
switch (err) {
case SSL_ERROR_ZERO_RETURN:
#ifndef NO_SSL_GLOBAL_LOCK
lock_release(tls_global_lock);
#endif
LM_INFO("New TLS connection to %s:%d failed cleanly\n",
ip_addr2a(&con->rcv.src_ip), con->rcv.src_port);
goto failure;
case SSL_ERROR_WANT_READ:
case SSL_ERROR_WANT_WRITE:
#ifndef NO_SSL_GLOBAL_LOCK
lock_release(tls_global_lock);
#endif
/* we need to retry, if time has not passed yet */
again:
elapsed=get_time_diff(&begin);
if (elapsed >= to) /* timed out */ {
LM_DBG("handshake timeout for connection %p %dms elapsed\n",
con, timeout);
return 0;
}
to -= elapsed;
#if defined(HAVE_SELECT) && defined(BLOCKING_USE_SELECT)
sel_set=orig_set;
timeout_val.tv_sec=to/1000000;
timeout_val.tv_usec=to%1000000;
n=select(fd+1, 0, &sel_set, 0, &timeout_val);
#else
n=poll(&pf, 1, to/1000);
#endif
if (n<0){
if (errno == EINTR)
goto again;
LM_ERR("poll/select failed:[server=%s:%d] (%d) %s\n",
ip_addr2a(&con->rcv.src_ip), con->rcv.src_port,
errno, strerror(errno));
goto failure;
}else if (n==0) /* timeout */
goto again;
#if defined(HAVE_SELECT) && defined(BLOCKING_USE_SELECT)
if (FD_ISSET(fd, &sel_set))
#else
if (pf.revents&(POLLERR|POLLHUP|POLLNVAL)){
LM_ERR("poll error: flags %x\n", pf.revents);
poll_err=1;
}
#endif
{
err_len=sizeof(err);
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &err_len) != 0) {
LM_WARN("getsockopt error: fd=%d [server=%s:%d]: (%d) %s\n", fd,
ip_addr2a(&con->rcv.src_ip), con->rcv.src_port,
errno, strerror(errno));
goto failure;
}
if ((err==0) && (poll_err==0))
continue; /* retry ssl connect */
if (err!=EINPROGRESS && err!=EALREADY){
LM_ERR("failed to retrieve SO_ERROR [server=%s:%d] (%d) %s\n",
ip_addr2a(&con->rcv.src_ip), con->rcv.src_port,
err, strerror(err));
goto failure;
}
continue;
}
case SSL_ERROR_SYSCALL:
#ifndef NO_SSL_GLOBAL_LOCK
lock_release(tls_global_lock);
#endif
LM_ERR("SSL_ERROR_SYSCALL err=%s(%d)\n",
strerror(errno), errno);
default:
LM_ERR("New TLS connection to %s:%d failed\n",
ip_addr2a(&con->rcv.src_ip), con->rcv.src_port);
LM_ERR("TLS error: %d (ret=%d) err=%s(%d)\n",
err, n, strerror(errno), errno);
con->state = S_CONN_BAD;
if ( TLS_TRACE_IS_ON( con ) ) {
if ( ( tls_err_s.len =
tls_get_errstack( tls_err_buf, TLS_ERR_MAX ) ) == 0 ) {
tls_err_s.len = snprintf( tls_err_buf, TLS_ERR_MAX,
"New TLS connection failed to connect" );
}
tls_err_s.s = tls_err_buf;
trace_tls( con, ssl, TRANS_TRACE_CONNECTED,
TRANS_TRACE_FAILURE, &tls_err_s);
tls_send_trace_data(con, t_dst);
}
tls_print_errstack();
#ifndef NO_SSL_GLOBAL_LOCK
lock_release(tls_global_lock);
#endif
return -1;
}
}
failure:
trace_tls(con, ssl, TRANS_TRACE_CONNECTED,
TRANS_TRACE_FAILURE, &CONNECT_FAIL);
tls_send_trace_data(con, t_dst);
con->state = S_CONN_BAD;
return -1;
}
int openssl_tls_write(struct tcp_connection *c, int fd, const void *buf,
size_t len, short *poll_events)
{
int ret,
err;
/*
* runs within write lock, no need to lock here
*/
SSL *ssl;
ssl = (SSL *) c->extra_data;
#ifndef NO_SSL_GLOBAL_LOCK
lock_get(tls_global_lock);
#endif
ERR_clear_error();
ret = SSL_write(ssl, buf, len);
if (ret > 0) {
#ifndef NO_SSL_GLOBAL_LOCK
lock_release(tls_global_lock);
#endif
LM_DBG("write was successful (%d bytes)\n", ret);
return ret;
} else {
err = SSL_get_error(ssl, ret);
switch (err) {
case SSL_ERROR_ZERO_RETURN:
#ifndef NO_SSL_GLOBAL_LOCK
lock_release(tls_global_lock);
#endif
LM_DBG("connection closed cleanly\n");
c->state = S_CONN_EOF;
return -1;
case SSL_ERROR_WANT_READ:
#ifndef NO_SSL_GLOBAL_LOCK
lock_release(tls_global_lock);
#endif
if (poll_events)
*poll_events = POLLIN;
return 0;
case SSL_ERROR_WANT_WRITE:
#ifndef NO_SSL_GLOBAL_LOCK
lock_release(tls_global_lock);
#endif
if (poll_events)
*poll_events = POLLOUT;
return 0;
default:
LM_ERR("TLS connection to %s:%d write failed (%d:%d:%d)\n",
ip_addr2a(&c->rcv.src_ip), c->rcv.src_port, err, ret, errno);
LM_ERR("TLS write error:\n");
c->state = S_CONN_BAD;
tls_print_errstack();
#ifndef NO_SSL_GLOBAL_LOCK
lock_release(tls_global_lock);
#endif
return -1;
}
}
LM_BUG("bug\n");
return -1;
}
int openssl_tls_blocking_write(struct tcp_connection *c, int fd,
const char *buf, size_t len, int handshake_timeout, int send_timeout,
trace_dest t_dst)
{
#define MAX_SSL_RETRIES 32
int written, n;
int timeout, retries;
struct pollfd pf;
pf.fd = fd;
written = 0;
retries = 0;
if (c->state!=S_CONN_OK) {
LM_ERR("TLS broken connection\n");
goto error;
}
if (openssl_tls_update_fd(c, fd) < 0)
goto error;
timeout = send_timeout;
again:
n = 0;
pf.events = 0;
if ( c->proto_flags & F_TLS_DO_ACCEPT ) {
if (openssl_tls_accept(c, &(pf.events)) < 0)
goto error;
timeout = handshake_timeout;
} else if ( c->proto_flags & F_TLS_DO_CONNECT ) {
if (openssl_tls_connect(c, &(pf.events), t_dst) < 0)
goto error;
timeout = handshake_timeout;
} else {
n = openssl_tls_write(c, fd, buf, len, &(pf.events));
timeout = send_timeout;
}
if (n < 0) {
LM_ERR("TLS failed to send data\n");
goto error;
}
/* nothing happens */
if (n==0) {
retries++;
/* avoid looping */
if (retries==MAX_SSL_RETRIES) {
LM_ERR("too many retries with no operation\n");
goto error;
}
} else {
/* reset the retries if we succeeded in doing something*/
retries = 0;
}
written += n;
if (n < len) {
/*
* partial write
*/
buf += n;
len -= n;
} else {
/*
* successful full write
*/
return written;
}
if (pf.events == 0)
pf.events = POLLOUT;
poll_loop:
while (1) {
n = poll(&pf, 1, timeout);
if (n < 0) {
if (errno == EINTR)
continue; /* signal, ignore */
else if (errno != EAGAIN && errno != EWOULDBLOCK) {
LM_ERR("TLS poll failed: %s [%d]\n",strerror(errno), errno);
goto error;
} else
goto poll_loop;
} else if (n == 0) {
/*
* timeout
*/
LM_ERR("TLS send timeout (%d)\n", timeout);
goto error;
}
if (pf.revents & POLLOUT || pf.revents & POLLIN) {
/*
* we can read or write again
*/