From 856e518e7b540ad2f8ca42c9a8a013586d26a1a9 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Fri, 20 Dec 2013 18:22:24 -0800 Subject: [PATCH] Add PGPPacket._serialize_generic_public_key_packet This is the inverse of _parse_generic_public_key_packet. See the _parse_generic_public_key_packet commit for references to RFC 4880. --- gpg-migrate.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/gpg-migrate.py b/gpg-migrate.py index 66c19ac..8b0f1a2 100755 --- a/gpg-migrate.py +++ b/gpg-migrate.py @@ -727,6 +727,49 @@ class PGPPacket (dict): return offset return b''.join(chunks) + def _serialize_public_key_packet(self): + return self._serialize_generic_public_key_packet() + + def _serialize_public_subkey_packet(self): + return self._serialize_generic_public_key_packet() + + def _serialize_generic_public_key_packet(self): + key_version = bytes([self['key-version']]) + chunks = [key_version] + if self['key-version'] != 4: + raise NotImplementedError( + 'public (sub)key packet version {}'.format( + self['key-version'])) + chunks.append(_struct.pack('>I', self['creation-time'])) + chunks.append(bytes([self._reverse( + self._public_key_algorithms, self['public-key-algorithm'])])) + if self['public-key-algorithm'].startswith('rsa '): + chunks.append(self._serialize_multiprecision_integer( + self['public-modulus'])) + chunks.append(self._serialize_multiprecision_integer( + self['public-exponent'])) + elif self['public-key-algorithm'].startswith('dsa '): + chunks.append(self._serialize_multiprecision_integer( + self['prime'])) + chunks.append(self._serialize_multiprecision_integer( + self['group-order'])) + chunks.append(self._serialize_multiprecision_integer( + self['group-generator'])) + chunks.append(self._serialize_multiprecision_integer( + self['public-key'])) + elif self['public-key-algorithm'].startswith('elgamal '): + chunks.append(self._serialize_multiprecision_integer( + self['prime'])) + chunks.append(self._serialize_multiprecision_integer( + self['group-generator'])) + chunks.append(self._serialize_multiprecision_integer( + self['public-key'])) + else: + raise NotImplementedError( + 'algorithm-specific key fields for {}'.format( + self['public-key-algorithm'])) + return b''.join(chunks) + def packets_from_bytes(data): offset = 0 -- 2.26.2