forked from mindw/wincertstore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wincertstore.py
400 lines (328 loc) · 11 KB
/
wincertstore.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#!/usr/bin/env python
#
# Copyright (c) 2013 by Christian Heimes <christian@python.org>
# Licensed to PSF under a Contributor Agreement.
# See http://www.python.org/psf/license for licensing details.
#
"""ctypes interface to Window's certificate store
Requirements:
Windows XP, Windows Server 2003 or newer
Python 2.7+
Python 3.2+
"""
from __future__ import print_function
import os
# import shutil
import sys
import tempfile
from ctypes import WinDLL, FormatError, string_at, pointer
from ctypes import create_unicode_buffer, resize, get_last_error
from ctypes import Structure, POINTER, c_void_p
from ctypes.wintypes import LPCWSTR, LPSTR, DWORD, BOOL, BYTE, LPWSTR
from base64 import b64encode
PY3 = sys.version_info[0] == 3
HCERTSTORE = c_void_p
PCCERT_INFO = c_void_p
PCCRL_INFO = c_void_p
LPTCSTR = LPCWSTR
PKCS_7_ASN_ENCODING = 0x00010000
# enhanced key usage
CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG = 0x2
CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG = 0x4
#CRYPT_E_NOT_FOUND = 0x80092004
CRYPT_E_NOT_FOUND = -2146885628
# cert name
CERT_NAME_SIMPLE_DISPLAY_TYPE = 4
CERT_NAME_FRIENDLY_DISPLAY_TYPE = 5
CERT_NAME_ISSUER_FLAG = 0x1
# OID mapping for enhanced key usage
SERVER_AUTH = "1.3.6.1.5.5.7.3.1"
CLIENT_AUTH = "1.3.6.1.5.5.7.3.2"
CODE_SIGNING = "1.3.6.1.5.5.7.3.3"
EMAIL_PROTECTION = "1.3.6.1.5.5.7.3.4"
IPSEC_END_SYSTEM = "1.3.6.1.5.5.7.3.5"
IPSEC_TUNNEL = "1.3.6.1.5.5.7.3.6"
IPSEC_USER = "1.3.6.1.5.5.7.3.7"
TIME_STAMPING = "1.3.6.1.5.5.7.3.8"
OCSP_SIGNING = "1.3.6.1.5.5.7.3.9"
DVCS = "1.3.6.1.5.5.7.3.10"
TrustOIDs = {
SERVER_AUTH: "SERVER_AUTH",
CLIENT_AUTH: "CLIENT_AUTH",
CODE_SIGNING: "CODE_SIGNING",
EMAIL_PROTECTION: "EMAIL_PROTECTION",
IPSEC_END_SYSTEM: "IPSEC_END_SYSTEM",
IPSEC_TUNNEL: "IPSEC_TUNNEL",
IPSEC_USER: "IPSEC_USER",
TIME_STAMPING: "TIME_STAMPING",
OCSP_SIGNING: "OCSP_SIGNING",
DVCS: "DVCS",
}
def isPKCS7(value):
"""PKCS#7 check
"""
return (value & PKCS_7_ASN_ENCODING) == PKCS_7_ASN_ENCODING
class ContextStruct(Structure):
cert_type = None
__slots__ = ()
_fields_ = []
def get_encoded(self):
"""Get encoded cert as byte string
"""
pass
def encoding_type(self):
"""Get encoding type for PEM
"""
if isPKCS7(self.dwCertEncodingType):
return "PKCS7"
else:
return self.cert_type
encoding_type = property(encoding_type)
def get_pem(self):
"""Get PEM encoded cert
"""
encoding_type = self.encoding_type
b64data = b64encode(self.get_encoded()).decode("ascii")
lines = ["-----BEGIN %s-----" % encoding_type]
# split up in lines of 64 chars each
quotient, remainder = divmod(len(b64data), 64)
linecount = quotient + bool(remainder)
for i in range(linecount):
lines.append(b64data[i * 64:(i + 1) * 64])
lines.append("-----END %s-----" % encoding_type)
# trailing newline
lines.append("")
return "\n".join(lines)
class CERT_CONTEXT(ContextStruct):
"""Cert context
"""
cert_type = "CERTIFICATE"
__slots__ = ("_enhkey")
_fields_ = [
("dwCertEncodingType", DWORD),
("pbCertEncoded", POINTER(BYTE)),
("cbCertEncoded", DWORD),
("pCertInfo", PCCERT_INFO),
("hCertStore", HCERTSTORE),
]
def get_encoded(self):
return string_at(self.pbCertEncoded, self.cbCertEncoded)
def _enhkey_error(self):
err = get_last_error()
if err == CRYPT_E_NOT_FOUND:
return True
errmsg = FormatError(err)
raise OSError(err, errmsg)
def _get_enhkey(self, flag):
pCertCtx = pointer(self)
enhkey = CERT_ENHKEY_USAGE()
size = DWORD()
res = CertGetEnhancedKeyUsage(pCertCtx, flag, None, pointer(size))
if res == 0:
return self._enhkey_error()
resize(enhkey, size.value)
res = CertGetEnhancedKeyUsage(pCertCtx, flag, pointer(enhkey),
pointer(size))
if res == 0:
return self._enhkey_error()
oids = set()
for i in range(enhkey.cUsageIdentifier):
oid = enhkey.rgpszUsageIdentifier[i]
if oid:
if PY3:
oid = oid.decode("ascii")
oids.add(oid)
return oids
def enhanced_keyusage(self):
enhkey = getattr(self, "_enhkey", None)
if enhkey is not None:
return enhkey
keyusage = self._get_enhkey(CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG)
if keyusage is True:
keyusage = self._get_enhkey(CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG)
if keyusage is True:
self._enhkey = True
else:
self._enhkey = frozenset(keyusage)
return keyusage
def enhanced_keyusage_names(self):
enhkey = self.enhanced_keyusage()
if enhkey is True:
return True
result = set()
for oid in self.enhanced_keyusage():
result.add(TrustOIDs.get(oid, oid))
return result
def get_name(self, typ=CERT_NAME_SIMPLE_DISPLAY_TYPE, flag=0):
pCertCtx = pointer(self)
cbsize = CertGetNameStringW(pCertCtx, typ, flag, None, None, 0)
buf = create_unicode_buffer(cbsize)
cbsize = CertGetNameStringW(pCertCtx, typ, flag, None, buf, cbsize)
return buf.value
class CRL_CONTEXT(ContextStruct):
"""Cert revocation list context
"""
cert_type = "X509 CRL"
__slots__ = ()
_fields_ = [
("dwCertEncodingType", DWORD),
("pbCrlEncoded", POINTER(BYTE)),
("cbCrlEncoded", DWORD),
("pCrlInfo", PCCRL_INFO),
("hCertStore", HCERTSTORE),
]
def get_encoded(self):
return string_at(self.pbCrlEncoded, self.cbCrlEncoded)
class CERT_ENHKEY_USAGE(Structure):
"""Enhanced Key Usage
"""
__slots__ = ()
_fields_ = [
("cUsageIdentifier", DWORD),
("rgpszUsageIdentifier", POINTER(LPSTR))
]
crypt32 = WinDLL("crypt32.dll", use_last_error=True)
CertOpenSystemStore = crypt32.CertOpenSystemStoreW
CertOpenSystemStore.argtypes = [c_void_p, LPTCSTR]
CertOpenSystemStore.restype = HCERTSTORE
CertCloseStore = crypt32.CertCloseStore
CertCloseStore.argtypes = [HCERTSTORE, DWORD]
CertCloseStore.restype = BOOL
PCCERT_CONTEXT = POINTER(CERT_CONTEXT)
CertEnumCertificatesInStore = crypt32.CertEnumCertificatesInStore
CertEnumCertificatesInStore.argtypes = [HCERTSTORE, PCCERT_CONTEXT]
CertEnumCertificatesInStore.restype = PCCERT_CONTEXT
PCCRL_CONTEXT = POINTER(CRL_CONTEXT)
CertEnumCRLsInStore = crypt32.CertEnumCRLsInStore
CertEnumCRLsInStore.argtypes = [HCERTSTORE, PCCRL_CONTEXT]
CertEnumCRLsInStore.restype = PCCRL_CONTEXT
PCERT_ENHKEY_USAGE = POINTER(CERT_ENHKEY_USAGE)
CertGetEnhancedKeyUsage = crypt32.CertGetEnhancedKeyUsage
CertGetEnhancedKeyUsage.argtypes = [PCCERT_CONTEXT, DWORD, PCERT_ENHKEY_USAGE,
POINTER(DWORD)]
CertGetEnhancedKeyUsage.restype = BOOL
CertGetNameStringW = crypt32.CertGetNameStringW
CertGetNameStringW.argtypes = [PCCERT_CONTEXT, DWORD, DWORD, c_void_p,
LPWSTR, DWORD]
CertGetNameStringW.restype = DWORD
class CertSystemStore(object):
"""Wrapper for Window's cert system store
http://msdn.microsoft.com/en-us/library/windows/desktop/aa376560%28v=vs.85%29.aspx
store names
-----------
CA:
Certification authority certificates
MY:
Certs with private keys
ROOT:
Root certificates
SPC:
Software Publisher Certificate
"""
__slots__ = ("_storename", "_hStore")
def __init__(self, storename):
self._storename = storename
self._hStore = CertOpenSystemStore(None, self.storename)
if not self._hStore: # NULL ptr
self._hStore = None
errmsg = FormatError(get_last_error())
raise OSError(errmsg)
@property
def storename(self):
"""Get store name
"""
return self._storename
def close(self):
CertCloseStore(self._hStore, 0)
self._hStore = None
def __enter__(self):
return self
def __exit__(self, exc, value, tb):
self.close()
def itercerts(self, usage=SERVER_AUTH):
"""Iterate over certificates
"""
pCertCtx = CertEnumCertificatesInStore(self._hStore, None)
while pCertCtx:
certCtx = pCertCtx[0]
enhkey = certCtx.enhanced_keyusage()
if usage:
if enhkey is True or usage in enhkey:
yield certCtx
else:
yield certCtx
pCertCtx = CertEnumCertificatesInStore(self._hStore, pCertCtx)
def itercrls(self):
"""Iterate over cert revocation lists
"""
pCrlCtx = CertEnumCRLsInStore(self._hStore, None)
while pCrlCtx:
crlCtx = pCrlCtx[0]
yield crlCtx
pCrlCtx = CertEnumCRLsInStore(self._hStore, pCrlCtx)
def __iter__(self):
for cert in self.itercerts():
yield cert
for crl in self.itercrls():
yield crl
class CertFile(object):
"""Wrapper to handle a temporary file for a CA.pem
Usage:
import wincertstore
import atexit
certfile = wincertstore.CertFile()
certfile.addstore("CA")
certfile.addstore("ROOT")
atexit.register(certfile.close) # cleanup and remove files on shutdown)
ca_cert = certfile.name
"""
def __init__(self, suffix="ca.pem"):
handle, self._capem = tempfile.mkstemp(suffix=suffix)
os.close(handle)
@property
def name(self):
"""Path to CA.pem
"""
return self._capem
def close(self):
# if someone modified tempfile.tempfir before creating a CertFile
# and also handled its cleanup, then our temp file does not exist.
if os.path.exists(self._capem):
os.remove(self._capem)
self._capem = None
def __enter__(self):
return self
def __exit__(self, exc, value, tb):
self.close()
def addcerts(self, certs):
"""Add certs to store
"""
with open(self._capem, "a") as f:
#f.seek(0, os.SEEK_END)
for cert in certs:
f.write(cert.get_pem())
def addstore(self, store):
"""Add store to CertFile
:param store: either a name of a store or a CertSystemStore instance
"""
if hasattr(store, "itercerts"):
self.addcerts(store.itercerts())
else:
store = CertSystemStore(store)
try:
self.addcerts(store.itercerts())
finally:
store.close()
def read(self):
"""Read CA.pem file and return content
"""
with open(self._capem, "r") as f:
return f.read()
if __name__ == "__main__":
for storename in ("CA", "ROOT"):
store = CertSystemStore(storename)
try:
for cert in store.itercerts():
print(cert.get_pem().strip())
finally:
store.close()