forked from apache/trafficserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSNIActionPerformer.cc
More file actions
560 lines (494 loc) · 18.3 KB
/
Copy pathSNIActionPerformer.cc
File metadata and controls
560 lines (494 loc) · 18.3 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
/** @file
Implementation of SNIActionPerformer
@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 "swoc/swoc_file.h"
#include "swoc/BufferWriter.h"
#include "tscore/Layout.h"
#include "proxy/IPAllow.h"
#include "SNIActionPerformer.h"
#include "P_SSLNetVConnection.h"
#if TS_USE_QUIC == 1
#include "P_QUICNetVConnection.h"
#endif
namespace
{
#if TS_USE_QUIC == 1
DbgCtl dbg_ctl_ssl_sni{"ssl_sni"};
#endif
} // end anonymous namespace
int
ControlQUIC::SNIAction([[maybe_unused]] SSL &ssl, const Context & /* ctx ATS_UNUSED */) const
{
#if TS_USE_QUIC == 1
if (enable_quic) {
return SSL_TLSEXT_ERR_OK;
}
// This action is only available for QUIC connections
if (QUICSupport::getInstance(&ssl) == nullptr) {
return SSL_TLSEXT_ERR_OK;
}
if (dbg_ctl_ssl_sni.on()) {
if (auto snis = TLSSNISupport::getInstance(&ssl)) {
const char *servername = snis->get_sni_server_name();
DbgPrint(dbg_ctl_ssl_sni, "Rejecting handshake due to QUIC being disabled for fqdn [%s]", servername);
}
}
return SSL_TLSEXT_ERR_ALERT_FATAL;
#else
return SSL_TLSEXT_ERR_OK;
#endif
}
int
ControlH2::SNIAction(SSL &ssl, const Context & /* ctx ATS_UNUSED */) const
{
auto snis = TLSSNISupport::getInstance(&ssl);
auto alpns = ALPNSupport::getInstance(&ssl);
if (snis == nullptr || alpns == nullptr) {
return SSL_TLSEXT_ERR_OK;
}
const char *servername = snis->get_sni_server_name();
if (!enable_h2) {
alpns->disableProtocol(TS_ALPN_PROTOCOL_INDEX_HTTP_2_0);
Dbg(dbg_ctl_ssl_sni, "H2 disabled, fqdn [%s]", servername);
} else {
alpns->enableProtocol(TS_ALPN_PROTOCOL_INDEX_HTTP_2_0);
Dbg(dbg_ctl_ssl_sni, "H2 enabled, fqdn [%s]", servername);
}
return SSL_TLSEXT_ERR_OK;
}
int
HTTP2BufferWaterMark::SNIAction(SSL &ssl, const Context & /* ctx ATS_UNUSED */) const
{
if (auto snis = TLSSNISupport::getInstance(&ssl)) {
snis->hints_from_sni.http2_buffer_water_mark = value;
}
return SSL_TLSEXT_ERR_OK;
}
int
HTTP2InitialWindowSizeIn::SNIAction(SSL &ssl, const Context & /* ctx ATS_UNUSED */) const
{
if (auto snis = TLSSNISupport::getInstance(&ssl)) {
snis->hints_from_sni.http2_initial_window_size_in = value;
}
return SSL_TLSEXT_ERR_OK;
}
int
HTTP2MaxSettingsFramesPerMinute::SNIAction(SSL &ssl, const Context & /* ctx ATS_UNUSED */) const
{
if (auto snis = TLSSNISupport::getInstance(&ssl)) {
snis->hints_from_sni.http2_max_settings_frames_per_minute = value;
}
return SSL_TLSEXT_ERR_OK;
}
int
HTTP2MaxPingFramesPerMinute::SNIAction(SSL &ssl, const Context & /* ctx ATS_UNUSED */) const
{
if (auto snis = TLSSNISupport::getInstance(&ssl)) {
snis->hints_from_sni.http2_max_ping_frames_per_minute = value;
}
return SSL_TLSEXT_ERR_OK;
}
int
HTTP2MaxPriorityFramesPerMinute::SNIAction(SSL &ssl, const Context & /* ctx ATS_UNUSED */) const
{
if (auto snis = TLSSNISupport::getInstance(&ssl)) {
snis->hints_from_sni.http2_max_priority_frames_per_minute = value;
}
return SSL_TLSEXT_ERR_OK;
}
int
HTTP2MaxRstStreamFramesPerMinute::SNIAction(SSL &ssl, const Context & /* ctx ATS_UNUSED */) const
{
if (auto snis = TLSSNISupport::getInstance(&ssl)) {
snis->hints_from_sni.http2_max_rst_stream_frames_per_minute = value;
}
return SSL_TLSEXT_ERR_OK;
}
int
HTTP2MaxContinuationFramesPerMinute::SNIAction(SSL &ssl, const Context & /* ctx ATS_UNUSED */) const
{
if (auto snis = TLSSNISupport::getInstance(&ssl)) {
snis->hints_from_sni.http2_max_continuation_frames_per_minute = value;
}
return SSL_TLSEXT_ERR_OK;
}
TunnelDestination::TunnelDestination(const std::string_view &dest, SNIRoutingType type, YamlSNIConfig::TunnelPreWarm prewarm,
const std::vector<int> &alpn)
: destination(dest), type(type), tunnel_prewarm(prewarm), alpn_ids(alpn)
{
// Check for port variable specification. Note that this is checked before
// the match group so that the corresponding function can be applied before
// the match group expansion(when the var_start_pos is still accurate).
auto recv_port_start_pos = destination.find(MAP_WITH_RECV_PORT_STR);
auto pp_port_start_pos = destination.find(MAP_WITH_PROXY_PROTOCOL_PORT_STR);
bool has_recv_port_var = recv_port_start_pos != std::string::npos;
bool has_pp_port_var = pp_port_start_pos != std::string::npos;
if (has_recv_port_var && has_pp_port_var) {
Error("Invalid destination \"%.*s\" in SNI configuration - Only one port variable can be specified.",
static_cast<int>(destination.size()), destination.data());
} else if (has_recv_port_var) {
fnArrIndexes.push_back(OpId::MAP_WITH_RECV_PORT);
var_start_pos = recv_port_start_pos;
} else if (has_pp_port_var) {
fnArrIndexes.push_back(OpId::MAP_WITH_PROXY_PROTOCOL_PORT);
var_start_pos = pp_port_start_pos;
}
// Check for match groups as well.
if (destination.find_first_of('$') != std::string::npos) {
fnArrIndexes.push_back(OpId::MATCH_GROUPS);
}
}
int
TunnelDestination::SNIAction(SSL &ssl, const Context &ctx) const
{
auto snis = TLSSNISupport::getInstance(&ssl);
auto tuns = TLSTunnelSupport::getInstance(&ssl);
auto alpns = ALPNSupport::getInstance(&ssl);
auto ssl_netvc = SSLNetVCAccess(&ssl);
if (snis == nullptr || tuns == nullptr || alpns == nullptr || ssl_netvc == nullptr) {
return SSL_TLSEXT_ERR_OK;
}
const char *servername = snis->get_sni_server_name();
if (fnArrIndexes.empty()) {
tuns->set_tunnel_destination(destination, type, !TLSTunnelSupport::PORT_IS_DYNAMIC, tunnel_prewarm);
Dbg(dbg_ctl_ssl_sni, "Destination now is [%s], fqdn [%s]", destination.c_str(), servername);
} else {
bool port_is_dynamic = false;
auto fixed_dst{destination};
// Apply mapping functions to get the final destination.
for (auto fnArrIndex : fnArrIndexes) {
// Dispatch to the correct tunnel destination port function.
fixed_dst = fix_destination[fnArrIndex](fixed_dst, var_start_pos, ctx, ssl_netvc, port_is_dynamic);
}
tuns->set_tunnel_destination(fixed_dst, type, port_is_dynamic, tunnel_prewarm);
Dbg(dbg_ctl_ssl_sni, "Destination now is [%s], configured [%s], fqdn [%s]", fixed_dst.c_str(), destination.c_str(), servername);
}
if (type == SNIRoutingType::BLIND) {
ssl_netvc->attributes = HttpProxyPort::TRANSPORT_BLIND_TUNNEL;
}
// ALPN
for (int id : alpn_ids) {
alpns->enableProtocol(id);
}
return SSL_TLSEXT_ERR_OK;
}
bool
TunnelDestination::is_number(std::string_view s)
{
return !s.empty() &&
std::find_if(std::begin(s), std::end(s), [](std::string_view::value_type c) { return !std::isdigit(c); }) == std::end(s);
}
/**
* `tunnel_route` may contain matching groups ie: `$1` which needs to be replaced by the corresponding
* captured group from the `fqdn`, this function will replace them using proper group string. Matching
* groups could be at any order.
*/
std::string
TunnelDestination::replace_match_groups(std::string_view dst, const ActionItem::Context::CapturedGroupViewVec &groups,
bool &port_is_dynamic)
{
port_is_dynamic = false;
if (dst.empty() || groups.empty()) {
return std::string{dst};
}
std::string real_dst;
std::string::size_type pos{0};
const auto end = std::end(dst);
// We need to split the tunnel string and place each corresponding match on the
// configured one, so we need to first, get the match, then get the match number
// making sure that it does exist in the captured group.
bool is_writing_port = false;
for (auto c = std::begin(dst); c != end; c++, pos++) {
if (*c == ':') {
is_writing_port = true;
}
if (*c == '$') {
// find the next '.' so we can get the group number.
const auto dot = dst.find('.', pos);
std::string::size_type to = std::string::npos;
if (dot != std::string::npos) {
to = dot - (pos + 1);
} else {
// It may not have a dot, which could be because it's the last part. In that case
// we should check for the port separator.
if (const auto port = dst.find(':', pos); port != std::string::npos) {
to = (port - pos) - 1;
}
}
std::string_view number_str{dst.substr(pos + 1, to)};
if (!is_number(number_str)) {
// it may be some issue on the configured string, place the char and keep going.
real_dst += *c;
continue;
}
const int group_index = swoc::svtoi(number_str);
if ((group_index - 1) < static_cast<int>(groups.size())) {
// place the captured group.
real_dst += groups[group_index - 1];
if (is_writing_port) {
port_is_dynamic = true;
}
// if it was the last match, then ...
if (dot == std::string::npos && to == std::string::npos) {
// that's it.
break;
}
pos += number_str.size() + 1;
std::advance(c, number_str.size() + 1);
}
// If there is no match for a specific group, then we keep the `$#` as defined in the string.
}
real_dst += *c;
}
return real_dst;
}
int
VerifyClient::SNIAction(SSL &ssl, const Context & /* ctx ATS_UNUSED */) const
{
auto snis = TLSSNISupport::getInstance(&ssl);
auto ssl_vc = SSLNetVCAccess(&ssl);
if (snis == nullptr || ssl_vc == nullptr) {
return SSL_TLSEXT_ERR_OK;
}
const char *servername = snis->get_sni_server_name();
Dbg(dbg_ctl_ssl_sni, "action verify param %d, fqdn [%s]", this->mode, servername);
setClientCertLevel(ssl_vc->ssl, this->mode);
ssl_vc->set_ca_cert_file(ca_file, ca_dir);
setClientCertCACerts(ssl_vc->ssl, ssl_vc->get_ca_cert_file(), ssl_vc->get_ca_cert_dir());
return SSL_TLSEXT_ERR_OK;
}
bool
VerifyClient::TestClientSNIAction(const char * /* servername ATS_UNUSED */, const IpEndpoint & /* ep ATS_UNUSED */,
int & /* policy ATS_UNUSED */) const
{
// This action is triggered by a SNI if it was set
return true;
}
int
HostSniPolicy::SNIAction(SSL & /* ssl ATS_UNUSED */, const Context & /* ctx ATS_UNUSED */) const
{
// On action this doesn't do anything
return SSL_TLSEXT_ERR_OK;
}
bool
HostSniPolicy::TestClientSNIAction(const char * /* servername ATS_UNUSED */, const IpEndpoint & /* ep ATS_UNUSED */,
int &in_policy) const
{
// Update the policy when testing
in_policy = this->policy;
// But this action didn't really trigger during the action phase
return false;
}
TLSValidProtocols::TLSValidProtocols(unsigned long protocols) : unset(false), protocol_mask(protocols)
{
Warning("valid_tls_versions_in is deprecated. Use valid_tls_version_min_in and valid_tls_version_max_in instead.");
}
int
TLSValidProtocols::SNIAction(SSL &ssl, const Context & /* ctx ATS_UNUSED */) const
{
auto snis = TLSSNISupport::getInstance(&ssl);
auto tbs = TLSBasicSupport::getInstance(&ssl);
if (snis == nullptr || tbs == nullptr) {
return SSL_TLSEXT_ERR_OK;
}
if (this->min_ver >= 0 || this->max_ver >= 0) {
const char *servername = snis->get_sni_server_name();
Dbg(dbg_ctl_ssl_sni, "TLSValidProtocol min=%d, max=%d, fqdn [%s]", this->min_ver, this->max_ver, servername);
tbs->set_valid_tls_version_min(this->min_ver);
tbs->set_valid_tls_version_max(this->max_ver);
} else {
if (!unset) {
const char *servername = snis->get_sni_server_name();
Dbg(dbg_ctl_ssl_sni, "TLSValidProtocol param 0%x, fqdn [%s]", static_cast<unsigned int>(this->protocol_mask), servername);
tbs->set_valid_tls_protocols(protocol_mask, TLSValidProtocols::max_mask);
}
}
return SSL_TLSEXT_ERR_OK;
}
SNI_IpAllow::SNI_IpAllow(std::string &ip_allow_list, std::string const &servername) : server_name(servername)
{
swoc::TextView content{ip_allow_list};
if (content && content[0] == '@') {
std::error_code ec;
swoc::file::path path{content.remove_prefix(1)};
if (path.is_relative()) {
path = swoc::file::path(Layout::get()->sysconfdir) / path;
}
ip_allow_list = swoc::file::load(path, ec);
if (ec) {
swoc::LocalBufferWriter<1024> w;
w.print("SNIConfig unable to load file {} - {}", path.string(), ec);
Warning("%.*s", int(w.size()), w.data());
}
}
this->load(ip_allow_list, servername);
}
void
SNI_IpAllow::load(swoc::TextView content, swoc::TextView server_name)
{
static constexpr swoc::TextView delim{",\n"};
while (!content.ltrim(delim).empty()) {
swoc::TextView token{content.take_prefix_at(delim)};
if (swoc::IPRange r; r.load(token)) {
Dbg(dbg_ctl_ssl_sni, "%.*s added to the ip_allow token %.*s", static_cast<int>(token.size()), token.data(),
int(server_name.size()), server_name.data());
ip_addrs.fill(r);
} else {
Dbg(dbg_ctl_ssl_sni, "%.*s is not a valid format", static_cast<int>(token.size()), token.data());
break;
}
}
}
int
SNI_IpAllow::SNIAction(SSL &ssl, ActionItem::Context const & /* ctx ATS_UNUSED */) const
{
// i.e, ip filtering is not required
if (ip_addrs.count() == 0) {
return SSL_TLSEXT_ERR_OK;
}
auto ssl_vc = SSLNetVCAccess(&ssl);
const sockaddr *client_ip = nullptr;
for (int i = 0; i < IpAllow::Subject::MAX_SUBJECTS; ++i) {
if (IpAllow::Subject::PEER == IpAllow::subjects[i]) {
client_ip = ssl_vc->get_effective_remote_addr();
break;
} else if (IpAllow::Subject::PROXY == IpAllow::subjects[i] &&
ssl_vc->get_proxy_protocol_version() != ProxyProtocolVersion::UNDEFINED) {
client_ip = ssl_vc->get_proxy_protocol_src_addr();
break;
}
}
swoc::IPAddr ip = swoc::IPAddr(client_ip);
// check the allowed ips
if (ip_addrs.contains(ip)) {
return SSL_TLSEXT_ERR_OK;
} else {
swoc::LocalBufferWriter<256> w;
w.print("{} is not allowed for {} - denying connection\0", ip, server_name);
Dbg(dbg_ctl_ssl_sni, "%s", w.data());
return SSL_TLSEXT_ERR_ALERT_FATAL;
}
}
bool
SNI_IpAllow::TestClientSNIAction(char const * /* servrername ATS_UNUSED */, IpEndpoint const &ep,
int & /* policy ATS_UNUSED */) const
{
return ip_addrs.contains(swoc::IPAddr(ep));
}
int
OutboundSNIPolicy::SNIAction(SSL &ssl, const Context & /* ctx ATS_UNUSED */) const
{
if (!policy.empty()) {
if (auto snis = TLSSNISupport::getInstance(&ssl)) {
snis->hints_from_sni.outbound_sni_policy = policy;
}
}
return SSL_TLSEXT_ERR_OK;
}
int
ServerMaxEarlyData::SNIAction([[maybe_unused]] SSL &ssl, const Context & /* ctx ATS_UNUSED */) const
{
#if TS_HAS_TLS_EARLY_DATA
auto snis = TLSSNISupport::getInstance(&ssl);
auto eds = TLSEarlyDataSupport::getInstance(&ssl);
if (snis == nullptr || eds == nullptr) {
return SSL_TLSEXT_ERR_OK;
}
snis->hints_from_sni.server_max_early_data = server_max_early_data;
const uint32_t server_recv_max_early_data =
server_max_early_data > 0 ? std::max(server_max_early_data, TLSEarlyDataSupport::DEFAULT_MAX_EARLY_DATA_SIZE) : 0;
eds->update_early_data_config(&ssl, server_max_early_data, server_recv_max_early_data);
#endif
return SSL_TLSEXT_ERR_OK;
}
int
ServerSessionTicketEnabled::SNIAction(SSL &ssl, const Context & /* ctx ATS_UNUSED */) const
{
#if TS_HAS_TLS_SESSION_TICKET
if (auto snis = TLSSNISupport::getInstance(&ssl)) {
const char *servername = snis->get_sni_server_name();
Dbg(dbg_ctl_ssl_sni, "Setting session ticket support from sni.yaml to %d for fqdn [%s]", session_ticket_enabled, servername);
snis->hints_from_sni.ssl_ticket_enabled = session_ticket_enabled;
}
// Apply the ticket enable/disable flag immediately so the current handshake
// sees the per-SNI override before TLS session ticket processing kicks in.
if (session_ticket_enabled != 0) {
SSL_clear_options(&ssl, SSL_OP_NO_TICKET);
} else {
SSL_set_options(&ssl, SSL_OP_NO_TICKET);
}
#endif
return SSL_TLSEXT_ERR_OK;
}
int
ServerSessionTicketNumber::SNIAction(SSL &ssl, const Context & /* ctx ATS_UNUSED */) const
{
#if TS_HAS_TLS_SESSION_TICKET
if (auto snis = TLSSNISupport::getInstance(&ssl)) {
const char *servername = snis->get_sni_server_name();
Dbg(dbg_ctl_ssl_sni, "Setting session ticket count from sni.yaml to %d for fqdn [%s]", session_ticket_number, servername);
snis->hints_from_sni.ssl_ticket_number = session_ticket_number;
}
#endif
return SSL_TLSEXT_ERR_OK;
}
int
ServerCipherSuite::SNIAction(SSL &ssl, const Context & /* ctx ATS_UNUSED */) const
{
if (server_cipher_suite.empty()) {
return SSL_TLSEXT_ERR_OK;
}
auto tbs = TLSBasicSupport::getInstance(&ssl);
if (tbs == nullptr) {
return SSL_TLSEXT_ERR_OK;
}
Dbg(dbg_ctl_ssl_sni, "Setting pre TLSv1.3 cipher suite from server_cipher_suite to %s", server_cipher_suite.c_str());
tbs->set_legacy_cipher_suite(server_cipher_suite);
return SSL_TLSEXT_ERR_OK;
}
int
ServerTLSv1_3CipherSuites::SNIAction(SSL &ssl, const Context & /* ctx ATS_UNUSED */) const
{
if (server_TLSV1_3_cipher_suites.empty()) {
return SSL_TLSEXT_ERR_OK;
}
auto tbs = TLSBasicSupport::getInstance(&ssl);
if (tbs == nullptr) {
return SSL_TLSEXT_ERR_OK;
}
Dbg(dbg_ctl_ssl_sni, "Setting TLSv1.3 or later cipher suites from server_TLSv1_3_cipher_suites to %s",
server_TLSV1_3_cipher_suites.c_str());
tbs->set_cipher_suite(server_TLSV1_3_cipher_suites);
return SSL_TLSEXT_ERR_OK;
}
int
ServerGroupsList::SNIAction(SSL &ssl, const Context & /* ctx ATS_UNUSED */) const
{
if (server_groups_list.empty()) {
return SSL_TLSEXT_ERR_OK;
}
auto tbs = TLSBasicSupport::getInstance(&ssl);
if (tbs == nullptr) {
return SSL_TLSEXT_ERR_OK;
}
Dbg(dbg_ctl_ssl_sni, "Setting groups list from server_groups_list to %s", server_groups_list.c_str());
if (!tbs->set_groups_list(server_groups_list)) {
Error("Invalid server_groups_list: %s", server_groups_list.c_str());
return SSL_TLSEXT_ERR_ALERT_WARNING;
}
return SSL_TLSEXT_ERR_OK;
}