File indexing completed on 2024-12-29 04:51:02

0001 #!/usr/bin/env python3
0002 # SPDX-FileCopyrightText: 2023 Volker Krause <vkrause@kde.org>
0003 # SPDX-License-Identifier: LGPL-2.0-or-later
0004 
0005 import argparse
0006 import base64
0007 import json
0008 import os
0009 import requests
0010 
0011 def hexToBase64(s):
0012     if len(s) % 2 == 1:
0013         s = '0' + s
0014     return base64.b64encode(bytes.fromhex(s)).decode()
0015 
0016 parser = argparse.ArgumentParser(description='Download RSP-6 public keys')
0017 parser.add_argument('--output', type=str, required=True, help='Path to which the output should be written')
0018 arguments = parser.parse_args()
0019 
0020 os.makedirs(arguments.output, exist_ok = True)
0021 
0022 req = requests.get('https://git.eta.st/eta/rsp6-decoder/raw/branch/master/keys.json')
0023 keys = json.loads(req.content)
0024 issuers = []
0025 
0026 # remove all existing certs so we clean up revoked/expired ones
0027 for f in os.listdir(arguments.output):
0028     if f.endswith(".json"):
0029         os.remove(os.path.join(arguments.output, f))
0030 
0031 # create per issuer key files
0032 for issuerId in keys:
0033     issuerKeys = []
0034     for key in keys[issuerId]:
0035         k = {}
0036         k['n'] = hexToBase64(key['modulus_hex'])
0037         k['e'] = hexToBase64(key['public_exponent_hex'])
0038         issuerKeys.append(k)
0039     keysFile = open(os.path.join(arguments.output, issuerId + '.json'), 'w')
0040     keysFile.write(json.dumps(issuerKeys))
0041     issuers.append(issuerId)
0042 
0043 # write qrc file
0044 issuers.sort()
0045 qrcFile = open(os.path.join(arguments.output, 'rsp6-keys.qrc'), 'w')
0046 qrcFile.write("""<!--
0047     SPDX-FileCopyrightText: none
0048     SPDX-License-Identifier: CC0-1.0
0049 -->
0050 <RCC>
0051   <qresource prefix="/org.kde.pim/kitinerary/rsp6/keys/">
0052 """)
0053 for issuerId in issuers:
0054     qrcFile.write(f"    <file>{issuerId}.json</file>\n")
0055 qrcFile.write("""  </qresource>
0056 </RCC>""")
0057 qrcFile.close()