-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicao-dsprobe.py
95 lines (67 loc) · 2.47 KB
/
icao-dsprobe.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
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 14 21:25:24 2022
@author: Vitaly Zuevsky
The latest collection of DSCA Certificates
at https://pkddownloadsg.icao.int/download
"""
import re
import sys
from pathlib import Path
from os import environ
from base64 import b64decode
from subprocess import Popen, PIPE
matched = 0
truncat = True
def procList():
global matched, truncat
der = b64decode(coded)
proc = Popen(['openssl', 'x509', '-inform', 'der', '-noout', '-serial',\
'-fingerprint', '-issuer', '-subject', '-ext',\
'subjectKeyIdentifier,subjectAltName,authorityKeyIdentifier,issuerAltName'\
], stdin=PIPE, stdout=PIPE, stderr=PIPE, env=environ)
out, err = proc.communicate(der)
if err: print(err.decode())
else:
grepable = ''.join(out.decode().split(':')).upper()
decimal = str(int(re.search('SERIAL=(\S+)', grepable).group(1), base=16))
grepable = decimal + '\n' + grepable
if needle in grepable:
matched += 1
Path(f"DSCA/{needle}").mkdir(parents=True, exist_ok=True)
if truncat:
truncat = False
with open(f"DSCA/{needle}/list", 'w'): pass
with open(f"DSCA/{needle}/list", 'a') as f:
f.write(grepable + '\n')
file = re.search('FINGERPRINT=(\w+)', grepable).group(1)
proc = Popen(['openssl', 'x509', '-inform', 'der', '-outform', 'pem',\
'-out', f"DSCA/{needle}/{file}.pem"], env=environ,\
stdin=PIPE, stdout=PIPE, stderr=PIPE)
out, err = proc.communicate(der)
if err: print(err.decode())
# 1/0
# main :
if len(sys.argv) < 3:
print(f"\nUsage: python3 {sys.argv[0]} <icaopkd-001-dsccrl-00XXXX.ldif> <needle>\n")
sys.exit()
with open(sys.argv[1], "r") as f:
lines = [line.rstrip() for line in f.readlines()]
needle = ''.join(sys.argv[2].split(':')).upper()
count = 0
coded = ''
followUp = False
print()
for i in lines:
if followUp:
if i.startswith(' '): coded += i.split(' ')[-1]
else:
procList()
count += 1
coded = ''
followUp = False
print(f"\r{count}", end='')
if i.startswith('userCertificate;binary:: '):
coded = i.split(' ')[-1]
followUp = True
print(f" inspected, {matched} certificate(s) dumped in DSCA/{needle} folder\n")