winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

AppInstallerMsixInfo.h (4986B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 #pragma once
      4 #include "AppInstallerProgress.h"
      5 #include "winget/ManagedFile.h"
      6 #include "winget/Manifest.h"
      7 #include "winget/MsixManifest.h"
      8 #include <AppInstallerVersions.h>
      9 
     10 #include <AppxPackaging.h>
     11 
     12 #include <wrl/client.h>
     13 #include <winrt/Windows.ApplicationModel.h>
     14 
     15 #include <filesystem>
     16 #include <optional>
     17 #include <string>
     18 #include <string_view>
     19 #include <vector>
     20 
     21 namespace AppInstaller::Msix
     22 {
     23     // Function to create an AppxBundle package reader given the input file name.
     24     // Returns true if success, false if the input stream is of wrong type.
     25     bool GetBundleReader(
     26         IStream* inputStream,
     27         IAppxBundleReader** reader);
     28 
     29     // Function to create an Appx package reader given the input file name.
     30     // Returns true if success, false if the input stream is of wrong type.
     31     bool GetPackageReader(
     32         IStream* inputStream,
     33         IAppxPackageReader** reader);
     34 
     35     // Function to create an Appx manifest reader given the input file name.
     36     void GetManifestReader(
     37         IStream* inputStream,
     38         IAppxManifestReader** reader);
     39 
     40     // Gets the package full name from the family name.
     41     // This will be the one registered for the current user, if any.
     42     std::optional<std::string> GetPackageFullNameFromFamilyName(std::string_view familyName);
     43 
     44     // Gets the package family name from the given full name.
     45     std::string GetPackageFamilyNameFromFullName(std::string_view fullName);
     46 
     47     // Gets the package location from the given full name.
     48     std::optional<std::filesystem::path> GetPackageLocationFromFullName(std::string_view fullName);
     49 
     50     struct PackageIdInfo
     51     {
     52         std::string Name;
     53         AppInstaller::Utility::UInt64Version Version;
     54     };
     55 
     56     // Gets the package id info from the given full name.
     57     PackageIdInfo GetPackageIdInfoFromFullName(std::string_view fullName);
     58 
     59     // MsixInfo class handles all appx/msix related query.
     60     struct MsixInfo
     61     {
     62         MsixInfo(std::string_view uriStr);
     63 
     64         template<typename T, std::enable_if_t<std::is_same_v<T, std::filesystem::path>, int> = 0>
     65         MsixInfo(const T& path) : MsixInfo(path.u8string()) {}
     66 
     67         MsixInfo(const MsixInfo&) = default;
     68         MsixInfo& operator=(const MsixInfo&) = default;
     69 
     70         MsixInfo(MsixInfo&&) = default;
     71         MsixInfo& operator=(MsixInfo&&) = default;
     72 
     73         inline bool GetIsBundle()
     74         {
     75             return m_isBundle;
     76         }
     77 
     78         // Full content of AppxSignature.p7x
     79         // If skipP7xFileId is true, returns content of converted .p7s
     80         std::vector<byte> GetSignature(bool skipP7xFileId = false);
     81 
     82         // Gets the signature sha256 hash.
     83         Utility::SHA256::HashBuffer GetSignatureHash();
     84 
     85         // Gets the digest of the package.
     86         std::wstring GetDigest();
     87 
     88         // Gets the package full name.
     89         std::wstring GetPackageFullNameWide();
     90         std::string GetPackageFullName();
     91 
     92         // Gets a value indicating whether the referenced info is newer than the given package.
     93         bool IsNewerThan(const std::filesystem::path& otherPackage);
     94 
     95         bool IsNewerThan(const winrt::Windows::ApplicationModel::PackageVersion& otherVersion);
     96 
     97         // Writes the package file to the given path.
     98         void WriteToFile(std::string_view packageFile, const std::filesystem::path& target, IProgressCallback& progress);
     99 
    100         // Writes the package's manifest to the given path.
    101         void WriteManifestToFile(const std::filesystem::path& target, IProgressCallback& progress);
    102 
    103         // Writes the package file to the given file handle.
    104         void WriteToFileHandle(std::string_view packageFile, HANDLE target, IProgressCallback& progress);
    105 
    106         // Get application package manifests from msix and msixbundle.
    107         std::vector<MsixPackageManifest> GetAppPackageManifests(bool includeStub = false) const;
    108 
    109     private:
    110         bool m_isBundle;
    111         Microsoft::WRL::ComPtr<IStream> m_stream;
    112         Microsoft::WRL::ComPtr<IAppxBundleReader> m_bundleReader;
    113         Microsoft::WRL::ComPtr<IAppxPackageReader> m_packageReader;
    114 
    115         // Get application packages. Ignore stub packages if any.
    116         std::vector<Microsoft::WRL::ComPtr<IAppxPackageReader>> GetAppPackages(bool includeStub = false) const;
    117     };
    118 
    119     struct GetCertContextResult
    120     {
    121         wil::unique_cert_context CertContext;
    122         wil::unique_hcertstore CertStore;
    123     };
    124 
    125     // Get cert context from a signed msix/msixbundle file.
    126     GetCertContextResult GetCertContextFromMsix(const std::filesystem::path& msixPath);
    127 
    128     struct WriteLockedMsixFile
    129     {
    130         WriteLockedMsixFile(const std::filesystem::path& path);
    131 
    132         bool ValidateTrustInfo(bool checkMicrosoftOrigin) const;
    133 
    134     private:
    135         Utility::ManagedFile m_file;
    136     };
    137 }