blob: 9476d2605c1ca9957ce50e2a7ca62d4dcc51f032 [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2012 The Chromium Authors
[email protected]ce2b62262009-06-27 05:11:412// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]74b962a2011-06-03 21:22:545#ifndef CONTENT_BROWSER_SSL_SSL_CLIENT_AUTH_HANDLER_H_
6#define CONTENT_BROWSER_SSL_SSL_CLIENT_AUTH_HANDLER_H_
[email protected]ce2b62262009-06-27 05:11:417
dcheng59716272016-04-09 05:19:088#include <memory>
9
Avi Drissmanadac21992023-01-11 23:46:3910#include "base/functional/callback.h"
Keishi Hattori0e45c022021-11-27 09:25:5211#include "base/memory/raw_ptr.h"
[email protected]3b63f8f42011-03-28 01:54:1512#include "base/memory/ref_counted.h"
davidben6cd57dd2014-12-12 19:23:5713#include "base/memory/weak_ptr.h"
[email protected]c38831a12011-10-28 12:44:4914#include "content/public/browser/browser_thread.h"
John Abd-El-Malek92bf3602019-07-31 02:25:4815#include "content/public/browser/web_contents.h"
mattm436ccfe2017-06-19 20:24:0816#include "net/ssl/client_cert_identity.h"
[email protected]536fd0b2013-03-14 17:41:5717#include "net/ssl/ssl_cert_request_info.h"
[email protected]ce2b62262009-06-27 05:11:4118
19namespace net {
[email protected]1ccb6992013-10-30 04:46:2020class ClientCertStore;
mattm436ccfe2017-06-19 20:24:0821class SSLPrivateKey;
[email protected]edfe7fab2010-11-28 13:11:5222class X509Certificate;
23} // namespace net
[email protected]ce2b62262009-06-27 05:11:4124
[email protected]89f23a32012-10-24 22:31:2425namespace content {
26
davidben78fee7d2014-12-03 19:41:5127// This class handles the approval and selection of a certificate for SSL client
David Benjaminf6a34092019-11-07 19:55:1028// authentication by the user. Should only be used on the UI thread. If the
davidben6cd57dd2014-12-12 19:23:5729// SSLClientAuthHandler is destroyed before the certificate is selected, the
davidben3b8455ae72015-03-11 19:42:1930// selection is canceled and the delegate never called.
davidben6cd57dd2014-12-12 19:23:5731class SSLClientAuthHandler {
davidben78fee7d2014-12-03 19:41:5132 public:
davidben3b8455ae72015-03-11 19:42:1933 // Delegate interface for SSLClientAuthHandler. Method implementations may
34 // delete the handler when called.
Lei Zhanged9be3a2021-11-17 22:01:1835 class Delegate {
davidben3b8455ae72015-03-11 19:42:1936 public:
37 Delegate() {}
davidben5b6618d2014-12-08 20:41:3938
Peter Boström9b036532021-10-28 23:37:2839 Delegate(const Delegate&) = delete;
40 Delegate& operator=(const Delegate&) = delete;
41
davidben3b8455ae72015-03-11 19:42:1942 // Called to continue the request with |cert|. |cert| may be nullptr.
mattm436ccfe2017-06-19 20:24:0843 virtual void ContinueWithCertificate(
44 scoped_refptr<net::X509Certificate> cert,
45 scoped_refptr<net::SSLPrivateKey> private_key) = 0;
davidben3b8455ae72015-03-11 19:42:1946
47 // Called to cancel the certificate selection and abort the request.
48 virtual void CancelCertificateSelection() = 0;
49
50 protected:
51 virtual ~Delegate() {}
davidben3b8455ae72015-03-11 19:42:1952 };
53
54 // Creates a new SSLClientAuthHandler. The caller ensures that the handler
Erik Chen7776c8d2024-01-24 04:46:1155 // 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 Cronindf260f5e2024-07-03 02:13:3961 // `process_id` corresponds to the ID of the renderer process initiating the
62 // request.
John Abd-El-Malek92bf3602019-07-31 02:25:4863 SSLClientAuthHandler(std::unique_ptr<net::ClientCertStore> client_cert_store,
Erik Chen7776c8d2024-01-24 04:46:1164 base::WeakPtr<BrowserContext> browser_context,
Devlin Cronindf260f5e2024-07-03 02:13:3965 int process_id,
Erik Chen7776c8d2024-01-24 04:46:1166 base::WeakPtr<WebContents> web_contents,
John Abd-El-Malek92bf3602019-07-31 02:25:4867 net::SSLCertRequestInfo* cert_request_info,
68 Delegate* delegate);
Peter Boström9b036532021-10-28 23:37:2869
70 SSLClientAuthHandler(const SSLClientAuthHandler&) = delete;
71 SSLClientAuthHandler& operator=(const SSLClientAuthHandler&) = delete;
72
davidben6cd57dd2014-12-12 19:23:5773 ~SSLClientAuthHandler();
[email protected]ce2b62262009-06-27 05:11:4174
[email protected]c99c442e2011-08-24 11:37:3075 // Selects a certificate and resumes the URL request with that certificate.
[email protected]ce2b62262009-06-27 05:11:4176 void SelectCertificate();
77
[email protected]ce2b62262009-06-27 05:11:4178 private:
David Benjaminf6a34092019-11-07 19:55:1079 class ClientCertificateDelegateImpl;
davidben78fee7d2014-12-03 19:41:5180
davidben6cd57dd2014-12-12 19:23:5781 // Called when |core_| is done retrieving the cert list.
mattm436ccfe2017-06-19 20:24:0882 void DidGetClientCerts(net::ClientCertIdentityList client_certs);
[email protected]1ccb6992013-10-30 04:46:2083
Arno Renevierb733ea82024-05-02 22:46:3384 void DidGetClientCertsOnPostTask(net::ClientCertIdentityList client_certs);
[email protected]c61769052011-05-18 18:38:3585
Daniel McArdle85735f52019-06-25 03:27:0686 // 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 Chen7776c8d2024-01-24 04:46:1190 base::WeakPtr<BrowserContext> browser_context_;
Devlin Cronindf260f5e2024-07-03 02:13:3991 const int process_id_;
Erik Chen7776c8d2024-01-24 04:46:1192 base::WeakPtr<WebContents> web_contents_;
[email protected]ce2b62262009-06-27 05:11:4193
94 // The certs to choose from.
95 scoped_refptr<net::SSLCertRequestInfo> cert_request_info_;
96
Arno Renevierb733ea82024-05-02 22:46:3397 // The ClientCertStore to retrieve the certs from.
98 std::unique_ptr<net::ClientCertStore> client_cert_store_;
99
davidben3b8455ae72015-03-11 19:42:19100 // The delegate to call back with the result.
Keishi Hattori0e45c022021-11-27 09:25:52101 raw_ptr<Delegate> delegate_;
davidben5b6618d2014-12-08 20:41:39102
Jeremy Roman3bca4bf2019-07-11 03:41:25103 base::WeakPtrFactory<SSLClientAuthHandler> weak_factory_{this};
[email protected]ce2b62262009-06-27 05:11:41104};
105
[email protected]89f23a32012-10-24 22:31:24106} // namespace content
107
[email protected]74b962a2011-06-03 21:22:54108#endif // CONTENT_BROWSER_SSL_SSL_CLIENT_AUTH_HANDLER_H_