Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | ce2b6226 | 2009-06-27 05:11:41 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 74b962a | 2011-06-03 21:22:54 | [diff] [blame] | 5 | #ifndef CONTENT_BROWSER_SSL_SSL_CLIENT_AUTH_HANDLER_H_ |
| 6 | #define CONTENT_BROWSER_SSL_SSL_CLIENT_AUTH_HANDLER_H_ |
[email protected] | ce2b6226 | 2009-06-27 05:11:41 | [diff] [blame] | 7 | |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 8 | #include <memory> |
| 9 | |
Avi Drissman | adac2199 | 2023-01-11 23:46:39 | [diff] [blame] | 10 | #include "base/functional/callback.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 11 | #include "base/memory/raw_ptr.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 12 | #include "base/memory/ref_counted.h" |
davidben | 6cd57dd | 2014-12-12 19:23:57 | [diff] [blame] | 13 | #include "base/memory/weak_ptr.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 14 | #include "content/public/browser/browser_thread.h" |
John Abd-El-Malek | 92bf360 | 2019-07-31 02:25:48 | [diff] [blame] | 15 | #include "content/public/browser/web_contents.h" |
mattm | 436ccfe | 2017-06-19 20:24:08 | [diff] [blame] | 16 | #include "net/ssl/client_cert_identity.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 17 | #include "net/ssl/ssl_cert_request_info.h" |
[email protected] | ce2b6226 | 2009-06-27 05:11:41 | [diff] [blame] | 18 | |
| 19 | namespace net { |
[email protected] | 1ccb699 | 2013-10-30 04:46:20 | [diff] [blame] | 20 | class ClientCertStore; |
mattm | 436ccfe | 2017-06-19 20:24:08 | [diff] [blame] | 21 | class SSLPrivateKey; |
[email protected] | edfe7fab | 2010-11-28 13:11:52 | [diff] [blame] | 22 | class X509Certificate; |
| 23 | } // namespace net |
[email protected] | ce2b6226 | 2009-06-27 05:11:41 | [diff] [blame] | 24 | |
[email protected] | 89f23a3 | 2012-10-24 22:31:24 | [diff] [blame] | 25 | namespace content { |
| 26 | |
davidben | 78fee7d | 2014-12-03 19:41:51 | [diff] [blame] | 27 | // This class handles the approval and selection of a certificate for SSL client |
David Benjamin | f6a3409 | 2019-11-07 19:55:10 | [diff] [blame] | 28 | // authentication by the user. Should only be used on the UI thread. If the |
davidben | 6cd57dd | 2014-12-12 19:23:57 | [diff] [blame] | 29 | // SSLClientAuthHandler is destroyed before the certificate is selected, the |
davidben | 3b8455ae7 | 2015-03-11 19:42:19 | [diff] [blame] | 30 | // selection is canceled and the delegate never called. |
davidben | 6cd57dd | 2014-12-12 19:23:57 | [diff] [blame] | 31 | class SSLClientAuthHandler { |
davidben | 78fee7d | 2014-12-03 19:41:51 | [diff] [blame] | 32 | public: |
davidben | 3b8455ae7 | 2015-03-11 19:42:19 | [diff] [blame] | 33 | // Delegate interface for SSLClientAuthHandler. Method implementations may |
| 34 | // delete the handler when called. |
Lei Zhang | ed9be3a | 2021-11-17 22:01:18 | [diff] [blame] | 35 | class Delegate { |
davidben | 3b8455ae7 | 2015-03-11 19:42:19 | [diff] [blame] | 36 | public: |
| 37 | Delegate() {} |
davidben | 5b6618d | 2014-12-08 20:41:39 | [diff] [blame] | 38 | |
Peter Boström | 9b03653 | 2021-10-28 23:37:28 | [diff] [blame] | 39 | Delegate(const Delegate&) = delete; |
| 40 | Delegate& operator=(const Delegate&) = delete; |
| 41 | |
davidben | 3b8455ae7 | 2015-03-11 19:42:19 | [diff] [blame] | 42 | // Called to continue the request with |cert|. |cert| may be nullptr. |
mattm | 436ccfe | 2017-06-19 20:24:08 | [diff] [blame] | 43 | virtual void ContinueWithCertificate( |
| 44 | scoped_refptr<net::X509Certificate> cert, |
| 45 | scoped_refptr<net::SSLPrivateKey> private_key) = 0; |
davidben | 3b8455ae7 | 2015-03-11 19:42:19 | [diff] [blame] | 46 | |
| 47 | // Called to cancel the certificate selection and abort the request. |
| 48 | virtual void CancelCertificateSelection() = 0; |
| 49 | |
| 50 | protected: |
| 51 | virtual ~Delegate() {} |
davidben | 3b8455ae7 | 2015-03-11 19:42:19 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | // Creates a new SSLClientAuthHandler. The caller ensures that the handler |
Erik Chen | 7776c8d | 2024-01-24 04:46:11 | [diff] [blame] | 55 | // does not outlive `delegate`. |
| 56 | // `browser_context` is always set, but may become invalid if the caller is |
| 57 | // destroyed. `web_contents` may be null for cases where the calling context |
| 58 | // is not associated with a document, such as service workers. If |
| 59 | // `web_contents` is not null, it is guaranteed to be associated with the same |
| 60 | // BrowserContext as `browser_context`. |
Devlin Cronin | df260f5e | 2024-07-03 02:13:39 | [diff] [blame] | 61 | // `process_id` corresponds to the ID of the renderer process initiating the |
| 62 | // request. |
John Abd-El-Malek | 92bf360 | 2019-07-31 02:25:48 | [diff] [blame] | 63 | SSLClientAuthHandler(std::unique_ptr<net::ClientCertStore> client_cert_store, |
Erik Chen | 7776c8d | 2024-01-24 04:46:11 | [diff] [blame] | 64 | base::WeakPtr<BrowserContext> browser_context, |
Devlin Cronin | df260f5e | 2024-07-03 02:13:39 | [diff] [blame] | 65 | int process_id, |
Erik Chen | 7776c8d | 2024-01-24 04:46:11 | [diff] [blame] | 66 | base::WeakPtr<WebContents> web_contents, |
John Abd-El-Malek | 92bf360 | 2019-07-31 02:25:48 | [diff] [blame] | 67 | net::SSLCertRequestInfo* cert_request_info, |
| 68 | Delegate* delegate); |
Peter Boström | 9b03653 | 2021-10-28 23:37:28 | [diff] [blame] | 69 | |
| 70 | SSLClientAuthHandler(const SSLClientAuthHandler&) = delete; |
| 71 | SSLClientAuthHandler& operator=(const SSLClientAuthHandler&) = delete; |
| 72 | |
davidben | 6cd57dd | 2014-12-12 19:23:57 | [diff] [blame] | 73 | ~SSLClientAuthHandler(); |
[email protected] | ce2b6226 | 2009-06-27 05:11:41 | [diff] [blame] | 74 | |
[email protected] | c99c442e | 2011-08-24 11:37:30 | [diff] [blame] | 75 | // Selects a certificate and resumes the URL request with that certificate. |
[email protected] | ce2b6226 | 2009-06-27 05:11:41 | [diff] [blame] | 76 | void SelectCertificate(); |
| 77 | |
[email protected] | ce2b6226 | 2009-06-27 05:11:41 | [diff] [blame] | 78 | private: |
David Benjamin | f6a3409 | 2019-11-07 19:55:10 | [diff] [blame] | 79 | class ClientCertificateDelegateImpl; |
davidben | 78fee7d | 2014-12-03 19:41:51 | [diff] [blame] | 80 | |
davidben | 6cd57dd | 2014-12-12 19:23:57 | [diff] [blame] | 81 | // Called when |core_| is done retrieving the cert list. |
mattm | 436ccfe | 2017-06-19 20:24:08 | [diff] [blame] | 82 | void DidGetClientCerts(net::ClientCertIdentityList client_certs); |
[email protected] | 1ccb699 | 2013-10-30 04:46:20 | [diff] [blame] | 83 | |
Arno Renevier | b733ea8 | 2024-05-02 22:46:33 | [diff] [blame] | 84 | void DidGetClientCertsOnPostTask(net::ClientCertIdentityList client_certs); |
[email protected] | c6176905 | 2011-05-18 18:38:35 | [diff] [blame] | 85 | |
Daniel McArdle | 85735f5 | 2019-06-25 03:27:06 | [diff] [blame] | 86 | // A callback that may be set by the UI implementation. If set, the callback |
| 87 | // will cancel the dialog corresponding to this certificate request. |
| 88 | base::OnceClosure cancellation_callback_; |
| 89 | |
Erik Chen | 7776c8d | 2024-01-24 04:46:11 | [diff] [blame] | 90 | base::WeakPtr<BrowserContext> browser_context_; |
Devlin Cronin | df260f5e | 2024-07-03 02:13:39 | [diff] [blame] | 91 | const int process_id_; |
Erik Chen | 7776c8d | 2024-01-24 04:46:11 | [diff] [blame] | 92 | base::WeakPtr<WebContents> web_contents_; |
[email protected] | ce2b6226 | 2009-06-27 05:11:41 | [diff] [blame] | 93 | |
| 94 | // The certs to choose from. |
| 95 | scoped_refptr<net::SSLCertRequestInfo> cert_request_info_; |
| 96 | |
Arno Renevier | b733ea8 | 2024-05-02 22:46:33 | [diff] [blame] | 97 | // The ClientCertStore to retrieve the certs from. |
| 98 | std::unique_ptr<net::ClientCertStore> client_cert_store_; |
| 99 | |
davidben | 3b8455ae7 | 2015-03-11 19:42:19 | [diff] [blame] | 100 | // The delegate to call back with the result. |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 101 | raw_ptr<Delegate> delegate_; |
davidben | 5b6618d | 2014-12-08 20:41:39 | [diff] [blame] | 102 | |
Jeremy Roman | 3bca4bf | 2019-07-11 03:41:25 | [diff] [blame] | 103 | base::WeakPtrFactory<SSLClientAuthHandler> weak_factory_{this}; |
[email protected] | ce2b6226 | 2009-06-27 05:11:41 | [diff] [blame] | 104 | }; |
| 105 | |
[email protected] | 89f23a3 | 2012-10-24 22:31:24 | [diff] [blame] | 106 | } // namespace content |
| 107 | |
[email protected] | 74b962a | 2011-06-03 21:22:54 | [diff] [blame] | 108 | #endif // CONTENT_BROWSER_SSL_SSL_CLIENT_AUTH_HANDLER_H_ |