| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef NET_BASE_SSL_CLIENT_SOCKET_WIN_H_ | |
| 6 #define NET_BASE_SSL_CLIENT_SOCKET_WIN_H_ | |
| 7 | |
| 8 #define SECURITY_WIN32 // Needs to be defined before including security.h | |
| 9 | |
| 10 #include <windows.h> | |
| 11 #include <wincrypt.h> | |
| 12 #include <security.h> | |
| 13 | |
| 14 #include <string> | |
| 15 | |
| 16 #include "base/scoped_ptr.h" | |
| 17 #include "net/base/cert_verifier.h" | |
| 18 #include "net/base/cert_verify_result.h" | |
| 19 #include "net/base/completion_callback.h" | |
| 20 #include "net/base/ssl_client_socket.h" | |
| 21 #include "net/base/ssl_config_service.h" | |
| 22 | |
| 23 namespace net { | |
| 24 | |
| 25 // An SSL client socket implemented with the Windows Schannel. | |
| 26 class SSLClientSocketWin : public SSLClientSocket { | |
| 27 public: | |
| 28 // Takes ownership of the transport_socket, which may already be connected. | |
| 29 // The given hostname will be compared with the name(s) in the server's | |
| 30 // certificate during the SSL handshake. ssl_config specifies the SSL | |
| 31 // settings. | |
| 32 SSLClientSocketWin(ClientSocket* transport_socket, | |
| 33 const std::string& hostname, | |
| 34 const SSLConfig& ssl_config); | |
| 35 ~SSLClientSocketWin(); | |
| 36 | |
| 37 // SSLClientSocket methods: | |
| 38 virtual void GetSSLInfo(SSLInfo* ssl_info); | |
| 39 virtual void GetSSLCertRequestInfo(SSLCertRequestInfo* cert_request_info); | |
| 40 | |
| 41 // ClientSocket methods: | |
| 42 virtual int Connect(CompletionCallback* callback); | |
| 43 virtual void Disconnect(); | |
| 44 virtual bool IsConnected() const; | |
| 45 virtual bool IsConnectedAndIdle() const; | |
| 46 | |
| 47 // Socket methods: | |
| 48 virtual int Read(IOBuffer* buf, int buf_len, CompletionCallback* callback); | |
| 49 virtual int Write(IOBuffer* buf, int buf_len, CompletionCallback* callback); | |
| 50 | |
| 51 private: | |
| 52 void DoCallback(int result); | |
| 53 void OnIOComplete(int result); | |
| 54 | |
| 55 int DoLoop(int last_io_result); | |
| 56 int DoHandshakeRead(); | |
| 57 int DoHandshakeReadComplete(int result); | |
| 58 int DoHandshakeWrite(); | |
| 59 int DoHandshakeWriteComplete(int result); | |
| 60 int DoVerifyCert(); | |
| 61 int DoVerifyCertComplete(int result); | |
| 62 int DoPayloadRead(); | |
| 63 int DoPayloadReadComplete(int result); | |
| 64 int DoPayloadEncrypt(); | |
| 65 int DoPayloadWrite(); | |
| 66 int DoPayloadWriteComplete(int result); | |
| 67 | |
| 68 int DidCallInitializeSecurityContext(); | |
| 69 int DidCompleteHandshake(); | |
| 70 void DidCompleteRenegotiation(int result); | |
| 71 void LogConnectionTypeMetrics() const; | |
| 72 void SetNextStateForRead(); | |
| 73 void FreeSendBuffer(); | |
| 74 | |
| 75 CompletionCallbackImpl<SSLClientSocketWin> io_callback_; | |
| 76 scoped_ptr<ClientSocket> transport_; | |
| 77 std::string hostname_; | |
| 78 SSLConfig ssl_config_; | |
| 79 | |
| 80 CompletionCallback* user_callback_; | |
| 81 | |
| 82 // Used by both Read and Write functions. | |
| 83 scoped_refptr<IOBuffer> user_buf_; | |
| 84 int user_buf_len_; | |
| 85 | |
| 86 // Used to Read and Write using transport_. | |
| 87 scoped_refptr<IOBuffer> transport_buf_; | |
| 88 | |
| 89 enum State { | |
| 90 STATE_NONE, | |
| 91 STATE_HANDSHAKE_READ, | |
| 92 STATE_HANDSHAKE_READ_COMPLETE, | |
| 93 STATE_HANDSHAKE_WRITE, | |
| 94 STATE_HANDSHAKE_WRITE_COMPLETE, | |
| 95 STATE_VERIFY_CERT, | |
| 96 STATE_VERIFY_CERT_COMPLETE, | |
| 97 STATE_PAYLOAD_ENCRYPT, | |
| 98 STATE_PAYLOAD_WRITE, | |
| 99 STATE_PAYLOAD_WRITE_COMPLETE, | |
| 100 STATE_PAYLOAD_READ, | |
| 101 STATE_PAYLOAD_READ_COMPLETE, | |
| 102 }; | |
| 103 State next_state_; | |
| 104 | |
| 105 SecPkgContext_StreamSizes stream_sizes_; | |
| 106 scoped_refptr<X509Certificate> server_cert_; | |
| 107 CertVerifier verifier_; | |
| 108 CertVerifyResult server_cert_verify_result_; | |
| 109 | |
| 110 CredHandle* creds_; | |
| 111 CtxtHandle ctxt_; | |
| 112 SecBuffer in_buffers_[2]; // Input buffers for InitializeSecurityContext. | |
| 113 SecBuffer send_buffer_; // Output buffer for InitializeSecurityContext. | |
| 114 SECURITY_STATUS isc_status_; // Return value of InitializeSecurityContext. | |
| 115 scoped_array<char> payload_send_buffer_; | |
| 116 int payload_send_buffer_len_; | |
| 117 int bytes_sent_; | |
| 118 | |
| 119 // recv_buffer_ holds the received ciphertext. Since Schannel decrypts | |
| 120 // data in place, sometimes recv_buffer_ may contain decrypted plaintext and | |
| 121 // any undecrypted ciphertext. (Ciphertext is decrypted one full SSL record | |
| 122 // at a time.) | |
| 123 // | |
| 124 // If bytes_decrypted_ is 0, the received ciphertext is at the beginning of | |
| 125 // recv_buffer_, ready to be passed to DecryptMessage. | |
| 126 scoped_array<char> recv_buffer_; | |
| 127 char* decrypted_ptr_; // Points to the decrypted plaintext in recv_buffer_ | |
| 128 int bytes_decrypted_; // The number of bytes of decrypted plaintext. | |
| 129 char* received_ptr_; // Points to the received ciphertext in recv_buffer_ | |
| 130 int bytes_received_; // The number of bytes of received ciphertext. | |
| 131 | |
| 132 // True if we're writing the first token (handshake message) to the server, | |
| 133 // false if we're writing a subsequent token. After we have written a token | |
| 134 // successfully, DoHandshakeWriteComplete checks this member to set the next | |
| 135 // state. | |
| 136 bool writing_first_token_; | |
| 137 | |
| 138 bool completed_handshake_; | |
| 139 | |
| 140 // Only used in the STATE_HANDSHAKE_READ_COMPLETE and | |
| 141 // STATE_PAYLOAD_READ_COMPLETE states. True if a 'result' argument of OK | |
| 142 // should be ignored, to prevent it from being interpreted as EOF. | |
| 143 // | |
| 144 // The reason we need this flag is that OK means not only "0 bytes of data | |
| 145 // were read" but also EOF. We set ignore_ok_result_ to true when we need | |
| 146 // to continue processing previously read data without reading more data. | |
| 147 // We have to pass a 'result' of OK to the DoLoop method, and don't want it | |
| 148 // to be interpreted as EOF. | |
| 149 bool ignore_ok_result_; | |
| 150 | |
| 151 // Renegotiation is in progress. | |
| 152 bool renegotiating_; | |
| 153 }; | |
| 154 | |
| 155 } // namespace net | |
| 156 | |
| 157 #endif // NET_BASE_SSL_CLIENT_SOCKET_WIN_H_ | |
| OLD | NEW |