blob: c4e605f38c9ff70375c8e7e39fdfaf83f1d5fbb8 [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2015 The Chromium Authors
pneubeck7ab2635c2015-09-04 14:12:082// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Oleksandr Kulkov9c7cf9552022-06-02 12:25:435#ifndef CHROME_BROWSER_CERTIFICATE_PROVIDER_SIGN_REQUESTS_H_
6#define CHROME_BROWSER_CERTIFICATE_PROVIDER_SIGN_REQUESTS_H_
pneubeck7ab2635c2015-09-04 14:12:087
8#include <map>
Arthur Sonzognife132ee2024-01-15 11:01:049#include <optional>
pneubeck7ab2635c2015-09-04 14:12:0810#include <string>
Maksim Ivanov7652d0d2019-09-04 19:12:1911#include <utility>
pneubeck7ab2635c2015-09-04 14:12:0812#include <vector>
13
Avi Drissman02e49e582023-01-07 01:23:1814#include "base/functional/callback.h"
Lei Zhang25439e72025-04-23 17:19:5415#include "base/memory/scoped_refptr.h"
Maksim Ivanov7652d0d2019-09-04 19:12:1916#include "components/account_id/account_id.h"
Maksim Ivanove9f36982019-06-25 09:49:5417#include "net/cert/x509_certificate.h"
pneubeck7ab2635c2015-09-04 14:12:0818#include "net/ssl/ssl_private_key.h"
19
Oleksandr Kulkov1213dee82022-05-17 20:45:3420namespace chromeos {
pneubeck7ab2635c2015-09-04 14:12:0821namespace certificate_provider {
22
23class SignRequests {
24 public:
Maksim Ivanov7652d0d2019-09-04 19:12:1925 using ExtensionNameRequestIdPair = std::pair<std::string, int>;
26
pneubeck7ab2635c2015-09-04 14:12:0827 SignRequests();
28 ~SignRequests();
29
30 // Returns the id of the new request. The returned request id is specific to
31 // the given extension.
Arthur Sonzognife132ee2024-01-15 11:01:0432 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 Ivanov7652d0d2019-09-04 19:12:1936
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;
pneubeck7ab2635c2015-09-04 14:12:0841
42 // Returns false if no request with the given id for |extension_id|
Maksim Ivanove9f36982019-06-25 09:49:5443 // could be found. Otherwise removes the request and sets |certificate| and
44 // |callback| to the values that were provided with AddRequest().
pneubeck7ab2635c2015-09-04 14:12:0845 bool RemoveRequest(const std::string& extension_id,
46 int request_id,
Maksim Ivanove9f36982019-06-25 09:49:5447 scoped_refptr<net::X509Certificate>* certificate,
pneubeck7ab2635c2015-09-04 14:12:0848 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 Ivanove9f36982019-06-25 09:49:5456 struct Request {
57 Request(const scoped_refptr<net::X509Certificate>& certificate,
Arthur Sonzognife132ee2024-01-15 11:01:0458 const std::optional<AccountId>& authenticating_user_account_id,
Maksim Ivanove9f36982019-06-25 09:49:5459 net::SSLPrivateKey::SignCallback callback);
60 Request(Request&& other);
Maksim Ivanove9f36982019-06-25 09:49:5461 Request& operator=(Request&&);
Fabian Sommer16e5bac2020-04-30 10:15:4862 ~Request();
Maksim Ivanove9f36982019-06-25 09:49:5463
64 scoped_refptr<net::X509Certificate> certificate;
Arthur Sonzognife132ee2024-01-15 11:01:0465 std::optional<AccountId> authenticating_user_account_id;
Maksim Ivanove9f36982019-06-25 09:49:5466 net::SSLPrivateKey::SignCallback callback;
Maksim Ivanove9f36982019-06-25 09:49:5467 };
68
pneubeck7ab2635c2015-09-04 14:12:0869 // Holds state of all sign requests to a single extension.
70 struct RequestsState {
71 RequestsState();
David Benjamin8f2d2c12018-02-27 00:08:2672 RequestsState(RequestsState&& other);
Fabian Sommer16e5bac2020-04-30 10:15:4873 RequestsState& operator=(RequestsState&&);
pneubeck7ab2635c2015-09-04 14:12:0874 ~RequestsState();
75
Maksim Ivanove9f36982019-06-25 09:49:5476 // Maps from request id to the request state.
77 std::map<int, Request> pending_requests;
pneubeck7ab2635c2015-09-04 14:12:0878
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_;
pneubeck7ab2635c2015-09-04 14:12:0886};
87
88} // namespace certificate_provider
Oleksandr Kulkov1213dee82022-05-17 20:45:3489} // namespace chromeos
pneubeck7ab2635c2015-09-04 14:12:0890
Oleksandr Kulkov9c7cf9552022-06-02 12:25:4391#endif // CHROME_BROWSER_CERTIFICATE_PROVIDER_SIGN_REQUESTS_H_