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_
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/server_bound_cert_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.:
24 typedef struct bio_st BIO
;
26 typedef struct evp_pkey_st EVP_PKEY
;
28 typedef struct ssl_st SSL
;
30 typedef struct x509_st X509
;
31 // <openssl/ossl_type.h>
32 typedef struct x509_store_ctx_st X509_STORE_CTX
;
37 class SingleRequestCertVerifier
;
38 class SSLCertRequestInfo
;
41 // An SSL client socket implemented with OpenSSL.
42 class SSLClientSocketOpenSSL
: public SSLClientSocket
{
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
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 void GetSSLCertRequestInfo(
61 SSLCertRequestInfo
* cert_request_info
) OVERRIDE
;
62 virtual NextProtoStatus
GetNextProto(std::string
* proto
,
63 std::string
* server_protos
) OVERRIDE
;
64 virtual ServerBoundCertService
* GetServerBoundCertService() const OVERRIDE
;
66 // SSLSocket implementation.
67 virtual int ExportKeyingMaterial(const base::StringPiece
& label
,
69 const base::StringPiece
& context
,
71 unsigned int outlen
) OVERRIDE
;
72 virtual int GetTLSUniqueChannelBinding(std::string
* out
) OVERRIDE
;
74 // StreamSocket implementation.
75 virtual int Connect(const CompletionCallback
& callback
) OVERRIDE
;
76 virtual void Disconnect() OVERRIDE
;
77 virtual bool IsConnected() const OVERRIDE
;
78 virtual bool IsConnectedAndIdle() const OVERRIDE
;
79 virtual int GetPeerAddress(IPEndPoint
* address
) const OVERRIDE
;
80 virtual int GetLocalAddress(IPEndPoint
* address
) const OVERRIDE
;
81 virtual const BoundNetLog
& NetLog() const OVERRIDE
;
82 virtual void SetSubresourceSpeculation() OVERRIDE
;
83 virtual void SetOmniboxSpeculation() OVERRIDE
;
84 virtual bool WasEverUsed() const OVERRIDE
;
85 virtual bool UsingTCPFastOpen() const OVERRIDE
;
86 virtual bool GetSSLInfo(SSLInfo
* ssl_info
) OVERRIDE
;
88 // Socket implementation.
89 virtual int Read(IOBuffer
* buf
, int buf_len
,
90 const CompletionCallback
& callback
) OVERRIDE
;
91 virtual int Write(IOBuffer
* buf
, int buf_len
,
92 const CompletionCallback
& callback
) OVERRIDE
;
93 virtual int SetReceiveBufferSize(int32 size
) OVERRIDE
;
94 virtual int SetSendBufferSize(int32 size
) OVERRIDE
;
97 // SSLClientSocket implementation.
98 virtual scoped_refptr
<X509Certificate
> GetUnverifiedServerCertificateChain()
102 class PeerCertificateChain
;
104 friend class SSLClientSocket
;
105 friend class SSLContext
;
108 void DoReadCallback(int result
);
109 void DoWriteCallback(int result
);
111 bool DoTransportIO();
113 int DoChannelIDLookup();
114 int DoChannelIDLookupComplete(int result
);
115 int DoVerifyCert(int result
);
116 int DoVerifyCertComplete(int result
);
117 void DoConnectCallback(int result
);
118 X509Certificate
* UpdateServerCert();
120 void OnHandshakeIOComplete(int result
);
121 void OnSendComplete(int result
);
122 void OnRecvComplete(int result
);
124 int DoHandshakeLoop(int last_io_result
);
125 int DoReadLoop(int result
);
126 int DoWriteLoop(int result
);
128 int DoPayloadWrite();
132 void BufferSendComplete(int result
);
133 void BufferRecvComplete(int result
);
134 void TransportWriteComplete(int result
);
135 int TransportReadComplete(int result
);
137 // Callback from the SSL layer that indicates the remote server is requesting
138 // a certificate for this client.
139 int ClientCertRequestCallback(SSL
* ssl
, X509
** x509
, EVP_PKEY
** pkey
);
141 // CertVerifyCallback is called to verify the server's certificates. We do
142 // verification after the handshake so this function only enforces that the
143 // certificates don't change during renegotiation.
144 int CertVerifyCallback(X509_STORE_CTX
*store_ctx
);
146 // Callback from the SSL layer to check which NPN protocol we are supporting
147 int SelectNextProtoCallback(unsigned char** out
, unsigned char* outlen
,
148 const unsigned char* in
, unsigned int inlen
);
150 bool transport_send_busy_
;
151 bool transport_recv_busy_
;
152 bool transport_recv_eof_
;
154 scoped_refptr
<DrainableIOBuffer
> send_buffer_
;
155 scoped_refptr
<IOBuffer
> recv_buffer_
;
157 CompletionCallback user_connect_callback_
;
158 CompletionCallback user_read_callback_
;
159 CompletionCallback user_write_callback_
;
161 base::WeakPtrFactory
<SSLClientSocketOpenSSL
> weak_factory_
;
163 // Used by Read function.
164 scoped_refptr
<IOBuffer
> user_read_buf_
;
165 int user_read_buf_len_
;
167 // Used by Write function.
168 scoped_refptr
<IOBuffer
> user_write_buf_
;
169 int user_write_buf_len_
;
171 // Used by DoPayloadRead() when attempting to fill the caller's buffer with
172 // as much data as possible without blocking.
173 // If DoPayloadRead() encounters an error after having read some data, stores
174 // the result to return on the *next* call to DoPayloadRead(). A value > 0
175 // indicates there is no pending result, otherwise 0 indicates EOF and < 0
176 // indicates an error.
177 int pending_read_error_
;
179 // Used by TransportWriteComplete() and TransportReadComplete() to signify an
180 // error writing to the transport socket. A value of OK indicates no error.
181 int transport_write_error_
;
183 // Set when handshake finishes.
184 scoped_ptr
<PeerCertificateChain
> server_cert_chain_
;
185 scoped_refptr
<X509Certificate
> server_cert_
;
186 CertVerifyResult server_cert_verify_result_
;
187 bool completed_handshake_
;
189 // Set when Read() or Write() successfully reads or writes data to or from the
193 // Stores client authentication information between ClientAuthHandler and
194 // GetSSLCertRequestInfo calls.
195 bool client_auth_cert_needed_
;
196 // List of DER-encoded X.509 DistinguishedName of certificate authorities
197 // allowed by the server.
198 std::vector
<std::string
> cert_authorities_
;
199 // List of SSLClientCertType values for client certificates allowed by the
201 std::vector
<SSLClientCertType
> cert_key_types_
;
203 CertVerifier
* const cert_verifier_
;
204 scoped_ptr
<SingleRequestCertVerifier
> verifier_
;
206 // The service for retrieving Channel ID keys. May be NULL.
207 ServerBoundCertService
* server_bound_cert_service_
;
213 scoped_ptr
<ClientSocketHandle
> transport_
;
214 const HostPortPair host_and_port_
;
215 SSLConfig ssl_config_
;
216 // ssl_session_cache_shard_ is an opaque string that partitions the SSL
217 // session cache. i.e. sessions created with one value will not attempt to
218 // resume on the socket with a different value.
219 const std::string ssl_session_cache_shard_
;
221 // Used for session cache diagnostics.
222 bool trying_cached_session_
;
227 STATE_CHANNEL_ID_LOOKUP
,
228 STATE_CHANNEL_ID_LOOKUP_COMPLETE
,
230 STATE_VERIFY_CERT_COMPLETE
,
232 State next_handshake_state_
;
233 NextProtoStatus npn_status_
;
234 std::string npn_proto_
;
235 std::string server_protos_
;
236 // Written by the |server_bound_cert_service_|.
237 std::string channel_id_private_key_
;
238 std::string channel_id_cert_
;
239 // True if channel ID extension was negotiated.
240 bool channel_id_xtn_negotiated_
;
241 // The request handle for |server_bound_cert_service_|.
242 ServerBoundCertService::RequestHandle channel_id_request_handle_
;
243 BoundNetLog net_log_
;
248 #endif // NET_SOCKET_SSL_CLIENT_SOCKET_OPENSSL_H_