-
Notifications
You must be signed in to change notification settings - Fork 188
/
mod_tls.c
10939 lines (8763 loc) · 310 KB
/
mod_tls.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
/*
* mod_tls - An RFC2228 SSL/TLS module for ProFTPD
*
* Copyright (c) 2000-2002 Peter 'Luna' Runestig <peter@runestig.com>
* Copyright (c) 2002-2015 TJ Saunders <tj@castaglia.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modifi-
* cation, are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright no-
* tice, this list of conditions and the following disclaimer in the do-
* cumentation and/or other materials provided with the distribution.
*
* o The names of the contributors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LI-
* ABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUEN-
* TIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEV-
* ER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABI-
* LITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* --- DO NOT DELETE BELOW THIS LINE ----
* $Libraries: -lssl -lcrypto$
*/
#include "conf.h"
#include "privs.h"
#include "mod_tls.h"
#ifdef PR_USE_CTRLS
# include "mod_ctrls.h"
#endif
/* Note that the openssl/ssl.h header is already included in mod_tls.h, so
* we don't need to include it here.
*/
#include <openssl/err.h>
#include <openssl/crypto.h>
#include <openssl/evp.h>
#include <openssl/x509v3.h>
#include <openssl/pkcs12.h>
#include <openssl/rand.h>
#if OPENSSL_VERSION_NUMBER > 0x000907000L
# include <openssl/engine.h>
# include <openssl/ocsp.h>
#endif
#ifdef PR_USE_OPENSSL_ECC
# include <openssl/ec.h>
# include <openssl/ecdh.h>
#endif /* PR_USE_OPENSSL_ECC */
#ifdef HAVE_MLOCK
# include <sys/mman.h>
#endif
#define MOD_TLS_VERSION "mod_tls/2.6"
/* Make sure the version of proftpd is as necessary. */
#if PROFTPD_VERSION_NUMBER < 0x0001030504
# error "ProFTPD 1.3.5rc4 or later required"
#endif
extern session_t session;
extern xaset_t *server_list;
/* DH parameters. These are generated using:
*
* # openssl dhparam -2|-5 512|768|1024|1536|2048 -C
*
* These should be regenerated periodically by the mod_tls maintainer.
* Last updated on 2013-01-13.
*/
/*
-----BEGIN DH PARAMETERS-----
MEYCQQC6VcB8+WFeFz/HQnk/vXreozxdppNBY4gN8rdzitjTDppPBswzU4ZL/hBS
e1Crmu4uTtpPNxVVA+g3FyB/GfUTAgEF
-----END DH PARAMETERS-----
*/
static unsigned char dh512_p[] = {
0xBA,0x55,0xC0,0x7C,0xF9,0x61,0x5E,0x17,0x3F,0xC7,0x42,0x79,
0x3F,0xBD,0x7A,0xDE,0xA3,0x3C,0x5D,0xA6,0x93,0x41,0x63,0x88,
0x0D,0xF2,0xB7,0x73,0x8A,0xD8,0xD3,0x0E,0x9A,0x4F,0x06,0xCC,
0x33,0x53,0x86,0x4B,0xFE,0x10,0x52,0x7B,0x50,0xAB,0x9A,0xEE,
0x2E,0x4E,0xDA,0x4F,0x37,0x15,0x55,0x03,0xE8,0x37,0x17,0x20,
0x7F,0x19,0xF5,0x13,
};
static unsigned char dh512_g[] = {
0x05,
};
static DH *get_dh512(void) {
DH *dh;
dh = DH_new();
if (dh == NULL)
return NULL;
dh->p = BN_bin2bn(dh512_p, sizeof(dh512_p), NULL);
dh->g = BN_bin2bn(dh512_g, sizeof(dh512_g), NULL);
if (dh->p == NULL ||
dh->g == NULL) {
DH_free(dh);
return NULL;
}
return dh;
}
/*
-----BEGIN DH PARAMETERS-----
MGYCYQDhEinscQZuuGMmmzoYLuNHbA8rZmDilZjB8RasGJ1Mo8knn9tFLtRGHVxw
IxUaxkQgeqAy2FjQFz2Z9hU3a1dCgb3VEgRPDXqMSW8t4UsVrHN/AMqszID/PZb+
VTp+9osCAQI=
-----END DH PARAMETERS-----
*/
static unsigned char dh768_p[] = {
0xE1,0x12,0x29,0xEC,0x71,0x06,0x6E,0xB8,0x63,0x26,0x9B,0x3A,
0x18,0x2E,0xE3,0x47,0x6C,0x0F,0x2B,0x66,0x60,0xE2,0x95,0x98,
0xC1,0xF1,0x16,0xAC,0x18,0x9D,0x4C,0xA3,0xC9,0x27,0x9F,0xDB,
0x45,0x2E,0xD4,0x46,0x1D,0x5C,0x70,0x23,0x15,0x1A,0xC6,0x44,
0x20,0x7A,0xA0,0x32,0xD8,0x58,0xD0,0x17,0x3D,0x99,0xF6,0x15,
0x37,0x6B,0x57,0x42,0x81,0xBD,0xD5,0x12,0x04,0x4F,0x0D,0x7A,
0x8C,0x49,0x6F,0x2D,0xE1,0x4B,0x15,0xAC,0x73,0x7F,0x00,0xCA,
0xAC,0xCC,0x80,0xFF,0x3D,0x96,0xFE,0x55,0x3A,0x7E,0xF6,0x8B,
};
static unsigned char dh768_g[] = {
0x02,
};
static DH *get_dh768(void) {
DH *dh;
dh = DH_new();
if (dh == NULL)
return NULL;
dh->p = BN_bin2bn(dh768_p, sizeof(dh768_p), NULL);
dh->g = BN_bin2bn(dh768_g, sizeof(dh768_g), NULL);
if (dh->p == NULL ||
dh->g == NULL) {
DH_free(dh);
return NULL;
}
return dh;
}
/*
-----BEGIN DH PARAMETERS-----
MIGHAoGBAPLQY9o7o6wDAUOW4OYq+Cp9j0cnN2qgkdCDQTMHHdqotbF7TiYv3iuP
a6Qu1WwNyVoa9k7vyzfW7ZxvLLChUBvTFZ1gBAKlVpvhq/XmWHLwXY4Q/auCrHjR
j6VNC3D6sD68viO0Cqf8yWjBGwdiXkW6tMNuz1iPS4/KZD09QJrDAgEC
-----END DH PARAMETERS-----
*/
static unsigned char dh1024_p[] = {
0xF2,0xD0,0x63,0xDA,0x3B,0xA3,0xAC,0x03,0x01,0x43,0x96,0xE0,
0xE6,0x2A,0xF8,0x2A,0x7D,0x8F,0x47,0x27,0x37,0x6A,0xA0,0x91,
0xD0,0x83,0x41,0x33,0x07,0x1D,0xDA,0xA8,0xB5,0xB1,0x7B,0x4E,
0x26,0x2F,0xDE,0x2B,0x8F,0x6B,0xA4,0x2E,0xD5,0x6C,0x0D,0xC9,
0x5A,0x1A,0xF6,0x4E,0xEF,0xCB,0x37,0xD6,0xED,0x9C,0x6F,0x2C,
0xB0,0xA1,0x50,0x1B,0xD3,0x15,0x9D,0x60,0x04,0x02,0xA5,0x56,
0x9B,0xE1,0xAB,0xF5,0xE6,0x58,0x72,0xF0,0x5D,0x8E,0x10,0xFD,
0xAB,0x82,0xAC,0x78,0xD1,0x8F,0xA5,0x4D,0x0B,0x70,0xFA,0xB0,
0x3E,0xBC,0xBE,0x23,0xB4,0x0A,0xA7,0xFC,0xC9,0x68,0xC1,0x1B,
0x07,0x62,0x5E,0x45,0xBA,0xB4,0xC3,0x6E,0xCF,0x58,0x8F,0x4B,
0x8F,0xCA,0x64,0x3D,0x3D,0x40,0x9A,0xC3,
};
static unsigned char dh1024_g[] = {
0x02,
};
static DH *get_dh1024(void) {
DH *dh;
dh = DH_new();
if (dh == NULL)
return NULL;
dh->p = BN_bin2bn(dh1024_p, sizeof(dh1024_p), NULL);
dh->g = BN_bin2bn(dh1024_g, sizeof(dh1024_g), NULL);
if (dh->p == NULL ||
dh->g == NULL) {
DH_free(dh);
return NULL;
}
return(dh);
}
/*
-----BEGIN DH PARAMETERS-----
MIHHAoHBAJSrJ66X9HpCRRUSuODRIhkQCFiJbNthRZclqtVRqJxIU6rpIkRD1L/A
lPlja3mSNiVwn37oOHP5lujp7e6mLkCj+1QSuStZgDg4MMFZK0vEyneCVZa/4HzN
tCZ8mdTcBU/HVOhkkFckJBzX6LVTd1DDrNOvdu5j+zKbMlgzmz+/YRHlPlEVrzSc
Jp3CM6YLxHbTa0NXIjlzHWjQEtMw6UDsrDJLDnU8dMbMSQorTTpBY/O/eeXeA05x
5cXAtpNOMwIBBQ==
-----END DH PARAMETERS-----
*/
static unsigned char dh1536_p[] = {
0x94,0xAB,0x27,0xAE,0x97,0xF4,0x7A,0x42,0x45,0x15,0x12,0xB8,
0xE0,0xD1,0x22,0x19,0x10,0x08,0x58,0x89,0x6C,0xDB,0x61,0x45,
0x97,0x25,0xAA,0xD5,0x51,0xA8,0x9C,0x48,0x53,0xAA,0xE9,0x22,
0x44,0x43,0xD4,0xBF,0xC0,0x94,0xF9,0x63,0x6B,0x79,0x92,0x36,
0x25,0x70,0x9F,0x7E,0xE8,0x38,0x73,0xF9,0x96,0xE8,0xE9,0xED,
0xEE,0xA6,0x2E,0x40,0xA3,0xFB,0x54,0x12,0xB9,0x2B,0x59,0x80,
0x38,0x38,0x30,0xC1,0x59,0x2B,0x4B,0xC4,0xCA,0x77,0x82,0x55,
0x96,0xBF,0xE0,0x7C,0xCD,0xB4,0x26,0x7C,0x99,0xD4,0xDC,0x05,
0x4F,0xC7,0x54,0xE8,0x64,0x90,0x57,0x24,0x24,0x1C,0xD7,0xE8,
0xB5,0x53,0x77,0x50,0xC3,0xAC,0xD3,0xAF,0x76,0xEE,0x63,0xFB,
0x32,0x9B,0x32,0x58,0x33,0x9B,0x3F,0xBF,0x61,0x11,0xE5,0x3E,
0x51,0x15,0xAF,0x34,0x9C,0x26,0x9D,0xC2,0x33,0xA6,0x0B,0xC4,
0x76,0xD3,0x6B,0x43,0x57,0x22,0x39,0x73,0x1D,0x68,0xD0,0x12,
0xD3,0x30,0xE9,0x40,0xEC,0xAC,0x32,0x4B,0x0E,0x75,0x3C,0x74,
0xC6,0xCC,0x49,0x0A,0x2B,0x4D,0x3A,0x41,0x63,0xF3,0xBF,0x79,
0xE5,0xDE,0x03,0x4E,0x71,0xE5,0xC5,0xC0,0xB6,0x93,0x4E,0x33,
};
static unsigned char dh1536_g[] = {
0x05,
};
static DH *get_dh1536(void) {
DH *dh;
dh = DH_new();
if (dh == NULL)
return NULL;
dh->p = BN_bin2bn(dh1536_p, sizeof(dh1536_p), NULL);
dh->g = BN_bin2bn(dh1536_g, sizeof(dh1536_g), NULL);
if (dh->p == NULL ||
dh->g == NULL) {
DH_free(dh);
return NULL;
}
return dh;
}
/*
-----BEGIN DH PARAMETERS-----
MIIBCAKCAQEA8uoKASu5Z9sdFVdEvpQOhZvbpHT7a+ZEKrUu+FRnA9vzK3uGn6gk
GwrLE/wcWcxcLO56mAY91kiordKHZYTW8KYq6416bA3JrOtBwiZveSAXG6pa+SSk
g3Dn6iK2rMado8s2y1MTUYQDQ8LsqnYOrHv551fP0kMq7v9bV0rr90bF54P54RFd
VKMx82r76n5gEtsVNsVKbTabob3wZVjdCCIlSVpumGYWJXbu2zFPF03taSP8zDGr
Z9jCVY+cEoU4z1hrWgHTtovfhoW8i1ULNrGfOOcdMzGhK/VtuIU6RMKqfC6OhmZL
Md/fa3Ip5joGRWGnl2oEQEK29ARJxG7UAwIBBQ==
-----END DH PARAMETERS-----
*/
static unsigned char dh2048_p[] = {
0xF2,0xEA,0x0A,0x01,0x2B,0xB9,0x67,0xDB,0x1D,0x15,0x57,0x44,
0xBE,0x94,0x0E,0x85,0x9B,0xDB,0xA4,0x74,0xFB,0x6B,0xE6,0x44,
0x2A,0xB5,0x2E,0xF8,0x54,0x67,0x03,0xDB,0xF3,0x2B,0x7B,0x86,
0x9F,0xA8,0x24,0x1B,0x0A,0xCB,0x13,0xFC,0x1C,0x59,0xCC,0x5C,
0x2C,0xEE,0x7A,0x98,0x06,0x3D,0xD6,0x48,0xA8,0xAD,0xD2,0x87,
0x65,0x84,0xD6,0xF0,0xA6,0x2A,0xEB,0x8D,0x7A,0x6C,0x0D,0xC9,
0xAC,0xEB,0x41,0xC2,0x26,0x6F,0x79,0x20,0x17,0x1B,0xAA,0x5A,
0xF9,0x24,0xA4,0x83,0x70,0xE7,0xEA,0x22,0xB6,0xAC,0xC6,0x9D,
0xA3,0xCB,0x36,0xCB,0x53,0x13,0x51,0x84,0x03,0x43,0xC2,0xEC,
0xAA,0x76,0x0E,0xAC,0x7B,0xF9,0xE7,0x57,0xCF,0xD2,0x43,0x2A,
0xEE,0xFF,0x5B,0x57,0x4A,0xEB,0xF7,0x46,0xC5,0xE7,0x83,0xF9,
0xE1,0x11,0x5D,0x54,0xA3,0x31,0xF3,0x6A,0xFB,0xEA,0x7E,0x60,
0x12,0xDB,0x15,0x36,0xC5,0x4A,0x6D,0x36,0x9B,0xA1,0xBD,0xF0,
0x65,0x58,0xDD,0x08,0x22,0x25,0x49,0x5A,0x6E,0x98,0x66,0x16,
0x25,0x76,0xEE,0xDB,0x31,0x4F,0x17,0x4D,0xED,0x69,0x23,0xFC,
0xCC,0x31,0xAB,0x67,0xD8,0xC2,0x55,0x8F,0x9C,0x12,0x85,0x38,
0xCF,0x58,0x6B,0x5A,0x01,0xD3,0xB6,0x8B,0xDF,0x86,0x85,0xBC,
0x8B,0x55,0x0B,0x36,0xB1,0x9F,0x38,0xE7,0x1D,0x33,0x31,0xA1,
0x2B,0xF5,0x6D,0xB8,0x85,0x3A,0x44,0xC2,0xAA,0x7C,0x2E,0x8E,
0x86,0x66,0x4B,0x31,0xDF,0xDF,0x6B,0x72,0x29,0xE6,0x3A,0x06,
0x45,0x61,0xA7,0x97,0x6A,0x04,0x40,0x42,0xB6,0xF4,0x04,0x49,
0xC4,0x6E,0xD4,0x03,
};
static unsigned char dh2048_g[] = {
0x05,
};
static DH *get_dh2048(void) {
DH *dh;
dh = DH_new();
if (dh == NULL)
return NULL;
dh->p = BN_bin2bn(dh2048_p, sizeof(dh2048_p), NULL);
dh->g = BN_bin2bn(dh2048_g, sizeof(dh2048_g), NULL);
if (dh->p == NULL ||
dh->g == NULL) {
DH_free(dh);
return NULL;
}
return dh;
}
/* ASN1_BIT_STRING_cmp was renamed in 0.9.5 */
#if OPENSSL_VERSION_NUMBER < 0x00905100L
# define M_ASN1_BIT_STRING_cmp ASN1_BIT_STRING_cmp
#endif
/* From src/dirtree.c */
extern int ServerUseReverseDNS;
module tls_module;
struct tls_next_proto {
const char *proto;
unsigned char *encoded_proto;
unsigned int encoded_protolen;
};
typedef struct tls_pkey_obj {
struct tls_pkey_obj *next;
size_t pkeysz;
char *rsa_pkey;
void *rsa_pkey_ptr;
char *dsa_pkey;
void *dsa_pkey_ptr;
#ifdef PR_USE_OPENSSL_ECC
char *ec_pkey;
void *ec_pkey_ptr;
#endif /* PR_USE_OPENSSL_ECC */
/* Used for stashing the password for a PKCS12 file, which should
* contain a certificate. Any passphrase for the private key for that
* certificate should be in one of the above RSA/DSA buffers.
*/
char *pkcs12_passwd;
void *pkcs12_passwd_ptr;
unsigned int flags;
server_rec *server;
} tls_pkey_t;
#define TLS_PKEY_USE_RSA 0x0100
#define TLS_PKEY_USE_DSA 0x0200
#define TLS_PKEY_USE_EC 0x0400
static tls_pkey_t *tls_pkey_list = NULL;
static unsigned int tls_npkeys = 0;
#define TLS_DEFAULT_CIPHER_SUITE "DEFAULT:!ADH:!EXPORT:!DES"
#define TLS_DEFAULT_NEXT_PROTO "ftp"
/* SSL record/buffer sizes */
#define TLS_DATA_WRITE_BUFFER_SIZE (16 * 1024)
#define TLS_HANDSHAKE_WRITE_BUFFER_SIZE 1400
/* Module variables */
#if OPENSSL_VERSION_NUMBER > 0x000907000L
static const char *tls_crypto_device = NULL;
#endif
static unsigned char tls_engine = FALSE;
static unsigned long tls_flags = 0UL, tls_opts = 0UL;
static tls_pkey_t *tls_pkey = NULL;
static int tls_logfd = -1;
static char *tls_passphrase_provider = NULL;
#define TLS_PASSPHRASE_TIMEOUT 10
#define TLS_PASSPHRASE_FL_RSA_KEY 0x0001
#define TLS_PASSPHRASE_FL_DSA_KEY 0x0002
#define TLS_PASSPHRASE_FL_PKCS12_PASSWD 0x0004
#define TLS_PASSPHRASE_FL_EC_KEY 0x0008
#define TLS_PROTO_SSL_V3 0x0001
#define TLS_PROTO_TLS_V1 0x0002
#define TLS_PROTO_TLS_V1_1 0x0004
#define TLS_PROTO_TLS_V1_2 0x0008
#if OPENSSL_VERSION_NUMBER >= 0x10001000L
# define TLS_PROTO_DEFAULT (TLS_PROTO_TLS_V1|TLS_PROTO_TLS_V1_1|TLS_PROTO_TLS_V1_2)
#else
# define TLS_PROTO_DEFAULT (TLS_PROTO_TLS_V1)
#endif /* OpenSSL 1.0.1 or later */
#ifdef SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
static int tls_ssl_opts = (SSL_OP_ALL|SSL_OP_NO_SSLv2|SSL_OP_SINGLE_DH_USE)^SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS;
#else
/* OpenSSL-0.9.6 and earlier (yes, it appears people still have these versions
* installed) does not define the DONT_INSERT_EMPTY_FRAGMENTS option.
*/
static int tls_ssl_opts = SSL_OP_ALL|SSL_OP_NO_SSLv2|SSL_OP_SINGLE_DH_USE;
#endif
static int tls_required_on_auth = 0;
static int tls_required_on_ctrl = 0;
static int tls_required_on_data = 0;
static unsigned char *tls_authenticated = NULL;
/* Define the minimum DH group length we allow (unless the AllowWeakDH
* TLSOption is used). Ideally this would be 2048, per https://weakdh.org,
* but for compatibility with older Java versions, which only support up to
* 1024, we'll use 1024. For now.
*/
#define TLS_DH_MIN_LEN 1024
/* mod_tls session flags */
#define TLS_SESS_ON_CTRL 0x0001
#define TLS_SESS_ON_DATA 0x0002
#define TLS_SESS_PBSZ_OK 0x0004
#define TLS_SESS_TLS_REQUIRED 0x0010
#define TLS_SESS_VERIFY_CLIENT 0x0020
#define TLS_SESS_NO_PASSWD_NEEDED 0x0040
#define TLS_SESS_NEED_DATA_PROT 0x0100
#define TLS_SESS_CTRL_RENEGOTIATING 0x0200
#define TLS_SESS_DATA_RENEGOTIATING 0x0400
#define TLS_SESS_HAVE_CCC 0x0800
#define TLS_SESS_VERIFY_SERVER 0x1000
#define TLS_SESS_VERIFY_SERVER_NO_DNS 0x2000
/* mod_tls option flags */
#define TLS_OPT_NO_CERT_REQUEST 0x0001
#define TLS_OPT_VERIFY_CERT_FQDN 0x0002
#define TLS_OPT_VERIFY_CERT_IP_ADDR 0x0004
#define TLS_OPT_ALLOW_DOT_LOGIN 0x0008
#define TLS_OPT_EXPORT_CERT_DATA 0x0010
#define TLS_OPT_STD_ENV_VARS 0x0020
#define TLS_OPT_ALLOW_PER_USER 0x0040
#define TLS_OPT_ENABLE_DIAGS 0x0080
#define TLS_OPT_NO_SESSION_REUSE_REQUIRED 0x0100
#define TLS_OPT_USE_IMPLICIT_SSL 0x0200
#define TLS_OPT_ALLOW_CLIENT_RENEGOTIATIONS 0x0400
#define TLS_OPT_VERIFY_CERT_CN 0x0800
#define TLS_OPT_NO_AUTO_ECDH 0x1000
#define TLS_OPT_ALLOW_WEAK_DH 0x2000
/* mod_tls SSCN modes */
#define TLS_SSCN_MODE_SERVER 0
#define TLS_SSCN_MODE_CLIENT 1
static unsigned int tls_sscn_mode = TLS_SSCN_MODE_SERVER;
/* mod_tls cleanup flags */
#define TLS_CLEANUP_FL_SESS_INIT 0x0001
/* mod_tls OCSP constants */
#define TLS_OCSP_RESP_MAX_AGE_SECS 300
static char *tls_cipher_suite = NULL;
static char *tls_crl_file = NULL, *tls_crl_path = NULL;
static char *tls_ec_cert_file = NULL, *tls_ec_key_file = NULL;
static char *tls_dsa_cert_file = NULL, *tls_dsa_key_file = NULL;
static char *tls_pkcs12_file = NULL;
static char *tls_rsa_cert_file = NULL, *tls_rsa_key_file = NULL;
static char *tls_rand_file = NULL;
#if defined(PSK_MAX_PSK_LEN)
static pr_table_t *tls_psks = NULL;
# define TLS_MIN_PSK_LEN 20
#endif /* PSK support */
/* Timeout given for TLS handshakes. The default is 5 minutes. */
static unsigned int tls_handshake_timeout = 300;
static unsigned char tls_handshake_timed_out = FALSE;
static int tls_handshake_timer_id = -1;
/* Note: 9 is the default OpenSSL depth. */
static int tls_verify_depth = 9;
#if OPENSSL_VERSION_NUMBER > 0x000907000L
/* Renegotiate control channel on TLS sessions after 4 hours, by default. */
static int tls_ctrl_renegotiate_timeout = 14400;
/* Renegotiate data channel on TLS sessions after 1 gigabyte, by default. */
static off_t tls_data_renegotiate_limit = 1024 * 1024 * 1024;
/* Timeout given for renegotiations to occur before the TLS session is
* shutdown. The default is 30 seconds.
*/
static int tls_renegotiate_timeout = 30;
/* Is client acceptance of a requested renegotiation required? */
static unsigned char tls_renegotiate_required = TRUE;
#endif
#define TLS_NETIO_NOTE "mod_tls.SSL"
static pr_netio_t *tls_ctrl_netio = NULL;
static pr_netio_stream_t *tls_ctrl_rd_nstrm = NULL;
static pr_netio_stream_t *tls_ctrl_wr_nstrm = NULL;
static pr_netio_t *tls_data_netio = NULL;
static pr_netio_stream_t *tls_data_rd_nstrm = NULL;
static pr_netio_stream_t *tls_data_wr_nstrm = NULL;
static tls_sess_cache_t *tls_sess_cache = NULL;
/* OpenSSL variables */
static SSL *ctrl_ssl = NULL;
static SSL_CTX *ssl_ctx = NULL;
static X509_STORE *tls_crl_store = NULL;
static array_header *tls_tmp_dhs = NULL;
static RSA *tls_tmp_rsa = NULL;
static int tls_sess_init(void);
/* SSL/TLS support functions */
static void tls_closelog(void);
static void tls_end_sess(SSL *, int, int);
#define TLS_SHUTDOWN_BIDIRECTIONAL 0x0001
static void tls_fatal_error(long, int);
static const char *tls_get_errors(void);
static char *tls_get_page(size_t, void **);
static size_t tls_get_pagesz(void);
static int tls_get_passphrase(server_rec *, const char *, const char *,
char *, size_t, int);
static char *tls_get_subj_name(SSL *);
static int tls_openlog(void);
static int tls_seed_prng(void);
static void tls_setup_environ(SSL *);
static int tls_verify_cb(int, X509_STORE_CTX *);
static int tls_verify_crl(int, X509_STORE_CTX *);
static int tls_verify_ocsp(int, X509_STORE_CTX *);
static char *tls_x509_name_oneline(X509_NAME *);
static int tls_readmore(int);
static int tls_writemore(int);
/* Session cache API */
static tls_sess_cache_t *tls_sess_cache_get_cache(const char *);
static long tls_sess_cache_get_cache_mode(void);
static int tls_sess_cache_open(char *, long);
static int tls_sess_cache_close(void);
#ifdef PR_USE_CTRLS
static int tls_sess_cache_clear(void);
static int tls_sess_cache_remove(void);
static int tls_sess_cache_status(pr_ctrls_t *, int);
#endif /* PR_USE_CTRLS */
static int tls_sess_cache_add_sess_cb(SSL *, SSL_SESSION *);
static SSL_SESSION *tls_sess_cache_get_sess_cb(SSL *, unsigned char *, int,
int *);
static void tls_sess_cache_delete_sess_cb(SSL_CTX *, SSL_SESSION *);
#ifdef PR_USE_CTRLS
static pool *tls_act_pool = NULL;
static ctrls_acttab_t tls_acttab[];
#endif /* PR_USE_CTRLS */
static int tls_ctrl_need_init_handshake = TRUE;
static int tls_data_need_init_handshake = TRUE;
static const char *trace_channel = "tls";
static const char *timing_channel = "timing";
static void tls_diags_cb(const SSL *ssl, int where, int ret) {
const char *str = "(unknown)";
int w;
pr_signals_handle();
w = where & ~SSL_ST_MASK;
if (w & SSL_ST_CONNECT) {
str = "connecting";
} else if (w & SSL_ST_ACCEPT) {
str = "accepting";
} else {
int ssl_state;
ssl_state = SSL_get_state(ssl);
if (ssl_state == SSL_ST_OK) {
str = "ok";
}
}
if (where & SSL_CB_ACCEPT_LOOP) {
int ssl_state;
ssl_state = SSL_get_state(ssl);
if (ssl_state == SSL3_ST_SR_CLNT_HELLO_A ||
ssl_state == SSL23_ST_SR_CLNT_HELLO_A) {
/* If we have already completed our initial handshake, then this might
* a session renegotiation.
*/
if ((ssl == ctrl_ssl && !tls_ctrl_need_init_handshake) ||
(ssl != ctrl_ssl && !tls_data_need_init_handshake)) {
/* Yes, this is indeed a session renegotiation. If it's a
* renegotiation that we requested, allow it. If it is from a
* data connection, allow it. Otherwise, it's a client-initiated
* renegotiation, and we probably don't want to allow it.
*/
if (ssl == ctrl_ssl &&
!(tls_flags & TLS_SESS_CTRL_RENEGOTIATING) &&
!(tls_flags & TLS_SESS_DATA_RENEGOTIATING)) {
if (!(tls_opts & TLS_OPT_ALLOW_CLIENT_RENEGOTIATIONS)) {
tls_log("warning: client-initiated session renegotiation "
"detected, aborting connection");
pr_log_pri(PR_LOG_NOTICE, MOD_TLS_VERSION
": client-initiated session renegotiation detected, "
"aborting connection");
tls_end_sess(ctrl_ssl, PR_NETIO_STRM_CTRL, 0);
pr_table_remove(tls_ctrl_rd_nstrm->notes, TLS_NETIO_NOTE, NULL);
pr_table_remove(tls_ctrl_wr_nstrm->notes, TLS_NETIO_NOTE, NULL);
ctrl_ssl = NULL;
pr_session_disconnect(&tls_module, PR_SESS_DISCONNECT_CONFIG_ACL,
"TLSOption AllowClientRenegotiations");
}
}
}
#if OPENSSL_VERSION_NUMBER >= 0x009080cfL
} else if (ssl_state & SSL_ST_RENEGOTIATE) {
if ((ssl == ctrl_ssl && !tls_ctrl_need_init_handshake) ||
(ssl != ctrl_ssl && !tls_data_need_init_handshake)) {
if (ssl == ctrl_ssl &&
!(tls_flags & TLS_SESS_CTRL_RENEGOTIATING) &&
!(tls_flags & TLS_SESS_DATA_RENEGOTIATING)) {
/* In OpenSSL-0.9.8l and later, SSL session renegotiations are
* automatically disabled. Thus if the admin has not explicitly
* configured support for client-initiated renegotations via the
* AllowClientRenegotiations TLSOption, then we need to disconnect
* the client here. Otherwise, the client would hang (up to the
* TLSTimeoutHandshake limit). Since we know, right now, that the
* handshake won't succeed, just drop the connection.
*/
if (!(tls_opts & TLS_OPT_ALLOW_CLIENT_RENEGOTIATIONS)) {
tls_log("warning: client-initiated session renegotiation detected, "
"aborting connection");
pr_log_pri(PR_LOG_NOTICE, MOD_TLS_VERSION
": client-initiated session renegotiation detected, "
"aborting connection");
tls_end_sess(ctrl_ssl, PR_NETIO_STRM_CTRL, 0);
pr_table_remove(tls_ctrl_rd_nstrm->notes, TLS_NETIO_NOTE, NULL);
pr_table_remove(tls_ctrl_wr_nstrm->notes, TLS_NETIO_NOTE, NULL);
ctrl_ssl = NULL;
pr_session_disconnect(&tls_module, PR_SESS_DISCONNECT_CONFIG_ACL,
"TLSOption AllowClientRenegotiations");
}
}
}
#endif
}
if (tls_opts & TLS_OPT_ENABLE_DIAGS) {
tls_log("[info] %s: %s", str, SSL_state_string_long(ssl));
}
} else if (where & SSL_CB_HANDSHAKE_DONE) {
if (ssl == ctrl_ssl) {
if (tls_ctrl_need_init_handshake == FALSE) {
int reused;
/* If this is an accepted renegotiation, log the possibly-changed
* ciphersuite et al.
*/
reused = SSL_session_reused((SSL *) ssl);
tls_log("%s renegotiation accepted, using cipher %s (%d bits%s)",
SSL_get_version(ssl), SSL_get_cipher_name(ssl),
SSL_get_cipher_bits(ssl, NULL),
reused > 0 ? ", resumed session" : "");
}
tls_ctrl_need_init_handshake = FALSE;
} else {
if (tls_data_need_init_handshake == FALSE) {
/* If this is an accepted renegotiation, log the possibly-changed
* ciphersuite et al.
*/
tls_log("%s renegotiation accepted, using cipher %s (%d bits)",
SSL_get_version(ssl), SSL_get_cipher_name(ssl),
SSL_get_cipher_bits(ssl, NULL));
}
tls_data_need_init_handshake = FALSE;
}
/* Clear the flags set for server-requested renegotiations. */
if (tls_flags & TLS_SESS_CTRL_RENEGOTIATING) {
tls_flags &= ~TLS_SESS_CTRL_RENEGOTIATING;
}
if (tls_flags & ~TLS_SESS_DATA_RENEGOTIATING) {
tls_flags &= ~TLS_SESS_DATA_RENEGOTIATING;
}
if (tls_opts & TLS_OPT_ENABLE_DIAGS) {
tls_log("[info] %s: %s", str, SSL_state_string_long(ssl));
}
} else if (where & SSL_CB_LOOP) {
if (tls_opts & TLS_OPT_ENABLE_DIAGS) {
tls_log("[info] %s: %s", str, SSL_state_string_long(ssl));
}
} else if (where & SSL_CB_ALERT) {
if (tls_opts & TLS_OPT_ENABLE_DIAGS) {
str = (where & SSL_CB_READ) ? "reading" : "writing";
tls_log("[info] %s: SSL/TLS alert %s: %s", str,
SSL_alert_type_string_long(ret), SSL_alert_desc_string_long(ret));
}
} else if (where & SSL_CB_EXIT) {
if (tls_opts & TLS_OPT_ENABLE_DIAGS) {
if (ret == 0) {
tls_log("[info] %s: failed in %s: %s", str,
SSL_state_string_long(ssl), tls_get_errors());
} else if (ret < 0 &&
errno != 0 &&
errno != EAGAIN) {
/* Ignore EAGAIN errors */
tls_log("[info] %s: error in %s (errno %d: %s)",
str, SSL_state_string_long(ssl), errno, strerror(errno));
}
}
}
}
#if OPENSSL_VERSION_NUMBER > 0x000907000L
static void tls_msg_cb(int io_flag, int version, int content_type,
const void *buf, size_t buflen, SSL *ssl, void *arg) {
char *action_str = NULL;
char *version_str = NULL;
char *bytes_str = buflen != 1 ? "bytes" : "byte";
if (io_flag == 0) {
action_str = "received";
} else if (io_flag == 1) {
action_str = "sent";
}
switch (version) {
case SSL2_VERSION:
version_str = "SSLv2";
break;
case SSL3_VERSION:
version_str = "SSLv3";
break;
case TLS1_VERSION:
version_str = "TLSv1";
break;
#if OPENSSL_VERSION_NUMBER >= 0x10001000L
case TLS1_1_VERSION:
version_str = "TLSv1.1";
break;
case TLS1_2_VERSION:
version_str = "TLSv1.2";
break;
#endif
default:
tls_log("[msg] unknown/unsupported version: %d", version);
break;
}
if (version == SSL3_VERSION ||
#if OPENSSL_VERSION_NUMBER >= 0x10001000L
version == TLS1_1_VERSION ||
version == TLS1_2_VERSION ||
#endif
version == TLS1_VERSION) {
switch (content_type) {
case 20:
/* ChangeCipherSpec message */
tls_log("[msg] %s %s ChangeCipherSpec message (%u %s)",
action_str, version_str, (unsigned int) buflen, bytes_str);
break;
case 21: {
/* Alert messages */
if (buflen == 2) {
char *severity_str = NULL;
/* Peek naughtily into the buffer. */
switch (((const unsigned char *) buf)[0]) {
case 1:
severity_str = "warning";
break;
case 2:
severity_str = "fatal";
break;
}
switch (((const unsigned char *) buf)[1]) {
case 0:
tls_log("[msg] %s %s %s 'close_notify' Alert message (%u %s)",
action_str, version_str, severity_str, (unsigned int) buflen,
bytes_str);
break;
case 10:
tls_log("[msg] %s %s %s 'unexpected_message' Alert message "
"(%u %s)", action_str, version_str, severity_str,
(unsigned int) buflen, bytes_str);
break;
case 20:
tls_log("[msg] %s %s %s 'bad_record_mac' Alert message (%u %s)",
action_str, version_str, severity_str, (unsigned int) buflen,
bytes_str);
break;
case 21:
tls_log("[msg] %s %s %s 'decryption_failed' Alert message "
"(%u %s)", action_str, version_str, severity_str,
(unsigned int) buflen, bytes_str);
break;
case 22:
tls_log("[msg] %s %s %s 'record_overflow' Alert message (%u %s)",
action_str, version_str, severity_str, (unsigned int) buflen,
bytes_str);
break;
case 30:
tls_log("[msg] %s %s %s 'decompression_failure' Alert message "
"(%u %s)", action_str, version_str, severity_str,
(unsigned int) buflen, bytes_str);
break;
case 40:
tls_log("[msg] %s %s %s 'handshake_failure' Alert message "
"(%u %s)", action_str, version_str, severity_str,
(unsigned int) buflen, bytes_str);
break;
}
} else {
tls_log("[msg] %s %s Alert message, unknown type (%u %s)", action_str,
version_str, (unsigned int) buflen, bytes_str);
}
break;
}
case 22: {
/* Handshake messages */
if (buflen > 0) {
/* Peek naughtily into the buffer. */
switch (((const unsigned char *) buf)[0]) {
case 0:
tls_log("[msg] %s %s 'HelloRequest' Handshake message (%u %s)",
action_str, version_str, (unsigned int) buflen, bytes_str);
break;
case 1:
tls_log("[msg] %s %s 'ClientHello' Handshake message (%u %s)",
action_str, version_str, (unsigned int) buflen, bytes_str);
break;
case 2:
tls_log("[msg] %s %s 'ServerHello' Handshake message (%u %s)",
action_str, version_str, (unsigned int) buflen, bytes_str);
break;
case 11:
tls_log("[msg] %s %s 'Certificate' Handshake message (%u %s)",
action_str, version_str, (unsigned int) buflen, bytes_str);
break;
case 12:
tls_log("[msg] %s %s 'ServerKeyExchange' Handshake message "
"(%u %s)", action_str, version_str, (unsigned int) buflen,
bytes_str);
break;
case 13:
tls_log("[msg] %s %s 'CertificateRequest' Handshake message "
"(%u %s)", action_str, version_str, (unsigned int) buflen,
bytes_str);
break;
case 14:
tls_log("[msg] %s %s 'ServerHelloDone' Handshake message (%u %s)",
action_str, version_str, (unsigned int) buflen, bytes_str);
break;
case 15:
tls_log("[msg] %s %s 'CertificateVerify' Handshake message "
"(%u %s)", action_str, version_str, (unsigned int) buflen,
bytes_str);
break;
case 16:
tls_log("[msg] %s %s 'ClientKeyExchange' Handshake message "
"(%u %s)", action_str, version_str, (unsigned int) buflen,
bytes_str);
break;
case 20:
tls_log("[msg] %s %s 'Finished' Handshake message (%u %s)",
action_str, version_str, (unsigned int) buflen, bytes_str);
break;
}
} else {
tls_log("[msg] %s %s Handshake message, unknown type (%u %s)",
action_str, version_str, (unsigned int) buflen, bytes_str);
}
break;
}
}
} else if (version == SSL2_VERSION) {
/* SSLv2 message. Ideally we wouldn't get these, but sometimes badly
* behaving FTPS clients send them.
*/
if (buflen > 0) {
/* Peek naughtily into the buffer. */
switch (((const unsigned char *) buf)[0]) {
case 0: {
/* Error */
if (buflen > 3) {
unsigned err_code = (((const unsigned char *) buf)[1] << 8) +
((const unsigned char *) buf)[2];
switch (err_code) {
case 0x0001:
tls_log("[msg] %s %s 'NO-CIPHER-ERROR' Error message (%u %s)",
action_str, version_str, (unsigned int) buflen, bytes_str);
break;
case 0x0002:
tls_log("[msg] %s %s 'NO-CERTIFICATE-ERROR' Error message "
"(%u %s)", action_str, version_str, (unsigned int) buflen,
bytes_str);
break;
case 0x0004:
tls_log("[msg] %s %s 'BAD-CERTIFICATE-ERROR' Error message "
"(%u %s)", action_str, version_str, (unsigned int) buflen,
bytes_str);
break;
case 0x0006:
tls_log("[msg] %s %s 'UNSUPPORTED-CERTIFICATE-TYPE-ERROR' "
"Error message (%u %s)", action_str, version_str,
(unsigned int) buflen, bytes_str);
break;
}
} else {
tls_log("[msg] %s %s Error message, unknown type (%u %s)",
action_str, version_str, (unsigned int) buflen, bytes_str);
}
break;
}