Jump to content

Recommended Posts

Posted (edited)
#include <WinHttp.au3> ; https://github.com/dragana-r/autoit-winhttp/tree/master

ConsoleWrite("- ExpiryTime: " & _WinHttp_SSL_ExpiryTime("www.google.com") & @CRLF)

Func _WinHttp_SSL_ExpiryTime($sSite, $iPort = 443, $sTimeType = "ExpiryTime")
    Local Const $tagINTERNET_CERTIFICATE_INFO = "dword ExpiryTime[2]; dword StartTime[2]; ptr SubjectInfo;" & _
    "ptr IssuerInfo; ptr ProtocolName; ptr SignatureAlgName; ptr EncryptionAlgName; dword KeySize"
    Local $tINTERNET_CERTIFICATE_INFO, $hOpen = _WinHttpOpen()
    _WinHttpSetOption($hOpen, $WINHTTP_OPTION_SECURITY_FLAGS, 0x00003300) ; $SECURITY_FLAG_IGNORE_ALL
    Local $hConnect = _WinHttpConnect($hOpen, $sSite, $iPort)
    Local $hRequest = _WinHttpSimpleSendSSLRequest($hConnect, "GET", "/")
    Local $tBufferLength = DllStructCreate("dword")
    DllStructSetData($tBufferLength, 1, 2048)
    Local $sReturn, $tBuffer = DllStructCreate("byte[2048]")
    Local $iError, $aResult = DllCall($hWINHTTPDLL__WINHTTP, "bool", "WinHttpQueryOption", _
            "handle", $hRequest, "dword", $WINHTTP_OPTION_SECURITY_CERTIFICATE_STRUCT, _
            "struct*", DllStructGetPtr($tBuffer), "dword*", DllStructGetPtr($tBufferLength))
    $iError = @error
    If Not $iError And $aResult[0] Then
        $tINTERNET_CERTIFICATE_INFO = DllStructCreate($tagINTERNET_CERTIFICATE_INFO, DllStructGetPtr($tBuffer))
        $sReturn = __WinHttp_INTERNET_CERTIFICATE_INFO_Time($tINTERNET_CERTIFICATE_INFO, $sTimeType) ; these are "ExpiryTime" and "StartTime"
    Else
        $iError = 99
        $sReturn = "error"
    EndIf
    $tBufferLength = 0
    $tBuffer = 0
    $tINTERNET_CERTIFICATE_INFO = 0
    _WinHttpCloseHandle($hRequest)
    _WinHttpCloseHandle($hConnect)
    _WinHttpCloseHandle($hOpen)
    Return SetError($iError, 0, $sReturn)
EndFunc   ;==>_WinHttp_SSL_ExpiryTime

Func __WinHttp_INTERNET_CERTIFICATE_INFO_Time($tStruct, $sTimeType = "ExpiryTime")
    Local $tSystTime = DllStructCreate("struct;word Year;word Month;word Dow;word Day;word Hour;word Minute;word Second;word MSeconds;endstruct")
    DllCall("kernel32.dll", "bool", "FileTimeToSystemTime", "struct*", DllStructGetPtr($tStruct, $sTimeType), "struct*", $tSystTime)
    Return StringFormat("%04d/%02d/%02d %02d:%02d:%02d", $tSystTime.Year, $tSystTime.Month, $tSystTime.Day, $tSystTime.Hour, $tSystTime.Minute, $tSystTime.Second)
EndFunc   ;==>__WinHttp_INTERNET_CERTIFICATE_INFO_Time

Had to scrape the site to get this. Shared here so you don't have to go trough the same trouble.

This function gets the date a SSL certificate expires on a web site.

Edit: Added to "ExpiryTime" the possibility of getting "StartTime". Was already there, might as well give the opportunity to get that too.


Spoiler
#include <WinHttp.au3> ; https://github.com/dragana-r/autoit-winhttp/tree/master

Example()
Func Example()
    Local $n, $aSSL_Info = _WinHttp_SSL_Info("www.google.com")
    For $n = 0 To UBound($aSSL_Info) - 1
        ConsoleWrite('- ==== ' & $aSSL_Info[$n][1] & ' === -' & @CRLF)
        ConsoleWrite('- >' & $aSSL_Info[$n][0] & '< -' & @CRLF & @CRLF)
    Next
    ConsoleWrite('- =============================== -' & @CRLF)
EndFunc   ;==>Example

Func _WinHttp_SSL_Info($sSite, $iPort = 443)
    Local $aReturn[5][2] = [["", "ExpiryTime"], ["", "StartTime"], ["", "KeySize"], ["", "SubjectInfo"], ["", "IssuerInfo"]]
    Local Const $tagINTERNET_CERTIFICATE_INFO = "dword ExpiryTime[2]; dword StartTime[2]; ptr SubjectInfo;" & _
            "ptr IssuerInfo; ptr ProtocolName; ptr SignatureAlgName; ptr EncryptionAlgName; dword KeySize"
    Local $tINTERNET_CERTIFICATE_INFO, $hOpen = _WinHttpOpen()
    _WinHttpSetOption($hOpen, $WINHTTP_OPTION_SECURITY_FLAGS, 0x00003300) ; $SECURITY_FLAG_IGNORE_ALL
    Local $hConnect = _WinHttpConnect($hOpen, $sSite, $iPort)
    Local $hRequest = _WinHttpSimpleSendSSLRequest($hConnect, "GET", "/")
    Local $tBufferLength = DllStructCreate("dword")
    DllStructSetData($tBufferLength, 1, 2048)
    Local $tBuffer = DllStructCreate("byte[2048]")
    Local $iError, $aResult = DllCall($hWINHTTPDLL__WINHTTP, "bool", "WinHttpQueryOption", _
            "handle", $hRequest, "dword", $WINHTTP_OPTION_SECURITY_CERTIFICATE_STRUCT, _
            "struct*", DllStructGetPtr($tBuffer), "dword*", DllStructGetPtr($tBufferLength))
    $iError = @error
    If Not $iError And $aResult[0] Then
        $tINTERNET_CERTIFICATE_INFO = DllStructCreate($tagINTERNET_CERTIFICATE_INFO, DllStructGetPtr($tBuffer))
        $aReturn[0][0] = __WinHttp_INTERNET_CERTIFICATE_INFO_Time($tINTERNET_CERTIFICATE_INFO)
        $aReturn[1][0] = __WinHttp_INTERNET_CERTIFICATE_INFO_Time($tINTERNET_CERTIFICATE_INFO, "StartTime")
        $aReturn[2][0] = DllStructGetData($tINTERNET_CERTIFICATE_INFO, "KeySize")
        $aReturn[3][0] = DllStructGetData(DllStructCreate("wchar [128]", DllStructGetData($tINTERNET_CERTIFICATE_INFO, "SubjectInfo")), 1)
        $aReturn[4][0] = DllStructGetData(DllStructCreate("wchar [128]", DllStructGetData($tINTERNET_CERTIFICATE_INFO, "IssuerInfo")), 1)
    Else
        $iError = 99
        $aReturn[0][0] = "error"
    EndIf
    $tBufferLength = 0
    $tBuffer = 0
    $tINTERNET_CERTIFICATE_INFO = 0
    _WinHttpCloseHandle($hRequest)
    _WinHttpCloseHandle($hConnect)
    _WinHttpCloseHandle($hOpen)
    Return SetError($iError, 0, $aReturn)
EndFunc   ;==>_WinHttp_SSL_Info

Func __WinHttp_INTERNET_CERTIFICATE_INFO_Time($tStruct, $sTimeType = "ExpiryTime")
    Local $tSystTime = DllStructCreate("struct;word Year;word Month;word Dow;word Day;word Hour;word Minute;word Second;word MSeconds;endstruct")
    DllCall("kernel32.dll", "bool", "FileTimeToSystemTime", "struct*", DllStructGetPtr($tStruct, $sTimeType), "struct*", $tSystTime)
    Return StringFormat("%04d/%02d/%02d %02d:%02d:%02d", $tSystTime.Year, $tSystTime.Month, $tSystTime.Day, $tSystTime.Hour, $tSystTime.Minute, $tSystTime.Second)
EndFunc   ;==>__WinHttp_INTERNET_CERTIFICATE_INFO_Time

 

..and this one has the rest of the info I could get.
If anyone can get ProtocolName, SignatureAlgName and EncryptionAlgName, post it. TIA

Edited by argumentum
expanded a bit

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...