forked from LeontyV/egrul_parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_cert.py
54 lines (49 loc) · 2.16 KB
/
check_cert.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
import OpenSSL
from asn1crypto import x509
import base64
def get_cert_attr(cert_path):
result = {}
cert_obj = open(cert_path, 'rb').read()
try:
cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_ASN1, cert_obj)
except OpenSSL.crypto.Error:
cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert_obj)
not_after = cert.get_notAfter().decode('ascii') # 20211029094827
hours_after = int(not_after[8:10]) + 3
result['конец'] = f'{not_after[6:8]}.{not_after[4:6]}.{not_after[:4]} {str(hours_after)}:{not_after[10:12]}:{not_after[12:14]}'
not_before = cert.get_notBefore().decode('ascii')
hours_before = int(not_before[8:10]) + 3
result['начало'] = f'{not_before[6:8]}.{not_before[4:6]}.{not_before[:4]} {str(hours_before)}:{not_before[10:12]}:{not_before[12:14]}'
result['Серийный номер'] = '{0:x}'.format(cert.get_serial_number()).upper()
subject_ext = cert.get_subject().get_components()
sn = ''
gn = ''
l = ''
street = ''
for key, value in cert.get_subject().get_components():
#print(key.decode('utf-8'), value.decode('utf-8'))
if key.decode('utf-8') == 'CN':
result['Общее имя'] = value.decode('utf-8')
if key.decode('utf-8') == 'SN':
sn = value.decode('utf-8')
if key.decode('utf-8') == 'GN':
gn = value.decode('utf-8')
if sn and gn:
result['ФИО'] = sn + ' ' + gn
else:
result['ФИО'] = ''
if key.decode('utf-8') == 'INN':
result['ИНН'] = value.decode('utf-8')
if key.decode('utf-8') == 'OGRN':
result['ОГРН'] = value.decode('utf-8')
if key.decode('utf-8') == 'SNILS':
result['СНИЛС'] = value.decode('utf-8')
if key.decode('utf-8') == 'O':
result['Организация'] = value.decode('utf-8')
if key.decode('utf-8') == 'L':
l = value.decode('utf-8')
if key.decode('utf-8') == 'street':
street = value.decode('utf-8')
if l and street:
result['Адрес'] = l + ' ' + street
return result