blob: 2da3dc3e9da2b7ff69f0266fd0475704bca450fb [file] [log] [blame]
[email protected]ffd2f79e2013-11-14 00:11:461// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROME_BROWSER_EXTENSIONS_INSTALL_SIGNER_H_
6#define CHROME_BROWSER_EXTENSIONS_INSTALL_SIGNER_H_
7
dchengc963c7142016-04-08 03:55:228#include <memory>
[email protected]ffd2f79e2013-11-14 00:11:469#include <set>
10#include <string>
11#include <vector>
12
[email protected]ffd2f79e2013-11-14 00:11:4613#include "base/callback.h"
avia2f4804a2015-12-24 23:11:1314#include "base/macros.h"
Devlin Cronin060876552020-01-06 22:20:3915#include "extensions/common/extension_id.h"
[email protected]ffd2f79e2013-11-14 00:11:4616
17namespace base {
18class DictionaryValue;
19}
20
Antonio Gomescfd0d892018-05-12 12:58:2521namespace network {
22class SimpleURLLoader;
23class SharedURLLoaderFactory;
24} // namespace network
[email protected]ffd2f79e2013-11-14 00:11:4625
26namespace extensions {
27
28// This represents a list of ids signed with a private key using an algorithm
29// that includes some salt bytes.
30struct InstallSignature {
31 // The set of ids that have been signed.
32 ExtensionIdSet ids;
33
34 // Both of these are just arrays of bytes, NOT base64-encoded.
35 std::string salt;
36 std::string signature;
37
38 // The date that the signature should expire, in YYYY-MM-DD format.
39 std::string expire_date;
40
[email protected]8abef232014-03-07 08:54:3741 // The time this signature was obtained from the server. Note that this
42 // is computed locally and *not* signed by the server key.
[email protected]33dc0c62014-02-13 00:00:3843 base::Time timestamp;
44
[email protected]8abef232014-03-07 08:54:3745 // The set of ids that the server indicated were invalid (ie not signed).
46 // Note that this is computed locally and *not* signed by the signature.
47 ExtensionIdSet invalid_ids;
48
[email protected]ffd2f79e2013-11-14 00:11:4649 InstallSignature();
vmpstrb8aacbe2016-02-26 02:00:4850 InstallSignature(const InstallSignature& other);
[email protected]ffd2f79e2013-11-14 00:11:4651 ~InstallSignature();
52
53 // Helper methods for serialization to/from a base::DictionaryValue.
54 void ToValue(base::DictionaryValue* value) const;
55
dchengc963c7142016-04-08 03:55:2256 static std::unique_ptr<InstallSignature> FromValue(
[email protected]ffd2f79e2013-11-14 00:11:4657 const base::DictionaryValue& value);
58};
59
60// Objects of this class encapsulate an operation to get a signature proving
61// that a set of ids are hosted in the webstore.
62class InstallSigner {
63 public:
Devlin Croninefd14a12019-01-04 00:51:4964 using SignatureCallback =
65 base::OnceCallback<void(std::unique_ptr<InstallSignature>)>;
[email protected]ffd2f79e2013-11-14 00:11:4666
67 // IMPORTANT NOTE: It is possible that only some, but not all, of the entries
68 // in |ids| will be successfully signed by the backend. Callers should always
69 // check the set of ids in the InstallSignature passed to their callback, as
70 // it may contain only a subset of the ids they passed in.
Antonio Gomescfd0d892018-05-12 12:58:2571 InstallSigner(
72 scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
73 const ExtensionIdSet& ids);
[email protected]ffd2f79e2013-11-14 00:11:4674 ~InstallSigner();
75
76 // Returns a set of ids that are forced to be considered not from webstore,
77 // e.g. by a command line flag used for testing.
78 static ExtensionIdSet GetForcedNotFromWebstore();
79
80 // Begins the process of fetching a signature from the backend. This should
81 // only be called once! If you want to get another signature, make another
82 // instance of this class.
Devlin Croninefd14a12019-01-04 00:51:4983 void GetSignature(SignatureCallback callback);
[email protected]ffd2f79e2013-11-14 00:11:4684
85 // Returns whether the signature in InstallSignature is properly signed with a
86 // known public key.
87 static bool VerifySignature(const InstallSignature& signature);
88
89 private:
[email protected]ffd2f79e2013-11-14 00:11:4690
[email protected]76f569d2013-12-11 21:37:2091 // A helper function that calls |callback_| with an indication that an error
92 // happened (currently done by passing an empty pointer).
93 void ReportErrorViaCallback();
94
Antonio Gomescfd0d892018-05-12 12:58:2595 // Called when |simple_loader_| has returned a result to parse the response,
[email protected]76f569d2013-12-11 21:37:2096 // and then call HandleSignatureResult with structured data.
Antonio Gomescfd0d892018-05-12 12:58:2597 void ParseFetchResponse(std::unique_ptr<std::string> response_body);
[email protected]76f569d2013-12-11 21:37:2098
[email protected]ffd2f79e2013-11-14 00:11:4699 // Handles the result from a backend fetch.
100 void HandleSignatureResult(const std::string& signature,
101 const std::string& expire_date,
102 const ExtensionIdSet& invalid_ids);
103
104 // The final callback for when we're done.
105 SignatureCallback callback_;
106
107 // The current set of ids we're trying to verify. This may contain fewer ids
108 // than we started with.
109 ExtensionIdSet ids_;
110
111 // An array of random bytes used as an input to hash with the machine id,
112 // which will need to be persisted in the eventual InstallSignature we get.
113 std::string salt_;
114
115 // These are used to make the call to a backend server for a signature.
Antonio Gomescfd0d892018-05-12 12:58:25116 scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
117 // The underlying SimpleURLLoader which does the actual load.
118 std::unique_ptr<network::SimpleURLLoader> simple_loader_;
[email protected]ffd2f79e2013-11-14 00:11:46119
[email protected]33dc0c62014-02-13 00:00:38120 // The time the request to the server was started.
121 base::Time request_start_time_;
122
[email protected]ffd2f79e2013-11-14 00:11:46123 DISALLOW_COPY_AND_ASSIGN(InstallSigner);
124};
125
126} // namespace extensions
127
128#endif // CHROME_BROWSER_EXTENSIONS_INSTALL_SIGNER_H_