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_H_
6 #define NET_SOCKET_SSL_CLIENT_SOCKET_H_
10 #include "base/gtest_prod_util.h"
11 #include "net/base/completion_callback.h"
12 #include "net/base/load_flags.h"
13 #include "net/base/net_errors.h"
14 #include "net/socket/ssl_socket.h"
15 #include "net/socket/stream_socket.h"
19 class CertPolicyEnforcer
;
21 class ChannelIDService
;
23 class SSLCertRequestInfo
;
26 class TransportSecurityState
;
27 class X509Certificate
;
29 // This struct groups together several fields which are used by various
30 // classes related to SSLClientSocket.
31 struct SSLClientSocketContext
{
32 SSLClientSocketContext()
33 : cert_verifier(NULL
),
34 channel_id_service(NULL
),
35 transport_security_state(NULL
),
36 cert_transparency_verifier(NULL
),
37 cert_policy_enforcer(NULL
) {}
39 SSLClientSocketContext(CertVerifier
* cert_verifier_arg
,
40 ChannelIDService
* channel_id_service_arg
,
41 TransportSecurityState
* transport_security_state_arg
,
42 CTVerifier
* cert_transparency_verifier_arg
,
43 CertPolicyEnforcer
* cert_policy_enforcer_arg
,
44 const std::string
& ssl_session_cache_shard_arg
)
45 : cert_verifier(cert_verifier_arg
),
46 channel_id_service(channel_id_service_arg
),
47 transport_security_state(transport_security_state_arg
),
48 cert_transparency_verifier(cert_transparency_verifier_arg
),
49 cert_policy_enforcer(cert_policy_enforcer_arg
),
50 ssl_session_cache_shard(ssl_session_cache_shard_arg
) {}
52 CertVerifier
* cert_verifier
;
53 ChannelIDService
* channel_id_service
;
54 TransportSecurityState
* transport_security_state
;
55 CTVerifier
* cert_transparency_verifier
;
56 CertPolicyEnforcer
* cert_policy_enforcer
;
57 // ssl_session_cache_shard is an opaque string that identifies a shard of the
58 // SSL session cache. SSL sockets with the same ssl_session_cache_shard may
59 // resume each other's SSL sessions but we'll never sessions between shards.
60 const std::string ssl_session_cache_shard
;
63 // A client socket that uses SSL as the transport layer.
65 // NOTE: The SSL handshake occurs within the Connect method after a TCP
66 // connection is established. If a SSL error occurs during the handshake,
69 class NET_EXPORT SSLClientSocket
: public SSLSocket
{
73 // Next Protocol Negotiation (NPN) allows a TLS client and server to come to
74 // an agreement about the application level protocol to speak over a
76 enum NextProtoStatus
{
77 // WARNING: These values are serialized to disk. Don't change them.
79 kNextProtoUnsupported
= 0, // The server doesn't support NPN.
80 kNextProtoNegotiated
= 1, // We agreed on a protocol.
81 kNextProtoNoOverlap
= 2, // No protocols in common. We requested
82 // the first protocol in our list.
85 // TLS extension used to negotiate protocol.
86 enum SSLNegotiationExtension
{
93 bool WasNpnNegotiated() const override
;
94 NextProto
GetNegotiatedProtocol() const override
;
96 // Gets the SSL CertificateRequest info of the socket after Connect failed
97 // with ERR_SSL_CLIENT_AUTH_CERT_NEEDED.
98 virtual void GetSSLCertRequestInfo(
99 SSLCertRequestInfo
* cert_request_info
) = 0;
101 // Get the application level protocol that we negotiated with the server.
102 // *proto is set to the resulting protocol (n.b. that the string may have
104 // kNextProtoUnsupported: *proto is cleared.
105 // kNextProtoNegotiated: *proto is set to the negotiated protocol.
106 // kNextProtoNoOverlap: *proto is set to the first protocol in the
108 virtual NextProtoStatus
GetNextProto(std::string
* proto
) const = 0;
110 static NextProto
NextProtoFromString(const std::string
& proto_string
);
112 static const char* NextProtoToString(NextProto next_proto
);
114 static const char* NextProtoStatusToString(const NextProtoStatus status
);
116 // Returns true if |error| is OK or |load_flags| ignores certificate errors
117 // and |error| is a certificate error.
118 static bool IgnoreCertError(int error
, int load_flags
);
120 // ClearSessionCache clears the SSL session cache, used to resume SSL
122 static void ClearSessionCache();
124 // Get the maximum SSL version supported by the underlying library and
125 // cryptographic implementation.
126 static uint16
GetMaxSupportedSSLVersion();
128 // Returns the ChannelIDService used by this socket, or NULL if
129 // channel ids are not supported.
130 virtual ChannelIDService
* GetChannelIDService() const = 0;
133 void set_negotiation_extension(
134 SSLNegotiationExtension negotiation_extension
) {
135 negotiation_extension_
= negotiation_extension
;
138 void set_signed_cert_timestamps_received(
139 bool signed_cert_timestamps_received
) {
140 signed_cert_timestamps_received_
= signed_cert_timestamps_received
;
143 void set_stapled_ocsp_response_received(bool stapled_ocsp_response_received
) {
144 stapled_ocsp_response_received_
= stapled_ocsp_response_received
;
147 // Record which TLS extension was used to negotiate protocol and protocol
148 // chosen in a UMA histogram.
149 void RecordNegotiationExtension();
151 // Records histograms for channel id support during full handshakes - resumed
152 // handshakes are ignored.
153 static void RecordChannelIDSupport(
154 ChannelIDService
* channel_id_service
,
155 bool negotiated_channel_id
,
156 bool channel_id_enabled
,
159 // Returns whether TLS channel ID is enabled.
160 static bool IsChannelIDEnabled(
161 const SSLConfig
& ssl_config
,
162 ChannelIDService
* channel_id_service
);
164 // Determine if there is at least one enabled cipher suite that satisfies
165 // Section 9.2 of the HTTP/2 specification. Note that the server might still
166 // pick an inadequate cipher suite.
167 static bool HasCipherAdequateForHTTP2(
168 const std::vector
<uint16
>& cipher_suites
);
170 // Determine if the TLS version required by Section 9.2 of the HTTP/2
171 // specification is enabled. Note that the server might still pick an
172 // inadequate TLS version.
173 static bool IsTLSVersionAdequateForHTTP2(const SSLConfig
& ssl_config
);
175 // Serializes |next_protos| in the wire format for ALPN: protocols are listed
176 // in order, each prefixed by a one-byte length. Any HTTP/2 protocols in
177 // |next_protos| are ignored if |can_advertise_http2| is false.
178 static std::vector
<uint8_t> SerializeNextProtos(
179 const NextProtoVector
& next_protos
,
180 bool can_advertise_http2
);
182 // For unit testing only.
183 // Returns the unverified certificate chain as presented by server.
184 // Note that chain may be different than the verified chain returned by
185 // StreamSocket::GetSSLInfo().
186 virtual scoped_refptr
<X509Certificate
> GetUnverifiedServerCertificateChain()
190 FRIEND_TEST_ALL_PREFIXES(SSLClientSocket
, SerializeNextProtos
);
191 // For signed_cert_timestamps_received_ and stapled_ocsp_response_received_.
192 FRIEND_TEST_ALL_PREFIXES(SSLClientSocketTest
,
193 ConnectSignedCertTimestampsEnabledTLSExtension
);
194 FRIEND_TEST_ALL_PREFIXES(SSLClientSocketTest
,
195 ConnectSignedCertTimestampsEnabledOCSP
);
196 FRIEND_TEST_ALL_PREFIXES(SSLClientSocketTest
,
197 ConnectSignedCertTimestampsDisabled
);
198 FRIEND_TEST_ALL_PREFIXES(SSLClientSocketTest
,
199 VerifyServerChainProperlyOrdered
);
201 // True if SCTs were received via a TLS extension.
202 bool signed_cert_timestamps_received_
;
203 // True if a stapled OCSP response was received.
204 bool stapled_ocsp_response_received_
;
205 // Protocol negotiation extension used.
206 SSLNegotiationExtension negotiation_extension_
;
211 #endif // NET_SOCKET_SSL_CLIENT_SOCKET_H_