#!/usr/bin/env python3
"""
Parse MSI file for info

Related:
- https://github.com/decalage2/oletools/blob/master/oletools/olemeta.py
- https://github.com/ralphje/signify/issues/24#issuecomment-1148320566
"""
import olefile
import os

import signify.authenticode.structures
import signify.pkcs7.signeddata
import sys

# from signify.authenticode.signed_pe import SignedPEFile


def main(pathname):
    # Check file exists:
    if not os.path.exists(pathname):
        raise FileNotFoundError(pathname)

    ole = olefile.OleFileIO(pathname)

    meta = ole.get_metadata()

    for prop in meta.SUMMARY_ATTRIBS:
        value = getattr(meta, prop)
        print((prop, value))

    # get raw info:
    # other_props = ole.getproperties("\x05SummaryInformation")
    # for item in other_props.values():
    #     print(item)

    if not ole.exists("\x05DigitalSignature"):
        print("WARNING: File not signed!")
    else:
        with ole.openstream("\x05DigitalSignature") as fh:
            b_data = fh.read()
            # print(f"DigitalSignature: {len(b_data)} bytes")
            # signed_data = (
            #     signify.authenticode.structures.AuthenticodeSignedData.from_envelope(
            #         b_data
            #     )
            # )
            signed_data_pkcs7 = signify.pkcs7.signeddata.SignedData(b_data)
            print(signed_data_pkcs7)
            # print(signed_data.explain_verify())

    # end:
    ole.close()


# if called directly, then run this example:
if __name__ == "__main__":
    try:
        sys.exit(main(sys.argv[1]))
    except IndexError:
        # using https://github.com/PowerShell/PowerShell/releases/download/v7.2.5/PowerShell-7.2.5-win-x64.msi
        EXAMPLE_PATH = r"Python\test.msi"
        sys.exit(main(EXAMPLE_PATH))

@@ -201,16 +201,17 @@
 566%0A%22%22%22%0A
+%0A
 import o
@@ -231,149 +231,18 @@
  os%0A
-%0Aimport signify.authenticode.structures%0Aimport signify.pkcs7.signeddata%0Aimport sys%0A%0A# from signify.authenticode.signed_pe import SignedPEFile
+import sys
 %0A%0A%0Ad
@@ -537,16 +537,417 @@
  value))
+%0A    # print(meta.dump())%0A%0A    create_time = getattr(meta, %22create_time%22)%0A    last_saved_time = getattr(meta, %22last_saved_time%22)%0A    dates_to_compare = %5B%5D%0A    dates_to_compare.append(create_time)%0A    dates_to_compare.append(last_saved_time)%0A    max_time = max(dates_to_compare)%0A    # print(max_time)%0A    max_time_yyyymmdd = max_time.strftime(%22%25Y-%25m-%25d%22)%0A    print(f%22Max Mod Time: %7Bmax_time_yyyymmdd%7D%22)
 %0A%0A    # 
@@ -1254,25 +1254,27 @@
             
-b
+sig
 _data = fh.r
@@ -1286,26 +1286,24 @@
 %0A           
- #
  print(f%22Dig
@@ -1302,24 +1302,25 @@
 nt(f%22Digital
+ 
 Signature: %7B
@@ -1323,17 +1323,19 @@
 e: %7Blen(
-b
+sig
 _data)%7D 
@@ -1345,354 +1345,8 @@
 es%22)
-%0A            # signed_data = (%0A            #     signify.authenticode.structures.AuthenticodeSignedData.from_envelope(%0A            #         b_data%0A            #     )%0A            # )%0A            signed_data_pkcs7 = signify.pkcs7.signeddata.SignedData(b_data)%0A            print(signed_data_pkcs7)%0A            # print(signed_data.explain_verify())
 %0A%0A  

parse dates