blob: fd24f2043f23c45ae01c970fa301e450bde51202 [file] [log] [blame]
Sen Jiang9c89e842018-02-02 13:51:21 -08001//
2// Copyright (C) 2018 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#ifndef UPDATE_ENGINE_PAYLOAD_CONSUMER_PAYLOAD_METADATA_H_
18#define UPDATE_ENGINE_PAYLOAD_CONSUMER_PAYLOAD_METADATA_H_
19
Sen Jiang9c89e842018-02-02 13:51:21 -080020#include <string>
Sen Jiang9c89e842018-02-02 13:51:21 -080021
Kokoa Matsuda1783eb32024-10-16 13:49:17 +090022#include <android-base/macros.h>
Sen Jiang9c89e842018-02-02 13:51:21 -080023#include <brillo/secure_blob.h>
24
Kelvin Zhang44bcf1f2024-12-03 10:54:14 -080025#include "update_engine/common/utils.h"
Sen Jiang9c89e842018-02-02 13:51:21 -080026#include "update_engine/common/error_code.h"
Tianjie Xu7a78d632019-10-08 16:32:39 -070027#include "update_engine/payload_consumer/payload_verifier.h"
Sen Jiang9c89e842018-02-02 13:51:21 -080028#include "update_engine/update_metadata.pb.h"
29
30namespace chromeos_update_engine {
31
32enum class MetadataParseResult {
33 kSuccess,
34 kError,
35 kInsufficientData,
36};
37
38// This class parses payload metadata and validate its signature.
39class PayloadMetadata {
40 public:
41 static const uint64_t kDeltaVersionOffset;
42 static const uint64_t kDeltaVersionSize;
43 static const uint64_t kDeltaManifestSizeOffset;
44 static const uint64_t kDeltaManifestSizeSize;
45 static const uint64_t kDeltaMetadataSignatureSizeSize;
46
47 PayloadMetadata() = default;
48
49 // Attempts to parse the update payload header starting from the beginning of
50 // |payload|. On success, returns kMetadataParseSuccess. Returns
51 // kMetadataParseInsufficientData if more data is needed to parse the complete
52 // metadata. Returns kMetadataParseError if the metadata can't be parsed given
53 // the payload.
54 MetadataParseResult ParsePayloadHeader(const brillo::Blob& payload,
Sen Jiang9c89e842018-02-02 13:51:21 -080055 ErrorCode* error);
Kelvin Zhang44bcf1f2024-12-03 10:54:14 -080056 MetadataParseResult ParsePayloadHeader(std::string_view payload,
57 ErrorCode* error) {
58 return ParsePayloadHeader(reinterpret_cast<const uint8_t*>(payload.data()),
59 payload.size(),
60 error);
61 }
Kelvin Zhang9b8a2bb2021-02-03 15:33:08 -050062 MetadataParseResult ParsePayloadHeader(const unsigned char* payload,
63 size_t size,
64 ErrorCode* error);
Sen Jiang44ac3ea2018-10-18 15:10:20 -070065 // Simpler version of the above, returns true on success.
66 bool ParsePayloadHeader(const brillo::Blob& payload);
Sen Jiang9c89e842018-02-02 13:51:21 -080067
68 // Given the |payload|, verifies that the signed hash of its metadata matches
69 // |metadata_signature| (if present) or the metadata signature in payload
70 // itself (if present). Returns ErrorCode::kSuccess on match or a suitable
71 // error code otherwise. This method must be called before any part of the
Tianjiee283ce42020-07-29 11:37:51 -070072 // metadata is parsed so that an on-path attack on the SSL connection
Sen Jiang9c89e842018-02-02 13:51:21 -080073 // to the payload server doesn't exploit any vulnerability in the code that
74 // parses the protocol buffer.
Tianjie Xu7a78d632019-10-08 16:32:39 -070075 ErrorCode ValidateMetadataSignature(
Kelvin Zhang44bcf1f2024-12-03 10:54:14 -080076 std::string_view payload,
Tianjie Xu7a78d632019-10-08 16:32:39 -070077 const std::string& metadata_signature,
78 const PayloadVerifier& payload_verifier) const;
Kelvin Zhang44bcf1f2024-12-03 10:54:14 -080079 ErrorCode ValidateMetadataSignature(
80 const std::vector<uint8_t>& payload,
81 const std::string& metadata_signature,
82 const PayloadVerifier& payload_verifier) const {
83 return ValidateMetadataSignature(
84 ToStringView(payload), metadata_signature, payload_verifier);
85 }
Sen Jiang9c89e842018-02-02 13:51:21 -080086
87 // Returns the major payload version. If the version was not yet parsed,
88 // returns zero.
89 uint64_t GetMajorVersion() const { return major_payload_version_; }
90
91 // Returns the size of the payload metadata, which includes the payload header
92 // and the manifest. If the header was not yet parsed, returns zero.
93 uint64_t GetMetadataSize() const { return metadata_size_; }
94
95 // Returns the size of the payload metadata signature. If the header was not
96 // yet parsed, returns zero.
97 uint32_t GetMetadataSignatureSize() const { return metadata_signature_size_; }
98
99 // Set |*out_manifest| to the manifest in |payload|.
100 // Returns true on success.
101 bool GetManifest(const brillo::Blob& payload,
102 DeltaArchiveManifest* out_manifest) const;
103
Kelvin Zhang9b8a2bb2021-02-03 15:33:08 -0500104 bool GetManifest(const unsigned char* payload,
105 size_t size,
106 DeltaArchiveManifest* out_manifest) const;
Kelvin Zhang44bcf1f2024-12-03 10:54:14 -0800107 bool GetManifest(std::string_view payload,
108 DeltaArchiveManifest* out_manifest) const {
109 return GetManifest(reinterpret_cast<const uint8_t*>(payload.data()),
110 payload.size(),
111 out_manifest);
112 }
Kelvin Zhang9b8a2bb2021-02-03 15:33:08 -0500113
Amin Hassani79821002019-05-06 17:40:49 -0700114 // Parses a payload file |payload_path| and prepares the metadata properties,
115 // manifest and metadata signatures. Can be used as an easy to use utility to
116 // get the payload information without manually the process.
117 bool ParsePayloadFile(const std::string& payload_path,
118 DeltaArchiveManifest* manifest,
119 Signatures* metadata_signatures);
Sen Jiang9c89e842018-02-02 13:51:21 -0800120
Sen Jiang9c89e842018-02-02 13:51:21 -0800121 private:
Amin Hassani822d4852020-03-10 01:50:42 +0000122 // Returns the byte offset at which the manifest protobuf begins in a payload.
123 uint64_t GetManifestOffset() const;
Sen Jiang9c89e842018-02-02 13:51:21 -0800124
Amin Hassani822d4852020-03-10 01:50:42 +0000125 // Returns the byte offset where the size of the metadata signature is stored
126 // in a payload.
127 uint64_t GetMetadataSignatureSizeOffset() const;
Sen Jiang9c89e842018-02-02 13:51:21 -0800128
129 uint64_t metadata_size_{0};
130 uint64_t manifest_size_{0};
131 uint32_t metadata_signature_size_{0};
132 uint64_t major_payload_version_{0};
133
134 DISALLOW_COPY_AND_ASSIGN(PayloadMetadata);
135};
136
137} // namespace chromeos_update_engine
138
139#endif // UPDATE_ENGINE_PAYLOAD_CONSUMER_PAYLOAD_METADATA_H_