xref: /aosp_15_r20/system/apex/apexd/apex_file.h (revision 33f3758387333dbd2962d7edbd98681940d895da)
1 /*
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 ANDROID_APEXD_APEX_FILE_H_
18 #define ANDROID_APEXD_APEX_FILE_H_
19 
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 #include <android-base/result.h>
25 #include <libavb/libavb.h>
26 
27 #include "apex_manifest.h"
28 
29 namespace android {
30 namespace apex {
31 
32 // Data needed to construct a valid VerityTable
33 struct ApexVerityData {
34   std::unique_ptr<AvbHashtreeDescriptor> desc;
35   std::string hash_algorithm;
36   std::string salt;
37   std::string root_digest;
38 };
39 
40 // Manages the content of an APEX package and provides utilities to navigate
41 // the content.
42 class ApexFile {
43  public:
44   static android::base::Result<ApexFile> Open(const std::string& path);
45 
46   ApexFile() = delete;
47   ApexFile(ApexFile&&) = default;
48   ApexFile& operator=(ApexFile&&) = default;
49   ApexFile(const ApexFile&) = default;
50   ApexFile& operator=(const ApexFile&) = default;
51 
GetPath()52   const std::string& GetPath() const { return apex_path_; }
GetImageOffset()53   const std::optional<uint32_t>& GetImageOffset() const {
54     return image_offset_;
55   }
GetImageSize()56   const std::optional<size_t>& GetImageSize() const { return image_size_; }
GetManifest()57   const ::apex::proto::ApexManifest& GetManifest() const { return manifest_; }
GetBundledPublicKey()58   const std::string& GetBundledPublicKey() const { return apex_pubkey_; }
GetFsType()59   const std::optional<std::string>& GetFsType() const { return fs_type_; }
60   android::base::Result<ApexVerityData> VerifyApexVerity(
61       const std::string& public_key) const;
IsCompressed()62   bool IsCompressed() const { return is_compressed_; }
63   android::base::Result<void> Decompress(const std::string& output_path) const;
64 
65  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   ApexFile(const std::string& apex_path,
67            const std::optional<uint32_t>& image_offset,
68            const std::optional<size_t>& image_size,
69            ::apex::proto::ApexManifest manifest, const std::string& apex_pubkey,
70            const std::optional<std::string>& fs_type, bool is_compressed)
71       : apex_path_(apex_path),
72         image_offset_(image_offset),
73         image_size_(image_size),
74         manifest_(std::move(manifest)),
75         apex_pubkey_(apex_pubkey),
76         fs_type_(fs_type),
77         is_compressed_(is_compressed) {}
78 
79   std::string apex_path_;
80   std::optional<uint32_t> image_offset_;
81   std::optional<size_t> image_size_;
82   ::apex::proto::ApexManifest manifest_;
83   std::string apex_pubkey_;
84   std::optional<std::string> fs_type_;
85   bool is_compressed_;
86 };
87 
88 }  // namespace apex
89 }  // namespace android
90 
91 #endif  // ANDROID_APEXD_APEX_FILE_H_
92