| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "crypto/openssl_util.h" | 5 #include "crypto/openssl_util.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/feature_list.h" | |
| 13 #include "base/lazy_instance.h" | |
| 14 #include "base/logging.h" | 12 #include "base/logging.h" |
| 15 #include "base/strings/string_piece.h" | 13 #include "base/strings/string_piece.h" |
| 16 #include "third_party/boringssl/src/include/openssl/crypto.h" | 14 #include "third_party/boringssl/src/include/openssl/crypto.h" |
| 17 #include "third_party/boringssl/src/include/openssl/err.h" | 15 #include "third_party/boringssl/src/include/openssl/err.h" |
| 18 #include "third_party/boringssl/src/include/openssl/evp.h" | |
| 19 | 16 |
| 20 namespace crypto { | 17 namespace crypto { |
| 21 | 18 |
| 22 namespace { | 19 namespace { |
| 23 | 20 |
| 24 // Callback routine for OpenSSL to print error messages. |str| is a | 21 // Callback routine for OpenSSL to print error messages. |str| is a |
| 25 // NULL-terminated string of length |len| containing diagnostic information | 22 // NULL-terminated string of length |len| containing diagnostic information |
| 26 // such as the library, function and reason for the error, the file and line | 23 // such as the library, function and reason for the error, the file and line |
| 27 // where the error originated, plus potentially any context-specific | 24 // where the error originated, plus potentially any context-specific |
| 28 // information about the error. |context| contains a pointer to user-supplied | 25 // information about the error. |context| contains a pointer to user-supplied |
| 29 // data, which is currently unused. | 26 // data, which is currently unused. |
| 30 // If this callback returns a value <= 0, OpenSSL will stop processing the | 27 // If this callback returns a value <= 0, OpenSSL will stop processing the |
| 31 // error queue and return, otherwise it will continue calling this function | 28 // error queue and return, otherwise it will continue calling this function |
| 32 // until all errors have been removed from the queue. | 29 // until all errors have been removed from the queue. |
| 33 int OpenSSLErrorCallback(const char* str, size_t len, void* context) { | 30 int OpenSSLErrorCallback(const char* str, size_t len, void* context) { |
| 34 DVLOG(1) << "\t" << base::StringPiece(str, len); | 31 DVLOG(1) << "\t" << base::StringPiece(str, len); |
| 35 return 1; | 32 return 1; |
| 36 } | 33 } |
| 37 | 34 |
| 38 // TODO(davidben): Remove this after Chrome 61 is released to | |
| 39 // stable. https://crbug.com/735616. | |
| 40 const base::Feature kBuggyRSAParser{ | |
| 41 "BuggyRSAParser", base::FEATURE_DISABLED_BY_DEFAULT, | |
| 42 }; | |
| 43 | |
| 44 class BuggyRSAParser { | |
| 45 public: | |
| 46 BuggyRSAParser() { | |
| 47 EVP_set_buggy_rsa_parser(base::FeatureList::IsEnabled(kBuggyRSAParser)); | |
| 48 } | |
| 49 }; | |
| 50 | |
| 51 base::LazyInstance<BuggyRSAParser>::Leaky g_buggy_rsa_parser = | |
| 52 LAZY_INSTANCE_INITIALIZER; | |
| 53 | |
| 54 } // namespace | 35 } // namespace |
| 55 | 36 |
| 56 void EnsureOpenSSLInit() { | 37 void EnsureOpenSSLInit() { |
| 57 // CRYPTO_library_init may be safely called concurrently. | 38 // CRYPTO_library_init may be safely called concurrently. |
| 58 CRYPTO_library_init(); | 39 CRYPTO_library_init(); |
| 59 | |
| 60 // Configure the RSA parser. | |
| 61 g_buggy_rsa_parser.Get(); | |
| 62 } | 40 } |
| 63 | 41 |
| 64 void ClearOpenSSLERRStack(const tracked_objects::Location& location) { | 42 void ClearOpenSSLERRStack(const tracked_objects::Location& location) { |
| 65 if (DCHECK_IS_ON() && VLOG_IS_ON(1)) { | 43 if (DCHECK_IS_ON() && VLOG_IS_ON(1)) { |
| 66 uint32_t error_num = ERR_peek_error(); | 44 uint32_t error_num = ERR_peek_error(); |
| 67 if (error_num == 0) | 45 if (error_num == 0) |
| 68 return; | 46 return; |
| 69 | 47 |
| 70 std::string message; | 48 std::string message; |
| 71 location.Write(true, true, &message); | 49 location.Write(true, true, &message); |
| 72 DVLOG(1) << "OpenSSL ERR_get_error stack from " << message; | 50 DVLOG(1) << "OpenSSL ERR_get_error stack from " << message; |
| 73 ERR_print_errors_cb(&OpenSSLErrorCallback, NULL); | 51 ERR_print_errors_cb(&OpenSSLErrorCallback, NULL); |
| 74 } else { | 52 } else { |
| 75 ERR_clear_error(); | 53 ERR_clear_error(); |
| 76 } | 54 } |
| 77 } | 55 } |
| 78 | 56 |
| 79 } // namespace crypto | 57 } // namespace crypto |
| OLD | NEW |