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 #include <ziparchive/zip_archive.h>
27 
28 #include "apex_constants.h"
29 #include "apex_manifest.h"
30 
31 namespace android {
32 namespace apex {
33 
34 // Data needed to construct a valid VerityTable
35 struct ApexVerityData {
36   std::unique_ptr<AvbHashtreeDescriptor> desc;
37   std::string hash_algorithm;
38   std::string salt;
39   std::string root_digest;
40 };
41 
42 // Manages the content of an APEX package and provides utilities to navigate
43 // the content.
44 class ApexFile {
45  public:
46   static android::base::Result<ApexFile> Open(const std::string& path);
47   ApexFile() = delete;
48   ApexFile(ApexFile&&) = default;
49 
GetPath()50   const std::string& GetPath() const { return apex_path_; }
GetImageOffset()51   int32_t GetImageOffset() const { return image_offset_; }
GetImageSize()52   size_t GetImageSize() const { return image_size_; }
GetManifest()53   const ApexManifest& GetManifest() const { return manifest_; }
GetBundledPublicKey()54   const std::string& GetBundledPublicKey() const { return apex_pubkey_; }
IsBuiltin()55   bool IsBuiltin() const { return is_builtin_; }
56   android::base::Result<ApexVerityData> VerifyApexVerity() const;
57   android::base::Result<void> VerifyManifestMatches(
58       const std::string& mount_path) const;
59 
60  private:
ApexFile(const std::string & apex_path,int32_t image_offset,size_t image_size,ApexManifest manifest,const std::string & apex_pubkey,bool is_builtin)61   ApexFile(const std::string& apex_path, int32_t image_offset,
62            size_t image_size, ApexManifest manifest,
63            const std::string& apex_pubkey, bool is_builtin)
64       : apex_path_(apex_path),
65         image_offset_(image_offset),
66         image_size_(image_size),
67         manifest_(std::move(manifest)),
68         apex_pubkey_(apex_pubkey),
69         is_builtin_(is_builtin) {}
70 
71   std::string apex_path_;
72   int32_t image_offset_;
73   size_t image_size_;
74   ApexManifest manifest_;
75   std::string apex_pubkey_;
76   bool is_builtin_;
77 };
78 
79 android::base::Result<std::vector<std::string>> FindApexes(
80     const std::vector<std::string>& paths);
81 android::base::Result<std::vector<std::string>> FindApexFilesByName(
82     const std::string& path);
83 
84 bool isPathForBuiltinApexes(const std::string& path);
85 
86 }  // namespace apex
87 }  // namespace android
88 
89 #endif  // ANDROID_APEXD_APEX_FILE_H_
90