blob: 69f03c9f4dfba40b2c58c409ed74d74695463e13 [file] [log] [blame]
[email protected]61f3ddf2012-02-08 02:45:391// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]d518cd92010-09-29 12:27:442// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef NET_SOCKET_SSL_CLIENT_SOCKET_OPENSSL_H_
6#define NET_SOCKET_SSL_CLIENT_SOCKET_OPENSSL_H_
7#pragma once
8
[email protected]ea4a1c6a2010-12-09 13:33:289#include <string>
10
[email protected]3b63f8f42011-03-28 01:54:1511#include "base/memory/scoped_ptr.h"
[email protected]170e76c2010-10-04 15:04:2012#include "net/base/cert_verify_result.h"
[email protected]d518cd92010-09-29 12:27:4413#include "net/base/completion_callback.h"
14#include "net/base/io_buffer.h"
15#include "net/base/ssl_config_service.h"
16#include "net/socket/ssl_client_socket.h"
17#include "net/socket/client_socket_handle.h"
18
19typedef struct bio_st BIO;
[email protected]718c9672010-12-02 10:04:1020typedef struct evp_pkey_st EVP_PKEY;
[email protected]d518cd92010-09-29 12:27:4421typedef struct ssl_st SSL;
[email protected]718c9672010-12-02 10:04:1022typedef struct x509_st X509;
[email protected]d518cd92010-09-29 12:27:4423
24namespace net {
25
[email protected]170e76c2010-10-04 15:04:2026class CertVerifier;
[email protected]822581d2010-12-16 17:27:1527class SingleRequestCertVerifier;
[email protected]d518cd92010-09-29 12:27:4428class SSLCertRequestInfo;
29class SSLConfig;
30class SSLInfo;
31
32// An SSL client socket implemented with OpenSSL.
33class SSLClientSocketOpenSSL : public SSLClientSocket {
34 public:
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
38 // settings.
39 SSLClientSocketOpenSSL(ClientSocketHandle* transport_socket,
[email protected]055d7f22010-11-15 12:03:1240 const HostPortPair& host_and_port,
[email protected]822581d2010-12-16 17:27:1541 const SSLConfig& ssl_config,
[email protected]feb79bcd2011-07-21 16:55:1742 const SSLClientSocketContext& context);
[email protected]d518cd92010-09-29 12:27:4443 ~SSLClientSocketOpenSSL();
44
[email protected]fbef13932010-11-23 12:38:5345 const HostPortPair& host_and_port() const { return host_and_port_; }
[email protected]c3456bb2011-12-12 22:22:1946 const std::string& ssl_session_cache_shard() const {
47 return ssl_session_cache_shard_;
48 }
[email protected]fbef13932010-11-23 12:38:5349
[email protected]718c9672010-12-02 10:04:1050 // 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);
53
[email protected]ea4a1c6a2010-12-09 13:33:2854 // 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);
57
[email protected]dbf036f2011-12-06 23:33:2458 // SSLClientSocket implementation.
[email protected]d518cd92010-09-29 12:27:4459 virtual void GetSSLInfo(SSLInfo* ssl_info);
60 virtual void GetSSLCertRequestInfo(SSLCertRequestInfo* cert_request_info);
[email protected]b0ff3f82011-07-23 05:12:3961 virtual int ExportKeyingMaterial(const base::StringPiece& label,
[email protected]1bc6f5e2012-03-15 00:20:5862 bool has_context,
[email protected]b0ff3f82011-07-23 05:12:3963 const base::StringPiece& context,
[email protected]1bc6f5e2012-03-15 00:20:5864 unsigned char* out,
[email protected]b0ff3f82011-07-23 05:12:3965 unsigned int outlen);
[email protected]55e973d2011-12-05 23:03:2466 virtual NextProtoStatus GetNextProto(std::string* proto,
67 std::string* server_protos);
[email protected]61f3ddf2012-02-08 02:45:3968 virtual OriginBoundCertService* GetOriginBoundCertService() const;
[email protected]d518cd92010-09-29 12:27:4469
[email protected]dbf036f2011-12-06 23:33:2470 // StreamSocket implementation.
[email protected]dbf036f2011-12-06 23:33:2471 virtual int Connect(const CompletionCallback& callback);
[email protected]d518cd92010-09-29 12:27:4472 virtual void Disconnect();
73 virtual bool IsConnected() const;
74 virtual bool IsConnectedAndIdle() const;
[email protected]7e5dd49f2010-12-08 18:33:4975 virtual int GetPeerAddress(AddressList* address) const;
[email protected]e7f74da2011-04-19 23:49:3576 virtual int GetLocalAddress(IPEndPoint* address) const;
[email protected]d518cd92010-09-29 12:27:4477 virtual const BoundNetLog& NetLog() const;
78 virtual void SetSubresourceSpeculation();
79 virtual void SetOmniboxSpeculation();
80 virtual bool WasEverUsed() const;
[email protected]7f7e92392010-10-26 18:29:2981 virtual bool UsingTCPFastOpen() const;
[email protected]1e71d3072011-07-05 11:34:4782 virtual int64 NumBytesRead() const;
83 virtual base::TimeDelta GetConnectTimeMicros() const;
[email protected]d518cd92010-09-29 12:27:4484
[email protected]dbf036f2011-12-06 23:33:2485 // Socket implementation.
[email protected]3f55aa12011-12-07 02:03:3386 virtual int Read(IOBuffer* buf, int buf_len,
87 const CompletionCallback& callback);
[email protected]83039bb2011-12-09 18:43:5588 virtual int Write(IOBuffer* buf, int buf_len,
89 const CompletionCallback& callback);
[email protected]d518cd92010-09-29 12:27:4490 virtual bool SetReceiveBufferSize(int32 size);
91 virtual bool SetSendBufferSize(int32 size);
92
93 private:
[email protected]d518cd92010-09-29 12:27:4494 bool Init();
95 void DoReadCallback(int result);
96 void DoWriteCallback(int result);
97
98 bool DoTransportIO();
99 int DoHandshake();
[email protected]170e76c2010-10-04 15:04:20100 int DoVerifyCert(int result);
101 int DoVerifyCertComplete(int result);
[email protected]d518cd92010-09-29 12:27:44102 void DoConnectCallback(int result);
[email protected]170e76c2010-10-04 15:04:20103 X509Certificate* UpdateServerCert();
[email protected]d518cd92010-09-29 12:27:44104
105 void OnHandshakeIOComplete(int result);
106 void OnSendComplete(int result);
107 void OnRecvComplete(int result);
108
109 int DoHandshakeLoop(int last_io_result);
110 int DoReadLoop(int result);
111 int DoWriteLoop(int result);
112 int DoPayloadRead();
113 int DoPayloadWrite();
114
115 int BufferSend();
116 int BufferRecv();
117 void BufferSendComplete(int result);
118 void BufferRecvComplete(int result);
119 void TransportWriteComplete(int result);
120 void TransportReadComplete(int result);
121
[email protected]d518cd92010-09-29 12:27:44122 bool transport_send_busy_;
123 scoped_refptr<DrainableIOBuffer> send_buffer_;
124 bool transport_recv_busy_;
125 scoped_refptr<IOBuffer> recv_buffer_;
126
[email protected]dbf036f2011-12-06 23:33:24127 CompletionCallback user_connect_callback_;
[email protected]3f55aa12011-12-07 02:03:33128 CompletionCallback user_read_callback_;
[email protected]83039bb2011-12-09 18:43:55129 CompletionCallback user_write_callback_;
[email protected]d518cd92010-09-29 12:27:44130
131 // Used by Read function.
132 scoped_refptr<IOBuffer> user_read_buf_;
133 int user_read_buf_len_;
134
135 // Used by Write function.
136 scoped_refptr<IOBuffer> user_write_buf_;
137 int user_write_buf_len_;
138
[email protected]170e76c2010-10-04 15:04:20139 // Set when handshake finishes.
140 scoped_refptr<X509Certificate> server_cert_;
141 CertVerifyResult server_cert_verify_result_;
[email protected]fbef13932010-11-23 12:38:53142 bool completed_handshake_;
[email protected]170e76c2010-10-04 15:04:20143
[email protected]d518cd92010-09-29 12:27:44144 // Stores client authentication information between ClientAuthHandler and
145 // GetSSLCertRequestInfo calls.
146 std::vector<scoped_refptr<X509Certificate> > client_certs_;
147 bool client_auth_cert_needed_;
148
[email protected]822581d2010-12-16 17:27:15149 CertVerifier* const cert_verifier_;
150 scoped_ptr<SingleRequestCertVerifier> verifier_;
[email protected]170e76c2010-10-04 15:04:20151
[email protected]d518cd92010-09-29 12:27:44152 // OpenSSL stuff
[email protected]d518cd92010-09-29 12:27:44153 SSL* ssl_;
154 BIO* transport_bio_;
155
156 scoped_ptr<ClientSocketHandle> transport_;
[email protected]055d7f22010-11-15 12:03:12157 const HostPortPair host_and_port_;
[email protected]d518cd92010-09-29 12:27:44158 SSLConfig ssl_config_;
[email protected]c3456bb2011-12-12 22:22:19159 // ssl_session_cache_shard_ is an opaque string that partitions the SSL
160 // session cache. i.e. sessions created with one value will not attempt to
161 // resume on the socket with a different value.
162 const std::string ssl_session_cache_shard_;
[email protected]d518cd92010-09-29 12:27:44163
[email protected]fbef13932010-11-23 12:38:53164 // Used for session cache diagnostics.
165 bool trying_cached_session_;
[email protected]d518cd92010-09-29 12:27:44166
167 enum State {
168 STATE_NONE,
169 STATE_HANDSHAKE,
170 STATE_VERIFY_CERT,
171 STATE_VERIFY_CERT_COMPLETE,
172 };
173 State next_handshake_state_;
[email protected]ea4a1c6a2010-12-09 13:33:28174 NextProtoStatus npn_status_;
175 std::string npn_proto_;
[email protected]55e973d2011-12-05 23:03:24176 std::string server_protos_;
[email protected]d518cd92010-09-29 12:27:44177 BoundNetLog net_log_;
178};
179
180} // namespace net
181
[email protected]7e5dd49f2010-12-08 18:33:49182#endif // NET_SOCKET_SSL_CLIENT_SOCKET_OPENSSL_H_