certificate_checker.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //
  2. // Copyright (C) 2012 The Android Open Source Project
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. #include "update_engine/certificate_checker.h"
  17. #include <string>
  18. #include <base/logging.h>
  19. #include <base/strings/string_number_conversions.h>
  20. #include <base/strings/string_util.h>
  21. #include <base/strings/stringprintf.h>
  22. #include <curl/curl.h>
  23. #include <openssl/evp.h>
  24. #include <openssl/ssl.h>
  25. #include "update_engine/common/constants.h"
  26. #include "update_engine/common/prefs_interface.h"
  27. #include "update_engine/common/utils.h"
  28. using std::string;
  29. namespace chromeos_update_engine {
  30. bool OpenSSLWrapper::GetCertificateDigest(X509_STORE_CTX* x509_ctx,
  31. int* out_depth,
  32. unsigned int* out_digest_length,
  33. uint8_t* out_digest) const {
  34. TEST_AND_RETURN_FALSE(out_digest);
  35. X509* certificate = X509_STORE_CTX_get_current_cert(x509_ctx);
  36. TEST_AND_RETURN_FALSE(certificate);
  37. int depth = X509_STORE_CTX_get_error_depth(x509_ctx);
  38. if (out_depth)
  39. *out_depth = depth;
  40. unsigned int len;
  41. const EVP_MD* digest_function = EVP_sha256();
  42. bool success = X509_digest(certificate, digest_function, out_digest, &len);
  43. if (success && out_digest_length)
  44. *out_digest_length = len;
  45. return success;
  46. }
  47. // static
  48. CertificateChecker* CertificateChecker::cert_checker_singleton_ = nullptr;
  49. CertificateChecker::CertificateChecker(PrefsInterface* prefs,
  50. OpenSSLWrapper* openssl_wrapper)
  51. : prefs_(prefs), openssl_wrapper_(openssl_wrapper) {}
  52. CertificateChecker::~CertificateChecker() {
  53. if (cert_checker_singleton_ == this)
  54. cert_checker_singleton_ = nullptr;
  55. }
  56. void CertificateChecker::Init() {
  57. CHECK(cert_checker_singleton_ == nullptr);
  58. cert_checker_singleton_ = this;
  59. }
  60. // static
  61. CURLcode CertificateChecker::ProcessSSLContext(CURL* curl_handle,
  62. SSL_CTX* ssl_ctx,
  63. void* ptr) {
  64. ServerToCheck* server_to_check = reinterpret_cast<ServerToCheck*>(ptr);
  65. if (!cert_checker_singleton_) {
  66. DLOG(WARNING) << "No CertificateChecker singleton initialized.";
  67. return CURLE_FAILED_INIT;
  68. }
  69. // From here we set the SSL_CTX to another callback, from the openssl library,
  70. // which will be called after each server certificate is validated. However,
  71. // since openssl does not allow us to pass our own data pointer to the
  72. // callback, the certificate check will have to be done statically. Since we
  73. // need to know which update server we are using in order to check the
  74. // certificate, we hardcode Chrome OS's two known update servers here, and
  75. // define a different static callback for each. Since this code should only
  76. // run in official builds, this should not be a problem. However, if an update
  77. // server different from the ones listed here is used, the check will not
  78. // take place.
  79. int (*verify_callback)(int, X509_STORE_CTX*);
  80. switch (*server_to_check) {
  81. case ServerToCheck::kDownload:
  82. verify_callback = &CertificateChecker::VerifySSLCallbackDownload;
  83. break;
  84. case ServerToCheck::kUpdate:
  85. verify_callback = &CertificateChecker::VerifySSLCallbackUpdate;
  86. break;
  87. case ServerToCheck::kNone:
  88. verify_callback = nullptr;
  89. break;
  90. }
  91. SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER, verify_callback);
  92. return CURLE_OK;
  93. }
  94. // static
  95. int CertificateChecker::VerifySSLCallbackDownload(int preverify_ok,
  96. X509_STORE_CTX* x509_ctx) {
  97. return VerifySSLCallback(preverify_ok, x509_ctx, ServerToCheck::kDownload);
  98. }
  99. // static
  100. int CertificateChecker::VerifySSLCallbackUpdate(int preverify_ok,
  101. X509_STORE_CTX* x509_ctx) {
  102. return VerifySSLCallback(preverify_ok, x509_ctx, ServerToCheck::kUpdate);
  103. }
  104. // static
  105. int CertificateChecker::VerifySSLCallback(int preverify_ok,
  106. X509_STORE_CTX* x509_ctx,
  107. ServerToCheck server_to_check) {
  108. CHECK(cert_checker_singleton_ != nullptr);
  109. return cert_checker_singleton_->CheckCertificateChange(
  110. preverify_ok, x509_ctx, server_to_check)
  111. ? 1
  112. : 0;
  113. }
  114. bool CertificateChecker::CheckCertificateChange(int preverify_ok,
  115. X509_STORE_CTX* x509_ctx,
  116. ServerToCheck server_to_check) {
  117. TEST_AND_RETURN_FALSE(prefs_ != nullptr);
  118. // If pre-verification failed, we are not interested in the current
  119. // certificate. We store a report to UMA and just propagate the fail result.
  120. if (!preverify_ok) {
  121. NotifyCertificateChecked(server_to_check, CertificateCheckResult::kFailed);
  122. return false;
  123. }
  124. int depth;
  125. unsigned int digest_length;
  126. uint8_t digest[EVP_MAX_MD_SIZE];
  127. if (!openssl_wrapper_->GetCertificateDigest(
  128. x509_ctx, &depth, &digest_length, digest)) {
  129. LOG(WARNING) << "Failed to generate digest of X509 certificate "
  130. << "from update server.";
  131. NotifyCertificateChecked(server_to_check, CertificateCheckResult::kValid);
  132. return true;
  133. }
  134. // We convert the raw bytes of the digest to an hex string, for storage in
  135. // prefs.
  136. string digest_string = base::HexEncode(digest, digest_length);
  137. string storage_key = base::StringPrintf("%s-%d-%d",
  138. kPrefsUpdateServerCertificate,
  139. static_cast<int>(server_to_check),
  140. depth);
  141. string stored_digest;
  142. // If there's no stored certificate, we just store the current one and return.
  143. if (!prefs_->GetString(storage_key, &stored_digest)) {
  144. if (!prefs_->SetString(storage_key, digest_string)) {
  145. LOG(WARNING) << "Failed to store server certificate on storage key "
  146. << storage_key;
  147. }
  148. NotifyCertificateChecked(server_to_check, CertificateCheckResult::kValid);
  149. return true;
  150. }
  151. // Certificate changed, we store a report to UMA and store the most recent
  152. // certificate.
  153. if (stored_digest != digest_string) {
  154. if (!prefs_->SetString(storage_key, digest_string)) {
  155. LOG(WARNING) << "Failed to store server certificate on storage key "
  156. << storage_key;
  157. }
  158. LOG(INFO) << "Certificate changed from " << stored_digest << " to "
  159. << digest_string << ".";
  160. NotifyCertificateChecked(server_to_check,
  161. CertificateCheckResult::kValidChanged);
  162. return true;
  163. }
  164. NotifyCertificateChecked(server_to_check, CertificateCheckResult::kValid);
  165. // Since we don't perform actual SSL verification, we return success.
  166. return true;
  167. }
  168. void CertificateChecker::NotifyCertificateChecked(
  169. ServerToCheck server_to_check, CertificateCheckResult result) {
  170. if (observer_)
  171. observer_->CertificateChecked(server_to_check, result);
  172. }
  173. } // namespace chromeos_update_engine