forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdtls_session.cc
More file actions
691 lines (579 loc) · 21.2 KB
/
Copy pathdtls_session.cc
File metadata and controls
691 lines (579 loc) · 21.2 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
#include "dtls_session.h"
#include "dtls.h"
#include "dtls_endpoint.h"
#if HAVE_OPENSSL && HAVE_DTLS
#include <aliased_struct-inl.h>
#include <async_wrap-inl.h>
#include <base_object-inl.h>
#include <env-inl.h>
#include <memory_tracker-inl.h>
#include <node_buffer.h>
#include <node_errors.h>
#include <node_sockaddr-inl.h>
#include <timer_wrap-inl.h>
#include <util-inl.h>
#include <openssl/err.h>
#include <openssl/srtp.h>
#include <openssl/ssl.h>
#include <cstring>
namespace node {
using v8::Context;
using v8::Function;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::HandleScope;
using v8::Isolate;
using v8::Local;
using v8::MaybeLocal;
using v8::Object;
using v8::String;
using v8::Value;
namespace dtls {
DTLSSession::DTLSSession(Environment* env,
Local<Object> wrap,
DTLSEndpoint* endpoint,
ncrypto::SSLPointer ssl,
BIO* enc_in,
BIO* enc_out,
const SocketAddress& remote,
bool is_server)
: AsyncWrap(env, wrap, PROVIDER_DTLS_SESSION),
endpoint_(endpoint),
ssl_(std::move(ssl)),
enc_in_(enc_in),
enc_out_(enc_out),
retransmit_timer_(env,
[this] {
if (destroyed_) return;
DTLS_STAT_INCREMENT(DTLSSessionStats,
retransmit_count);
int ret = DTLSv1_handle_timeout(ssl_.get());
if (ret < 0) {
// Handshake timeout expired.
HandleScope hs(this->env()->isolate());
Context::Scope cs(this->env()->context());
Local<Value> argv[] = {
String::NewFromUtf8(this->env()->isolate(),
"DTLS handshake timeout")
.ToLocalChecked(),
};
EmitCallback(DTLS_CB_SESSION_ERROR, 1, argv);
return;
}
Cycle();
}),
remote_address_(remote),
is_server_(is_server),
state_(env->isolate()),
stats_(env->isolate()) {
MakeWeak();
DTLS_STAT_RECORD_TIMESTAMP(DTLSSessionStats, created_at);
retransmit_timer_.Unref();
// Update shared state.
state_->handshaking = 1;
state_->open = 0;
// Store this session in SSL app data for callbacks.
SSL_set_app_data(ssl_.get(), this);
// Enable keylog for TLS key export (useful for Wireshark debugging).
SSL_CTX_set_keylog_callback(SSL_get_SSL_CTX(ssl_.get()), SSLKeylogCallback);
// Set the MTU on the SSL object.
SSL_set_mtu(ssl_.get(), endpoint->mtu());
}
DTLSSession::~DTLSSession() = default;
Local<FunctionTemplate> DTLSSession::GetConstructorTemplate(Environment* env) {
auto tmpl = env->dtls_session_constructor_template();
if (tmpl.IsEmpty()) {
Isolate* isolate = env->isolate();
tmpl = NewFunctionTemplate(isolate, New);
tmpl->SetClassName(FIXED_ONE_BYTE_STRING(isolate, "DTLSSession"));
tmpl->InstanceTemplate()->SetInternalFieldCount(
AsyncWrap::kInternalFieldCount);
SetProtoMethod(isolate, tmpl, "send", DoSend);
SetProtoMethod(isolate, tmpl, "close", DoClose);
SetProtoMethod(isolate, tmpl, "destroy", DoDestroy);
SetProtoMethod(isolate, tmpl, "getState", GetState);
SetProtoMethod(isolate, tmpl, "getStats", GetStats);
SetProtoMethod(isolate, tmpl, "getRemoteAddress", GetRemoteAddress);
SetProtoMethod(isolate, tmpl, "getProtocol", GetProtocol);
SetProtoMethod(isolate, tmpl, "getCipher", GetCipher);
SetProtoMethod(isolate, tmpl, "getPeerCertificate", GetPeerCertificate);
SetProtoMethod(isolate, tmpl, "getALPNProtocol", GetALPNProtocol);
SetProtoMethod(isolate, tmpl, "exportKeyingMaterial", ExportKeyingMaterial);
SetProtoMethod(isolate, tmpl, "getSRTPProfile", GetSRTPProfile);
SetProtoMethod(isolate, tmpl, "setServername", SetServername);
SetProtoMethod(isolate, tmpl, "getServername", GetServername);
env->set_dtls_session_constructor_template(tmpl);
}
return tmpl;
}
void DTLSSession::InitPerContext(Local<Object> target,
Local<Context> context,
Environment* env) {
SetConstructorFunction(
context, target, "DTLSSession", GetConstructorTemplate(env));
}
void DTLSSession::RegisterExternalReferences(
ExternalReferenceRegistry* registry) {
registry->Register(New);
registry->Register(DoSend);
registry->Register(DoClose);
registry->Register(DoDestroy);
registry->Register(GetState);
registry->Register(GetStats);
registry->Register(GetRemoteAddress);
registry->Register(GetProtocol);
registry->Register(GetCipher);
registry->Register(GetPeerCertificate);
registry->Register(GetALPNProtocol);
registry->Register(ExportKeyingMaterial);
registry->Register(GetSRTPProfile);
registry->Register(SetServername);
registry->Register(GetServername);
}
BaseObjectPtr<DTLSSession> DTLSSession::Create(Environment* env,
DTLSEndpoint* endpoint,
SSL_CTX* ssl_ctx,
const SocketAddress& remote,
bool is_server) {
// Create the SSL object.
SSL* ssl_raw = SSL_new(ssl_ctx);
if (ssl_raw == nullptr) {
THROW_ERR_CRYPTO_OPERATION_FAILED(env, "SSL_new failed");
return {};
}
ncrypto::SSLPointer ssl(ssl_raw);
// Create memory BIOs for encrypted data I/O.
BIO* enc_in = BIO_new(BIO_s_mem());
BIO* enc_out = BIO_new(BIO_s_mem());
if (enc_in == nullptr || enc_out == nullptr) {
BIO_free(enc_in);
BIO_free(enc_out);
THROW_ERR_CRYPTO_OPERATION_FAILED(env, "BIO_new failed");
return {};
}
// Make the BIOs non-blocking.
BIO_set_mem_eof_return(enc_in, -1);
BIO_set_mem_eof_return(enc_out, -1);
// Associate BIOs with the SSL object. SSL_set_bio takes ownership.
SSL_set_bio(ssl.get(), enc_in, enc_out);
// Set the MTU (since we use SSL_OP_NO_QUERY_MTU).
SSL_set_mtu(ssl.get(), endpoint->mtu());
// Set the handshake direction.
if (is_server) {
SSL_set_accept_state(ssl.get());
} else {
SSL_set_connect_state(ssl.get());
}
// Create the JS wrapper object.
Local<FunctionTemplate> tmpl = GetConstructorTemplate(env);
Local<Object> obj;
if (!tmpl->InstanceTemplate()->NewInstance(env->context()).ToLocal(&obj)) {
return {};
}
auto session = MakeBaseObject<DTLSSession>(
env, obj, endpoint, std::move(ssl), enc_in, enc_out, remote, is_server);
return session;
}
BaseObjectPtr<DTLSSession> DTLSSession::CreateFromSSL(
Environment* env,
DTLSEndpoint* endpoint,
ncrypto::SSLPointer ssl,
BIO* enc_in,
BIO* enc_out,
const SocketAddress& remote) {
Local<FunctionTemplate> tmpl = GetConstructorTemplate(env);
Local<Object> obj;
if (!tmpl->InstanceTemplate()->NewInstance(env->context()).ToLocal(&obj)) {
return {};
}
return MakeBaseObject<DTLSSession>(env,
obj,
endpoint,
std::move(ssl),
enc_in,
enc_out,
remote,
true /* is_server */);
}
void DTLSSession::New(const FunctionCallbackInfo<Value>& args) {
// Sessions are created internally via DTLSSession::Create,
// not directly from JS.
CHECK(args.IsConstructCall());
}
void DTLSSession::Receive(const uint8_t* data, size_t len) {
if (destroyed_ || closed_) return;
// Write the encrypted datagram into enc_in_ BIO.
int written = BIO_write(enc_in_, data, len);
if (written <= 0) return;
// Run the state machine.
Cycle();
}
void DTLSSession::Cycle() {
if (destroyed_) return;
// Prevent infinite recursion.
if (++cycle_depth_ > 1) {
cycle_depth_--;
return;
}
HandleScope handle_scope(env()->isolate());
Context::Scope context_scope(env()->context());
// If handshake is not yet complete, drive it forward.
if (!handshake_complete_) {
int ret = SSL_do_handshake(ssl_.get());
if (ret <= 0) {
int err = SSL_get_error(ssl_.get(), ret);
if (err == SSL_ERROR_SSL) {
unsigned long ossl_err = ERR_get_error(); // NOLINT(runtime/int)
char err_buf[256];
ERR_error_string_n(ossl_err, err_buf, sizeof(err_buf));
Local<Value> argv[] = {
String::NewFromUtf8(env()->isolate(), err_buf).ToLocalChecked(),
};
EmitCallback(DTLS_CB_SESSION_ERROR, 1, argv);
cycle_depth_--;
return;
}
// SSL_ERROR_WANT_READ/WRITE is normal during handshake.
}
// Flush any handshake data produced.
EncOut();
// Check if handshake just completed.
if (SSL_is_init_finished(ssl_.get()) && !handshake_complete_) {
handshake_complete_ = true;
state_->handshaking = 0;
state_->open = 1;
DTLS_STAT_RECORD_TIMESTAMP(DTLSSessionStats, handshake_completed_at);
Local<Value> argv[] = {
String::NewFromUtf8(env()->isolate(), SSL_get_version(ssl_.get()))
.ToLocalChecked(),
};
EmitCallback(DTLS_CB_SESSION_HANDSHAKE, 1, argv);
}
}
// Read any decrypted application data.
ClearOut();
// Flush any pending encrypted output.
EncOut();
UpdateTimer();
cycle_depth_--;
}
void DTLSSession::ClearOut() {
if (destroyed_) return;
// Try to read decrypted application data from OpenSSL.
uint8_t buf[65536];
int read;
while ((read = SSL_read(ssl_.get(), buf, sizeof(buf))) > 0) {
DTLS_STAT_INCREMENT_N(DTLSSessionStats, bytes_received, read);
DTLS_STAT_INCREMENT(DTLSSessionStats, messages_received);
// Emit the data to JS via callback.
Local<Value> argv[] = {
Buffer::Copy(env(), reinterpret_cast<const char*>(buf), read)
.ToLocalChecked(),
};
EmitCallback(DTLS_CB_SESSION_MESSAGE, 1, argv);
}
int err = SSL_get_error(ssl_.get(), read);
switch (err) {
case SSL_ERROR_WANT_READ:
case SSL_ERROR_WANT_WRITE:
// Normal - need more data or need to flush.
break;
case SSL_ERROR_ZERO_RETURN:
// Peer sent close_notify.
if (!closed_) {
closed_ = true;
state_->closing = 1;
state_->open = 0;
// Send our close_notify back.
SSL_shutdown(ssl_.get());
EncOut();
Local<Value> argv[] = {};
EmitCallback(DTLS_CB_SESSION_CLOSE, 0, argv);
}
break;
case SSL_ERROR_SSL: {
// SSL error during handshake or data exchange.
unsigned long ossl_err = ERR_get_error(); // NOLINT(runtime/int)
char err_buf[256];
ERR_error_string_n(ossl_err, err_buf, sizeof(err_buf));
Local<Value> argv[] = {
String::NewFromUtf8(env()->isolate(), err_buf).ToLocalChecked(),
};
EmitCallback(DTLS_CB_SESSION_ERROR, 1, argv);
break;
}
default:
break;
}
}
void DTLSSession::EncOut() {
if (destroyed_) return;
auto ep = endpoint_.get();
if (ep == nullptr) return;
// Read encrypted data from enc_out_ BIO and send via UDP.
// Read in a loop since there may be multiple DTLS records.
uint8_t buf[65536];
int read;
while ((read = BIO_read(enc_out_, buf, sizeof(buf))) > 0) {
ep->SendTo(remote_address_, buf, read);
}
}
void DTLSSession::UpdateTimer() {
if (destroyed_) return;
struct timeval tv;
if (DTLSv1_get_timeout(ssl_.get(), &tv)) {
uint64_t timeout_ms = tv.tv_sec * 1000 + tv.tv_usec / 1000;
if (timeout_ms == 0) timeout_ms = 1; // Minimum 1ms.
retransmit_timer_.Update(timeout_ms);
} else {
// No timeout needed (handshake complete or not started).
retransmit_timer_.Stop();
}
}
int DTLSSession::Send(const uint8_t* data, size_t len) {
if (destroyed_ || closed_) return -1;
if (!handshake_complete_) {
// Can't send application data before handshake.
return -1;
}
int written = SSL_write(ssl_.get(), data, len);
if (written > 0) {
DTLS_STAT_INCREMENT_N(DTLSSessionStats, bytes_sent, written);
DTLS_STAT_INCREMENT(DTLSSessionStats, messages_sent);
EncOut();
}
return written;
}
void DTLSSession::Close() {
if (destroyed_ || closed_) return;
closed_ = true;
state_->closing = 1;
DTLS_STAT_RECORD_TIMESTAMP(DTLSSessionStats, closing_at);
// Send close_notify.
int ret = SSL_shutdown(ssl_.get());
if (ret == 0) {
// Need to call again for bidirectional shutdown.
SSL_shutdown(ssl_.get());
}
EncOut();
retransmit_timer_.Stop();
state_->open = 0;
// Notify JS.
HandleScope handle_scope(env()->isolate());
Context::Scope context_scope(env()->context());
Local<Value> argv[] = {};
EmitCallback(DTLS_CB_SESSION_CLOSE, 0, argv);
}
void DTLSSession::Destroy() {
if (destroyed_) return;
destroyed_ = true;
closed_ = true;
state_->destroyed = 1;
DTLS_STAT_RECORD_TIMESTAMP(DTLSSessionStats, destroyed_at);
state_->open = 0;
state_->handshaking = 0;
retransmit_timer_.Close();
// Promote to strong ref to keep endpoint alive during removal,
// then release our weak pointer.
BaseObjectPtr<DTLSEndpoint> ep = endpoint_;
endpoint_.reset();
if (ep) ep->RemoveSession(remote_address_);
}
void DTLSSession::SSLKeylogCallback(const SSL* ssl, const char* line) {
DTLSSession* session = static_cast<DTLSSession*>(SSL_get_app_data(ssl));
if (session == nullptr || session->destroyed_) return;
HandleScope handle_scope(session->env()->isolate());
Context::Scope context_scope(session->env()->context());
Local<Value> argv[] = {
String::NewFromUtf8(session->env()->isolate(), line).ToLocalChecked(),
};
session->EmitCallback(DTLS_CB_SESSION_KEYLOG, 1, argv);
}
MaybeLocal<Value> DTLSSession::EmitCallback(int cb_index,
int argc,
Local<Value>* argv) {
auto ep = endpoint_.get();
if (ep == nullptr) return MaybeLocal<Value>();
Local<Function> cb = ep->GetCallback(cb_index);
if (cb.IsEmpty()) return MaybeLocal<Value>();
return MakeCallback(cb, argc, argv);
}
// --- JS binding methods ---
void DTLSSession::DoSend(const FunctionCallbackInfo<Value>& args) {
DTLSSession* session;
ASSIGN_OR_RETURN_UNWRAP(&session, args.This());
if (!Buffer::HasInstance(args[0])) {
return THROW_ERR_INVALID_ARG_TYPE(session->env(), "data must be a Buffer");
}
const uint8_t* data = reinterpret_cast<const uint8_t*>(Buffer::Data(args[0]));
size_t len = Buffer::Length(args[0]);
int written = session->Send(data, len);
args.GetReturnValue().Set(written);
}
void DTLSSession::DoClose(const FunctionCallbackInfo<Value>& args) {
DTLSSession* session;
ASSIGN_OR_RETURN_UNWRAP(&session, args.This());
session->Close();
}
void DTLSSession::DoDestroy(const FunctionCallbackInfo<Value>& args) {
DTLSSession* session;
ASSIGN_OR_RETURN_UNWRAP(&session, args.This());
session->Destroy();
}
void DTLSSession::GetState(const FunctionCallbackInfo<Value>& args) {
DTLSSession* session;
ASSIGN_OR_RETURN_UNWRAP(&session, args.This());
args.GetReturnValue().Set(session->state_.GetArrayBuffer());
}
void DTLSSession::GetStats(const FunctionCallbackInfo<Value>& args) {
DTLSSession* session;
ASSIGN_OR_RETURN_UNWRAP(&session, args.This());
args.GetReturnValue().Set(session->stats_.GetArrayBuffer());
}
void DTLSSession::GetRemoteAddress(const FunctionCallbackInfo<Value>& args) {
DTLSSession* session;
ASSIGN_OR_RETURN_UNWRAP(&session, args.This());
Environment* env = session->env();
Local<Object> obj;
if (session->remote_address_.ToJS(env).ToLocal(&obj)) {
args.GetReturnValue().Set(obj);
}
}
void DTLSSession::GetProtocol(const FunctionCallbackInfo<Value>& args) {
DTLSSession* session;
ASSIGN_OR_RETURN_UNWRAP(&session, args.This());
const char* version = SSL_get_version(session->ssl_.get());
args.GetReturnValue().Set(
String::NewFromUtf8(session->env()->isolate(), version).ToLocalChecked());
}
void DTLSSession::GetCipher(const FunctionCallbackInfo<Value>& args) {
DTLSSession* session;
ASSIGN_OR_RETURN_UNWRAP(&session, args.This());
Environment* env = session->env();
const SSL_CIPHER* cipher = SSL_get_current_cipher(session->ssl_.get());
if (cipher == nullptr) return;
Local<Object> info = Object::New(env->isolate());
info->Set(env->context(),
FIXED_ONE_BYTE_STRING(env->isolate(), "name"),
String::NewFromUtf8(env->isolate(), SSL_CIPHER_get_name(cipher))
.ToLocalChecked())
.Check();
info->Set(
env->context(),
FIXED_ONE_BYTE_STRING(env->isolate(), "standardName"),
String::NewFromUtf8(env->isolate(), SSL_CIPHER_standard_name(cipher))
.ToLocalChecked())
.Check();
info->Set(env->context(),
FIXED_ONE_BYTE_STRING(env->isolate(), "version"),
String::NewFromUtf8(env->isolate(), SSL_CIPHER_get_version(cipher))
.ToLocalChecked())
.Check();
args.GetReturnValue().Set(info);
}
void DTLSSession::GetPeerCertificate(const FunctionCallbackInfo<Value>& args) {
DTLSSession* session;
ASSIGN_OR_RETURN_UNWRAP(&session, args.This());
Environment* env = session->env();
X509* peer_cert = SSL_get0_peer_certificate(session->ssl_.get());
if (peer_cert == nullptr) return;
// Return the PEM-encoded certificate.
BIO* bio = BIO_new(BIO_s_mem());
if (PEM_write_bio_X509(bio, peer_cert)) {
char* data;
long len = BIO_get_mem_data(bio, &data); // NOLINT(runtime/int)
if (len > 0) {
args.GetReturnValue().Set(
String::NewFromUtf8(
env->isolate(), data, v8::NewStringType::kNormal, len)
.ToLocalChecked());
}
}
BIO_free(bio);
}
void DTLSSession::GetALPNProtocol(const FunctionCallbackInfo<Value>& args) {
DTLSSession* session;
ASSIGN_OR_RETURN_UNWRAP(&session, args.This());
const unsigned char* alpn = nullptr;
unsigned int alpn_len = 0;
SSL_get0_alpn_selected(session->ssl_.get(), &alpn, &alpn_len);
if (alpn != nullptr && alpn_len > 0) {
args.GetReturnValue().Set(
String::NewFromUtf8(session->env()->isolate(),
reinterpret_cast<const char*>(alpn),
v8::NewStringType::kNormal,
alpn_len)
.ToLocalChecked());
}
}
void DTLSSession::ExportKeyingMaterial(
const FunctionCallbackInfo<Value>& args) {
DTLSSession* session;
ASSIGN_OR_RETURN_UNWRAP(&session, args.This());
Environment* env = session->env();
if (!args[0]->IsNumber() || !args[1]->IsString()) {
return THROW_ERR_INVALID_ARG_TYPE(
env, "Expected (length: number, label: string[, context: Buffer])");
}
int length = args[0]->Int32Value(env->context()).FromJust();
Utf8Value label(env->isolate(), args[1]);
const uint8_t* context_value = nullptr;
size_t context_len = 0;
bool use_context = false;
if (args.Length() > 2 && Buffer::HasInstance(args[2])) {
context_value = reinterpret_cast<const uint8_t*>(Buffer::Data(args[2]));
context_len = Buffer::Length(args[2]);
use_context = true;
}
std::vector<uint8_t> out(length);
int ret = SSL_export_keying_material(session->ssl_.get(),
out.data(),
length,
*label,
label.length(),
context_value,
context_len,
use_context ? 1 : 0);
if (ret != 1) {
return THROW_ERR_CRYPTO_OPERATION_FAILED(
env, "SSL_export_keying_material failed");
}
args.GetReturnValue().Set(
Buffer::Copy(env, reinterpret_cast<const char*>(out.data()), length)
.ToLocalChecked());
}
void DTLSSession::GetSRTPProfile(const FunctionCallbackInfo<Value>& args) {
DTLSSession* session;
ASSIGN_OR_RETURN_UNWRAP(&session, args.This());
const SRTP_PROTECTION_PROFILE* profile =
SSL_get_selected_srtp_profile(session->ssl_.get());
if (profile != nullptr) {
args.GetReturnValue().Set(
String::NewFromUtf8(session->env()->isolate(), profile->name)
.ToLocalChecked());
}
}
void DTLSSession::SetServername(const FunctionCallbackInfo<Value>& args) {
DTLSSession* session;
ASSIGN_OR_RETURN_UNWRAP(&session, args.This());
CHECK(args[0]->IsString());
Utf8Value servername(session->env()->isolate(), args[0]);
SSL_set_tlsext_host_name(session->ssl_.get(), *servername);
}
void DTLSSession::GetServername(const FunctionCallbackInfo<Value>& args) {
DTLSSession* session;
ASSIGN_OR_RETURN_UNWRAP(&session, args.This());
const char* servername =
SSL_get_servername(session->ssl_.get(), TLSEXT_NAMETYPE_host_name);
if (servername != nullptr) {
args.GetReturnValue().Set(
String::NewFromUtf8(session->env()->isolate(), servername)
.ToLocalChecked());
}
}
void DTLSSession::MemoryInfo(MemoryTracker* tracker) const {
tracker->TrackField("remote_address", remote_address_);
}
} // namespace dtls
} // namespace node
#endif // HAVE_OPENSSL && HAVE_DTLS