1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/ssl/openssl_ssl_util.h"
6
7 #include <errno.h>
8
9 #include <utility>
10
11 #include "base/lazy_instance.h"
12 #include "base/location.h"
13 #include "base/logging.h"
14 #include "base/notreached.h"
15 #include "base/values.h"
16 #include "build/build_config.h"
17 #include "crypto/openssl_util.h"
18 #include "net/base/net_errors.h"
19 #include "net/cert/x509_util.h"
20 #include "net/log/net_log_with_source.h"
21 #include "net/ssl/ssl_connection_status_flags.h"
22 #include "third_party/boringssl/src/include/openssl/err.h"
23 #include "third_party/boringssl/src/include/openssl/ssl.h"
24
25 namespace net {
26
27 SslSetClearMask::SslSetClearMask() = default;
28
ConfigureFlag(long flag,bool state)29 void SslSetClearMask::ConfigureFlag(long flag, bool state) {
30 (state ? set_mask : clear_mask) |= flag;
31 // Make sure we haven't got any intersection in the set & clear options.
32 DCHECK_EQ(0, set_mask & clear_mask) << flag << ":" << state;
33 }
34
35 namespace {
36
37 class OpenSSLNetErrorLibSingleton {
38 public:
OpenSSLNetErrorLibSingleton()39 OpenSSLNetErrorLibSingleton() {
40 crypto::EnsureOpenSSLInit();
41
42 // Allocate a new error library value for inserting net errors into
43 // OpenSSL. This does not register any ERR_STRING_DATA for the errors, so
44 // stringifying error codes through OpenSSL will return NULL.
45 net_error_lib_ = ERR_get_next_error_library();
46 }
47
net_error_lib() const48 int net_error_lib() const { return net_error_lib_; }
49
50 private:
51 int net_error_lib_;
52 };
53
54 base::LazyInstance<OpenSSLNetErrorLibSingleton>::Leaky g_openssl_net_error_lib =
55 LAZY_INSTANCE_INITIALIZER;
56
OpenSSLNetErrorLib()57 int OpenSSLNetErrorLib() {
58 return g_openssl_net_error_lib.Get().net_error_lib();
59 }
60
MapOpenSSLErrorSSL(uint32_t error_code)61 int MapOpenSSLErrorSSL(uint32_t error_code) {
62 DCHECK_EQ(ERR_LIB_SSL, ERR_GET_LIB(error_code));
63
64 #if DCHECK_IS_ON()
65 char buf[ERR_ERROR_STRING_BUF_LEN];
66 ERR_error_string_n(error_code, buf, sizeof(buf));
67 DVLOG(1) << "OpenSSL SSL error, reason: " << ERR_GET_REASON(error_code)
68 << ", name: " << buf;
69 #endif
70
71 switch (ERR_GET_REASON(error_code)) {
72 case SSL_R_READ_TIMEOUT_EXPIRED:
73 return ERR_TIMED_OUT;
74 case SSL_R_UNKNOWN_CERTIFICATE_TYPE:
75 case SSL_R_UNKNOWN_CIPHER_TYPE:
76 case SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE:
77 case SSL_R_UNKNOWN_SSL_VERSION:
78 return ERR_NOT_IMPLEMENTED;
79 case SSL_R_NO_CIPHER_MATCH:
80 case SSL_R_NO_SHARED_CIPHER:
81 case SSL_R_TLSV1_ALERT_INSUFFICIENT_SECURITY:
82 case SSL_R_TLSV1_ALERT_PROTOCOL_VERSION:
83 case SSL_R_UNSUPPORTED_PROTOCOL:
84 return ERR_SSL_VERSION_OR_CIPHER_MISMATCH;
85 case SSL_R_SSLV3_ALERT_BAD_CERTIFICATE:
86 case SSL_R_SSLV3_ALERT_UNSUPPORTED_CERTIFICATE:
87 case SSL_R_SSLV3_ALERT_CERTIFICATE_REVOKED:
88 case SSL_R_SSLV3_ALERT_CERTIFICATE_EXPIRED:
89 case SSL_R_SSLV3_ALERT_CERTIFICATE_UNKNOWN:
90 case SSL_R_TLSV1_ALERT_ACCESS_DENIED:
91 case SSL_R_TLSV1_ALERT_CERTIFICATE_REQUIRED:
92 case SSL_R_TLSV1_ALERT_UNKNOWN_CA:
93 return ERR_BAD_SSL_CLIENT_AUTH_CERT;
94 case SSL_R_SSLV3_ALERT_DECOMPRESSION_FAILURE:
95 return ERR_SSL_DECOMPRESSION_FAILURE_ALERT;
96 case SSL_R_SSLV3_ALERT_BAD_RECORD_MAC:
97 return ERR_SSL_BAD_RECORD_MAC_ALERT;
98 case SSL_R_TLSV1_ALERT_DECRYPT_ERROR:
99 return ERR_SSL_DECRYPT_ERROR_ALERT;
100 case SSL_R_TLSV1_UNRECOGNIZED_NAME:
101 return ERR_SSL_UNRECOGNIZED_NAME_ALERT;
102 case SSL_R_SERVER_CERT_CHANGED:
103 return ERR_SSL_SERVER_CERT_CHANGED;
104 case SSL_R_WRONG_VERSION_ON_EARLY_DATA:
105 return ERR_WRONG_VERSION_ON_EARLY_DATA;
106 case SSL_R_TLS13_DOWNGRADE:
107 return ERR_TLS13_DOWNGRADE_DETECTED;
108 case SSL_R_ECH_REJECTED:
109 return ERR_ECH_NOT_NEGOTIATED;
110 // SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE may be returned from the server after
111 // receiving ClientHello if there's no common supported cipher. Map that
112 // specific case to ERR_SSL_VERSION_OR_CIPHER_MISMATCH to match the NSS
113 // implementation. See https://goo.gl/oMtZW and https://crbug.com/446505.
114 case SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE: {
115 uint32_t previous = ERR_peek_error();
116 if (previous != 0 && ERR_GET_LIB(previous) == ERR_LIB_SSL &&
117 ERR_GET_REASON(previous) == SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO) {
118 return ERR_SSL_VERSION_OR_CIPHER_MISMATCH;
119 }
120 return ERR_SSL_PROTOCOL_ERROR;
121 }
122 case SSL_R_KEY_USAGE_BIT_INCORRECT:
123 return ERR_SSL_KEY_USAGE_INCOMPATIBLE;
124 default:
125 return ERR_SSL_PROTOCOL_ERROR;
126 }
127 }
128
NetLogOpenSSLErrorParams(int net_error,int ssl_error,const OpenSSLErrorInfo & error_info)129 base::Value::Dict NetLogOpenSSLErrorParams(int net_error,
130 int ssl_error,
131 const OpenSSLErrorInfo& error_info) {
132 base::Value::Dict dict;
133 dict.Set("net_error", net_error);
134 dict.Set("ssl_error", ssl_error);
135 if (error_info.error_code != 0) {
136 dict.Set("error_lib", ERR_GET_LIB(error_info.error_code));
137 dict.Set("error_reason", ERR_GET_REASON(error_info.error_code));
138 }
139 if (error_info.file != nullptr)
140 dict.Set("file", error_info.file);
141 if (error_info.line != 0)
142 dict.Set("line", error_info.line);
143 return dict;
144 }
145
146 } // namespace
147
OpenSSLPutNetError(const base::Location & location,int err)148 void OpenSSLPutNetError(const base::Location& location, int err) {
149 // Net error codes are negative. Encode them as positive numbers.
150 err = -err;
151 if (err < 0 || err > 0xfff) {
152 // OpenSSL reserves 12 bits for the reason code.
153 NOTREACHED();
154 err = ERR_INVALID_ARGUMENT;
155 }
156 ERR_put_error(OpenSSLNetErrorLib(), 0 /* unused */, err, location.file_name(),
157 location.line_number());
158 }
159
MapOpenSSLError(int err,const crypto::OpenSSLErrStackTracer & tracer)160 int MapOpenSSLError(int err, const crypto::OpenSSLErrStackTracer& tracer) {
161 OpenSSLErrorInfo error_info;
162 return MapOpenSSLErrorWithDetails(err, tracer, &error_info);
163 }
164
MapOpenSSLErrorWithDetails(int err,const crypto::OpenSSLErrStackTracer & tracer,OpenSSLErrorInfo * out_error_info)165 int MapOpenSSLErrorWithDetails(int err,
166 const crypto::OpenSSLErrStackTracer& tracer,
167 OpenSSLErrorInfo* out_error_info) {
168 *out_error_info = OpenSSLErrorInfo();
169
170 switch (err) {
171 case SSL_ERROR_WANT_READ:
172 case SSL_ERROR_WANT_WRITE:
173 return ERR_IO_PENDING;
174 case SSL_ERROR_EARLY_DATA_REJECTED:
175 return ERR_EARLY_DATA_REJECTED;
176 case SSL_ERROR_SYSCALL:
177 PLOG(ERROR) << "OpenSSL SYSCALL error, earliest error code in "
178 "error queue: "
179 << ERR_peek_error();
180 return ERR_FAILED;
181 case SSL_ERROR_SSL:
182 // Walk down the error stack to find an SSL or net error.
183 while (true) {
184 OpenSSLErrorInfo error_info;
185 error_info.error_code =
186 ERR_get_error_line(&error_info.file, &error_info.line);
187 if (error_info.error_code == 0) {
188 // Map errors to ERR_SSL_PROTOCOL_ERROR by default, reporting the most
189 // recent error in |*out_error_info|.
190 return ERR_SSL_PROTOCOL_ERROR;
191 }
192
193 *out_error_info = error_info;
194 if (ERR_GET_LIB(error_info.error_code) == ERR_LIB_SSL) {
195 return MapOpenSSLErrorSSL(error_info.error_code);
196 }
197 if (ERR_GET_LIB(error_info.error_code) == OpenSSLNetErrorLib()) {
198 // Net error codes are negative but encoded in OpenSSL as positive
199 // numbers.
200 return -ERR_GET_REASON(error_info.error_code);
201 }
202 }
203 default:
204 // TODO(joth): Implement full mapping.
205 LOG(WARNING) << "Unknown OpenSSL error " << err;
206 return ERR_SSL_PROTOCOL_ERROR;
207 }
208 }
209
NetLogOpenSSLError(const NetLogWithSource & net_log,NetLogEventType type,int net_error,int ssl_error,const OpenSSLErrorInfo & error_info)210 void NetLogOpenSSLError(const NetLogWithSource& net_log,
211 NetLogEventType type,
212 int net_error,
213 int ssl_error,
214 const OpenSSLErrorInfo& error_info) {
215 net_log.AddEvent(type, [&] {
216 return NetLogOpenSSLErrorParams(net_error, ssl_error, error_info);
217 });
218 }
219
GetNetSSLVersion(SSL * ssl)220 int GetNetSSLVersion(SSL* ssl) {
221 switch (SSL_version(ssl)) {
222 case TLS1_VERSION:
223 return SSL_CONNECTION_VERSION_TLS1;
224 case TLS1_1_VERSION:
225 return SSL_CONNECTION_VERSION_TLS1_1;
226 case TLS1_2_VERSION:
227 return SSL_CONNECTION_VERSION_TLS1_2;
228 case TLS1_3_VERSION:
229 return SSL_CONNECTION_VERSION_TLS1_3;
230 default:
231 NOTREACHED();
232 return SSL_CONNECTION_VERSION_UNKNOWN;
233 }
234 }
235
SetSSLChainAndKey(SSL * ssl,X509Certificate * cert,EVP_PKEY * pkey,const SSL_PRIVATE_KEY_METHOD * custom_key)236 bool SetSSLChainAndKey(SSL* ssl,
237 X509Certificate* cert,
238 EVP_PKEY* pkey,
239 const SSL_PRIVATE_KEY_METHOD* custom_key) {
240 std::vector<CRYPTO_BUFFER*> chain_raw;
241 chain_raw.reserve(1 + cert->intermediate_buffers().size());
242 chain_raw.push_back(cert->cert_buffer());
243 for (const auto& handle : cert->intermediate_buffers())
244 chain_raw.push_back(handle.get());
245
246 if (!SSL_set_chain_and_key(ssl, chain_raw.data(), chain_raw.size(), pkey,
247 custom_key)) {
248 LOG(WARNING) << "Failed to set client certificate";
249 return false;
250 }
251
252 return true;
253 }
254
255 } // namespace net
256