Add PGPPacket._serialize_generic_public_key_packet
authorW. Trevor King <wking@tremily.us>
Sat, 21 Dec 2013 02:22:24 +0000 (18:22 -0800)
committerW. Trevor King <wking@tremily.us>
Mon, 23 Dec 2013 02:32:15 +0000 (18:32 -0800)
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

index 66c19acafd6076814056f33780ea6e3a9088c107..8b0f1a22023388245564ab78886adb73d581a0bf 100755 (executable)
@@ -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