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 #include "net/socket/ssl_client_socket.h"
7 #include "base/metrics/histogram.h"
8 #include "base/strings/string_util.h"
9 #include "crypto/ec_private_key.h"
10 #include "net/ssl/server_bound_cert_service.h"
11 #include "net/ssl/ssl_config_service.h"
15 SSLClientSocket::SSLClientSocket()
16 : was_npn_negotiated_(false),
17 was_spdy_negotiated_(false),
18 protocol_negotiated_(kProtoUnknown
),
19 channel_id_sent_(false) {
23 NextProto
SSLClientSocket::NextProtoFromString(
24 const std::string
& proto_string
) {
25 if (proto_string
== "http1.1" || proto_string
== "http/1.1") {
27 } else if (proto_string
== "spdy/2") {
28 return kProtoDeprecatedSPDY2
;
29 } else if (proto_string
== "spdy/3") {
31 } else if (proto_string
== "spdy/3.1") {
33 } else if (proto_string
== "spdy/4a2") {
35 } else if (proto_string
== "HTTP-draft-04/2.0") {
36 return kProtoHTTP2Draft04
;
37 } else if (proto_string
== "quic/1+spdy/3") {
38 return kProtoQUIC1SPDY3
;
45 const char* SSLClientSocket::NextProtoToString(NextProto next_proto
) {
49 case kProtoDeprecatedSPDY2
:
57 case kProtoHTTP2Draft04
:
58 return "HTTP-draft-04/2.0";
59 case kProtoQUIC1SPDY3
:
60 return "quic/1+spdy/3";
68 const char* SSLClientSocket::NextProtoStatusToString(
69 const SSLClientSocket::NextProtoStatus status
) {
71 case kNextProtoUnsupported
:
73 case kNextProtoNegotiated
:
75 case kNextProtoNoOverlap
:
82 std::string
SSLClientSocket::ServerProtosToString(
83 const std::string
& server_protos
) {
84 const char* protos
= server_protos
.c_str();
85 size_t protos_len
= server_protos
.length();
86 std::vector
<std::string
> server_protos_with_commas
;
87 for (size_t i
= 0; i
< protos_len
; ) {
88 const size_t len
= protos
[i
];
89 std::string
proto_str(&protos
[i
+ 1], len
);
90 server_protos_with_commas
.push_back(proto_str
);
93 return JoinString(server_protos_with_commas
, ',');
96 bool SSLClientSocket::WasNpnNegotiated() const {
97 return was_npn_negotiated_
;
100 NextProto
SSLClientSocket::GetNegotiatedProtocol() const {
101 return protocol_negotiated_
;
104 bool SSLClientSocket::IgnoreCertError(int error
, int load_flags
) {
105 if (error
== OK
|| load_flags
& LOAD_IGNORE_ALL_CERT_ERRORS
)
108 if (error
== ERR_CERT_COMMON_NAME_INVALID
&&
109 (load_flags
& LOAD_IGNORE_CERT_COMMON_NAME_INVALID
))
112 if (error
== ERR_CERT_DATE_INVALID
&&
113 (load_flags
& LOAD_IGNORE_CERT_DATE_INVALID
))
116 if (error
== ERR_CERT_AUTHORITY_INVALID
&&
117 (load_flags
& LOAD_IGNORE_CERT_AUTHORITY_INVALID
))
123 bool SSLClientSocket::set_was_npn_negotiated(bool negotiated
) {
124 return was_npn_negotiated_
= negotiated
;
127 bool SSLClientSocket::was_spdy_negotiated() const {
128 return was_spdy_negotiated_
;
131 bool SSLClientSocket::set_was_spdy_negotiated(bool negotiated
) {
132 return was_spdy_negotiated_
= negotiated
;
135 void SSLClientSocket::set_protocol_negotiated(NextProto protocol_negotiated
) {
136 protocol_negotiated_
= protocol_negotiated
;
139 bool SSLClientSocket::WasChannelIDSent() const {
140 return channel_id_sent_
;
143 void SSLClientSocket::set_channel_id_sent(bool channel_id_sent
) {
144 channel_id_sent_
= channel_id_sent
;
148 void SSLClientSocket::RecordChannelIDSupport(
149 ServerBoundCertService
* server_bound_cert_service
,
150 bool negotiated_channel_id
,
151 bool channel_id_enabled
,
153 // Since this enum is used for a histogram, do not change or re-use values.
157 CLIENT_AND_SERVER
= 2,
159 CLIENT_BAD_SYSTEM_TIME
= 4,
160 CLIENT_NO_SERVER_BOUND_CERT_SERVICE
= 5,
161 DOMAIN_BOUND_CERT_USAGE_MAX
162 } supported
= DISABLED
;
163 if (negotiated_channel_id
) {
164 supported
= CLIENT_AND_SERVER
;
165 } else if (channel_id_enabled
) {
166 if (!server_bound_cert_service
)
167 supported
= CLIENT_NO_SERVER_BOUND_CERT_SERVICE
;
168 else if (!supports_ecc
)
169 supported
= CLIENT_NO_ECC
;
170 else if (!server_bound_cert_service
->IsSystemTimeValid())
171 supported
= CLIENT_BAD_SYSTEM_TIME
;
173 supported
= CLIENT_ONLY
;
175 UMA_HISTOGRAM_ENUMERATION("DomainBoundCerts.Support", supported
,
176 DOMAIN_BOUND_CERT_USAGE_MAX
);
180 bool SSLClientSocket::IsChannelIDEnabled(
181 const SSLConfig
& ssl_config
,
182 ServerBoundCertService
* server_bound_cert_service
) {
183 if (!ssl_config
.channel_id_enabled
)
185 if (!server_bound_cert_service
) {
186 DVLOG(1) << "NULL server_bound_cert_service_, not enabling channel ID.";
189 if (!crypto::ECPrivateKey::IsSupported()) {
190 DVLOG(1) << "Elliptic Curve not supported, not enabling channel ID.";
193 if (!server_bound_cert_service
->IsSystemTimeValid()) {
194 DVLOG(1) << "System time is not within the supported range for certificate "
195 "generation, not enabling channel ID.";