VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "CFileSig"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Enum serialFormats
    sf_spaced
    sf_colon
    sf_reg
End Enum

Public path As String
Public Issuer As String
Public Subject As String
Public Error As String

Private m_serial As String

Private Declare Function signerInfo Lib "siginfo.dll" (ByVal wpath As Long) As Variant

Property Get Serial(Optional f As serialFormats = sf_reg) As String
    If f = sf_colon Then
        Serial = Replace(m_serial, " ", ":")
    ElseIf f = sf_spaced Then
        Serial = m_serial
    Else
        Serial = Replace(m_serial, " ", "")
    End If
End Property

Function LoadFileSig(ByVal fpath As String) As Boolean

    Dim v As Variant, t() As String, tt
    
    'in case they reuse the object..
    path = fpath
    Error = Empty
    Issuer = Empty
    Subject = Empty
    m_serial = Empty
    
    If Not fso.FileExists(fpath) Then
        Error = "File not found"
        Exit Function
    End If
    
    If hSigInfo = 0 Then
        hSigInfo = LoadLibrary(App.path & IIf(isIde(), "\..\", "") & "\siginfo.dll")
        If hSigInfo = 0 Then
            Error = "File not found"
            Exit Function
        End If
    End If
    
    
    v = signerInfo(StrPtr(path))
    
    'Serial Number: 62 f7 4e 68 b8 19 7a cb 17 aa 18 42 39 28 b3 7f
    'Issuer Name:Sectigo Public Code Signing CA R36
    'Subject Name: Bitvise Limited
    '
    'Err: CryptQueryObject failed: Cannot find the requested object.

    t = Split(v, vbCrLf)
    For Each tt In t
        If Left(tt, 4) = "Err:" Then
            Error = Error & trim(Mid(tt, 5)) & vbCrLf
        ElseIf Left(tt, 14) = "Serial Number:" Then
            m_serial = trim(Mid(tt, 15))
        ElseIf Left(tt, 12) = "Issuer Name:" Then
            Issuer = trim(Mid(tt, 13))
        ElseIf Left(tt, 13) = "Subject Name:" Then
            Subject = trim(Mid(tt, 14))
        End If
    Next
    
    LoadFileSig = (Len(Error) = 0)
    
End Function
