From 2ce448f7367e209ba700f2ea72b316b3dfbe3e2e Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Wed, 22 Jul 2015 13:47:53 +0300 Subject: [PATCH] formats: split serialization from decompression --- sshagent/formats.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/sshagent/formats.py b/sshagent/formats.py index c7b49f7..a95811b 100644 --- a/sshagent/formats.py +++ b/sshagent/formats.py @@ -9,7 +9,7 @@ import logging log = logging.getLogger(__name__) DER_OCTET_STRING = b'\x04' -ECDSA_KEY_TYPE = b'ecdsa-sha2-nistp256' +ECDSA_KEY_PREFIX = b'ecdsa-sha2-' ECDSA_CURVE_NAME = b'nistp256' hashfunc = hashlib.sha256 @@ -61,15 +61,19 @@ def decompress_pubkey(pub, curve=ecdsa.NIST256p): y = (P-beta) if ((beta + p0) % 2) else beta point = ecdsa.ellipticcurve.Point(curve.curve, x, y) - vk = ecdsa.VerifyingKey.from_public_point(point, curve=curve, - hashfunc=hashfunc) - parts = [ECDSA_KEY_TYPE, ECDSA_CURVE_NAME, - DER_OCTET_STRING + vk.to_string()] + return ecdsa.VerifyingKey.from_public_point(point, curve=curve, + hashfunc=hashfunc) + +def serialize_verifying_key(vk): + key_type = ECDSA_KEY_PREFIX + ECDSA_CURVE_NAME + curve_name = ECDSA_CURVE_NAME + key_blob = DER_OCTET_STRING + vk.to_string() + parts = [key_type, curve_name, key_blob] return b''.join([util.frame(p) for p in parts]) def export_public_key(pubkey, label): - blob = decompress_pubkey(pubkey) + blob = serialize_verifying_key(decompress_pubkey(pubkey)) log.debug('fingerprint: %s', fingerprint(blob)) b64 = base64.b64encode(blob).decode('ascii') return '{} {} {}\n'.format(ECDSA_KEY_TYPE.decode('ascii'), b64, label)