@@ -208,7 +208,7 @@ void TLSWrap::Receive(const FunctionCallbackInfo<Value>& args) { |
208 | 208 | uv_buf_t buf; |
209 | 209 |
|
210 | 210 | // Copy given buffer entirely or partiall if handle becomes closed |
211 | | - while (len > 0 && !wrap->IsClosing()) { |
| 211 | + while (len > 0 && wrap->IsAlive() && !wrap->IsClosing()) { |
212 | 212 | wrap->stream_->OnAlloc(len, &buf); |
213 | 213 | size_t copy = buf.len > len ? len : buf.len; |
214 | 214 | memcpy(buf.base, data, copy); |
@@ -282,6 +282,9 @@ void TLSWrap::EncOut() { |
282 | 282 | if (established_ && !write_item_queue_.IsEmpty()) |
283 | 283 | MakePending(); |
284 | 284 |
|
| 285 | + if (ssl_ == nullptr) |
| 286 | + return; |
| 287 | + |
285 | 288 | // No data to write |
286 | 289 | if (BIO_pending(enc_out_) == 0) { |
287 | 290 | if (clear_in_->Length() == 0) |
@@ -396,7 +399,8 @@ void TLSWrap::ClearOut() { |
396 | 399 | if (eof_) |
397 | 400 | return; |
398 | 401 |
|
399 | | - CHECK_NE(ssl_, nullptr); |
| 402 | + if (ssl_ == nullptr) |
| 403 | + return; |
400 | 404 |
|
401 | 405 | char out[kClearOutChunkSize]; |
402 | 406 | int read; |
@@ -451,6 +455,9 @@ bool TLSWrap::ClearIn() { |
451 | 455 | if (!hello_parser_.IsEnded()) |
452 | 456 | return false; |
453 | 457 |
|
| 458 | + if (ssl_ == nullptr) |
| 459 | + return false; |
| 460 | + |
454 | 461 | int written = 0; |
455 | 462 | while (clear_in_->Length() > 0) { |
456 | 463 | size_t avail = 0; |
@@ -503,7 +510,7 @@ int TLSWrap::GetFD() { |
503 | 510 |
|
504 | 511 |
|
505 | 512 | bool TLSWrap::IsAlive() { |
506 | | - return stream_->IsAlive(); |
| 513 | + return ssl_ != nullptr && stream_->IsAlive(); |
507 | 514 | } |
508 | 515 |
|
509 | 516 |
|
@@ -573,6 +580,9 @@ int TLSWrap::DoWrite(WriteWrap* w, |
573 | 580 | return 0; |
574 | 581 | } |
575 | 582 |
|
| 583 | + if (ssl_ == nullptr) |
| 584 | + return UV_EPROTO; |
| 585 | + |
576 | 586 | int written = 0; |
577 | 587 | for (i = 0; i < count; i++) { |
578 | 588 | written = SSL_write(ssl_, bufs[i].base, bufs[i].len); |
@@ -660,7 +670,10 @@ void TLSWrap::DoRead(ssize_t nread, |
660 | 670 | } |
661 | 671 |
|
662 | 672 | // Only client connections can receive data |
663 | | - CHECK_NE(ssl_, nullptr); |
| 673 | + if (ssl_ == nullptr) { |
| 674 | + OnRead(UV_EPROTO, nullptr); |
| 675 | + return; |
| 676 | + } |
664 | 677 |
|
665 | 678 | // Commit read data |
666 | 679 | NodeBIO* enc_in = NodeBIO::FromBIO(enc_in_); |
@@ -680,7 +693,7 @@ void TLSWrap::DoRead(ssize_t nread, |
680 | 693 |
|
681 | 694 |
|
682 | 695 | int TLSWrap::DoShutdown(ShutdownWrap* req_wrap) { |
683 | | - if (SSL_shutdown(ssl_) == 0) |
| 696 | + if (ssl_ != nullptr && SSL_shutdown(ssl_) == 0) |
684 | 697 | SSL_shutdown(ssl_); |
685 | 698 | shutdown_ = true; |
686 | 699 | EncOut(); |
@@ -696,6 +709,9 @@ void TLSWrap::SetVerifyMode(const FunctionCallbackInfo<Value>& args) { |
696 | 709 | if (args.Length() < 2 || !args[0]->IsBoolean() || !args[1]->IsBoolean()) |
697 | 710 | return env->ThrowTypeError("Bad arguments, expected two booleans"); |
698 | 711 |
|
| 712 | + if (wrap->ssl_ == nullptr) |
| 713 | + return env->ThrowTypeError("SetVerifyMode after destroySSL"); |
| 714 | + |
699 | 715 | int verify_mode; |
700 | 716 | if (wrap->is_server()) { |
701 | 717 | bool request_cert = args[0]->IsTrue(); |
@@ -735,6 +751,14 @@ void TLSWrap::EnableHelloParser(const FunctionCallbackInfo<Value>& args) { |
735 | 751 | } |
736 | 752 |
|
737 | 753 |
|
| 754 | +void TLSWrap::DestroySSL(const FunctionCallbackInfo<Value>& args) { |
| 755 | + TLSWrap* wrap = Unwrap<TLSWrap>(args.Holder()); |
| 756 | + wrap->SSLWrap<TLSWrap>::DestroySSL(); |
| 757 | + delete wrap->clear_in_; |
| 758 | + wrap->clear_in_ = nullptr; |
| 759 | +} |
| 760 | + |
| 761 | + |
738 | 762 | void TLSWrap::OnClientHelloParseEnd(void* arg) { |
739 | 763 | TLSWrap* c = static_cast<TLSWrap*>(arg); |
740 | 764 | c->Cycle(); |
@@ -747,6 +771,8 @@ void TLSWrap::GetServername(const FunctionCallbackInfo<Value>& args) { |
747 | 771 |
|
748 | 772 | TLSWrap* wrap = Unwrap<TLSWrap>(args.Holder()); |
749 | 773 |
|
| 774 | + CHECK_NE(wrap->ssl_, nullptr); |
| 775 | + |
750 | 776 | const char* servername = SSL_get_servername(wrap->ssl_, |
751 | 777 | TLSEXT_NAMETYPE_host_name); |
752 | 778 | if (servername != nullptr) { |
@@ -771,6 +797,8 @@ void TLSWrap::SetServername(const FunctionCallbackInfo<Value>& args) { |
771 | 797 | if (!wrap->is_client()) |
772 | 798 | return; |
773 | 799 |
|
| 800 | + CHECK_NE(wrap->ssl_, nullptr); |
| 801 | + |
774 | 802 | #ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB |
775 | 803 | node::Utf8Value servername(env->isolate(), args[0].As<String>()); |
776 | 804 | SSL_set_tlsext_host_name(wrap->ssl_, *servername); |
@@ -830,6 +858,7 @@ void TLSWrap::Initialize(Handle<Object> target, |
830 | 858 | env->SetProtoMethod(t, "setVerifyMode", SetVerifyMode); |
831 | 859 | env->SetProtoMethod(t, "enableSessionCallbacks", EnableSessionCallbacks); |
832 | 860 | env->SetProtoMethod(t, "enableHelloParser", EnableHelloParser); |
| 861 | + env->SetProtoMethod(t, "destroySSL", DestroySSL); |
833 | 862 |
|
834 | 863 | StreamBase::AddMethods<TLSWrap>(env, t, StreamBase::kFlagHasWritev); |
835 | 864 | SSLWrap<TLSWrap>::AddMethods(env, t); |
|