2017-05-17 01:38:17 +08:00
|
|
|
#! /usr/bin/env python3
|
2017-12-21 02:10:55 +08:00
|
|
|
#
|
|
|
|
# Copyright 2017 Linaro Limited
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2017-05-17 01:38:17 +08:00
|
|
|
|
|
|
|
import argparse
|
2017-11-17 06:11:10 +08:00
|
|
|
import getpass
|
2017-05-17 01:38:17 +08:00
|
|
|
from imgtool import keys
|
|
|
|
from imgtool import image
|
|
|
|
from imgtool import version
|
|
|
|
import sys
|
|
|
|
|
2017-12-07 01:53:35 +08:00
|
|
|
def get_password(args):
|
2017-11-17 06:11:10 +08:00
|
|
|
if args.password:
|
|
|
|
while True:
|
|
|
|
passwd = getpass.getpass("Enter key passphrase: ")
|
|
|
|
passwd2 = getpass.getpass("Reenter passphrase: ")
|
|
|
|
if passwd == passwd2:
|
|
|
|
break
|
|
|
|
print("Passwords do not match, try again")
|
|
|
|
|
|
|
|
# Password must be bytes, always use UTF-8 for consistent
|
|
|
|
# encoding.
|
|
|
|
return passwd.encode('utf-8')
|
|
|
|
else:
|
2017-12-07 01:53:35 +08:00
|
|
|
return None
|
|
|
|
|
|
|
|
def gen_rsa2048(args):
|
|
|
|
passwd = get_password(args)
|
2017-11-17 06:11:10 +08:00
|
|
|
keys.RSA2048.generate().export_private(path=args.key, passwd=passwd)
|
|
|
|
|
2017-05-17 01:38:17 +08:00
|
|
|
def gen_ecdsa_p256(args):
|
2017-12-07 01:53:35 +08:00
|
|
|
passwd = get_password(args)
|
|
|
|
keys.ECDSA256P1.generate().export_private(args.key, passwd=passwd)
|
|
|
|
|
2017-05-17 01:38:17 +08:00
|
|
|
def gen_ecdsa_p224(args):
|
|
|
|
print("TODO: p-224 not yet implemented")
|
|
|
|
|
|
|
|
keygens = {
|
|
|
|
'rsa-2048': gen_rsa2048,
|
|
|
|
'ecdsa-p256': gen_ecdsa_p256,
|
|
|
|
'ecdsa-p224': gen_ecdsa_p224, }
|
|
|
|
|
|
|
|
def do_keygen(args):
|
|
|
|
if args.type not in keygens:
|
|
|
|
msg = "Unexpected key type: {}".format(args.type)
|
|
|
|
raise argparse.ArgumentTypeError(msg)
|
|
|
|
keygens[args.type](args)
|
|
|
|
|
2017-11-17 06:11:10 +08:00
|
|
|
def load_key(args):
|
2017-05-17 01:38:17 +08:00
|
|
|
key = keys.load(args.key)
|
2017-11-17 06:11:10 +08:00
|
|
|
if key is not None:
|
|
|
|
return key
|
|
|
|
passwd = getpass.getpass("Enter key passphrase: ")
|
|
|
|
passwd = passwd.encode('utf-8')
|
|
|
|
return keys.load(args.key, passwd)
|
|
|
|
|
|
|
|
def do_getpub(args):
|
|
|
|
key = load_key(args)
|
2017-09-01 23:33:00 +08:00
|
|
|
if args.lang == 'c':
|
|
|
|
key.emit_c()
|
|
|
|
elif args.lang == 'rust':
|
|
|
|
key.emit_rust()
|
|
|
|
else:
|
|
|
|
msg = "Unsupported language, valid are: c, or rust"
|
|
|
|
raise argparse.ArgumentTypeError(msg)
|
2017-05-17 01:38:17 +08:00
|
|
|
|
|
|
|
def do_sign(args):
|
|
|
|
img = image.Image.load(args.infile, version=args.version,
|
|
|
|
header_size=args.header_size,
|
2017-06-09 00:03:42 +08:00
|
|
|
included_header=args.included_header,
|
2017-05-17 01:38:17 +08:00
|
|
|
pad=args.pad)
|
2017-11-17 06:11:10 +08:00
|
|
|
key = load_key(args) if args.key else None
|
2017-05-17 01:38:17 +08:00
|
|
|
img.sign(key)
|
|
|
|
|
|
|
|
if args.pad:
|
|
|
|
img.pad_to(args.pad, args.align)
|
|
|
|
|
|
|
|
img.save(args.outfile)
|
|
|
|
|
|
|
|
subcmds = {
|
|
|
|
'keygen': do_keygen,
|
|
|
|
'getpub': do_getpub,
|
|
|
|
'sign': do_sign, }
|
|
|
|
|
|
|
|
def alignment_value(text):
|
|
|
|
value = int(text)
|
|
|
|
if value not in [1, 2, 4, 8]:
|
|
|
|
msg = "{} must be one of 1, 2, 4 or 8".format(value)
|
|
|
|
raise argparse.ArgumentTypeError(msg)
|
|
|
|
return value
|
|
|
|
|
|
|
|
def intparse(text):
|
|
|
|
"""Parse a command line argument as an integer.
|
|
|
|
|
|
|
|
Accepts 0x and other prefixes to allow other bases to be used."""
|
|
|
|
return int(text, 0)
|
|
|
|
|
|
|
|
def args():
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
subs = parser.add_subparsers(help='subcommand help', dest='subcmd')
|
|
|
|
|
|
|
|
keygenp = subs.add_parser('keygen', help='Generate pub/private keypair')
|
|
|
|
keygenp.add_argument('-k', '--key', metavar='filename', required=True)
|
|
|
|
keygenp.add_argument('-t', '--type', metavar='type',
|
2017-10-16 17:14:26 +08:00
|
|
|
choices=keygens.keys(), required=True)
|
2017-11-17 06:11:10 +08:00
|
|
|
keygenp.add_argument('-p', '--password', default=False, action='store_true',
|
|
|
|
help='Prompt for password to protect key')
|
2017-05-17 01:38:17 +08:00
|
|
|
|
|
|
|
getpub = subs.add_parser('getpub', help='Get public key from keypair')
|
|
|
|
getpub.add_argument('-k', '--key', metavar='filename', required=True)
|
2017-09-01 23:33:00 +08:00
|
|
|
getpub.add_argument('-l', '--lang', metavar='lang', default='c')
|
2017-05-17 01:38:17 +08:00
|
|
|
|
|
|
|
sign = subs.add_parser('sign', help='Sign an image with a private key')
|
2017-06-08 23:26:24 +08:00
|
|
|
sign.add_argument('-k', '--key', metavar='filename')
|
2017-05-17 01:38:17 +08:00
|
|
|
sign.add_argument("--align", type=alignment_value, required=True)
|
|
|
|
sign.add_argument("-v", "--version", type=version.decode_version, required=True)
|
|
|
|
sign.add_argument("-H", "--header-size", type=intparse, required=True)
|
2017-06-09 00:03:42 +08:00
|
|
|
sign.add_argument("--included-header", default=False, action='store_true',
|
|
|
|
help='Image has gap for header')
|
2017-05-17 01:38:17 +08:00
|
|
|
sign.add_argument("--pad", type=intparse,
|
|
|
|
help='Pad image to this many bytes, adding trailer magic')
|
|
|
|
sign.add_argument("infile")
|
|
|
|
sign.add_argument("outfile")
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
if args.subcmd is None:
|
|
|
|
print('Must specify a subcommand', file=sys.stderr)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
subcmds[args.subcmd](args)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
args()
|