-
Notifications
You must be signed in to change notification settings - Fork 0
/
p7b2certs.py
44 lines (32 loc) · 1.53 KB
/
p7b2certs.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
from asn1crypto import cms,x509,pem
import sys
import os
p7bfile: str = sys.argv[1]
if (not os.path.isfile(p7bfile)):
exit("File does not exist")
else:
with open(p7bfile, 'rb') as p7f:
p7b = cms.ContentInfo.load(p7f.read())
for fpkicert in p7b['content']['certificates']:
try:
cert = x509.Certificate.load(fpkicert.dump())
except Exception as e:
print("Error loading certificate from p7b: ", e)
continue
cert_subj_rdn_type = list(cert.subject.native.keys())[-1]
cert_subj_rdn_value = cert.subject.native[cert_subj_rdn_type]
if (isinstance(cert_subj_rdn_value, list)):
cert_subj_rdn_value = cert_subj_rdn_value[-1]
cert_issuer_rdn_type = list(cert.issuer.native.keys())[-1]
cert_issuer_rdn_value = cert.issuer.native[cert_issuer_rdn_type]
if(isinstance(cert_issuer_rdn_value, list)):
cert_issuer_rdn_value = cert_issuer_rdn_value[-1]
filename = f"exports/{cert_issuer_rdn_value}--to--{cert_subj_rdn_value}--{cert.serial_number}.cer"
print("Writing ", filename)
try:
with open(filename, 'wb') as certfile:
pem_bytes = pem.armor('CERTIFICATE', cert.dump())
certfile.write(pem_bytes)
except Exception as e:
print("Unable to open file ", filename, ": ", e)
print("Number of certs: ", len(p7b['content']['certificates']))