Show Pages in chrome://md-settings
[chromium-blink-merge.git] / net / socket / ssl_client_socket_openssl.h
blobf19813ed05667bcf3a0d4037e46fd892767fbcd6
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 void GetSSLCertRequestInfo(SSLCertRequestInfo* cert_request_info) override;
64 NextProtoStatus GetNextProto(std::string* proto) override;
65 ChannelIDService* GetChannelIDService() const override;
67 // SSLSocket implementation.
68 int ExportKeyingMaterial(const base::StringPiece& label,
69 bool has_context,
70 const base::StringPiece& context,
71 unsigned char* out,
72 unsigned int outlen) override;
73 int GetTLSUniqueChannelBinding(std::string* out) override;
75 // StreamSocket implementation.
76 int Connect(const CompletionCallback& callback) override;
77 void Disconnect() override;
78 bool IsConnected() const override;
79 bool IsConnectedAndIdle() const override;
80 int GetPeerAddress(IPEndPoint* address) const override;
81 int GetLocalAddress(IPEndPoint* address) const override;
82 const BoundNetLog& NetLog() const override;
83 void SetSubresourceSpeculation() override;
84 void SetOmniboxSpeculation() override;
85 bool WasEverUsed() const override;
86 bool UsingTCPFastOpen() const override;
87 bool GetSSLInfo(SSLInfo* ssl_info) override;
89 // Socket implementation.
90 int Read(IOBuffer* buf,
91 int buf_len,
92 const CompletionCallback& callback) override;
93 int Write(IOBuffer* buf,
94 int buf_len,
95 const CompletionCallback& callback) override;
96 int SetReceiveBufferSize(int32 size) override;
97 int SetSendBufferSize(int32 size) override;
99 protected:
100 // SSLClientSocket implementation.
101 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 bool DoTransportIO();
115 int DoHandshake();
116 int DoChannelIDLookup();
117 int DoChannelIDLookupComplete(int result);
118 int DoVerifyCert(int result);
119 int DoVerifyCertComplete(int result);
120 void DoConnectCallback(int result);
121 void UpdateServerCert();
122 void VerifyCT();
124 void OnHandshakeIOComplete(int result);
125 void OnSendComplete(int result);
126 void OnRecvComplete(int result);
128 int DoHandshakeLoop(int last_io_result);
129 int DoReadLoop();
130 int DoWriteLoop();
131 int DoPayloadRead();
132 int DoPayloadWrite();
134 int BufferSend();
135 int BufferRecv();
136 void BufferSendComplete(int result);
137 void BufferRecvComplete(int result);
138 void TransportWriteComplete(int result);
139 int TransportReadComplete(int result);
141 // Callback from the SSL layer that indicates the remote server is requesting
142 // a certificate for this client.
143 int ClientCertRequestCallback(SSL* ssl);
145 // CertVerifyCallback is called to verify the server's certificates. We do
146 // verification after the handshake so this function only enforces that the
147 // certificates don't change during renegotiation.
148 int CertVerifyCallback(X509_STORE_CTX *store_ctx);
150 // Callback from the SSL layer to check which NPN protocol we are supporting
151 int SelectNextProtoCallback(unsigned char** out, unsigned char* outlen,
152 const unsigned char* in, unsigned int inlen);
154 // Called during an operation on |transport_bio_|'s peer. Checks saved
155 // transport error state and, if appropriate, returns an error through
156 // OpenSSL's error system.
157 long MaybeReplayTransportError(BIO *bio,
158 int cmd,
159 const char *argp, int argi, long argl,
160 long retvalue);
162 // Callback from the SSL layer when an operation is performed on
163 // |transport_bio_|'s peer.
164 static long BIOCallback(BIO *bio,
165 int cmd,
166 const char *argp, int argi, long argl,
167 long retvalue);
169 // Adds the SignedCertificateTimestamps from ct_verify_result_ to |ssl_info|.
170 // SCTs are held in three separate vectors in ct_verify_result, each
171 // vetor representing a particular verification state, this method associates
172 // each of the SCTs with the corresponding SCTVerifyStatus as it adds it to
173 // the |ssl_info|.signed_certificate_timestamps list.
174 void AddSCTInfoToSSLInfo(SSLInfo* ssl_info) const;
176 // Returns a unique key string for the SSL session cache for
177 // this socket.
178 std::string GetSessionCacheKey() const;
180 bool transport_send_busy_;
181 bool transport_recv_busy_;
183 // Buffers which are shared by BoringSSL and SSLClientSocketOpenSSL.
184 // GrowableIOBuffer is used to keep ownership and setting offset.
185 scoped_refptr<GrowableIOBuffer> send_buffer_;
186 scoped_refptr<GrowableIOBuffer> recv_buffer_;
188 CompletionCallback user_connect_callback_;
189 CompletionCallback user_read_callback_;
190 CompletionCallback user_write_callback_;
192 // Used by Read function.
193 scoped_refptr<IOBuffer> user_read_buf_;
194 int user_read_buf_len_;
196 // Used by Write function.
197 scoped_refptr<IOBuffer> user_write_buf_;
198 int user_write_buf_len_;
200 // Used by DoPayloadRead() when attempting to fill the caller's buffer with
201 // as much data as possible without blocking.
202 // If DoPayloadRead() encounters an error after having read some data, stores
203 // the result to return on the *next* call to DoPayloadRead(). A value > 0
204 // indicates there is no pending result, otherwise 0 indicates EOF and < 0
205 // indicates an error.
206 int pending_read_error_;
208 // If there is a pending read result, the OpenSSL result code (output of
209 // SSL_get_error) associated with it.
210 int pending_read_ssl_error_;
212 // If there is a pending read result, the OpenSSLErrorInfo associated with it.
213 OpenSSLErrorInfo pending_read_error_info_;
215 // Used by TransportReadComplete() to signify an error reading from the
216 // transport socket. A value of OK indicates the socket is still
217 // readable. EOFs are mapped to ERR_CONNECTION_CLOSED.
218 int transport_read_error_;
220 // Used by TransportWriteComplete() and TransportReadComplete() to signify an
221 // error writing to the transport socket. A value of OK indicates no error.
222 int transport_write_error_;
224 // Set when Connect finishes.
225 scoped_ptr<PeerCertificateChain> server_cert_chain_;
226 scoped_refptr<X509Certificate> server_cert_;
227 CertVerifyResult server_cert_verify_result_;
228 bool completed_connect_;
230 // Set when Read() or Write() successfully reads or writes data to or from the
231 // network.
232 bool was_ever_used_;
234 // Stores client authentication information between ClientAuthHandler and
235 // GetSSLCertRequestInfo calls.
236 bool client_auth_cert_needed_;
237 // List of DER-encoded X.509 DistinguishedName of certificate authorities
238 // allowed by the server.
239 std::vector<std::string> cert_authorities_;
240 // List of SSLClientCertType values for client certificates allowed by the
241 // server.
242 std::vector<SSLClientCertType> cert_key_types_;
244 CertVerifier* const cert_verifier_;
245 scoped_ptr<SingleRequestCertVerifier> verifier_;
246 base::TimeTicks start_cert_verification_time_;
248 // Certificate Transparency: Verifier and result holder.
249 ct::CTVerifyResult ct_verify_result_;
250 CTVerifier* cert_transparency_verifier_;
252 // The service for retrieving Channel ID keys. May be NULL.
253 ChannelIDService* channel_id_service_;
255 // OpenSSL stuff
256 SSL* ssl_;
257 BIO* transport_bio_;
259 scoped_ptr<ClientSocketHandle> transport_;
260 const HostPortPair host_and_port_;
261 SSLConfig ssl_config_;
262 // ssl_session_cache_shard_ is an opaque string that partitions the SSL
263 // session cache. i.e. sessions created with one value will not attempt to
264 // resume on the socket with a different value.
265 const std::string ssl_session_cache_shard_;
267 // Used for session cache diagnostics.
268 bool trying_cached_session_;
270 enum State {
271 STATE_NONE,
272 STATE_HANDSHAKE,
273 STATE_CHANNEL_ID_LOOKUP,
274 STATE_CHANNEL_ID_LOOKUP_COMPLETE,
275 STATE_VERIFY_CERT,
276 STATE_VERIFY_CERT_COMPLETE,
278 State next_handshake_state_;
279 NextProtoStatus npn_status_;
280 std::string npn_proto_;
281 // Written by the |channel_id_service_|.
282 std::string channel_id_private_key_;
283 std::string channel_id_cert_;
284 // True if channel ID extension was negotiated.
285 bool channel_id_xtn_negotiated_;
286 // The request handle for |channel_id_service_|.
287 ChannelIDService::RequestHandle channel_id_request_handle_;
289 TransportSecurityState* transport_security_state_;
291 CertPolicyEnforcer* const policy_enforcer_;
293 // pinning_failure_log contains a message produced by
294 // TransportSecurityState::CheckPublicKeyPins in the event of a
295 // pinning failure. It is a (somewhat) human-readable string.
296 std::string pinning_failure_log_;
298 BoundNetLog net_log_;
299 base::WeakPtrFactory<SSLClientSocketOpenSSL> weak_factory_;
302 } // namespace net
304 #endif // NET_SOCKET_SSL_CLIENT_SOCKET_OPENSSL_H_