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