| 1 |
from _pypy_openssl import lib, ffi |
| 2 |
|
| 3 |
|
| 4 |
def enum_certificates(store_name): |
| 5 |
"""Retrieve certificates from Windows' cert store. |
| 6 |
|
| 7 |
store_name may be one of 'CA', 'ROOT' or 'MY'. The system may provide |
| 8 |
more cert storages, too. The function returns a list of (bytes, |
| 9 |
encoding_type, trust) tuples. The encoding_type flag can be interpreted |
| 10 |
with X509_ASN_ENCODING or PKCS_7_ASN_ENCODING. The trust setting is either |
| 11 |
a set of OIDs or the boolean True. |
| 12 |
""" |
| 13 |
hStore = lib.CertOpenStore(lib.CERT_STORE_PROV_SYSTEM_A, 0, ffi.NULL, |
| 14 |
lib.CERT_STORE_READONLY_FLAG | lib.CERT_SYSTEM_STORE_LOCAL_MACHINE, |
| 15 |
bytes(store_name, "ascii")) |
| 16 |
if hStore == ffi.NULL: |
| 17 |
raise WindowsError(*ffi.getwinerror()) |
| 18 |
|
| 19 |
result = [] |
| 20 |
pCertCtx = ffi.NULL |
| 21 |
try: |
| 22 |
while True: |
| 23 |
pCertCtx = lib.CertEnumCertificatesInStore(hStore, pCertCtx) |
| 24 |
if pCertCtx == ffi.NULL: |
| 25 |
break |
| 26 |
cert = ffi.buffer(pCertCtx.pbCertEncoded, pCertCtx.cbCertEncoded)[:] |
| 27 |
enc = certEncodingType(pCertCtx.dwCertEncodingType) |
| 28 |
keyusage = parseKeyUsage(pCertCtx, lib.CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG) |
| 29 |
if keyusage is True: |
| 30 |
keyusage = parseKeyUsage(pCertCtx, lib.CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG) |
| 31 |
result.append((cert, enc, keyusage)) |
| 32 |
finally: |
| 33 |
if pCertCtx != ffi.NULL: |
| 34 |
lib.CertFreeCertificateContext(pCertCtx) |
| 35 |
if not lib.CertCloseStore(hStore, 0): |
| 36 |
|
| 37 |
raise WindowsError(*ffi.getwinerror()) |
| 38 |
return result |
| 39 |
|
| 40 |
|
| 41 |
def enum_crls(store_name): |
| 42 |
"""Retrieve CRLs from Windows' cert store. |
| 43 |
|
| 44 |
store_name may be one of 'CA', 'ROOT' or 'MY'. The system may provide |
| 45 |
more cert storages, too. The function returns a list of (bytes, |
| 46 |
encoding_type) tuples. The encoding_type flag can be interpreted with |
| 47 |
X509_ASN_ENCODING or PKCS_7_ASN_ENCODING.""" |
| 48 |
hStore = lib.CertOpenStore(lib.CERT_STORE_PROV_SYSTEM_A, 0, ffi.NULL, |
| 49 |
lib.CERT_STORE_READONLY_FLAG | lib.CERT_SYSTEM_STORE_LOCAL_MACHINE, |
| 50 |
bytes(store_name, "ascii")) |
| 51 |
if hStore == ffi.NULL: |
| 52 |
raise WindowsError(*ffi.getwinerror()) |
| 53 |
|
| 54 |
result = [] |
| 55 |
pCrlCtx = ffi.NULL |
| 56 |
try: |
| 57 |
while True: |
| 58 |
pCrlCtx = lib.CertEnumCRLsInStore(hStore, pCrlCtx) |
| 59 |
if pCrlCtx == ffi.NULL: |
| 60 |
break |
| 61 |
crl = ffi.buffer(pCrlCtx.pbCrlEncoded, pCrlCtx.cbCrlEncoded)[:] |
| 62 |
enc = certEncodingType(pCrlCtx.dwCertEncodingType) |
| 63 |
result.append((crl, enc)) |
| 64 |
finally: |
| 65 |
if pCrlCtx != ffi.NULL: |
| 66 |
lib.CertFreeCRLContext(pCrlCtx) |
| 67 |
if not lib.CertCloseStore(hStore, 0): |
| 68 |
|
| 69 |
raise WindowsError(*ffi.getwinerror()) |
| 70 |
return result |
| 71 |
|
| 72 |
|
| 73 |
def certEncodingType(encodingType): |
| 74 |
if encodingType == lib.X509_ASN_ENCODING: |
| 75 |
return "x509_asn" |
| 76 |
if encodingType == lib.PKCS_7_ASN_ENCODING: |
| 77 |
return "pkcs_7_asn" |
| 78 |
return encodingType |
| 79 |
|
| 80 |
def parseKeyUsage(pCertCtx, flags): |
| 81 |
pSize = ffi.new("DWORD *") |
| 82 |
if not lib.CertGetEnhancedKeyUsage(pCertCtx, flags, ffi.NULL, pSize): |
| 83 |
error_with_message = ffi.getwinerror() |
| 84 |
if error_with_message[0] == lib.CRYPT_E_NOT_FOUND: |
| 85 |
return True |
| 86 |
raise WindowsError(*error_with_message) |
| 87 |
|
| 88 |
pUsageMem = ffi.new("char[]", pSize[0]) |
| 89 |
pUsage = ffi.cast("PCERT_ENHKEY_USAGE", pUsageMem) |
| 90 |
if not lib.CertGetEnhancedKeyUsage(pCertCtx, flags, pUsage, pSize): |
| 91 |
error_with_message = ffi.getwinerror() |
| 92 |
if error_with_message[0] == lib.CRYPT_E_NOT_FOUND: |
| 93 |
return True |
| 94 |
raise WindowsError(*error_with_message) |
| 95 |
|
| 96 |
retval = set() |
| 97 |
for i in range(pUsage.cUsageIdentifier): |
| 98 |
if pUsage.rgpszUsageIdentifier[i]: |
| 99 |
oid = ffi.string(pUsage.rgpszUsageIdentifier[i]).decode('ascii') |
| 100 |
retval.add(oid) |
| 101 |
return retval |