trezor-agent/trezor_agent/gpg/__main__.py

53 lines
1.6 KiB
Python
Raw Normal View History

#!/usr/bin/env python
"""Create signatures and export public keys for GPG using TREZOR."""
import argparse
2016-05-21 14:21:16 +00:00
import contextlib
import logging
2016-05-28 20:02:45 +00:00
import os
2016-04-24 11:23:16 +00:00
import sys
import time
2016-05-30 14:49:21 +00:00
from . import encode, keyring, proto
log = logging.getLogger(__name__)
2016-05-06 19:16:03 +00:00
def run_create(args):
"""Generate a new pubkey for a new/existing GPG identity."""
user_id = os.environ['TREZOR_GPG_USER_ID']
2016-05-21 14:25:01 +00:00
f = encode.Factory(user_id=user_id, created=args.time,
2016-05-21 17:00:38 +00:00
curve_name=args.ecdsa_curve)
2016-05-21 14:21:16 +00:00
2016-05-21 14:25:01 +00:00
with contextlib.closing(f):
2016-05-21 14:21:16 +00:00
if args.subkey:
2016-05-27 08:19:10 +00:00
primary_key = keyring.export_public_key(user_id=user_id)
result = f.create_subkey(primary_bytes=primary_key)
2016-05-21 14:21:16 +00:00
else:
2016-05-21 14:25:01 +00:00
result = f.create_primary()
2016-05-06 19:16:03 +00:00
2016-05-21 14:15:42 +00:00
sys.stdout.write(proto.armor(result, 'PUBLIC KEY BLOCK'))
2016-05-06 19:16:03 +00:00
2016-04-24 11:23:16 +00:00
def main():
2016-04-24 09:22:02 +00:00
"""Main function."""
p = argparse.ArgumentParser()
p.add_argument('-v', '--verbose', action='store_true', default=False)
2016-05-06 19:16:03 +00:00
subparsers = p.add_subparsers()
subparsers.required = True
subparsers.dest = 'command'
2016-05-06 19:16:03 +00:00
create = subparsers.add_parser('create')
create.add_argument('-s', '--subkey', action='store_true', default=False)
create.add_argument('-e', '--ecdsa-curve', default='nist256p1')
create.add_argument('-t', '--time', type=int, default=int(time.time()))
create.set_defaults(run=run_create)
args = p.parse_args()
logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO,
format='%(asctime)s %(levelname)-10s %(message)s')
args.run(args)
if __name__ == '__main__':
main()