Files

13 lines
941 B
Python

#!/usr/bin/env python3
"""Emit pubkey.h: the patch-manifest public key as a BCRYPT_RSAPUBLIC_BLOB (RSA1 header, exponent, modulus big-endian)."""
import re, struct, subprocess, sys
from pathlib import Path
pem = Path(sys.argv[1]); out = Path(sys.argv[2])
t = subprocess.run(['openssl', 'pkey', '-pubin', '-in', str(pem), '-noout', '-text'], capture_output=True, text=True, check=True).stdout
mod = bytes.fromhex(''.join(re.search(r'Modulus:\s*\n((?:\s+[0-9a-f:]+\n)+)', t).group(1).split()).replace(':', ''))
mod = mod.lstrip(b'\0'); e = int(re.search(r'Exponent: (\d+)', t).group(1)); eb = e.to_bytes((e.bit_length() + 7) // 8, 'big')
blob = b'RSA1' + struct.pack('<IIIII', len(mod) * 8, len(eb), len(mod), 0, 0) + eb + mod
out.write_text('// generated by gen_pubkey.py from ' + pem.name + '\nstatic const unsigned char kPatchPublicKey[] = {' +
','.join(str(b) for b in blob) + '};\n')
print(len(mod) * 8, 'bit key ->', out)