[Python] Access windows local machine store with Python win32crypt

Discussão em 'Python' iniciado por Stack, Fevereiro 28, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to access a certificate, that's stored in windows local machine store. This can't be done with wincertstore, as it uses CertOpenSystemStoreA function (see remarks: https://learn.microsoft.com/en-us/windows/win32/api/wincrypt/nf-wincrypt-certopensystemstorea)

    However CertOpenStore from pywin32 has access, I just don't know how to pass the right parameters. Here's my code:

    import win32crypt

    # store provider
    CERT_STORE_PROV_SYSTEM = 13
    #dwFlags
    CERT_SYSTEM_STORE_CURRENT_SERVICE = 0x0100
    CERT_SYSTEM_STORE_CURRENT_USER = 0x0200
    CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY = 0x0400
    CERT_SYSTEM_STORE_LOCAL_MACHINE = 0x0800
    CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE = 0x1000
    CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY = 0x2000
    CERT_SYSTEM_STORE_SERVICES = 0x4000
    CERT_SYSTEM_STORE_USERS = 0x8000

    store = win32crypt.CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, None, CERT_SYSTEM_STORE_LOCAL_MACHINE, "MY")
    for cert in store.CertEnumCertificatesInStore():
    print("1 Cert: " + str(cert))
    print("2 CertEnumCertificateContextProperties: " + str(cert.CertEnumCertificateContextProperties()))
    print("3 cert.Subject: " + str(win32crypt.CertNameToStr(cert.Subject)))


    When running, I get an exception: (-2147024809, 'CertOpenStore', 'Wrong Parameter.')

    Documentation of CertOpenStore in pywin32: http://timgolden.me.uk/pywin32-docs/win32crypt__CertOpenStore_meth.html Documentaion of CertOpenStore in Windows Dev Center: https://learn.microsoft.com/en-us/windows/win32/api/wincrypt/nf-wincrypt-certopenstore

    Continue reading...

Compartilhe esta Página