From e5c3d22ef14d1ef5a6e67c916077edbe4b550574 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Wed, 18 Dec 2013 20:56:42 -0800 Subject: [PATCH] gpg-migrate.py: Convert PGPPacket 'packet-tag' key to 'type' And convert the integer to a string using the table from RFC 4880 [1]: The packet tag denotes what type of packet the body holds. Note that old format headers can only have tags less than 16, whereas new format headers can have tags as great as 63. The defined tags (in decimal) are as follows: 0 -- Reserved - a packet tag MUST NOT have this value 1 -- Public-Key Encrypted Session Key Packet 2 -- Signature Packet 3 -- Symmetric-Key Encrypted Session Key Packet 4 -- One-Pass Signature Packet 5 -- Secret-Key Packet 6 -- Public-Key Packet 7 -- Secret-Subkey Packet 8 -- Compressed Data Packet 9 -- Symmetrically Encrypted Data Packet 10 -- Marker Packet 11 -- Literal Data Packet 12 -- Trust Packet 13 -- User ID Packet 14 -- Public-Subkey Packet 17 -- User Attribute Packet 18 -- Sym. Encrypted and Integrity Protected Data Packet 19 -- Modification Detection Code Packet 60 to 63 -- Private or Experimental Values [1]: http://tools.ietf.org/search/rfc4880#section-4.3 --- gpg-migrate.py | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/gpg-migrate.py b/gpg-migrate.py index 699b560..c41ddd4 100755 --- a/gpg-migrate.py +++ b/gpg-migrate.py @@ -25,6 +25,31 @@ class PGPPacket (dict): 3: (None, None), } + _packet_types = { + 0: 'reserved', + 1: 'public-key encrypted session key packet', + 2: 'signature packet', + 3: 'symmetric-key encrypted session key packet', + 4: 'one-pass signature packet', + 5: 'secret-key packet', + 6: 'public-key packet', + 7: 'secret-subkey packet', + 8: 'compressed data packet', + 9: 'symmetrically encrypted data packet', + 10: 'marker packet', + 11: 'literal data packet', + 12: 'trust packet', + 13: 'user id packet', + 14: 'public-subkey packet', + 17: 'user attribute packet', + 18: 'sym. encrypted and integrity protected data packet', + 19: 'modification detection code packet', + 60: 'private', + 61: 'private', + 62: 'private', + 63: 'private', + } + def from_bytes(self, data): offset = self._parse_header(data=data) packet = data[offset:offset + self['length']] @@ -42,10 +67,10 @@ class PGPPacket (dict): raise ValueError('most significant packet tag bit not set') self['new-format'] = packet_tag & 1 << 6 if self['new-format']: - self['packet-tag'] = packet_tag & 0b111111 + type_code = packet_tag & 0b111111 raise NotImplementedError('new-format packet length') else: - self['packet-tag'] = packet_tag >> 2 & 0b1111 + type_code = packet_tag >> 2 & 0b1111 self['length-type'] = packet_tag & 0b11 length_bytes, length_type = self._old_format_packet_length_type[ self['length-type']] @@ -56,6 +81,7 @@ class PGPPacket (dict): length_data = data[offset: offset + length_bytes] offset += length_bytes self['length'] = _struct.unpack(length_format, length_data)[0] + self['type'] = self._packet_types[type_code] return offset def to_bytes(self): -- 2.26.2