/
node_quic_session-inl.h
447 lines (373 loc) Β· 13.6 KB
/
node_quic_session-inl.h
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
#ifndef SRC_QUIC_NODE_QUIC_SESSION_INL_H_
#define SRC_QUIC_NODE_QUIC_SESSION_INL_H_
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
#include "debug_utils-inl.h"
#include "node_crypto.h"
#include "crypto/crypto_common.h"
#include "node_quic_crypto.h"
#include "node_quic_session.h"
#include "node_quic_socket-inl.h"
#include "node_quic_stream-inl.h"
#include <openssl/ssl.h>
#include <memory>
#include <string>
namespace node {
namespace quic {
void QuicSessionConfig::GenerateStatelessResetToken(
QuicSession* session,
const QuicCID& cid) {
transport_params.stateless_reset_token_present = 1;
StatelessResetToken token(
transport_params.stateless_reset_token,
session->socket()->session_reset_secret(),
cid);
Debug(session, "Generated stateless reset token %s for CID %s", token, cid);
}
void QuicSessionConfig::GeneratePreferredAddressToken(
ConnectionIDStrategy connection_id_strategy,
QuicSession* session,
QuicCID* pscid) {
connection_id_strategy(session, pscid->cid(), kScidLen);
transport_params.preferred_address.cid = **pscid;
StatelessResetToken(
transport_params.preferred_address.stateless_reset_token,
session->socket()->session_reset_secret(),
*pscid);
}
void QuicSessionConfig::set_original_connection_id(
const QuicCID& ocid,
const QuicCID& scid) {
if (ocid) {
transport_params.original_dcid = *ocid;
transport_params.retry_scid = *scid;
transport_params.retry_scid_present = 1;
} else {
transport_params.original_dcid = *scid;
}
}
void QuicSessionConfig::set_qlog(const ngtcp2_qlog_settings& qlog_) {
qlog = qlog_;
}
QuicCryptoContext::QuicCryptoContext(
QuicSession* session,
BaseObjectPtr<crypto::SecureContext> secure_context,
ngtcp2_crypto_side side,
uint32_t options) :
session_(session),
secure_context_(secure_context),
side_(side),
options_(options) {
ssl_.reset(SSL_new(secure_context_->ctx_.get()));
CHECK(ssl_);
}
void QuicCryptoContext::Initialize() {
InitializeTLS(session(), ssl_);
}
// Cancels and frees any remaining outbound handshake data
// at each crypto level.
uint64_t QuicCryptoContext::Cancel() {
uint64_t len = handshake_[0].Cancel();
len += handshake_[1].Cancel();
len += handshake_[2].Cancel();
return len;
}
v8::MaybeLocal<v8::Value> QuicCryptoContext::ocsp_response() const {
Environment* env = session()->env();
return crypto::GetSSLOCSPResponse(
env,
ssl_.get(),
v8::Undefined(env->isolate()));
}
ngtcp2_crypto_level QuicCryptoContext::read_crypto_level() const {
return from_ossl_level(SSL_quic_read_level(ssl_.get()));
}
ngtcp2_crypto_level QuicCryptoContext::write_crypto_level() const {
return from_ossl_level(SSL_quic_write_level(ssl_.get()));
}
// TLS Keylogging is enabled per-QuicSession by attaching an handler to the
// "keylog" event. Each keylog line is emitted to JavaScript where it can
// be routed to whatever destination makes sense. Typically, this will be
// to a keylog file that can be consumed by tools like Wireshark to intercept
// and decrypt QUIC network traffic.
void QuicCryptoContext::Keylog(const char* line) {
if (UNLIKELY(session_->state_->keylog_enabled))
session_->listener()->OnKeylog(line, strlen(line));
}
// Following a pause in the handshake for OCSP or client hello, we kickstart
// the handshake again here by triggering ngtcp2 to serialize data.
void QuicCryptoContext::ResumeHandshake() {
// We haven't received any actual new handshake data but calling
// this will trigger the handshake to continue.
Receive(read_crypto_level(), 0, nullptr, 0);
session_->SendPendingData();
}
// For 0RTT, this sets the TLS session data from the given buffer.
bool QuicCryptoContext::set_session(crypto::SSLSessionPointer session) {
if (side_ == NGTCP2_CRYPTO_SIDE_CLIENT && session != nullptr) {
set_early_data(
SSL_SESSION_get_max_early_data(session.get()) == 0xffffffffUL);
}
return crypto::SetTLSSession(ssl_, std::move(session));
}
v8::MaybeLocal<v8::Array> QuicCryptoContext::hello_ciphers() const {
return crypto::GetClientHelloCiphers(session()->env(), ssl_);
}
v8::MaybeLocal<v8::Value> QuicCryptoContext::cipher_name() const {
return crypto::GetCipherName(session()->env(), ssl_);
}
v8::MaybeLocal<v8::Value> QuicCryptoContext::cipher_version() const {
return crypto::GetCipherVersion(session()->env(), ssl_);
}
const char* QuicCryptoContext::servername() const {
return crypto::GetServerName(ssl_.get());
}
const char* QuicCryptoContext::hello_alpn() const {
return crypto::GetClientHelloALPN(ssl_);
}
const char* QuicCryptoContext::hello_servername() const {
return crypto::GetClientHelloServerName(ssl_);
}
v8::MaybeLocal<v8::Object> QuicCryptoContext::ephemeral_key() const {
return crypto::GetEphemeralKey(session()->env(), ssl_);
}
v8::MaybeLocal<v8::Value> QuicCryptoContext::peer_cert(bool abbreviated) const {
return crypto::GetPeerCert(
session()->env(),
ssl_,
abbreviated,
session()->is_server());
}
v8::MaybeLocal<v8::Value> QuicCryptoContext::cert() const {
return crypto::GetCert(session()->env(), ssl_);
}
std::string QuicCryptoContext::selected_alpn() const {
const unsigned char* alpn_buf = nullptr;
unsigned int alpnlen;
SSL_get0_alpn_selected(ssl_.get(), &alpn_buf, &alpnlen);
return alpnlen ?
std::string(reinterpret_cast<const char*>(alpn_buf), alpnlen) :
std::string();
}
bool QuicCryptoContext::early_data() const {
return
(is_early_data() &&
SSL_get_early_data_status(ssl_.get()) == SSL_EARLY_DATA_ACCEPTED) ||
SSL_get_max_early_data(ssl_.get()) == 0xffffffffUL;
}
void QuicCryptoContext::set_tls_alert(int err) {
Debug(session(), "TLS Alert [%d]: %s", err, SSL_alert_type_string_long(err));
session_->set_last_error(QuicError(QUIC_ERROR_CRYPTO, err));
}
QuicApplication::QuicApplication(QuicSession* session) : session_(session) {}
void QuicApplication::set_stream_fin(int64_t stream_id) {
BaseObjectPtr<QuicStream> stream = session()->FindStream(stream_id);
CHECK(stream);
stream->set_fin_sent();
}
std::unique_ptr<QuicPacket> QuicApplication::CreateStreamDataPacket() {
return QuicPacket::Create(
"stream data",
session()->max_packet_length());
}
Environment* QuicApplication::env() const {
return session()->env();
}
// Extends the stream-level flow control by the given number of bytes.
void QuicSession::ExtendStreamOffset(int64_t stream_id, size_t amount) {
Debug(this, "Extending max stream %" PRId64 " offset by %" PRId64 " bytes",
stream_id, amount);
ngtcp2_conn_extend_max_stream_offset(
connection(),
stream_id,
amount);
}
// Extends the connection-level flow control for the entire session by
// the given number of bytes.
void QuicSession::ExtendOffset(size_t amount) {
Debug(this, "Extending session offset by %" PRId64 " bytes", amount);
ngtcp2_conn_extend_max_offset(connection(), amount);
}
// Copies the local transport params into the given struct for serialization.
void QuicSession::GetLocalTransportParams(ngtcp2_transport_params* params) {
CHECK(!is_destroyed());
ngtcp2_conn_get_local_transport_params(connection(), params);
}
// Gets the QUIC version negotiated for this QuicSession
uint32_t QuicSession::negotiated_version() const {
CHECK(!is_destroyed());
return ngtcp2_conn_get_negotiated_version(connection());
}
bool QuicSession::is_handshake_completed() const {
DCHECK(!is_destroyed());
return ngtcp2_conn_get_handshake_completed(connection());
}
void QuicSession::ShutdownStream(int64_t stream_id, uint64_t code) {
if (is_in_closing_period() ||
is_in_draining_period() ||
is_silent_closing()) {
return; // Nothing to do because we can't send any frames.
}
SendSessionScope send_scope(this);
ngtcp2_conn_shutdown_stream(connection(), stream_id, 0);
}
// When a QuicSession hits the idle timeout, it is to be silently and
// immediately closed without attempting to send any additional data to
// the peer. All existing streams are abandoned and closed.
void QuicSession::OnIdleTimeout() {
if (!is_destroyed()) {
if (state_->idle_timeout == 1) {
Debug(this, "Idle timeout");
Close(QuicSessionListener::SESSION_CLOSE_FLAG_SILENT);
return;
}
state_->idle_timeout = 1;
UpdateClosingTimer();
}
}
QuicCID QuicSession::dcid() const {
return QuicCID(ngtcp2_conn_get_dcid(connection()));
}
void QuicSession::CheckAllocatedSize(size_t previous_size) const {
CHECK_GE(current_ngtcp2_memory_, previous_size);
}
void QuicSession::IncreaseAllocatedSize(size_t size) {
current_ngtcp2_memory_ += size;
}
void QuicSession::DecreaseAllocatedSize(size_t size) {
current_ngtcp2_memory_ -= size;
}
uint64_t QuicSession::max_data_left() const {
return ngtcp2_conn_get_max_data_left(connection());
}
uint64_t QuicSession::max_local_streams_uni() const {
return ngtcp2_conn_get_max_local_streams_uni(connection());
}
void QuicSession::set_last_error(QuicError error) {
last_error_ = error;
}
void QuicSession::set_last_error(int32_t family, uint64_t code) {
set_last_error({ family, code });
}
void QuicSession::set_last_error(int32_t family, int code) {
set_last_error({ family, code });
}
bool QuicSession::is_in_closing_period() const {
return ngtcp2_conn_is_in_closing_period(connection());
}
bool QuicSession::is_in_draining_period() const {
return ngtcp2_conn_is_in_draining_period(connection());
}
bool QuicSession::HasStream(int64_t id) const {
return streams_.find(id) != std::end(streams_);
}
bool QuicSession::allow_early_data() const {
// TODO(@jasnell): For now, we always allow early data.
// Later there will be reasons we do not want to allow
// it, such as lack of available system resources.
return true;
}
void QuicSession::SetSessionTicketAppData(
const SessionTicketAppData& app_data) {
application_->SetSessionTicketAppData(app_data);
}
SessionTicketAppData::Status QuicSession::GetSessionTicketAppData(
const SessionTicketAppData& app_data,
SessionTicketAppData::Flag flag) {
return application_->GetSessionTicketAppData(app_data, flag);
}
bool QuicSession::is_server() const {
return crypto_context_->side() == NGTCP2_CRYPTO_SIDE_SERVER;
}
void QuicSession::StartGracefulClose() {
set_graceful_closing();
RecordTimestamp(&QuicSessionStats::closing_at);
}
// The connection ID Strategy is a function that generates
// connection ID values. By default these are generated randomly.
void QuicSession::set_connection_id_strategy(ConnectionIDStrategy strategy) {
CHECK_NOT_NULL(strategy);
connection_id_strategy_ = strategy;
}
bool QuicSession::is_unable_to_send_packets() {
return NgCallbackScope::InNgCallbackScope(this) ||
is_destroyed() ||
is_in_draining_period() ||
(is_server() && is_in_closing_period()) ||
socket() == nullptr;
}
void QuicSession::set_preferred_address_strategy(
PreferredAddressStrategy strategy) {
preferred_address_strategy_ = strategy;
}
QuicSocket* QuicSession::socket() const {
return socket_.get();
}
// Indicates that the stream is blocked from transmitting any
// data. The specific handling of this is application specific.
// By default, we keep track of statistics but leave it up to
// the application to perform specific handling.
void QuicSession::StreamDataBlocked(int64_t stream_id) {
IncrementStat(&QuicSessionStats::block_count);
listener_->OnStreamBlocked(stream_id);
}
// When a server advertises a preferred address in its initial
// transport parameters, ngtcp2 on the client side will trigger
// the OnSelectPreferredAdddress callback which will call this.
// The paddr argument contains the advertised preferred address.
// If the new address is going to be used, it needs to be copied
// over to dest, otherwise dest is left alone. There are two
// possible strategies that we currently support via user
// configuration: use the preferred address or ignore it.
void QuicSession::SelectPreferredAddress(
const PreferredAddress& preferred_address) {
CHECK(!is_server());
preferred_address_strategy_(this, preferred_address);
}
// This variant of SendPacket is used by QuicApplication
// instances to transmit a packet and update the network
// path used at the same time.
bool QuicSession::SendPacket(
std::unique_ptr<QuicPacket> packet,
const ngtcp2_path_storage& path) {
UpdateEndpoint(path.path);
return SendPacket(std::move(packet));
}
// Set the transport parameters received from the remote peer
void QuicSession::set_remote_transport_params() {
DCHECK(!is_destroyed());
ngtcp2_conn_get_remote_transport_params(connection(), &transport_params_);
set_transport_params_set();
}
// Submits information headers only if the selected application
// supports headers.
bool QuicSession::SubmitInformation(
int64_t stream_id,
v8::Local<v8::Array> headers) {
return application_->SubmitInformation(stream_id, headers);
}
// Submits initial headers only if the selected application
// supports headers. For http3, for instance, this is the
// method used to submit both request and response headers.
bool QuicSession::SubmitHeaders(
int64_t stream_id,
v8::Local<v8::Array> headers,
uint32_t flags) {
return application_->SubmitHeaders(stream_id, headers, flags);
}
// Submits trailing headers only if the selected application
// supports headers.
bool QuicSession::SubmitTrailers(
int64_t stream_id,
v8::Local<v8::Array> headers) {
return application_->SubmitTrailers(stream_id, headers);
}
// Submits a new push stream
BaseObjectPtr<QuicStream> QuicSession::SubmitPush(
int64_t stream_id,
v8::Local<v8::Array> headers) {
return application_->SubmitPush(stream_id, headers);
}
} // namespace quic
} // namespace node
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
#endif // SRC_QUIC_NODE_QUIC_SESSION_INL_H_