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/nss_ssl_util.h"
15 #include "base/bind.h"
16 #include "base/lazy_instance.h"
17 #include "base/logging.h"
18 #include "base/memory/singleton.h"
19 #include "base/threading/thread_restrictions.h"
20 #include "base/values.h"
21 #include "build/build_config.h"
22 #include "crypto/nss_util.h"
23 #include "net/base/net_errors.h"
24 #include "net/base/net_log.h"
27 #include "base/win/windows_version.h"
32 class NSSSSLInitSingleton {
34 NSSSSLInitSingleton() {
35 crypto::EnsureNSSInit();
37 NSS_SetDomesticPolicy();
39 const PRUint16* const ssl_ciphers = SSL_GetImplementedCiphers();
40 const PRUint16 num_ciphers = SSL_GetNumImplementedCiphers();
42 // Disable ECDSA cipher suites on platforms that do not support ECDSA
43 // signed certificates, as servers may use the presence of such
44 // ciphersuites as a hint to send an ECDSA certificate.
45 bool disableECDSA = false;
47 if (base::win::GetVersion() < base::win::VERSION_VISTA)
51 // Explicitly enable exactly those ciphers with keys of at least 80 bits
52 for (int i = 0; i < num_ciphers; i++) {
53 SSLCipherSuiteInfo info;
54 if (SSL_GetCipherSuiteInfo(ssl_ciphers[i], &info,
55 sizeof(info)) == SECSuccess) {
56 bool enabled = info.effectiveKeyBits >= 80;
57 if (info.authAlgorithm == ssl_auth_ecdsa && disableECDSA)
60 // Trim the list of cipher suites in order to keep the size of the
61 // ClientHello down. DSS, ECDH, CAMELLIA, SEED, ECC+3DES, and
62 // HMAC-SHA256 cipher suites are disabled.
63 if (info.symCipher == ssl_calg_camellia ||
64 info.symCipher == ssl_calg_seed ||
65 (info.symCipher == ssl_calg_3des && info.keaType != ssl_kea_rsa) ||
66 info.authAlgorithm == ssl_auth_dsa ||
67 info.macAlgorithm == ssl_hmac_sha256 ||
69 strcmp(info.keaTypeName, "ECDH") == 0) {
73 // We also disable ChaCha20 based cipher suites for now because we
74 // aren't quite ready to use them in M32.
75 if (info.symCipher == ssl_calg_chacha20)
78 if (ssl_ciphers[i] == TLS_DHE_DSS_WITH_AES_128_CBC_SHA) {
79 // Enabled to allow servers with only a DSA certificate to function.
82 SSL_CipherPrefSetDefault(ssl_ciphers[i], enabled);
87 SSL_OptionSetDefault(SSL_SECURITY, PR_TRUE);
89 // All other SSL options are set per-session by SSLClientSocket and
93 ~NSSSSLInitSingleton() {
94 // Have to clear the cache, or NSS_Shutdown fails with SEC_ERROR_BUSY.
95 SSL_ClearSessionCache();
99 static base::LazyInstance<NSSSSLInitSingleton> g_nss_ssl_init_singleton =
100 LAZY_INSTANCE_INITIALIZER;
102 // Initialize the NSS SSL library if it isn't already initialized. This must
103 // be called before any other NSS SSL functions. This function is
104 // thread-safe, and the NSS SSL library will only ever be initialized once.
105 // The NSS SSL library will be properly shut down on program exit.
106 void EnsureNSSSSLInit() {
107 // Initializing SSL causes us to do blocking IO.
108 // Temporarily allow it until we fix
109 // http://code.google.com/p/chromium/issues/detail?id=59847
110 base::ThreadRestrictions::ScopedAllowIO allow_io;
112 g_nss_ssl_init_singleton.Get();
115 // Map a Chromium net error code to an NSS error code.
116 // See _MD_unix_map_default_error in the NSS source
117 // tree for inspiration.
118 PRErrorCode MapErrorToNSS(int result) {
124 return PR_WOULD_BLOCK_ERROR;
125 case ERR_ACCESS_DENIED:
126 case ERR_NETWORK_ACCESS_DENIED:
127 // For connect, this could be mapped to PR_ADDRESS_NOT_SUPPORTED_ERROR.
128 return PR_NO_ACCESS_RIGHTS_ERROR;
129 case ERR_NOT_IMPLEMENTED:
130 return PR_NOT_IMPLEMENTED_ERROR;
131 case ERR_SOCKET_NOT_CONNECTED:
132 return PR_NOT_CONNECTED_ERROR;
133 case ERR_INTERNET_DISCONNECTED: // Equivalent to ENETDOWN.
134 return PR_NETWORK_UNREACHABLE_ERROR; // Best approximation.
135 case ERR_CONNECTION_TIMED_OUT:
137 return PR_IO_TIMEOUT_ERROR;
138 case ERR_CONNECTION_RESET:
139 return PR_CONNECT_RESET_ERROR;
140 case ERR_CONNECTION_ABORTED:
141 return PR_CONNECT_ABORTED_ERROR;
142 case ERR_CONNECTION_REFUSED:
143 return PR_CONNECT_REFUSED_ERROR;
144 case ERR_ADDRESS_UNREACHABLE:
145 return PR_HOST_UNREACHABLE_ERROR; // Also PR_NETWORK_UNREACHABLE_ERROR.
146 case ERR_ADDRESS_INVALID:
147 return PR_ADDRESS_NOT_AVAILABLE_ERROR;
148 case ERR_NAME_NOT_RESOLVED:
149 return PR_DIRECTORY_LOOKUP_ERROR;
151 LOG(WARNING) << "MapErrorToNSS " << result
152 << " mapped to PR_UNKNOWN_ERROR";
153 return PR_UNKNOWN_ERROR;
157 // The default error mapping function.
158 // Maps an NSS error code to a network error code.
159 int MapNSSError(PRErrorCode err) {
160 // TODO(port): fill this out as we learn what's important
162 case PR_WOULD_BLOCK_ERROR:
163 return ERR_IO_PENDING;
164 case PR_ADDRESS_NOT_SUPPORTED_ERROR: // For connect.
165 case PR_NO_ACCESS_RIGHTS_ERROR:
166 return ERR_ACCESS_DENIED;
167 case PR_IO_TIMEOUT_ERROR:
168 return ERR_TIMED_OUT;
169 case PR_CONNECT_RESET_ERROR:
170 return ERR_CONNECTION_RESET;
171 case PR_CONNECT_ABORTED_ERROR:
172 return ERR_CONNECTION_ABORTED;
173 case PR_CONNECT_REFUSED_ERROR:
174 return ERR_CONNECTION_REFUSED;
175 case PR_NOT_CONNECTED_ERROR:
176 return ERR_SOCKET_NOT_CONNECTED;
177 case PR_HOST_UNREACHABLE_ERROR:
178 case PR_NETWORK_UNREACHABLE_ERROR:
179 return ERR_ADDRESS_UNREACHABLE;
180 case PR_ADDRESS_NOT_AVAILABLE_ERROR:
181 return ERR_ADDRESS_INVALID;
182 case PR_INVALID_ARGUMENT_ERROR:
183 return ERR_INVALID_ARGUMENT;
184 case PR_END_OF_FILE_ERROR:
185 return ERR_CONNECTION_CLOSED;
186 case PR_NOT_IMPLEMENTED_ERROR:
187 return ERR_NOT_IMPLEMENTED;
189 case SEC_ERROR_LIBRARY_FAILURE:
190 return ERR_UNEXPECTED;
191 case SEC_ERROR_INVALID_ARGS:
192 return ERR_INVALID_ARGUMENT;
193 case SEC_ERROR_NO_MEMORY:
194 return ERR_OUT_OF_MEMORY;
195 case SEC_ERROR_NO_KEY:
196 return ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY;
197 case SEC_ERROR_INVALID_KEY:
198 case SSL_ERROR_SIGN_HASHES_FAILURE:
199 LOG(ERROR) << "ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED: NSS error " << err
200 << ", OS error " << PR_GetOSError();
201 return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED;
202 // A handshake (initial or renegotiation) may fail because some signature
203 // (for example, the signature in the ServerKeyExchange message for an
204 // ephemeral Diffie-Hellman cipher suite) is invalid.
205 case SEC_ERROR_BAD_SIGNATURE:
206 return ERR_SSL_PROTOCOL_ERROR;
208 case SSL_ERROR_SSL_DISABLED:
209 return ERR_NO_SSL_VERSIONS_ENABLED;
210 case SSL_ERROR_NO_CYPHER_OVERLAP:
211 case SSL_ERROR_PROTOCOL_VERSION_ALERT:
212 case SSL_ERROR_UNSUPPORTED_VERSION:
213 return ERR_SSL_VERSION_OR_CIPHER_MISMATCH;
214 case SSL_ERROR_HANDSHAKE_FAILURE_ALERT:
215 case SSL_ERROR_HANDSHAKE_UNEXPECTED_ALERT:
216 case SSL_ERROR_ILLEGAL_PARAMETER_ALERT:
217 return ERR_SSL_PROTOCOL_ERROR;
218 case SSL_ERROR_DECOMPRESSION_FAILURE_ALERT:
219 return ERR_SSL_DECOMPRESSION_FAILURE_ALERT;
220 case SSL_ERROR_BAD_MAC_ALERT:
221 return ERR_SSL_BAD_RECORD_MAC_ALERT;
222 case SSL_ERROR_DECRYPT_ERROR_ALERT:
223 return ERR_SSL_DECRYPT_ERROR_ALERT;
224 case SSL_ERROR_UNSAFE_NEGOTIATION:
225 return ERR_SSL_UNSAFE_NEGOTIATION;
226 case SSL_ERROR_WEAK_SERVER_EPHEMERAL_DH_KEY:
227 return ERR_SSL_WEAK_SERVER_EPHEMERAL_DH_KEY;
228 case SSL_ERROR_HANDSHAKE_NOT_COMPLETED:
229 return ERR_SSL_HANDSHAKE_NOT_COMPLETED;
230 case SEC_ERROR_BAD_KEY:
231 case SSL_ERROR_EXTRACT_PUBLIC_KEY_FAILURE:
232 // TODO(wtc): the following errors may also occur in contexts unrelated
233 // to the peer's public key. We should add new error codes for them, or
234 // map them to ERR_SSL_BAD_PEER_PUBLIC_KEY only in the right context.
235 // General unsupported/unknown key algorithm error.
236 case SEC_ERROR_UNSUPPORTED_KEYALG:
237 // General DER decoding errors.
238 case SEC_ERROR_BAD_DER:
239 case SEC_ERROR_EXTRA_INPUT:
240 return ERR_SSL_BAD_PEER_PUBLIC_KEY;
241 // During renegotiation, the server presented a different certificate than
243 case SSL_ERROR_WRONG_CERTIFICATE:
244 return ERR_SSL_SERVER_CERT_CHANGED;
247 if (IS_SSL_ERROR(err)) {
248 LOG(WARNING) << "Unknown SSL error " << err
249 << " mapped to net::ERR_SSL_PROTOCOL_ERROR";
250 return ERR_SSL_PROTOCOL_ERROR;
252 LOG(WARNING) << "Unknown error " << err << " mapped to net::ERR_FAILED";
258 // Returns parameters to attach to the NetLog when we receive an error in
259 // response to a call to an NSS function. Used instead of
260 // NetLogSSLErrorCallback with events of type TYPE_SSL_NSS_ERROR.
261 base::Value* NetLogSSLFailedNSSFunctionCallback(
262 const char* function,
265 NetLog::LogLevel /* log_level */) {
266 base::DictionaryValue* dict = new base::DictionaryValue();
267 dict->SetString("function", function);
268 if (param[0] != '\0')
269 dict->SetString("param", param);
270 dict->SetInteger("ssl_lib_error", ssl_lib_error);
274 void LogFailedNSSFunction(const BoundNetLog& net_log,
275 const char* function,
280 NetLog::TYPE_SSL_NSS_ERROR,
281 base::Bind(&NetLogSSLFailedNSSFunctionCallback,
282 function, param, PR_GetError()));