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_
11 #include "base/compiler_specific.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "net/base/cert_verify_result.h"
14 #include "net/base/completion_callback.h"
15 #include "net/base/io_buffer.h"
16 #include "net/base/ssl_config_service.h"
17 #include "net/socket/ssl_client_socket.h"
18 #include "net/socket/client_socket_handle.h"
20 typedef struct bio_st BIO
;
21 typedef struct evp_pkey_st EVP_PKEY
;
22 typedef struct ssl_st SSL
;
23 typedef struct x509_st X509
;
28 class SingleRequestCertVerifier
;
29 class SSLCertRequestInfo
;
32 // An SSL client socket implemented with OpenSSL.
33 class SSLClientSocketOpenSSL
: public SSLClientSocket
{
35 // Takes ownership of the transport_socket, which may already be connected.
36 // The given hostname will be compared with the name(s) in the server's
37 // certificate during the SSL handshake. ssl_config specifies the SSL
39 SSLClientSocketOpenSSL(ClientSocketHandle
* transport_socket
,
40 const HostPortPair
& host_and_port
,
41 const SSLConfig
& ssl_config
,
42 const SSLClientSocketContext
& context
);
43 virtual ~SSLClientSocketOpenSSL();
45 const HostPortPair
& host_and_port() const { return host_and_port_
; }
46 const std::string
& ssl_session_cache_shard() const {
47 return ssl_session_cache_shard_
;
50 // Callback from the SSL layer that indicates the remote server is requesting
51 // a certificate for this client.
52 int ClientCertRequestCallback(SSL
* ssl
, X509
** x509
, EVP_PKEY
** pkey
);
54 // Callback from the SSL layer to check which NPN protocol we are supporting
55 int SelectNextProtoCallback(unsigned char** out
, unsigned char* outlen
,
56 const unsigned char* in
, unsigned int inlen
);
58 // SSLClientSocket implementation.
59 virtual void GetSSLInfo(SSLInfo
* ssl_info
) OVERRIDE
;
60 virtual void GetSSLCertRequestInfo(
61 SSLCertRequestInfo
* cert_request_info
) OVERRIDE
;
62 virtual int ExportKeyingMaterial(const base::StringPiece
& label
,
64 const base::StringPiece
& context
,
66 unsigned int outlen
) OVERRIDE
;
67 virtual NextProtoStatus
GetNextProto(std::string
* proto
,
68 std::string
* server_protos
) OVERRIDE
;
69 virtual ServerBoundCertService
* GetServerBoundCertService() const OVERRIDE
;
71 // StreamSocket implementation.
72 virtual int Connect(const CompletionCallback
& callback
) OVERRIDE
;
73 virtual void Disconnect() OVERRIDE
;
74 virtual bool IsConnected() const OVERRIDE
;
75 virtual bool IsConnectedAndIdle() const OVERRIDE
;
76 virtual int GetPeerAddress(IPEndPoint
* address
) const OVERRIDE
;
77 virtual int GetLocalAddress(IPEndPoint
* address
) const OVERRIDE
;
78 virtual const BoundNetLog
& NetLog() const OVERRIDE
;
79 virtual void SetSubresourceSpeculation() OVERRIDE
;
80 virtual void SetOmniboxSpeculation() OVERRIDE
;
81 virtual bool WasEverUsed() const OVERRIDE
;
82 virtual bool UsingTCPFastOpen() const OVERRIDE
;
83 virtual int64
NumBytesRead() const OVERRIDE
;
84 virtual base::TimeDelta
GetConnectTimeMicros() const OVERRIDE
;
86 // Socket implementation.
87 virtual int Read(IOBuffer
* buf
, int buf_len
,
88 const CompletionCallback
& callback
) OVERRIDE
;
89 virtual int Write(IOBuffer
* buf
, int buf_len
,
90 const CompletionCallback
& callback
) OVERRIDE
;
91 virtual bool SetReceiveBufferSize(int32 size
) OVERRIDE
;
92 virtual bool SetSendBufferSize(int32 size
) OVERRIDE
;
96 void DoReadCallback(int result
);
97 void DoWriteCallback(int result
);
101 int DoVerifyCert(int result
);
102 int DoVerifyCertComplete(int result
);
103 void DoConnectCallback(int result
);
104 X509Certificate
* UpdateServerCert();
106 void OnHandshakeIOComplete(int result
);
107 void OnSendComplete(int result
);
108 void OnRecvComplete(int result
);
110 int DoHandshakeLoop(int last_io_result
);
111 int DoReadLoop(int result
);
112 int DoWriteLoop(int result
);
114 int DoPayloadWrite();
118 void BufferSendComplete(int result
);
119 void BufferRecvComplete(int result
);
120 void TransportWriteComplete(int result
);
121 void TransportReadComplete(int result
);
123 bool transport_send_busy_
;
124 scoped_refptr
<DrainableIOBuffer
> send_buffer_
;
125 bool transport_recv_busy_
;
126 bool transport_recv_eof_
;
127 scoped_refptr
<IOBuffer
> recv_buffer_
;
129 CompletionCallback user_connect_callback_
;
130 CompletionCallback user_read_callback_
;
131 CompletionCallback user_write_callback_
;
133 // Used by Read function.
134 scoped_refptr
<IOBuffer
> user_read_buf_
;
135 int user_read_buf_len_
;
137 // Used by Write function.
138 scoped_refptr
<IOBuffer
> user_write_buf_
;
139 int user_write_buf_len_
;
141 // Set when handshake finishes.
142 scoped_refptr
<X509Certificate
> server_cert_
;
143 CertVerifyResult server_cert_verify_result_
;
144 bool completed_handshake_
;
146 // Stores client authentication information between ClientAuthHandler and
147 // GetSSLCertRequestInfo calls.
148 std::vector
<scoped_refptr
<X509Certificate
> > client_certs_
;
149 bool client_auth_cert_needed_
;
151 CertVerifier
* const cert_verifier_
;
152 scoped_ptr
<SingleRequestCertVerifier
> verifier_
;
158 scoped_ptr
<ClientSocketHandle
> transport_
;
159 const HostPortPair host_and_port_
;
160 SSLConfig ssl_config_
;
161 // ssl_session_cache_shard_ is an opaque string that partitions the SSL
162 // session cache. i.e. sessions created with one value will not attempt to
163 // resume on the socket with a different value.
164 const std::string ssl_session_cache_shard_
;
166 // Used for session cache diagnostics.
167 bool trying_cached_session_
;
173 STATE_VERIFY_CERT_COMPLETE
,
175 State next_handshake_state_
;
176 NextProtoStatus npn_status_
;
177 std::string npn_proto_
;
178 std::string server_protos_
;
179 BoundNetLog net_log_
;
184 #endif // NET_SOCKET_SSL_CLIENT_SOCKET_OPENSSL_H_