Fix build.
[chromium-blink-merge.git] / net / socket / ssl_client_socket_openssl.h
blob53d33c4c8c7c8bb850efb71b645a7ff6e47e3e5a
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/cert/ct_verify_result.h"
17 #include "net/socket/client_socket_handle.h"
18 #include "net/socket/ssl_client_socket.h"
19 #include "net/ssl/channel_id_service.h"
20 #include "net/ssl/openssl_ssl_util.h"
21 #include "net/ssl/ssl_client_cert_type.h"
22 #include "net/ssl/ssl_config_service.h"
24 // Avoid including misc OpenSSL headers, i.e.:
25 // <openssl/bio.h>
26 typedef struct bio_st BIO;
27 // <openssl/evp.h>
28 typedef struct evp_pkey_st EVP_PKEY;
29 // <openssl/ssl.h>
30 typedef struct ssl_st SSL;
31 // <openssl/x509.h>
32 typedef struct x509_st X509;
33 // <openssl/ossl_type.h>
34 typedef struct x509_store_ctx_st X509_STORE_CTX;
36 namespace net {
38 class CertVerifier;
39 class CTVerifier;
40 class SingleRequestCertVerifier;
41 class SSLCertRequestInfo;
42 class SSLInfo;
44 // An SSL client socket implemented with OpenSSL.
45 class SSLClientSocketOpenSSL : public SSLClientSocket {
46 public:
47 // Takes ownership of the transport_socket, which may already be connected.
48 // The given hostname will be compared with the name(s) in the server's
49 // certificate during the SSL handshake. ssl_config specifies the SSL
50 // settings.
51 SSLClientSocketOpenSSL(scoped_ptr<ClientSocketHandle> transport_socket,
52 const HostPortPair& host_and_port,
53 const SSLConfig& ssl_config,
54 const SSLClientSocketContext& context);
55 ~SSLClientSocketOpenSSL() override;
57 const HostPortPair& host_and_port() const { return host_and_port_; }
58 const std::string& ssl_session_cache_shard() const {
59 return ssl_session_cache_shard_;
62 // SSLClientSocket implementation.
63 std::string GetSessionCacheKey() const override;
64 bool InSessionCache() const override;
65 void SetHandshakeCompletionCallback(const base::Closure& callback) override;
66 void GetSSLCertRequestInfo(SSLCertRequestInfo* cert_request_info) override;
67 NextProtoStatus GetNextProto(std::string* proto) override;
68 ChannelIDService* GetChannelIDService() const override;
70 // SSLSocket implementation.
71 int ExportKeyingMaterial(const base::StringPiece& label,
72 bool has_context,
73 const base::StringPiece& context,
74 unsigned char* out,
75 unsigned int outlen) override;
76 int GetTLSUniqueChannelBinding(std::string* out) override;
78 // StreamSocket implementation.
79 int Connect(const CompletionCallback& callback) override;
80 void Disconnect() override;
81 bool IsConnected() const override;
82 bool IsConnectedAndIdle() const override;
83 int GetPeerAddress(IPEndPoint* address) const override;
84 int GetLocalAddress(IPEndPoint* address) const override;
85 const BoundNetLog& NetLog() const override;
86 void SetSubresourceSpeculation() override;
87 void SetOmniboxSpeculation() override;
88 bool WasEverUsed() const override;
89 bool UsingTCPFastOpen() const override;
90 bool GetSSLInfo(SSLInfo* ssl_info) override;
92 // Socket implementation.
93 int Read(IOBuffer* buf,
94 int buf_len,
95 const CompletionCallback& callback) override;
96 int Write(IOBuffer* buf,
97 int buf_len,
98 const CompletionCallback& callback) override;
99 int SetReceiveBufferSize(int32 size) override;
100 int SetSendBufferSize(int32 size) override;
102 protected:
103 // SSLClientSocket implementation.
104 scoped_refptr<X509Certificate> GetUnverifiedServerCertificateChain()
105 const override;
107 private:
108 class PeerCertificateChain;
109 class SSLContext;
110 friend class SSLClientSocket;
111 friend class SSLContext;
113 int Init();
114 void DoReadCallback(int result);
115 void DoWriteCallback(int result);
117 void OnHandshakeCompletion();
119 bool DoTransportIO();
120 int DoHandshake();
121 int DoChannelIDLookup();
122 int DoChannelIDLookupComplete(int result);
123 int DoVerifyCert(int result);
124 int DoVerifyCertComplete(int result);
125 void DoConnectCallback(int result);
126 void UpdateServerCert();
127 void VerifyCT();
129 void OnHandshakeIOComplete(int result);
130 void OnSendComplete(int result);
131 void OnRecvComplete(int result);
133 int DoHandshakeLoop(int last_io_result);
134 int DoReadLoop();
135 int DoWriteLoop();
136 int DoPayloadRead();
137 int DoPayloadWrite();
139 int BufferSend();
140 int BufferRecv();
141 void BufferSendComplete(int result);
142 void BufferRecvComplete(int result);
143 void TransportWriteComplete(int result);
144 int TransportReadComplete(int result);
146 // Callback from the SSL layer that indicates the remote server is requesting
147 // a certificate for this client.
148 int ClientCertRequestCallback(SSL* ssl);
150 // CertVerifyCallback is called to verify the server's certificates. We do
151 // verification after the handshake so this function only enforces that the
152 // certificates don't change during renegotiation.
153 int CertVerifyCallback(X509_STORE_CTX *store_ctx);
155 // Callback from the SSL layer to check which NPN protocol we are supporting
156 int SelectNextProtoCallback(unsigned char** out, unsigned char* outlen,
157 const unsigned char* in, unsigned int inlen);
159 // Called during an operation on |transport_bio_|'s peer. Checks saved
160 // transport error state and, if appropriate, returns an error through
161 // OpenSSL's error system.
162 long MaybeReplayTransportError(BIO *bio,
163 int cmd,
164 const char *argp, int argi, long argl,
165 long retvalue);
167 // Callback from the SSL layer when an operation is performed on
168 // |transport_bio_|'s peer.
169 static long BIOCallback(BIO *bio,
170 int cmd,
171 const char *argp, int argi, long argl,
172 long retvalue);
174 // Callback that is used to obtain information about the state of the SSL
175 // handshake.
176 static void InfoCallback(const SSL* ssl, int type, int val);
178 void CheckIfHandshakeFinished();
180 // Adds the SignedCertificateTimestamps from ct_verify_result_ to |ssl_info|.
181 // SCTs are held in three separate vectors in ct_verify_result, each
182 // vetor representing a particular verification state, this method associates
183 // each of the SCTs with the corresponding SCTVerifyStatus as it adds it to
184 // the |ssl_info|.signed_certificate_timestamps list.
185 void AddSCTInfoToSSLInfo(SSLInfo* ssl_info) const;
187 bool transport_send_busy_;
188 bool transport_recv_busy_;
190 scoped_refptr<DrainableIOBuffer> send_buffer_;
191 scoped_refptr<IOBuffer> recv_buffer_;
193 CompletionCallback user_connect_callback_;
194 CompletionCallback user_read_callback_;
195 CompletionCallback user_write_callback_;
197 // Used by Read function.
198 scoped_refptr<IOBuffer> user_read_buf_;
199 int user_read_buf_len_;
201 // Used by Write function.
202 scoped_refptr<IOBuffer> user_write_buf_;
203 int user_write_buf_len_;
205 // Used by DoPayloadRead() when attempting to fill the caller's buffer with
206 // as much data as possible without blocking.
207 // If DoPayloadRead() encounters an error after having read some data, stores
208 // the result to return on the *next* call to DoPayloadRead(). A value > 0
209 // indicates there is no pending result, otherwise 0 indicates EOF and < 0
210 // indicates an error.
211 int pending_read_error_;
213 // If there is a pending read result, the OpenSSL result code (output of
214 // SSL_get_error) associated with it.
215 int pending_read_ssl_error_;
217 // If there is a pending read result, the OpenSSLErrorInfo associated with it.
218 OpenSSLErrorInfo pending_read_error_info_;
220 // Used by TransportReadComplete() to signify an error reading from the
221 // transport socket. A value of OK indicates the socket is still
222 // readable. EOFs are mapped to ERR_CONNECTION_CLOSED.
223 int transport_read_error_;
225 // Used by TransportWriteComplete() and TransportReadComplete() to signify an
226 // error writing to the transport socket. A value of OK indicates no error.
227 int transport_write_error_;
229 // Set when Connect finishes.
230 scoped_ptr<PeerCertificateChain> server_cert_chain_;
231 scoped_refptr<X509Certificate> server_cert_;
232 CertVerifyResult server_cert_verify_result_;
233 bool completed_connect_;
235 // Set when Read() or Write() successfully reads or writes data to or from the
236 // network.
237 bool was_ever_used_;
239 // Stores client authentication information between ClientAuthHandler and
240 // GetSSLCertRequestInfo calls.
241 bool client_auth_cert_needed_;
242 // List of DER-encoded X.509 DistinguishedName of certificate authorities
243 // allowed by the server.
244 std::vector<std::string> cert_authorities_;
245 // List of SSLClientCertType values for client certificates allowed by the
246 // server.
247 std::vector<SSLClientCertType> cert_key_types_;
249 CertVerifier* const cert_verifier_;
250 scoped_ptr<SingleRequestCertVerifier> verifier_;
251 base::TimeTicks start_cert_verification_time_;
253 // Certificate Transparency: Verifier and result holder.
254 ct::CTVerifyResult ct_verify_result_;
255 CTVerifier* cert_transparency_verifier_;
257 // The service for retrieving Channel ID keys. May be NULL.
258 ChannelIDService* channel_id_service_;
260 // Callback that is invoked when the connection finishes.
262 // Note: this callback will be run in Disconnect(). It will not alter
263 // any member variables of the SSLClientSocketOpenSSL.
264 base::Closure handshake_completion_callback_;
266 // OpenSSL stuff
267 SSL* ssl_;
268 BIO* transport_bio_;
270 scoped_ptr<ClientSocketHandle> transport_;
271 const HostPortPair host_and_port_;
272 SSLConfig ssl_config_;
273 // ssl_session_cache_shard_ is an opaque string that partitions the SSL
274 // session cache. i.e. sessions created with one value will not attempt to
275 // resume on the socket with a different value.
276 const std::string ssl_session_cache_shard_;
278 // Used for session cache diagnostics.
279 bool trying_cached_session_;
281 enum State {
282 STATE_NONE,
283 STATE_HANDSHAKE,
284 STATE_CHANNEL_ID_LOOKUP,
285 STATE_CHANNEL_ID_LOOKUP_COMPLETE,
286 STATE_VERIFY_CERT,
287 STATE_VERIFY_CERT_COMPLETE,
289 State next_handshake_state_;
290 NextProtoStatus npn_status_;
291 std::string npn_proto_;
292 // Written by the |channel_id_service_|.
293 std::string channel_id_private_key_;
294 std::string channel_id_cert_;
295 // True if channel ID extension was negotiated.
296 bool channel_id_xtn_negotiated_;
297 // True if InfoCallback has been run with result = SSL_CB_HANDSHAKE_DONE.
298 bool handshake_succeeded_;
299 // True if MarkSSLSessionAsGood has been called for this socket's
300 // SSL session.
301 bool marked_session_as_good_;
302 // The request handle for |channel_id_service_|.
303 ChannelIDService::RequestHandle channel_id_request_handle_;
305 TransportSecurityState* transport_security_state_;
307 // pinning_failure_log contains a message produced by
308 // TransportSecurityState::CheckPublicKeyPins in the event of a
309 // pinning failure. It is a (somewhat) human-readable string.
310 std::string pinning_failure_log_;
312 BoundNetLog net_log_;
313 base::WeakPtrFactory<SSLClientSocketOpenSSL> weak_factory_;
316 } // namespace net
318 #endif // NET_SOCKET_SSL_CLIENT_SOCKET_OPENSSL_H_