aw_ssl_host_state_delegate.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #ifndef ANDROID_WEBVIEW_BROWSER_AW_SSL_HOST_STATE_DELEGATE_H_
  5. #define ANDROID_WEBVIEW_BROWSER_AW_SSL_HOST_STATE_DELEGATE_H_
  6. #include <map>
  7. #include <string>
  8. #include "content/public/browser/ssl_host_state_delegate.h"
  9. #include "net/base/hash_value.h"
  10. #include "net/cert/x509_certificate.h"
  11. namespace android_webview {
  12. namespace internal {
  13. // This class maintains the policy for storing actions on certificate errors.
  14. class CertPolicy {
  15. public:
  16. CertPolicy();
  17. ~CertPolicy();
  18. // Returns true if the user has decided to proceed through the ssl error
  19. // before. For a certificate to be allowed, it must not have any
  20. // *additional* errors from when it was allowed.
  21. bool Check(const net::X509Certificate& cert, int error) const;
  22. // Causes the policy to allow this certificate for a given |error|. And
  23. // remember the user's choice.
  24. void Allow(const net::X509Certificate& cert, int error);
  25. // Returns true if and only if there exists a user allow exception for some
  26. // certificate.
  27. bool HasAllowException() const { return allowed_.size() > 0; }
  28. private:
  29. // The set of fingerprints of allowed certificates.
  30. std::map<net::SHA256HashValue, int> allowed_;
  31. };
  32. } // namespace internal
  33. class AwSSLHostStateDelegate : public content::SSLHostStateDelegate {
  34. public:
  35. AwSSLHostStateDelegate();
  36. AwSSLHostStateDelegate(const AwSSLHostStateDelegate&) = delete;
  37. AwSSLHostStateDelegate& operator=(const AwSSLHostStateDelegate&) = delete;
  38. ~AwSSLHostStateDelegate() override;
  39. // Records that |cert| is permitted to be used for |host| in the future, for
  40. // a specified |error| type.
  41. void AllowCert(const std::string& host,
  42. const net::X509Certificate& cert,
  43. int error,
  44. content::StoragePartition* storage_partition) override;
  45. void Clear(
  46. base::RepeatingCallback<bool(const std::string&)> host_filter) override;
  47. // Queries whether |cert| is allowed or denied for |host| and |error|.
  48. content::SSLHostStateDelegate::CertJudgment QueryPolicy(
  49. const std::string& host,
  50. const net::X509Certificate& cert,
  51. int error,
  52. content::StoragePartition* storage_partition) override;
  53. // Records that a host has run insecure content.
  54. void HostRanInsecureContent(const std::string& host,
  55. int child_id,
  56. InsecureContentType content_type) override;
  57. // Returns whether the specified host ran insecure content.
  58. bool DidHostRunInsecureContent(const std::string& host,
  59. int child_id,
  60. InsecureContentType content_type) override;
  61. // HTTPS-First Mode is not implemented in Android Webview.
  62. void AllowHttpForHost(const std::string& host,
  63. content::StoragePartition* storage_partition) override;
  64. bool IsHttpAllowedForHost(
  65. const std::string& host,
  66. content::StoragePartition* storage_partition) override;
  67. // Revokes all SSL certificate error allow exceptions made by the user for
  68. // |host|.
  69. void RevokeUserAllowExceptions(const std::string& host) override;
  70. // Returns whether the user has allowed a certificate error exception for
  71. // |host|. This does not mean that *all* certificate errors are allowed, just
  72. // that there exists an exception. To see if a particular certificate and
  73. // error combination exception is allowed, use QueryPolicy().
  74. bool HasAllowException(const std::string& host,
  75. content::StoragePartition* storage_partition) override;
  76. private:
  77. // Certificate policies for each host.
  78. std::map<std::string, internal::CertPolicy> cert_policy_for_host_;
  79. };
  80. } // namespace android_webview
  81. #endif // ANDROID_WEBVIEW_BROWSER_AW_SSL_HOST_STATE_DELEGATE_H_