OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // This file includes code SSLClientSocketNSS::DoVerifyCertComplete() derived | 5 // This file includes code SSLClientSocketNSS::DoVerifyCertComplete() derived |
6 // from AuthCertificateCallback() in | 6 // from AuthCertificateCallback() in |
7 // mozilla/security/manager/ssl/src/nsNSSCallbacks.cpp. | 7 // mozilla/security/manager/ssl/src/nsNSSCallbacks.cpp. |
8 | 8 |
9 /* ***** BEGIN LICENSE BLOCK ***** | 9 /* ***** BEGIN LICENSE BLOCK ***** |
10 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 | 10 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
(...skipping 624 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
635 // any time. | 635 // any time. |
636 const HandshakeState& state() const { return network_handshake_state_; } | 636 const HandshakeState& state() const { return network_handshake_state_; } |
637 | 637 |
638 // Called on the network task runner. | 638 // Called on the network task runner. |
639 // Read() and Write() mirror the net::Socket functions of the same name. | 639 // Read() and Write() mirror the net::Socket functions of the same name. |
640 // If ERR_IO_PENDING is returned, |callback| will be invoked on the network | 640 // If ERR_IO_PENDING is returned, |callback| will be invoked on the network |
641 // task runner at a later point, unless the caller calls Detach(). | 641 // task runner at a later point, unless the caller calls Detach(). |
642 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); | 642 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); |
643 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback); | 643 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback); |
644 | 644 |
645 // Called on the network task runner. | |
646 bool IsClosed(); | |
647 bool HasPendingAsyncOperation(); | |
648 bool HasUnhandledReceivedData(); | |
649 | |
645 private: | 650 private: |
646 friend class base::RefCountedThreadSafe<Core>; | 651 friend class base::RefCountedThreadSafe<Core>; |
647 ~Core(); | 652 ~Core(); |
648 | 653 |
649 enum State { | 654 enum State { |
650 STATE_NONE, | 655 STATE_NONE, |
651 STATE_HANDSHAKE, | 656 STATE_HANDSHAKE, |
652 STATE_GET_DOMAIN_BOUND_CERT_COMPLETE, | 657 STATE_GET_DOMAIN_BOUND_CERT_COMPLETE, |
653 }; | 658 }; |
654 | 659 |
660 enum Operation { | |
661 OPERATION_READ, | |
662 OPERATION_WRITE, | |
663 OPERATION_UPDATE, | |
664 }; | |
665 | |
655 bool OnNSSTaskRunner() const; | 666 bool OnNSSTaskRunner() const; |
656 bool OnNetworkTaskRunner() const; | 667 bool OnNetworkTaskRunner() const; |
657 | 668 |
658 //////////////////////////////////////////////////////////////////////////// | 669 //////////////////////////////////////////////////////////////////////////// |
659 // Methods that are ONLY called on the NSS task runner: | 670 // Methods that are ONLY called on the NSS task runner: |
660 //////////////////////////////////////////////////////////////////////////// | 671 //////////////////////////////////////////////////////////////////////////// |
661 | 672 |
662 // Called by NSS during full handshakes to allow the application to | 673 // Called by NSS during full handshakes to allow the application to |
663 // verify the certificate. Instead of verifying the certificate in the midst | 674 // verify the certificate. Instead of verifying the certificate in the midst |
664 // of the handshake, SECSuccess is always returned and the peer's certificate | 675 // of the handshake, SECSuccess is always returned and the peer's certificate |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
733 bool DoTransportIO(); | 744 bool DoTransportIO(); |
734 int BufferRecv(); | 745 int BufferRecv(); |
735 int BufferSend(); | 746 int BufferSend(); |
736 | 747 |
737 void OnRecvComplete(int result); | 748 void OnRecvComplete(int result); |
738 void OnSendComplete(int result); | 749 void OnSendComplete(int result); |
739 | 750 |
740 void DoConnectCallback(int result); | 751 void DoConnectCallback(int result); |
741 void DoReadCallback(int result); | 752 void DoReadCallback(int result); |
742 void DoWriteCallback(int result); | 753 void DoWriteCallback(int result); |
754 void UpdateNSSTaskRunnerStatus(int amount_in_read_buffer, | |
755 bool closed, | |
756 Operation operation, | |
757 const base::Closure& callback); | |
743 | 758 |
744 // Client channel ID handler. | 759 // Client channel ID handler. |
745 static SECStatus ClientChannelIDHandler( | 760 static SECStatus ClientChannelIDHandler( |
746 void* arg, | 761 void* arg, |
747 PRFileDesc* socket, | 762 PRFileDesc* socket, |
748 SECKEYPublicKey **out_public_key, | 763 SECKEYPublicKey **out_public_key, |
749 SECKEYPrivateKey **out_private_key); | 764 SECKEYPrivateKey **out_private_key); |
750 | 765 |
751 // ImportChannelIDKeys is a helper function for turning a DER-encoded cert and | 766 // ImportChannelIDKeys is a helper function for turning a DER-encoded cert and |
752 // key into a SECKEYPublicKey and SECKEYPrivateKey. Returns OK upon success | 767 // key into a SECKEYPublicKey and SECKEYPrivateKey. Returns OK upon success |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
810 ClientSocketHandle* transport_; | 825 ClientSocketHandle* transport_; |
811 base::WeakPtrFactory<BoundNetLog> weak_net_log_factory_; | 826 base::WeakPtrFactory<BoundNetLog> weak_net_log_factory_; |
812 | 827 |
813 // The current handshake state. Mirrors |nss_handshake_state_|. | 828 // The current handshake state. Mirrors |nss_handshake_state_|. |
814 HandshakeState network_handshake_state_; | 829 HandshakeState network_handshake_state_; |
815 | 830 |
816 // The service for retrieving Channel ID keys. May be NULL. | 831 // The service for retrieving Channel ID keys. May be NULL. |
817 ServerBoundCertService* server_bound_cert_service_; | 832 ServerBoundCertService* server_bound_cert_service_; |
818 ServerBoundCertService::RequestHandle domain_bound_cert_request_handle_; | 833 ServerBoundCertService::RequestHandle domain_bound_cert_request_handle_; |
819 | 834 |
835 // The information about NSS task runner. | |
836 int unhandled_buffer_size_; | |
837 bool nss_waiting_read_; | |
838 bool nss_waiting_write_; | |
839 bool nss_is_closed_; | |
840 | |
820 //////////////////////////////////////////////////////////////////////////// | 841 //////////////////////////////////////////////////////////////////////////// |
821 // Members that are ONLY accessed on the NSS task runner: | 842 // Members that are ONLY accessed on the NSS task runner: |
822 //////////////////////////////////////////////////////////////////////////// | 843 //////////////////////////////////////////////////////////////////////////// |
823 HostPortPair host_and_port_; | 844 HostPortPair host_and_port_; |
824 SSLConfig ssl_config_; | 845 SSLConfig ssl_config_; |
825 | 846 |
826 // NSS SSL socket. | 847 // NSS SSL socket. |
827 PRFileDesc* nss_fd_; | 848 PRFileDesc* nss_fd_; |
828 | 849 |
829 // Buffers for the network end of the SSL state machine | 850 // Buffers for the network end of the SSL state machine |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
889 base::SequencedTaskRunner* nss_task_runner, | 910 base::SequencedTaskRunner* nss_task_runner, |
890 ClientSocketHandle* transport, | 911 ClientSocketHandle* transport, |
891 const HostPortPair& host_and_port, | 912 const HostPortPair& host_and_port, |
892 const SSLConfig& ssl_config, | 913 const SSLConfig& ssl_config, |
893 BoundNetLog* net_log, | 914 BoundNetLog* net_log, |
894 ServerBoundCertService* server_bound_cert_service) | 915 ServerBoundCertService* server_bound_cert_service) |
895 : detached_(false), | 916 : detached_(false), |
896 transport_(transport), | 917 transport_(transport), |
897 weak_net_log_factory_(net_log), | 918 weak_net_log_factory_(net_log), |
898 server_bound_cert_service_(server_bound_cert_service), | 919 server_bound_cert_service_(server_bound_cert_service), |
920 unhandled_buffer_size_(0), | |
921 nss_waiting_read_(false), | |
922 nss_waiting_write_(false), | |
923 nss_is_closed_(false), | |
899 host_and_port_(host_and_port), | 924 host_and_port_(host_and_port), |
900 ssl_config_(ssl_config), | 925 ssl_config_(ssl_config), |
901 nss_fd_(NULL), | 926 nss_fd_(NULL), |
902 nss_bufs_(NULL), | 927 nss_bufs_(NULL), |
903 next_handshake_state_(STATE_NONE), | 928 next_handshake_state_(STATE_NONE), |
904 channel_id_xtn_negotiated_(false), | 929 channel_id_xtn_negotiated_(false), |
905 channel_id_needed_(false), | 930 channel_id_needed_(false), |
906 client_auth_cert_needed_(false), | 931 client_auth_cert_needed_(false), |
907 handshake_callback_called_(false), | 932 handshake_callback_called_(false), |
908 transport_recv_busy_(false), | 933 transport_recv_busy_(false), |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1097 return posted ? ERR_IO_PENDING : ERR_ABORTED; | 1122 return posted ? ERR_IO_PENDING : ERR_ABORTED; |
1098 } | 1123 } |
1099 | 1124 |
1100 DCHECK(OnNSSTaskRunner()); | 1125 DCHECK(OnNSSTaskRunner()); |
1101 DCHECK(handshake_callback_called_); | 1126 DCHECK(handshake_callback_called_); |
1102 DCHECK_EQ(STATE_NONE, next_handshake_state_); | 1127 DCHECK_EQ(STATE_NONE, next_handshake_state_); |
1103 DCHECK(user_read_callback_.is_null()); | 1128 DCHECK(user_read_callback_.is_null()); |
1104 DCHECK(user_connect_callback_.is_null()); | 1129 DCHECK(user_connect_callback_.is_null()); |
1105 DCHECK(!user_read_buf_); | 1130 DCHECK(!user_read_buf_); |
1106 DCHECK(nss_bufs_); | 1131 DCHECK(nss_bufs_); |
1132 DCHECK(!nss_waiting_read_); | |
1107 | 1133 |
1108 user_read_buf_ = buf; | 1134 user_read_buf_ = buf; |
1109 user_read_buf_len_ = buf_len; | 1135 user_read_buf_len_ = buf_len; |
1110 | 1136 |
1111 int rv = DoReadLoop(OK); | 1137 int rv = DoReadLoop(OK); |
1112 if (rv == ERR_IO_PENDING) { | 1138 if (rv == ERR_IO_PENDING) { |
1113 user_read_callback_ = callback; | 1139 user_read_callback_ = callback; |
1140 nss_waiting_read_ = true; | |
1114 } else { | 1141 } else { |
1115 user_read_buf_ = NULL; | 1142 user_read_buf_ = NULL; |
1116 user_read_buf_len_ = 0; | 1143 user_read_buf_len_ = 0; |
1117 | 1144 |
1118 if (!OnNetworkTaskRunner()) { | 1145 if (!OnNetworkTaskRunner()) { |
1119 PostOrRunCallback(FROM_HERE, base::Bind(callback, rv)); | 1146 PostOrRunCallback(FROM_HERE, base::Bind(callback, rv)); |
1120 return ERR_IO_PENDING; | 1147 return ERR_IO_PENDING; |
1121 } | 1148 } |
1122 } | 1149 } |
1123 | 1150 |
(...skipping 15 matching lines...) Expand all Loading... | |
1139 return rv; | 1166 return rv; |
1140 } | 1167 } |
1141 | 1168 |
1142 DCHECK(OnNSSTaskRunner()); | 1169 DCHECK(OnNSSTaskRunner()); |
1143 DCHECK(handshake_callback_called_); | 1170 DCHECK(handshake_callback_called_); |
1144 DCHECK_EQ(STATE_NONE, next_handshake_state_); | 1171 DCHECK_EQ(STATE_NONE, next_handshake_state_); |
1145 DCHECK(user_write_callback_.is_null()); | 1172 DCHECK(user_write_callback_.is_null()); |
1146 DCHECK(user_connect_callback_.is_null()); | 1173 DCHECK(user_connect_callback_.is_null()); |
1147 DCHECK(!user_write_buf_); | 1174 DCHECK(!user_write_buf_); |
1148 DCHECK(nss_bufs_); | 1175 DCHECK(nss_bufs_); |
1176 DCHECK(!nss_waiting_write_); | |
1149 | 1177 |
1150 user_write_buf_ = buf; | 1178 user_write_buf_ = buf; |
1151 user_write_buf_len_ = buf_len; | 1179 user_write_buf_len_ = buf_len; |
1152 | 1180 |
1153 int rv = DoWriteLoop(OK); | 1181 int rv = DoWriteLoop(OK); |
1154 if (rv == ERR_IO_PENDING) { | 1182 if (rv == ERR_IO_PENDING) { |
1155 user_write_callback_ = callback; | 1183 user_write_callback_ = callback; |
1184 nss_waiting_write_ = true; | |
1156 } else { | 1185 } else { |
1157 user_write_buf_ = NULL; | 1186 user_write_buf_ = NULL; |
1158 user_write_buf_len_ = 0; | 1187 user_write_buf_len_ = 0; |
1159 | 1188 |
1160 if (!OnNetworkTaskRunner()) { | 1189 if (!OnNetworkTaskRunner()) { |
1161 PostOrRunCallback(FROM_HERE, base::Bind(callback, rv)); | 1190 PostOrRunCallback(FROM_HERE, base::Bind(callback, rv)); |
1162 return ERR_IO_PENDING; | 1191 return ERR_IO_PENDING; |
1163 } | 1192 } |
1164 } | 1193 } |
1165 | 1194 |
1166 return rv; | 1195 return rv; |
1167 } | 1196 } |
1168 | 1197 |
1198 bool SSLClientSocketNSS::Core::IsClosed() { | |
1199 return nss_is_closed_; | |
1200 } | |
1201 | |
1202 bool SSLClientSocketNSS::Core::HasPendingAsyncOperation() { | |
1203 DCHECK(OnNetworkTaskRunner()); | |
1204 return nss_waiting_read_ || nss_waiting_write_; | |
1205 } | |
1206 | |
1207 bool SSLClientSocketNSS::Core::HasUnhandledReceivedData() { | |
1208 DCHECK(OnNetworkTaskRunner()); | |
1209 return unhandled_buffer_size_ != 0; | |
1210 } | |
1211 | |
1169 bool SSLClientSocketNSS::Core::OnNSSTaskRunner() const { | 1212 bool SSLClientSocketNSS::Core::OnNSSTaskRunner() const { |
1170 return nss_task_runner_->RunsTasksOnCurrentThread(); | 1213 return nss_task_runner_->RunsTasksOnCurrentThread(); |
1171 } | 1214 } |
1172 | 1215 |
1173 bool SSLClientSocketNSS::Core::OnNetworkTaskRunner() const { | 1216 bool SSLClientSocketNSS::Core::OnNetworkTaskRunner() const { |
1174 return network_task_runner_->RunsTasksOnCurrentThread(); | 1217 return network_task_runner_->RunsTasksOnCurrentThread(); |
1175 } | 1218 } |
1176 | 1219 |
1177 // static | 1220 // static |
1178 SECStatus SSLClientSocketNSS::Core::OwnAuthCertHandler( | 1221 SECStatus SSLClientSocketNSS::Core::OwnAuthCertHandler( |
(...skipping 858 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2037 GotoState(STATE_HANDSHAKE); | 2080 GotoState(STATE_HANDSHAKE); |
2038 return OK; | 2081 return OK; |
2039 } | 2082 } |
2040 | 2083 |
2041 int SSLClientSocketNSS::Core::DoPayloadRead() { | 2084 int SSLClientSocketNSS::Core::DoPayloadRead() { |
2042 DCHECK(OnNSSTaskRunner()); | 2085 DCHECK(OnNSSTaskRunner()); |
2043 DCHECK(user_read_buf_); | 2086 DCHECK(user_read_buf_); |
2044 DCHECK_GT(user_read_buf_len_, 0); | 2087 DCHECK_GT(user_read_buf_len_, 0); |
2045 | 2088 |
2046 int rv = PR_Read(nss_fd_, user_read_buf_->data(), user_read_buf_len_); | 2089 int rv = PR_Read(nss_fd_, user_read_buf_->data(), user_read_buf_len_); |
2090 // Update NSSTaskRunner status because PR_Read may consume |nss_bufs_|, then | |
2091 // following |amount_in_read_buffer| may be changed here. | |
2092 int amount_in_read_buffer = memio_GetReadableBufferSize(nss_bufs_); | |
2093 base::Closure task = base::Bind( | |
2094 &Core::UpdateNSSTaskRunnerStatus, | |
2095 this, | |
2096 amount_in_read_buffer, | |
2097 rv <= 0 && rv != ERR_IO_PENDING, | |
2098 OPERATION_UPDATE, | |
2099 base::Closure()); | |
2100 | |
2047 if (client_auth_cert_needed_) { | 2101 if (client_auth_cert_needed_) { |
2048 // We don't need to invalidate the non-client-authenticated SSL session | 2102 // We don't need to invalidate the non-client-authenticated SSL session |
2049 // because the server will renegotiate anyway. | 2103 // because the server will renegotiate anyway. |
2050 rv = ERR_SSL_CLIENT_AUTH_CERT_NEEDED; | 2104 rv = ERR_SSL_CLIENT_AUTH_CERT_NEEDED; |
2051 PostOrRunCallback( | 2105 PostOrRunCallback( |
2052 FROM_HERE, | 2106 FROM_HERE, |
2053 base::Bind(&AddLogEventWithCallback, weak_net_log_, | 2107 base::Bind(&AddLogEventWithCallback, weak_net_log_, |
2054 NetLog::TYPE_SSL_READ_ERROR, | 2108 NetLog::TYPE_SSL_READ_ERROR, |
2055 CreateNetLogSSLErrorCallback(rv, 0))); | 2109 CreateNetLogSSLErrorCallback(rv, 0))); |
2056 return rv; | 2110 return rv; |
(...skipping 17 matching lines...) Expand all Loading... | |
2074 NetLog::TYPE_SSL_READ_ERROR, | 2128 NetLog::TYPE_SSL_READ_ERROR, |
2075 CreateNetLogSSLErrorCallback(rv, prerr))); | 2129 CreateNetLogSSLErrorCallback(rv, prerr))); |
2076 return rv; | 2130 return rv; |
2077 } | 2131 } |
2078 | 2132 |
2079 int SSLClientSocketNSS::Core::DoPayloadWrite() { | 2133 int SSLClientSocketNSS::Core::DoPayloadWrite() { |
2080 DCHECK(OnNSSTaskRunner()); | 2134 DCHECK(OnNSSTaskRunner()); |
2081 | 2135 |
2082 DCHECK(user_write_buf_); | 2136 DCHECK(user_write_buf_); |
2083 | 2137 |
2138 int old_amount_in_read_buffer = memio_GetReadableBufferSize(nss_bufs_); | |
2084 int rv = PR_Write(nss_fd_, user_write_buf_->data(), user_write_buf_len_); | 2139 int rv = PR_Write(nss_fd_, user_write_buf_->data(), user_write_buf_len_); |
2140 int new_amount_in_read_buffer = memio_GetReadableBufferSize(nss_bufs_); | |
2141 // PR_Write could potentially consume the unhandled data in the memio read | |
2142 // buffer if a renegotiation is in progress. If the buffer is consumed, | |
2143 // notify the latest buffer size to NetworkRunner. | |
2144 if (old_amount_in_read_buffer != new_amount_in_read_buffer) { | |
2145 base::Closure task = base::Bind( | |
2146 &Core::UpdateNSSTaskRunnerStatus, | |
2147 this, | |
2148 new_amount_in_read_buffer, | |
2149 false, | |
2150 OPERATION_UPDATE, | |
2151 base::Closure()); | |
2152 } | |
2085 if (rv >= 0) { | 2153 if (rv >= 0) { |
2086 PostOrRunCallback( | 2154 PostOrRunCallback( |
2087 FROM_HERE, | 2155 FROM_HERE, |
2088 base::Bind(&LogByteTransferEvent, weak_net_log_, | 2156 base::Bind(&LogByteTransferEvent, weak_net_log_, |
2089 NetLog::TYPE_SSL_SOCKET_BYTES_SENT, rv, | 2157 NetLog::TYPE_SSL_SOCKET_BYTES_SENT, rv, |
2090 scoped_refptr<IOBuffer>(user_write_buf_))); | 2158 scoped_refptr<IOBuffer>(user_write_buf_))); |
2091 return rv; | 2159 return rv; |
2092 } | 2160 } |
2093 PRErrorCode prerr = PR_GetError(); | 2161 PRErrorCode prerr = PR_GetError(); |
2094 if (prerr == PR_WOULD_BLOCK_ERROR) | 2162 if (prerr == PR_WOULD_BLOCK_ERROR) |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2280 PostOrRunCallback(FROM_HERE, c); | 2348 PostOrRunCallback(FROM_HERE, c); |
2281 } | 2349 } |
2282 | 2350 |
2283 void SSLClientSocketNSS::Core::DoReadCallback(int rv) { | 2351 void SSLClientSocketNSS::Core::DoReadCallback(int rv) { |
2284 DCHECK(OnNSSTaskRunner()); | 2352 DCHECK(OnNSSTaskRunner()); |
2285 DCHECK_NE(ERR_IO_PENDING, rv); | 2353 DCHECK_NE(ERR_IO_PENDING, rv); |
2286 DCHECK(!user_read_callback_.is_null()); | 2354 DCHECK(!user_read_callback_.is_null()); |
2287 | 2355 |
2288 user_read_buf_ = NULL; | 2356 user_read_buf_ = NULL; |
2289 user_read_buf_len_ = 0; | 2357 user_read_buf_len_ = 0; |
2290 base::Closure c = base::Bind( | 2358 int amount_in_read_buffer = memio_GetReadableBufferSize(nss_bufs_); |
2291 base::ResetAndReturn(&user_read_callback_), | 2359 // This will invoke the user callback with |rv| as result. |
2292 rv); | 2360 base::Closure user_cb = base::Bind( |
2293 PostOrRunCallback(FROM_HERE, c); | 2361 base::ResetAndReturn(&user_read_callback_), rv); |
2362 // This is used to curry the |amount_int_read_buffer| and |user_cb| back to | |
2363 // the network task runner. | |
2364 base::Closure task = base::Bind( | |
2365 &Core::UpdateNSSTaskRunnerStatus, | |
2366 this, | |
2367 amount_in_read_buffer, | |
2368 rv <= 0, // EOF or errors. | |
2369 OPERATION_READ, | |
2370 user_cb); | |
2371 PostOrRunCallback(FROM_HERE, task); | |
2294 } | 2372 } |
2295 | 2373 |
2296 void SSLClientSocketNSS::Core::DoWriteCallback(int rv) { | 2374 void SSLClientSocketNSS::Core::DoWriteCallback(int rv) { |
2297 DCHECK(OnNSSTaskRunner()); | 2375 DCHECK(OnNSSTaskRunner()); |
2298 DCHECK_NE(ERR_IO_PENDING, rv); | 2376 DCHECK_NE(ERR_IO_PENDING, rv); |
2299 DCHECK(!user_write_callback_.is_null()); | 2377 DCHECK(!user_write_callback_.is_null()); |
2300 | 2378 |
2301 // Since Run may result in Write being called, clear |user_write_callback_| | 2379 // Since Run may result in Write being called, clear |user_write_callback_| |
2302 // up front. | 2380 // up front. |
2303 user_write_buf_ = NULL; | 2381 user_write_buf_ = NULL; |
2304 user_write_buf_len_ = 0; | 2382 user_write_buf_len_ = 0; |
2305 base::Closure c = base::Bind( | 2383 // Update buffer status because DoWriteLoop called DoTransportIO which may |
2306 base::ResetAndReturn(&user_write_callback_), | 2384 // perform read operations. |
2307 rv); | 2385 int amount_in_read_buffer = memio_GetReadableBufferSize(nss_bufs_); |
2308 PostOrRunCallback(FROM_HERE, c); | 2386 // This will invoke the user callback with |rv| as result. |
2387 base::Closure user_cb = base::Bind( | |
2388 base::ResetAndReturn(&user_write_callback_), rv); | |
2389 // This is used to curry the |amount_int_read_buffer| and |user_cb| back to | |
2390 // the network task runner. | |
2391 base::Closure task = base::Bind( | |
2392 &Core::UpdateNSSTaskRunnerStatus, | |
2393 this, | |
2394 amount_in_read_buffer, | |
2395 rv < 0, // Errors. | |
2396 OPERATION_WRITE, | |
2397 user_cb); | |
2398 PostOrRunCallback(FROM_HERE, task); | |
2399 } | |
2400 | |
2401 void SSLClientSocketNSS::Core::UpdateNSSTaskRunnerStatus( | |
2402 int amount_in_read_buffer, | |
2403 bool closed, | |
2404 Operation operation, | |
2405 const base::Closure& callback) { | |
2406 DCHECK(OnNetworkTaskRunner()); | |
2407 DCHECK(!callback.is_null()); | |
Ryan Sleevi
2013/01/18 00:03:24
Have you actually run this on a debug build on Lin
Takashi Toyoshima
2013/01/21 14:51:17
Thanks.
I didn't check it in debug build this time
| |
2408 DCHECK((operation == OPERATION_READ && nss_waiting_read_) || | |
2409 (operation == OPERATION_WRITE && nss_waiting_write_) || | |
2410 operation == OPERATION_UPDATE); | |
2411 | |
2412 unhandled_buffer_size_ = amount_in_read_buffer; | |
2413 if (closed) | |
2414 nss_is_closed_ = true; | |
2415 if (operation == OPERATION_READ) | |
2416 nss_waiting_read_ = false; | |
2417 else if (operation == OPERATION_WRITE) | |
2418 nss_waiting_write_ = false; | |
2419 | |
2420 if (operation != OPERATION_UPDATE) | |
2421 callback.Run(); | |
2309 } | 2422 } |
2310 | 2423 |
2311 SECStatus SSLClientSocketNSS::Core::ClientChannelIDHandler( | 2424 SECStatus SSLClientSocketNSS::Core::ClientChannelIDHandler( |
2312 void* arg, | 2425 void* arg, |
2313 PRFileDesc* socket, | 2426 PRFileDesc* socket, |
2314 SECKEYPublicKey **out_public_key, | 2427 SECKEYPublicKey **out_public_key, |
2315 SECKEYPrivateKey **out_private_key) { | 2428 SECKEYPrivateKey **out_private_key) { |
2316 Core* core = reinterpret_cast<Core*>(arg); | 2429 Core* core = reinterpret_cast<Core*>(arg); |
2317 DCHECK(core->OnNSSTaskRunner()); | 2430 DCHECK(core->OnNSSTaskRunner()); |
2318 | 2431 |
(...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2727 const SSLConfig& ssl_config, | 2840 const SSLConfig& ssl_config, |
2728 const SSLClientSocketContext& context) | 2841 const SSLClientSocketContext& context) |
2729 : nss_task_runner_(nss_task_runner), | 2842 : nss_task_runner_(nss_task_runner), |
2730 transport_(transport_socket), | 2843 transport_(transport_socket), |
2731 host_and_port_(host_and_port), | 2844 host_and_port_(host_and_port), |
2732 ssl_config_(ssl_config), | 2845 ssl_config_(ssl_config), |
2733 cert_verifier_(context.cert_verifier), | 2846 cert_verifier_(context.cert_verifier), |
2734 server_bound_cert_service_(context.server_bound_cert_service), | 2847 server_bound_cert_service_(context.server_bound_cert_service), |
2735 ssl_session_cache_shard_(context.ssl_session_cache_shard), | 2848 ssl_session_cache_shard_(context.ssl_session_cache_shard), |
2736 completed_handshake_(false), | 2849 completed_handshake_(false), |
2850 ssl_is_closed_(false), | |
2737 next_handshake_state_(STATE_NONE), | 2851 next_handshake_state_(STATE_NONE), |
2738 nss_fd_(NULL), | 2852 nss_fd_(NULL), |
2739 net_log_(transport_socket->socket()->NetLog()), | 2853 net_log_(transport_socket->socket()->NetLog()), |
2740 transport_security_state_(context.transport_security_state), | 2854 transport_security_state_(context.transport_security_state), |
2741 valid_thread_id_(base::kInvalidThreadId) { | 2855 valid_thread_id_(base::kInvalidThreadId) { |
2742 EnterFunction(""); | 2856 EnterFunction(""); |
2743 InitCore(); | 2857 InitCore(); |
2744 LeaveFunction(""); | 2858 LeaveFunction(""); |
2745 } | 2859 } |
2746 | 2860 |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2915 user_connect_callback_.Reset(); | 3029 user_connect_callback_.Reset(); |
2916 server_cert_verify_result_.Reset(); | 3030 server_cert_verify_result_.Reset(); |
2917 completed_handshake_ = false; | 3031 completed_handshake_ = false; |
2918 start_cert_verification_time_ = base::TimeTicks(); | 3032 start_cert_verification_time_ = base::TimeTicks(); |
2919 InitCore(); | 3033 InitCore(); |
2920 | 3034 |
2921 LeaveFunction(""); | 3035 LeaveFunction(""); |
2922 } | 3036 } |
2923 | 3037 |
2924 bool SSLClientSocketNSS::IsConnected() const { | 3038 bool SSLClientSocketNSS::IsConnected() const { |
2925 // Ideally, we should also check if we have received the close_notify alert | |
2926 // message from the server, and return false in that case. We're not doing | |
2927 // that, so this function may return a false positive. Since the upper | |
2928 // layer (HttpNetworkTransaction) needs to handle a persistent connection | |
2929 // closed by the server when we send a request anyway, a false positive in | |
2930 // exchange for simpler code is a good trade-off. | |
2931 EnterFunction(""); | 3039 EnterFunction(""); |
2932 bool ret = completed_handshake_ && transport_->socket()->IsConnected(); | 3040 bool ret = completed_handshake_ && |
3041 !ssl_is_closed_ && | |
3042 (core_->HasPendingAsyncOperation() || | |
3043 (!core_->IsClosed() && core_->HasUnhandledReceivedData()) || | |
3044 transport_->socket()->IsConnected()); | |
Ryan Sleevi
2013/01/18 00:03:24
indents are wrong here
Takashi Toyoshima
2013/01/21 14:51:17
Done.
| |
2933 LeaveFunction(""); | 3045 LeaveFunction(""); |
2934 return ret; | 3046 return ret; |
2935 } | 3047 } |
2936 | 3048 |
2937 bool SSLClientSocketNSS::IsConnectedAndIdle() const { | 3049 bool SSLClientSocketNSS::IsConnectedAndIdle() const { |
2938 // Unlike IsConnected, this method doesn't return a false positive. | |
2939 // | |
2940 // Strictly speaking, we should check if we have received the close_notify | |
2941 // alert message from the server, and return false in that case. Although | |
2942 // the close_notify alert message means EOF in the SSL layer, it is just | |
2943 // bytes to the transport layer below, so | |
2944 // transport_->socket()->IsConnectedAndIdle() returns the desired false | |
2945 // when we receive close_notify. | |
2946 EnterFunction(""); | 3050 EnterFunction(""); |
2947 bool ret = completed_handshake_ && transport_->socket()->IsConnectedAndIdle(); | 3051 bool ret = IsConnected() && |
3052 (core_->IsClosed() || !core_->HasUnhandledReceivedData()) && | |
3053 transport_->socket()->IsConnectedAndIdle(); | |
2948 LeaveFunction(""); | 3054 LeaveFunction(""); |
2949 return ret; | 3055 return ret; |
2950 } | 3056 } |
2951 | 3057 |
2952 int SSLClientSocketNSS::GetPeerAddress(IPEndPoint* address) const { | 3058 int SSLClientSocketNSS::GetPeerAddress(IPEndPoint* address) const { |
2953 return transport_->socket()->GetPeerAddress(address); | 3059 return transport_->socket()->GetPeerAddress(address); |
2954 } | 3060 } |
2955 | 3061 |
2956 int SSLClientSocketNSS::GetLocalAddress(IPEndPoint* address) const { | 3062 int SSLClientSocketNSS::GetLocalAddress(IPEndPoint* address) const { |
2957 return transport_->socket()->GetLocalAddress(address); | 3063 return transport_->socket()->GetLocalAddress(address); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3009 return base::TimeDelta::FromMicroseconds(-1); | 3115 return base::TimeDelta::FromMicroseconds(-1); |
3010 } | 3116 } |
3011 | 3117 |
3012 int SSLClientSocketNSS::Read(IOBuffer* buf, int buf_len, | 3118 int SSLClientSocketNSS::Read(IOBuffer* buf, int buf_len, |
3013 const CompletionCallback& callback) { | 3119 const CompletionCallback& callback) { |
3014 DCHECK(core_); | 3120 DCHECK(core_); |
3015 DCHECK(!callback.is_null()); | 3121 DCHECK(!callback.is_null()); |
3016 | 3122 |
3017 EnterFunction(buf_len); | 3123 EnterFunction(buf_len); |
3018 int rv = core_->Read(buf, buf_len, callback); | 3124 int rv = core_->Read(buf, buf_len, callback); |
3125 if (rv <= 0 && rv != ERR_IO_PENDING) | |
3126 ssl_is_closed_ = true; | |
3019 LeaveFunction(rv); | 3127 LeaveFunction(rv); |
3020 | 3128 |
3021 return rv; | 3129 return rv; |
3022 } | 3130 } |
3023 | 3131 |
3024 int SSLClientSocketNSS::Write(IOBuffer* buf, int buf_len, | 3132 int SSLClientSocketNSS::Write(IOBuffer* buf, int buf_len, |
3025 const CompletionCallback& callback) { | 3133 const CompletionCallback& callback) { |
3026 DCHECK(core_); | 3134 DCHECK(core_); |
3027 DCHECK(!callback.is_null()); | 3135 DCHECK(!callback.is_null()); |
3028 | 3136 |
3029 EnterFunction(buf_len); | 3137 EnterFunction(buf_len); |
3030 int rv = core_->Write(buf, buf_len, callback); | 3138 int rv = core_->Write(buf, buf_len, callback); |
3139 if (rv < 0 && rv != ERR_IO_PENDING) | |
3140 ssl_is_closed_ = true; | |
3031 LeaveFunction(rv); | 3141 LeaveFunction(rv); |
3032 | 3142 |
3033 return rv; | 3143 return rv; |
3034 } | 3144 } |
3035 | 3145 |
3036 bool SSLClientSocketNSS::SetReceiveBufferSize(int32 size) { | 3146 bool SSLClientSocketNSS::SetReceiveBufferSize(int32 size) { |
3037 return transport_->socket()->SetReceiveBufferSize(size); | 3147 return transport_->socket()->SetReceiveBufferSize(size); |
3038 } | 3148 } |
3039 | 3149 |
3040 bool SSLClientSocketNSS::SetSendBufferSize(int32 size) { | 3150 bool SSLClientSocketNSS::SetSendBufferSize(int32 size) { |
(...skipping 450 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3491 EnsureThreadIdAssigned(); | 3601 EnsureThreadIdAssigned(); |
3492 base::AutoLock auto_lock(lock_); | 3602 base::AutoLock auto_lock(lock_); |
3493 return valid_thread_id_ == base::PlatformThread::CurrentId(); | 3603 return valid_thread_id_ == base::PlatformThread::CurrentId(); |
3494 } | 3604 } |
3495 | 3605 |
3496 ServerBoundCertService* SSLClientSocketNSS::GetServerBoundCertService() const { | 3606 ServerBoundCertService* SSLClientSocketNSS::GetServerBoundCertService() const { |
3497 return server_bound_cert_service_; | 3607 return server_bound_cert_service_; |
3498 } | 3608 } |
3499 | 3609 |
3500 } // namespace net | 3610 } // namespace net |
OLD | NEW |