2016-04-24 10:04:53 +00:00
|
|
|
"""Create GPG ECDSA signatures and public keys using TREZOR device."""
|
2016-10-18 12:32:29 +00:00
|
|
|
import io
|
2016-04-24 10:04:53 +00:00
|
|
|
import logging
|
|
|
|
|
2016-10-18 12:32:29 +00:00
|
|
|
from . import decode, keyring, protocol
|
2016-10-14 20:10:05 +00:00
|
|
|
from .. import util
|
2016-04-24 10:04:53 +00:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2016-10-14 20:10:05 +00:00
|
|
|
|
2016-12-30 16:55:18 +00:00
|
|
|
def create_primary(user_id, pubkey, signer_func, secret_bytes=b''):
|
2016-06-04 17:54:07 +00:00
|
|
|
"""Export new primary GPG public key, ready for "gpg2 --import"."""
|
2016-12-30 16:55:18 +00:00
|
|
|
pubkey_packet = protocol.packet(tag=(5 if secret_bytes else 6),
|
|
|
|
blob=(pubkey.data() + secret_bytes))
|
2017-06-24 10:45:25 +00:00
|
|
|
user_id_bytes = user_id.encode('utf-8')
|
|
|
|
user_id_packet = protocol.packet(tag=13, blob=user_id_bytes)
|
|
|
|
data_to_sign = (pubkey.data_to_hash() + user_id_packet[:1] +
|
|
|
|
util.prefix_len('>L', user_id_bytes))
|
2016-06-04 17:54:07 +00:00
|
|
|
hashed_subpackets = [
|
2016-07-26 16:37:42 +00:00
|
|
|
protocol.subpacket_time(pubkey.created), # signature time
|
2016-06-11 07:34:59 +00:00
|
|
|
# https://tools.ietf.org/html/rfc4880#section-5.2.3.7
|
2016-07-26 16:37:42 +00:00
|
|
|
protocol.subpacket_byte(0x0B, 9), # preferred symmetric algo (AES-256)
|
2016-06-04 17:54:07 +00:00
|
|
|
# https://tools.ietf.org/html/rfc4880#section-5.2.3.4
|
2016-07-26 16:37:42 +00:00
|
|
|
protocol.subpacket_byte(0x1B, 1 | 2), # key flags (certify & sign)
|
2016-06-04 17:54:07 +00:00
|
|
|
# https://tools.ietf.org/html/rfc4880#section-5.2.3.21
|
2017-10-10 06:05:43 +00:00
|
|
|
protocol.subpacket_bytes(0x15, [8, 9, 10]), # preferred hash
|
2016-06-04 17:54:07 +00:00
|
|
|
# https://tools.ietf.org/html/rfc4880#section-5.2.3.8
|
2017-10-10 06:05:43 +00:00
|
|
|
protocol.subpacket_bytes(0x16, [2, 3, 1]), # preferred compression
|
2016-06-04 17:54:07 +00:00
|
|
|
# https://tools.ietf.org/html/rfc4880#section-5.2.3.9
|
2017-10-10 06:49:31 +00:00
|
|
|
protocol.subpacket_byte(0x17, 0x80), # key server prefs (no-modify)
|
2016-06-04 17:54:07 +00:00
|
|
|
# https://tools.ietf.org/html/rfc4880#section-5.2.3.17
|
2017-10-10 06:49:31 +00:00
|
|
|
protocol.subpacket_byte(0x1E, 0x01), # advanced features (MDC)
|
|
|
|
# https://tools.ietf.org/html/rfc4880#section-5.2.3.24
|
2016-06-04 17:54:07 +00:00
|
|
|
]
|
|
|
|
unhashed_subpackets = [
|
2016-07-26 16:37:42 +00:00
|
|
|
protocol.subpacket(16, pubkey.key_id()), # issuer key id
|
|
|
|
protocol.CUSTOM_SUBPACKET]
|
2016-06-04 17:54:07 +00:00
|
|
|
|
2016-07-26 16:37:42 +00:00
|
|
|
signature = protocol.make_signature(
|
2016-06-04 17:54:07 +00:00
|
|
|
signer_func=signer_func,
|
|
|
|
public_algo=pubkey.algo_id,
|
|
|
|
data_to_sign=data_to_sign,
|
|
|
|
sig_type=0x13, # user id & public key
|
|
|
|
hashed_subpackets=hashed_subpackets,
|
|
|
|
unhashed_subpackets=unhashed_subpackets)
|
|
|
|
|
2016-07-26 16:37:42 +00:00
|
|
|
sign_packet = protocol.packet(tag=2, blob=signature)
|
2016-06-04 17:54:07 +00:00
|
|
|
return pubkey_packet + user_id_packet + sign_packet
|
|
|
|
|
|
|
|
|
2017-01-01 16:14:52 +00:00
|
|
|
def create_subkey(primary_bytes, subkey, signer_func, secret_bytes=b''):
|
2016-06-04 17:54:07 +00:00
|
|
|
"""Export new subkey to GPG primary key."""
|
2017-01-01 16:14:52 +00:00
|
|
|
subkey_packet = protocol.packet(tag=(7 if secret_bytes else 14),
|
|
|
|
blob=(subkey.data() + secret_bytes))
|
2016-10-18 12:32:29 +00:00
|
|
|
packets = list(decode.parse_packets(io.BytesIO(primary_bytes)))
|
|
|
|
primary, user_id, signature = packets[:3]
|
2016-06-04 17:54:07 +00:00
|
|
|
|
2016-10-18 12:32:29 +00:00
|
|
|
data_to_sign = primary['_to_hash'] + subkey.data_to_hash()
|
|
|
|
|
|
|
|
if subkey.ecdh:
|
2016-06-04 17:54:07 +00:00
|
|
|
embedded_sig = None
|
|
|
|
else:
|
|
|
|
# Primary Key Binding Signature
|
|
|
|
hashed_subpackets = [
|
2016-10-18 12:32:29 +00:00
|
|
|
protocol.subpacket_time(subkey.created)] # signature time
|
2016-06-04 17:54:07 +00:00
|
|
|
unhashed_subpackets = [
|
2016-10-18 12:32:29 +00:00
|
|
|
protocol.subpacket(16, subkey.key_id())] # issuer key id
|
2016-07-26 16:37:42 +00:00
|
|
|
embedded_sig = protocol.make_signature(
|
2016-06-04 17:54:07 +00:00
|
|
|
signer_func=signer_func,
|
|
|
|
data_to_sign=data_to_sign,
|
2016-10-18 12:32:29 +00:00
|
|
|
public_algo=subkey.algo_id,
|
2016-06-04 17:54:07 +00:00
|
|
|
sig_type=0x19,
|
|
|
|
hashed_subpackets=hashed_subpackets,
|
|
|
|
unhashed_subpackets=unhashed_subpackets)
|
|
|
|
|
|
|
|
# Subkey Binding Signature
|
|
|
|
|
|
|
|
# Key flags: https://tools.ietf.org/html/rfc4880#section-5.2.3.21
|
2016-06-11 11:38:11 +00:00
|
|
|
# (certify & sign) (encrypt)
|
2016-10-18 12:32:29 +00:00
|
|
|
flags = (2) if (not subkey.ecdh) else (4 | 8)
|
2016-06-04 17:54:07 +00:00
|
|
|
|
|
|
|
hashed_subpackets = [
|
2016-10-18 12:32:29 +00:00
|
|
|
protocol.subpacket_time(subkey.created), # signature time
|
2016-07-26 16:37:42 +00:00
|
|
|
protocol.subpacket_byte(0x1B, flags)]
|
2016-06-04 17:54:07 +00:00
|
|
|
|
|
|
|
unhashed_subpackets = []
|
2016-07-26 16:37:42 +00:00
|
|
|
unhashed_subpackets.append(protocol.subpacket(16, primary['key_id']))
|
2016-06-04 17:54:07 +00:00
|
|
|
if embedded_sig is not None:
|
2016-07-26 16:37:42 +00:00
|
|
|
unhashed_subpackets.append(protocol.subpacket(32, embedded_sig))
|
|
|
|
unhashed_subpackets.append(protocol.CUSTOM_SUBPACKET)
|
2016-06-04 17:54:07 +00:00
|
|
|
|
2016-11-11 10:53:14 +00:00
|
|
|
if not decode.has_custom_subpacket(signature):
|
2016-10-18 12:32:29 +00:00
|
|
|
signer_func = keyring.create_agent_signer(user_id['value'])
|
2016-06-11 10:47:56 +00:00
|
|
|
|
2016-07-26 16:37:42 +00:00
|
|
|
signature = protocol.make_signature(
|
2016-06-11 10:47:56 +00:00
|
|
|
signer_func=signer_func,
|
2016-06-04 17:54:07 +00:00
|
|
|
data_to_sign=data_to_sign,
|
|
|
|
public_algo=primary['algo'],
|
|
|
|
sig_type=0x18,
|
|
|
|
hashed_subpackets=hashed_subpackets,
|
|
|
|
unhashed_subpackets=unhashed_subpackets)
|
2016-07-26 16:37:42 +00:00
|
|
|
sign_packet = protocol.packet(tag=2, blob=signature)
|
2016-06-04 17:54:07 +00:00
|
|
|
return primary_bytes + subkey_packet + sign_packet
|