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";
50
51 // These are JSON specific properties to handle 64-bit sizes (> 53-bits).
52 const char kPayloadPropertyJsonMetadataSizeStr[] = "metadata_size_str";
53 const char kPayloadPropertyJsonPayloadSizeStr[] = "size_str";
54 } // namespace
55
PayloadProperties(const string & payload_path)56 PayloadProperties::PayloadProperties(const string& payload_path)
57 : payload_path_(payload_path) {}
58
GetPropertiesAsJson(string * json_str)59 bool PayloadProperties::GetPropertiesAsJson(string* json_str) {
60 TEST_AND_RETURN_FALSE(LoadFromPayload());
61
62 base::DictionaryValue properties;
63 properties.SetInteger(kPayloadPropertyJsonVersion, version_);
64 properties.SetInteger(kPayloadPropertyJsonMetadataSize, metadata_size_);
65 properties.SetString(kPayloadPropertyJsonMetadataSizeStr,
66 std::to_string(metadata_size_));
67 properties.SetString(kPayloadPropertyJsonMetadataSignature,
68 metadata_signatures_);
69 properties.SetInteger(kPayloadPropertyJsonPayloadSize, payload_size_);
70 properties.SetString(kPayloadPropertyJsonPayloadSizeStr,
71 std::to_string(payload_size_));
72 properties.SetString(kPayloadPropertyJsonPayloadHash, payload_hash_);
73 properties.SetBoolean(kPayloadPropertyJsonIsDelta, is_delta_);
74
75 return base::JSONWriter::Write(properties, json_str);
76 }
77
GetPropertiesAsKeyValue(string * key_value_str)78 bool PayloadProperties::GetPropertiesAsKeyValue(string* key_value_str) {
79 TEST_AND_RETURN_FALSE(LoadFromPayload());
80
81 brillo::KeyValueStore properties;
82 properties.SetString(kPayloadPropertyFileSize, std::to_string(payload_size_));
83 properties.SetString(kPayloadPropertyMetadataSize,
84 std::to_string(metadata_size_));
85 properties.SetString(kPayloadPropertyFileHash, payload_hash_);
86 properties.SetString(kPayloadPropertyMetadataHash, metadata_hash_);
87
88 *key_value_str = properties.SaveToString();
89 return true;
90 }
91
LoadFromPayload()92 bool PayloadProperties::LoadFromPayload() {
93 PayloadMetadata payload_metadata;
94 DeltaArchiveManifest manifest;
95 Signatures metadata_signatures;
96 TEST_AND_RETURN_FALSE(payload_metadata.ParsePayloadFile(
97 payload_path_, &manifest, &metadata_signatures));
98
99 metadata_size_ = payload_metadata.GetMetadataSize();
100 payload_size_ = utils::FileSize(payload_path_);
101
102 brillo::Blob metadata_hash;
103 TEST_AND_RETURN_FALSE(HashCalculator::RawHashOfFile(
104 payload_path_, metadata_size_, &metadata_hash) ==
105 static_cast<off_t>(metadata_size_));
106 metadata_hash_ = brillo::data_encoding::Base64Encode(metadata_hash);
107
108 brillo::Blob payload_hash;
109 TEST_AND_RETURN_FALSE(HashCalculator::RawHashOfFile(
110 payload_path_, payload_size_, &payload_hash) ==
111 static_cast<off_t>(payload_size_));
112 payload_hash_ = brillo::data_encoding::Base64Encode(payload_hash);
113
114 if (payload_metadata.GetMetadataSignatureSize() > 0) {
115 TEST_AND_RETURN_FALSE(metadata_signatures.signatures_size() > 0);
116 vector<string> base64_signatures;
117 for (const auto& sig : metadata_signatures.signatures()) {
118 base64_signatures.push_back(
119 brillo::data_encoding::Base64Encode(sig.data()));
120 }
121 metadata_signatures_ = base::JoinString(base64_signatures, ":");
122 }
123
124 is_delta_ = std::any_of(manifest.partitions().begin(),
125 manifest.partitions().end(),
126 [](const PartitionUpdate& part) {
127 return part.has_old_partition_info();
128 });
129 return true;
130 }
131
132 } // namespace chromeos_update_engine
133