-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathkey.py
More file actions
95 lines (81 loc) · 3.2 KB
/
Copy pathkey.py
File metadata and controls
95 lines (81 loc) · 3.2 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from datetime import datetime, timezone
from PGPy.pgpy import PGPKey, PGPUID
from PGPy.pgpy.constants import (
PubKeyAlgorithm,
KeyFlags,
EllipticCurveOID,
HashAlgorithm,
SymmetricKeyAlgorithm,
CompressionAlgorithm,
)
from PGPy.pgpy.packet.fields import ECPoint
KEY_ALGORITHM = PubKeyAlgorithm.ECDSA
# secp256r1
# KEY_CURVE = EllipticCurveOID.NIST_P256
KEY_CURVE = EllipticCurveOID.SECP256K1
KEY_CREATION_TIME = datetime(2009, 1, 3, 18, 5, 5, tzinfo=timezone.utc)
UID_PARAMS = {
"usage": {KeyFlags.Sign},
"hashes": [HashAlgorithm.SHA256],
"ciphers": [SymmetricKeyAlgorithm.AES256],
"compression": [CompressionAlgorithm.ZLIB],
"created": KEY_CREATION_TIME,
}
class KeyManager:
"""Class to manage the creation and injection of keys"""
def __init__(self):
self.uid = None
self.key = None
self.cert_sig = None
def load_key(self, pubkey):
"""Create a new key pair and inject a pubkey point from an existing key"""
key = PGPKey.new(KEY_ALGORITHM, KEY_CURVE, created=KEY_CREATION_TIME)
key._key.keymaterial.p = pubkey._key.keymaterial.p
key.add_uid(
pubkey.userids[0],
selfsign=False,
usage={KeyFlags.Sign},
hashes=[HashAlgorithm.SHA256],
ciphers=[SymmetricKeyAlgorithm.AES256],
compression=[CompressionAlgorithm.ZLIB],
)
return key
def _reverse64(self, b: bytes) -> bytes:
"""Converts (a,b) from little (secp256k1) to big endian to be consistent with PGPy"""
if len(b) != 64:
raise ValueError(f"Expected 64 bytes for reversal, got {len(b)}")
x = b[:32]
y = b[32:]
return x[::-1] + y[::-1]
def create_key(
self,
name: str,
email: str,
ext_key_material: bytes,
curve: EllipticCurveOID = KEY_CURVE,
) -> bytes:
"""Create a new key pair and inject a pubkey point from an existing key"""
# Validate key length
if len(ext_key_material) != 64:
raise ValueError(
f"Expected 64 bytes key material, got {len(ext_key_material)} bytes"
)
# Process key material according to curve requirements
if curve == EllipticCurveOID.SECP256K1:
# SECP256K1 needs conversion to big endian
ext_key_material = self._reverse64(ext_key_material)
elif curve == EllipticCurveOID.NIST_P256:
pass # No conversion needed
else:
raise ValueError(f"Unsupported curve: {curve}")
# Create key with processed material
self.key = PGPKey.new(KEY_ALGORITHM, curve, created=KEY_CREATION_TIME)
original_key_point = self.key._key.keymaterial.p.to_mpibytes()
injected_key_point = original_key_point[:3] + ext_key_material
self.key._key.keymaterial.p = ECPoint(injected_key_point)
self.uid = PGPUID.new(name, email=email)
sig_data = self.key.add_uid(self.uid, extract=True, **UID_PARAMS)
return sig_data
def inject_key(self, injected_cert: bytes) -> None:
"""Inject a pubkey point from an existing key"""
self.cert_sig = self.key.add_uid(self.uid, inject=injected_cert, **UID_PARAMS)