forked from simonvorobjev/CryptoApi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto_support.py
345 lines (310 loc) · 11.2 KB
/
crypto_support.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
__author__ = 'simon'
from ctypes import *
import os
# Загрузка библиотеки
if os.name == 'nt':
crypt_dll = windll.LoadLibrary("C:\\Windows\\System32\\crypt32.dll")
capi10_dll = windll.LoadLibrary("C:\\Windows\\System32\\advapi32.dll")
else:
crypt_dll = cdll.LoadLibrary("/opt/cprocsp/lib/amd64/libcapi20.so.4.0.5")
capi10_dll = cdll.LoadLibrary("/opt/cprocsp/lib/amd64/libcapi10.so.4.0.5")
# Константы для работы с криптоапи
CRYPT_VERIFYCONTEXT = 0xF0000000
PROV_GOST_2012_256 = 80
PP_NAME = 4
# Определение всех необходимых для получения расширения структур (смотри msdn на szOID_SUBJECT_ALT_NAME2)
class CRYPT_DATA_BLOB(Structure):
_fields_ = [("cbData", c_ulong),
("pbData", POINTER(c_ubyte))]
class CERT_OTHER_NAME(Structure):
_fields_ = [("pszObjId", c_char_p),
("Value", CRYPT_DATA_BLOB)]
class CERT_ALT_NAME_ENTRY_UNION(Union):
_fields_ = [("pOtherName", POINTER(CERT_OTHER_NAME)),
("pwszRfc822Name", c_wchar_p),
("pwszDNSName", c_wchar_p),
("DirectoryName", CRYPT_DATA_BLOB),
("pwszURL", c_wchar_p),
("IPAddress", CRYPT_DATA_BLOB),
("pszRegisteredID", c_char_p)]
class CERT_ALT_NAME_ENTRY(Structure):
_anonymous_ = ("u",)
_fields_ = [("dwAltNameChoice", c_ulong),
("u", CERT_ALT_NAME_ENTRY_UNION)]
class CERT_ALT_NAME_INFO(Structure):
_fields_ = [("cAltEntry", c_ulong),
("rgAltEntry", POINTER(CERT_ALT_NAME_ENTRY))]
class CERT_EXTENSION(Structure):
_fields_ = [("pszObjId", c_char_p),
("fCritical", c_bool),
("Value", CRYPT_DATA_BLOB)]
class CERT_EXTENSIONS(Structure):
_fields_ = [("cExtension", c_ulong),
("rgExtension", POINTER(CERT_EXTENSION))]
# Определения констант
CERT_ALT_NAME_REGISTERED_ID = 9
CERT_ALT_NAME_DNS_NAME = 3
X509_ASN_ENCODING = 0x00000001
PKCS_7_ASN_ENCODING = 0x00010000
X509_ALTERNATE_NAME = 12
szOID_CERT_EXTENSIONS = "1.3.6.1.4.1.311.2.1.14"
szOID_SUBJECT_ALT_NAME2 = "2.5.29.17"
CERT_STORE_PROV_SYSTEM_W = 10
CERT_STORE_PROV_SYSTEM = CERT_STORE_PROV_SYSTEM_W
CERT_STORE_OPEN_EXISTING_FLAG = 0x00004000
CERT_SYSTEM_STORE_CURRENT_USER_ID = 1
CERT_SYSTEM_STORE_LOCAL_MACHINE_ID = 2
CERT_SYSTEM_STORE_LOCATION_SHIFT = 16
CERT_SYSTEM_STORE_CURRENT_USER = (CERT_SYSTEM_STORE_CURRENT_USER_ID << CERT_SYSTEM_STORE_LOCATION_SHIFT)
CERT_SYSTEM_STORE_LOCAL_MACHINE = (CERT_SYSTEM_STORE_LOCAL_MACHINE_ID << CERT_SYSTEM_STORE_LOCATION_SHIFT)
HCERTSTORE = c_void_p
HCRYPTPROV = c_ulong
HCRYPTMSG = c_void_p
NCRYPT_KEY_HANDLE = c_ulong
LPSTR = c_char_p
LPCSTR = LPSTR
LPWSTR = c_wchar_p
DWORD = c_ulong
BYTE = c_ubyte
X509_ASN_ENCODING = 0x00000001
PKCS_7_ASN_ENCODING = 0x00010000
CERT_COMPARE_ANY = 0
CERT_COMPARE_NAME_STR_W = 8
CERT_COMPARE_SHIFT = 16
CERT_INFO_SUBJECT_FLAG = 7
CERT_FIND_ANY = (CERT_COMPARE_ANY << CERT_COMPARE_SHIFT)
CERT_FIND_SUBJECT_STR_W = CERT_COMPARE_NAME_STR_W << CERT_COMPARE_SHIFT | CERT_INFO_SUBJECT_FLAG
CERT_X500_NAME_STR = 3
CERT_CLOSE_STORE_FORCE_FLAG = 0x00000001
TRUE = 1
FALSE = 0
CMSG_SIGNER_COUNT_PARAM = 5
CMSG_CONTENT_PARAM = 2
CMSG_CERT_PARAM = 12
CMSG_SIGNED = 2
CMSG_SIGNER_CERT_INFO_PARAM = 7
CRYPT_E_ATTRIBUTES_MISSING = 0x8009100F
CMSG_VERIFY_SIGNER_CERT = 2
CMSG_CTRL_VERIFY_SIGNATURE_EX = 19
CRYPT_ACQUIRE_CACHE_FLAG = 0x00000001
CRYPT_ACQUIRE_COMPARE_KEY_FLAG = 0x00000004
# public key
szOID_CP_GOST_R3410EL = "1.2.643.2.2.19"
szOID_CP_GOST_R3410_12_256 = "1.2.643.7.1.1.1.1"
szOID_CP_GOST_R3410_12_512 = "1.2.643.7.1.1.1.2"
# hash
szOID_CP_GOST_R3411 = "1.2.643.2.2.9"
szOID_CP_GOST_R3411_12_256 = "1.2.643.7.1.1.2.2"
szOID_CP_GOST_R3411_12_512 = "1.2.643.7.1.1.2.3"
szOID_OIWSEC_sha1 = "1.3.14.3.2.26"
szOID_NIST_sha256 = "2.16.840.1.101.3.4.2.1"
class CRYPT_IDENTIFIER(Structure):
_fields_ = [("pszObjId", LPSTR),
("Parameters", CRYPT_DATA_BLOB)]
class FILETIME(Structure):
_fields_ = [("dwLowDateTime", c_uint),
("dwHighDateTime", c_uint)]
class CERT_PUBLIC_KEY_INFO(Structure):
_fields_ = [("Algorithm", CRYPT_IDENTIFIER),
("PublicKey", CRYPT_DATA_BLOB)]
class CERT_INFO(Structure):
_fields_ = [("dwVersion", DWORD),
("SerialNumber", CRYPT_DATA_BLOB),
("SignatureAlgorithm", CRYPT_IDENTIFIER),
("Issuer", CRYPT_DATA_BLOB),
("NotBefore", FILETIME),
("NotAfter", FILETIME),
("Subject", CRYPT_DATA_BLOB),
("SubjectPublicKeyInfo", CERT_PUBLIC_KEY_INFO),
("IssuerUniqueId", CRYPT_DATA_BLOB),
("SubjectUniqueId", CRYPT_DATA_BLOB),
("cExtension", DWORD),
("rgExtension", POINTER(CERT_EXTENSION))]
class CERT_CONTEXT(Structure):
_fields_ = [("dwCertEncodingType", DWORD),
("pbCertEncoded", POINTER(c_ubyte)),
("cbCertEncoded", DWORD),
("pCertInfo", POINTER(CERT_INFO)),
("hCertStore", HCERTSTORE)]
FN_CMSG_STREAM_OUTPUT = CFUNCTYPE(c_void_p, POINTER(BYTE), DWORD, c_bool)
class CMSG_STREAM_INFO(Structure):
_fields_ = [("cbContent", DWORD),
("pfnStreamOutput", POINTER(FN_CMSG_STREAM_OUTPUT)),
("pvArg", c_void_p)]
class CMSG_CTRL_VERIFY_SIGNATURE_EX_PARA(Structure):
_fields_ = [("cbSize", DWORD),
("hCryptProv", HCRYPTPROV),
("dwSignerIndex", DWORD),
("dwSignerType", DWORD),
("pvSigner", c_void_p)]
class CMSG_SIGNER_ENCODE_INFO_UNION(Union):
_fields_ = [("hCryptProv", HCRYPTPROV),
("hNCryptKey", NCRYPT_KEY_HANDLE)]
class CRYPT_ATTRIBUTE(Structure):
_fields_ = [("pszObjId", LPSTR),
("cValue", DWORD),
("rgValue", POINTER(CRYPT_DATA_BLOB))]
class CMSG_SIGNER_ENCODE_INFO(Structure):
_anonymous_ = ("u",)
_fields_ = [("cbSize", DWORD),
("pCertInfo", POINTER(CERT_INFO)),
("u", CMSG_SIGNER_ENCODE_INFO_UNION),
("dwKeySpec", DWORD),
("HashAlgorithm", CRYPT_IDENTIFIER),
("pvHashAuxInfo", c_void_p),
("cAuthAttr", DWORD),
("rgAuthAttr", POINTER(CRYPT_ATTRIBUTE)),
("cUnauthAttr", DWORD),
("rgUnauthAttr", POINTER(CRYPT_ATTRIBUTE))]
class CMSG_SIGNED_ENCODE_INFO(Structure):
_fields_ = [("cbSize", DWORD),
("cSigners", DWORD),
("rgSigners", POINTER(CMSG_SIGNER_ENCODE_INFO)),
("cCertEncoded", DWORD),
("rgCertEncoded", POINTER(CRYPT_DATA_BLOB)),
("cCrlEncoded", DWORD),
("rgCrlEncoded", POINTER(CRYPT_DATA_BLOB))]
# определение прототипа функции CryptAcquireContextA
fCryptAcquireContextA = capi10_dll.CryptAcquireContextA
fCryptAcquireContextA.restype = c_bool
fCryptAcquireContextA.argtypes = [
POINTER(HCRYPTPROV), # Parameters 1 ...
LPCSTR,
LPCSTR,
DWORD,
DWORD]
# определение прототипа функции CryptGetProvParam
fCryptGetProvParam = capi10_dll.CryptGetProvParam
fCryptGetProvParam.restype = c_bool
fCryptGetProvParam.argtypes = [
HCRYPTPROV, # Parameters 1 ...
DWORD,
POINTER(BYTE),
POINTER(DWORD),
DWORD]
# Определяем прототип функции CryptEncodeObject
fCryptEncodeObject = crypt_dll.CryptEncodeObject
fCryptEncodeObject.restype = c_bool
fCryptEncodeObject.argtypes = [
DWORD, # Parameters 1 ...
LPCSTR,
c_void_p,
POINTER(BYTE),
POINTER(DWORD)]
# Определяем прототип функции CertOpenStore
fCertOpenStore = crypt_dll.CertOpenStore
fCertOpenStore.restype = HCERTSTORE
fCertOpenStore.argtypes = [
LPCSTR, # Parameters 1 ...
DWORD,
HCRYPTPROV,
DWORD,
c_void_p]
fCertFindCertificateInStore = crypt_dll.CertFindCertificateInStore
fCertFindCertificateInStore.restype = POINTER(CERT_CONTEXT)
fCertFindCertificateInStore.argtypes = [
HCERTSTORE, # Parameters 1 ...
DWORD,
DWORD,
DWORD,
c_void_p,
POINTER(CERT_CONTEXT)]
fCertNameToStrW = crypt_dll.CertNameToStrW
fCertNameToStrW.restype = DWORD
fCertNameToStrW.argtypes = [
DWORD, # Parameters 1 ...
POINTER(CRYPT_DATA_BLOB),
DWORD,
LPWSTR,
DWORD]
fCertNameToStrA = crypt_dll.CertNameToStrA
fCertNameToStrA.restype = DWORD
fCertNameToStrA.argtypes = [
DWORD, # Parameters 1 ...
POINTER(CRYPT_DATA_BLOB),
DWORD,
LPSTR,
DWORD]
fCertCreateCertificateContext = crypt_dll.CertCreateCertificateContext
fCertCreateCertificateContext.restype = POINTER(CERT_CONTEXT)
fCertCreateCertificateContext.argtypes = [
DWORD,
POINTER(BYTE),
DWORD]
fCertDuplicateCertificateContext = crypt_dll.CertDuplicateCertificateContext
fCertDuplicateCertificateContext.restype = POINTER(CERT_CONTEXT)
fCertDuplicateCertificateContext.argtypes = [POINTER(CERT_CONTEXT)]
fCertCloseStore = crypt_dll.CertCloseStore
fCertCloseStore.restype = c_bool
fCertCloseStore.argtypes = [
HCERTSTORE,
DWORD]
fCertFreeCertificateContext = crypt_dll.CertFreeCertificateContext
fCertFreeCertificateContext.restype = c_bool
fCertFreeCertificateContext.argtypes = [POINTER(CERT_CONTEXT)]
fCryptMsgOpenToDecode = crypt_dll.CryptMsgOpenToDecode
fCryptMsgOpenToDecode.restype = HCRYPTMSG
fCryptMsgOpenToDecode.argtypes = [
DWORD,
DWORD,
DWORD,
HCRYPTPROV,
POINTER(CERT_INFO),
POINTER(CMSG_STREAM_INFO)]
fCryptMsgOpenToEncode = crypt_dll.CryptMsgOpenToEncode
fCryptMsgOpenToEncode.restype = HCRYPTMSG
fCryptMsgOpenToEncode.argtypes = [
DWORD,
DWORD,
DWORD,
c_void_p,
LPSTR,
POINTER(CMSG_STREAM_INFO)]
fCryptMsgUpdate = crypt_dll.CryptMsgUpdate
fCryptMsgUpdate.restype = c_bool
fCryptMsgUpdate.argtypes = [
HCRYPTMSG,
POINTER(BYTE),
DWORD,
c_bool]
fCryptMsgGetParam = crypt_dll.CryptMsgGetParam
fCryptMsgGetParam.restype = c_bool
fCryptMsgGetParam.argtypes = [
HCRYPTMSG,
DWORD,
DWORD,
c_void_p,
POINTER(DWORD)]
fCertCompareCertificate = crypt_dll.CertCompareCertificate
fCertCompareCertificate.restype = c_bool
fCertCompareCertificate.argtypes = [
DWORD,
POINTER(CERT_INFO),
POINTER(CERT_INFO)]
fCryptMsgControl = crypt_dll.CryptMsgControl
fCryptMsgControl.restype = c_bool
fCryptMsgControl.argtypes = [
HCRYPTMSG,
DWORD,
DWORD,
c_void_p]
fCryptAcquireCertificatePrivateKey = crypt_dll.CryptAcquireCertificatePrivateKey
fCryptAcquireCertificatePrivateKey.restype = c_bool
fCryptAcquireCertificatePrivateKey.argtypes = [
POINTER(CERT_CONTEXT),
DWORD,
c_void_p,
POINTER(HCRYPTPROV),
POINTER(DWORD),
POINTER(c_bool)]
fCryptMsgClose = crypt_dll.CryptMsgClose
fCryptMsgClose.restype = c_bool
fCryptMsgClose.argtypes = [HCRYPTMSG]
fCryptMsgCalculateEncodedLength = crypt_dll.CryptMsgCalculateEncodedLength
fCryptMsgCalculateEncodedLength.restype = DWORD
fCryptMsgCalculateEncodedLength.argtypes = [DWORD,
DWORD,
DWORD,
c_void_p,
LPSTR,
DWORD]