forked from ria-ee/X-Road-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathocsp_produced.py
executable file
·81 lines (72 loc) · 3.6 KB
/
ocsp_produced.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
#!/usr/bin/python3
"""Get OCSP production time for X-Road certificates."""
import argparse
import calendar
import os
import re
import sys
import time
from subprocess import Popen, PIPE, check_output
from xml.etree import ElementTree
def main():
"""Main function"""
parser = argparse.ArgumentParser(
description='Get OCSP production time for X-Road certificates.',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog='Status returns number of seconds since production of oldest OCSP response.'
)
parser.add_argument('-s', help='Output status only', action="store_true")
args = parser.parse_args()
cache = {}
for file_name in os.listdir('/var/cache/xroad'):
if re.match(r'^.*\.ocsp$', file_name):
out = check_output(
['openssl', 'ocsp', '-noverify', '-text', '-respin',
f'/var/cache/xroad/{file_name}']).decode('utf-8')
search = re.search('^ {6}Serial Number: (.+)$', out, re.MULTILINE)
if search and search.group(1):
cache[search.group(1)] = out
ocsp_time = 0
with open('/etc/xroad/signer/keyconf.xml', 'r', encoding='utf-8') as keyconf:
root = ElementTree.fromstring(keyconf.read())
for key in root.findall('./device/key'):
key_type = 'SIGN' if key.attrib['usage'] == 'SIGNING' else 'AUTH'
key_id = key.find('./keyId').text
friendly_name = key.find('./friendlyName').text if \
key.find('./friendlyName') is not None \
and key.find('./friendlyName').text is not None else ''
for cert in key.findall('./cert'):
if not (cert.attrib['active'] == 'true' and cert.find(
'./status').text == 'registered'):
continue
contents = cert.find('./contents').text
# Adding newlines to base64
contents = '\n'.join([contents[i:i + 76] for i in range(0, len(contents), 76)])
pem = f'-----BEGIN CERTIFICATE-----\n{contents}\n-----END CERTIFICATE-----\n'
with Popen(
['openssl', 'x509', '-noout', '-serial'],
stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc:
stdout, _ = proc.communicate(pem.encode('utf-8'))
search = re.match('^serial=(.+)$', stdout.decode('utf-8'))
if search and search.group(1):
serial = search.group(1)
search = re.search(
'^ {4}Produced At: (.+)$', cache.get(serial, ''), re.MULTILINE)
if serial in cache and search and re.search(
'^ {4}Cert Status: good$', cache.get(serial, ''), re.MULTILINE):
produced_time = time.strptime(search.group(1), '%b %d %H:%M:%S %Y %Z')
produced = time.strftime('%Y-%m-%d %H:%M:%S', produced_time)
if not args.s:
print(f'{produced}\t{key_type}\t{key_id}\t{friendly_name}')
elif not ocsp_time or calendar.timegm(produced_time) > ocsp_time:
ocsp_time = calendar.timegm(produced_time)
elif not args.s:
print(f'ERROR\t{key_type}\t{key_id}\t{friendly_name}')
else:
# One of certificates does not have OCSP response
print(1000000000)
sys.exit(0)
if args.s and ocsp_time:
print(int(time.time()) - ocsp_time)
if __name__ == '__main__':
main()