9c8bca68edf3f6ebe028136d38779cb014ed7962
[gpg-migrate.git] / gpg-migrate.py
1 #!/usr/bin/python
2
3 import re as _re
4 import subprocess as _subprocess
5 import struct as _struct
6
7
8 def _get_stdout(args, stdin=None):
9     stdin_pipe = None
10     if stdin is not None:
11         stdin_pipe = _subprocess.PIPE
12     p = _subprocess.Popen(args, stdin=stdin_pipe, stdout=_subprocess.PIPE)
13     stdout, stderr = p.communicate(stdin)
14     status = p.wait()
15     if status != 0:
16         raise RuntimeError(status)
17     return stdout
18
19
20 class PGPPacket (dict):
21     # http://tools.ietf.org/search/rfc4880
22     _old_format_packet_length_type = {  # type: (bytes, struct type)
23         0: (1, 'B'),  # 1-byte unsigned integer
24         1: (2, 'H'),  # 2-byte unsigned integer
25         2: (4, 'I'),  # 4-byte unsigned integer
26         3: (None, None),
27         }
28
29     _packet_types = {
30         0: 'reserved',
31         1: 'public-key encrypted session key packet',
32         2: 'signature packet',
33         3: 'symmetric-key encrypted session key packet',
34         4: 'one-pass signature packet',
35         5: 'secret-key packet',
36         6: 'public-key packet',
37         7: 'secret-subkey packet',
38         8: 'compressed data packet',
39         9: 'symmetrically encrypted data packet',
40         10: 'marker packet',
41         11: 'literal data packet',
42         12: 'trust packet',
43         13: 'user id packet',
44         14: 'public-subkey packet',
45         17: 'user attribute packet',
46         18: 'sym. encrypted and integrity protected data packet',
47         19: 'modification detection code packet',
48         60: 'private',
49         61: 'private',
50         62: 'private',
51         63: 'private',
52         }
53
54     _public_key_algorithms = {
55         1: 'rsa (encrypt or sign)',
56         2: 'rsa encrypt-only',
57         3: 'rsa sign-only',
58         16: 'elgamal (encrypt-only)',
59         17: 'dsa (digital signature algorithm)',
60         18: 'reserved for elliptic curve',
61         19: 'reserved for ecdsa',
62         20: 'reserved (formerly elgamal encrypt or sign)',
63         21: 'reserved for diffie-hellman',
64         100: 'private',
65         101: 'private',
66         102: 'private',
67         103: 'private',
68         104: 'private',
69         105: 'private',
70         106: 'private',
71         107: 'private',
72         108: 'private',
73         109: 'private',
74         110: 'private',
75         }
76
77     _clean_type_regex = _re.compile('\W+')
78
79     def _clean_type(self):
80         return self._clean_type_regex.sub('_', self['type'])
81
82     def from_bytes(self, data):
83         offset = self._parse_header(data=data)
84         packet = data[offset:offset + self['length']]
85         if len(packet) < self['length']:
86             raise ValueError('packet too short ({} < {})'.format(
87                 len(packet), self['length']))
88         offset += self['length']
89         method_name = '_parse_{}'.format(self._clean_type())
90         method = getattr(self, method_name, None)
91         if not method:
92             raise NotImplementedError(
93                 'cannot parse packet type {!r}'.format(self['type']))
94         method(data=packet)
95         return offset
96
97     def _parse_header(self, data):
98         packet_tag = data[0]
99         offset = 1
100         always_one = packet_tag & 1 << 7
101         if not always_one:
102             raise ValueError('most significant packet tag bit not set')
103         self['new-format'] = packet_tag & 1 << 6
104         if self['new-format']:
105             type_code = packet_tag & 0b111111
106             raise NotImplementedError('new-format packet length')
107         else:
108             type_code = packet_tag >> 2 & 0b1111
109             self['length-type'] = packet_tag & 0b11
110             length_bytes, length_type = self._old_format_packet_length_type[
111                 self['length-type']]
112             if not length_bytes:
113                 raise NotImplementedError(
114                     'old-format packet of indeterminate length')
115             length_format = '>{}'.format(length_type)
116             length_data = data[offset: offset + length_bytes]
117             offset += length_bytes
118             self['length'] = _struct.unpack(length_format, length_data)[0]
119         self['type'] = self._packet_types[type_code]
120         return offset
121
122     def _parse_public_key_packet(self, data):
123         self._parse_generic_public_key_packet(data=data)
124
125     def _parse_public_subkey_packet(self, data):
126         self._parse_generic_public_key_packet(data=data)
127
128     def _parse_generic_public_key_packet(self, data):
129         self['key-version'] = data[0]
130         offset = 1
131         if self['key-version'] != 4:
132             raise NotImplementedError(
133                 'public (sub)key packet version {}'.format(
134                     self['key-version']))
135         length = 5
136         self['creation_time'], self['public-key-algorithm'] = _struct.unpack(
137             '>IB', data[offset: offset + length])
138         offset += length
139         self['key'] = data[offset:]
140
141     def to_bytes(self):
142         pass
143
144
145 def packets_from_bytes(data):
146     offset = 0
147     while offset < len(data):
148         packet = PGPPacket()
149         offset += packet.from_bytes(data=data[offset:])
150         yield packet
151
152
153 def migrate(old_key, new_key):
154     """Add the old key and sub-keys to the new key
155
156     For example, to upgrade your master key, while preserving old
157     signatures you'd made.  You will lose signature *on* your old key
158     though, since sub-keys can't be signed (I don't think).
159     """
160     old_key_export = _get_stdout(
161         ['gpg', '--export', old_key])
162     old_key_packets = list(
163         packets_from_bytes(data=old_key_export))
164     if old_key_packets[0]['type'] != 'public-key packet':
165         raise ValueError(
166             '{} does not start with a public-key packet'.format(
167                 old_key))
168     old_key_secret_export = _get_stdout(
169         ['gpg', '--export-secret-keys', old_key])
170     old_key_secret_packets = list(
171         packets_from_bytes(data=old_key_secret_export))
172
173     import pprint
174     pprint.pprint(old_key_packets)
175     pprint.pprint(old_key_secret_packets)
176
177
178 if __name__ == '__main__':
179     import sys as _sys
180
181     old_key, new_key = _sys.argv[1:3]
182     migrate(old_key=old_key, new_key=new_key)