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