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