Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors |
pneubeck | 7ab2635c | 2015-09-04 14:12:08 | [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 | |
Oleksandr Kulkov | 9c7cf955 | 2022-06-02 12:25:43 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_CERTIFICATE_PROVIDER_SIGN_REQUESTS_H_ |
| 6 | #define CHROME_BROWSER_CERTIFICATE_PROVIDER_SIGN_REQUESTS_H_ |
pneubeck | 7ab2635c | 2015-09-04 14:12:08 | [diff] [blame] | 7 | |
| 8 | #include <map> |
Arthur Sonzogni | fe132ee | 2024-01-15 11:01:04 | [diff] [blame] | 9 | #include <optional> |
pneubeck | 7ab2635c | 2015-09-04 14:12:08 | [diff] [blame] | 10 | #include <string> |
Maksim Ivanov | 7652d0d | 2019-09-04 19:12:19 | [diff] [blame] | 11 | #include <utility> |
pneubeck | 7ab2635c | 2015-09-04 14:12:08 | [diff] [blame] | 12 | #include <vector> |
| 13 | |
Avi Drissman | 02e49e58 | 2023-01-07 01:23:18 | [diff] [blame] | 14 | #include "base/functional/callback.h" |
Lei Zhang | 25439e7 | 2025-04-23 17:19:54 | [diff] [blame] | 15 | #include "base/memory/scoped_refptr.h" |
Maksim Ivanov | 7652d0d | 2019-09-04 19:12:19 | [diff] [blame] | 16 | #include "components/account_id/account_id.h" |
Maksim Ivanov | e9f3698 | 2019-06-25 09:49:54 | [diff] [blame] | 17 | #include "net/cert/x509_certificate.h" |
pneubeck | 7ab2635c | 2015-09-04 14:12:08 | [diff] [blame] | 18 | #include "net/ssl/ssl_private_key.h" |
| 19 | |
Oleksandr Kulkov | 1213dee8 | 2022-05-17 20:45:34 | [diff] [blame] | 20 | namespace chromeos { |
pneubeck | 7ab2635c | 2015-09-04 14:12:08 | [diff] [blame] | 21 | namespace certificate_provider { |
| 22 | |
| 23 | class SignRequests { |
| 24 | public: |
Maksim Ivanov | 7652d0d | 2019-09-04 19:12:19 | [diff] [blame] | 25 | using ExtensionNameRequestIdPair = std::pair<std::string, int>; |
| 26 | |
pneubeck | 7ab2635c | 2015-09-04 14:12:08 | [diff] [blame] | 27 | SignRequests(); |
| 28 | ~SignRequests(); |
| 29 | |
| 30 | // Returns the id of the new request. The returned request id is specific to |
| 31 | // the given extension. |
Arthur Sonzogni | fe132ee | 2024-01-15 11:01:04 | [diff] [blame] | 32 | int AddRequest(const std::string& extension_id, |
| 33 | const scoped_refptr<net::X509Certificate>& certificate, |
| 34 | const std::optional<AccountId>& authenticating_user_account_id, |
| 35 | net::SSLPrivateKey::SignCallback callback); |
Maksim Ivanov | 7652d0d | 2019-09-04 19:12:19 | [diff] [blame] | 36 | |
| 37 | // Returns the list of requests that correspond to the authentication of the |
| 38 | // given user. |
| 39 | std::vector<ExtensionNameRequestIdPair> FindRequestsForAuthenticatingUser( |
| 40 | const AccountId& authenticating_user_account_id) const; |
pneubeck | 7ab2635c | 2015-09-04 14:12:08 | [diff] [blame] | 41 | |
| 42 | // Returns false if no request with the given id for |extension_id| |
Maksim Ivanov | e9f3698 | 2019-06-25 09:49:54 | [diff] [blame] | 43 | // could be found. Otherwise removes the request and sets |certificate| and |
| 44 | // |callback| to the values that were provided with AddRequest(). |
pneubeck | 7ab2635c | 2015-09-04 14:12:08 | [diff] [blame] | 45 | bool RemoveRequest(const std::string& extension_id, |
| 46 | int request_id, |
Maksim Ivanov | e9f3698 | 2019-06-25 09:49:54 | [diff] [blame] | 47 | scoped_refptr<net::X509Certificate>* certificate, |
pneubeck | 7ab2635c | 2015-09-04 14:12:08 | [diff] [blame] | 48 | net::SSLPrivateKey::SignCallback* callback); |
| 49 | |
| 50 | // Remove all pending requests for this extension and return their |
| 51 | // callbacks. |
| 52 | std::vector<net::SSLPrivateKey::SignCallback> RemoveAllRequests( |
| 53 | const std::string& extension_id); |
| 54 | |
| 55 | private: |
Maksim Ivanov | e9f3698 | 2019-06-25 09:49:54 | [diff] [blame] | 56 | struct Request { |
| 57 | Request(const scoped_refptr<net::X509Certificate>& certificate, |
Arthur Sonzogni | fe132ee | 2024-01-15 11:01:04 | [diff] [blame] | 58 | const std::optional<AccountId>& authenticating_user_account_id, |
Maksim Ivanov | e9f3698 | 2019-06-25 09:49:54 | [diff] [blame] | 59 | net::SSLPrivateKey::SignCallback callback); |
| 60 | Request(Request&& other); |
Maksim Ivanov | e9f3698 | 2019-06-25 09:49:54 | [diff] [blame] | 61 | Request& operator=(Request&&); |
Fabian Sommer | 16e5bac | 2020-04-30 10:15:48 | [diff] [blame] | 62 | ~Request(); |
Maksim Ivanov | e9f3698 | 2019-06-25 09:49:54 | [diff] [blame] | 63 | |
| 64 | scoped_refptr<net::X509Certificate> certificate; |
Arthur Sonzogni | fe132ee | 2024-01-15 11:01:04 | [diff] [blame] | 65 | std::optional<AccountId> authenticating_user_account_id; |
Maksim Ivanov | e9f3698 | 2019-06-25 09:49:54 | [diff] [blame] | 66 | net::SSLPrivateKey::SignCallback callback; |
Maksim Ivanov | e9f3698 | 2019-06-25 09:49:54 | [diff] [blame] | 67 | }; |
| 68 | |
pneubeck | 7ab2635c | 2015-09-04 14:12:08 | [diff] [blame] | 69 | // Holds state of all sign requests to a single extension. |
| 70 | struct RequestsState { |
| 71 | RequestsState(); |
David Benjamin | 8f2d2c1 | 2018-02-27 00:08:26 | [diff] [blame] | 72 | RequestsState(RequestsState&& other); |
Fabian Sommer | 16e5bac | 2020-04-30 10:15:48 | [diff] [blame] | 73 | RequestsState& operator=(RequestsState&&); |
pneubeck | 7ab2635c | 2015-09-04 14:12:08 | [diff] [blame] | 74 | ~RequestsState(); |
| 75 | |
Maksim Ivanov | e9f3698 | 2019-06-25 09:49:54 | [diff] [blame] | 76 | // Maps from request id to the request state. |
| 77 | std::map<int, Request> pending_requests; |
pneubeck | 7ab2635c | 2015-09-04 14:12:08 | [diff] [blame] | 78 | |
| 79 | // The request id that will be used for the next sign request to this |
| 80 | // extension. |
| 81 | int next_free_id = 0; |
| 82 | }; |
| 83 | |
| 84 | // Contains the state of all sign requests per extension. |
| 85 | std::map<std::string, RequestsState> extension_to_requests_; |
pneubeck | 7ab2635c | 2015-09-04 14:12:08 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | } // namespace certificate_provider |
Oleksandr Kulkov | 1213dee8 | 2022-05-17 20:45:34 | [diff] [blame] | 89 | } // namespace chromeos |
pneubeck | 7ab2635c | 2015-09-04 14:12:08 | [diff] [blame] | 90 | |
Oleksandr Kulkov | 9c7cf955 | 2022-06-02 12:25:43 | [diff] [blame] | 91 | #endif // CHROME_BROWSER_CERTIFICATE_PROVIDER_SIGN_REQUESTS_H_ |