-
Notifications
You must be signed in to change notification settings - Fork 0
/
make-p12.py
341 lines (304 loc) · 20 KB
/
make-p12.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
#!/usr/bin/env python3
"""
make-p12
Утилита для создания невалидных сертификатов НУЦ РК по различным шаблонам:
- Сертификат физического лица для аутентификации (AUTH_RSA256_*.p12)
- Сертификат физического лица для подписи (RSA256_*.p12)
- Сертификат юридического лица для аутентификации (AUTH_RSA256_*.p12)
Установка зависимостей Python 3.10+: pip install -r requirements.txt
The software is written for educational purposes only. Use only if you have permission of the owner
Программное обеспечение написано только для образовательных целей. Используйте только с разрешения владельца
Copyright (c) 2023 Konstantin Burov. All rights reserved.
This work is licensed under the terms of the MIT license.
For a copy, see <https://opensource.org/licenses/MIT>.
https://github.com/sadshade/FCALayer/
"""
import re
import os
import base64
import sys
import getopt
from datetime import datetime, timedelta
from cryptography.hazmat.primitives import serialization
from pyasn1.codec.der import decoder as asn1decoder
from pyasn1.codec.der import encoder as asn1encoder
from cryptography import x509
from cryptography.hazmat.primitives.serialization import pkcs12
from cryptography.hazmat.primitives.serialization import BestAvailableEncryption
# Шаблон сертификата X.509 для физического лица
IND_CERT_AUTH: str = """
MIIGVTCCBD2gAwIBAgIUOo7keDU4mX/KewnAlVWyiq8KlO8wDQYJKoZIhvcNAQELBQAwUjELMAkGA1UEBhMCS1oxQzBBBgNV
BAMMOtKw0JvQotCi0KvSmiDQmtCj05jQm9CQ0J3QlNCr0KDQo9Co0Ksg0J7QoNCi0JDQm9Cr0pogKFJTQSkwHhcNMjMxMDI0MDM0NzA4WhcNMjQxMDIzMDM0
NzA4WjB0MR4wHAYDVQQDDBXQmNC80Y8g0KTQsNC80LjQu9C40Y8xCzAJBgNVBAQMAm5vMRgwFgYDVQQFEw9JSU4xMTExMTExMTExMTExCzAJBgNVBAYTAkta
MQswCQYDVQQqDAJubzERMA8GCSqGSIb3DQEJARYCbm8wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDVY0797mzvqbhObsIRepFcZXQCak3l8YGP
brXkTm3UodwOF5zARFniVYtGS1UA7FgrLaIEIzw3HrD6YidcGd1vsE4cUiPyGY6XWEZQ6pdHhw0J++Z6qVS0aGPRJR28FeVuDRK+bqJztrH1+isft/HYSdn4
FVENrlLauh+EPBfcJ0XUsojPJx5RbWV7d0tUlgfq76M1j/jdSTdm4ToBUwIw9nslk+L+qEKVjz7tDv6SlLr39eXQCaFvCM7QakUeuL9TB/wNZwSQ+psxUKxQ
KppTyBoLkQ8cD8hgRdCZ4ek0oGiudls0/KF6JoQnLAjUyKT75pMtkrWzi/HTmhmzbTTBAgMBAAGjggH/MIIB+zAoBgNVHSUEITAfBggrBgEFBQcDAgY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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==
"""
# Шаблон сертификата X.509 для физического лица для подписи (RSA_*.p12)
IND_CERT_SIGN: str = """MIIGVDCCBDygAwIBAgIUOo7keDU4mX/KewnAlVWyiq8KlO8wDQYJKoZIhvcNAQELBQAwUjELMAkGA1UEBhMCS1oxQzBBBgNVBAMMO
tKw0JvQotCi0KvSmiDQmtCj05jQm9CQ0J3QlNCr0KDQo9Co0Ksg0J7QoNCi0JDQm9Cr0pogKFJTQSkwHhcNMjMxMDI0MDM0NzA4WhcNMjQxMDIzMDM0NzA4W
jB0MR4wHAYDVQQDDBXQmNC80Y8g0KTQsNC80LjQu9C40Y8xCzAJBgNVBAQMAm5vMRgwFgYDVQQFEw9JSU4xMTExMTExMTExMTExCzAJBgNVBAYTAktaMQswC
QYDVQQqDAJubzERMA8GCSqGSIb3DQEJARYCbm8wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDVY0797mzvqbhObsIRepFcZXQCak3l8YGPbrXkT
m3UodwOF5zARFniVYtGS1UA7FgrLaIEIzw3HrD6YidcGd1vsE4cUiPyGY6XWEZQ6pdHhw0J++Z6qVS0aGPRJR28FeVuDRK+bqJztrH1+isft/HYSdn4FVENr
lLauh+EPBfcJ0XUsojPJx5RbWV7d0tUlgfq76M1j/jdSTdm4ToBUwIw9nslk+L+qEKVjz7tDv6SlLr39eXQCaFvCM7QakUeuL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HczdbyJ+kFcDS43k
qbghWJ6PBqbN2w0eKddE1XUiTJuWL8Yg8J6jAu8CgYBBnhe/BJWyha0m4ODhNSyVnSBlwuTzvp5KBgxhVlWgX2djwCMDZzfTuAoVBd1CjfyKbcudE4RLktiQ
smGiJXYwqzKivS4+s+lcckGsDkLes0DM1V60nK9h019EbahU2zMc+4HziIk6NnQjhbKhVwZ074exQngtzEkWd40LYW7aqQKBgCdOwCdbePYqL+tO+T6nHsbi
nBxhJnx9X5XWftZY96AKjqPjxLhjvblesX7rf5Dim0BmWguP2byuOooqwDPoolQFT+osa4D9cT4jQJ0Md2aLCGhWrma1R/4FdRocvLiJiR6hPMNuM1h6qzNl
OO2Vt6qaHkOG+zAJrlT42gLolx7T
"""
def print_help_and_exit():
print(os.path.basename(__file__) +
':\n\t★ --type ind|ent (Физическое/Юридическое лицо)'
'\n\t --iin ИИН [111111111110]'
'\n\t --commonname [ИВАНОВ ИВАН]'
'\n\t --surname [ИВАНОВ]'
'\n\t --country [KZ]'
'\n\t --locality [АСТАНА]'
'\n\t --state [АСТАНА]'
'\n\t --org [ТОО Рога и копыта]'
'\n\t --bin БИН [222222222220]'
'\n\t --givenname [ИВАНОВИЧ]'
'\n\t --email [test@example.com]'
'\n\t --keytype [auth] | sign '
'\n\t --file (Path to PKCS#12 container)'
'\n\t --password')
sys.exit(1)
def get_cert_params(argv):
# Сбор параметров запуска
cert_info = dict(type='', iin='111111111110', commonname='ИВАНОВ ИВАН', surname='ИВАНОВ', country='KZ',
locality='АСТАНА', state='АСТАНА', org='ТОО Рога и копыта', bin='222222222220',
givenname='ИВАНОВИЧ', email='test@example.com', keytype='auth', file='', password='')
opts, args = getopt.getopt(argv, "h",
["type=", "iin=", "bin=", "commonname=", "surname=", "country=", "givenname=", "email=",
"locality=", "state=", "org=", "bin=", "keytype=", "file=", "password="])
if not opts:
opts = [('-h', '')]
# Check args
for opt, arg in opts:
match opt:
case '-h':
print_help_and_exit()
case "--type":
# Юридическое/Физическое лицо
if arg in {"ind", "ent"}:
cert_info['type'] = arg
else:
print_help_and_exit()
case "--commonname":
# CN (Фамилия Имя)
cert_info['commonname'] = arg
case "--surname":
# SN (Фамилия)
cert_info['surname'] = arg
case "--iin":
# serialNumber (ИИН)
if len(arg) != 12:
print("*** Warn: Make sure IIN is the correct length.")
cert_info['iin'] = arg
case "--country":
# C (Country)
cert_info['country'] = arg
case "--locality":
# L (Город)
cert_info['locality'] = arg
case "--state":
# S (Область)
cert_info['state'] = arg
case "--org":
# O (Компания)
cert_info['org'] = arg
case "--bin":
# OU (БИН)
if len(arg) != 12:
print("*** Warn: Make sure BIN is the correct length.")
cert_info['bin'] = arg
case "--givenname":
# G (Отчество)
cert_info['givenname'] = arg
case "--email":
# E (email)
cert_info['email'] = arg
case "--file":
# (file)
cert_info['file'] = arg
case "--password":
# (passwd)
cert_info['password'] = arg
case "--keytype":
# Тип ключа:
# auth - для аутентификации
# sign - для подписи (только для физ.лица)
if arg in ("auth", "sign"):
cert_info['keytype'] = arg
else:
print("*** Err: Unsupported key type!")
sys.exit(1)
if cert_info['type'] == '':
print_help_and_exit()
return cert_info
def get_altered_cert(cert_info):
# Изменения шаблона сертификата по заданным параметрам
cert = None
match cert_info['type']:
case "ind":
match cert_info['keytype']:
case "auth":
cert, rest = asn1decoder.decode(base64.b64decode(IND_CERT_AUTH.encode()))
case "sign":
cert, rest = asn1decoder.decode(base64.b64decode(IND_CERT_SIGN.encode()))
case "ent":
if cert_info['keytype'] == "sign":
print("*** Err: Sign key type only supported for individuals")
sys.exit(1)
else:
cert, rest = asn1decoder.decode(base64.b64decode(ENT_CERT_AUTH.encode()))
'''
E = дрес электронный почты
Serialnumber = IIN012345678910
CN = Фамилия Имя
SN = Фамилия
OU = BIN012345678910
O = Наименование организации
L = Город регистрации юридического лица
S = Область регистрации юридического лица
C = KZ
G = Отчество
'''
# Changing certificate properties according to run params
if cert_info['commonname'] != '':
# CN (Фамилия Имя)
cert['field-0']['field-5'][0][0]['field-1'] = cert_info['commonname']
if cert_info['surname'] != '':
# SN (Фамилия)
cert['field-0']['field-5'][1][0]['field-1'] = cert_info['surname']
if cert_info['iin'] != '':
# Serialnumber (ИИН)
if re.search(r"^\d{12}$", cert_info['iin']) and not re.search(r"IIN", cert_info['iin']):
cert['field-0']['field-5'][2][0]['field-1'] = 'IIN' + cert_info['iin']
else:
cert['field-0']['field-5'][2][0]['field-1'] = cert_info['iin']
if cert_info['country'] != '':
# C (Country)
cert['field-0']['field-5'][3][0]['field-1'] = cert_info['country']
if cert_info['locality'] != '' and cert_info['type'] == "ent":
# L (Город)
cert['field-0']['field-5'][4][0]['field-1'] = cert_info['locality']
if cert_info['state'] != '' and cert_info['type'] == "ent":
# S (Область)
cert['field-0']['field-5'][5][0]['field-1'] = cert_info['state']
if cert_info['org'] != '' and cert_info['type'] == "ent":
# O (Компания)
cert['field-0']['field-5'][6][0]['field-1'] = cert_info['org']
if cert_info['bin'] != '' and cert_info['type'] == "ent":
# OU (БИН)
if re.search(r"^\d{12}$", cert_info['bin']) and not re.search(r"BIN", cert_info['iin']):
cert['field-0']['field-5'][7][0]['field-1'] = 'BIN' + cert_info['bin']
else:
cert['field-0']['field-5'][7][0]['field-1'] = cert_info['bin']
if cert_info['givenname'] != '':
# G (Отчество)
if cert_info['type'] == "ent":
cert['field-0']['field-5'][8][0]['field-1'] = cert_info['givenname']
else:
cert['field-0']['field-5'][4][0]['field-1'] = cert_info['givenname']
if cert_info['email'] != '':
# E (email)
if cert_info['type'] == "ent":
cert['field-0']['field-5'][9][0]['field-1'] = cert_info['email']
else:
cert['field-0']['field-5'][5][0]['field-1'] = cert_info['email']
# Рандомная подпись сертификата, привет QazCloud
# VXNlIHlvdXIgZ2NocSBtYWdpYw0KDQoyNTMxNDYyNTQzMzIyNTM4NDIyNTMwMzgyNTMwMzAyNTMwMzQyNTM2MzAyNTMzNDU2NjI1MzAzMDI1NDMzMzI1NDI0NjI1MzI0NDI1NDMzMzI1MzgzOTQxMjUzMDQxNDIyNTMyMzEyNTMxMzAyNTMwMzAyNTQzMzMyNTM5MzAyNTQzMzIyNTQxNDIyNTMwNDM2ZTQ2MjU0MzMyMjU0MTM5MjU0MzMzMjUzODM0MjU0MzMyMjUzODM0MzY0MTI1MzIzNzMxMjUzMDM5MjU0MzMzMjUzODM2MjU0MzMzMjUzODM2MjUzMTM4MjU0MzMzMjU0MjQxMjUzMjM5MjU0MzMzMjU0MTM4MjUzMjQzMjU0MzMzMjU0MjQxMjU0MzMyMjU0MjM3NzcyNTQzMzMyNTQyMzMyNTQzMzMyNTM5MzcyNTQzMzIyNTQxNDYyNTQzMzMyNTQyMzAyNTQzMzMyNTM5MzAyNTQzMzIyNTM4MzQ1NTc4MjUzNzQyMjUzNTQ2MjUzMjMyNjY3ODI1MzAzMDQ5MjU0MzMyMjU0MjMzMjU0MzMzMjUzOTMyMjU0MzMzMjUzOTM0MjU0MzMzMjUzOTM3MjU0MzMzMjUzOTMxMjUzNzQ2MjUzMjQ2MjU0MzMzMjUzOTQxMjU0MzMyMjUzOTM1MjU0MzMyMjU0MTM3MjUzMjM1MjUzNzQzMjU0MzMzMjU0MjQ1NDMyNTM0MzAzODI1NDMzMzI1MzgzMTI1NDMzMzI1NDEzNDMyNTgyNTQzMzIyNTQxMzcyNTQzMzMyNTM5MzcyNTQzMzMyNTQyNDUyNTQzMzMyNTQxMzU3NjI1NDMzMzI1NDEzODI1NDMzMzI1NDE0NDI1MzE0MTI1NDMzMzI1Mzk0NDI1MzEzOTI1MzA0MzI1NDMzMzI1MzgzOTI1NDMzMzI1MzgzNzM4MjU0MzMyMjUzOTM3MjU0MzMzMjU0MTMyMjUzMzQ0MjUzMjQ2MjUzMDM1MjU0MzMyMjU0MjQ0MzIyNTMwMzY1NDI1MzAzMDI1MzAzMDI1MzAzMA==
# Adjusting the certificate date in the range of 180 days
cert['field-0']['field-4'][0] = (datetime.now() - timedelta(days=180)).strftime("%y%m%d%H%M%S") + 'Z'
cert['field-0']['field-4'][1] = (datetime.now() + timedelta(days=180)).strftime("%y%m%d%H%M%S") + 'Z'
# Serialize back to ASN.1 and return in base64 encoded certificate
return base64.b64encode(asn1encoder.encode(cert)).decode('ascii')
def main(argv):
global altered_cert
global cert_info
cert_info = get_cert_params(argv)
altered_cert = get_altered_cert(cert_info)
if cert_info['file'] == None or cert_info['password'] == None:
print("*** Err: No file name or password provided")
sys.exit(1)
try:
cert = x509.load_der_x509_certificate(base64.b64decode(altered_cert))
key = serialization.load_der_private_key(base64.b64decode(PRIVATE_KEY), None)
p12 = pkcs12.serialize_key_and_certificates(
b"26C4C49698E130D170439A057A09796A8128C085", key, cert, None, BestAvailableEncryption(
cert_info['password'].encode('utf-8'))
)
with open(cert_info['file'], "wb") as file:
file.write(p12)
file.close()
print("OK")
except OSError as err:
print("*** Err: " + err.strerror)
sys.exit(1)
except ValueError as err:
print("*** Err: " + err.__str__())
sys.exit(1)
if __name__ == "__main__":
main(sys.argv[1:])