Add get_certs_from_cpl.
[dcpomatic.git] / hacks / get_certs_from_cpl
1 #!/usr/bin/python3
2
3 import os
4 import sys
5
6 import bs4
7
8 with open(sys.argv[1]) as file:
9     xml = file.read()
10
11 soup = bs4.BeautifulSoup(xml, 'xml')
12 signature = soup.CompositionPlaylist.Signature
13 if not signature:
14     print("Unsigned CPL")
15     sys.exit(0)
16
17 number_of_certs = len(signature.KeyInfo.findAll('X509Certificate'))
18
19 n = 0
20 for data in signature.KeyInfo.findAll('X509Certificate'):
21     if n == 0:
22         name = 'leaf'
23     elif n == number_of_certs - 1:
24         name = 'root'
25     else:
26         name = f'inter_{number_of_certs - n - 1}'
27
28     with open(f"{name}.pem", "w") as out:
29         print("-----BEGIN CERTIFICATE-----", file=out)
30         print(data.text, file=out)
31         print("-----END CERTIFICATE-----", file=out)
32     os.system(f"openssl x509 -text -in {name}.pem > {name}.dump")
33     os.system(f"openssl asn1parse < {name}.pem > {name}.asn1")
34     n += 1
35
36