blob: 5f92e8c7c88ea904509981bcc7ddcf36df27e5f8 [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>
9#include <string>
Maksim Ivanov7652d0d2019-09-04 19:12:1910#include <utility>
pneubeck7ab2635c2015-09-04 14:12:0811#include <vector>
12
Maksim Ivanove9f36982019-06-25 09:49:5413#include "base/callback.h"
Maksim Ivanove9f36982019-06-25 09:49:5414#include "base/memory/ref_counted.h"
Maksim Ivanov7652d0d2019-09-04 19:12:1915#include "components/account_id/account_id.h"
Maksim Ivanove9f36982019-06-25 09:49:5416#include "net/cert/x509_certificate.h"
pneubeck7ab2635c2015-09-04 14:12:0817#include "net/ssl/ssl_private_key.h"
Anton Bikineev46bbb972021-05-15 17:53:5318#include "third_party/abseil-cpp/absl/types/optional.h"
pneubeck7ab2635c2015-09-04 14:12:0819
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.
Maksim Ivanov7652d0d2019-09-04 19:12:1932 int AddRequest(
33 const std::string& extension_id,
34 const scoped_refptr<net::X509Certificate>& certificate,
Anton Bikineev46bbb972021-05-15 17:53:5335 const absl::optional<AccountId>& authenticating_user_account_id,
Maksim Ivanov7652d0d2019-09-04 19:12:1936 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;
pneubeck7ab2635c2015-09-04 14:12:0842
43 // Returns false if no request with the given id for |extension_id|
Maksim Ivanove9f36982019-06-25 09:49:5444 // could be found. Otherwise removes the request and sets |certificate| and
45 // |callback| to the values that were provided with AddRequest().
pneubeck7ab2635c2015-09-04 14:12:0846 bool RemoveRequest(const std::string& extension_id,
47 int request_id,
Maksim Ivanove9f36982019-06-25 09:49:5448 scoped_refptr<net::X509Certificate>* certificate,
pneubeck7ab2635c2015-09-04 14:12:0849 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 Ivanove9f36982019-06-25 09:49:5457 struct Request {
58 Request(const scoped_refptr<net::X509Certificate>& certificate,
Anton Bikineev46bbb972021-05-15 17:53:5359 const absl::optional<AccountId>& authenticating_user_account_id,
Maksim Ivanove9f36982019-06-25 09:49:5460 net::SSLPrivateKey::SignCallback callback);
61 Request(Request&& other);
Maksim Ivanove9f36982019-06-25 09:49:5462 Request& operator=(Request&&);
Fabian Sommer16e5bac2020-04-30 10:15:4863 ~Request();
Maksim Ivanove9f36982019-06-25 09:49:5464
65 scoped_refptr<net::X509Certificate> certificate;
Anton Bikineev46bbb972021-05-15 17:53:5366 absl::optional<AccountId> authenticating_user_account_id;
Maksim Ivanove9f36982019-06-25 09:49:5467 net::SSLPrivateKey::SignCallback callback;
Maksim Ivanove9f36982019-06-25 09:49:5468 };
69
pneubeck7ab2635c2015-09-04 14:12:0870 // Holds state of all sign requests to a single extension.
71 struct RequestsState {
72 RequestsState();
David Benjamin8f2d2c12018-02-27 00:08:2673 RequestsState(RequestsState&& other);
Fabian Sommer16e5bac2020-04-30 10:15:4874 RequestsState& operator=(RequestsState&&);
pneubeck7ab2635c2015-09-04 14:12:0875 ~RequestsState();
76
Maksim Ivanove9f36982019-06-25 09:49:5477 // Maps from request id to the request state.
78 std::map<int, Request> pending_requests;
pneubeck7ab2635c2015-09-04 14:12:0879
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_;
pneubeck7ab2635c2015-09-04 14:12:0887};
88
89} // namespace certificate_provider
Oleksandr Kulkov1213dee82022-05-17 20:45:3490} // namespace chromeos
pneubeck7ab2635c2015-09-04 14:12:0891
Oleksandr Kulkov9c7cf9552022-06-02 12:25:4392#endif // CHROME_BROWSER_CERTIFICATE_PROVIDER_SIGN_REQUESTS_H_