OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of dart.io; | 5 part of dart.io; |
6 | 6 |
7 /** | 7 /** |
8 * A high-level class for communicating securely over a TCP socket, using | 8 * A high-level class for communicating securely over a TCP socket, using |
9 * TLS and SSL. The [SecureSocket] exposes both a [Stream] and an | 9 * TLS and SSL. The [SecureSocket] exposes both a [Stream] and an |
10 * [IOSink] interface, making it ideal for using together with | 10 * [IOSink] interface, making it ideal for using together with |
(...skipping 498 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
509 onResume: _onPauseStateChange, | 509 onResume: _onPauseStateChange, |
510 onCancel: _onSubscriptionStateChange); | 510 onCancel: _onSubscriptionStateChange); |
511 _stream = _controller.stream; | 511 _stream = _controller.stream; |
512 // Throw an ArgumentError if any field is invalid. After this, all | 512 // Throw an ArgumentError if any field is invalid. After this, all |
513 // errors will be reported through the future or the stream. | 513 // errors will be reported through the future or the stream. |
514 _secureFilter.init(); | 514 _secureFilter.init(); |
515 _filterPointer = _secureFilter._pointer(); | 515 _filterPointer = _secureFilter._pointer(); |
516 _secureFilter.registerHandshakeCompleteCallback( | 516 _secureFilter.registerHandshakeCompleteCallback( |
517 _secureHandshakeCompleteHandler); | 517 _secureHandshakeCompleteHandler); |
518 if (onBadCertificate != null) { | 518 if (onBadCertificate != null) { |
519 _secureFilter.registerBadCertificateCallback(onBadCertificate); | 519 _secureFilter.registerBadCertificateCallback(_onBadCertificateWrapper); |
520 } | 520 } |
521 var futureSocket; | 521 var futureSocket; |
522 if (socket == null) { | 522 if (socket == null) { |
523 futureSocket = RawSocket.connect(address, requestedPort); | 523 futureSocket = RawSocket.connect(address, requestedPort); |
524 } else { | 524 } else { |
525 futureSocket = new Future.value(socket); | 525 futureSocket = new Future.value(socket); |
526 } | 526 } |
527 futureSocket.then((rawSocket) { | 527 futureSocket.then((rawSocket) { |
528 _connectPending = true; | 528 _connectPending = true; |
529 _socket = rawSocket; | 529 _socket = rawSocket; |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
713 _secureFilter.buffers[WRITE_PLAINTEXT].write(data, offset, bytes); | 713 _secureFilter.buffers[WRITE_PLAINTEXT].write(data, offset, bytes); |
714 if (written > 0) { | 714 if (written > 0) { |
715 _filterStatus.writeEmpty = false; | 715 _filterStatus.writeEmpty = false; |
716 } | 716 } |
717 _scheduleFilter(); | 717 _scheduleFilter(); |
718 return written; | 718 return written; |
719 } | 719 } |
720 | 720 |
721 X509Certificate get peerCertificate => _secureFilter.peerCertificate; | 721 X509Certificate get peerCertificate => _secureFilter.peerCertificate; |
722 | 722 |
| 723 bool _onBadCertificateWrapper(X509Certificate certificate) { |
| 724 if (onBadCertificate == null) return false; |
| 725 var result = onBadCertificate(certificate); |
| 726 if (result is bool) return result; |
| 727 throw new ArgumentError( |
| 728 "onBadCertificate callback returned non-boolean $result"); |
| 729 } |
| 730 |
723 bool setOption(SocketOption option, bool enabled) { | 731 bool setOption(SocketOption option, bool enabled) { |
724 if (_socket == null) return false; | 732 if (_socket == null) return false; |
725 return _socket.setOption(option, enabled); | 733 return _socket.setOption(option, enabled); |
726 } | 734 } |
727 | 735 |
728 void _eventDispatcher(RawSocketEvent event) { | 736 void _eventDispatcher(RawSocketEvent event) { |
729 try { | 737 try { |
730 if (event == RawSocketEvent.READ) { | 738 if (event == RawSocketEvent.READ) { |
731 _readHandler(); | 739 _readHandler(); |
732 } else if (event == RawSocketEvent.WRITE) { | 740 } else if (event == RawSocketEvent.WRITE) { |
(...skipping 21 matching lines...) Expand all Loading... |
754 _close(); | 762 _close(); |
755 } | 763 } |
756 } | 764 } |
757 | 765 |
758 void _reportError(e) { | 766 void _reportError(e) { |
759 if (_status == CLOSED) { | 767 if (_status == CLOSED) { |
760 return; | 768 return; |
761 } else if (_connectPending) { | 769 } else if (_connectPending) { |
762 // _connectPending is true after the underlying connection has been | 770 // _connectPending is true after the underlying connection has been |
763 // made, but before the handshake has completed. | 771 // made, but before the handshake has completed. |
764 if (e is! TlsException) { | |
765 e = new HandshakeException("$e", null); | |
766 } | |
767 _handshakeComplete.completeError(e); | 772 _handshakeComplete.completeError(e); |
768 } else { | 773 } else { |
769 _controller.addError(e); | 774 _controller.addError(e); |
770 } | 775 } |
771 _close(); | 776 _close(); |
772 } | 777 } |
773 | 778 |
774 void _closeHandler() { | 779 void _closeHandler() { |
775 if (_status == CONNECTED) { | 780 if (_status == CONNECTED) { |
776 if (_closedRead) return; | 781 if (_closedRead) return; |
(...skipping 478 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1255 /** | 1260 /** |
1256 * An exception that happens in the handshake phase of establishing | 1261 * An exception that happens in the handshake phase of establishing |
1257 * a secure network connection, when looking up or verifying a | 1262 * a secure network connection, when looking up or verifying a |
1258 * certificate. | 1263 * certificate. |
1259 */ | 1264 */ |
1260 class CertificateException extends TlsException { | 1265 class CertificateException extends TlsException { |
1261 const CertificateException([String message = "", | 1266 const CertificateException([String message = "", |
1262 OSError osError = null]) | 1267 OSError osError = null]) |
1263 : super._("CertificateException", message, osError); | 1268 : super._("CertificateException", message, osError); |
1264 } | 1269 } |
OLD | NEW |