Python module to extract CA and CRL certs from Windows' cert store

0.2.1 · deprecated · verified Wed Apr 15

wincertstore is a Python module designed to extract CA and CRL certificates from the Windows certificate store using ctypes and the Windows system cert store API through `crypt32.dll`. However, the package is officially deprecated. Since Python 2.7.9, the standard `ssl.create_default_context()` function automatically handles loading certificates from the Windows certificate store, making `wincertstore` largely redundant for modern Python applications.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to iterate through certificates in the Windows system stores ('CA', 'ROOT') using `CertSystemStore` and how to prepare a temporary CA bundle file using `CertFile` for use with Python's `ssl` module. The `CertSystemStore.itercerts()` method by default filters for `SERVER_AUTH` usage.

import wincertstore
import atexit
import ssl
import os

if os.name == 'nt':
    print("--- Listing SERVER_AUTH certificates from CA and ROOT stores ---")
    for storename in ("CA", "ROOT"):
        try:
            with wincertstore.CertSystemStore(storename) as store:
                print(f"Store: {storename}")
                for cert in store.itercerts(usage=wincertstore.SERVER_AUTH):
                    print(f"  Name: {cert.get_name()}")
                    # print(f"  Enhanced Key Usage: {cert.enhanced_keyusage_names()}")
                    # print(cert.get_pem().decode("ascii")) # Uncomment to see PEM content
        except Exception as e:
            print(f"  Could not open store {storename}: {e}")

    print("\n--- Example using CertFile for SSL context (requires a socket) ---")
    # This part requires an actual socket connection to be fully runnable.
    # For demonstration, we'll just show the setup.
    certfile = wincertstore.CertFile()
    certfile.addstore("CA")
    certfile.addstore("ROOT")
    atexit.register(certfile.close) # Ensure cleanup of temporary file

    # In a real application, you would pass certfile.name to ssl.wrap_socket
    # or a requests session for CA certificate verification.
    # Example (conceptual, requires 'sock' object):
    # sock = some_socket_connection()
    # ssl_sock = ssl.wrap_socket(sock, ca_certs=certfile.name, cert_reqs=ssl.CERT_REQUIRED)
    print(f"Temporary CA file created at: {certfile.name}")
    print("Remember to call certfile.close() or use atexit.register for cleanup.")
else:
    print("wincertstore is only applicable to Windows operating systems.")

view raw JSON →