|
|
|
@ -1,5 +1,6 @@
|
|
|
|
|
"""Decoders for GPG v2 data structures."""
|
|
|
|
|
import base64
|
|
|
|
|
import copy
|
|
|
|
|
import functools
|
|
|
|
|
import hashlib
|
|
|
|
|
import io
|
|
|
|
@ -272,6 +273,30 @@ def digest_packets(packets, hasher):
|
|
|
|
|
return hasher.digest()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def collect_packets(packets, types_to_collect):
|
|
|
|
|
"""Collect specified packet types into their leading packet."""
|
|
|
|
|
packet = None
|
|
|
|
|
result = []
|
|
|
|
|
for p in packets:
|
|
|
|
|
if p['type'] in types_to_collect:
|
|
|
|
|
packet.setdefault(p['type'], []).append(p)
|
|
|
|
|
else:
|
|
|
|
|
packet = copy.deepcopy(p)
|
|
|
|
|
result.append(packet)
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def parse_public_key(stream):
|
|
|
|
|
"""Parse GPG public key into hierarchy of packets."""
|
|
|
|
|
packets = list(parse_packets(stream))
|
|
|
|
|
packets = collect_packets(packets, {'signature'})
|
|
|
|
|
packets = collect_packets(packets, {'user_id', 'user_attribute'})
|
|
|
|
|
packets = collect_packets(packets, {'subkey'})
|
|
|
|
|
if len(packets) != 1:
|
|
|
|
|
raise ValueError('Unexpected packet structure: %r', packets)
|
|
|
|
|
return packets[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HASH_ALGORITHMS = {
|
|
|
|
|
1: 'md5',
|
|
|
|
|
2: 'sha1',
|
|
|
|
|