| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent ff7fc09 commit 2ff593a
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -192,11 +192,11 @@ CryptoStream.prototype._blow = function() { | |||
| 192 | 192 | var pool = new Buffer(4096); // alloc every time? | |
| 193 | 193 | ||
| 194 | 194 | do { | |
| 195 | - try { | ||
| 196 | - chunkBytes = this._blower(pool, bytesRead, pool.length - bytesRead); | ||
| 197 | - } catch (e) { | ||
| 195 | + chunkBytes = this._blower(pool, bytesRead, pool.length - bytesRead); | ||
| 196 | + | ||
| 197 | + if (this.pair._ssl && this.pair._ssl.error) { | ||
| 198 | 198 | if (this.pair._secureEstablished) { | |
| 199 | - this.pair._error(e); | ||
| 199 | + this.pair._error(); | ||
| 200 | 200 | } else { | |
| 201 | 201 | this.pair._destroy(); | |
| 202 | 202 | } | |
@@ -244,7 +244,6 @@ CryptoStream.prototype._blow = function() { | |||
| 244 | 244 | // }); | |
| 245 | 245 | // | |
| 246 | 246 | CryptoStream.prototype._suck = function() { | |
| 247 | - var rv; | ||
| 248 | 247 | var havePending = this._pending.length > 0; | |
| 249 | 248 | ||
| 250 | 249 | while (this._pending.length > 0) { | |
@@ -253,18 +252,18 @@ CryptoStream.prototype._suck = function() { | |||
| 253 | 252 | ||
| 254 | 253 | assert(this._pending.length === this._pendingCallbacks.length); | |
| 255 | 254 | ||
| 256 | - try { | ||
| 257 | - rv = this._sucker(tmp); | ||
| 258 | - } catch (e) { | ||
| 255 | + var rv = this._sucker(tmp); | ||
| 256 | + | ||
| 257 | + if (this.pair._ssl && this.pair._ssl.error) { | ||
| 259 | 258 | if (this.pair._secureEstablished) { | |
| 260 | - this.pair._error(e); | ||
| 259 | + this.pair._error(); | ||
| 261 | 260 | } else { | |
| 262 | 261 | this.pair._destroy(); | |
| 263 | 262 | } | |
| 264 | 263 | return; | |
| 265 | 264 | } | |
| 266 | 265 | ||
| 267 | - if (rv === 0) { | ||
| 266 | + if (rv === 0 || rv < 0) { | ||
| 268 | 267 | this._pending.unshift(tmp); | |
| 269 | 268 | this._pendingCallbacks.unshift(cb); | |
| 270 | 269 | break; | |
@@ -453,9 +452,10 @@ SecurePair.prototype._cycle = function() { | |||
| 453 | 452 | }; | |
| 454 | 453 | ||
| 455 | 454 | ||
| 456 | - SecurePair.prototype._destroy = function(err) { | ||
| 455 | + SecurePair.prototype._destroy = function() { | ||
| 457 | 456 | if (!this._done) { | |
| 458 | 457 | this._done = true; | |
| 458 | + this._ssl.error = null; | ||
| 459 | 459 | this._ssl.close(); | |
| 460 | 460 | this._ssl = null; | |
| 461 | 461 | ||
@@ -469,7 +469,10 @@ SecurePair.prototype._destroy = function(err) { | |||
| 469 | 469 | }; | |
| 470 | 470 | ||
| 471 | 471 | ||
| 472 | - SecurePair.prototype._error = function(err) { | ||
| 472 | + SecurePair.prototype._error = function() { | ||
| 473 | + var err = this._ssl.error; | ||
| 474 | + this._ssl.error = null; | ||
| 475 | + | ||
| 473 | 476 | if (this._isServer && | |
| 474 | 477 | this._rejectUnauthorized && | |
| 475 | 478 | /peer did not return a certificate/.test(err.message)) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -287,28 +287,55 @@ Handle<Value> SecureContext::Close(const Arguments& args) { | |||
| 287 | 287 | return False(); | |
| 288 | 288 | } | |
| 289 | 289 | ||
| 290 | - char ssl_error_buf[512]; | ||
| 290 | + #ifdef SSL_PRINT_DEBUG | ||
| 291 | + # define DEBUG_PRINT(...) fprintf (stderr, __VA_ARGS__) | ||
| 292 | + #else | ||
| 293 | + # define DEBUG_PRINT(...) | ||
| 294 | + #endif | ||
| 291 | 295 | ||
| 292 | - static int serr(SSL *ssl, const char* func, int rv) { | ||
| 293 | - if (rv >= 0) { | ||
| 294 | - return rv; | ||
| 295 | - } | ||
| 296 | 296 | ||
| 297 | - int err = SSL_get_error(ssl, rv); | ||
| 298 | - if (err != SSL_ERROR_WANT_WRITE && | ||
| 299 | - err != SSL_ERROR_WANT_READ) { | ||
| 300 | - ERR_error_string_n(ERR_get_error(), &ssl_error_buf[0], sizeof(ssl_error_buf)); | ||
| 301 | - /* fprintf(stderr, "[%p] SSL: %s failed: (%d:%d) %s\n", ssl, func, err, rv, buf); */ | ||
| 302 | - return rv; | ||
| 303 | - } else if (err == SSL_ERROR_WANT_WRITE) { | ||
| 304 | - /* fprintf(stderr, "[%p] SSL: %s want write\n", ssl, func); */ | ||
| 297 | + int Connection::HandleError(const char* func, int rv, bool ignore_error) { | ||
| 298 | + if (rv >= 0) return rv; | ||
| 299 | + | ||
| 300 | + int err = SSL_get_error(ssl_, rv); | ||
| 301 | + | ||
| 302 | + if (err == SSL_ERROR_WANT_WRITE) { | ||
| 303 | + DEBUG_PRINT("[%p] SSL: %s want write\n", ssl_, func); | ||
| 304 | + return 0; | ||
| 305 | + | ||
| 305 | 306 | } else if (err == SSL_ERROR_WANT_READ) { | |
| 306 | - /* fprintf(stderr, "[%p] SSL: %s want read\n", ssl, func); */ | ||
| 307 | + DEBUG_PRINT("[%p] SSL: %s want read\n", ssl_, func); | ||
| 308 | + return 0; | ||
| 309 | + | ||
| 310 | + } else { | ||
| 311 | + static char ssl_error_buf[512]; | ||
| 312 | + ERR_error_string_n(err, ssl_error_buf, sizeof(ssl_error_buf)); | ||
| 313 | + | ||
| 314 | + if (!ignore_error) { | ||
| 315 | + HandleScope scope; | ||
| 316 | + Local<Value> e = Exception::Error(String::New(ssl_error_buf)); | ||
| 317 | + handle_->Set(String::New("error"), e); | ||
| 318 | + } | ||
| 319 | + | ||
| 320 | + DEBUG_PRINT("[%p] SSL: %s failed: (%d:%d) %s\n", ssl_, func, err, rv, ssl_error_buf); | ||
| 321 | + | ||
| 322 | + return rv; | ||
| 307 | 323 | } | |
| 308 | 324 | ||
| 309 | 325 | return 0; | |
| 310 | 326 | } | |
| 311 | 327 | ||
| 328 | + | ||
| 329 | + void Connection::ClearError() { | ||
| 330 | + #ifndef NDEBUG | ||
| 331 | + HandleScope scope; | ||
| 332 | + | ||
| 333 | + // We should clear the error in JS-land | ||
| 334 | + assert(handle_->Get(String::New("error"))->BooleanValue() == false); | ||
| 335 | + #endif // NDEBUG | ||
| 336 | + } | ||
| 337 | + | ||
| 338 | + | ||
| 312 | 339 | void Connection::Initialize(Handle<Object> target) { | |
| 313 | 340 | HandleScope scope; | |
| 314 | 341 | ||
@@ -440,7 +467,7 @@ Handle<Value> Connection::New(const Arguments& args) { | |||
| 440 | 467 | Handle<Value> Connection::EncIn(const Arguments& args) { | |
| 441 | 468 | HandleScope scope; | |
| 442 | 469 | ||
| 443 | - Connection *ss = ObjectWrap::Unwrap<Connection>(args.Holder()); | ||
| 470 | + Connection *ss = Connection::Unwrap(args); | ||
| 444 | 471 | ||
| 445 | 472 | if (args.Length() < 3) { | |
| 446 | 473 | return ThrowException(Exception::TypeError( | |
@@ -468,12 +495,9 @@ Handle<Value> Connection::EncIn(const Arguments& args) { | |||
| 468 | 495 | String::New("Length is extends beyond buffer"))); | |
| 469 | 496 | } | |
| 470 | 497 | ||
| 471 | - int bytes_written = serr(ss->ssl_, "BIO_write", BIO_write(ss->bio_read_, (char*)buffer_data + off, len)); | ||
| 498 | + int bytes_written = BIO_write(ss->bio_read_, (char*)buffer_data + off, len); | ||
| 472 | 499 | ||
| 473 | - if (bytes_written < 0) { | ||
| 474 | - if (errno == EAGAIN || errno == EINTR) return Null(); | ||
| 475 | - return ThrowException(ErrnoException(errno, "read")); | ||
| 476 | - } | ||
| 500 | + ss->HandleError("BIO_write", bytes_written); | ||
| 477 | 501 | ||
| 478 | 502 | return scope.Close(Integer::New(bytes_written)); | |
| 479 | 503 | } | |
@@ -482,7 +506,7 @@ Handle<Value> Connection::EncIn(const Arguments& args) { | |||
| 482 | 506 | Handle<Value> Connection::ClearOut(const Arguments& args) { | |
| 483 | 507 | HandleScope scope; | |
| 484 | 508 | ||
| 485 | - Connection *ss = ObjectWrap::Unwrap<Connection>(args.Holder()); | ||
| 509 | + Connection *ss = Connection::Unwrap(args); | ||
| 486 | 510 | ||
| 487 | 511 | if (args.Length() < 3) { | |
| 488 | 512 | return ThrowException(Exception::TypeError( | |
@@ -510,33 +534,33 @@ Handle<Value> Connection::ClearOut(const Arguments& args) { | |||
| 510 | 534 | String::New("Length is extends beyond buffer"))); | |
| 511 | 535 | } | |
| 512 | 536 | ||
| 513 | - int bytes_read; | ||
| 514 | 537 | ||
| 515 | 538 | if (!SSL_is_init_finished(ss->ssl_)) { | |
| 539 | + int rv; | ||
| 540 | + | ||
| 516 | 541 | if (ss->is_server_) { | |
| 517 | - bytes_read = serr(ss->ssl_, "SSL_accept:ClearOut", SSL_accept(ss->ssl_)); | ||
| 542 | + rv = SSL_accept(ss->ssl_); | ||
| 543 | + ss->HandleError("SSL_accept:ClearOut", rv); | ||
| 518 | 544 | } else { | |
| 519 | - bytes_read = serr(ss->ssl_, "SSL_connect:ClearOut", SSL_connect(ss->ssl_)); | ||
| 520 | - } | ||
| 521 | - if (bytes_read < 0) { | ||
| 522 | - return ThrowException(Exception::Error(v8::String::New(ssl_error_buf))); | ||
| 545 | + rv = SSL_connect(ss->ssl_); | ||
| 546 | + ss->HandleError("SSL_connect:ClearOut", rv); | ||
| 523 | 547 | } | |
| 524 | - return scope.Close(Integer::New(0)); | ||
| 525 | - } | ||
| 526 | 548 | ||
| 527 | - bytes_read = serr(ss->ssl_, "SSL_read:ClearOut", SSL_read(ss->ssl_, (char*)buffer_data + off, len)); | ||
| 528 | - if (bytes_read < 0) { | ||
| 529 | - return ThrowException(Exception::Error(v8::String::New(ssl_error_buf))); | ||
| 549 | + if (rv < 0) return scope.Close(Integer::New(rv)); | ||
| 530 | 550 | } | |
| 531 | 551 | ||
| 552 | + int bytes_read = SSL_read(ss->ssl_, (char*)buffer_data + off, len); | ||
| 553 | + ss->HandleError("SSL_read:ClearOut", bytes_read); | ||
| 554 | + | ||
| 532 | 555 | return scope.Close(Integer::New(bytes_read)); | |
| 533 | 556 | } | |
| 534 | 557 | ||
| 535 | 558 | ||
| 536 | 559 | Handle<Value> Connection::ClearPending(const Arguments& args) { | |
| 537 | 560 | HandleScope scope; | |
| 538 | 561 | ||
| 539 | - Connection *ss = ObjectWrap::Unwrap<Connection>(args.Holder()); | ||
| 562 | + Connection *ss = Connection::Unwrap(args); | ||
| 563 | + | ||
| 540 | 564 | int bytes_pending = BIO_pending(ss->bio_read_); | |
| 541 | 565 | return scope.Close(Integer::New(bytes_pending)); | |
| 542 | 566 | } | |
@@ -545,7 +569,8 @@ Handle<Value> Connection::ClearPending(const Arguments& args) { | |||
| 545 | 569 | Handle<Value> Connection::EncPending(const Arguments& args) { | |
| 546 | 570 | HandleScope scope; | |
| 547 | 571 | ||
| 548 | - Connection *ss = ObjectWrap::Unwrap<Connection>(args.Holder()); | ||
| 572 | + Connection *ss = Connection::Unwrap(args); | ||
| 573 | + | ||
| 549 | 574 | int bytes_pending = BIO_pending(ss->bio_write_); | |
| 550 | 575 | return scope.Close(Integer::New(bytes_pending)); | |
| 551 | 576 | } | |
@@ -554,7 +579,7 @@ Handle<Value> Connection::EncPending(const Arguments& args) { | |||
| 554 | 579 | Handle<Value> Connection::EncOut(const Arguments& args) { | |
| 555 | 580 | HandleScope scope; | |
| 556 | 581 | ||
| 557 | - Connection *ss = ObjectWrap::Unwrap<Connection>(args.Holder()); | ||
| 582 | + Connection *ss = Connection::Unwrap(args); | ||
| 558 | 583 | ||
| 559 | 584 | if (args.Length() < 3) { | |
| 560 | 585 | return ThrowException(Exception::TypeError( | |
@@ -582,7 +607,9 @@ Handle<Value> Connection::EncOut(const Arguments& args) { | |||
| 582 | 607 | String::New("Length is extends beyond buffer"))); | |
| 583 | 608 | } | |
| 584 | 609 | ||
| 585 | - int bytes_read = serr(ss->ssl_, "BIO_read:EncOut", BIO_read(ss->bio_write_, (char*)buffer_data + off, len)); | ||
| 610 | + int bytes_read = BIO_read(ss->bio_write_, (char*)buffer_data + off, len); | ||
| 611 | + | ||
| 612 | + ss->HandleError("BIO_read:EncOut", bytes_read, true); | ||
| 586 | 613 | ||
| 587 | 614 | return scope.Close(Integer::New(bytes_read)); | |
| 588 | 615 | } | |
@@ -591,7 +618,7 @@ Handle<Value> Connection::EncOut(const Arguments& args) { | |||
| 591 | 618 | Handle<Value> Connection::ClearIn(const Arguments& args) { | |
| 592 | 619 | HandleScope scope; | |
| 593 | 620 | ||
| 594 | - Connection *ss = ObjectWrap::Unwrap<Connection>(args.Holder()); | ||
| 621 | + Connection *ss = Connection::Unwrap(args); | ||
| 595 | 622 | ||
| 596 | 623 | if (args.Length() < 3) { | |
| 597 | 624 | return ThrowException(Exception::TypeError( | |
@@ -620,25 +647,21 @@ Handle<Value> Connection::ClearIn(const Arguments& args) { | |||
| 620 | 647 | } | |
| 621 | 648 | ||
| 622 | 649 | if (!SSL_is_init_finished(ss->ssl_)) { | |
| 623 | - int s; | ||
| 650 | + int rv; | ||
| 624 | 651 | if (ss->is_server_) { | |
| 625 | - s = serr(ss->ssl_, "SSL_accept:ClearIn", SSL_accept(ss->ssl_)); | ||
| 652 | + rv = SSL_accept(ss->ssl_); | ||
| 653 | + ss->HandleError("SSL_accept:ClearIn", rv); | ||
| 626 | 654 | } else { | |
| 627 | - s = serr(ss->ssl_, "SSL_connect:ClearIn", SSL_connect(ss->ssl_)); | ||
| 628 | - } | ||
| 629 | - | ||
| 630 | - if (s < 0) { | ||
| 631 | - return ThrowException(Exception::Error(v8::String::New(ssl_error_buf))); | ||
| 655 | + rv = SSL_connect(ss->ssl_); | ||
| 656 | + ss->HandleError("SSL_connect:ClearIn", rv); | ||
| 632 | 657 | } | |
| 633 | 658 | ||
| 634 | - return scope.Close(Integer::New(0)); | ||
| 659 | + if (rv < 0) return scope.Close(Integer::New(rv)); | ||
| 635 | 660 | } | |
| 636 | 661 | ||
| 637 | - int bytes_written = serr(ss->ssl_, "SSL_write:ClearIn", SSL_write(ss->ssl_, (char*)buffer_data + off, len)); | ||
| 662 | + int bytes_written = SSL_write(ss->ssl_, (char*)buffer_data + off, len); | ||
| 638 | 663 | ||
| 639 | - if (bytes_written < 0) { | ||
| 640 | - return ThrowException(Exception::Error(v8::String::New(ssl_error_buf))); | ||
| 641 | - } | ||
| 664 | + ss->HandleError("SSL_write:ClearIn", bytes_written); | ||
| 642 | 665 | ||
| 643 | 666 | return scope.Close(Integer::New(bytes_written)); | |
| 644 | 667 | } | |
@@ -647,7 +670,7 @@ Handle<Value> Connection::ClearIn(const Arguments& args) { | |||
| 647 | 670 | Handle<Value> Connection::GetPeerCertificate(const Arguments& args) { | |
| 648 | 671 | HandleScope scope; | |
| 649 | 672 | ||
| 650 | - Connection *ss = ObjectWrap::Unwrap<Connection>(args.Holder()); | ||
| 673 | + Connection *ss = Connection::Unwrap(args); | ||
| 651 | 674 | ||
| 652 | 675 | if (ss->ssl_ == NULL) return Undefined(); | |
| 653 | 676 | Local<Object> info = Object::New(); | |
@@ -719,36 +742,30 @@ Handle<Value> Connection::GetPeerCertificate(const Arguments& args) { | |||
| 719 | 742 | ||
| 720 | 743 | Handle<Value> Connection::Start(const Arguments& args) { | |
| 721 | 744 | HandleScope scope; | |
| 722 | - int rv; | ||
| 723 | 745 | ||
| 724 | - Connection *ss = ObjectWrap::Unwrap<Connection>(args.Holder()); | ||
| 746 | + Connection *ss = Connection::Unwrap(args); | ||
| 725 | 747 | ||
| 726 | 748 | if (!SSL_is_init_finished(ss->ssl_)) { | |
| 749 | + int rv; | ||
| 727 | 750 | if (ss->is_server_) { | |
| 728 | - rv = serr(ss->ssl_, "SSL_accept:Start", SSL_accept(ss->ssl_)); | ||
| 751 | + rv = SSL_accept(ss->ssl_); | ||
| 752 | + ss->HandleError("SSL_accept:Start", rv); | ||
| 729 | 753 | } else { | |
| 730 | - rv = serr(ss->ssl_, "SSL_connect:Start", SSL_connect(ss->ssl_)); | ||
| 731 | - } | ||
| 732 | - | ||
| 733 | - if (rv < 0) { | ||
| 734 | - return ThrowException(Exception::Error(v8::String::New(ssl_error_buf))); | ||
| 754 | + rv = SSL_connect(ss->ssl_); | ||
| 755 | + ss->HandleError("SSL_connect:Start", rv); | ||
| 735 | 756 | } | |
| 736 | 757 | ||
| 737 | - if (rv == 1) { | ||
| 738 | - return True(); | ||
| 739 | - } else { | ||
| 740 | - return False(); | ||
| 741 | - } | ||
| 758 | + return scope.Close(Integer::New(rv)); | ||
| 742 | 759 | } | |
| 743 | 760 | ||
| 744 | - return True(); | ||
| 761 | + return scope.Close(Integer::New(0)); | ||
| 745 | 762 | } | |
| 746 | 763 | ||
| 747 | 764 | ||
| 748 | 765 | Handle<Value> Connection::Shutdown(const Arguments& args) { | |
| 749 | 766 | HandleScope scope; | |
| 750 | 767 | ||
| 751 | - Connection *ss = ObjectWrap::Unwrap<Connection>(args.Holder()); | ||
| 768 | + Connection *ss = Connection::Unwrap(args); | ||
| 752 | 769 | ||
| 753 | 770 | if (ss->ssl_ == NULL) return False(); | |
| 754 | 771 | int r = SSL_shutdown(ss->ssl_); | |
@@ -760,7 +777,7 @@ Handle<Value> Connection::Shutdown(const Arguments& args) { | |||
| 760 | 777 | Handle<Value> Connection::ReceivedShutdown(const Arguments& args) { | |
| 761 | 778 | HandleScope scope; | |
| 762 | 779 | ||
| 763 | - Connection *ss = ObjectWrap::Unwrap<Connection>(args.Holder()); | ||
| 780 | + Connection *ss = Connection::Unwrap(args); | ||
| 764 | 781 | ||
| 765 | 782 | if (ss->ssl_ == NULL) return False(); | |
| 766 | 783 | int r = SSL_get_shutdown(ss->ssl_); | |
@@ -773,7 +790,9 @@ Handle<Value> Connection::ReceivedShutdown(const Arguments& args) { | |||
| 773 | 790 | ||
| 774 | 791 | Handle<Value> Connection::IsInitFinished(const Arguments& args) { | |
| 775 | 792 | HandleScope scope; | |
| 776 | - Connection *ss = ObjectWrap::Unwrap<Connection>(args.Holder()); | ||
| 793 | + | ||
| 794 | + Connection *ss = Connection::Unwrap(args); | ||
| 795 | + | ||
| 777 | 796 | if (ss->ssl_ == NULL) return False(); | |
| 778 | 797 | return SSL_is_init_finished(ss->ssl_) ? True() : False(); | |
| 779 | 798 | } | |
@@ -782,7 +801,7 @@ Handle<Value> Connection::IsInitFinished(const Arguments& args) { | |||
| 782 | 801 | Handle<Value> Connection::VerifyError(const Arguments& args) { | |
| 783 | 802 | HandleScope scope; | |
| 784 | 803 | ||
| 785 | - Connection *ss = ObjectWrap::Unwrap<Connection>(args.Holder()); | ||
| 804 | + Connection *ss = Connection::Unwrap(args); | ||
| 786 | 805 | ||
| 787 | 806 | if (ss->ssl_ == NULL) return Null(); | |
| 788 | 807 | ||
@@ -926,7 +945,8 @@ Handle<Value> Connection::VerifyError(const Arguments& args) { | |||
| 926 | 945 | Handle<Value> Connection::GetCurrentCipher(const Arguments& args) { | |
| 927 | 946 | HandleScope scope; | |
| 928 | 947 | ||
| 929 | - Connection *ss = ObjectWrap::Unwrap<Connection>(args.Holder()); | ||
| 948 | + Connection *ss = Connection::Unwrap(args); | ||
| 949 | + | ||
| 930 | 950 | OPENSSL_CONST SSL_CIPHER *c; | |
| 931 | 951 | ||
| 932 | 952 | if ( ss->ssl_ == NULL ) return Undefined(); | |
@@ -943,7 +963,7 @@ Handle<Value> Connection::GetCurrentCipher(const Arguments& args) { | |||
| 943 | 963 | Handle<Value> Connection::Close(const Arguments& args) { | |
| 944 | 964 | HandleScope scope; | |
| 945 | 965 | ||
| 946 | - Connection *ss = ObjectWrap::Unwrap<Connection>(args.Holder()); | ||
| 966 | + Connection *ss = Connection::Unwrap(args); | ||
| 947 | 967 | ||
| 948 | 968 | if (ss->ssl_ != NULL) { | |
| 949 | 969 | SSL_free(ss->ssl_); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments