forked from apache/trafficserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSSLUtils.cc
More file actions
2646 lines (2305 loc) · 87.6 KB
/
Copy pathSSLUtils.cc
File metadata and controls
2646 lines (2305 loc) · 87.6 KB
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
/** @file
@section license License
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "BoringSSLUtils.h"
#include "P_SSLUtils.h"
#include "P_Net.h"
#include "P_OCSPStapling.h"
#include "P_SSLConfig.h"
#include "P_SSLNetVConnection.h"
#include "P_TLSKeyLogger.h"
#include "SSLStats.h"
#include "SSLSessionCache.h"
#include "SSLSessionTicket.h"
#include "SSLDynlock.h" // IWYU pragma: keep - for ssl_dyn_*
#include "iocore/net/SSLMultiCertConfigLoader.h"
#include "iocore/net/SSLAPIHooks.h"
#include "iocore/net/SSLDiags.h"
#include "iocore/net/TLSSessionResumptionSupport.h"
#include "records/RecHttp.h"
#include "tscore/MatcherUtils.h"
#include "tscore/ink_config.h"
#include "tscore/SimpleTokenizer.h"
#include "tscore/Layout.h"
#include "tscore/ink_cap.h"
#include "tscore/ink_mutex.h"
#include "tscore/Filenames.h"
#if TS_USE_QUIC == 1
#include "iocore/net/QUICSupport.h"
#endif
#include "swoc/swoc_file.h"
#include "swoc/Errata.h"
#include <openssl/asn1.h>
#include <openssl/bio.h>
#include <openssl/bn.h>
#include <openssl/conf.h>
#include <openssl/dh.h>
#include <openssl/ec.h>
#include <openssl/engine.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/objects.h>
#include <openssl/pem.h>
#include <openssl/rand.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#if HAVE_OPENSSL_TS_H
#include <openssl/ts.h>
#endif
#include <utility>
#include <string>
#include <unistd.h>
#include <termios.h>
#include <vector>
using namespace std::literals;
// ssl_multicert.config field names:
static constexpr std::string_view SSL_IP_TAG("dest_ip"sv);
static constexpr std::string_view SSL_CERT_TAG("ssl_cert_name"sv);
static constexpr std::string_view SSL_PRIVATE_KEY_TAG("ssl_key_name"sv);
static constexpr std::string_view SSL_OCSP_RESPONSE_TAG("ssl_ocsp_name"sv);
static constexpr std::string_view SSL_CA_TAG("ssl_ca_name"sv);
static constexpr std::string_view SSL_ACTION_TAG("action"sv);
static constexpr std::string_view SSL_ACTION_TUNNEL_TAG("tunnel"sv);
static constexpr std::string_view SSL_SESSION_TICKET_ENABLED("ssl_ticket_enabled"sv);
static constexpr std::string_view SSL_SESSION_TICKET_NUMBER("ssl_ticket_number"sv);
static constexpr std::string_view SSL_KEY_DIALOG("ssl_key_dialog"sv);
static constexpr std::string_view SSL_SERVERNAME("dest_fqdn"sv);
static constexpr char SSL_CERT_SEPARATE_DELIM = ',';
#ifndef evp_md_func
#ifdef OPENSSL_NO_SHA256
#define evp_md_func EVP_sha1()
#else
#define evp_md_func EVP_sha256()
#endif
#endif
SSLSessionCache *session_cache; // declared extern in P_SSLConfig.h
static int ssl_vc_index = -1;
static ink_mutex *mutex_buf = nullptr;
static bool open_ssl_initialized = false;
static DbgCtl dbg_ctl_ssl_load{"ssl_load"};
static DbgCtl dbg_ctl_ssl_session_cache{"ssl.session_cache"};
static DbgCtl dbg_ctl_ssl_error{"ssl.error"};
static DbgCtl dbg_ctl_ssl_verify{"ssl_verify"};
#if TS_HAS_TLS_SESSION_TICKET
static bool ssl_context_enable_ticket_callback(SSL_CTX *ctx);
static bool ssl_apply_sni_session_ticket_properties(SSL *ssl);
static bool ssl_set_session_ticket_number(SSL *ssl, size_t num_tickets);
#endif
/* Using pthread thread ID and mutex functions directly, instead of
* ATS this_ethread / ProxyMutex, so that other linked libraries
* may use pthreads and openssl without confusing us here. (TS-2271).
*/
#if !defined(CRYPTO_THREADID_set_callback)
static void
SSL_pthreads_thread_id(CRYPTO_THREADID *id)
{
CRYPTO_THREADID_set_numeric(id, (unsigned long)pthread_self());
}
#endif
// The locking callback goes away with openssl 1.1 and CRYPTO_LOCK is on longer defined
#if defined(CRYPTO_LOCK) && !defined(CRYPTO_set_locking_callback)
static void
SSL_locking_callback(int mode, int type, const char *file, int line)
{
DbgCtl dbg_ctl{"v_ssl_lock"};
Dbg(dbg_ctl, "file: %s line: %d type: %d", file, line, type);
ink_assert(type < CRYPTO_num_locks());
#ifdef OPENSSL_FIPS
// don't need to lock for FIPS if it has POSTed and we are not going to change the mode on the fly
if (type == CRYPTO_LOCK_FIPS || type == CRYPTO_LOCK_FIPS2) {
return;
}
#endif
if (mode & CRYPTO_LOCK) {
ink_mutex_acquire(&mutex_buf[type]);
} else if (mode & CRYPTO_UNLOCK) {
ink_mutex_release(&mutex_buf[type]);
} else {
Dbg(dbg_ctl_ssl_load, "invalid SSL locking mode 0x%x", mode);
ink_assert(0);
}
}
#endif
static bool
SSL_CTX_add_extra_chain_cert_bio(SSL_CTX *ctx, BIO *bio)
{
X509 *cert;
for (;;) {
cert = PEM_read_bio_X509_AUX(bio, nullptr, nullptr, nullptr);
if (!cert) {
// No more the certificates in this file.
break;
}
// This transfers ownership of the cert (X509) to the SSL context, if successful.
#ifdef SSL_CTX_add0_chain_cert
if (!SSL_CTX_add0_chain_cert(ctx, cert)) {
#else
if (!SSL_CTX_add_extra_chain_cert(ctx, cert)) {
#endif
X509_free(cert);
return false;
}
}
return true;
}
static bool
SSL_CTX_add_extra_chain_cert_file(SSL_CTX *ctx, const char *chainfile)
{
scoped_BIO bio(BIO_new_file(chainfile, "r"));
return SSL_CTX_add_extra_chain_cert_bio(ctx, bio.get());
}
static SSL_SESSION *
#if defined(LIBRESSL_VERSION_NUMBER)
ssl_get_cached_session(SSL *ssl, unsigned char *id, int len, int *copy)
#else
ssl_get_cached_session(SSL *ssl, const unsigned char *id, int len, int *copy)
#endif
{
TLSSessionResumptionSupport *srs = TLSSessionResumptionSupport::getInstance(ssl);
ink_assert(srs);
if (srs) {
return srs->getSession(ssl, id, len, copy);
}
return nullptr;
}
static int
ssl_new_cached_session(SSL *ssl, SSL_SESSION *sess)
{
#ifdef TLS1_3_VERSION
if (SSL_SESSION_get_protocol_version(sess) == TLS1_3_VERSION) {
return 0;
}
#endif
unsigned int len = 0;
const unsigned char *id = SSL_SESSION_get_id(sess, &len);
SSLSessionID sid(id, len);
if (diags()->on()) {
static DbgCtl dbg_ctl("ssl_session_cache.insert");
if (dbg_ctl.tag_on()) {
char printable_buf[(len * 2) + 1];
sid.toString(printable_buf, sizeof(printable_buf));
DbgPrint(dbg_ctl, "ssl_new_cached_session session '%s' and context %p", printable_buf, SSL_get_SSL_CTX(ssl));
}
}
Metrics::Counter::increment(ssl_rsb.session_cache_new_session);
session_cache->insertSession(sid, sess, ssl);
// Call hook after new session is created
APIHook *hook = SSLAPIHooks::instance()->get(TSSslHookInternalID(TS_SSL_SESSION_HOOK));
while (hook) {
hook->invoke(TS_EVENT_SSL_SESSION_NEW, &sid);
hook = hook->m_link.next;
}
return 0;
}
static void
ssl_rm_cached_session(SSL_CTX * /* ctx ATS_UNUSED */, SSL_SESSION *sess)
{
#ifdef TLS1_3_VERSION
if (SSL_SESSION_get_protocol_version(sess) == TLS1_3_VERSION) {
return;
}
#endif
unsigned int len = 0;
const unsigned char *id = SSL_SESSION_get_id(sess, &len);
SSLSessionID sid(id, len);
// Call hook before session is removed
APIHook *hook = SSLAPIHooks::instance()->get(TSSslHookInternalID(TS_SSL_SESSION_HOOK));
while (hook) {
hook->invoke(TS_EVENT_SSL_SESSION_REMOVE, &sid);
hook = hook->m_link.next;
}
if (diags()->on()) {
static DbgCtl dbg_ctl("ssl_session_cache.remove");
if (dbg_ctl.tag_on()) {
char printable_buf[(len * 2) + 1];
sid.toString(printable_buf, sizeof(printable_buf));
DbgPrint(dbg_ctl, "ssl_rm_cached_session cached session '%s'", printable_buf);
}
}
session_cache->removeSession(sid);
}
// Callback function for verifying client certificate
static int
ssl_verify_client_callback(int preverify_ok, X509_STORE_CTX *ctx)
{
Dbg(dbg_ctl_ssl_verify, "Callback: verify client cert");
auto *ssl = static_cast<SSL *>(X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx()));
SSLNetVConnection *netvc = SSLNetVCAccess(ssl);
TLSBasicSupport *tbs = TLSBasicSupport::getInstance(ssl);
if (tbs == nullptr) {
Dbg(dbg_ctl_ssl_verify, "call back on stale netvc");
return false;
}
if (tbs->verify_certificate(ctx) == 1) { // hook moved the handshake state to terminal
Warning("TS_EVENT_SSL_VERIFY_CLIENT plugin failed the client certificate check for %s.", netvc->options.sni_servername.get());
return false;
}
return preverify_ok;
}
#if HAVE_SSL_CTX_SET_CLIENT_HELLO_CB
// Pausable callback
static int
ssl_client_hello_callback(SSL *s, int * /* al ATS_UNUSED */, void * /* arg ATS_UNUSED */)
{
TLSSNISupport::ClientHello ch = {s};
#elif HAVE_SSL_CTX_SET_SELECT_CERTIFICATE_CB
static ssl_select_cert_result_t
ssl_client_hello_callback(const SSL_CLIENT_HELLO *client_hello)
{
SSL *s = client_hello->ssl;
TLSSNISupport::ClientHello ch = {client_hello};
#endif
TLSSNISupport *snis = TLSSNISupport::getInstance(s);
if (snis) {
snis->on_client_hello(ch);
int ret = snis->perform_sni_action(*s);
if (ret != SSL_TLSEXT_ERR_OK) {
return CLIENT_HELLO_ERROR;
}
} else {
// This error suggests either of these:
// 1) Call back on unsupported netvc -- Don't register callback unnecessarily
// 2) Call back on stale netvc
Dbg(dbg_ctl_ssl_error, "ssl_client_hello_callback was called unexpectedly");
return CLIENT_HELLO_ERROR;
}
TLSEventSupport *es = TLSEventSupport::getInstance(s);
if (es) {
bool reenabled = es->callHooks(TS_EVENT_SSL_CLIENT_HELLO);
if (!reenabled) {
return CLIENT_HELLO_RETRY;
}
} else {
Dbg(dbg_ctl_ssl_error, "ssl_client_hello_callback call back on stale netvc");
return CLIENT_HELLO_ERROR;
}
return CLIENT_HELLO_SUCCESS;
}
/**
* Called before either the server or the client certificate is used
* Return 1 on success, 0 on error, or -1 to pause, -2 to retry
*/
static int
ssl_cert_callback(SSL *ssl, [[maybe_unused]] void *arg)
{
TLSCertSwitchSupport *tcss = TLSCertSwitchSupport::getInstance(ssl);
TLSEventSupport *tes = TLSEventSupport::getInstance(ssl);
SSLNetVConnection *sslnetvc = dynamic_cast<SSLNetVConnection *>(tcss);
bool reenabled;
int retval = 1;
// If we are in tunnel mode, don't select a cert. Pause!
if (sslnetvc) {
NetVConnection *netvc = reinterpret_cast<NetVConnection *>(sslnetvc);
if (HttpProxyPort::TRANSPORT_BLIND_TUNNEL == netvc->attributes) {
#ifdef OPENSSL_IS_BORINGSSL
return -2; // Retry
#else
return -1; // Pause
#endif
}
}
SSLCertContextType ctxType = SSLCertContextType::GENERIC;
#ifndef HAVE_NATIVE_DUAL_CERT_SUPPORT
if (arg != nullptr) {
const SSL_CLIENT_HELLO *client_hello = (const SSL_CLIENT_HELLO *)arg;
const bool client_ecdsa_capable = BoringSSLUtils::isClientEcdsaCapable(client_hello);
ctxType = client_ecdsa_capable ? SSLCertContextType::EC : SSLCertContextType::RSA;
}
#endif
if (tcss) {
if (tes) {
// Do the common certificate lookup only once. If we pause
// and restart processing, do not execute the common logic again
if (!tes->calledHooks(TS_EVENT_SSL_CERT)) {
retval = tcss->selectCertificate(ssl, ctxType);
if (retval != 1) {
return retval;
}
}
// Call the plugin cert code
reenabled = tes->callHooks(TS_EVENT_SSL_CERT);
// If it did not re-enable, return the code to
// stop the accept processing
if (!reenabled) {
retval = -1; // Pause
}
} else {
if (tcss->selectCertificate(ssl, ctxType) == 1) {
retval = 1;
} else {
retval = 0;
}
}
}
#if TS_HAS_TLS_SESSION_TICKET
if (retval == 1) {
// After replacing the SSL_CTX, make sure the overridden ca_cert_file is still set
if (sslnetvc) {
setClientCertCACerts(ssl, sslnetvc->get_ca_cert_file(), sslnetvc->get_ca_cert_dir());
}
if (!ssl_apply_sni_session_ticket_properties(ssl)) {
retval = 0;
}
}
#endif
// Return 1 for success, 0 for error, or -1 to pause
return retval;
}
/*
* Cannot stop this callback. Always reeneabled
*/
static int
ssl_servername_callback(SSL *ssl, int *al, void *arg)
{
TLSSNISupport *snis = TLSSNISupport::getInstance(ssl);
if (snis) {
if (TLSEventSupport *es = TLSEventSupport::getInstance(ssl); es) {
es->callHooks(TS_EVENT_SSL_SERVERNAME);
}
snis->on_servername(ssl, al, arg);
#if !TS_USE_HELLO_CB
// Only call the SNI actions here if not already performed in the HELLO_CB
int ret = snis->perform_sni_action(*ssl);
if (ret != SSL_TLSEXT_ERR_OK) {
return SSL_TLSEXT_ERR_ALERT_FATAL;
}
#endif
} else {
// This error suggests either of these:
// 1) Call back on unsupported netvc -- Don't register callback unnecessarily
// 2) Call back on stale netvc
Dbg(dbg_ctl_ssl_error, "ssl_servername_callback was called unexpectedly");
return SSL_TLSEXT_ERR_ALERT_FATAL;
}
return SSL_TLSEXT_ERR_OK;
}
// NextProtocolNegotiation TLS extension callback. The NPN extension
// allows the client to select a preferred protocol, so all we have
// to do here is tell them what out protocol set is.
int
ssl_next_protos_advertised_callback(SSL *ssl, const unsigned char **out, unsigned *outlen, void *)
{
ALPNSupport *alpns = ALPNSupport::getInstance(ssl);
ink_assert(alpns);
if (alpns) {
return alpns->advertise_next_protocol(out, outlen);
}
return SSL_TLSEXT_ERR_NOACK;
}
int
ssl_alpn_select_callback(SSL *ssl, const unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned inlen,
void *)
{
ALPNSupport *alpns = ALPNSupport::getInstance(ssl);
ink_assert(alpns);
if (alpns) {
return alpns->select_next_protocol(out, outlen, in, inlen);
}
return SSL_TLSEXT_ERR_NOACK;
}
#if TS_USE_GET_DH_2048_256 == 0
/* Build 2048-bit MODP Group with 256-bit Prime Order Subgroup from RFC 5114 */
static DH *
DH_get_2048_256()
{
static const unsigned char dh2048_p[] = {
0x87, 0xA8, 0xE6, 0x1D, 0xB4, 0xB6, 0x66, 0x3C, 0xFF, 0xBB, 0xD1, 0x9C, 0x65, 0x19, 0x59, 0x99, 0x8C, 0xEE, 0xF6, 0x08,
0x66, 0x0D, 0xD0, 0xF2, 0x5D, 0x2C, 0xEE, 0xD4, 0x43, 0x5E, 0x3B, 0x00, 0xE0, 0x0D, 0xF8, 0xF1, 0xD6, 0x19, 0x57, 0xD4,
0xFA, 0xF7, 0xDF, 0x45, 0x61, 0xB2, 0xAA, 0x30, 0x16, 0xC3, 0xD9, 0x11, 0x34, 0x09, 0x6F, 0xAA, 0x3B, 0xF4, 0x29, 0x6D,
0x83, 0x0E, 0x9A, 0x7C, 0x20, 0x9E, 0x0C, 0x64, 0x97, 0x51, 0x7A, 0xBD, 0x5A, 0x8A, 0x9D, 0x30, 0x6B, 0xCF, 0x67, 0xED,
0x91, 0xF9, 0xE6, 0x72, 0x5B, 0x47, 0x58, 0xC0, 0x22, 0xE0, 0xB1, 0xEF, 0x42, 0x75, 0xBF, 0x7B, 0x6C, 0x5B, 0xFC, 0x11,
0xD4, 0x5F, 0x90, 0x88, 0xB9, 0x41, 0xF5, 0x4E, 0xB1, 0xE5, 0x9B, 0xB8, 0xBC, 0x39, 0xA0, 0xBF, 0x12, 0x30, 0x7F, 0x5C,
0x4F, 0xDB, 0x70, 0xC5, 0x81, 0xB2, 0x3F, 0x76, 0xB6, 0x3A, 0xCA, 0xE1, 0xCA, 0xA6, 0xB7, 0x90, 0x2D, 0x52, 0x52, 0x67,
0x35, 0x48, 0x8A, 0x0E, 0xF1, 0x3C, 0x6D, 0x9A, 0x51, 0xBF, 0xA4, 0xAB, 0x3A, 0xD8, 0x34, 0x77, 0x96, 0x52, 0x4D, 0x8E,
0xF6, 0xA1, 0x67, 0xB5, 0xA4, 0x18, 0x25, 0xD9, 0x67, 0xE1, 0x44, 0xE5, 0x14, 0x05, 0x64, 0x25, 0x1C, 0xCA, 0xCB, 0x83,
0xE6, 0xB4, 0x86, 0xF6, 0xB3, 0xCA, 0x3F, 0x79, 0x71, 0x50, 0x60, 0x26, 0xC0, 0xB8, 0x57, 0xF6, 0x89, 0x96, 0x28, 0x56,
0xDE, 0xD4, 0x01, 0x0A, 0xBD, 0x0B, 0xE6, 0x21, 0xC3, 0xA3, 0x96, 0x0A, 0x54, 0xE7, 0x10, 0xC3, 0x75, 0xF2, 0x63, 0x75,
0xD7, 0x01, 0x41, 0x03, 0xA4, 0xB5, 0x43, 0x30, 0xC1, 0x98, 0xAF, 0x12, 0x61, 0x16, 0xD2, 0x27, 0x6E, 0x11, 0x71, 0x5F,
0x69, 0x38, 0x77, 0xFA, 0xD7, 0xEF, 0x09, 0xCA, 0xDB, 0x09, 0x4A, 0xE9, 0x1E, 0x1A, 0x15, 0x97};
static const unsigned char dh2048_g[] = {
0x3F, 0xB3, 0x2C, 0x9B, 0x73, 0x13, 0x4D, 0x0B, 0x2E, 0x77, 0x50, 0x66, 0x60, 0xED, 0xBD, 0x48, 0x4C, 0xA7, 0xB1, 0x8F,
0x21, 0xEF, 0x20, 0x54, 0x07, 0xF4, 0x79, 0x3A, 0x1A, 0x0B, 0xA1, 0x25, 0x10, 0xDB, 0xC1, 0x50, 0x77, 0xBE, 0x46, 0x3F,
0xFF, 0x4F, 0xED, 0x4A, 0xAC, 0x0B, 0xB5, 0x55, 0xBE, 0x3A, 0x6C, 0x1B, 0x0C, 0x6B, 0x47, 0xB1, 0xBC, 0x37, 0x73, 0xBF,
0x7E, 0x8C, 0x6F, 0x62, 0x90, 0x12, 0x28, 0xF8, 0xC2, 0x8C, 0xBB, 0x18, 0xA5, 0x5A, 0xE3, 0x13, 0x41, 0x00, 0x0A, 0x65,
0x01, 0x96, 0xF9, 0x31, 0xC7, 0x7A, 0x57, 0xF2, 0xDD, 0xF4, 0x63, 0xE5, 0xE9, 0xEC, 0x14, 0x4B, 0x77, 0x7D, 0xE6, 0x2A,
0xAA, 0xB8, 0xA8, 0x62, 0x8A, 0xC3, 0x76, 0xD2, 0x82, 0xD6, 0xED, 0x38, 0x64, 0xE6, 0x79, 0x82, 0x42, 0x8E, 0xBC, 0x83,
0x1D, 0x14, 0x34, 0x8F, 0x6F, 0x2F, 0x91, 0x93, 0xB5, 0x04, 0x5A, 0xF2, 0x76, 0x71, 0x64, 0xE1, 0xDF, 0xC9, 0x67, 0xC1,
0xFB, 0x3F, 0x2E, 0x55, 0xA4, 0xBD, 0x1B, 0xFF, 0xE8, 0x3B, 0x9C, 0x80, 0xD0, 0x52, 0xB9, 0x85, 0xD1, 0x82, 0xEA, 0x0A,
0xDB, 0x2A, 0x3B, 0x73, 0x13, 0xD3, 0xFE, 0x14, 0xC8, 0x48, 0x4B, 0x1E, 0x05, 0x25, 0x88, 0xB9, 0xB7, 0xD2, 0xBB, 0xD2,
0xDF, 0x01, 0x61, 0x99, 0xEC, 0xD0, 0x6E, 0x15, 0x57, 0xCD, 0x09, 0x15, 0xB3, 0x35, 0x3B, 0xBB, 0x64, 0xE0, 0xEC, 0x37,
0x7F, 0xD0, 0x28, 0x37, 0x0D, 0xF9, 0x2B, 0x52, 0xC7, 0x89, 0x14, 0x28, 0xCD, 0xC6, 0x7E, 0xB6, 0x18, 0x4B, 0x52, 0x3D,
0x1D, 0xB2, 0x46, 0xC3, 0x2F, 0x63, 0x07, 0x84, 0x90, 0xF0, 0x0E, 0xF8, 0xD6, 0x47, 0xD1, 0x48, 0xD4, 0x79, 0x54, 0x51,
0x5E, 0x23, 0x27, 0xCF, 0xEF, 0x98, 0xC5, 0x82, 0x66, 0x4B, 0x4C, 0x0F, 0x6C, 0xC4, 0x16, 0x59};
DH *dh;
BIGNUM *p;
BIGNUM *g;
if ((dh = DH_new()) == nullptr) {
return nullptr;
}
p = BN_bin2bn(dh2048_p, sizeof(dh2048_p), nullptr);
g = BN_bin2bn(dh2048_g, sizeof(dh2048_g), nullptr);
if (p == nullptr || g == nullptr) {
DH_free(dh);
BN_free(p);
BN_free(g);
return nullptr;
}
DH_set0_pqg(dh, p, nullptr, g);
return (dh);
}
#endif
bool
SSLMultiCertConfigLoader::_enable_ktls([[maybe_unused]] SSL_CTX *ctx)
{
#ifdef SSL_OP_ENABLE_KTLS
if (SSLConfigParams::ssl_ktls_enabled) {
if (SSL_CTX_set_options(ctx, SSL_OP_ENABLE_KTLS)) {
static DbgCtl dbg_ctl{"ssl.ktls"};
Dbg(dbg_ctl, "KTLS is enabled");
} else {
return false;
}
}
#endif
return true;
}
bool
SSLMultiCertConfigLoader::_enable_early_data([[maybe_unused]] SSL_CTX *ctx)
{
#if TS_HAS_TLS_EARLY_DATA
if (SSLConfigParams::server_max_early_data > 0) {
#if HAVE_SSL_IN_EARLY_DATA
// If SSL_in_early_data is available, it's probably BoringSSL
// and SSL_set_early_data_enabled should be available.
SSL_CTX_set_early_data_enabled(ctx, 1);
#endif
}
#endif
return true;
}
static SSL_CTX *
ssl_context_enable_dhe(const char *dhparams_file, SSL_CTX *ctx)
{
DH *server_dh;
if (dhparams_file) {
scoped_BIO bio(BIO_new_file(dhparams_file, "r"));
server_dh = PEM_read_bio_DHparams(bio.get(), nullptr, nullptr, nullptr);
} else {
server_dh = DH_get_2048_256();
}
if (!server_dh) {
Error("SSL dhparams source returned invalid parameters");
return nullptr;
}
if (!SSL_CTX_set_options(ctx, SSL_OP_SINGLE_DH_USE) || !SSL_CTX_set_tmp_dh(ctx, server_dh)) {
DH_free(server_dh);
Error("failed to configure SSL DH");
return nullptr;
}
DH_free(server_dh);
return ctx;
}
#if TS_HAS_TLS_SESSION_TICKET
static bool
ssl_context_enable_ticket_callback(SSL_CTX *ctx)
{
#ifdef HAVE_SSL_CTX_SET_TLSEXT_TICKET_KEY_EVP_CB
if (SSL_CTX_set_tlsext_ticket_key_evp_cb(ctx, ssl_callback_session_ticket) == 0) {
#else
if (SSL_CTX_set_tlsext_ticket_key_cb(ctx, ssl_callback_session_ticket) == 0) {
#endif
Error("failed to set session ticket callback");
return false;
}
return true;
}
static bool
ssl_set_session_ticket_number(SSL *ssl, size_t num_tickets)
{
#if defined(OPENSSL_IS_BORINGSSL)
// BoringSSL only exposes SSL_CTX_set_num_tickets(), so the per-connection
// sni.yaml override is not available here.
(void)ssl;
(void)num_tickets;
return true;
#else
return SSL_set_num_tickets(ssl, num_tickets) == 1;
#endif
}
static bool
ssl_apply_sni_session_ticket_properties(SSL *ssl)
{
auto snis = TLSSNISupport::getInstance(ssl);
if (snis == nullptr) {
return true;
}
auto const &hints = snis->hints_from_sni;
if (!hints.ssl_ticket_enabled.has_value() && !hints.ssl_ticket_number.has_value()) {
return true;
}
std::optional<size_t> num_tickets;
if (hints.ssl_ticket_enabled.has_value()) {
if (hints.ssl_ticket_enabled.value() != 0) {
SSL_clear_options(ssl, SSL_OP_NO_TICKET);
Dbg(dbg_ctl_ssl_load, "Enabled session tickets due to sni.yaml override");
} else {
SSL_set_options(ssl, SSL_OP_NO_TICKET);
num_tickets = 0;
Dbg(dbg_ctl_ssl_load, "Disabled session tickets due to sni.yaml override");
}
}
if ((!hints.ssl_ticket_enabled.has_value() || hints.ssl_ticket_enabled.value() != 0) && hints.ssl_ticket_number.has_value()) {
num_tickets = hints.ssl_ticket_number.value() > 0 ? static_cast<size_t>(hints.ssl_ticket_number.value()) : 0;
}
if (num_tickets.has_value()) {
if (!ssl_set_session_ticket_number(ssl, num_tickets.value())) {
Error("failed to set session ticket number from sni.yaml");
return false;
}
Dbg(dbg_ctl_ssl_load, "Set session ticket number from sni.yaml to %zu", num_tickets.value());
}
return true;
}
#endif
static ssl_ticket_key_block *
ssl_context_enable_tickets(SSL_CTX *ctx, const char *ticket_key_path)
{
#if TS_HAS_TLS_SESSION_TICKET
ssl_ticket_key_block *keyblock = nullptr;
keyblock = ssl_create_ticket_keyblock(ticket_key_path);
// On the "first run" the metrics have not been initialized, so this has to check it.
if (ssl_rsb.total_ticket_keys_renewed) {
Metrics::Counter::increment(ssl_rsb.total_ticket_keys_renewed);
}
// Setting the callback can only fail if OpenSSL does not recognize the
// SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB constant. we set the callback first
// so that we don't leave a ticket_key pointer attached if it fails.
if (!ssl_context_enable_ticket_callback(ctx)) {
ticket_block_free(keyblock);
return nullptr;
}
SSL_CTX_clear_options(ctx, SSL_OP_NO_TICKET);
return keyblock;
#else /* !TS_HAS_TLS_SESSION_TICKET */
(void)ticket_key_path;
return nullptr;
#endif /* TS_HAS_TLS_SESSION_TICKET */
}
// RAII implementation for struct termios
struct ssl_termios : public termios {
ssl_termios(int fd)
{
_fd = -1;
// populate base class data
if (tcgetattr(fd, this) == 0) { // success
_fd = fd;
}
// save our copy
_initialAttr = *this;
}
~ssl_termios()
{
if (_fd != -1) {
tcsetattr(_fd, 0, &_initialAttr);
}
}
bool
ok() const
{
return (_fd != -1);
}
private:
int _fd;
struct termios _initialAttr;
};
static int
ssl_getpassword(const char *prompt, char *buffer, int size)
{
fprintf(stdout, "%s", prompt);
// disable echo and line buffering
ssl_termios tty_attr(STDIN_FILENO);
if (!tty_attr.ok()) {
return -1;
}
tty_attr.c_lflag &= ~ICANON; // no buffer, no backspace
tty_attr.c_lflag &= ~ECHO; // no echo
tty_attr.c_lflag &= ~ISIG; // no signal for ctrl-c
if (tcsetattr(STDIN_FILENO, 0, &tty_attr) < 0) {
return -1;
}
int i = 0;
int ch = 0;
*buffer = 0;
while ((ch = getchar()) != '\n' && ch != EOF) {
// make sure room in buffer
if (i >= size - 1) {
return -1;
}
buffer[i] = ch;
buffer[++i] = 0;
}
return i;
}
static int
ssl_private_key_passphrase_callback_exec(char *buf, int size, int rwflag, void *userdata)
{
if (0 == size) {
return 0;
}
*buf = 0;
const SSLMultiCertConfigParams *sslMultCertSettings = static_cast<SSLMultiCertConfigParams *>(userdata);
Dbg(dbg_ctl_ssl_load, "ssl_private_key_passphrase_callback_exec rwflag=%d dialog=%s", rwflag, sslMultCertSettings->dialog.get());
// only respond to reading private keys, not writing them (does ats even do that?)
if (0 == rwflag) {
// execute the dialog program and use the first line output as the passphrase
ink_assert(strncmp(sslMultCertSettings->dialog, "exec:", 5) == 0);
const char *serverDialog = &sslMultCertSettings->dialog[5];
FILE *f = popen(serverDialog, "r");
if (f) {
if (fgets(buf, size, f)) {
// remove any ending CR or LF
for (char *pass = buf; *pass; pass++) {
if (*pass == '\n' || *pass == '\r') {
*pass = 0;
break;
}
}
}
pclose(f);
} else { // popen failed
Error("could not open dialog '%s' - %s", serverDialog, strerror(errno));
}
}
return strlen(buf);
}
static int
ssl_private_key_passphrase_callback_builtin(char *buf, int size, int rwflag, void *userdata)
{
if (0 == size) {
return 0;
}
*buf = 0;
const SSLMultiCertConfigParams *sslMultCertSettings = static_cast<SSLMultiCertConfigParams *>(userdata);
Dbg(dbg_ctl_ssl_load, "ssl_private_key_passphrase_callback rwflag=%d dialog=%s", rwflag, sslMultCertSettings->dialog.get());
// only respond to reading private keys, not writing them (does ats even do that?)
if (0 == rwflag) {
// output request
fprintf(stdout, "Some of your private key files are encrypted for security reasons.\n");
fprintf(stdout, "In order to read them you have to provide the pass phrases.\n");
fprintf(stdout, "ssl_cert_name=%s", sslMultCertSettings->cert.get());
if (sslMultCertSettings->key.get()) { // output ssl_key_name if provided
fprintf(stdout, " ssl_key_name=%s", sslMultCertSettings->key.get());
}
fprintf(stdout, "\n");
// get passphrase
// if error, then no passphrase
if (ssl_getpassword("Enter passphrase:", buf, size) <= 0) {
*buf = 0;
}
fprintf(stdout, "\n");
}
return strlen(buf);
}
static bool
ssl_private_key_validate_exec(const char *cmdLine)
{
if (nullptr == cmdLine) {
errno = EINVAL;
return false;
}
bool bReturn = false;
char *cmdLineCopy = ats_strdup(cmdLine);
char *ptr = cmdLineCopy;
while (*ptr && !isspace(*ptr)) {
++ptr;
}
*ptr = 0;
if (access(cmdLineCopy, X_OK) != -1) {
bReturn = true;
}
ats_free(cmdLineCopy);
return bReturn;
}
#if defined(LIBRESSL_VERSION_NUMBER)
#define ssl_malloc(size, file, line) ssl_malloc(size)
#define ssl_realloc(ptr, size, file, line) ssl_realloc(ptr, size)
#define ssl_free(ptr, file, line) ssl_free(ptr)
#define ssl_track_malloc(size, file, line) ssl_track_malloc(size)
#define ssl_track_realloc(ptr, size, file, line) ssl_track_realloc(ptr, size)
#define ssl_track_free(ptr, file, line) ssl_track_free(ptr)
#endif
void *
ssl_malloc(size_t size, const char * /*filename */, int /*lineno*/)
{
return ats_malloc(size);
}
void *
ssl_realloc(void *ptr, size_t size, const char * /*filename*/, int /*lineno*/)
{
return ats_realloc(ptr, size);
}
void
ssl_free(void *ptr, const char * /*filename*/, int /*lineno*/)
{
ats_free(ptr);
}
void *
ssl_track_malloc(size_t size, const char * /*filename*/, int /*lineno*/)
{
return ats_track_malloc(size, &ssl_memory_allocated);
}
void *
ssl_track_realloc(void *ptr, size_t size, const char * /*filename*/, int /*lineno*/)
{
return ats_track_realloc(ptr, size, &ssl_memory_allocated, &ssl_memory_freed);
}
void
ssl_track_free(void *ptr, const char * /*filename*/, int /*lineno*/)
{
ats_track_free(ptr, &ssl_memory_freed);
}
/*
* Some items are only initialized if certain config values are set
* Must have a second pass that initializes after loading the SSL config
*/
void
SSLPostConfigInitialize()
{
if (SSLConfigParams::engine_conf_file) {
#if HAVE_ENGINE_LOAD_DYNAMIC
ENGINE_load_dynamic();
#endif
OPENSSL_load_builtin_modules();
if (CONF_modules_load_file(SSLConfigParams::engine_conf_file, nullptr, 0) <= 0) {
char err_buf[256] = {0};
ERR_error_string_n(ERR_get_error(), err_buf, sizeof(err_buf));
Error("Could not load SSL engine configuration file %s: %s", SSLConfigParams::engine_conf_file, err_buf);
}
}
}
void
SSLInitializeLibrary()
{
if (!open_ssl_initialized) {
// BoringSSL does not have the memory functions
#ifdef HAVE_CRYPTO_SET_MEM_FUNCTIONS
if (res_track_memory >= 2) {
CRYPTO_set_mem_functions(ssl_track_malloc, ssl_track_realloc, ssl_track_free);
} else {
CRYPTO_set_mem_functions(ssl_malloc, ssl_realloc, ssl_free);
}
#endif
SSL_load_error_strings();
SSL_library_init();
#ifdef OPENSSL_FIPS
// calling FIPS_mode_set() will force FIPS to POST (Power On Self Test)
// After POST we don't have to lock for FIPS
int mode = FIPS_mode();
FIPS_mode_set(mode);
Dbg(dbg_ctl_ssl_load, "FIPS_mode: %d", mode);
#endif
mutex_buf = static_cast<ink_mutex *>(OPENSSL_malloc(CRYPTO_num_locks() * sizeof(ink_mutex)));
for (int i = 0; i < CRYPTO_num_locks(); i++) {
ink_mutex_init(&mutex_buf[i]);
}
CRYPTO_set_locking_callback(SSL_locking_callback);
#if !defined(CRYPTO_THREADID_set_callback)
CRYPTO_THREADID_set_callback(SSL_pthreads_thread_id);
#endif
CRYPTO_set_dynlock_create_callback(ssl_dyn_create_callback);
CRYPTO_set_dynlock_lock_callback(ssl_dyn_lock_callback);
CRYPTO_set_dynlock_destroy_callback(ssl_dyn_destroy_callback);
}
ssl_stapling_ex_init();
// Reserve an application data index so that we can attach
// the SSLNetVConnection to the SSL session.
ssl_vc_index = SSL_get_ex_new_index(0, (void *)"NetVC index", nullptr, nullptr, nullptr);
TLSBasicSupport::initialize();
TLSEventSupport::initialize();
ALPNSupport::initialize();
TLSSessionResumptionSupport::initialize();
TLSSNISupport::initialize();
TLSEarlyDataSupport::initialize();
TLSTunnelSupport::initialize();
TLSCertSwitchSupport::initialize();
#if TS_USE_QUIC == 1
QUICSupport::initialize();
#endif
open_ssl_initialized = true;
}
SSL_CTX *
SSLMultiCertConfigLoader::default_server_ssl_ctx()
{
return SSL_CTX_new(SSLv23_server_method());
}
static bool
SSLPrivateKeyHandler(SSL_CTX *ctx, const char *keyPath, const char *secret_data, int secret_data_len)
{
EVP_PKEY *pkey = nullptr;
#if HAVE_ENGINE_GET_DEFAULT_RSA && HAVE_ENGINE_LOAD_PRIVATE_KEY
ENGINE *e = ENGINE_get_default_RSA();
if (e != nullptr) {
pkey = ENGINE_load_private_key(e, keyPath, nullptr, nullptr);
if (pkey) {
if (!SSL_CTX_use_PrivateKey(ctx, pkey)) {
Dbg(dbg_ctl_ssl_load, "failed to load server private key from engine");
EVP_PKEY_free(pkey);
return false;
}
}
}
#else