We started redesigning GpuMemoryBuffer interface to handle multiple buffers [0].
[chromium-blink-merge.git] / net / socket / nss_ssl_util.cc
blobe0a2257187f406f2525911a825aa6a08d9654519
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"
7 #include <nss.h>
8 #include <secerr.h>
9 #include <ssl.h>
10 #include <sslerr.h>
11 #include <sslproto.h>
13 #include <string>
15 #include "base/bind.h"
16 #include "base/cpu.h"
17 #include "base/lazy_instance.h"
18 #include "base/logging.h"
19 #include "base/memory/singleton.h"
20 #include "base/threading/thread_restrictions.h"
21 #include "base/values.h"
22 #include "build/build_config.h"
23 #include "crypto/nss_util.h"
24 #include "net/base/net_errors.h"
25 #include "net/base/nss_memio.h"
26 #include "net/log/net_log.h"
28 #if defined(OS_WIN)
29 #include "base/win/windows_version.h"
30 #endif
32 namespace net {
34 namespace {
36 // CiphersRemove takes a zero-terminated array of cipher suite ids in
37 // |to_remove| and sets every instance of them in |ciphers| to zero. It returns
38 // true if it found and removed every element of |to_remove|. It assumes that
39 // there are no duplicates in |ciphers| nor in |to_remove|.
40 bool CiphersRemove(const uint16* to_remove, uint16* ciphers, size_t num) {
41 size_t i, found = 0;
43 for (i = 0; ; i++) {
44 if (to_remove[i] == 0)
45 break;
47 for (size_t j = 0; j < num; j++) {
48 if (to_remove[i] == ciphers[j]) {
49 ciphers[j] = 0;
50 found++;
51 break;
56 return found == i;
59 // CiphersCompact takes an array of cipher suite ids in |ciphers|, where some
60 // entries are zero, and moves the entries so that all the non-zero elements
61 // are compacted at the end of the array.
62 void CiphersCompact(uint16* ciphers, size_t num) {
63 size_t j = num - 1;
65 for (size_t i = num - 1; i < num; i--) {
66 if (ciphers[i] == 0)
67 continue;
68 ciphers[j--] = ciphers[i];
72 // CiphersCopy copies the zero-terminated array |in| to |out|. It returns the
73 // number of cipher suite ids copied.
74 size_t CiphersCopy(const uint16* in, uint16* out) {
75 for (size_t i = 0; ; i++) {
76 if (in[i] == 0)
77 return i;
78 out[i] = in[i];
82 base::Value* NetLogSSLErrorCallback(int net_error,
83 int ssl_lib_error,
84 NetLog::LogLevel /* log_level */) {
85 base::DictionaryValue* dict = new base::DictionaryValue();
86 dict->SetInteger("net_error", net_error);
87 if (ssl_lib_error)
88 dict->SetInteger("ssl_lib_error", ssl_lib_error);
89 return dict;
92 class NSSSSLInitSingleton {
93 public:
94 NSSSSLInitSingleton() : model_fd_(NULL) {
95 crypto::EnsureNSSInit();
97 NSS_SetDomesticPolicy();
99 const PRUint16* const ssl_ciphers = SSL_GetImplementedCiphers();
100 const PRUint16 num_ciphers = SSL_GetNumImplementedCiphers();
102 // Disable ECDSA cipher suites on platforms that do not support ECDSA
103 // signed certificates, as servers may use the presence of such
104 // ciphersuites as a hint to send an ECDSA certificate.
105 bool disableECDSA = false;
106 #if defined(OS_WIN)
107 if (base::win::GetVersion() < base::win::VERSION_VISTA)
108 disableECDSA = true;
109 #endif
111 // Explicitly enable exactly those ciphers with keys of at least 80 bits.
112 for (int i = 0; i < num_ciphers; i++) {
113 SSLCipherSuiteInfo info;
114 if (SSL_GetCipherSuiteInfo(ssl_ciphers[i], &info,
115 sizeof(info)) == SECSuccess) {
116 bool enabled = info.effectiveKeyBits >= 80;
117 if (info.authAlgorithm == ssl_auth_ecdsa && disableECDSA)
118 enabled = false;
120 // Trim the list of cipher suites in order to keep the size of the
121 // ClientHello down. DSS, ECDH, CAMELLIA, SEED, ECC+3DES, and
122 // HMAC-SHA256 cipher suites are disabled.
123 if (info.symCipher == ssl_calg_camellia ||
124 info.symCipher == ssl_calg_seed ||
125 (info.symCipher == ssl_calg_3des && info.keaType != ssl_kea_rsa) ||
126 info.authAlgorithm == ssl_auth_dsa ||
127 info.macAlgorithm == ssl_hmac_sha256 ||
128 info.nonStandard ||
129 strcmp(info.keaTypeName, "ECDH") == 0) {
130 enabled = false;
133 SSL_CipherPrefSetDefault(ssl_ciphers[i], enabled);
137 // Enable SSL.
138 SSL_OptionSetDefault(SSL_SECURITY, PR_TRUE);
140 // Calculate the order of ciphers that we'll use for NSS sockets. (Note
141 // that, even if a cipher is specified in the ordering, it must still be
142 // enabled in order to be included in a ClientHello.)
144 // Our top preference cipher suites are either forward-secret AES-GCM or
145 // forward-secret ChaCha20-Poly1305. If the local machine has AES-NI then
146 // we prefer AES-GCM, otherwise ChaCha20. The remainder of the cipher suite
147 // preference is inheriented from NSS. */
148 static const uint16 chacha_ciphers[] = {
149 TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
150 TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
153 static const uint16 aes_gcm_ciphers[] = {
154 TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
155 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
156 TLS_DHE_RSA_WITH_AES_128_GCM_SHA256,
159 scoped_ptr<uint16[]> ciphers(new uint16[num_ciphers]);
160 memcpy(ciphers.get(), ssl_ciphers, sizeof(uint16)*num_ciphers);
162 if (CiphersRemove(chacha_ciphers, ciphers.get(), num_ciphers) &&
163 CiphersRemove(aes_gcm_ciphers, ciphers.get(), num_ciphers)) {
164 CiphersCompact(ciphers.get(), num_ciphers);
166 const uint16* preference_ciphers = chacha_ciphers;
167 const uint16* other_ciphers = aes_gcm_ciphers;
168 base::CPU cpu;
170 if (cpu.has_aesni() && cpu.has_avx()) {
171 preference_ciphers = aes_gcm_ciphers;
172 other_ciphers = chacha_ciphers;
174 unsigned i = CiphersCopy(preference_ciphers, ciphers.get());
175 CiphersCopy(other_ciphers, &ciphers[i]);
177 if ((model_fd_ = memio_CreateIOLayer(1, 1)) == NULL ||
178 SSL_ImportFD(NULL, model_fd_) == NULL ||
179 SECSuccess !=
180 SSL_CipherOrderSet(model_fd_, ciphers.get(), num_ciphers)) {
181 NOTREACHED();
182 if (model_fd_) {
183 PR_Close(model_fd_);
184 model_fd_ = NULL;
189 // All other SSL options are set per-session by SSLClientSocket and
190 // SSLServerSocket.
193 PRFileDesc* GetModelSocket() {
194 return model_fd_;
197 ~NSSSSLInitSingleton() {
198 // Have to clear the cache, or NSS_Shutdown fails with SEC_ERROR_BUSY.
199 SSL_ClearSessionCache();
200 if (model_fd_)
201 PR_Close(model_fd_);
204 private:
205 PRFileDesc* model_fd_;
208 base::LazyInstance<NSSSSLInitSingleton>::Leaky g_nss_ssl_init_singleton =
209 LAZY_INSTANCE_INITIALIZER;
211 } // anonymous namespace
213 // Initialize the NSS SSL library if it isn't already initialized. This must
214 // be called before any other NSS SSL functions. This function is
215 // thread-safe, and the NSS SSL library will only ever be initialized once.
216 // The NSS SSL library will be properly shut down on program exit.
217 void EnsureNSSSSLInit() {
218 // Initializing SSL causes us to do blocking IO.
219 // Temporarily allow it until we fix
220 // http://code.google.com/p/chromium/issues/detail?id=59847
221 base::ThreadRestrictions::ScopedAllowIO allow_io;
223 g_nss_ssl_init_singleton.Get();
226 PRFileDesc* GetNSSModelSocket() {
227 return g_nss_ssl_init_singleton.Get().GetModelSocket();
230 // Map a Chromium net error code to an NSS error code.
231 // See _MD_unix_map_default_error in the NSS source
232 // tree for inspiration.
233 PRErrorCode MapErrorToNSS(int result) {
234 if (result >=0)
235 return result;
237 switch (result) {
238 case ERR_IO_PENDING:
239 return PR_WOULD_BLOCK_ERROR;
240 case ERR_ACCESS_DENIED:
241 case ERR_NETWORK_ACCESS_DENIED:
242 // For connect, this could be mapped to PR_ADDRESS_NOT_SUPPORTED_ERROR.
243 return PR_NO_ACCESS_RIGHTS_ERROR;
244 case ERR_NOT_IMPLEMENTED:
245 return PR_NOT_IMPLEMENTED_ERROR;
246 case ERR_SOCKET_NOT_CONNECTED:
247 return PR_NOT_CONNECTED_ERROR;
248 case ERR_INTERNET_DISCONNECTED: // Equivalent to ENETDOWN.
249 return PR_NETWORK_UNREACHABLE_ERROR; // Best approximation.
250 case ERR_CONNECTION_TIMED_OUT:
251 case ERR_TIMED_OUT:
252 return PR_IO_TIMEOUT_ERROR;
253 case ERR_CONNECTION_RESET:
254 return PR_CONNECT_RESET_ERROR;
255 case ERR_CONNECTION_ABORTED:
256 return PR_CONNECT_ABORTED_ERROR;
257 case ERR_CONNECTION_REFUSED:
258 return PR_CONNECT_REFUSED_ERROR;
259 case ERR_ADDRESS_UNREACHABLE:
260 return PR_HOST_UNREACHABLE_ERROR; // Also PR_NETWORK_UNREACHABLE_ERROR.
261 case ERR_ADDRESS_INVALID:
262 return PR_ADDRESS_NOT_AVAILABLE_ERROR;
263 case ERR_NAME_NOT_RESOLVED:
264 return PR_DIRECTORY_LOOKUP_ERROR;
265 default:
266 LOG(WARNING) << "MapErrorToNSS " << result
267 << " mapped to PR_UNKNOWN_ERROR";
268 return PR_UNKNOWN_ERROR;
272 // The default error mapping function.
273 // Maps an NSS error code to a network error code.
274 int MapNSSError(PRErrorCode err) {
275 // TODO(port): fill this out as we learn what's important
276 switch (err) {
277 case PR_WOULD_BLOCK_ERROR:
278 return ERR_IO_PENDING;
279 case PR_ADDRESS_NOT_SUPPORTED_ERROR: // For connect.
280 case PR_NO_ACCESS_RIGHTS_ERROR:
281 return ERR_ACCESS_DENIED;
282 case PR_IO_TIMEOUT_ERROR:
283 return ERR_TIMED_OUT;
284 case PR_CONNECT_RESET_ERROR:
285 return ERR_CONNECTION_RESET;
286 case PR_CONNECT_ABORTED_ERROR:
287 return ERR_CONNECTION_ABORTED;
288 case PR_CONNECT_REFUSED_ERROR:
289 return ERR_CONNECTION_REFUSED;
290 case PR_NOT_CONNECTED_ERROR:
291 return ERR_SOCKET_NOT_CONNECTED;
292 case PR_HOST_UNREACHABLE_ERROR:
293 case PR_NETWORK_UNREACHABLE_ERROR:
294 return ERR_ADDRESS_UNREACHABLE;
295 case PR_ADDRESS_NOT_AVAILABLE_ERROR:
296 return ERR_ADDRESS_INVALID;
297 case PR_INVALID_ARGUMENT_ERROR:
298 return ERR_INVALID_ARGUMENT;
299 case PR_END_OF_FILE_ERROR:
300 return ERR_CONNECTION_CLOSED;
301 case PR_NOT_IMPLEMENTED_ERROR:
302 return ERR_NOT_IMPLEMENTED;
304 case SEC_ERROR_LIBRARY_FAILURE:
305 return ERR_UNEXPECTED;
306 case SEC_ERROR_INVALID_ARGS:
307 return ERR_INVALID_ARGUMENT;
308 case SEC_ERROR_NO_MEMORY:
309 return ERR_OUT_OF_MEMORY;
310 case SEC_ERROR_NO_KEY:
311 return ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY;
312 case SEC_ERROR_INVALID_KEY:
313 case SSL_ERROR_SIGN_HASHES_FAILURE:
314 LOG(ERROR) << "ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED: NSS error " << err
315 << ", OS error " << PR_GetOSError();
316 return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED;
317 // A handshake (initial or renegotiation) may fail because some signature
318 // (for example, the signature in the ServerKeyExchange message for an
319 // ephemeral Diffie-Hellman cipher suite) is invalid.
320 case SEC_ERROR_BAD_SIGNATURE:
321 return ERR_SSL_PROTOCOL_ERROR;
323 case SSL_ERROR_SSL_DISABLED:
324 return ERR_NO_SSL_VERSIONS_ENABLED;
325 case SSL_ERROR_NO_CYPHER_OVERLAP:
326 case SSL_ERROR_PROTOCOL_VERSION_ALERT:
327 case SSL_ERROR_UNSUPPORTED_VERSION:
328 return ERR_SSL_VERSION_OR_CIPHER_MISMATCH;
329 case SSL_ERROR_HANDSHAKE_FAILURE_ALERT:
330 case SSL_ERROR_HANDSHAKE_UNEXPECTED_ALERT:
331 case SSL_ERROR_ILLEGAL_PARAMETER_ALERT:
332 return ERR_SSL_PROTOCOL_ERROR;
333 case SSL_ERROR_DECOMPRESSION_FAILURE_ALERT:
334 return ERR_SSL_DECOMPRESSION_FAILURE_ALERT;
335 case SSL_ERROR_BAD_MAC_ALERT:
336 return ERR_SSL_BAD_RECORD_MAC_ALERT;
337 case SSL_ERROR_DECRYPT_ERROR_ALERT:
338 return ERR_SSL_DECRYPT_ERROR_ALERT;
339 case SSL_ERROR_UNRECOGNIZED_NAME_ALERT:
340 return ERR_SSL_UNRECOGNIZED_NAME_ALERT;
341 case SSL_ERROR_UNSAFE_NEGOTIATION:
342 return ERR_SSL_UNSAFE_NEGOTIATION;
343 case SSL_ERROR_WEAK_SERVER_EPHEMERAL_DH_KEY:
344 return ERR_SSL_WEAK_SERVER_EPHEMERAL_DH_KEY;
345 case SSL_ERROR_HANDSHAKE_NOT_COMPLETED:
346 return ERR_SSL_HANDSHAKE_NOT_COMPLETED;
347 case SEC_ERROR_BAD_KEY:
348 case SSL_ERROR_EXTRACT_PUBLIC_KEY_FAILURE:
349 // TODO(wtc): the following errors may also occur in contexts unrelated
350 // to the peer's public key. We should add new error codes for them, or
351 // map them to ERR_SSL_BAD_PEER_PUBLIC_KEY only in the right context.
352 // General unsupported/unknown key algorithm error.
353 case SEC_ERROR_UNSUPPORTED_KEYALG:
354 // General DER decoding errors.
355 case SEC_ERROR_BAD_DER:
356 case SEC_ERROR_EXTRA_INPUT:
357 return ERR_SSL_BAD_PEER_PUBLIC_KEY;
358 // During renegotiation, the server presented a different certificate than
359 // was used earlier.
360 case SSL_ERROR_WRONG_CERTIFICATE:
361 return ERR_SSL_SERVER_CERT_CHANGED;
362 case SSL_ERROR_INAPPROPRIATE_FALLBACK_ALERT:
363 return ERR_SSL_INAPPROPRIATE_FALLBACK;
365 default: {
366 const char* err_name = PR_ErrorToName(err);
367 if (err_name == NULL)
368 err_name = "";
369 if (IS_SSL_ERROR(err)) {
370 LOG(WARNING) << "Unknown SSL error " << err << " (" << err_name << ")"
371 << " mapped to net::ERR_SSL_PROTOCOL_ERROR";
372 return ERR_SSL_PROTOCOL_ERROR;
374 LOG(WARNING) << "Unknown error " << err << " (" << err_name << ")"
375 << " mapped to net::ERR_FAILED";
376 return ERR_FAILED;
381 // Returns parameters to attach to the NetLog when we receive an error in
382 // response to a call to an NSS function. Used instead of
383 // NetLogSSLErrorCallback with events of type TYPE_SSL_NSS_ERROR.
384 base::Value* NetLogSSLFailedNSSFunctionCallback(
385 const char* function,
386 const char* param,
387 int ssl_lib_error,
388 NetLog::LogLevel /* log_level */) {
389 base::DictionaryValue* dict = new base::DictionaryValue();
390 dict->SetString("function", function);
391 if (param[0] != '\0')
392 dict->SetString("param", param);
393 dict->SetInteger("ssl_lib_error", ssl_lib_error);
394 return dict;
397 void LogFailedNSSFunction(const BoundNetLog& net_log,
398 const char* function,
399 const char* param) {
400 DCHECK(function);
401 DCHECK(param);
402 net_log.AddEvent(
403 NetLog::TYPE_SSL_NSS_ERROR,
404 base::Bind(&NetLogSSLFailedNSSFunctionCallback,
405 function, param, PR_GetError()));
408 NetLog::ParametersCallback CreateNetLogSSLErrorCallback(int net_error,
409 int ssl_lib_error) {
410 return base::Bind(&NetLogSSLErrorCallback, net_error, ssl_lib_error);
413 } // namespace net