Amin Hassani | 7982100 | 2019-05-06 17:40:49 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2019 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
| 16 | |
| 17 | #include "update_engine/payload_generator/payload_properties.h" |
| 18 | |
| 19 | #include <algorithm> |
| 20 | #include <string> |
| 21 | #include <utility> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include <base/json/json_writer.h> |
| 25 | #include <base/strings/string_util.h> |
| 26 | #include <base/values.h> |
| 27 | #include <brillo/data_encoding.h> |
| 28 | |
| 29 | #include "update_engine/common/constants.h" |
| 30 | #include "update_engine/common/hash_calculator.h" |
| 31 | #include "update_engine/common/utils.h" |
| 32 | #include "update_engine/payload_consumer/payload_metadata.h" |
| 33 | #include "update_engine/update_metadata.pb.h" |
| 34 | |
| 35 | using std::string; |
| 36 | using std::vector; |
| 37 | |
| 38 | namespace chromeos_update_engine { |
| 39 | |
| 40 | namespace { |
| 41 | // These ones are needed by the GoldenEye. |
| 42 | const char kPayloadPropertyJsonVersion[] = "version"; |
| 43 | const char kPayloadPropertyJsonPayloadHash[] = "sha256_hex"; |
| 44 | const char kPayloadPropertyJsonMetadataSize[] = "metadata_size"; |
| 45 | const char kPayloadPropertyJsonMetadataSignature[] = "metadata_signature"; |
| 46 | |
| 47 | // These are needed by the Nebraska and devserver. |
| 48 | const char kPayloadPropertyJsonPayloadSize[] = "size"; |
| 49 | const char kPayloadPropertyJsonIsDelta[] = "is_delta"; |
Amin Hassani | 7982100 | 2019-05-06 17:40:49 -0700 | [diff] [blame] | 50 | } // namespace |
| 51 | |
| 52 | PayloadProperties::PayloadProperties(const string& payload_path) |
| 53 | : payload_path_(payload_path) {} |
| 54 | |
| 55 | bool PayloadProperties::GetPropertiesAsJson(string* json_str) { |
| 56 | TEST_AND_RETURN_FALSE(LoadFromPayload()); |
| 57 | |
| 58 | base::DictionaryValue properties; |
| 59 | properties.SetInteger(kPayloadPropertyJsonVersion, version_); |
| 60 | properties.SetInteger(kPayloadPropertyJsonMetadataSize, metadata_size_); |
| 61 | properties.SetString(kPayloadPropertyJsonMetadataSignature, |
| 62 | metadata_signatures_); |
| 63 | properties.SetInteger(kPayloadPropertyJsonPayloadSize, payload_size_); |
| 64 | properties.SetString(kPayloadPropertyJsonPayloadHash, payload_hash_); |
| 65 | properties.SetBoolean(kPayloadPropertyJsonIsDelta, is_delta_); |
Amin Hassani | 7982100 | 2019-05-06 17:40:49 -0700 | [diff] [blame] | 66 | |
| 67 | return base::JSONWriter::Write(properties, json_str); |
| 68 | } |
| 69 | |
| 70 | bool PayloadProperties::GetPropertiesAsKeyValue(string* key_value_str) { |
| 71 | TEST_AND_RETURN_FALSE(LoadFromPayload()); |
| 72 | |
| 73 | brillo::KeyValueStore properties; |
| 74 | properties.SetString(kPayloadPropertyFileSize, std::to_string(payload_size_)); |
| 75 | properties.SetString(kPayloadPropertyMetadataSize, |
| 76 | std::to_string(metadata_size_)); |
| 77 | properties.SetString(kPayloadPropertyFileHash, payload_hash_); |
| 78 | properties.SetString(kPayloadPropertyMetadataHash, metadata_hash_); |
| 79 | |
| 80 | *key_value_str = properties.SaveToString(); |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | bool PayloadProperties::LoadFromPayload() { |
| 85 | PayloadMetadata payload_metadata; |
| 86 | DeltaArchiveManifest manifest; |
| 87 | Signatures metadata_signatures; |
| 88 | TEST_AND_RETURN_FALSE(payload_metadata.ParsePayloadFile( |
| 89 | payload_path_, &manifest, &metadata_signatures)); |
| 90 | |
| 91 | metadata_size_ = payload_metadata.GetMetadataSize(); |
| 92 | payload_size_ = utils::FileSize(payload_path_); |
| 93 | |
| 94 | brillo::Blob metadata_hash; |
| 95 | TEST_AND_RETURN_FALSE(HashCalculator::RawHashOfFile( |
| 96 | payload_path_, metadata_size_, &metadata_hash) == |
| 97 | static_cast<off_t>(metadata_size_)); |
| 98 | metadata_hash_ = brillo::data_encoding::Base64Encode(metadata_hash); |
| 99 | |
| 100 | brillo::Blob payload_hash; |
| 101 | TEST_AND_RETURN_FALSE(HashCalculator::RawHashOfFile( |
| 102 | payload_path_, payload_size_, &payload_hash) == |
| 103 | static_cast<off_t>(payload_size_)); |
| 104 | payload_hash_ = brillo::data_encoding::Base64Encode(payload_hash); |
| 105 | |
| 106 | if (payload_metadata.GetMetadataSignatureSize() > 0) { |
| 107 | TEST_AND_RETURN_FALSE(metadata_signatures.signatures_size() > 0); |
| 108 | vector<string> base64_signatures; |
| 109 | for (const auto& sig : metadata_signatures.signatures()) { |
| 110 | base64_signatures.push_back( |
| 111 | brillo::data_encoding::Base64Encode(sig.data())); |
| 112 | } |
| 113 | metadata_signatures_ = base::JoinString(base64_signatures, ":"); |
| 114 | } |
| 115 | |
Vyshu | a81598b | 2020-09-17 21:37:21 +0000 | [diff] [blame^] | 116 | is_delta_ = std::any_of(manifest.partitions().begin(), |
Amin Hassani | 7982100 | 2019-05-06 17:40:49 -0700 | [diff] [blame] | 117 | manifest.partitions().end(), |
| 118 | [](const PartitionUpdate& part) { |
| 119 | return part.has_old_partition_info(); |
| 120 | }); |
Amin Hassani | 7982100 | 2019-05-06 17:40:49 -0700 | [diff] [blame] | 121 | return true; |
| 122 | } |
| 123 | |
| 124 | } // namespace chromeos_update_engine |