From 016e8645034c7dadf9b8cf0b44b6c7389c0ed884 Mon Sep 17 00:00:00 2001 From: Nicolas Pouillard Date: Mon, 5 Sep 2016 23:47:01 +0200 Subject: [PATCH] Attempt at fixing issue #32 --- trezor_agent/gpg/protocol.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/trezor_agent/gpg/protocol.py b/trezor_agent/gpg/protocol.py index e0f0709..4785bbc 100644 --- a/trezor_agent/gpg/protocol.py +++ b/trezor_agent/gpg/protocol.py @@ -47,9 +47,22 @@ def subpacket_byte(subpacket_type, value): return subpacket(subpacket_type, '>B', value) +def subpacket_prefix_len(blob): + """Prefix GPG subpacket with the encoding of it's length.""" + if len(blob) < 2**8: + length_type = 0 + elif len(blob) < 2**16: + length_type = 1 + else: + length_type = 2 + fmt = ['>B', '>H', '>L'][length_type] + prefix = [b'', b'', struct.pack('>B', 255)][length_type] + return prefix + struct.pack(fmt, len(blob)) + blob + + def subpackets(*items): """Serialize several GPG subpackets.""" - prefixed = [util.prefix_len('>B', item) for item in items] + prefixed = [subpacket_prefix_len(item) for item in items] return util.prefix_len('>H', b''.join(prefixed))