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_
16 #include "base/memory/scoped_ptr.h"
17 #include "base/synchronization/lock.h"
18 #include "base/threading/platform_thread.h"
19 #include "base/time.h"
20 #include "base/timer.h"
21 #include "net/base/completion_callback.h"
22 #include "net/base/host_port_pair.h"
23 #include "net/base/net_export.h"
24 #include "net/base/net_log.h"
25 #include "net/base/nss_memio.h"
26 #include "net/cert/cert_verify_result.h"
27 #include "net/cert/x509_certificate.h"
28 #include "net/socket/ssl_client_socket.h"
29 #include "net/ssl/server_bound_cert_service.h"
30 #include "net/ssl/ssl_config_service.h"
33 class SequencedTaskRunner
;
40 class ClientSocketHandle
;
41 class ServerBoundCertService
;
42 class SingleRequestCertVerifier
;
43 class TransportSecurityState
;
44 class X509Certificate
;
46 // An SSL client socket implemented with Mozilla NSS.
47 class SSLClientSocketNSS
: public SSLClientSocket
{
49 // Takes ownership of the |transport_socket|, which must already be connected.
50 // The hostname specified in |host_and_port| will be compared with the name(s)
51 // in the server's certificate during the SSL handshake. If SSL client
52 // authentication is requested, the host_and_port field of SSLCertRequestInfo
53 // will be populated with |host_and_port|. |ssl_config| specifies
56 // Because calls to NSS may block, such as due to needing to access slow
57 // hardware or needing to synchronously unlock protected tokens, calls to
58 // NSS may optionally be run on a dedicated thread. If synchronous/blocking
59 // behaviour is desired, for performance or compatibility, the current task
60 // runner should be supplied instead.
61 SSLClientSocketNSS(base::SequencedTaskRunner
* nss_task_runner
,
62 ClientSocketHandle
* transport_socket
,
63 const HostPortPair
& host_and_port
,
64 const SSLConfig
& ssl_config
,
65 const SSLClientSocketContext
& context
);
66 virtual ~SSLClientSocketNSS();
68 // SSLClientSocket implementation.
69 virtual void GetSSLCertRequestInfo(
70 SSLCertRequestInfo
* cert_request_info
) OVERRIDE
;
71 virtual NextProtoStatus
GetNextProto(std::string
* proto
,
72 std::string
* server_protos
) OVERRIDE
;
74 // SSLSocket implementation.
75 virtual int ExportKeyingMaterial(const base::StringPiece
& label
,
77 const base::StringPiece
& context
,
79 unsigned int outlen
) OVERRIDE
;
80 virtual int GetTLSUniqueChannelBinding(std::string
* out
) OVERRIDE
;
82 // StreamSocket implementation.
83 virtual int Connect(const CompletionCallback
& callback
) OVERRIDE
;
84 virtual void Disconnect() OVERRIDE
;
85 virtual bool IsConnected() const OVERRIDE
;
86 virtual bool IsConnectedAndIdle() const OVERRIDE
;
87 virtual int GetPeerAddress(IPEndPoint
* address
) const OVERRIDE
;
88 virtual int GetLocalAddress(IPEndPoint
* address
) const OVERRIDE
;
89 virtual const BoundNetLog
& NetLog() const OVERRIDE
;
90 virtual void SetSubresourceSpeculation() OVERRIDE
;
91 virtual void SetOmniboxSpeculation() OVERRIDE
;
92 virtual bool WasEverUsed() const OVERRIDE
;
93 virtual bool UsingTCPFastOpen() const OVERRIDE
;
94 virtual bool GetSSLInfo(SSLInfo
* ssl_info
) OVERRIDE
;
96 // Socket implementation.
97 virtual int Read(IOBuffer
* buf
,
99 const CompletionCallback
& callback
) OVERRIDE
;
100 virtual int Write(IOBuffer
* buf
,
102 const CompletionCallback
& callback
) OVERRIDE
;
103 virtual bool SetReceiveBufferSize(int32 size
) OVERRIDE
;
104 virtual bool SetSendBufferSize(int32 size
) OVERRIDE
;
105 virtual ServerBoundCertService
* GetServerBoundCertService() const OVERRIDE
;
108 // Helper class to handle marshalling any NSS interaction to and from the
109 // NSS and network task runners. Not every call needs to happen on the Core
115 STATE_HANDSHAKE_COMPLETE
,
117 STATE_VERIFY_CERT_COMPLETE
,
123 // Initializes NSS SSL options. Returns a net error code.
124 int InitializeSSLOptions();
126 // Initializes the socket peer name in SSL. Returns a net error code.
127 int InitializeSSLPeerName();
129 void DoConnectCallback(int result
);
130 void OnHandshakeIOComplete(int result
);
132 int DoHandshakeLoop(int last_io_result
);
134 int DoHandshakeComplete(int result
);
135 int DoVerifyCert(int result
);
136 int DoVerifyCertComplete(int result
);
138 void LogConnectionTypeMetrics() const;
140 // The following methods are for debugging bug 65948. Will remove this code
141 // after fixing bug 65948.
142 void EnsureThreadIdAssigned() const;
143 bool CalledOnValidThread() const;
145 // The task runner used to perform NSS operations.
146 scoped_refptr
<base::SequencedTaskRunner
> nss_task_runner_
;
147 scoped_ptr
<ClientSocketHandle
> transport_
;
148 HostPortPair host_and_port_
;
149 SSLConfig ssl_config_
;
151 scoped_refptr
<Core
> core_
;
153 CompletionCallback user_connect_callback_
;
155 CertVerifyResult server_cert_verify_result_
;
156 HashValueVector side_pinned_public_keys_
;
158 CertVerifier
* const cert_verifier_
;
159 scoped_ptr
<SingleRequestCertVerifier
> verifier_
;
161 // The service for retrieving Channel ID keys. May be NULL.
162 ServerBoundCertService
* server_bound_cert_service_
;
164 // ssl_session_cache_shard_ is an opaque string that partitions the SSL
165 // session cache. i.e. sessions created with one value will not attempt to
166 // resume on the socket with a different value.
167 const std::string ssl_session_cache_shard_
;
169 // True if the SSL handshake has been completed.
170 bool completed_handshake_
;
172 State next_handshake_state_
;
174 // The NSS SSL state machine. This is owned by |core_|.
175 // TODO(rsleevi): http://crbug.com/130616 - Remove this member once
176 // ExportKeyingMaterial is updated to be asynchronous.
179 BoundNetLog net_log_
;
181 base::TimeTicks start_cert_verification_time_
;
183 TransportSecurityState
* transport_security_state_
;
185 // The following two variables are added for debugging bug 65948. Will
186 // remove this code after fixing bug 65948.
187 // Added the following code Debugging in release mode.
188 mutable base::Lock lock_
;
189 // This is mutable so that CalledOnValidThread can set it.
190 // It's guarded by |lock_|.
191 mutable base::PlatformThreadId valid_thread_id_
;
196 #endif // NET_SOCKET_SSL_CLIENT_SOCKET_NSS_H_