# Path: debug.py
# def debug(level,text,DEBUG,bindir):
#     try:
#         if bindir == False or level <= 0:
#             print("DEBUG: %s" % (text))
#             return False
#         bindir = os.getcwd()
#         if bindir.endswith("system32"):
#             if os.path.exists(devdir()):
#                 bindir=devdir()
#             else:
#                 print("DEBUG: %s" % (text))
#                 return False
#         
#         logfile = "%s\\client_debug.log" % (bindir)
#         global DEBUGcount
#         global DEBUGfrombefore
#         
#         if level > LOGLEVEL:
#             if not level in LOGLEVELS:
#                 return False
#         
#         timefromboot = round(time.time() - BOOTTIME,3)
#         tempdebuglist = list()
#         if DEBUGcount > 0 and not DEBUGfrombefore == text:
#             debugstringsht1 = "(%s):(d1) %s (repeat: %s)" % (timefromboot, DEBUGfrombefore,DEBUGcount)
#             debugstringsht2 = "(%s):(d2) %s" % (timefromboot,text)
#             tempdebuglist.append(debugstringsht1)
#             tempdebuglist.append(debugstringsht2)
#             DEBUGcount = 0
#         elif DEBUGcount >= 4096 and DEBUGfrombefore == text:
#             debugstringsht = "(%s):(d3) %s (repeated: %s e2)" % (timefromboot, DEBUGfrombefore,DEBUGcount)
#             tempdebuglist.append(debugstringsht)
#             DEBUGcount = 0
#         elif DEBUGfrombefore == text:
#             DEBUGcount += 1
#             return
#         elif not DEBUGfrombefore == text:
#             debugstringsht = "(%s):(d4) %s"%(timefromboot,text)
#             tempdebuglist.append(debugstringsht)
#         DEBUGfrombefore = text
#         if len(tempdebuglist) > 0:
#             for entry in tempdebuglist:
#                 print(entry)
#                 debug_cache(entry,'add')
#                 if DEBUG == True:
#                     write_debug(level,entry,timefromboot,logfile)
#     except Exception as e:
#         print("[debug.py] debug failed, exception = '%s'" % (e))
# 
# Path: debug.py
# def devmode():
#     return getmode("DEVMODE")
# 
# Path: debug.py
# def devdir():
#     dir = "E:\\ovpn-client"
#     return dir


import os, subprocess
import hashings
import openvpn
from debug import debug
from debug import devmode
from debug import devdir
            debug(1,"[signtool.py] def signtool_verify: file = '%s' signature failed"%(file),DEBUG,True)
            """
            # only check file is signed from same CA if DEVMODE is True
            if devmode() == False:
                return False
            else:
                return False
            """
            """
            debug(1,"[signtool.py] def signtool_verify: file = '%s' check signature CA hash"%(file),DEBUG,True)
            cacertsha1 = "21F94C255A8B20D21A323CA5ACB8EBF284E09037"
            string2 = '"%s" verify /v /a /all /pa /tw /ca %s "%s"' % (signtool,cacertsha1,file)
            """
            debug(1,"[signtool.py] def signtool_verify: file = '%s' check signature old hash"%(file),DEBUG,True)
            oldcscertsha1 = "21F94C255A8B20D21A323CA5ACB8EBF284E09037"
            string2 = '"%s" verify %s /a /all /pa /tw /sha1 %s "%s"' % (signtool,verbose,oldcscertsha1,file)
            
            debug(1,"[signtool.py] def signtool_verify: string2 = '%s'"%(string2),DEBUG,True)
            exitcode2 = 1
            try:
                exitcode2 = subprocess.check_call("%s" % (string2),shell=True)
            except:
                pass
            if exitcode2 == 0:
                debug(1,"[signtool.py] def signtool_verify: file = '%s' signature old hash verified"%(file),DEBUG,True)
                return True
            else:
                debug(1,"[signtool.py] def signtool_verify: file = '%s' signature old hash failed"%(file),DEBUG,True)
                return False
    else:

# Path: debug.py
# def debug(level,text,DEBUG,bindir):
#     try:
#         if bindir == False or level <= 0:
#             print("DEBUG: %s" % (text))
#             return False
#         bindir = os.getcwd()
#         if bindir.endswith("system32"):
#             if os.path.exists(devdir()):
#                 bindir=devdir()
#             else:
#                 print("DEBUG: %s" % (text))
#                 return False
#         
#         logfile = "%s\\client_debug.log" % (bindir)
#         global DEBUGcount
#         global DEBUGfrombefore
#         
#         if level > LOGLEVEL:
#             if not level in LOGLEVELS:
#                 return False
#         
#         timefromboot = round(time.time() - BOOTTIME,3)
#         tempdebuglist = list()
#         if DEBUGcount > 0 and not DEBUGfrombefore == text:
#             debugstringsht1 = "(%s):(d1) %s (repeat: %s)" % (timefromboot, DEBUGfrombefore,DEBUGcount)
#             debugstringsht2 = "(%s):(d2) %s" % (timefromboot,text)
#             tempdebuglist.append(debugstringsht1)
#             tempdebuglist.append(debugstringsht2)
#             DEBUGcount = 0
#         elif DEBUGcount >= 4096 and DEBUGfrombefore == text:
#             debugstringsht = "(%s):(d3) %s (repeated: %s e2)" % (timefromboot, DEBUGfrombefore,DEBUGcount)
#             tempdebuglist.append(debugstringsht)
#             DEBUGcount = 0
#         elif DEBUGfrombefore == text:
#             DEBUGcount += 1
#             return
#         elif not DEBUGfrombefore == text:
#             debugstringsht = "(%s):(d4) %s"%(timefromboot,text)
#             tempdebuglist.append(debugstringsht)
#         DEBUGfrombefore = text
#         if len(tempdebuglist) > 0:
#             for entry in tempdebuglist:
#                 print(entry)
#                 debug_cache(entry,'add')
#                 if DEBUG == True:
#                     write_debug(level,entry,timefromboot,logfile)
#     except Exception as e:
#         print("[debug.py] debug failed, exception = '%s'" % (e))
# 
# Path: debug.py
# def devmode():
#     return getmode("DEVMODE")
# 
# Path: debug.py
# def devdir():
#     dir = "E:\\ovpn-client"
#     return dir


# Path: signtool.py
import os, subprocess
import hashings
import openvpn
from debug import debug
from debug import devmode
from debug import devdir

            debug(1,"[signtool.py] def signtool_verify: file = '%s' signature failed"%(file),DEBUG,True)
            """
            # only check file is signed from same CA if DEVMODE is True
            if devmode() == False:
                return False
            else:
                return False
            """
            """
            debug(1,"[signtool.py] def signtool_verify: file = '%s' check signature CA hash"%(file),DEBUG,True)
            cacertsha1 = "21F94C255A8B20D21A323CA5ACB8EBF284E09037"
            string2 = '"%s" verify /v /a /all /pa /tw /ca %s "%s"' % (signtool,cacertsha1,file)
            """
            debug(1,"[signtool.py] def signtool_verify: file = '%s' check signature old hash"%(file),DEBUG,True)
            oldcscertsha1 = "21F94C255A8B20D21A323CA5ACB8EBF284E09037"
            string2 = '"%s" verify %s /a /all /pa /tw /sha1 %s "%s"' % (signtool,verbose,oldcscertsha1,file)
            
            debug(1,"[signtool.py] def signtool_verify: string2 = '%s'"%(string2),DEBUG,True)
            exitcode2 = 1
            try:
                exitcode2 = subprocess.check_call("%s" % (string2),shell=True)
            except:
                pass
            if exitcode2 == 0:
                debug(1,"[signtool.py] def signtool_verify: file = '%s' signature old hash verified"%(file),DEBUG,True)
                return True
            else:
                debug(1,"[signtool.py] def signtool_verify: file = '%s' signature old hash failed"%(file),DEBUG,True)
                return False
    else:

        debug(1,"[signtool.py] def signtool_verify(%s) not found"%(file),DEBUG,True)