|
@@ -0,0 +1,44 @@
|
|
|
+#!/usr/bin/env python3
|
|
|
+
|
|
|
+import cryptography.hazmat.backends
|
|
|
+import cryptography.hazmat.primitives.serialization
|
|
|
+import math
|
|
|
+import sys
|
|
|
+
|
|
|
+backend = cryptography.hazmat.backends.default_backend()
|
|
|
+
|
|
|
+with open('smartcard-app-id.hex', 'r') as f:
|
|
|
+ appid = int(f.read(), 16)
|
|
|
+
|
|
|
+with open('pub.pem', 'rb') as f:
|
|
|
+ pubkey = cryptography.hazmat.primitives.serialization.load_pem_public_key(
|
|
|
+ f.read(),
|
|
|
+ backend,
|
|
|
+ )
|
|
|
+
|
|
|
+pubnums = pubkey.public_numbers()
|
|
|
+
|
|
|
+key_data = ['shadowed-private-key', [
|
|
|
+ 'rsa',
|
|
|
+ ['n', pubnums.n],
|
|
|
+ ['e', pubnums.e],
|
|
|
+ ['shadowed', 't1-v1', [appid, 'OPENPGP.1']],
|
|
|
+]]
|
|
|
+
|
|
|
+def convert(data):
|
|
|
+ if isinstance(data, int):
|
|
|
+ return convert(data.to_bytes(
|
|
|
+ math.ceil(data.bit_length() / 8),
|
|
|
+ 'big',
|
|
|
+ ))
|
|
|
+ elif isinstance(data, str):
|
|
|
+ return convert(data.encode())
|
|
|
+ elif isinstance(data, bytes):
|
|
|
+ return str(len(data)).encode() + b':' + data
|
|
|
+ else:
|
|
|
+ return b'(' + b''.join(convert(i) for i in data) + b')'
|
|
|
+
|
|
|
+key = convert(key_data)
|
|
|
+
|
|
|
+with open('gpg-key', 'wb') as f:
|
|
|
+ f.write(key)
|