Preserve public/secret distinction in _serialize_signature_packet_target
[gpg-migrate.git] / gpg-migrate.py
index f5a757c2aa84eb4432ce9ac1233c4d0ba0cbe0de..ad4f0f52b94ac609fc424295dc7e2a265419fbbd 100755 (executable)
@@ -2,6 +2,7 @@
 
 import getpass as _getpass
 import hashlib as _hashlib
+import logging as _logging
 import math as _math
 import re as _re
 import subprocess as _subprocess
@@ -11,6 +12,17 @@ import Crypto.Cipher.AES as _crypto_cipher_aes
 import Crypto.Cipher.Blowfish as _crypto_cipher_blowfish
 import Crypto.Cipher.CAST as _crypto_cipher_cast
 import Crypto.Cipher.DES3 as _crypto_cipher_des3
+import Crypto.PublicKey.DSA as _crypto_publickey_dsa
+import Crypto.PublicKey.ElGamal as _crypto_publickey_elgamal
+import Crypto.PublicKey.RSA as _crypto_publickey_rsa
+import Crypto.Random.random as _crypto_random_random
+import Crypto.Signature.PKCS1_v1_5 as _crypto_signature_pkcs1_v1_5
+import Crypto.Hash.SHA as _crypto_hash_sha
+
+
+LOG = _logging.getLogger('gpg-migrate')
+LOG.addHandler(_logging.StreamHandler())
+LOG.setLevel(_logging.WARNING)
 
 
 def _get_stdout(args, stdin=None):
@@ -136,12 +148,19 @@ class PGPPacket (dict):
         }
 
     _crypto_module = {
+        # symmetric-key encryption
         'aes with 128-bit key': _crypto_cipher_aes,
         'aes with 192-bit key': _crypto_cipher_aes,
         'aes with 256-bit key': _crypto_cipher_aes,
         'blowfish': _crypto_cipher_blowfish,
         'cast5': _crypto_cipher_cast,
         'tripledes': _crypto_cipher_des3,
+        # public-key encryption
+        'dsa (digital signature algorithm)': _crypto_publickey_dsa,
+        'elgamal (encrypt-only)': _crypto_publickey_elgamal,
+        'rsa (encrypt or sign)': _crypto_publickey_rsa,
+        'rsa encrypt-only': _crypto_publickey_rsa,
+        'rsa sign-only': _crypto_publickey_rsa,
         }
 
     _key_size = {  # in bits
@@ -309,6 +328,11 @@ class PGPPacket (dict):
         """
         return [k for k,v in dict.items() if v == value][0]
 
+    def copy(self):
+        packet = PGPPacket(key=self.key)
+        packet.update(self)
+        return packet
+
     def __str__(self):
         method_name = '_str_{}'.format(self._clean_type())
         method = getattr(self, method_name, None)
@@ -566,9 +590,8 @@ class PGPPacket (dict):
                 'algorithm-specific key fields for {}'.format(
                     self['public-key-algorithm']))
         fingerprint = _hashlib.sha1()
-        fingerprint.update(b'\x99')
-        fingerprint.update(_struct.pack('>H', len(data)))
-        fingerprint.update(data)
+        fingerprint.update(
+            self._serialize_signature_packet_target(target=self))
         self['fingerprint'] = fingerprint.hexdigest()
         return offset
 
@@ -724,17 +747,31 @@ class PGPPacket (dict):
         offset += unhashed_count
         self['signed-hash-word'] = data[offset: offset + 2]
         offset += 2
-        self['signature'] = data[offset:]
+        self['signature'] = []
+        while offset < len(data):
+            o, mpi = self._parse_multiprecision_integer(data=data[offset:])
+            offset += o
+            self['signature'].append(mpi)
+        if self.key.secret_packets:
+            packets = self.key.secret_packets
+        else:
+            packets = self.key.public_packets
         if self['signature-type'] == 'standalone':
             self['target'] = None
         elif self['signature-type'].endswith(' user id and public-key packet'):
             self['target'] = [
-                [p for p in self.key.public_packets if p['type'] == 'public-key packet'][-1],
-                [p for p in self.key.public_packets if p['type'] == 'user id packet'][-1],
+                packets[0],
+                [p for p in packets if p['type'] == 'user id packet'][-1],
+                ]
+        elif self['signature-type'].endswith('key binding'):
+            self['target'] = [
+                packets[0],
+                [p for p in packets if p['type'] == 'public-subkey packet'][-1],
                 ]
         else:
             raise NotImplementedError(
                 'target for {}'.format(self['signature-type']))
+        self.verify()
 
     def _parse_signature_creation_time_signature_subpacket(
             self, data, subpacket):
@@ -795,18 +832,18 @@ class PGPPacket (dict):
 
     def _parse_embedded_signature_signature_subpacket(self, data, subpacket):
         subpacket['embedded'] = PGPPacket(key=self.key)
+        subpacket['embedded']['type'] = 'signature packet'
+        subpacket['embedded']['embedded'] = True
         subpacket['embedded']._parse_signature_packet(data=data)
+        subpacket['embedded']['raw'] = data
 
     def _parse_user_id_packet(self, data):
         self['user'] = str(data, 'utf-8')
 
     def to_bytes(self):
-        method_name = '_serialize_{}'.format(self._clean_type())
-        method = getattr(self, method_name, None)
-        if not method:
-            raise NotImplementedError(
-                'cannot serialize packet type {!r}'.format(self['type']))
-        body = method()
+        body = self._serialize_body()
+        if body is None:
+            raise ValueError(method)
         self['length'] = len(body)
         return b''.join([
             self._serialize_header(),
@@ -832,6 +869,14 @@ class PGPPacket (dict):
             length_data,
             ])
 
+    def _serialize_body(self):
+        method_name = '_serialize_{}'.format(self._clean_type())
+        method = getattr(self, method_name, None)
+        if not method:
+            raise NotImplementedError(
+                'cannot serialize packet type {!r}'.format(self['type']))
+        return method()
+
     @staticmethod
     def _serialize_multiprecision_integer(integer):
         r"""Serialize RFC 4880's multipricision integers
@@ -933,6 +978,182 @@ class PGPPacket (dict):
                     self['public-key-algorithm']))
         return b''.join(chunks)
 
+    def _serialize_signature_subpackets(self, subpackets):
+        return b''.join(
+            self._serialize_signature_subpacket(subpacket=subpacket)
+            for subpacket in subpackets)
+
+    def _serialize_signature_subpacket(self, subpacket):
+        method_name = '_serialize_{}_signature_subpacket'.format(
+            self._clean_type(type=subpacket['type']))
+        method = getattr(self, method_name, None)
+        if not method:
+            raise NotImplementedError(
+                'cannot serialize signature subpacket type {!r}'.format(
+                    subpacket['type']))
+        body = method(subpacket=subpacket)
+        length = len(body) + 1
+        chunks = []
+        if length < 192:
+            chunks.append(bytes([length]))
+        else:
+            first = ((length - 192) >> 8) + 192
+            if first < 255:
+                chunks.extend([
+                    first,
+                    (length - 192) % 256,
+                    ])
+            else:
+                chunks.append(_struct.pack('>I', length))
+        chunks.append(bytes([self._reverse(
+            self._signature_subpacket_types, subpacket['type'])]))
+        chunks.append(body)
+        return b''.join(chunks)
+
+    def _serialize_signature_packet_target(self, target):
+        if target is None:
+            return b''
+        elif isinstance(target, bytes):
+            return target
+        elif isinstance(target, PGPPacket):
+            if target['type'].endswith('-subkey packet'):
+                target = target.copy()
+                target['type'] = target['type'].replace(
+                    '-subkey packet', '-key packet')
+            serialized = target._serialize_body()
+            if target['type'] in [
+                    'public-key packet',
+                    'public-subkey packet'
+                    'secret-key packet',
+                    'secret-subkey packet'
+                    ]:
+                serialized = b''.join([
+                    b'\x99',
+                    _struct.pack('>H', len(serialized)),
+                    serialized,
+                    ])
+            elif target['type'] == 'user id packet':
+                serialized = b''.join([
+                    b'\xb4',
+                    _struct.pack('>I', len(serialized)),
+                    serialized,
+                    ])
+            elif target['type'] == 'user attribute packet':
+                serialized = b''.join([
+                    b'\xd1',
+                    _struct.pack('>I', len(serialized)),
+                    serialized,
+                    ])
+            return serialized
+        elif isinstance(target, list):
+            return b''.join(
+                self._serialize_signature_packet_target(target=x)
+                for x in target)
+
+    def _serialize_hashed_signature_packet(self):
+        if self['signature-version'] != 4:
+            raise NotImplementedError(
+                'signature packet version {}'.format(
+                    self['signature-version']))
+        chunks = [bytes([self['signature-version']])]
+        chunks.append(bytes([self._reverse(
+            self._signature_types, self['signature-type'])]))
+        chunks.append(bytes([self._reverse(
+            self._public_key_algorithms, self['public-key-algorithm'])]))
+        chunks.append(bytes([self._reverse(
+            self._hash_algorithms, self['hash-algorithm'])]))
+        hashed_subpackets = self._serialize_signature_subpackets(
+            self['hashed-subpackets'])
+        chunks.append(_struct.pack('>H', len(hashed_subpackets)))
+        chunks.append(hashed_subpackets)
+        return b''.join(chunks)
+
+    def _signature_packet_signed_data(self, hashed_signature_data):
+        target = self._serialize_signature_packet_target(target=self['target'])
+        return b''.join([
+            target,
+            hashed_signature_data,
+            bytes([self['signature-version']]),
+            b'\xff',
+            _struct.pack('>I', len(hashed_signature_data)),
+            ])
+
+    def _serialize_signature_packet(self):
+        hashed_signature_data = self._serialize_hashed_signature_packet()
+        chunks = [hashed_signature_data]
+        unhashed_subpackets = self._serialize_signature_subpackets(
+            self['unhashed-subpackets'])
+        chunks.append(_struct.pack('>H', len(unhashed_subpackets)))
+        chunks.append(unhashed_subpackets)
+        signed_data = self._signature_packet_signed_data(
+            hashed_signature_data=hashed_signature_data)
+        digest, signature = self.key.sign(
+            data=signed_data, hash_algorithm=self['hash-algorithm'],
+            signature_algorithm=self['public-key-algorithm'])
+        chunks.append(digest[:2])
+        chunks.extend(
+            self._serialize_multiprecision_integer(integer=integer)
+            for integer in signature)
+        return b''.join(chunks)
+
+    def _serialize_signature_creation_time_signature_subpacket(
+            self, subpacket):
+        return _struct.pack('>I', subpacket['signature-creation-time'])
+
+    def _serialize_issuer_signature_subpacket(self, subpacket):
+        return string_bytes(data=subpacket['issuer'], sep='')
+
+    def _serialize_key_expiration_time_signature_subpacket(self, subpacket):
+        return _struct.pack('>I', subpacket['key-expiration-time'])
+
+    def _serialize_preferred_symmetric_algorithms_signature_subpacket(
+            self, subpacket):
+        return bytes(
+            self._reverse(self._symmetric_key_algorithms, a)
+            for a in subpacket['preferred-symmetric-algorithms'])
+
+    def _serialize_preferred_hash_algorithms_signature_subpacket(
+            self, subpacket):
+        return bytes(
+            self._reverse(self._hash_algorithms, a)
+            for a in subpacket['preferred-hash-algorithms'])
+
+    def _serialize_preferred_compression_algorithms_signature_subpacket(
+            self, subpacket):
+        return bytes(
+            self._reverse(self._compression_algorithms, a)
+            for a in subpacket['preferred-compression-algorithms'])
+
+    def _serialize_key_server_preferences_signature_subpacket(self, subpacket):
+        return bytes([
+            0x80 * ('no-modify' in subpacket['key-server-preferences']) |
+            0,
+            ])
+
+    def _serialize_primary_user_id_signature_subpacket(self, subpacket):
+        return bytes([0x1 * subpacket['primary-user-id']])
+
+    def _serialize_key_flags_signature_subpacket(self, subpacket):
+        return bytes([
+            0x1 * ('can certify' in subpacket['key-flags']) |
+            0x2 * ('can sign' in subpacket['key-flags']) |
+            0x4 * ('can encrypt communications' in subpacket['key-flags']) |
+            0x8 * ('can encrypt storage' in subpacket['key-flags']) |
+            0x10 * ('private split' in subpacket['key-flags']) |
+            0x20 * ('can authenticate' in subpacket['key-flags']) |
+            0x80 * ('private shated' in subpacket['key-flags']) |
+            0,
+            ])
+
+    def _serialize_features_signature_subpacket(self, subpacket):
+        return bytes([
+            0x1 * ('modification detection' in subpacket['features']) |
+            0,
+            ])
+
+    def _serialize_embedded_signature_signature_subpacket(self, subpacket):
+        return subpacket['embedded'].to_bytes()
+
     def _serialize_user_id_packet(self):
         return self['user'].encode('utf-8')
 
@@ -1032,6 +1253,40 @@ class PGPPacket (dict):
                             self['type'], i, byte_string(data=out_chunk),
                             byte_string(data=in_chunk)))
 
+    def verify(self):
+        if self['type'] != 'signature packet':
+            raise NotImplmentedError('verify {}'.format(self['type']))
+        hashed_signature_data = self._serialize_hashed_signature_packet()
+        signed_data = self._signature_packet_signed_data(
+            hashed_signature_data=hashed_signature_data)
+        key_packet = None
+        subpackets = self['hashed-subpackets'] + self['unhashed-subpackets']
+        issuer_subpackets = [p for p in subpackets if p['type'] == 'issuer']
+        if issuer_subpackets:
+            issuer = issuer_subpackets[0]
+            packets = (self.key.public_packets or []) + (
+                self.key.secret_packets or [])
+            keys = [k for k in packets
+                    if k.get('fingerprint', '').endswith(issuer['issuer'])]
+            if keys:
+                key_packet = keys[-1]
+            else:
+                LOG.info('no packet found for issuer {}'.format(
+                    issuer['issuer'][-8:].upper()))
+                return
+        LOG.debug('verify {} with {}'.format(
+            self['signature-type'],
+            key_packet['fingerprint'][-8:].upper()))
+        verified = self.key.verify(
+            data=signed_data, signature=self['signature'],
+            hash_algorithm=self['hash-algorithm'],
+            signature_algorithm=self['public-key-algorithm'],
+            key_packet=key_packet, digest_check=self['signed-hash-word'])
+        if not verified:
+            raise ValueError('verification failed for {}'.format(self))
+        else:
+            LOG.debug('verified {}'.format(self['signature-type']))
+
 
 class PGPKey (object):
     """An OpenPGP key with public and private parts.
@@ -1095,16 +1350,16 @@ class PGPKey (object):
     def import_from_gpg(self):
         key_export = _get_stdout(
             ['gpg', '--export', self.fingerprint])
-        self.public_packets = list(
-            self._packets_from_bytes(data=key_export))
+        self.public_packets = []
+        self._packets_from_bytes(list=self.public_packets, data=key_export)
         if self.public_packets[0]['type'] != 'public-key packet':
             raise ValueError(
                 '{} does not start with a public-key packet'.format(
                     self.fingerprint))
         key_secret_export = _get_stdout(
             ['gpg', '--export-secret-keys', self.fingerprint])
-        self.secret_packets = list(
-            self._packets_from_bytes(data=key_secret_export))
+        self.secret_packets = []
+        self._packets_from_bytes(list=self.secret_packets, data=key_secret_export)
         if self.secret_packets[0]['type'] != 'secret-key packet':
             raise ValueError(
                 '{} does not start with a secret-key packet'.format(
@@ -1112,12 +1367,12 @@ class PGPKey (object):
         for packet in self.public_packets + self.secret_packets:
             packet.check_roundtrip()
 
-    def _packets_from_bytes(self, data):
+    def _packets_from_bytes(self, list, data):
         offset = 0
         while offset < len(data):
             packet = PGPPacket(key=self)
             offset += packet.from_bytes(data=data[offset:])
-            yield packet
+            list.append(packet)
 
     def export_to_gpg(self):
         raise NotImplemetedError('export to gpg')
@@ -1126,6 +1381,78 @@ class PGPKey (object):
         """Migrate the (sub)keys into this key"""
         pass
 
+    def _get_signer(self, signature_algorithm=None, key_packet=None,
+                    secret=False):
+        if key_packet is None:
+            if secret:
+                key_packet = self.secret_packets[0]
+            else:
+                key_packet = self.public_packets[0]
+        elif secret:
+            if 'secret' not in key_packet['type']:
+                raise ValueError(
+                    '{} is not a secret key'.format(key_packet['type']))
+        if signature_algorithm is None:
+            signature_algorithm = key_packet['public-key-algorithm']
+        if signature_algorithm != key_packet['public-key-algorithm']:
+            raise ValueError(
+                'cannot act on a {} signature with a {} key'.format(
+                    signature_algorithm, key_packet['public-key-algorithm']))
+        module = key_packet._crypto_module[signature_algorithm]
+        if signature_algorithm.startswith('rsa '):
+            key = module.construct((
+                key_packet['public-modulus'],   # n
+                key_packet['public-exponent'],  # e
+                ))
+            if secret:
+                LOG.debug('secret')
+                key.d = key_packet['secret-exponent']
+                key.p = key_packet['secret-prime-p']
+                key.q = key_packet['secret-prime-q']
+                key.u = key_packet['secret-inverse-of-p-mod-q']
+            signer = _crypto_signature_pkcs1_v1_5.new(key)
+        elif signature_algorithm.startswith('dsa '):
+            signer = module.construct((
+                key_packet['public-key'],       # y
+                key_packet['group-generator'],  # g
+                key_packet['prime'],            # p
+                key_packet['group-order'],      # q
+                ))
+            if secret:
+                signer.x = key_packet['secret-exponent']
+        else:
+            raise NotImplementedError(
+                'construct {}'.format(signature_algorithm))
+        return (key_packet, signer)
+
+    def _hash(self, data, hash_algorithm, key_packet):
+        hash_name = key_packet._hashlib_name[hash_algorithm]
+        data_hash = _hashlib.new(hash_name)
+        data_hash.update(data)
+        return data_hash
+
+    def verify(self, data, signature, hash_algorithm, signature_algorithm=None,
+               key_packet=None, digest_check=None):
+        key_packet, signer = self._get_signer(
+            signature_algorithm=signature_algorithm, key_packet=key_packet)
+        data_hash = self._hash(
+            data=data, hash_algorithm=hash_algorithm, key_packet=key_packet)
+        digest = data_hash.digest()
+        hexdigest = data_hash.hexdigest()
+        if digest_check and not digest.startswith(digest_check):
+            raise ValueError(
+                'corrupted hash: {} does not start with {}'.format(
+                    byte_string(digest),
+                    byte_string(digest_check)))
+        if signature_algorithm.startswith('rsa '):
+            sig_hex = '{:x}'.format(signature[0])
+            signature = string_bytes(data=sig_hex, sep='')
+        elif signature_algorithm.startswith('dsa '):
+            data_hash = digest
+        LOG.debug('verify signature {} on {} with {}'.format(
+            signature, hexdigest, signer))
+        return signer.verify(data_hash, signature)
+
 
 def migrate(old_key, new_key, cache_passphrase=False):
     """Add the old key and sub-keys to the new key