xref: /aosp_15_r20/system/apex/apexd/apex_file.h (revision 33f3758387333dbd2962d7edbd98681940d895da)
1*33f37583SAndroid Build Coastguard Worker /*
2*33f37583SAndroid Build Coastguard Worker  * Copyright (C) 2018 The Android Open Source Project
3*33f37583SAndroid Build Coastguard Worker  *
4*33f37583SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*33f37583SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*33f37583SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*33f37583SAndroid Build Coastguard Worker  *
8*33f37583SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*33f37583SAndroid Build Coastguard Worker  *
10*33f37583SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*33f37583SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*33f37583SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*33f37583SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*33f37583SAndroid Build Coastguard Worker  * limitations under the License.
15*33f37583SAndroid Build Coastguard Worker  */
16*33f37583SAndroid Build Coastguard Worker 
17*33f37583SAndroid Build Coastguard Worker #ifndef ANDROID_APEXD_APEX_FILE_H_
18*33f37583SAndroid Build Coastguard Worker #define ANDROID_APEXD_APEX_FILE_H_
19*33f37583SAndroid Build Coastguard Worker 
20*33f37583SAndroid Build Coastguard Worker #include <memory>
21*33f37583SAndroid Build Coastguard Worker #include <string>
22*33f37583SAndroid Build Coastguard Worker #include <vector>
23*33f37583SAndroid Build Coastguard Worker 
24*33f37583SAndroid Build Coastguard Worker #include <android-base/result.h>
25*33f37583SAndroid Build Coastguard Worker #include <libavb/libavb.h>
26*33f37583SAndroid Build Coastguard Worker 
27*33f37583SAndroid Build Coastguard Worker #include "apex_manifest.h"
28*33f37583SAndroid Build Coastguard Worker 
29*33f37583SAndroid Build Coastguard Worker namespace android {
30*33f37583SAndroid Build Coastguard Worker namespace apex {
31*33f37583SAndroid Build Coastguard Worker 
32*33f37583SAndroid Build Coastguard Worker // Data needed to construct a valid VerityTable
33*33f37583SAndroid Build Coastguard Worker struct ApexVerityData {
34*33f37583SAndroid Build Coastguard Worker   std::unique_ptr<AvbHashtreeDescriptor> desc;
35*33f37583SAndroid Build Coastguard Worker   std::string hash_algorithm;
36*33f37583SAndroid Build Coastguard Worker   std::string salt;
37*33f37583SAndroid Build Coastguard Worker   std::string root_digest;
38*33f37583SAndroid Build Coastguard Worker };
39*33f37583SAndroid Build Coastguard Worker 
40*33f37583SAndroid Build Coastguard Worker // Manages the content of an APEX package and provides utilities to navigate
41*33f37583SAndroid Build Coastguard Worker // the content.
42*33f37583SAndroid Build Coastguard Worker class ApexFile {
43*33f37583SAndroid Build Coastguard Worker  public:
44*33f37583SAndroid Build Coastguard Worker   static android::base::Result<ApexFile> Open(const std::string& path);
45*33f37583SAndroid Build Coastguard Worker 
46*33f37583SAndroid Build Coastguard Worker   ApexFile() = delete;
47*33f37583SAndroid Build Coastguard Worker   ApexFile(ApexFile&&) = default;
48*33f37583SAndroid Build Coastguard Worker   ApexFile& operator=(ApexFile&&) = default;
49*33f37583SAndroid Build Coastguard Worker   ApexFile(const ApexFile&) = default;
50*33f37583SAndroid Build Coastguard Worker   ApexFile& operator=(const ApexFile&) = default;
51*33f37583SAndroid Build Coastguard Worker 
GetPath()52*33f37583SAndroid Build Coastguard Worker   const std::string& GetPath() const { return apex_path_; }
GetImageOffset()53*33f37583SAndroid Build Coastguard Worker   const std::optional<uint32_t>& GetImageOffset() const {
54*33f37583SAndroid Build Coastguard Worker     return image_offset_;
55*33f37583SAndroid Build Coastguard Worker   }
GetImageSize()56*33f37583SAndroid Build Coastguard Worker   const std::optional<size_t>& GetImageSize() const { return image_size_; }
GetManifest()57*33f37583SAndroid Build Coastguard Worker   const ::apex::proto::ApexManifest& GetManifest() const { return manifest_; }
GetBundledPublicKey()58*33f37583SAndroid Build Coastguard Worker   const std::string& GetBundledPublicKey() const { return apex_pubkey_; }
GetFsType()59*33f37583SAndroid Build Coastguard Worker   const std::optional<std::string>& GetFsType() const { return fs_type_; }
60*33f37583SAndroid Build Coastguard Worker   android::base::Result<ApexVerityData> VerifyApexVerity(
61*33f37583SAndroid Build Coastguard Worker       const std::string& public_key) const;
IsCompressed()62*33f37583SAndroid Build Coastguard Worker   bool IsCompressed() const { return is_compressed_; }
63*33f37583SAndroid Build Coastguard Worker   android::base::Result<void> Decompress(const std::string& output_path) const;
64*33f37583SAndroid Build Coastguard Worker 
65*33f37583SAndroid Build Coastguard Worker  private:
ApexFile(const std::string & apex_path,const std::optional<uint32_t> & image_offset,const std::optional<size_t> & image_size,::apex::proto::ApexManifest manifest,const std::string & apex_pubkey,const std::optional<std::string> & fs_type,bool is_compressed)66*33f37583SAndroid Build Coastguard Worker   ApexFile(const std::string& apex_path,
67*33f37583SAndroid Build Coastguard Worker            const std::optional<uint32_t>& image_offset,
68*33f37583SAndroid Build Coastguard Worker            const std::optional<size_t>& image_size,
69*33f37583SAndroid Build Coastguard Worker            ::apex::proto::ApexManifest manifest, const std::string& apex_pubkey,
70*33f37583SAndroid Build Coastguard Worker            const std::optional<std::string>& fs_type, bool is_compressed)
71*33f37583SAndroid Build Coastguard Worker       : apex_path_(apex_path),
72*33f37583SAndroid Build Coastguard Worker         image_offset_(image_offset),
73*33f37583SAndroid Build Coastguard Worker         image_size_(image_size),
74*33f37583SAndroid Build Coastguard Worker         manifest_(std::move(manifest)),
75*33f37583SAndroid Build Coastguard Worker         apex_pubkey_(apex_pubkey),
76*33f37583SAndroid Build Coastguard Worker         fs_type_(fs_type),
77*33f37583SAndroid Build Coastguard Worker         is_compressed_(is_compressed) {}
78*33f37583SAndroid Build Coastguard Worker 
79*33f37583SAndroid Build Coastguard Worker   std::string apex_path_;
80*33f37583SAndroid Build Coastguard Worker   std::optional<uint32_t> image_offset_;
81*33f37583SAndroid Build Coastguard Worker   std::optional<size_t> image_size_;
82*33f37583SAndroid Build Coastguard Worker   ::apex::proto::ApexManifest manifest_;
83*33f37583SAndroid Build Coastguard Worker   std::string apex_pubkey_;
84*33f37583SAndroid Build Coastguard Worker   std::optional<std::string> fs_type_;
85*33f37583SAndroid Build Coastguard Worker   bool is_compressed_;
86*33f37583SAndroid Build Coastguard Worker };
87*33f37583SAndroid Build Coastguard Worker 
88*33f37583SAndroid Build Coastguard Worker }  // namespace apex
89*33f37583SAndroid Build Coastguard Worker }  // namespace android
90*33f37583SAndroid Build Coastguard Worker 
91*33f37583SAndroid Build Coastguard Worker #endif  // ANDROID_APEXD_APEX_FILE_H_
92