Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / net / socket / ssl_client_socket_openssl.h
blob14ea3c8c6331cbbfb891210cfa1339cbb4474e09
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
3 // found in the LICENSE file.
5 #ifndef NET_SOCKET_SSL_CLIENT_SOCKET_OPENSSL_H_
6 #define NET_SOCKET_SSL_CLIENT_SOCKET_OPENSSL_H_
8 #include <string>
10 #include "base/compiler_specific.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "net/base/completion_callback.h"
14 #include "net/base/io_buffer.h"
15 #include "net/cert/cert_verify_result.h"
16 #include "net/socket/client_socket_handle.h"
17 #include "net/socket/ssl_client_socket.h"
18 #include "net/ssl/channel_id_service.h"
19 #include "net/ssl/ssl_client_cert_type.h"
20 #include "net/ssl/ssl_config_service.h"
22 // Avoid including misc OpenSSL headers, i.e.:
23 // <openssl/bio.h>
24 typedef struct bio_st BIO;
25 // <openssl/evp.h>
26 typedef struct evp_pkey_st EVP_PKEY;
27 // <openssl/ssl.h>
28 typedef struct ssl_st SSL;
29 // <openssl/x509.h>
30 typedef struct x509_st X509;
31 // <openssl/ossl_type.h>
32 typedef struct x509_store_ctx_st X509_STORE_CTX;
34 namespace net {
36 class CertVerifier;
37 class SingleRequestCertVerifier;
38 class SSLCertRequestInfo;
39 class SSLInfo;
41 // An SSL client socket implemented with OpenSSL.
42 class SSLClientSocketOpenSSL : public SSLClientSocket {
43 public:
44 // Takes ownership of the transport_socket, which may already be connected.
45 // The given hostname will be compared with the name(s) in the server's
46 // certificate during the SSL handshake. ssl_config specifies the SSL
47 // settings.
48 SSLClientSocketOpenSSL(scoped_ptr<ClientSocketHandle> transport_socket,
49 const HostPortPair& host_and_port,
50 const SSLConfig& ssl_config,
51 const SSLClientSocketContext& context);
52 virtual ~SSLClientSocketOpenSSL();
54 const HostPortPair& host_and_port() const { return host_and_port_; }
55 const std::string& ssl_session_cache_shard() const {
56 return ssl_session_cache_shard_;
59 // SSLClientSocket implementation.
60 virtual std::string GetSessionCacheKey() const OVERRIDE;
61 virtual bool InSessionCache() const OVERRIDE;
62 virtual void SetHandshakeCompletionCallback(
63 const base::Closure& callback) OVERRIDE;
64 virtual void GetSSLCertRequestInfo(
65 SSLCertRequestInfo* cert_request_info) OVERRIDE;
66 virtual NextProtoStatus GetNextProto(std::string* proto) OVERRIDE;
67 virtual ChannelIDService* GetChannelIDService() const OVERRIDE;
69 // SSLSocket implementation.
70 virtual int ExportKeyingMaterial(const base::StringPiece& label,
71 bool has_context,
72 const base::StringPiece& context,
73 unsigned char* out,
74 unsigned int outlen) OVERRIDE;
75 virtual int GetTLSUniqueChannelBinding(std::string* out) OVERRIDE;
77 // StreamSocket implementation.
78 virtual int Connect(const CompletionCallback& callback) OVERRIDE;
79 virtual void Disconnect() OVERRIDE;
80 virtual bool IsConnected() const OVERRIDE;
81 virtual bool IsConnectedAndIdle() const OVERRIDE;
82 virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE;
83 virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE;
84 virtual const BoundNetLog& NetLog() const OVERRIDE;
85 virtual void SetSubresourceSpeculation() OVERRIDE;
86 virtual void SetOmniboxSpeculation() OVERRIDE;
87 virtual bool WasEverUsed() const OVERRIDE;
88 virtual bool UsingTCPFastOpen() const OVERRIDE;
89 virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE;
91 // Socket implementation.
92 virtual int Read(IOBuffer* buf, int buf_len,
93 const CompletionCallback& callback) OVERRIDE;
94 virtual int Write(IOBuffer* buf, int buf_len,
95 const CompletionCallback& callback) OVERRIDE;
96 virtual int SetReceiveBufferSize(int32 size) OVERRIDE;
97 virtual int SetSendBufferSize(int32 size) OVERRIDE;
99 protected:
100 // SSLClientSocket implementation.
101 virtual scoped_refptr<X509Certificate> GetUnverifiedServerCertificateChain()
102 const OVERRIDE;
104 private:
105 class PeerCertificateChain;
106 class SSLContext;
107 friend class SSLClientSocket;
108 friend class SSLContext;
110 int Init();
111 void DoReadCallback(int result);
112 void DoWriteCallback(int result);
114 void OnHandshakeCompletion();
116 bool DoTransportIO();
117 int DoHandshake();
118 int DoChannelIDLookup();
119 int DoChannelIDLookupComplete(int result);
120 int DoVerifyCert(int result);
121 int DoVerifyCertComplete(int result);
122 void DoConnectCallback(int result);
123 X509Certificate* UpdateServerCert();
125 void OnHandshakeIOComplete(int result);
126 void OnSendComplete(int result);
127 void OnRecvComplete(int result);
129 int DoHandshakeLoop(int last_io_result);
130 int DoReadLoop(int result);
131 int DoWriteLoop(int result);
132 int DoPayloadRead();
133 int DoPayloadWrite();
135 int BufferSend();
136 int BufferRecv();
137 void BufferSendComplete(int result);
138 void BufferRecvComplete(int result);
139 void TransportWriteComplete(int result);
140 int TransportReadComplete(int result);
142 // Callback from the SSL layer that indicates the remote server is requesting
143 // a certificate for this client.
144 int ClientCertRequestCallback(SSL* ssl);
146 // CertVerifyCallback is called to verify the server's certificates. We do
147 // verification after the handshake so this function only enforces that the
148 // certificates don't change during renegotiation.
149 int CertVerifyCallback(X509_STORE_CTX *store_ctx);
151 // Callback from the SSL layer to check which NPN protocol we are supporting
152 int SelectNextProtoCallback(unsigned char** out, unsigned char* outlen,
153 const unsigned char* in, unsigned int inlen);
155 // Called during an operation on |transport_bio_|'s peer. Checks saved
156 // transport error state and, if appropriate, returns an error through
157 // OpenSSL's error system.
158 long MaybeReplayTransportError(BIO *bio,
159 int cmd,
160 const char *argp, int argi, long argl,
161 long retvalue);
163 // Callback from the SSL layer when an operation is performed on
164 // |transport_bio_|'s peer.
165 static long BIOCallback(BIO *bio,
166 int cmd,
167 const char *argp, int argi, long argl,
168 long retvalue);
170 // Callback that is used to obtain information about the state of the SSL
171 // handshake.
172 static void InfoCallback(const SSL* ssl, int type, int val);
174 void CheckIfHandshakeFinished();
176 bool transport_send_busy_;
177 bool transport_recv_busy_;
179 scoped_refptr<DrainableIOBuffer> send_buffer_;
180 scoped_refptr<IOBuffer> recv_buffer_;
182 CompletionCallback user_connect_callback_;
183 CompletionCallback user_read_callback_;
184 CompletionCallback user_write_callback_;
186 // Used by Read function.
187 scoped_refptr<IOBuffer> user_read_buf_;
188 int user_read_buf_len_;
190 // Used by Write function.
191 scoped_refptr<IOBuffer> user_write_buf_;
192 int user_write_buf_len_;
194 // Used by DoPayloadRead() when attempting to fill the caller's buffer with
195 // as much data as possible without blocking.
196 // If DoPayloadRead() encounters an error after having read some data, stores
197 // the result to return on the *next* call to DoPayloadRead(). A value > 0
198 // indicates there is no pending result, otherwise 0 indicates EOF and < 0
199 // indicates an error.
200 int pending_read_error_;
202 // Used by TransportReadComplete() to signify an error reading from the
203 // transport socket. A value of OK indicates the socket is still
204 // readable. EOFs are mapped to ERR_CONNECTION_CLOSED.
205 int transport_read_error_;
207 // Used by TransportWriteComplete() and TransportReadComplete() to signify an
208 // error writing to the transport socket. A value of OK indicates no error.
209 int transport_write_error_;
211 // Set when Connect finishes.
212 scoped_ptr<PeerCertificateChain> server_cert_chain_;
213 scoped_refptr<X509Certificate> server_cert_;
214 CertVerifyResult server_cert_verify_result_;
215 bool completed_connect_;
217 // Set when Read() or Write() successfully reads or writes data to or from the
218 // network.
219 bool was_ever_used_;
221 // Stores client authentication information between ClientAuthHandler and
222 // GetSSLCertRequestInfo calls.
223 bool client_auth_cert_needed_;
224 // List of DER-encoded X.509 DistinguishedName of certificate authorities
225 // allowed by the server.
226 std::vector<std::string> cert_authorities_;
227 // List of SSLClientCertType values for client certificates allowed by the
228 // server.
229 std::vector<SSLClientCertType> cert_key_types_;
231 CertVerifier* const cert_verifier_;
232 scoped_ptr<SingleRequestCertVerifier> verifier_;
233 base::TimeTicks start_cert_verification_time_;
235 // The service for retrieving Channel ID keys. May be NULL.
236 ChannelIDService* channel_id_service_;
238 // Callback that is invoked when the connection finishes.
240 // Note: this callback will be run in Disconnect(). It will not alter
241 // any member variables of the SSLClientSocketOpenSSL.
242 base::Closure handshake_completion_callback_;
244 // OpenSSL stuff
245 SSL* ssl_;
246 BIO* transport_bio_;
248 scoped_ptr<ClientSocketHandle> transport_;
249 const HostPortPair host_and_port_;
250 SSLConfig ssl_config_;
251 // ssl_session_cache_shard_ is an opaque string that partitions the SSL
252 // session cache. i.e. sessions created with one value will not attempt to
253 // resume on the socket with a different value.
254 const std::string ssl_session_cache_shard_;
256 // Used for session cache diagnostics.
257 bool trying_cached_session_;
259 enum State {
260 STATE_NONE,
261 STATE_HANDSHAKE,
262 STATE_CHANNEL_ID_LOOKUP,
263 STATE_CHANNEL_ID_LOOKUP_COMPLETE,
264 STATE_VERIFY_CERT,
265 STATE_VERIFY_CERT_COMPLETE,
267 State next_handshake_state_;
268 NextProtoStatus npn_status_;
269 std::string npn_proto_;
270 // Written by the |channel_id_service_|.
271 std::string channel_id_private_key_;
272 std::string channel_id_cert_;
273 // True if channel ID extension was negotiated.
274 bool channel_id_xtn_negotiated_;
275 // True if InfoCallback has been run with result = SSL_CB_HANDSHAKE_DONE.
276 bool handshake_succeeded_;
277 // True if MarkSSLSessionAsGood has been called for this socket's
278 // SSL session.
279 bool marked_session_as_good_;
280 // The request handle for |channel_id_service_|.
281 ChannelIDService::RequestHandle channel_id_request_handle_;
283 TransportSecurityState* transport_security_state_;
285 // pinning_failure_log contains a message produced by
286 // TransportSecurityState::CheckPublicKeyPins in the event of a
287 // pinning failure. It is a (somewhat) human-readable string.
288 std::string pinning_failure_log_;
290 BoundNetLog net_log_;
291 base::WeakPtrFactory<SSLClientSocketOpenSSL> weak_factory_;
294 } // namespace net
296 #endif // NET_SOCKET_SSL_CLIENT_SOCKET_OPENSSL_H_