blob: b110bef7baf24cd05411b8881a54d014a185e1b0 [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
8#include <set>
9#include <string>
10#include <vector>
11
12#include "base/basictypes.h"
13#include "base/callback.h"
14#include "base/memory/scoped_ptr.h"
15#include "chrome/common/extensions/extension.h"
16
17namespace base {
18class DictionaryValue;
19}
20
21namespace net {
22class URLFetcher;
23class URLRequestContextGetter;
24}
25
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
41 InstallSignature();
42 ~InstallSignature();
43
44 // Helper methods for serialization to/from a base::DictionaryValue.
45 void ToValue(base::DictionaryValue* value) const;
46
47 static scoped_ptr<InstallSignature> FromValue(
48 const base::DictionaryValue& value);
49};
50
51// Objects of this class encapsulate an operation to get a signature proving
52// that a set of ids are hosted in the webstore.
53class InstallSigner {
54 public:
55 typedef base::Callback<void(scoped_ptr<InstallSignature>)> SignatureCallback;
56
57 // IMPORTANT NOTE: It is possible that only some, but not all, of the entries
58 // in |ids| will be successfully signed by the backend. Callers should always
59 // check the set of ids in the InstallSignature passed to their callback, as
60 // it may contain only a subset of the ids they passed in.
61 InstallSigner(net::URLRequestContextGetter* context_getter,
62 const ExtensionIdSet& ids);
63 ~InstallSigner();
64
65 // Returns a set of ids that are forced to be considered not from webstore,
66 // e.g. by a command line flag used for testing.
67 static ExtensionIdSet GetForcedNotFromWebstore();
68
69 // Begins the process of fetching a signature from the backend. This should
70 // only be called once! If you want to get another signature, make another
71 // instance of this class.
72 void GetSignature(const SignatureCallback& callback);
73
74 // Returns whether the signature in InstallSignature is properly signed with a
75 // known public key.
76 static bool VerifySignature(const InstallSignature& signature);
77
78 private:
79 // A very simple delegate just used to call ourself back when a url fetch is
80 // complete.
81 class FetcherDelegate;
82
83 // Handles the result from a backend fetch.
84 void HandleSignatureResult(const std::string& signature,
85 const std::string& expire_date,
86 const ExtensionIdSet& invalid_ids);
87
88 // The final callback for when we're done.
89 SignatureCallback callback_;
90
91 // The current set of ids we're trying to verify. This may contain fewer ids
92 // than we started with.
93 ExtensionIdSet ids_;
94
95 // An array of random bytes used as an input to hash with the machine id,
96 // which will need to be persisted in the eventual InstallSignature we get.
97 std::string salt_;
98
99 // These are used to make the call to a backend server for a signature.
100 net::URLRequestContextGetter* context_getter_;
101 scoped_ptr<net::URLFetcher> url_fetcher_;
102 scoped_ptr<FetcherDelegate> delegate_;
103
104 DISALLOW_COPY_AND_ASSIGN(InstallSigner);
105};
106
107} // namespace extensions
108
109#endif // CHROME_BROWSER_EXTENSIONS_INSTALL_SIGNER_H_