Disable rei.com on oopif benchmarks.
[chromium-blink-merge.git] / net / socket / ssl_client_socket_nss.h
blob84bb6acf51e8a9ef2659a2dbc4afc8d08310617e
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_NSS_H_
6 #define NET_SOCKET_SSL_CLIENT_SOCKET_NSS_H_
8 #include <certt.h>
9 #include <keyt.h>
10 #include <nspr.h>
11 #include <nss.h>
13 #include <string>
14 #include <vector>
16 #include "base/memory/scoped_ptr.h"
17 #include "base/synchronization/lock.h"
18 #include "base/threading/platform_thread.h"
19 #include "base/time/time.h"
20 #include "net/base/completion_callback.h"
21 #include "net/base/host_port_pair.h"
22 #include "net/base/net_export.h"
23 #include "net/base/nss_memio.h"
24 #include "net/cert/cert_verifier.h"
25 #include "net/cert/cert_verify_result.h"
26 #include "net/cert/ct_verify_result.h"
27 #include "net/cert/x509_certificate.h"
28 #include "net/log/net_log.h"
29 #include "net/socket/ssl_client_socket.h"
30 #include "net/ssl/channel_id_service.h"
31 #include "net/ssl/ssl_config_service.h"
33 namespace base {
34 class SequencedTaskRunner;
37 namespace net {
39 class BoundNetLog;
40 class CertPolicyEnforcer;
41 class CertVerifier;
42 class ChannelIDService;
43 class CTVerifier;
44 class ClientSocketHandle;
45 class TransportSecurityState;
46 class X509Certificate;
48 // An SSL client socket implemented with Mozilla NSS.
49 class SSLClientSocketNSS : public SSLClientSocket {
50 public:
51 // Takes ownership of the |transport_socket|, which must already be connected.
52 // The hostname specified in |host_and_port| will be compared with the name(s)
53 // in the server's certificate during the SSL handshake. If SSL client
54 // authentication is requested, the host_and_port field of SSLCertRequestInfo
55 // will be populated with |host_and_port|. |ssl_config| specifies
56 // the SSL settings.
58 // Because calls to NSS may block, such as due to needing to access slow
59 // hardware or needing to synchronously unlock protected tokens, calls to
60 // NSS may optionally be run on a dedicated thread. If synchronous/blocking
61 // behaviour is desired, for performance or compatibility, the current task
62 // runner should be supplied instead.
63 SSLClientSocketNSS(base::SequencedTaskRunner* nss_task_runner,
64 scoped_ptr<ClientSocketHandle> transport_socket,
65 const HostPortPair& host_and_port,
66 const SSLConfig& ssl_config,
67 const SSLClientSocketContext& context);
68 ~SSLClientSocketNSS() override;
70 // SSLClientSocket implementation.
71 void GetSSLCertRequestInfo(SSLCertRequestInfo* cert_request_info) override;
72 NextProtoStatus GetNextProto(std::string* proto) const override;
74 // SSLSocket implementation.
75 int ExportKeyingMaterial(const base::StringPiece& label,
76 bool has_context,
77 const base::StringPiece& context,
78 unsigned char* out,
79 unsigned int outlen) override;
80 int GetTLSUniqueChannelBinding(std::string* out) override;
82 // StreamSocket implementation.
83 int Connect(const CompletionCallback& callback) override;
84 void Disconnect() override;
85 bool IsConnected() const override;
86 bool IsConnectedAndIdle() const override;
87 int GetPeerAddress(IPEndPoint* address) const override;
88 int GetLocalAddress(IPEndPoint* address) const override;
89 const BoundNetLog& NetLog() const override;
90 void SetSubresourceSpeculation() override;
91 void SetOmniboxSpeculation() override;
92 bool WasEverUsed() const override;
93 bool UsingTCPFastOpen() const override;
94 bool GetSSLInfo(SSLInfo* ssl_info) override;
95 void GetConnectionAttempts(ConnectionAttempts* out) const override;
96 void ClearConnectionAttempts() override {}
97 void AddConnectionAttempts(const ConnectionAttempts& attempts) override {}
99 // Socket implementation.
100 int Read(IOBuffer* buf,
101 int buf_len,
102 const CompletionCallback& callback) override;
103 int Write(IOBuffer* buf,
104 int buf_len,
105 const CompletionCallback& callback) override;
106 int SetReceiveBufferSize(int32 size) override;
107 int SetSendBufferSize(int32 size) override;
109 // SSLClientSocket implementation.
110 ChannelIDService* GetChannelIDService() const override;
111 SSLFailureState GetSSLFailureState() const override;
113 private:
114 // Helper class to handle marshalling any NSS interaction to and from the
115 // NSS and network task runners. Not every call needs to happen on the Core
116 class Core;
118 enum State {
119 STATE_NONE,
120 STATE_HANDSHAKE,
121 STATE_HANDSHAKE_COMPLETE,
122 STATE_VERIFY_CERT,
123 STATE_VERIFY_CERT_COMPLETE,
126 int Init();
127 void InitCore();
129 // Initializes NSS SSL options. Returns a net error code.
130 int InitializeSSLOptions();
132 // Initializes the socket peer name in SSL. Returns a net error code.
133 int InitializeSSLPeerName();
135 void DoConnectCallback(int result);
136 void OnHandshakeIOComplete(int result);
138 int DoHandshakeLoop(int last_io_result);
139 int DoHandshake();
140 int DoHandshakeComplete(int result);
141 int DoVerifyCert(int result);
142 int DoVerifyCertComplete(int result);
144 void VerifyCT();
146 // The following methods are for debugging bug 65948. Will remove this code
147 // after fixing bug 65948.
148 void EnsureThreadIdAssigned() const;
149 bool CalledOnValidThread() const;
151 // Adds the SignedCertificateTimestamps from ct_verify_result_ to |ssl_info|.
152 // SCTs are held in three separate vectors in ct_verify_result, each
153 // vetor representing a particular verification state, this method associates
154 // each of the SCTs with the corresponding SCTVerifyStatus as it adds it to
155 // the |ssl_info|.signed_certificate_timestamps list.
156 void AddSCTInfoToSSLInfo(SSLInfo* ssl_info) const;
158 // The task runner used to perform NSS operations.
159 scoped_refptr<base::SequencedTaskRunner> nss_task_runner_;
160 scoped_ptr<ClientSocketHandle> transport_;
161 HostPortPair host_and_port_;
162 SSLConfig ssl_config_;
164 scoped_refptr<Core> core_;
166 CompletionCallback user_connect_callback_;
168 CertVerifyResult server_cert_verify_result_;
170 CertVerifier* const cert_verifier_;
171 scoped_ptr<CertVerifier::Request> cert_verifier_request_;
173 // Certificate Transparency: Verifier and result holder.
174 ct::CTVerifyResult ct_verify_result_;
175 CTVerifier* cert_transparency_verifier_;
177 // The service for retrieving Channel ID keys. May be NULL.
178 ChannelIDService* channel_id_service_;
180 // ssl_session_cache_shard_ is an opaque string that partitions the SSL
181 // session cache. i.e. sessions created with one value will not attempt to
182 // resume on the socket with a different value.
183 const std::string ssl_session_cache_shard_;
185 // True if the SSL handshake has been completed.
186 bool completed_handshake_;
188 State next_handshake_state_;
190 // True if the socket has been disconnected.
191 bool disconnected_;
193 // The NSS SSL state machine. This is owned by |core_|.
194 // TODO(rsleevi): http://crbug.com/130616 - Remove this member once
195 // ExportKeyingMaterial is updated to be asynchronous.
196 PRFileDesc* nss_fd_;
198 BoundNetLog net_log_;
200 base::TimeTicks start_cert_verification_time_;
202 TransportSecurityState* transport_security_state_;
204 CertPolicyEnforcer* const policy_enforcer_;
206 // pinning_failure_log contains a message produced by
207 // TransportSecurityState::CheckPublicKeyPins in the event of a
208 // pinning failure. It is a (somewhat) human-readable string.
209 std::string pinning_failure_log_;
211 // The following two variables are added for debugging bug 65948. Will
212 // remove this code after fixing bug 65948.
213 // Added the following code Debugging in release mode.
214 mutable base::Lock lock_;
215 // This is mutable so that CalledOnValidThread can set it.
216 // It's guarded by |lock_|.
217 mutable base::PlatformThreadId valid_thread_id_;
220 } // namespace net
222 #endif // NET_SOCKET_SSL_CLIENT_SOCKET_NSS_H_