Xiaochu Liu | cc7ff8c | 2018-07-23 11:54:02 -0700 | [diff] [blame] | 1 | // Copyright 2018 The Chromium OS 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 | |
Xiaochu Liu | cc7ff8c | 2018-07-23 11:54:02 -0700 | [diff] [blame] | 5 | #include <memory> |
| 6 | #include <utility> |
| 7 | |
| 8 | #include <base/json/json_string_value_serializer.h> |
| 9 | #include <base/strings/string_number_conversions.h> |
| 10 | |
Xiaochu Liu | e61e1d6 | 2018-11-12 13:20:09 -0800 | [diff] [blame] | 11 | #include "imageloader/manifest.h" |
| 12 | |
Xiaochu Liu | cc7ff8c | 2018-07-23 11:54:02 -0700 | [diff] [blame] | 13 | namespace imageloader { |
| 14 | |
| 15 | namespace { |
| 16 | // The current version of the manifest file. |
| 17 | constexpr int kCurrentManifestVersion = 1; |
| 18 | // The name of the version field in the manifest. |
| 19 | constexpr char kManifestVersionField[] = "manifest-version"; |
| 20 | // The name of the component version field in the manifest. |
| 21 | constexpr char kVersionField[] = "version"; |
| 22 | // The name of the field containing the image hash. |
| 23 | constexpr char kImageHashField[] = "image-sha256-hash"; |
| 24 | // The name of the bool field indicating whether component is removable. |
| 25 | constexpr char kIsRemovableField[] = "is-removable"; |
| 26 | // The name of the metadata field. |
| 27 | constexpr char kMetadataField[] = "metadata"; |
| 28 | // The name of the field containing the table hash. |
| 29 | constexpr char kTableHashField[] = "table-sha256-hash"; |
Colin Howes | dc15ba2 | 2018-12-12 11:02:42 -0800 | [diff] [blame] | 30 | // Optional manifest fields. |
Xiaochu Liu | cc7ff8c | 2018-07-23 11:54:02 -0700 | [diff] [blame] | 31 | constexpr char kFSType[] = "fs-type"; |
Colin Howes | dc15ba2 | 2018-12-12 11:02:42 -0800 | [diff] [blame] | 32 | constexpr char kId[] = "id"; |
Amin Hassani | 0badef1 | 2019-03-18 17:08:02 -0700 | [diff] [blame^] | 33 | constexpr char kPackage[] = "package"; |
Colin Howes | dc15ba2 | 2018-12-12 11:02:42 -0800 | [diff] [blame] | 34 | constexpr char kName[] = "name"; |
| 35 | constexpr char kImageType[] = "image-type"; |
| 36 | constexpr char kPreallocatedSize[] = "pre-allocated-size"; |
| 37 | constexpr char kSize[] = "size"; |
Xiaochu Liu | cc7ff8c | 2018-07-23 11:54:02 -0700 | [diff] [blame] | 38 | |
| 39 | bool GetSHA256FromString(const std::string& hash_str, |
| 40 | std::vector<uint8_t>* bytes) { |
| 41 | if (!base::HexStringToBytes(hash_str, bytes)) |
| 42 | return false; |
| 43 | return bytes->size() == 32; |
| 44 | } |
| 45 | |
| 46 | // Ensure the metadata entry is a dictionary mapping strings to strings and |
| 47 | // parse it into |out_metadata| and return true if so. |
| 48 | bool ParseMetadata(const base::Value* metadata_element, |
| 49 | std::map<std::string, std::string>* out_metadata) { |
| 50 | DCHECK(out_metadata); |
| 51 | |
| 52 | const base::DictionaryValue* metadata_dict = nullptr; |
| 53 | if (!metadata_element->GetAsDictionary(&metadata_dict)) |
| 54 | return false; |
| 55 | |
| 56 | base::DictionaryValue::Iterator it(*metadata_dict); |
| 57 | for (; !it.IsAtEnd(); it.Advance()) { |
| 58 | std::string parsed_value; |
| 59 | if (!it.value().GetAsString(&parsed_value)) { |
| 60 | LOG(ERROR) << "Key \"" << it.key() << "\" did not map to string value"; |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | (*out_metadata)[it.key()] = std::move(parsed_value); |
| 65 | } |
| 66 | |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | } // namespace |
| 71 | |
Ben Chan | 791bee2 | 2018-09-27 12:54:21 -0700 | [diff] [blame] | 72 | Manifest::Manifest() |
Xiaochu Liu | d9d853a | 2019-01-23 12:54:18 -0800 | [diff] [blame] | 73 | : manifest_version_(0), |
| 74 | fs_type_(FileSystem::kExt4), |
| 75 | preallocated_size_(-1), |
| 76 | is_removable_(false) {} |
Xiaochu Liu | cc7ff8c | 2018-07-23 11:54:02 -0700 | [diff] [blame] | 77 | |
| 78 | bool Manifest::ParseManifest(const std::string& manifest_raw) { |
| 79 | // Now deserialize the manifest json and read out the rest of the component. |
| 80 | int error_code; |
| 81 | std::string error_message; |
| 82 | JSONStringValueDeserializer deserializer(manifest_raw); |
| 83 | std::unique_ptr<base::Value> value = |
| 84 | deserializer.Deserialize(&error_code, &error_message); |
| 85 | |
| 86 | if (!value) { |
| 87 | LOG(ERROR) << "Could not deserialize the manifest file. Error " |
| 88 | << error_code << ": " << error_message; |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | base::DictionaryValue* manifest_dict = nullptr; |
| 93 | if (!value->GetAsDictionary(&manifest_dict)) { |
| 94 | LOG(ERROR) << "Could not parse manifest file as JSON."; |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | // This will have to be changed if the manifest version is bumped. |
| 99 | int version; |
| 100 | if (!manifest_dict->GetInteger(kManifestVersionField, &version)) { |
| 101 | LOG(ERROR) << "Could not parse manifest version field from manifest."; |
| 102 | return false; |
| 103 | } |
| 104 | if (version != kCurrentManifestVersion) { |
| 105 | LOG(ERROR) << "Unsupported version of the manifest."; |
| 106 | return false; |
| 107 | } |
| 108 | manifest_version_ = version; |
| 109 | |
| 110 | std::string image_hash_str; |
| 111 | if (!manifest_dict->GetString(kImageHashField, &image_hash_str)) { |
| 112 | LOG(ERROR) << "Could not parse image hash from manifest."; |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | if (!GetSHA256FromString(image_hash_str, &(image_sha256_))) { |
| 117 | LOG(ERROR) << "Could not convert image hash to bytes."; |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | std::string table_hash_str; |
| 122 | if (!manifest_dict->GetString(kTableHashField, &table_hash_str)) { |
| 123 | LOG(ERROR) << "Could not parse table hash from manifest."; |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | if (!GetSHA256FromString(table_hash_str, &(table_sha256_))) { |
| 128 | LOG(ERROR) << "Could not convert table hash to bytes."; |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | if (!manifest_dict->GetString(kVersionField, &(version_))) { |
| 133 | LOG(ERROR) << "Could not parse component version from manifest."; |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | // The fs_type field is optional, and squashfs by default. |
| 138 | fs_type_ = FileSystem::kSquashFS; |
| 139 | std::string fs_type; |
| 140 | if (manifest_dict->GetString(kFSType, &fs_type)) { |
| 141 | if (fs_type == "ext4") { |
| 142 | fs_type_ = FileSystem::kExt4; |
| 143 | } else if (fs_type == "squashfs") { |
| 144 | fs_type_ = FileSystem::kSquashFS; |
| 145 | } else { |
| 146 | LOG(ERROR) << "Unsupported file system type: " << fs_type; |
| 147 | return false; |
| 148 | } |
| 149 | } |
| 150 | |
Colin Howes | dc15ba2 | 2018-12-12 11:02:42 -0800 | [diff] [blame] | 151 | if (!manifest_dict->GetBoolean(kIsRemovableField, &is_removable_)) { |
Xiaochu Liu | cc7ff8c | 2018-07-23 11:54:02 -0700 | [diff] [blame] | 152 | // If is_removable field does not exist, by default it is false. |
| 153 | is_removable_ = false; |
| 154 | } |
| 155 | |
Colin Howes | dc15ba2 | 2018-12-12 11:02:42 -0800 | [diff] [blame] | 156 | // All of these fields are optional. |
| 157 | manifest_dict->GetString(kId, &id_); |
Amin Hassani | 0badef1 | 2019-03-18 17:08:02 -0700 | [diff] [blame^] | 158 | manifest_dict->GetString(kPackage, &package_); |
Colin Howes | dc15ba2 | 2018-12-12 11:02:42 -0800 | [diff] [blame] | 159 | manifest_dict->GetString(kName, &name_); |
| 160 | manifest_dict->GetString(kImageType, &image_type_); |
| 161 | // TODO(http://crbug.com/904539 comment #11): This can overflow, should get |
| 162 | // binary blob and convert to size_t. |
| 163 | manifest_dict->GetInteger(kPreallocatedSize, &preallocated_size_); |
| 164 | // TODO(http://crbug.com/904539 comment #11): This can overflow, should get |
| 165 | // binary blob and convert to size_t. |
| 166 | manifest_dict->GetInteger(kSize, &size_); |
| 167 | |
Xiaochu Liu | cc7ff8c | 2018-07-23 11:54:02 -0700 | [diff] [blame] | 168 | // Copy out the metadata, if it's there. |
| 169 | const base::Value* metadata = nullptr; |
| 170 | if (manifest_dict->Get(kMetadataField, &metadata)) { |
Colin Howes | dc15ba2 | 2018-12-12 11:02:42 -0800 | [diff] [blame] | 171 | if (!ParseMetadata(metadata, &metadata_)) { |
Xiaochu Liu | cc7ff8c | 2018-07-23 11:54:02 -0700 | [diff] [blame] | 172 | LOG(ERROR) << "Manifest metadata was malformed"; |
| 173 | return false; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | return true; |
| 178 | } |
| 179 | |
Xiaochu Liu | cc7ff8c | 2018-07-23 11:54:02 -0700 | [diff] [blame] | 180 | } // namespace imageloader |