OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "net/socket/openssl_ssl_util.h" | |
6 | |
7 #include <openssl/err.h> | |
8 #include <openssl/ssl.h> | |
9 | |
10 #include "base/lazy_instance.h" | |
11 #include "base/location.h" | |
12 #include "base/logging.h" | |
13 #include "crypto/openssl_util.h" | |
14 #include "net/base/net_errors.h" | |
15 | |
16 namespace net { | |
17 | |
18 SslSetClearMask::SslSetClearMask() | |
19 : set_mask(0), | |
20 clear_mask(0) { | |
21 } | |
22 | |
23 void SslSetClearMask::ConfigureFlag(long flag, bool state) { | |
24 (state ? set_mask : clear_mask) |= flag; | |
25 // Make sure we haven't got any intersection in the set & clear options. | |
26 DCHECK_EQ(0, set_mask & clear_mask) << flag << ":" << state; | |
27 } | |
28 | |
29 namespace { | |
30 | |
31 class OpenSSLNetErrorLibSingleton { | |
32 public: | |
33 OpenSSLNetErrorLibSingleton() { | |
34 crypto::EnsureOpenSSLInit(); | |
35 | |
36 // Allocate a new error library value for inserting net errors into | |
37 // OpenSSL. This does not register any ERR_STRING_DATA for the errors, so | |
38 // stringifying error codes through OpenSSL will return NULL. | |
39 net_error_lib_ = ERR_get_next_error_library(); | |
40 } | |
41 | |
42 int net_error_lib() const { return net_error_lib_; } | |
43 | |
44 private: | |
45 int net_error_lib_; | |
46 }; | |
47 | |
48 base::LazyInstance<OpenSSLNetErrorLibSingleton>::Leaky g_openssl_net_error_lib = | |
49 LAZY_INSTANCE_INITIALIZER; | |
50 | |
51 int OpenSSLNetErrorLib() { | |
52 return g_openssl_net_error_lib.Get().net_error_lib(); | |
53 } | |
54 | |
55 int MapOpenSSLErrorSSL(unsigned long error_code) { | |
56 DCHECK_EQ(ERR_LIB_SSL, ERR_GET_LIB(error_code)); | |
57 | |
58 DVLOG(1) << "OpenSSL SSL error, reason: " << ERR_GET_REASON(error_code) | |
59 << ", name: " << ERR_error_string(error_code, NULL); | |
60 switch (ERR_GET_REASON(error_code)) { | |
61 case SSL_R_READ_TIMEOUT_EXPIRED: | |
62 return ERR_TIMED_OUT; | |
63 case SSL_R_BAD_RESPONSE_ARGUMENT: | |
64 return ERR_INVALID_ARGUMENT; | |
65 case SSL_R_UNKNOWN_CERTIFICATE_TYPE: | |
66 case SSL_R_UNKNOWN_CIPHER_TYPE: | |
67 case SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE: | |
68 case SSL_R_UNKNOWN_PKEY_TYPE: | |
69 case SSL_R_UNKNOWN_REMOTE_ERROR_TYPE: | |
70 case SSL_R_UNKNOWN_SSL_VERSION: | |
71 return ERR_NOT_IMPLEMENTED; | |
72 case SSL_R_UNSUPPORTED_SSL_VERSION: | |
73 case SSL_R_NO_CIPHER_MATCH: | |
74 case SSL_R_NO_SHARED_CIPHER: | |
75 case SSL_R_TLSV1_ALERT_INSUFFICIENT_SECURITY: | |
76 case SSL_R_TLSV1_ALERT_PROTOCOL_VERSION: | |
77 case SSL_R_UNSUPPORTED_PROTOCOL: | |
78 return ERR_SSL_VERSION_OR_CIPHER_MISMATCH; | |
79 case SSL_R_SSLV3_ALERT_BAD_CERTIFICATE: | |
80 case SSL_R_SSLV3_ALERT_UNSUPPORTED_CERTIFICATE: | |
81 case SSL_R_SSLV3_ALERT_CERTIFICATE_REVOKED: | |
82 case SSL_R_SSLV3_ALERT_CERTIFICATE_EXPIRED: | |
83 case SSL_R_SSLV3_ALERT_CERTIFICATE_UNKNOWN: | |
84 case SSL_R_TLSV1_ALERT_ACCESS_DENIED: | |
85 case SSL_R_TLSV1_ALERT_UNKNOWN_CA: | |
86 return ERR_BAD_SSL_CLIENT_AUTH_CERT; | |
87 case SSL_R_BAD_DECOMPRESSION: | |
88 case SSL_R_SSLV3_ALERT_DECOMPRESSION_FAILURE: | |
89 return ERR_SSL_DECOMPRESSION_FAILURE_ALERT; | |
90 case SSL_R_SSLV3_ALERT_BAD_RECORD_MAC: | |
91 return ERR_SSL_BAD_RECORD_MAC_ALERT; | |
92 case SSL_R_TLSV1_ALERT_DECRYPT_ERROR: | |
93 return ERR_SSL_DECRYPT_ERROR_ALERT; | |
94 case SSL_R_TLSV1_UNRECOGNIZED_NAME: | |
95 return ERR_SSL_UNRECOGNIZED_NAME_ALERT; | |
96 case SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED: | |
97 return ERR_SSL_UNSAFE_NEGOTIATION; | |
98 case SSL_R_WRONG_NUMBER_OF_KEY_BITS: | |
99 return ERR_SSL_WEAK_SERVER_EPHEMERAL_DH_KEY; | |
100 // SSL_R_UNKNOWN_PROTOCOL is reported if premature application data is | |
101 // received (see http://crbug.com/42538), and also if all the protocol | |
102 // versions supported by the server were disabled in this socket instance. | |
103 // Mapped to ERR_SSL_PROTOCOL_ERROR for compatibility with other SSL sockets | |
104 // in the former scenario. | |
105 case SSL_R_UNKNOWN_PROTOCOL: | |
106 case SSL_R_SSL_HANDSHAKE_FAILURE: | |
107 case SSL_R_DECRYPTION_FAILED: | |
108 case SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC: | |
109 case SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG: | |
110 case SSL_R_DIGEST_CHECK_FAILED: | |
111 case SSL_R_DUPLICATE_COMPRESSION_ID: | |
112 case SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER: | |
113 case SSL_R_ENCRYPTED_LENGTH_TOO_LONG: | |
114 case SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST: | |
115 case SSL_R_EXCESSIVE_MESSAGE_SIZE: | |
116 case SSL_R_EXTRA_DATA_IN_MESSAGE: | |
117 case SSL_R_GOT_A_FIN_BEFORE_A_CCS: | |
118 case SSL_R_ILLEGAL_PADDING: | |
119 case SSL_R_INVALID_CHALLENGE_LENGTH: | |
120 case SSL_R_INVALID_COMMAND: | |
121 case SSL_R_INVALID_PURPOSE: | |
122 case SSL_R_INVALID_STATUS_RESPONSE: | |
123 case SSL_R_INVALID_TICKET_KEYS_LENGTH: | |
124 case SSL_R_KEY_ARG_TOO_LONG: | |
125 case SSL_R_READ_WRONG_PACKET_TYPE: | |
126 // SSL_do_handshake reports this error when the server responds to a | |
127 // ClientHello with a fatal close_notify alert. | |
128 case SSL_AD_REASON_OFFSET + SSL_AD_CLOSE_NOTIFY: | |
129 case SSL_R_SSLV3_ALERT_UNEXPECTED_MESSAGE: | |
130 // TODO(joth): SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE may be returned from the | |
131 // server after receiving ClientHello if there's no common supported cipher. | |
132 // Ideally we'd map that specific case to ERR_SSL_VERSION_OR_CIPHER_MISMATCH | |
133 // to match the NSS implementation. See also http://goo.gl/oMtZW | |
134 case SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE: | |
135 case SSL_R_SSLV3_ALERT_NO_CERTIFICATE: | |
136 case SSL_R_SSLV3_ALERT_ILLEGAL_PARAMETER: | |
137 case SSL_R_TLSV1_ALERT_DECODE_ERROR: | |
138 case SSL_R_TLSV1_ALERT_DECRYPTION_FAILED: | |
139 case SSL_R_TLSV1_ALERT_EXPORT_RESTRICTION: | |
140 case SSL_R_TLSV1_ALERT_INTERNAL_ERROR: | |
141 case SSL_R_TLSV1_ALERT_NO_RENEGOTIATION: | |
142 case SSL_R_TLSV1_ALERT_RECORD_OVERFLOW: | |
143 case SSL_R_TLSV1_ALERT_USER_CANCELLED: | |
144 return ERR_SSL_PROTOCOL_ERROR; | |
145 case SSL_R_CERTIFICATE_VERIFY_FAILED: | |
146 // The only way that the certificate verify callback can fail is if | |
147 // the leaf certificate changed during a renegotiation. | |
148 return ERR_SSL_SERVER_CERT_CHANGED; | |
149 default: | |
150 LOG(WARNING) << "Unmapped error reason: " << ERR_GET_REASON(error_code); | |
151 return ERR_FAILED; | |
152 } | |
153 } | |
154 | |
155 } // namespace | |
156 | |
157 void OpenSSLPutNetError(const tracked_objects::Location& location, int err) { | |
158 // Net error codes are negative. Encode them as positive numbers. | |
159 err = -err; | |
160 if (err < 0 || err > 0xfff) { | |
161 // OpenSSL reserves 12 bits for the reason code. | |
162 NOTREACHED(); | |
163 err = ERR_INVALID_ARGUMENT; | |
164 } | |
165 ERR_PUT_error(OpenSSLNetErrorLib(), 0, err, | |
166 location.file_name(), location.line_number()); | |
167 } | |
168 | |
169 int MapOpenSSLError(int err, const crypto::OpenSSLErrStackTracer& tracer) { | |
170 switch (err) { | |
171 case SSL_ERROR_WANT_READ: | |
172 case SSL_ERROR_WANT_WRITE: | |
173 return ERR_IO_PENDING; | |
174 case SSL_ERROR_SYSCALL: | |
175 LOG(ERROR) << "OpenSSL SYSCALL error, earliest error code in " | |
176 "error queue: " << ERR_peek_error() << ", errno: " | |
177 << errno; | |
178 return ERR_SSL_PROTOCOL_ERROR; | |
179 case SSL_ERROR_SSL: | |
180 // Walk down the error stack to find an SSL or net error. | |
181 unsigned long error_code; | |
182 do { | |
183 error_code = ERR_get_error(); | |
184 if (ERR_GET_LIB(error_code) == ERR_LIB_SSL) { | |
185 return MapOpenSSLErrorSSL(error_code); | |
186 } else if (ERR_GET_LIB(error_code) == OpenSSLNetErrorLib()) { | |
187 // Net error codes are negative but encoded in OpenSSL as positive | |
188 // numbers. | |
189 return -ERR_GET_REASON(error_code); | |
190 } | |
191 } while (error_code != 0); | |
192 return ERR_SSL_PROTOCOL_ERROR; | |
193 default: | |
194 // TODO(joth): Implement full mapping. | |
195 LOG(WARNING) << "Unknown OpenSSL error " << err; | |
196 return ERR_SSL_PROTOCOL_ERROR; | |
197 } | |
198 } | |
199 | |
200 } // namespace net | |
OLD | NEW |