blob: 7d850f8f0b07ee0c58e9130f3d9589cf7b249649 [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"
10#include "net/base/completion_callback.h"
11#include "net/base/io_buffer.h"
12#include "net/base/ssl_config_service.h"
13#include "net/socket/ssl_client_socket.h"
14#include "net/socket/client_socket_handle.h"
15
16typedef struct bio_st BIO;
17typedef struct ssl_ctx_st SSL_CTX;
18typedef struct ssl_st SSL;
19
20namespace net {
21
22class SSLCertRequestInfo;
23class SSLConfig;
24class SSLInfo;
25
26// An SSL client socket implemented with OpenSSL.
27class SSLClientSocketOpenSSL : public SSLClientSocket {
28 public:
29 // Takes ownership of the transport_socket, which may already be connected.
30 // The given hostname will be compared with the name(s) in the server's
31 // certificate during the SSL handshake. ssl_config specifies the SSL
32 // settings.
33 SSLClientSocketOpenSSL(ClientSocketHandle* transport_socket,
34 const std::string& hostname,
35 const SSLConfig& ssl_config);
36 ~SSLClientSocketOpenSSL();
37
38 // SSLClientSocket methods:
39 virtual void GetSSLInfo(SSLInfo* ssl_info);
40 virtual void GetSSLCertRequestInfo(SSLCertRequestInfo* cert_request_info);
41 virtual NextProtoStatus GetNextProto(std::string* proto);
42
43 // ClientSocket methods:
44 virtual int Connect(CompletionCallback* callback);
45 virtual void Disconnect();
46 virtual bool IsConnected() const;
47 virtual bool IsConnectedAndIdle() const;
48 virtual int GetPeerAddress(AddressList*) const;
49 virtual const BoundNetLog& NetLog() const;
50 virtual void SetSubresourceSpeculation();
51 virtual void SetOmniboxSpeculation();
52 virtual bool WasEverUsed() const;
53
54 // Socket methods:
55 virtual int Read(IOBuffer* buf, int buf_len, CompletionCallback* callback);
56 virtual int Write(IOBuffer* buf, int buf_len, CompletionCallback* callback);
57 virtual bool SetReceiveBufferSize(int32 size);
58 virtual bool SetSendBufferSize(int32 size);
59
60 private:
61 bool InitOpenSSL();
62 bool Init();
63 void DoReadCallback(int result);
64 void DoWriteCallback(int result);
65
66 bool DoTransportIO();
67 int DoHandshake();
68 void DoConnectCallback(int result);
69
70 void OnHandshakeIOComplete(int result);
71 void OnSendComplete(int result);
72 void OnRecvComplete(int result);
73
74 int DoHandshakeLoop(int last_io_result);
75 int DoReadLoop(int result);
76 int DoWriteLoop(int result);
77 int DoPayloadRead();
78 int DoPayloadWrite();
79
80 int BufferSend();
81 int BufferRecv();
82 void BufferSendComplete(int result);
83 void BufferRecvComplete(int result);
84 void TransportWriteComplete(int result);
85 void TransportReadComplete(int result);
86
87 CompletionCallbackImpl<SSLClientSocketOpenSSL> buffer_send_callback_;
88 CompletionCallbackImpl<SSLClientSocketOpenSSL> buffer_recv_callback_;
89 bool transport_send_busy_;
90 scoped_refptr<DrainableIOBuffer> send_buffer_;
91 bool transport_recv_busy_;
92 scoped_refptr<IOBuffer> recv_buffer_;
93
94 CompletionCallback* user_connect_callback_;
95 CompletionCallback* user_read_callback_;
96 CompletionCallback* user_write_callback_;
97
98 // Used by Read function.
99 scoped_refptr<IOBuffer> user_read_buf_;
100 int user_read_buf_len_;
101
102 // Used by Write function.
103 scoped_refptr<IOBuffer> user_write_buf_;
104 int user_write_buf_len_;
105
106 // Stores client authentication information between ClientAuthHandler and
107 // GetSSLCertRequestInfo calls.
108 std::vector<scoped_refptr<X509Certificate> > client_certs_;
109 bool client_auth_cert_needed_;
110
111 // OpenSSL stuff
112 static SSL_CTX* g_ctx;
113 SSL* ssl_;
114 BIO* transport_bio_;
115
116 scoped_ptr<ClientSocketHandle> transport_;
117 std::string hostname_;
118 SSLConfig ssl_config_;
119
120 bool completed_handshake_;
121
122 enum State {
123 STATE_NONE,
124 STATE_HANDSHAKE,
125 STATE_VERIFY_CERT,
126 STATE_VERIFY_CERT_COMPLETE,
127 };
128 State next_handshake_state_;
129 BoundNetLog net_log_;
130};
131
132} // namespace net
133
134#endif // NET_SOCKET_SSL_CLIENT_SOCKET_OPENSSL_H_
135