| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/nss_ssl_util.h" | 5 #include "net/socket/nss_ssl_util.h" |
| 6 | 6 |
| 7 #include <nss.h> | 7 #include <nss.h> |
| 8 #include <secerr.h> | 8 #include <secerr.h> |
| 9 #include <ssl.h> | 9 #include <ssl.h> |
| 10 #include <sslerr.h> | 10 #include <sslerr.h> |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 | 31 |
| 32 namespace { | 32 namespace { |
| 33 | 33 |
| 34 // CiphersRemove takes a zero-terminated array of cipher suite ids in | 34 // CiphersRemove takes a zero-terminated array of cipher suite ids in |
| 35 // |to_remove| and sets every instance of them in |ciphers| to zero. It returns | 35 // |to_remove| and sets every instance of them in |ciphers| to zero. It returns |
| 36 // true if it found and removed every element of |to_remove|. It assumes that | 36 // true if it found and removed every element of |to_remove|. It assumes that |
| 37 // there are no duplicates in |ciphers| nor in |to_remove|. | 37 // there are no duplicates in |ciphers| nor in |to_remove|. |
| 38 bool CiphersRemove(const uint16* to_remove, uint16* ciphers, size_t num) { | 38 bool CiphersRemove(const uint16* to_remove, uint16* ciphers, size_t num) { |
| 39 size_t i, found = 0; | 39 size_t i, found = 0; |
| 40 | 40 |
| 41 for (i = 0; ; i++) { | 41 for (i = 0;; i++) { |
| 42 if (to_remove[i] == 0) | 42 if (to_remove[i] == 0) |
| 43 break; | 43 break; |
| 44 | 44 |
| 45 for (size_t j = 0; j < num; j++) { | 45 for (size_t j = 0; j < num; j++) { |
| 46 if (to_remove[i] == ciphers[j]) { | 46 if (to_remove[i] == ciphers[j]) { |
| 47 ciphers[j] = 0; | 47 ciphers[j] = 0; |
| 48 found++; | 48 found++; |
| 49 break; | 49 break; |
| 50 } | 50 } |
| 51 } | 51 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 63 for (size_t i = num - 1; i < num; i--) { | 63 for (size_t i = num - 1; i < num; i--) { |
| 64 if (ciphers[i] == 0) | 64 if (ciphers[i] == 0) |
| 65 continue; | 65 continue; |
| 66 ciphers[j--] = ciphers[i]; | 66 ciphers[j--] = ciphers[i]; |
| 67 } | 67 } |
| 68 } | 68 } |
| 69 | 69 |
| 70 // CiphersCopy copies the zero-terminated array |in| to |out|. It returns the | 70 // CiphersCopy copies the zero-terminated array |in| to |out|. It returns the |
| 71 // number of cipher suite ids copied. | 71 // number of cipher suite ids copied. |
| 72 size_t CiphersCopy(const uint16* in, uint16* out) { | 72 size_t CiphersCopy(const uint16* in, uint16* out) { |
| 73 for (size_t i = 0; ; i++) { | 73 for (size_t i = 0;; i++) { |
| 74 if (in[i] == 0) | 74 if (in[i] == 0) |
| 75 return i; | 75 return i; |
| 76 out[i] = in[i]; | 76 out[i] = in[i]; |
| 77 } | 77 } |
| 78 } | 78 } |
| 79 | 79 |
| 80 } // anonymous namespace | 80 } // anonymous namespace |
| 81 | 81 |
| 82 namespace net { | 82 namespace net { |
| 83 | 83 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 96 // ciphersuites as a hint to send an ECDSA certificate. | 96 // ciphersuites as a hint to send an ECDSA certificate. |
| 97 bool disableECDSA = false; | 97 bool disableECDSA = false; |
| 98 #if defined(OS_WIN) | 98 #if defined(OS_WIN) |
| 99 if (base::win::GetVersion() < base::win::VERSION_VISTA) | 99 if (base::win::GetVersion() < base::win::VERSION_VISTA) |
| 100 disableECDSA = true; | 100 disableECDSA = true; |
| 101 #endif | 101 #endif |
| 102 | 102 |
| 103 // Explicitly enable exactly those ciphers with keys of at least 80 bits | 103 // Explicitly enable exactly those ciphers with keys of at least 80 bits |
| 104 for (int i = 0; i < num_ciphers; i++) { | 104 for (int i = 0; i < num_ciphers; i++) { |
| 105 SSLCipherSuiteInfo info; | 105 SSLCipherSuiteInfo info; |
| 106 if (SSL_GetCipherSuiteInfo(ssl_ciphers[i], &info, | 106 if (SSL_GetCipherSuiteInfo(ssl_ciphers[i], &info, sizeof(info)) == |
| 107 sizeof(info)) == SECSuccess) { | 107 SECSuccess) { |
| 108 bool enabled = info.effectiveKeyBits >= 80; | 108 bool enabled = info.effectiveKeyBits >= 80; |
| 109 if (info.authAlgorithm == ssl_auth_ecdsa && disableECDSA) | 109 if (info.authAlgorithm == ssl_auth_ecdsa && disableECDSA) |
| 110 enabled = false; | 110 enabled = false; |
| 111 | 111 |
| 112 // Trim the list of cipher suites in order to keep the size of the | 112 // Trim the list of cipher suites in order to keep the size of the |
| 113 // ClientHello down. DSS, ECDH, CAMELLIA, SEED, ECC+3DES, and | 113 // ClientHello down. DSS, ECDH, CAMELLIA, SEED, ECC+3DES, and |
| 114 // HMAC-SHA256 cipher suites are disabled. | 114 // HMAC-SHA256 cipher suites are disabled. |
| 115 if (info.symCipher == ssl_calg_camellia || | 115 if (info.symCipher == ssl_calg_camellia || |
| 116 info.symCipher == ssl_calg_seed || | 116 info.symCipher == ssl_calg_seed || |
| 117 (info.symCipher == ssl_calg_3des && info.keaType != ssl_kea_rsa) || | 117 (info.symCipher == ssl_calg_3des && info.keaType != ssl_kea_rsa) || |
| 118 info.authAlgorithm == ssl_auth_dsa || | 118 info.authAlgorithm == ssl_auth_dsa || |
| 119 info.macAlgorithm == ssl_hmac_sha256 || | 119 info.macAlgorithm == ssl_hmac_sha256 || info.nonStandard || |
| 120 info.nonStandard || | |
| 121 strcmp(info.keaTypeName, "ECDH") == 0) { | 120 strcmp(info.keaTypeName, "ECDH") == 0) { |
| 122 enabled = false; | 121 enabled = false; |
| 123 } | 122 } |
| 124 | 123 |
| 125 if (ssl_ciphers[i] == TLS_DHE_DSS_WITH_AES_128_CBC_SHA) { | 124 if (ssl_ciphers[i] == TLS_DHE_DSS_WITH_AES_128_CBC_SHA) { |
| 126 // Enabled to allow servers with only a DSA certificate to function. | 125 // Enabled to allow servers with only a DSA certificate to function. |
| 127 enabled = true; | 126 enabled = true; |
| 128 } | 127 } |
| 129 SSL_CipherPrefSetDefault(ssl_ciphers[i], enabled); | 128 SSL_CipherPrefSetDefault(ssl_ciphers[i], enabled); |
| 130 } | 129 } |
| 131 } | 130 } |
| 132 | 131 |
| 133 // Enable SSL. | 132 // Enable SSL. |
| 134 SSL_OptionSetDefault(SSL_SECURITY, PR_TRUE); | 133 SSL_OptionSetDefault(SSL_SECURITY, PR_TRUE); |
| 135 | 134 |
| 136 // Calculate the order of ciphers that we'll use for NSS sockets. (Note | 135 // Calculate the order of ciphers that we'll use for NSS sockets. (Note |
| 137 // that, even if a cipher is specified in the ordering, it must still be | 136 // that, even if a cipher is specified in the ordering, it must still be |
| 138 // enabled in order to be included in a ClientHello.) | 137 // enabled in order to be included in a ClientHello.) |
| 139 // | 138 // |
| 140 // Our top preference cipher suites are either forward-secret AES-GCM or | 139 // Our top preference cipher suites are either forward-secret AES-GCM or |
| 141 // forward-secret ChaCha20-Poly1305. If the local machine has AES-NI then | 140 // forward-secret ChaCha20-Poly1305. If the local machine has AES-NI then |
| 142 // we prefer AES-GCM, otherwise ChaCha20. The remainder of the cipher suite | 141 // we prefer AES-GCM, otherwise ChaCha20. The remainder of the cipher suite |
| 143 // preference is inheriented from NSS. */ | 142 // preference is inheriented from NSS. */ |
| 144 static const uint16 chacha_ciphers[] = { | 143 static const uint16 chacha_ciphers[] = { |
| 145 TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, | 144 TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, |
| 146 TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, | 145 TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, 0, |
| 147 0, | |
| 148 }; | 146 }; |
| 149 static const uint16 aes_gcm_ciphers[] = { | 147 static const uint16 aes_gcm_ciphers[] = { |
| 150 TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, | 148 TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 151 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, | 149 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, |
| 152 TLS_DHE_RSA_WITH_AES_128_GCM_SHA256, | 150 TLS_DHE_RSA_WITH_AES_128_GCM_SHA256, 0, |
| 153 0, | |
| 154 }; | 151 }; |
| 155 scoped_ptr<uint16[]> ciphers(new uint16[num_ciphers]); | 152 scoped_ptr<uint16[]> ciphers(new uint16[num_ciphers]); |
| 156 memcpy(ciphers.get(), ssl_ciphers, sizeof(uint16)*num_ciphers); | 153 memcpy(ciphers.get(), ssl_ciphers, sizeof(uint16) * num_ciphers); |
| 157 | 154 |
| 158 if (CiphersRemove(chacha_ciphers, ciphers.get(), num_ciphers) && | 155 if (CiphersRemove(chacha_ciphers, ciphers.get(), num_ciphers) && |
| 159 CiphersRemove(aes_gcm_ciphers, ciphers.get(), num_ciphers)) { | 156 CiphersRemove(aes_gcm_ciphers, ciphers.get(), num_ciphers)) { |
| 160 CiphersCompact(ciphers.get(), num_ciphers); | 157 CiphersCompact(ciphers.get(), num_ciphers); |
| 161 | 158 |
| 162 const uint16* preference_ciphers = chacha_ciphers; | 159 const uint16* preference_ciphers = chacha_ciphers; |
| 163 const uint16* other_ciphers = aes_gcm_ciphers; | 160 const uint16* other_ciphers = aes_gcm_ciphers; |
| 164 base::CPU cpu; | 161 base::CPU cpu; |
| 165 | 162 |
| 166 if (cpu.has_aesni() && cpu.has_avx()) { | 163 if (cpu.has_aesni() && cpu.has_avx()) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 179 PR_Close(model_fd_); | 176 PR_Close(model_fd_); |
| 180 model_fd_ = NULL; | 177 model_fd_ = NULL; |
| 181 } | 178 } |
| 182 } | 179 } |
| 183 } | 180 } |
| 184 | 181 |
| 185 // All other SSL options are set per-session by SSLClientSocket and | 182 // All other SSL options are set per-session by SSLClientSocket and |
| 186 // SSLServerSocket. | 183 // SSLServerSocket. |
| 187 } | 184 } |
| 188 | 185 |
| 189 PRFileDesc* GetModelSocket() { | 186 PRFileDesc* GetModelSocket() { return model_fd_; } |
| 190 return model_fd_; | |
| 191 } | |
| 192 | 187 |
| 193 ~NSSSSLInitSingleton() { | 188 ~NSSSSLInitSingleton() { |
| 194 // Have to clear the cache, or NSS_Shutdown fails with SEC_ERROR_BUSY. | 189 // Have to clear the cache, or NSS_Shutdown fails with SEC_ERROR_BUSY. |
| 195 SSL_ClearSessionCache(); | 190 SSL_ClearSessionCache(); |
| 196 if (model_fd_) | 191 if (model_fd_) |
| 197 PR_Close(model_fd_); | 192 PR_Close(model_fd_); |
| 198 } | 193 } |
| 199 | 194 |
| 200 private: | 195 private: |
| 201 PRFileDesc* model_fd_; | 196 PRFileDesc* model_fd_; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 218 } | 213 } |
| 219 | 214 |
| 220 PRFileDesc* GetNSSModelSocket() { | 215 PRFileDesc* GetNSSModelSocket() { |
| 221 return g_nss_ssl_init_singleton.Get().GetModelSocket(); | 216 return g_nss_ssl_init_singleton.Get().GetModelSocket(); |
| 222 } | 217 } |
| 223 | 218 |
| 224 // Map a Chromium net error code to an NSS error code. | 219 // Map a Chromium net error code to an NSS error code. |
| 225 // See _MD_unix_map_default_error in the NSS source | 220 // See _MD_unix_map_default_error in the NSS source |
| 226 // tree for inspiration. | 221 // tree for inspiration. |
| 227 PRErrorCode MapErrorToNSS(int result) { | 222 PRErrorCode MapErrorToNSS(int result) { |
| 228 if (result >=0) | 223 if (result >= 0) |
| 229 return result; | 224 return result; |
| 230 | 225 |
| 231 switch (result) { | 226 switch (result) { |
| 232 case ERR_IO_PENDING: | 227 case ERR_IO_PENDING: |
| 233 return PR_WOULD_BLOCK_ERROR; | 228 return PR_WOULD_BLOCK_ERROR; |
| 234 case ERR_ACCESS_DENIED: | 229 case ERR_ACCESS_DENIED: |
| 235 case ERR_NETWORK_ACCESS_DENIED: | 230 case ERR_NETWORK_ACCESS_DENIED: |
| 236 // For connect, this could be mapped to PR_ADDRESS_NOT_SUPPORTED_ERROR. | 231 // For connect, this could be mapped to PR_ADDRESS_NOT_SUPPORTED_ERROR. |
| 237 return PR_NO_ACCESS_RIGHTS_ERROR; | 232 return PR_NO_ACCESS_RIGHTS_ERROR; |
| 238 case ERR_NOT_IMPLEMENTED: | 233 case ERR_NOT_IMPLEMENTED: |
| 239 return PR_NOT_IMPLEMENTED_ERROR; | 234 return PR_NOT_IMPLEMENTED_ERROR; |
| 240 case ERR_SOCKET_NOT_CONNECTED: | 235 case ERR_SOCKET_NOT_CONNECTED: |
| 241 return PR_NOT_CONNECTED_ERROR; | 236 return PR_NOT_CONNECTED_ERROR; |
| 242 case ERR_INTERNET_DISCONNECTED: // Equivalent to ENETDOWN. | 237 case ERR_INTERNET_DISCONNECTED: // Equivalent to ENETDOWN. |
| 243 return PR_NETWORK_UNREACHABLE_ERROR; // Best approximation. | 238 return PR_NETWORK_UNREACHABLE_ERROR; // Best approximation. |
| 244 case ERR_CONNECTION_TIMED_OUT: | 239 case ERR_CONNECTION_TIMED_OUT: |
| 245 case ERR_TIMED_OUT: | 240 case ERR_TIMED_OUT: |
| 246 return PR_IO_TIMEOUT_ERROR; | 241 return PR_IO_TIMEOUT_ERROR; |
| 247 case ERR_CONNECTION_RESET: | 242 case ERR_CONNECTION_RESET: |
| 248 return PR_CONNECT_RESET_ERROR; | 243 return PR_CONNECT_RESET_ERROR; |
| 249 case ERR_CONNECTION_ABORTED: | 244 case ERR_CONNECTION_ABORTED: |
| 250 return PR_CONNECT_ABORTED_ERROR; | 245 return PR_CONNECT_ABORTED_ERROR; |
| 251 case ERR_CONNECTION_REFUSED: | 246 case ERR_CONNECTION_REFUSED: |
| 252 return PR_CONNECT_REFUSED_ERROR; | 247 return PR_CONNECT_REFUSED_ERROR; |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 388 return dict; | 383 return dict; |
| 389 } | 384 } |
| 390 | 385 |
| 391 void LogFailedNSSFunction(const BoundNetLog& net_log, | 386 void LogFailedNSSFunction(const BoundNetLog& net_log, |
| 392 const char* function, | 387 const char* function, |
| 393 const char* param) { | 388 const char* param) { |
| 394 DCHECK(function); | 389 DCHECK(function); |
| 395 DCHECK(param); | 390 DCHECK(param); |
| 396 net_log.AddEvent( | 391 net_log.AddEvent( |
| 397 NetLog::TYPE_SSL_NSS_ERROR, | 392 NetLog::TYPE_SSL_NSS_ERROR, |
| 398 base::Bind(&NetLogSSLFailedNSSFunctionCallback, | 393 base::Bind( |
| 399 function, param, PR_GetError())); | 394 &NetLogSSLFailedNSSFunctionCallback, function, param, PR_GetError())); |
| 400 } | 395 } |
| 401 | 396 |
| 402 } // namespace net | 397 } // namespace net |
| OLD | NEW |