verified_contents.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2014 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. #ifndef EXTENSIONS_BROWSER_VERIFIED_CONTENTS_H_
  5. #define EXTENSIONS_BROWSER_VERIFIED_CONTENTS_H_
  6. #include <stdint.h>
  7. #include <map>
  8. #include <memory>
  9. #include <string>
  10. #include <vector>
  11. #include "base/containers/span.h"
  12. #include "base/files/file_path.h"
  13. #include "base/version.h"
  14. #include "extensions/browser/content_verifier/content_verifier_utils.h"
  15. namespace extensions {
  16. using CanonicalRelativePath = content_verifier_utils::CanonicalRelativePath;
  17. // This class encapsulates the data in a "verified_contents.json" file
  18. // generated by the webstore for a .crx file. That data includes a set of
  19. // signed expected hashes of file content which can be used to check for
  20. // corruption of extension files on local disk.
  21. class VerifiedContents {
  22. public:
  23. VerifiedContents(const VerifiedContents&) = delete;
  24. VerifiedContents& operator=(const VerifiedContents&) = delete;
  25. ~VerifiedContents();
  26. // Returns verified contents after successfully parsing verified_contents.json
  27. // file at |path| and validating the enclosed signature. Returns nullptr on
  28. // failure.
  29. // Note: |public_key| must remain valid for the lifetime of the returned
  30. // object.
  31. static std::unique_ptr<VerifiedContents> CreateFromFile(
  32. base::span<const uint8_t> public_key,
  33. const base::FilePath& path);
  34. // Returns verified contents after successfully parsing |contents| and
  35. // validating the enclosed signature. Returns nullptr on failure. Note:
  36. // |public_key| must remain valid for the lifetime of the returned object.
  37. static std::unique_ptr<VerifiedContents> Create(
  38. base::span<const uint8_t> public_key,
  39. base::StringPiece contents);
  40. int block_size() const { return block_size_; }
  41. const std::string& extension_id() const { return extension_id_; }
  42. const base::Version& version() const { return version_; }
  43. bool HasTreeHashRoot(const base::FilePath& relative_path) const;
  44. bool TreeHashRootEquals(const base::FilePath& relative_path,
  45. const std::string& expected) const;
  46. bool TreeHashRootEqualsForCanonicalPath(
  47. const CanonicalRelativePath& canonical_relative_path,
  48. const std::string& expected) const;
  49. // If InitFrom has not been called yet, or was used in "ignore invalid
  50. // signature" mode, this can return false.
  51. bool valid_signature() { return valid_signature_; }
  52. private:
  53. // Note: the public_key must remain valid for the lifetime of this object.
  54. explicit VerifiedContents(base::span<const uint8_t> public_key);
  55. // Returns the base64url-decoded "payload" field from the |contents|, if
  56. // the signature was valid.
  57. bool GetPayload(base::StringPiece contents, std::string* payload);
  58. // The |protected_value| and |payload| arguments should be base64url encoded
  59. // strings, and |signature_bytes| should be a byte array. See comments in the
  60. // .cc file on GetPayload for where these come from in the overall input
  61. // file.
  62. bool VerifySignature(const std::string& protected_value,
  63. const std::string& payload,
  64. const std::string& signature_bytes);
  65. // The public key we should use for signature verification.
  66. base::span<const uint8_t> public_key_;
  67. // Indicates whether the signature was successfully validated or not.
  68. bool valid_signature_;
  69. // The block size used for computing the treehash root hashes.
  70. int block_size_;
  71. // Information about which extension these signed hashes are for.
  72. std::string extension_id_;
  73. base::Version version_;
  74. // The expected treehash root hashes for each file.
  75. // For case-sensitive systems (linux/chromeos) the key is exact cased, but for
  76. // case-insensitive systems (win/macos) the key is lower cased to support
  77. // case-insensitive lookups.
  78. //
  79. // We use a multi-map here so that we can do fast lookups of paths from
  80. // requests on case-insensitive systems (windows, mac) where the request path
  81. // might not have the exact right capitalization. Note that this doesn't
  82. // affect case-sensitive systems (linux, chromeos) as we use the exact cased
  83. // keys.
  84. // TODO(crbug.com/29941) - we should give developers client-side warnings in
  85. // each of those cases, and have the webstore reject the cases they can
  86. // statically detect.
  87. typedef std::multimap<CanonicalRelativePath, std::string> RootHashes;
  88. RootHashes root_hashes_;
  89. };
  90. } // namespace extensions
  91. #endif // EXTENSIONS_BROWSER_VERIFIED_CONTENTS_H_