-
Notifications
You must be signed in to change notification settings - Fork 564
/
tls_mgm.c
1479 lines (1278 loc) · 43.7 KB
/
tls_mgm.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) 2001-2003 FhG Fokus
* 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
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*
* 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 <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/tcp.h>
#include <netinet/ip.h>
#include <unistd.h>
#include "../../dprint.h"
#include "../../mem/shm_mem.h"
#include "../../sr_module.h"
#include "../../net/api_proto.h"
#include "../../net/api_proto_net.h"
#include "../../net/net_tcp.h"
#include "../../socket_info.h"
#include "../../tsend.h"
#include "../../timer.h"
#include "../../receive.h"
#include "../../pt.h"
#include "../../parser/msg_parser.h"
#include "../../pvar.h"
#include "../../db/db.h"
#include "../../str_list.h"
#include "../tls_openssl/openssl_api.h"
#include "../tls_wolfssl/wolfssl_api.h"
#include "../../net/proto_tcp/tcp_common_defs.h"
#include "tls_config.h"
#include "tls_domain.h"
#include "tls_params.h"
#include "tls_select.h"
#include "api.h"
#define DB_CAP DB_CAP_QUERY | DB_CAP_UPDATE
#define len(s) s == NULL?0:strlen(s)
#define check_val( _col, _val, _type, _not_null, _is_empty_str) \
do{\
if ((_val)->type!=_type) { \
LM_ERR("column %.*s has a bad type\n", _col.len, _col.s); \
continue;\
} \
if (_not_null && (_val)->nul) { \
LM_ERR("column %.*s is null\n", _col.len, _col.s); \
continue;\
} \
if (_is_empty_str && VAL_STRING(_val)==0) { \
LM_ERR("column %.*s (str) is empty\n", _col.len, _col.s); \
continue;\
} \
if ((_val)->type == DB_INT && (_val)->nul) { \
(_val)->val.int_val = -1;\
} \
}while(0)
struct openssl_binds openssl_api;
struct wolfssl_binds wolfssl_api;
enum os_tls_library tls_library;
static char *tls_domain_avp = NULL;
static char *sip_domain_avp = NULL;
static int mod_init(void);
static int child_init(int rank);
static void mod_destroy(void);
static int load_tls_mgm(struct tls_mgm_binds *binds);
static mi_response_t *tls_reload(const mi_params_t *params,
struct mi_handler *async_hdl);
static mi_response_t *tls_list(const mi_params_t *params,
struct mi_handler *async_hdl);
static int list_domain(mi_item_t *domains_arr, struct tls_domain *d);
/* DB handler */
static db_con_t *db_hdl = 0;
/* DB functions */
static db_func_t dr_dbf;
static param_export_t params[] = {
{ "tls_library", STR_PARAM, &tls_library_param.s },
{ "client_tls_domain_avp", STR_PARAM, &tls_domain_avp },
{ "client_sip_domain_avp", STR_PARAM, &sip_domain_avp },
{ "server_domain", STR_PARAM|USE_FUNC_PARAM, (void*)tlsp_add_srv_domain },
{ "client_domain", STR_PARAM|USE_FUNC_PARAM, (void*)tlsp_add_cli_domain },
{ "match_ip_address", STR_PARAM|USE_FUNC_PARAM, (void*)tlsp_set_match_addr },
{ "match_sip_domain", STR_PARAM|USE_FUNC_PARAM, (void*)tlsp_set_match_dom },
{ "tls_method", STR_PARAM|USE_FUNC_PARAM, (void*)tlsp_set_method },
{ "verify_cert", STR_PARAM|USE_FUNC_PARAM, (void*)tlsp_set_verify },
{ "require_cert", STR_PARAM|USE_FUNC_PARAM, (void*)tlsp_set_require },
{ "certificate", STR_PARAM|USE_FUNC_PARAM, (void*)tlsp_set_certificate},
{ "private_key", STR_PARAM|USE_FUNC_PARAM, (void*)tlsp_set_pk },
{ "crl_check_all", STR_PARAM|USE_FUNC_PARAM, (void*)tlsp_set_crl_check },
{ "crl_dir", STR_PARAM|USE_FUNC_PARAM, (void*)tlsp_set_crldir },
{ "ca_list", STR_PARAM|USE_FUNC_PARAM, (void*)tlsp_set_calist },
{ "ca_dir", STR_PARAM|USE_FUNC_PARAM, (void*)tlsp_set_cadir },
{ "ciphers_list", STR_PARAM|USE_FUNC_PARAM, (void*)tlsp_set_cplist },
{ "dh_params", STR_PARAM|USE_FUNC_PARAM, (void*)tlsp_set_dhparams },
{ "ec_curve", STR_PARAM|USE_FUNC_PARAM, (void*)tlsp_set_eccurve },
{ "db_url", STR_PARAM, &tls_db_url.s },
{ "db_table", STR_PARAM, &tls_db_table.s },
{ "domain_col", STR_PARAM, &domain_col.s },
{ "match_ip_address_col", STR_PARAM, &match_address_col.s },
{ "match_sip_domain_col", STR_PARAM, &match_domain_col.s },
{ "tls_method_col", STR_PARAM, &method_col.s },
{ "verify_cert_col", STR_PARAM, &verify_cert_col.s },
{ "require_cert_col", STR_PARAM, &require_cert_col.s },
{ "certificate_col", STR_PARAM, &certificate_col.s },
{ "private_key_col", STR_PARAM, &pk_col.s },
{ "crl_check_all_col", STR_PARAM, &crl_check_col.s },
{ "crl_dir_col", STR_PARAM, &crl_dir_col.s },
{ "ca_list_col", STR_PARAM, &calist_col.s },
{ "ca_dir_col", STR_PARAM, &cadir_col.s },
{ "ciphers_list_col", STR_PARAM, &cplist_col.s },
{ "dh_params_col", STR_PARAM, &dhparams_col.s },
{ "ec_curve_col", STR_PARAM, &eccurve_col.s },
{0, 0, 0}
};
static cmd_export_t cmds[] = {
{"is_peer_verified", (cmd_function)tls_is_peer_verified, {{0,0,0}},
REQUEST_ROUTE},
{"load_tls_mgm", (cmd_function)load_tls_mgm,
{{0,0,0}}, ALL_ROUTES},
{0,0,{{0,0,0}},0}
};
/*
* Exported MI functions
*/
static mi_export_t mi_cmds[] = {
{ "tls_reload", "reloads stored data from the database", 0, 0, {
{tls_reload, {0}},
{EMPTY_MI_RECIPE}}
},
{ "tls_list", "lists all domains", 0, 0, {
{tls_list, {0}},
{EMPTY_MI_RECIPE}}
},
{EMPTY_MI_EXPORT}
};
/*
* pseudo variables
*/
static pv_export_t mod_items[] = {
/* TLS session parameters */
{{"tls_version", sizeof("tls_version")-1},
850, tlsops_version, 0,
0, 0, 0, 0 },
{{"tls_description", sizeof("tls_description")-1},
850, tlsops_desc, 0,
0, 0, 0, 0 },
{{"tls_cipher_info", sizeof("tls_cipher_info")-1},
850, tlsops_cipher, 0,
0, 0, 0, 0 },
{{"tls_cipher_bits", sizeof("tls_cipher_bits")-1},
850, tlsops_bits, 0,
0, 0, 0, 0 },
/* general certificate parameters for peer and local */
{{"tls_peer_version", sizeof("tls_peer_version")-1},
850, tlsops_cert_version, 0,
0, 0, pv_init_iname, VAR_CERT_PEER },
{{"tls_my_version", sizeof("tls_my_version")-1},
850, tlsops_cert_version, 0,
0, 0, pv_init_iname, VAR_CERT_LOCAL },
{{"tls_peer_serial", sizeof("tls_peer_serial")-1},
850, tlsops_sn, 0,
0, 0, pv_init_iname, VAR_CERT_PEER },
{{"tls_my_serial", sizeof("tls_my_serial")-1},
850, tlsops_sn,0,
0, 0, pv_init_iname, VAR_CERT_LOCAL },
/* certificate parameters for peer and local, for subject and issuer*/
{{"tls_peer_subject", sizeof("tls_peer_subject")-1},
850, tlsops_comp, 0,
0, 0, pv_init_iname, VAR_CERT_PEER | VAR_CERT_SUBJECT },
{{"tls_peer_issuer", sizeof("tls_peer_issuer")-1},
850, tlsops_comp, 0,
0, 0, pv_init_iname, VAR_CERT_PEER | VAR_CERT_ISSUER },
{{"tls_my_subject", sizeof("tls_my_subject")-1},
850, tlsops_comp, 0,
0, 0, pv_init_iname, VAR_CERT_LOCAL | VAR_CERT_SUBJECT },
{{"tls_my_issuer", sizeof("tls_my_issuer")-1},
850, tlsops_comp, 0,
0, 0, pv_init_iname, VAR_CERT_LOCAL | VAR_CERT_ISSUER },
{{"tls_peer_subject_cn", sizeof("tls_peer_subject_cn")-1},
850, tlsops_comp, 0,
0, 0, pv_init_iname, VAR_CERT_PEER | VAR_CERT_SUBJECT | VAR_COMP_CN },
{{"tls_peer_issuer_cn", sizeof("tls_peer_issuer_cn")-1},
850, tlsops_comp, 0,
0, 0, pv_init_iname, VAR_CERT_PEER | VAR_CERT_ISSUER | VAR_COMP_CN },
{{"tls_my_subject_cn", sizeof("tls_my_subject_cn")-1},
850, tlsops_comp, 0,
0, 0, pv_init_iname, VAR_CERT_LOCAL | VAR_CERT_SUBJECT | VAR_COMP_CN },
{{"tls_my_issuer_cn", sizeof("tls_my_issuer_cn")-1},
850, tlsops_comp, 0,
0, 0, pv_init_iname, VAR_CERT_LOCAL | VAR_CERT_ISSUER | VAR_COMP_CN },
{{"tls_peer_subject_locality", sizeof("tls_peer_subject_locality")-1},
850, tlsops_comp, 0,
0, 0, pv_init_iname, VAR_CERT_PEER | VAR_CERT_SUBJECT | VAR_COMP_L },
{{"tls_peer_issuer_locality", sizeof("tls_peer_issuer_locality")-1},
850, tlsops_comp, 0,
0, 0, pv_init_iname, VAR_CERT_PEER | VAR_CERT_ISSUER | VAR_COMP_L },
{{"tls_my_subject_locality", sizeof("tls_my_subject_locality")-1},
850, tlsops_comp, 0,
0, 0, pv_init_iname, VAR_CERT_LOCAL | VAR_CERT_SUBJECT | VAR_COMP_L },
{{"tls_my_issuer_locality", sizeof("tls_my_issuer_locality")-1},
850, tlsops_comp, 0,
0, 0, pv_init_iname, VAR_CERT_LOCAL | VAR_CERT_ISSUER | VAR_COMP_L },
{{"tls_peer_subject_country", sizeof("tls_peer_subject_country")-1},
850, tlsops_comp, 0,
0, 0, pv_init_iname, VAR_CERT_PEER | VAR_CERT_SUBJECT | VAR_COMP_C },
{{"tls_peer_issuer_country", sizeof("tls_peer_issuer_country")-1},
850, tlsops_comp, 0,
0, 0, pv_init_iname, VAR_CERT_PEER | VAR_CERT_ISSUER | VAR_COMP_C },
{{"tls_my_subject_country", sizeof("tls_my_subject_country")-1},
850, tlsops_comp, 0,
0, 0, pv_init_iname, VAR_CERT_LOCAL | VAR_CERT_SUBJECT | VAR_COMP_C },
{{"tls_my_issuer_country", sizeof("tls_my_issuer_country")-1},
850, tlsops_comp, 0,
0, 0, pv_init_iname, VAR_CERT_LOCAL | VAR_CERT_ISSUER | VAR_COMP_C },
{{"tls_peer_subject_state", sizeof("tls_peer_subject_state")-1},
850, tlsops_comp, 0,
0, 0, pv_init_iname, VAR_CERT_PEER | VAR_CERT_SUBJECT | VAR_COMP_ST },
{{"tls_peer_issuer_state", sizeof("tls_peer_issuer_state")-1},
850, tlsops_comp, 0,
0, 0, pv_init_iname, VAR_CERT_PEER | VAR_CERT_ISSUER | VAR_COMP_ST },
{{"tls_my_subject_state", sizeof("tls_my_subject_state")-1},
850, tlsops_comp, 0,
0, 0, pv_init_iname, VAR_CERT_LOCAL | VAR_CERT_SUBJECT | VAR_COMP_ST },
{{"tls_my_issuer_state", sizeof("tls_my_issuer_state")-1},
850, tlsops_comp, 0,
0, 0, pv_init_iname, VAR_CERT_LOCAL | VAR_CERT_ISSUER | VAR_COMP_ST },
{{"tls_peer_subject_organization", sizeof("tls_peer_subject_organization")-1},
850, tlsops_comp, 0,
0, 0, pv_init_iname, VAR_CERT_PEER | VAR_CERT_SUBJECT | VAR_COMP_O },
{{"tls_peer_issuer_organization", sizeof("tls_peer_issuer_organization")-1},
850, tlsops_comp, 0,
0, 0, pv_init_iname, VAR_CERT_PEER | VAR_CERT_ISSUER | VAR_COMP_O },
{{"tls_my_subject_organization", sizeof("tls_my_subject_organization")-1},
850, tlsops_comp, 0,
0, 0, pv_init_iname, VAR_CERT_LOCAL | VAR_CERT_SUBJECT | VAR_COMP_O },
{{"tls_my_issuer_organization", sizeof("tls_my_issuer_organization")-1},
850, tlsops_comp, 0,
0, 0, pv_init_iname, VAR_CERT_LOCAL | VAR_CERT_ISSUER | VAR_COMP_O },
{{"tls_peer_subject_unit", sizeof("tls_peer_subject_unit")-1},
850, tlsops_comp, 0,
0, 0, pv_init_iname, VAR_CERT_PEER | VAR_CERT_SUBJECT | VAR_COMP_OU },
{{"tls_peer_issuer_unit", sizeof("tls_peer_issuer_unit")-1},
850, tlsops_comp, 0,
0, 0, pv_init_iname, VAR_CERT_PEER | VAR_CERT_ISSUER | VAR_COMP_OU },
{{"tls_my_subject_unit", sizeof("tls_my_subject_unit")-1},
850, tlsops_comp, 0,
0, 0, pv_init_iname, VAR_CERT_LOCAL | VAR_CERT_SUBJECT | VAR_COMP_OU },
{{"tls_my_subject_serial", sizeof("tls_my_subject_serial")-1},
850, tlsops_comp, 0,
0, 0, pv_init_iname, VAR_CERT_LOCAL | VAR_CERT_SUBJECT | VAR_COMP_SUBJECT_SERIAL },
{{"tls_peer_subject_serial", sizeof("tls_peer_subject_serial")-1},
850, tlsops_comp, 0,
0, 0, pv_init_iname, VAR_CERT_PEER | VAR_CERT_SUBJECT | VAR_COMP_SUBJECT_SERIAL },
{{"tls_my_issuer_unit", sizeof("tls_my_issuer_unit")-1},
850, tlsops_comp, 0,
0, 0, pv_init_iname, VAR_CERT_LOCAL | VAR_CERT_ISSUER | VAR_COMP_OU },
/* subject alternative name parameters for peer and local */
{{"tls_peer_san_email", sizeof("tls_peer_san_email")-1},
850, tlsops_alt, 0,
0, 0, pv_init_iname, VAR_CERT_PEER | VAR_COMP_E },
{{"tls_my_san_email", sizeof("tls_my_san_email")-1},
850, tlsops_alt, 0,
0, 0, pv_init_iname, VAR_CERT_LOCAL | VAR_COMP_E },
{{"tls_peer_san_hostname", sizeof("tls_peer_san_hostname")-1},
850, tlsops_alt, 0,
0, 0, pv_init_iname, VAR_CERT_PEER | VAR_COMP_HOST },
{{"tls_my_san_hostname", sizeof("tls_my_san_hostname")-1},
850, tlsops_alt, 0,
0, 0, pv_init_iname, VAR_CERT_LOCAL | VAR_COMP_HOST },
{{"tls_peer_san_uri", sizeof("tls_peer_san_uri")-1},
850, tlsops_alt, 0,
0, 0, pv_init_iname, VAR_CERT_PEER | VAR_COMP_URI },
{{"tls_my_san_uri", sizeof("tls_my_san_uri")-1},
850, tlsops_alt, 0,
0, 0, pv_init_iname, VAR_CERT_LOCAL | VAR_COMP_URI },
{{"tls_peer_san_ip", sizeof("tls_peer_san_ip")-1},
850, tlsops_alt, 0,
0, 0, pv_init_iname, VAR_CERT_PEER | VAR_COMP_IP },
{{"tls_my_san_ip", sizeof("tls_my_san_ip")-1},
850, tlsops_alt, 0,
0, 0, pv_init_iname, VAR_CERT_LOCAL | VAR_COMP_IP },
/* peer certificate validation parameters */
{{"tls_peer_verified", sizeof("tls_peer_verified")-1},
850, tlsops_check_cert, 0,
0, 0, pv_init_iname, VAR_CERT_VERIFIED },
{{"tls_peer_revoked", sizeof("tls_peer_revoked")-1},
850, tlsops_check_cert, 0,
0, 0, pv_init_iname, VAR_CERT_REVOKED },
{{"tls_peer_expired", sizeof("tls_peer_expired")-1},
850, tlsops_check_cert, 0,
0, 0, pv_init_iname, VAR_CERT_EXPIRED },
{{"tls_peer_selfsigned", sizeof("tls_peer_selfsigned")-1},
850, tlsops_check_cert, 0,
0, 0, pv_init_iname, VAR_CERT_SELFSIGNED },
{{"tls_peer_notBefore", sizeof("tls_peer_notBefore")-1},
850, tlsops_validity, 0,
0, 0, pv_init_iname, VAR_CERT_NOTBEFORE },
{{"tls_peer_notAfter", sizeof("tls_peer_notAfter")-1},
850, tlsops_validity, 0,
0, 0, pv_init_iname, VAR_CERT_NOTAFTER },
{ {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
};
static dep_export_t deps = {
{ /* OpenSIPS module dependencies */
{ MOD_TYPE_DEFAULT, "tls_openssl", DEP_SILENT },
{ MOD_TYPE_DEFAULT, "tls_wolfssl", DEP_SILENT },
{ MOD_TYPE_NULL, NULL, 0 },
},
{ /* modparam dependencies */
{ NULL, NULL },
},
};
struct module_exports exports = {
"tls_mgm", /* module name*/
MOD_TYPE_DEFAULT, /* class of this module */
MODULE_VERSION,
DEFAULT_DLFLAGS, /* dlopen flags */
0, /* load function */
&deps, /* OpenSIPS module dependencies */
cmds, /* exported functions */
0, /* exported async functions */
params, /* module parameters */
0, /* exported statistics */
mi_cmds, /* exported MI functions */
mod_items, /* exported pseudo-variables */
0, /* exported transformations */
0, /* extra processes */
0, /* module pre-initialization function */
mod_init, /* module initialization function */
0, /* response function */
mod_destroy,/* destroy function */
child_init, /* per-child init function */
0 /* reload confirm function */
};
/* loads data from the db */
int load_info(struct tls_domain **serv_dom, struct tls_domain **cli_dom,
struct tls_domain *script_srv_doms, struct tls_domain *script_cli_doms)
{
int int_vals[NO_INT_VALS];
char *str_vals[NO_STR_VALS];
str blob_vals[NO_BLOB_VALS];
int i, n;
int no_rows = 5;
int db_cols = NO_DB_COLS;
/* the columns from the db table */
db_key_t columns[NO_DB_COLS];
/* result from a db query */
db_res_t* res;
/* a row from the db table */
db_row_t* row;
res = 0;
columns[0] = &id_col;
columns[1] = &domain_col;
columns[2] = &match_address_col;
columns[3] = &match_domain_col;
columns[4] = &type_col;
columns[5] = &method_col;
columns[6] = &verify_cert_col;
columns[7] = &require_cert_col;
columns[8] = &certificate_col;
columns[9] = &pk_col;
columns[10] = &crl_check_col;
columns[11] = &crl_dir_col;
columns[12] = &calist_col;
columns[13] = &cadir_col;
columns[14] = &cplist_col;
columns[15] = &dhparams_col;
columns[16] = &eccurve_col;
/* checking if the table version is up to date*/
if (db_check_table_version(&dr_dbf, db_hdl, &tls_db_table, TLS_TABLE_VERSION) != 0)
goto error;
/* table to use*/
if (dr_dbf.use_table(db_hdl, &tls_db_table) < 0) {
LM_ERR("cannot select table \"%.*s\"\n", tls_db_table.len, tls_db_table.s);
goto error;
}
if (DB_CAPABILITY(dr_dbf, DB_CAP_FETCH)) {
if (dr_dbf.query(db_hdl, 0, 0, 0, columns, 0, db_cols, 0, 0) < 0) {
LM_ERR("DB query failed - retrieve valid connections \n");
goto error;
}
no_rows = estimate_available_rows(4 + 45 + 4 + 45 + 4 + 4 + 45 +
45 + 4 + 45 + 45 + 4 * 4096, db_cols);
if (no_rows == 0) no_rows = 5;
if (dr_dbf.fetch_result(db_hdl, &res, no_rows) < 0) {
LM_ERR("Error fetching rows\n");
goto error;
}
} else {
if (dr_dbf.query(db_hdl, 0, 0, 0, columns, 0, db_cols, 0, &res) < 0) {
LM_ERR("DB query failed - retrieve valid connections\n");
goto error;
}
}
LM_DBG("%d rows found in %.*s\n",
RES_ROW_N(res), tls_db_table.len, tls_db_table.s);
n = 0;
do {
for (i = 0; i < RES_ROW_N(res); i++) {
row = RES_ROWS(res) + i;
n++;
check_val(id_col, ROW_VALUES(row), DB_INT, 1, 0);
int_vals[INT_VALS_ID_COL] = VAL_INT(ROW_VALUES(row));
check_val(domain_col, ROW_VALUES(row) + 1, DB_STRING, 0, 0);
if (VAL_NULL(ROW_VALUES(row) + 1))
str_vals[STR_VALS_DOMAIN_COL] = 0;
else
str_vals[STR_VALS_DOMAIN_COL] = (char *) VAL_STRING(ROW_VALUES(row) + 1);
check_val(match_address_col, ROW_VALUES(row) + 2, DB_STRING, 0, 1);
if (VAL_NULL(ROW_VALUES(row) + 2))
str_vals[STR_VALS_MATCH_ADDRESS_COL] = 0;
else
str_vals[STR_VALS_MATCH_ADDRESS_COL] = (char *) VAL_STRING(ROW_VALUES(row) + 2);
check_val(match_domain_col, ROW_VALUES(row) + 3, DB_STRING, 0, 1);
if (VAL_NULL(ROW_VALUES(row) + 3))
str_vals[STR_VALS_MATCH_DOMAIN_COL] = 0;
else
str_vals[STR_VALS_MATCH_DOMAIN_COL] = (char *) VAL_STRING(ROW_VALUES(row) + 3);
check_val(type_col, ROW_VALUES(row) + 4, DB_INT, 1, 0);
int_vals[INT_VALS_TYPE_COL] = VAL_INT(ROW_VALUES(row) + 4);
check_val(method_col, ROW_VALUES(row) + 5, DB_STRING, 0, 0);
str_vals[STR_VALS_METHOD_COL] = (char *) VAL_STRING(ROW_VALUES(row) + 5);
check_val(verify_cert_col, ROW_VALUES(row) + 6, DB_INT, 0, 0);
int_vals[INT_VALS_VERIFY_CERT_COL] = VAL_INT(ROW_VALUES(row) + 6);
check_val(require_cert_col, ROW_VALUES(row) + 7, DB_INT, 0, 0);
int_vals[INT_VALS_REQUIRE_CERT_COL] = VAL_INT(ROW_VALUES(row) + 7);
check_val(certificate_col, ROW_VALUES(row) + 8, DB_BLOB, 0, 0);
blob_vals[BLOB_VALS_CERTIFICATE_COL] = VAL_BLOB(ROW_VALUES(row) + 8);
check_val(pk_col, ROW_VALUES(row) + 9, DB_BLOB, 0, 0);
blob_vals[BLOB_VALS_PK_COL] = VAL_BLOB(ROW_VALUES(row) + 9);
check_val(crl_check_col, ROW_VALUES(row) + 10, DB_INT, 0, 0);
int_vals[INT_VALS_CRL_CHECK_COL] = VAL_INT(ROW_VALUES(row) + 10);
check_val(crl_dir_col, ROW_VALUES(row) + 11, DB_STRING, 0, 0);
str_vals[STR_VALS_CRL_DIR_COL] = (char *) VAL_STRING(ROW_VALUES(row) + 11);
check_val(calist_col, ROW_VALUES(row) + 12, DB_BLOB, 0, 0);
blob_vals[BLOB_VALS_CALIST_COL] = VAL_BLOB(ROW_VALUES(row) + 12);
check_val(cadir_col, ROW_VALUES(row) + 13, DB_STRING, 0, 0);
str_vals[STR_VALS_CADIR_COL] = (char *) VAL_STRING(ROW_VALUES(row) + 13);
check_val(cplist_col, ROW_VALUES(row) + 14, DB_STRING, 0, 0);
str_vals[STR_VALS_CPLIST_COL] = (char *) VAL_STRING(ROW_VALUES(row) + 14);
check_val(dhparams_col, ROW_VALUES(row) + 15, DB_BLOB, 0, 0);
blob_vals[BLOB_VALS_DHPARAMS_COL] = VAL_BLOB(ROW_VALUES(row) + 15);
check_val(eccurve_col, ROW_VALUES(row) + 16, DB_STRING, 0, 0);
str_vals[STR_VALS_ECCURVE_COL] = (char *) VAL_STRING(ROW_VALUES(row) + 16);
if (db_add_domain(str_vals, int_vals, blob_vals, serv_dom, cli_dom,
script_srv_doms, script_cli_doms) < 0) {
if (str_vals[STR_VALS_DOMAIN_COL])
LM_ERR("failed to add TLS domain '%s' id: %d, skipping... \n",
str_vals[STR_VALS_DOMAIN_COL], int_vals[INT_VALS_ID_COL]);
else
LM_ERR("failed to add TLS domain id: %d, skipping... \n",
int_vals[INT_VALS_ID_COL]);
}
}
if (DB_CAPABILITY(dr_dbf, DB_CAP_FETCH)) {
if (dr_dbf.fetch_result(db_hdl, &res, no_rows) < 0) {
LM_ERR("fetching rows\n");
goto error;
}
} else {
break;
}
} while (RES_ROW_N(res) > 0);
LM_DBG("%d records found in %.*s\n",
n, tls_db_table.len, tls_db_table.s);
dr_dbf.free_result(db_hdl, res);
res = 0;
return 0;
error:
LM_ERR("Unable to load domains info from DB\n");
return -1;
}
int tls_sni_cb(struct tls_domain *dom, struct tcp_connection *c,
void *ssl_ctx, char *servername)
{
str srvname = {NULL, 0};
struct tls_domain *new_dom;
str match_no_sni = str_init(MATCH_NO_SNI_VAL);
str *match_val;
int rc;
srvname.s = servername;
if (!srvname.s)
match_val = &match_no_sni;
else {
srvname.len = strlen(servername);
match_val = &srvname;
}
new_dom = tls_find_domain_by_filters(&c->rcv.dst_ip, c->rcv.dst_port,
match_val, DOM_FLAG_SRV);
if (!new_dom) {
LM_INFO("No domain found matching host: %.*s in servername extension\n",
srvname.len, srvname.s);
return -2;
} else if (new_dom && new_dom != dom) {
/* switch SSL context to the one with the proper certificate
* for the indicated hostname */
if (tls_library == TLS_LIB_OPENSSL) {
rc = openssl_api.switch_ssl_ctx(new_dom, ssl_ctx);
} else if (tls_library == TLS_LIB_WOLFSSL) {
rc = wolfssl_api.switch_ssl_ctx(new_dom, ssl_ctx);
} else {
LM_CRIT("No TLS library module loaded\n");
tls_release_domain(dom);
return -1;
}
if (rc < 0) {
tls_release_domain(dom);
return -1;
}
tls_release_domain(dom);
LM_DBG("Switched to TLS server domain: %.*s due to SNI\n",
new_dom->name.len, new_dom->name.s);
return 0;
} else {
/* the originally matched domain is the correct one */
tls_release_domain(new_dom);
return 0;
}
}
void destroy_tls_dom(struct tls_domain *d)
{
if (tls_library == TLS_LIB_OPENSSL)
openssl_api.destroy_tls_dom(d);
else if (tls_library == TLS_LIB_WOLFSSL)
wolfssl_api.destroy_tls_dom(d);
}
static int init_tls_dom(struct tls_domain *d)
{
int init_flags = 0;
LM_INFO("Processing TLS domain '%.*s'\n",
d->name.len, ZSW(d->name.s));
if (!d->dh_param.s) {
init_flags |= TLS_DOM_DH_FILE_FL;
LM_DBG("no DH params file for tls domain '%.*s' defined, using default '%s'\n",
d->name.len, ZSW(d->name.s), tls_tmp_dh_file);
d->dh_param.s = tls_tmp_dh_file;
d->dh_param.len = len(tls_tmp_dh_file);
}
if( d->ciphers_list != 0 )
LM_NOTICE("setting cipher list to %s\n", d->ciphers_list);
else
LM_DBG( "cipher list null ... setting default\n");
/*
* set method
*/
if (!d->method_str.s) {
LM_DBG("no method for tls domain '%.*s', using default\n",
d->name.len, ZSW(d->name.s));
d->method = tls_default_method;
d->method_max = tls_default_method;
}
if (!d->cert.s) {
init_flags |= TLS_DOM_CERT_FILE_FL;
LM_NOTICE("no certificate for tls domain '%.*s' defined, using default '%s'\n",
d->name.len, ZSW(d->name.s), tls_cert_file);
d->cert.s = tls_cert_file;
d->cert.len = len(tls_cert_file);
}
if (!d->ca.s) {
init_flags |= TLS_DOM_CA_FILE_FL;
LM_NOTICE("no CA list for tls domain '%.*s' defined, using default '%s'\n",
d->name.len, ZSW(d->name.s), tls_ca_file);
d->ca.s = tls_ca_file;
d->ca.len = len(tls_ca_file);
}
/*
* load ca from directory
*/
if (!d->ca_directory) {
LM_NOTICE("no CA dir for tls '%.*s' defined, using default '%s'\n",
d->name.len, ZSW(d->name.s), tls_ca_dir);
d->ca_directory = tls_ca_dir;
}
if (!d->crl_directory)
LM_NOTICE("no crl for tls, using none\n");
if (tls_library == TLS_LIB_OPENSSL)
return openssl_api.init_tls_dom(d, init_flags);
else if (tls_library == TLS_LIB_WOLFSSL)
return wolfssl_api.init_tls_dom(d, init_flags);
else
return 0;
}
/*
* initialize tls virtual domains
*/
static int init_tls_domains(struct tls_domain **dom)
{
struct tls_domain *d, *tmp, *prev = NULL;
int from_file = 0;
int db = 0;
int rc;
d = *dom;
while (d) {
if (init_tls_dom(d) < 0) {
db = d->flags & DOM_FLAG_DB;
if (!db)
LM_ERR("Failed to init TLS domain '%.*s'\n", d->name.len, ZSW(d->name.s));
else
LM_WARN("Failed to init TLS domain '%.*s', skipping...\n",
d->name.len, ZSW(d->name.s));
if (d == *dom)
*dom = d->next;
if (prev)
prev->next = d->next;
tmp = d;
d = d->next;
tls_free_domain(tmp);
if (!db)
return -1;
} else {
prev = d;
d = d->next;
}
}
/*
* load all private keys as the last step (may prompt for password)
*/
d = *dom;
prev = NULL;
while (d) {
if (!d->pkey.s) {
LM_NOTICE("no private key for tls domain '%.*s' defined, using default '%s'\n",
d->name.len, ZSW(d->name.s), tls_pkey_file);
d->pkey.s = tls_pkey_file;
d->pkey.len = len(tls_pkey_file);
from_file = 1;
}
if (tls_library == TLS_LIB_OPENSSL)
rc = openssl_api.load_priv_key(d, from_file);
else if (tls_library == TLS_LIB_WOLFSSL)
rc = wolfssl_api.load_priv_key(d, from_file);
else {
prev = d;
d = d->next;
continue;
}
if (rc < 0) {
db = d->flags & DOM_FLAG_DB;
if (!db)
LM_ERR("Failed to init TLS domain '%.*s'\n", d->name.len, ZSW(d->name.s));
else
LM_WARN("Failed to init TLS domain '%.*s', skipping...\n",
d->name.len, ZSW(d->name.s));
if (d == *dom)
*dom = d->next;
if (prev)
prev->next = d->next;
tmp = d;
d = d->next;
tls_free_domain(tmp);
if (!db)
return -1;
} else {
prev = d;
d = d->next;
}
}
return 0;
}
static struct {
char *name;
enum tls_method method;
} ssl_versions_struct[] = {
{ "SSLv23", TLS_USE_SSLv23 },
{ "TLSv1", TLS_USE_TLSv1 },
{ "TLSv1_2", TLS_USE_TLSv1_2 },
{ "TLSv1_3", TLS_USE_TLSv1_3 },
};
#define SSL_VERSIONS_SIZE (sizeof(ssl_versions_struct)/sizeof(ssl_versions_struct[0]))
static inline char *get_ssl_method_name(enum tls_method method)
{
if (method < 1 || method > SSL_VERSIONS_SIZE)
return "UNKNOWN";
return ssl_versions_struct[method-1].name;
}
/* reloads data from the db */
static int reload_data(void)
{
struct tls_domain *tls_client_domains_tmp = NULL;
struct tls_domain *tls_server_domains_tmp = NULL;
struct tls_domain *script_cli_doms, *script_srv_doms, *dom;
script_srv_doms = find_first_script_dom(*tls_server_domains);
script_cli_doms = find_first_script_dom(*tls_client_domains);
/* load new domains from db */
if (load_info(&tls_server_domains_tmp, &tls_client_domains_tmp,
script_srv_doms, script_cli_doms) < 0)
return -1;
/*
* initialize new domains
*/
init_tls_domains(&tls_server_domains_tmp);
init_tls_domains(&tls_client_domains_tmp);
lock_start_write(dom_lock);
tls_free_db_domains(*tls_server_domains);
/* link the new DB domains with the existing script domains */
if (script_srv_doms) {
for (dom = tls_server_domains_tmp; dom; dom = dom->next)
if (!dom->next)
break;
if (dom)
dom->next = script_srv_doms;
}
if (tls_server_domains_tmp)
*tls_server_domains = tls_server_domains_tmp;
else
*tls_server_domains = script_srv_doms;
tls_free_db_domains(*tls_client_domains);
if (script_cli_doms) {
for (dom = tls_client_domains_tmp; dom; dom = dom->next)
if (!dom->next)
break;
if (dom)
dom->next = script_cli_doms;
}
if (tls_client_domains_tmp)
*tls_client_domains = tls_client_domains_tmp;
else
*tls_client_domains = script_cli_doms;
for (dom = *tls_server_domains; dom; dom = dom->next)
if (update_matching_map(dom) < 0) {
LM_ERR("Unable to update domain matching map\n");
return -1;
}
for (dom = *tls_client_domains; dom; dom = dom->next)
if (update_matching_map(dom) < 0) {
LM_ERR("Unable to update domain matching map\n");
return -1;
}
/* sort arrays of domain filters in order to be able to select the
* most specific domain first in case of definitions with wildcard patterns */
if (*tls_server_domains)
sort_map_dom_arrays(server_dom_matching);
if (*tls_client_domains)
sort_map_dom_arrays(client_dom_matching);
lock_stop_write(dom_lock);
return 0;
}
/* reloads data from the db */
static mi_response_t *tls_reload(const mi_params_t *params,
struct mi_handler *async_hdl)
{
LM_INFO("reload data MI command received!\n");
if (!tls_db_url.s)
return init_mi_error(500, MI_SSTR("DB url not set"));
if (reload_data() < 0) {
LM_ERR("failed to load tls data\n");
return init_mi_error(500, MI_SSTR("Failed to reload"));
}
return init_mi_result_ok();
}
static int load_tls_library(void)
{
int wolfssl_loaded, openssl_loaded;
tls_library = TLS_LIB_NONE;
openssl_loaded = module_loaded("tls_openssl");
wolfssl_loaded = module_loaded("tls_wolfssl");
tls_library_param.len = strlen(tls_library_param.s);
if (!str_strcmp(&tls_library_param, _str(TLS_LIB_AUTO_STR))) {
if (openssl_loaded)
tls_library = TLS_LIB_OPENSSL;
if (wolfssl_loaded) {
if (openssl_loaded) {
LM_ERR("Multiple TLS library modules loaded\n");
return -1;
}
tls_library = TLS_LIB_WOLFSSL;
}
if (tls_library == TLS_LIB_NONE) {
LM_ERR("No TLS library module loaded\n");
return -1;
}
} else if (!str_strcmp(&tls_library_param, _str(TLS_LIB_NONE_STR))) {
LM_INFO("No TLS library configured\n");
} else if (!str_strcmp(&tls_library_param, _str(TLS_LIB_OPENSSL_STR))) {
if (!openssl_loaded) {
LM_ERR("Configured to use openssl library but 'tls_openssl' "
"module not loaded!\n");
return -1;
}
tls_library = TLS_LIB_OPENSSL;
} else if (!str_strcmp(&tls_library_param, _str(TLS_LIB_WOLFSSL_STR))) {
if (!wolfssl_loaded) {
LM_ERR("Configured to use wolfSSL library but 'tls_wolfssl' "
"module not loaded!\n");
return -1;
}
tls_library = TLS_LIB_WOLFSSL;
} else {
LM_ERR("Bad value for tls_library module parameter\n");
return -1;
}
if (tls_library == TLS_LIB_OPENSSL) {
if (load_tls_openssl_api(&openssl_api)) {
LM_DBG("Failed to load openssl API\n");
return -1;
}
openssl_api.reg_tls_sni_cb(tls_sni_cb);
} else if (tls_library == TLS_LIB_WOLFSSL) {
if (load_tls_wolfssl_api(&wolfssl_api)) {
LM_DBG("Failed to load wolfSSL API\n");
return -1;
}
wolfssl_api.reg_tls_sni_cb(tls_sni_cb);
}
return 0;
}
static int mod_init(void) {
str s;
str tls_db_param = str_init(DB_TLS_DOMAIN_PARAM_EQ);
struct tls_domain *tls_client_domains_tmp = NULL;
struct tls_domain *tls_server_domains_tmp = NULL;
struct tls_domain *dom;
LM_INFO("initializing TLS management\n");
if (load_tls_library() < 0)
return -1;
if (tls_db_url.s) {
/* create & init lock */
if ((dom_lock = lock_init_rw()) == NULL) {
LM_CRIT("failed to init lock\n");
return -1;
}
init_db_url(tls_db_url, 0 /*cannot be null*/);
if (str_strstr(&tls_db_url, &tls_db_param)) {
/* this would cause a circular dependency between the DB module and tls_mgm */
LM_CRIT("Cannot use a TLS connection to DB for the tls_mgm module itself\n");
return -1;
}
tls_db_table.len = strlen(tls_db_table.s);
if (tls_db_table.len == 0) {
LM_ERR("db table not specified\n");
return -1;
}
id_col.len = strlen(id_col.s);
domain_col.len = strlen(domain_col.s);
match_address_col.len = strlen(match_address_col.s);
match_domain_col.len = strlen(match_domain_col.s);
type_col.len = strlen(type_col.s);
method_col.len = strlen(method_col.s);
verify_cert_col.len = strlen(verify_cert_col.s);
require_cert_col.len = strlen(require_cert_col.s);
certificate_col.len = strlen(certificate_col.s);
pk_col.len = strlen(pk_col.s);
crl_check_col.len = strlen(crl_check_col.s);
calist_col.len = strlen(calist_col.s);