2121
2222#include " tls_wrap.h"
2323#include " async_wrap-inl.h"
24+ #include " debug_utils.h"
2425#include " node_buffer.h" // Buffer
2526#include " node_crypto.h" // SecureContext
2627#include " node_crypto_bio.h" // NodeBIO
@@ -74,15 +75,18 @@ TLSWrap::TLSWrap(Environment* env,
7475 stream->PushStreamListener (this );
7576
7677 InitSSL ();
78+ Debug (this , " Created new TLSWrap" );
7779}
7880
7981
8082TLSWrap::~TLSWrap () {
83+ Debug (this , " ~TLSWrap()" );
8184 sc_ = nullptr ;
8285}
8386
8487
8588bool TLSWrap::InvokeQueued (int status, const char * error_str) {
89+ Debug (this , " InvokeQueued(%d, %s)" , status, error_str);
8690 if (!write_callback_scheduled_)
8791 return false ;
8892
@@ -97,6 +101,7 @@ bool TLSWrap::InvokeQueued(int status, const char* error_str) {
97101
98102
99103void TLSWrap::NewSessionDoneCb () {
104+ Debug (this , " NewSessionDoneCb()" );
100105 Cycle ();
101106}
102107
@@ -174,6 +179,7 @@ void TLSWrap::Receive(const FunctionCallbackInfo<Value>& args) {
174179 CHECK (Buffer::HasInstance (args[0 ]));
175180 char * data = Buffer::Data (args[0 ]);
176181 size_t len = Buffer::Length (args[0 ]);
182+ Debug (wrap, " Receiving %zu bytes injected from JS" , len);
177183
178184 // Copy given buffer entirely or partiall if handle becomes closed
179185 while (len > 0 && wrap->IsAlive () && !wrap->IsClosing ()) {
@@ -221,6 +227,9 @@ void TLSWrap::SSLInfoCallback(const SSL* ssl_, int where, int ret) {
221227 Local<Object> object = c->object ();
222228
223229 if (where & SSL_CB_HANDSHAKE_START) {
230+ Debug (c, " SSLInfoCallback(SSL_CB_HANDSHAKE_START);" );
231+ // Start is tracked to limit number and frequency of renegotiation attempts,
232+ // since excessive renegotiation may be an attack.
224233 Local<Value> callback;
225234
226235 if (object->Get (env->context (), env->onhandshakestart_string ())
@@ -234,6 +243,7 @@ void TLSWrap::SSLInfoCallback(const SSL* ssl_, int where, int ret) {
234243 // sending HelloRequest in OpenSSL-1.1.1.
235244 // We need to check whether this is in a renegotiation state or not.
236245 if (where & SSL_CB_HANDSHAKE_DONE && !SSL_renegotiate_pending (ssl)) {
246+ Debug (c, " SSLInfoCallback(SSL_CB_HANDSHAKE_DONE);" );
237247 Local<Value> callback;
238248
239249 c->established_ = true ;
@@ -247,27 +257,40 @@ void TLSWrap::SSLInfoCallback(const SSL* ssl_, int where, int ret) {
247257
248258
249259void TLSWrap::EncOut () {
260+ Debug (this , " Trying to write encrypted output" );
261+
250262 // Ignore cycling data if ClientHello wasn't yet parsed
251- if (!hello_parser_.IsEnded ())
263+ if (!hello_parser_.IsEnded ()) {
264+ Debug (this , " Returning from EncOut(), hello_parser_ active" );
252265 return ;
266+ }
253267
254268 // Write in progress
255- if (write_size_ != 0 )
269+ if (write_size_ != 0 ) {
270+ Debug (this , " Returning from EncOut(), write currently in progress" );
256271 return ;
272+ }
257273
258274 // Wait for `newSession` callback to be invoked
259- if (is_awaiting_new_session ())
275+ if (is_awaiting_new_session ()) {
276+ Debug (this , " Returning from EncOut(), awaiting new session" );
260277 return ;
278+ }
261279
262280 // Split-off queue
263- if (established_ && current_write_ != nullptr )
281+ if (established_ && current_write_ != nullptr ) {
282+ Debug (this , " EncOut() setting write_callback_scheduled_" );
264283 write_callback_scheduled_ = true ;
284+ }
265285
266- if (ssl_ == nullptr )
286+ if (ssl_ == nullptr ) {
287+ Debug (this , " Returning from EncOut(), ssl_ == nullptr" );
267288 return ;
289+ }
268290
269291 // No encrypted output ready to write to the underlying stream.
270292 if (BIO_pending (enc_out_) == 0 ) {
293+ Debug (this , " No pending encrypted output" );
271294 if (pending_cleartext_input_.empty ())
272295 InvokeQueued (0 );
273296 return ;
@@ -286,6 +309,7 @@ void TLSWrap::EncOut() {
286309 for (size_t i = 0 ; i < count; i++)
287310 buf[i] = uv_buf_init (data[i], size[i]);
288311
312+ Debug (this , " Writing %zu buffers to the underlying stream" , count);
289313 StreamWriteResult res = underlying_stream ()->Write (bufs, count);
290314 if (res.err != 0 ) {
291315 InvokeQueued (res.err );
@@ -295,6 +319,7 @@ void TLSWrap::EncOut() {
295319 NODE_COUNT_NET_BYTES_SENT (write_size_);
296320
297321 if (!res.async ) {
322+ Debug (this , " Write finished synchronously" );
298323 HandleScope handle_scope (env ()->isolate ());
299324
300325 // Simulate asynchronous finishing, TLS cannot handle this at the moment.
@@ -306,21 +331,26 @@ void TLSWrap::EncOut() {
306331
307332
308333void TLSWrap::OnStreamAfterWrite (WriteWrap* req_wrap, int status) {
334+ Debug (this , " OnStreamAfterWrite(status = %d)" , status);
309335 if (current_empty_write_ != nullptr ) {
336+ Debug (this , " Had empty write" );
310337 WriteWrap* finishing = current_empty_write_;
311338 current_empty_write_ = nullptr ;
312339 finishing->Done (status);
313340 return ;
314341 }
315342
316- if (ssl_ == nullptr )
343+ if (ssl_ == nullptr ) {
344+ Debug (this , " ssl_ == nullptr, marking as cancelled" );
317345 status = UV_ECANCELED;
346+ }
318347
319348 // Handle error
320349 if (status) {
321- // Ignore errors after shutdown
322- if (shutdown_)
350+ if (shutdown_) {
351+ Debug ( this , " Ignoring error after shutdown " );
323352 return ;
353+ }
324354
325355 // Notify about error
326356 InvokeQueued (status);
@@ -383,23 +413,31 @@ Local<Value> TLSWrap::GetSSLError(int status, int* err, std::string* msg) {
383413
384414
385415void TLSWrap::ClearOut () {
416+ Debug (this , " Trying to read cleartext output" );
386417 // Ignore cycling data if ClientHello wasn't yet parsed
387- if (!hello_parser_.IsEnded ())
418+ if (!hello_parser_.IsEnded ()) {
419+ Debug (this , " Returning from ClearOut(), hello_parser_ active" );
388420 return ;
421+ }
389422
390423 // No reads after EOF
391- if (eof_)
424+ if (eof_) {
425+ Debug (this , " Returning from ClearOut(), EOF reached" );
392426 return ;
427+ }
393428
394- if (ssl_ == nullptr )
429+ if (ssl_ == nullptr ) {
430+ Debug (this , " Returning from ClearOut(), ssl_ == nullptr" );
395431 return ;
432+ }
396433
397434 crypto::MarkPopErrorOnReturn mark_pop_error_on_return;
398435
399436 char out[kClearOutChunkSize ];
400437 int read;
401438 for (;;) {
402439 read = SSL_read (ssl_.get (), out, sizeof (out));
440+ Debug (this , " Read %d bytes of cleartext output" , read);
403441
404442 if (read <= 0 )
405443 break ;
@@ -417,8 +455,10 @@ void TLSWrap::ClearOut() {
417455 // Caveat emptor: OnRead() calls into JS land which can result in
418456 // the SSL context object being destroyed. We have to carefully
419457 // check that ssl_ != nullptr afterwards.
420- if (ssl_ == nullptr )
458+ if (ssl_ == nullptr ) {
459+ Debug (this , " Returning from read loop, ssl_ == nullptr" );
421460 return ;
461+ }
422462
423463 read -= avail;
424464 current += avail;
@@ -444,6 +484,7 @@ void TLSWrap::ClearOut() {
444484 return ;
445485
446486 if (!arg.IsEmpty ()) {
487+ Debug (this , " Got SSL error (%d), calling onerror" , err);
447488 // When TLS Alert are stored in wbio,
448489 // it should be flushed to socket before destroyed.
449490 if (BIO_pending (enc_out_) != 0 )
@@ -456,12 +497,17 @@ void TLSWrap::ClearOut() {
456497
457498
458499void TLSWrap::ClearIn () {
500+ Debug (this , " Trying to write cleartext input" );
459501 // Ignore cycling data if ClientHello wasn't yet parsed
460- if (!hello_parser_.IsEnded ())
502+ if (!hello_parser_.IsEnded ()) {
503+ Debug (this , " Returning from ClearIn(), hello_parser_ active" );
461504 return ;
505+ }
462506
463- if (ssl_ == nullptr )
507+ if (ssl_ == nullptr ) {
508+ Debug (this , " Returning from ClearIn(), ssl_ == nullptr" );
464509 return ;
510+ }
465511
466512 std::vector<uv_buf_t > buffers;
467513 buffers.swap (pending_cleartext_input_);
@@ -474,13 +520,15 @@ void TLSWrap::ClearIn() {
474520 size_t avail = buffers[i].len ;
475521 char * data = buffers[i].base ;
476522 written = SSL_write (ssl_.get (), data, avail);
523+ Debug (this , " Writing %zu bytes, written = %d" , avail, written);
477524 CHECK (written == -1 || written == static_cast <int >(avail));
478525 if (written == -1 )
479526 break ;
480527 }
481528
482529 // All written
483530 if (i == buffers.size ()) {
531+ Debug (this , " Successfully wrote all data to SSL" );
484532 // We wrote all the buffers, so no writes failed (written < 0 on failure).
485533 CHECK_GE (written, 0 );
486534 return ;
@@ -494,11 +542,13 @@ void TLSWrap::ClearIn() {
494542 std::string error_str;
495543 Local<Value> arg = GetSSLError (written, &err, &error_str);
496544 if (!arg.IsEmpty ()) {
545+ Debug (this , " Got SSL error (%d)" , err);
497546 write_callback_scheduled_ = true ;
498547 // XXX(sam) Should forward an error object with .code/.function/.etc, if
499548 // possible.
500549 InvokeQueued (UV_EPROTO, error_str.c_str ());
501550 } else {
551+ Debug (this , " Pushing back %zu buffers" , buffers.size () - i);
502552 // Push back the not-yet-written pending buffers into their queue.
503553 // This can be skipped in the error case because no further writes
504554 // would succeed anyway.
@@ -511,6 +561,17 @@ void TLSWrap::ClearIn() {
511561}
512562
513563
564+ std::string TLSWrap::diagnostic_name () const {
565+ std::string name = " TLSWrap " ;
566+ if (is_server ())
567+ name += " server (" ;
568+ else
569+ name += " client (" ;
570+ name += std::to_string (static_cast <int64_t >(get_async_id ())) + " )" ;
571+ return name;
572+ }
573+
574+
514575AsyncWrap* TLSWrap::GetAsyncWrap () {
515576 return static_cast <AsyncWrap*>(this );
516577}
@@ -540,13 +601,15 @@ bool TLSWrap::IsClosing() {
540601
541602
542603int TLSWrap::ReadStart () {
604+ Debug (this , " ReadStart()" );
543605 if (stream_ != nullptr )
544606 return stream_->ReadStart ();
545607 return 0 ;
546608}
547609
548610
549611int TLSWrap::ReadStop () {
612+ Debug (this , " ReadStop()" );
550613 if (stream_ != nullptr )
551614 return stream_->ReadStop ();
552615 return 0 ;
@@ -569,6 +632,7 @@ int TLSWrap::DoWrite(WriteWrap* w,
569632 size_t count,
570633 uv_stream_t * send_handle) {
571634 CHECK_NULL (send_handle);
635+ Debug (this , " DoWrite()" );
572636
573637 if (ssl_ == nullptr ) {
574638 ClearError ();
@@ -596,8 +660,10 @@ int TLSWrap::DoWrite(WriteWrap* w,
596660 // onto the socket, we just want the side-effects. After, make sure the
597661 // WriteWrap was accepted by the stream, or that we call Done() on it.
598662 if (empty) {
663+ Debug (this , " Empty write" );
599664 ClearOut ();
600665 if (BIO_pending (enc_out_) == 0 ) {
666+ Debug (this , " No pending encrypted output, writing to underlying stream" );
601667 CHECK_NULL (current_empty_write_);
602668 current_empty_write_ = w;
603669 StreamWriteResult res =
@@ -628,6 +694,7 @@ int TLSWrap::DoWrite(WriteWrap* w,
628694 for (i = 0 ; i < count; i++) {
629695 written = SSL_write (ssl_.get (), bufs[i].base , bufs[i].len );
630696 CHECK (written == -1 || written == static_cast <int >(bufs[i].len ));
697+ Debug (this , " Writing %zu bytes, written = %d" , bufs[i].len , written);
631698 if (written == -1 )
632699 break ;
633700 }
@@ -638,10 +705,12 @@ int TLSWrap::DoWrite(WriteWrap* w,
638705
639706 // If we stopped writing because of an error, it's fatal, discard the data.
640707 if (!arg.IsEmpty ()) {
708+ Debug (this , " Got SSL error (%d), returning UV_EPROTO" , err);
641709 current_write_ = nullptr ;
642710 return UV_EPROTO;
643711 }
644712
713+ Debug (this , " Saving %zu buffers for later write" , count - i);
645714 // Otherwise, save unwritten data so it can be written later by ClearIn().
646715 pending_cleartext_input_.insert (pending_cleartext_input_.end (),
647716 &bufs[i],
@@ -665,6 +734,7 @@ uv_buf_t TLSWrap::OnStreamAlloc(size_t suggested_size) {
665734
666735
667736void TLSWrap::OnStreamRead (ssize_t nread, const uv_buf_t & buf) {
737+ Debug (this , " Read %zd bytes from underlying stream" , nread);
668738 if (nread < 0 ) {
669739 // Error should be emitted only after all data was read
670740 ClearOut ();
@@ -698,6 +768,7 @@ void TLSWrap::OnStreamRead(ssize_t nread, const uv_buf_t& buf) {
698768 size_t avail = 0 ;
699769 uint8_t * data = reinterpret_cast <uint8_t *>(enc_in->Peek (&avail));
700770 CHECK_IMPLIES (data == nullptr , avail == 0 );
771+ Debug (this , " Passing %zu bytes to the hello parser" , avail);
701772 return hello_parser_.Parse (data, avail);
702773 }
703774
@@ -712,6 +783,7 @@ ShutdownWrap* TLSWrap::CreateShutdownWrap(Local<Object> req_wrap_object) {
712783
713784
714785int TLSWrap::DoShutdown (ShutdownWrap* req_wrap) {
786+ Debug (this , " DoShutdown()" );
715787 crypto::MarkPopErrorOnReturn mark_pop_error_on_return;
716788
717789 if (ssl_ && SSL_shutdown (ssl_.get ()) == 0 )
@@ -770,6 +842,7 @@ void TLSWrap::EnableSessionCallbacks(
770842void TLSWrap::DestroySSL (const FunctionCallbackInfo<Value>& args) {
771843 TLSWrap* wrap;
772844 ASSIGN_OR_RETURN_UNWRAP (&wrap, args.Holder ());
845+ Debug (wrap, " DestroySSL()" );
773846
774847 // If there is a write happening, mark it as finished.
775848 wrap->write_callback_scheduled_ = true ;
@@ -784,6 +857,7 @@ void TLSWrap::DestroySSL(const FunctionCallbackInfo<Value>& args) {
784857
785858 if (wrap->stream_ != nullptr )
786859 wrap->stream_ ->RemoveStreamListener (wrap);
860+ Debug (wrap, " DestroySSL() finished" );
787861}
788862
789863
@@ -796,6 +870,7 @@ void TLSWrap::EnableCertCb(const FunctionCallbackInfo<Value>& args) {
796870
797871void TLSWrap::OnClientHelloParseEnd (void * arg) {
798872 TLSWrap* c = static_cast <TLSWrap*>(arg);
873+ Debug (c, " OnClientHelloParseEnd()" );
799874 c->Cycle ();
800875}
801876
0 commit comments