blob: 5e0915a85c2638cbd67d0f19f2296a0dd9ebdbd7 [file] [log] [blame]
[email protected]d518cd92010-09-29 12:27:441// Copyright (c) 2010 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.
4
5#ifndef NET_SOCKET_SSL_CLIENT_SOCKET_OPENSSL_H_
6#define NET_SOCKET_SSL_CLIENT_SOCKET_OPENSSL_H_
7#pragma once
8
9#include "base/scoped_ptr.h"
[email protected]170e76c2010-10-04 15:04:2010#include "net/base/cert_verify_result.h"
[email protected]d518cd92010-09-29 12:27:4411#include "net/base/completion_callback.h"
12#include "net/base/io_buffer.h"
13#include "net/base/ssl_config_service.h"
14#include "net/socket/ssl_client_socket.h"
15#include "net/socket/client_socket_handle.h"
16
17typedef struct bio_st BIO;
[email protected]718c9672010-12-02 10:04:1018typedef struct evp_pkey_st EVP_PKEY;
[email protected]d518cd92010-09-29 12:27:4419typedef struct ssl_st SSL;
[email protected]718c9672010-12-02 10:04:1020typedef struct x509_st X509;
[email protected]d518cd92010-09-29 12:27:4421
22namespace net {
23
[email protected]170e76c2010-10-04 15:04:2024class CertVerifier;
[email protected]d518cd92010-09-29 12:27:4425class SSLCertRequestInfo;
26class SSLConfig;
27class SSLInfo;
28
29// An SSL client socket implemented with OpenSSL.
30class SSLClientSocketOpenSSL : public SSLClientSocket {
31 public:
32 // Takes ownership of the transport_socket, which may already be connected.
33 // The given hostname will be compared with the name(s) in the server's
34 // certificate during the SSL handshake. ssl_config specifies the SSL
35 // settings.
36 SSLClientSocketOpenSSL(ClientSocketHandle* transport_socket,
[email protected]055d7f22010-11-15 12:03:1237 const HostPortPair& host_and_port,
[email protected]d518cd92010-09-29 12:27:4438 const SSLConfig& ssl_config);
39 ~SSLClientSocketOpenSSL();
40
[email protected]fbef13932010-11-23 12:38:5341 const HostPortPair& host_and_port() const { return host_and_port_; }
42
[email protected]718c9672010-12-02 10:04:1043 // Callback from the SSL layer that indicates the remote server is requesting
44 // a certificate for this client.
45 int ClientCertRequestCallback(SSL* ssl, X509** x509, EVP_PKEY** pkey);
46
[email protected]d518cd92010-09-29 12:27:4447 // SSLClientSocket methods:
48 virtual void GetSSLInfo(SSLInfo* ssl_info);
49 virtual void GetSSLCertRequestInfo(SSLCertRequestInfo* cert_request_info);
50 virtual NextProtoStatus GetNextProto(std::string* proto);
51
52 // ClientSocket methods:
53 virtual int Connect(CompletionCallback* callback);
54 virtual void Disconnect();
55 virtual bool IsConnected() const;
56 virtual bool IsConnectedAndIdle() const;
[email protected]7e5dd49f2010-12-08 18:33:4957 virtual int GetPeerAddress(AddressList* address) const;
[email protected]d518cd92010-09-29 12:27:4458 virtual const BoundNetLog& NetLog() const;
59 virtual void SetSubresourceSpeculation();
60 virtual void SetOmniboxSpeculation();
61 virtual bool WasEverUsed() const;
[email protected]7f7e92392010-10-26 18:29:2962 virtual bool UsingTCPFastOpen() const;
[email protected]d518cd92010-09-29 12:27:4463
64 // Socket methods:
65 virtual int Read(IOBuffer* buf, int buf_len, CompletionCallback* callback);
66 virtual int Write(IOBuffer* buf, int buf_len, CompletionCallback* callback);
67 virtual bool SetReceiveBufferSize(int32 size);
68 virtual bool SetSendBufferSize(int32 size);
69
70 private:
[email protected]d518cd92010-09-29 12:27:4471 bool Init();
72 void DoReadCallback(int result);
73 void DoWriteCallback(int result);
74
75 bool DoTransportIO();
76 int DoHandshake();
[email protected]170e76c2010-10-04 15:04:2077 int DoVerifyCert(int result);
78 int DoVerifyCertComplete(int result);
[email protected]d518cd92010-09-29 12:27:4479 void DoConnectCallback(int result);
[email protected]170e76c2010-10-04 15:04:2080 X509Certificate* UpdateServerCert();
[email protected]d518cd92010-09-29 12:27:4481
82 void OnHandshakeIOComplete(int result);
83 void OnSendComplete(int result);
84 void OnRecvComplete(int result);
85
86 int DoHandshakeLoop(int last_io_result);
87 int DoReadLoop(int result);
88 int DoWriteLoop(int result);
89 int DoPayloadRead();
90 int DoPayloadWrite();
91
92 int BufferSend();
93 int BufferRecv();
94 void BufferSendComplete(int result);
95 void BufferRecvComplete(int result);
96 void TransportWriteComplete(int result);
97 void TransportReadComplete(int result);
98
99 CompletionCallbackImpl<SSLClientSocketOpenSSL> buffer_send_callback_;
100 CompletionCallbackImpl<SSLClientSocketOpenSSL> buffer_recv_callback_;
101 bool transport_send_busy_;
102 scoped_refptr<DrainableIOBuffer> send_buffer_;
103 bool transport_recv_busy_;
104 scoped_refptr<IOBuffer> recv_buffer_;
105
106 CompletionCallback* user_connect_callback_;
107 CompletionCallback* user_read_callback_;
108 CompletionCallback* user_write_callback_;
109
110 // Used by Read function.
111 scoped_refptr<IOBuffer> user_read_buf_;
112 int user_read_buf_len_;
113
114 // Used by Write function.
115 scoped_refptr<IOBuffer> user_write_buf_;
116 int user_write_buf_len_;
117
[email protected]170e76c2010-10-04 15:04:20118 // Set when handshake finishes.
119 scoped_refptr<X509Certificate> server_cert_;
120 CertVerifyResult server_cert_verify_result_;
[email protected]fbef13932010-11-23 12:38:53121 bool completed_handshake_;
[email protected]170e76c2010-10-04 15:04:20122
[email protected]d518cd92010-09-29 12:27:44123 // Stores client authentication information between ClientAuthHandler and
124 // GetSSLCertRequestInfo calls.
125 std::vector<scoped_refptr<X509Certificate> > client_certs_;
126 bool client_auth_cert_needed_;
127
[email protected]170e76c2010-10-04 15:04:20128 scoped_ptr<CertVerifier> verifier_;
129 CompletionCallbackImpl<SSLClientSocketOpenSSL> handshake_io_callback_;
130
[email protected]d518cd92010-09-29 12:27:44131 // OpenSSL stuff
[email protected]d518cd92010-09-29 12:27:44132 SSL* ssl_;
133 BIO* transport_bio_;
134
135 scoped_ptr<ClientSocketHandle> transport_;
[email protected]055d7f22010-11-15 12:03:12136 const HostPortPair host_and_port_;
[email protected]d518cd92010-09-29 12:27:44137 SSLConfig ssl_config_;
138
[email protected]fbef13932010-11-23 12:38:53139 // Used for session cache diagnostics.
140 bool trying_cached_session_;
[email protected]d518cd92010-09-29 12:27:44141
142 enum State {
143 STATE_NONE,
144 STATE_HANDSHAKE,
145 STATE_VERIFY_CERT,
146 STATE_VERIFY_CERT_COMPLETE,
147 };
148 State next_handshake_state_;
149 BoundNetLog net_log_;
150};
151
152} // namespace net
153
[email protected]7e5dd49f2010-12-08 18:33:49154#endif // NET_SOCKET_SSL_CLIENT_SOCKET_OPENSSL_H_
[email protected]d518cd92010-09-29 12:27:44155