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 ApexFile() = delete; 46 ApexFile(ApexFile&&) = default; 47 ApexFile& operator=(ApexFile&&) = default; 48 GetPath()49 const std::string& GetPath() const { return apex_path_; } GetImageOffset()50 const std::optional<int32_t>& GetImageOffset() const { return image_offset_; } GetImageSize()51 const std::optional<size_t>& GetImageSize() const { return image_size_; } GetManifest()52 const ::apex::proto::ApexManifest& GetManifest() const { return manifest_; } GetBundledPublicKey()53 const std::string& GetBundledPublicKey() const { return apex_pubkey_; } GetFsType()54 const std::optional<std::string>& GetFsType() const { return fs_type_; } 55 android::base::Result<ApexVerityData> VerifyApexVerity( 56 const std::string& public_key) const; IsCompressed()57 bool IsCompressed() const { return is_compressed_; } 58 android::base::Result<void> Decompress(const std::string& output_path) const; 59 60 private: ApexFile(const std::string & apex_path,const std::optional<int32_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)61 ApexFile(const std::string& apex_path, 62 const std::optional<int32_t>& image_offset, 63 const std::optional<size_t>& image_size, 64 ::apex::proto::ApexManifest manifest, const std::string& apex_pubkey, 65 const std::optional<std::string>& fs_type, bool is_compressed) 66 : apex_path_(apex_path), 67 image_offset_(image_offset), 68 image_size_(image_size), 69 manifest_(std::move(manifest)), 70 apex_pubkey_(apex_pubkey), 71 fs_type_(fs_type), 72 is_compressed_(is_compressed) {} 73 74 std::string apex_path_; 75 std::optional<int32_t> image_offset_; 76 std::optional<size_t> image_size_; 77 ::apex::proto::ApexManifest manifest_; 78 std::string apex_pubkey_; 79 std::optional<std::string> fs_type_; 80 bool is_compressed_; 81 }; 82 83 } // namespace apex 84 } // namespace android 85 86 #endif // ANDROID_APEXD_APEX_FILE_H_ 87