OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/socket/openssl_ssl_util.h" | 5 #include "net/socket/openssl_ssl_util.h" |
6 | 6 |
7 #include <errno.h> | |
8 | |
9 #include <openssl/err.h> | 7 #include <openssl/err.h> |
10 #include <openssl/ssl.h> | 8 #include <openssl/ssl.h> |
11 | 9 |
12 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
13 #include "base/location.h" | 11 #include "base/location.h" |
14 #include "base/logging.h" | 12 #include "base/logging.h" |
15 #include "crypto/openssl_util.h" | 13 #include "crypto/openssl_util.h" |
16 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
17 | 15 |
18 namespace net { | 16 namespace net { |
(...skipping 15 matching lines...) Expand all Loading... |
34 public: | 32 public: |
35 OpenSSLNetErrorLibSingleton() { | 33 OpenSSLNetErrorLibSingleton() { |
36 crypto::EnsureOpenSSLInit(); | 34 crypto::EnsureOpenSSLInit(); |
37 | 35 |
38 // Allocate a new error library value for inserting net errors into | 36 // Allocate a new error library value for inserting net errors into |
39 // OpenSSL. This does not register any ERR_STRING_DATA for the errors, so | 37 // OpenSSL. This does not register any ERR_STRING_DATA for the errors, so |
40 // stringifying error codes through OpenSSL will return NULL. | 38 // stringifying error codes through OpenSSL will return NULL. |
41 net_error_lib_ = ERR_get_next_error_library(); | 39 net_error_lib_ = ERR_get_next_error_library(); |
42 } | 40 } |
43 | 41 |
44 unsigned net_error_lib() const { return net_error_lib_; } | 42 int net_error_lib() const { return net_error_lib_; } |
45 | 43 |
46 private: | 44 private: |
47 unsigned net_error_lib_; | 45 int net_error_lib_; |
48 }; | 46 }; |
49 | 47 |
50 base::LazyInstance<OpenSSLNetErrorLibSingleton>::Leaky g_openssl_net_error_lib = | 48 base::LazyInstance<OpenSSLNetErrorLibSingleton>::Leaky g_openssl_net_error_lib = |
51 LAZY_INSTANCE_INITIALIZER; | 49 LAZY_INSTANCE_INITIALIZER; |
52 | 50 |
53 unsigned OpenSSLNetErrorLib() { | 51 int OpenSSLNetErrorLib() { |
54 return g_openssl_net_error_lib.Get().net_error_lib(); | 52 return g_openssl_net_error_lib.Get().net_error_lib(); |
55 } | 53 } |
56 | 54 |
57 int MapOpenSSLErrorSSL(unsigned long error_code) { | 55 int MapOpenSSLErrorSSL(unsigned long error_code) { |
58 DCHECK_EQ(ERR_LIB_SSL, ERR_GET_LIB(error_code)); | 56 DCHECK_EQ(ERR_LIB_SSL, ERR_GET_LIB(error_code)); |
59 | 57 |
60 DVLOG(1) << "OpenSSL SSL error, reason: " << ERR_GET_REASON(error_code) | 58 DVLOG(1) << "OpenSSL SSL error, reason: " << ERR_GET_REASON(error_code) |
61 << ", name: " << ERR_error_string(error_code, NULL); | 59 << ", name: " << ERR_error_string(error_code, NULL); |
62 switch (ERR_GET_REASON(error_code)) { | 60 switch (ERR_GET_REASON(error_code)) { |
63 case SSL_R_READ_TIMEOUT_EXPIRED: | 61 case SSL_R_READ_TIMEOUT_EXPIRED: |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 } // namespace | 155 } // namespace |
158 | 156 |
159 void OpenSSLPutNetError(const tracked_objects::Location& location, int err) { | 157 void OpenSSLPutNetError(const tracked_objects::Location& location, int err) { |
160 // Net error codes are negative. Encode them as positive numbers. | 158 // Net error codes are negative. Encode them as positive numbers. |
161 err = -err; | 159 err = -err; |
162 if (err < 0 || err > 0xfff) { | 160 if (err < 0 || err > 0xfff) { |
163 // OpenSSL reserves 12 bits for the reason code. | 161 // OpenSSL reserves 12 bits for the reason code. |
164 NOTREACHED(); | 162 NOTREACHED(); |
165 err = ERR_INVALID_ARGUMENT; | 163 err = ERR_INVALID_ARGUMENT; |
166 } | 164 } |
167 ERR_put_error(OpenSSLNetErrorLib(), 0, err, | 165 ERR_PUT_error(OpenSSLNetErrorLib(), 0, err, |
168 location.file_name(), location.line_number()); | 166 location.file_name(), location.line_number()); |
169 } | 167 } |
170 | 168 |
171 int MapOpenSSLError(int err, const crypto::OpenSSLErrStackTracer& tracer) { | 169 int MapOpenSSLError(int err, const crypto::OpenSSLErrStackTracer& tracer) { |
172 switch (err) { | 170 switch (err) { |
173 case SSL_ERROR_WANT_READ: | 171 case SSL_ERROR_WANT_READ: |
174 case SSL_ERROR_WANT_WRITE: | 172 case SSL_ERROR_WANT_WRITE: |
175 return ERR_IO_PENDING; | 173 return ERR_IO_PENDING; |
176 case SSL_ERROR_SYSCALL: | 174 case SSL_ERROR_SYSCALL: |
177 LOG(ERROR) << "OpenSSL SYSCALL error, earliest error code in " | 175 LOG(ERROR) << "OpenSSL SYSCALL error, earliest error code in " |
(...skipping 15 matching lines...) Expand all Loading... |
193 } while (error_code != 0); | 191 } while (error_code != 0); |
194 return ERR_SSL_PROTOCOL_ERROR; | 192 return ERR_SSL_PROTOCOL_ERROR; |
195 default: | 193 default: |
196 // TODO(joth): Implement full mapping. | 194 // TODO(joth): Implement full mapping. |
197 LOG(WARNING) << "Unknown OpenSSL error " << err; | 195 LOG(WARNING) << "Unknown OpenSSL error " << err; |
198 return ERR_SSL_PROTOCOL_ERROR; | 196 return ERR_SSL_PROTOCOL_ERROR; |
199 } | 197 } |
200 } | 198 } |
201 | 199 |
202 } // namespace net | 200 } // namespace net |
OLD | NEW |