aw_ssl_host_state_delegate.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright (c) 2014 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.
  4. #include "android_webview/browser/aw_ssl_host_state_delegate.h"
  5. #include "base/callback.h"
  6. #include "net/base/hash_value.h"
  7. using content::SSLHostStateDelegate;
  8. namespace android_webview {
  9. namespace internal {
  10. CertPolicy::CertPolicy() {
  11. }
  12. CertPolicy::~CertPolicy() {
  13. }
  14. // For an allowance, we consider a given |cert| to be a match to a saved
  15. // allowed cert if the |error| is an exact match to or subset of the errors
  16. // in the saved CertStatus.
  17. bool CertPolicy::Check(const net::X509Certificate& cert, int error) const {
  18. net::SHA256HashValue fingerprint = cert.CalculateChainFingerprint256();
  19. auto allowed_iter = allowed_.find(fingerprint);
  20. if ((allowed_iter != allowed_.end()) && (allowed_iter->second & error) &&
  21. ((allowed_iter->second & error) == error)) {
  22. return true;
  23. }
  24. return false;
  25. }
  26. void CertPolicy::Allow(const net::X509Certificate& cert, int error) {
  27. // If this same cert had already been saved with a different error status,
  28. // this will replace it with the new error status.
  29. net::SHA256HashValue fingerprint = cert.CalculateChainFingerprint256();
  30. allowed_[fingerprint] = error;
  31. }
  32. } // namespace internal
  33. AwSSLHostStateDelegate::AwSSLHostStateDelegate() {
  34. }
  35. AwSSLHostStateDelegate::~AwSSLHostStateDelegate() {
  36. }
  37. void AwSSLHostStateDelegate::HostRanInsecureContent(
  38. const std::string& host,
  39. int child_id,
  40. InsecureContentType content_type) {
  41. // Intentional no-op for Android WebView.
  42. }
  43. bool AwSSLHostStateDelegate::DidHostRunInsecureContent(
  44. const std::string& host,
  45. int child_id,
  46. InsecureContentType content_type) {
  47. // Intentional no-op for Android WebView.
  48. return false;
  49. }
  50. void AwSSLHostStateDelegate::AllowHttpForHost(
  51. const std::string& host,
  52. content::StoragePartition* storage_partition) {
  53. // Intentional no-op for Android WebView.
  54. }
  55. bool AwSSLHostStateDelegate::IsHttpAllowedForHost(
  56. const std::string& host,
  57. content::StoragePartition* storage_partition) {
  58. // Intentional no-op for Android WebView. Return value does not matter as
  59. // HTTPS-First Mode is not enabled on WebView.
  60. return false;
  61. }
  62. void AwSSLHostStateDelegate::AllowCert(
  63. const std::string& host,
  64. const net::X509Certificate& cert,
  65. int error,
  66. content::StoragePartition* storage_partition) {
  67. cert_policy_for_host_[host].Allow(cert, error);
  68. }
  69. void AwSSLHostStateDelegate::Clear(
  70. base::RepeatingCallback<bool(const std::string&)> host_filter) {
  71. if (!host_filter) {
  72. cert_policy_for_host_.clear();
  73. return;
  74. }
  75. for (auto it = cert_policy_for_host_.begin();
  76. it != cert_policy_for_host_.end();) {
  77. auto next_it = std::next(it);
  78. if (host_filter.Run(it->first))
  79. cert_policy_for_host_.erase(it);
  80. it = next_it;
  81. }
  82. }
  83. SSLHostStateDelegate::CertJudgment AwSSLHostStateDelegate::QueryPolicy(
  84. const std::string& host,
  85. const net::X509Certificate& cert,
  86. int error,
  87. content::StoragePartition* storage_partition) {
  88. return cert_policy_for_host_[host].Check(cert, error)
  89. ? SSLHostStateDelegate::ALLOWED
  90. : SSLHostStateDelegate::DENIED;
  91. }
  92. void AwSSLHostStateDelegate::RevokeUserAllowExceptions(
  93. const std::string& host) {
  94. cert_policy_for_host_.erase(host);
  95. }
  96. bool AwSSLHostStateDelegate::HasAllowException(
  97. const std::string& host,
  98. content::StoragePartition* storage_partition) {
  99. auto policy_iterator = cert_policy_for_host_.find(host);
  100. return policy_iterator != cert_policy_for_host_.end() &&
  101. policy_iterator->second.HasAllowException();
  102. }
  103. } // namespace android_webview