blob: 32923518b75787bc0e30d54e91a79803a0fbd40c [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
20#include <inttypes.h>
21
22#include <string>
23#include <vector>
24
Sen Jiang08c6da12019-01-07 18:28:56 -080025#include <base/macros.h>
Sen Jiang9c89e842018-02-02 13:51:21 -080026#include <brillo/secure_blob.h>
27
28#include "update_engine/common/error_code.h"
Amin Hassanifac20222020-01-21 11:32:50 -080029#include "update_engine/common/hardware_interface.h"
Sen Jiang9c89e842018-02-02 13:51:21 -080030#include "update_engine/common/platform_constants.h"
31#include "update_engine/update_metadata.pb.h"
32
33namespace chromeos_update_engine {
34
35enum class MetadataParseResult {
36 kSuccess,
37 kError,
38 kInsufficientData,
39};
40
41// This class parses payload metadata and validate its signature.
42class PayloadMetadata {
43 public:
44 static const uint64_t kDeltaVersionOffset;
45 static const uint64_t kDeltaVersionSize;
46 static const uint64_t kDeltaManifestSizeOffset;
47 static const uint64_t kDeltaManifestSizeSize;
48 static const uint64_t kDeltaMetadataSignatureSizeSize;
49
50 PayloadMetadata() = default;
51
52 // Attempts to parse the update payload header starting from the beginning of
53 // |payload|. On success, returns kMetadataParseSuccess. Returns
54 // kMetadataParseInsufficientData if more data is needed to parse the complete
55 // metadata. Returns kMetadataParseError if the metadata can't be parsed given
56 // the payload.
57 MetadataParseResult ParsePayloadHeader(const brillo::Blob& payload,
Amin Hassanifac20222020-01-21 11:32:50 -080058 HardwareInterface* hardware,
Sen Jiang9c89e842018-02-02 13:51:21 -080059 ErrorCode* error);
Sen Jiang44ac3ea2018-10-18 15:10:20 -070060 // Simpler version of the above, returns true on success.
Amin Hassanifac20222020-01-21 11:32:50 -080061 bool ParsePayloadHeader(const brillo::Blob& payload,
62 HardwareInterface* hardware);
Sen Jiang9c89e842018-02-02 13:51:21 -080063
64 // Given the |payload|, verifies that the signed hash of its metadata matches
65 // |metadata_signature| (if present) or the metadata signature in payload
66 // itself (if present). Returns ErrorCode::kSuccess on match or a suitable
67 // error code otherwise. This method must be called before any part of the
68 // metadata is parsed so that a man-in-the-middle attack on the SSL connection
69 // to the payload server doesn't exploit any vulnerability in the code that
70 // parses the protocol buffer.
71 ErrorCode ValidateMetadataSignature(const brillo::Blob& payload,
Sen Jiang08c6da12019-01-07 18:28:56 -080072 const std::string& metadata_signature,
73 const std::string& pem_public_key) const;
Sen Jiang9c89e842018-02-02 13:51:21 -080074
75 // Returns the major payload version. If the version was not yet parsed,
76 // returns zero.
77 uint64_t GetMajorVersion() const { return major_payload_version_; }
78
79 // Returns the size of the payload metadata, which includes the payload header
80 // and the manifest. If the header was not yet parsed, returns zero.
81 uint64_t GetMetadataSize() const { return metadata_size_; }
82
83 // Returns the size of the payload metadata signature. If the header was not
84 // yet parsed, returns zero.
85 uint32_t GetMetadataSignatureSize() const { return metadata_signature_size_; }
86
87 // Set |*out_manifest| to the manifest in |payload|.
88 // Returns true on success.
89 bool GetManifest(const brillo::Blob& payload,
90 DeltaArchiveManifest* out_manifest) const;
91
Amin Hassani79821002019-05-06 17:40:49 -070092 // Parses a payload file |payload_path| and prepares the metadata properties,
93 // manifest and metadata signatures. Can be used as an easy to use utility to
94 // get the payload information without manually the process.
95 bool ParsePayloadFile(const std::string& payload_path,
96 DeltaArchiveManifest* manifest,
97 Signatures* metadata_signatures);
98
Sen Jiang9c89e842018-02-02 13:51:21 -080099 private:
Amin Hassanifac20222020-01-21 11:32:50 -0800100 // Set |*out_offset| to the byte offset at which the manifest protobuf begins
101 // in a payload. Return true on success, false if the offset is unknown.
102 bool GetManifestOffset(uint64_t* out_offset) const;
Sen Jiang9c89e842018-02-02 13:51:21 -0800103
Amin Hassanifac20222020-01-21 11:32:50 -0800104 // Set |*out_offset| to the byte offset where the size of the metadata
105 // signature is stored in a payload. Return true on success, if this field is
106 // not present in the payload, return false.
107 bool GetMetadataSignatureSizeOffset(uint64_t* out_offset) const;
Sen Jiang9c89e842018-02-02 13:51:21 -0800108
109 uint64_t metadata_size_{0};
110 uint64_t manifest_size_{0};
111 uint32_t metadata_signature_size_{0};
112 uint64_t major_payload_version_{0};
113
114 DISALLOW_COPY_AND_ASSIGN(PayloadMetadata);
115};
116
117} // namespace chromeos_update_engine
118
119#endif // UPDATE_ENGINE_PAYLOAD_CONSUMER_PAYLOAD_METADATA_H_