blob: f26592d99547f4f96e1c54a40c2f5672cde276b9 [file] [log] [blame]
Fabio Utzige89841d2018-12-21 11:19:06 -02001#! /usr/bin/env python3
2#
3# Copyright 2017 Linaro Limited
David Vinczeda8c9192019-03-26 17:17:41 +01004# Copyright 2019 Arm Limited
Fabio Utzige89841d2018-12-21 11:19:06 -02005#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17
David Vinczeda8c9192019-03-26 17:17:41 +010018import re
Fabio Utzige89841d2018-12-21 11:19:06 -020019import click
20import getpass
21import imgtool.keys as keys
22from imgtool import image
23from imgtool.version import decode_version
24
25
26def gen_rsa2048(keyfile, passwd):
27 keys.RSA2048.generate().export_private(path=keyfile, passwd=passwd)
28
29
30def gen_ecdsa_p256(keyfile, passwd):
31 keys.ECDSA256P1.generate().export_private(keyfile, passwd=passwd)
32
33
34def gen_ecdsa_p224(keyfile, passwd):
35 print("TODO: p-224 not yet implemented")
36
37
38valid_langs = ['c', 'rust']
39keygens = {
40 'rsa-2048': gen_rsa2048,
41 'ecdsa-p256': gen_ecdsa_p256,
42 'ecdsa-p224': gen_ecdsa_p224,
43}
44
45
46def load_key(keyfile):
47 # TODO: better handling of invalid pass-phrase
48 key = keys.load(keyfile)
49 if key is not None:
50 return key
51 passwd = getpass.getpass("Enter key passphrase: ").encode('utf-8')
52 return keys.load(keyfile, passwd)
53
54
55def get_password():
56 while True:
57 passwd = getpass.getpass("Enter key passphrase: ")
58 passwd2 = getpass.getpass("Reenter passphrase: ")
59 if passwd == passwd2:
60 break
61 print("Passwords do not match, try again")
62
63 # Password must be bytes, always use UTF-8 for consistent
64 # encoding.
65 return passwd.encode('utf-8')
66
67
68@click.option('-p', '--password', is_flag=True,
69 help='Prompt for password to protect key')
70@click.option('-t', '--type', metavar='type', required=True,
71 type=click.Choice(keygens.keys()))
72@click.option('-k', '--key', metavar='filename', required=True)
73@click.command(help='Generate pub/private keypair')
74def keygen(type, key, password):
75 password = get_password() if password else None
76 keygens[type](key, password)
77
78
79@click.option('-l', '--lang', metavar='lang', default=valid_langs[0],
80 type=click.Choice(valid_langs))
81@click.option('-k', '--key', metavar='filename', required=True)
82@click.command(help='Get public key from keypair')
83def getpub(key, lang):
84 key = load_key(key)
85 if key is None:
86 print("Invalid passphrase")
87 elif lang == 'c':
88 key.emit_c()
89 elif lang == 'rust':
90 key.emit_rust()
91 else:
92 raise ValueError("BUG: should never get here!")
93
94
95def validate_version(ctx, param, value):
96 try:
97 decode_version(value)
98 return value
99 except ValueError as e:
100 raise click.BadParameter("{}".format(e))
101
102
103def validate_header_size(ctx, param, value):
104 min_hdr_size = image.IMAGE_HEADER_SIZE
105 if value < min_hdr_size:
106 raise click.BadParameter(
107 "Minimum value for -H/--header-size is {}".format(min_hdr_size))
108 return value
109
110
David Vinczeda8c9192019-03-26 17:17:41 +0100111def get_dependencies(ctx, param, value):
112 if value is not None:
113 versions = []
114 images = re.findall(r"\((\d+)", value)
115 if len(images) == 0:
116 raise click.BadParameter(
117 "Image dependency format is invalid: {}".format(value))
118 raw_versions = re.findall(r",\s*([0-9.+]+)\)", value)
119 if len(images) != len(raw_versions):
120 raise click.BadParameter(
121 '''There's a mismatch between the number of dependency images
122 and versions in: {}'''.format(value))
123 for raw_version in raw_versions:
124 try:
125 versions.append(decode_version(raw_version))
126 except ValueError as e:
127 raise click.BadParameter("{}".format(e))
128 dependencies = dict()
129 dependencies[image.DEP_IMAGES_KEY] = images
130 dependencies[image.DEP_VERSIONS_KEY] = versions
131 return dependencies
132
133
Fabio Utzige89841d2018-12-21 11:19:06 -0200134class BasedIntParamType(click.ParamType):
135 name = 'integer'
136
137 def convert(self, value, param, ctx):
138 try:
139 if value[:2].lower() == '0x':
140 return int(value[2:], 16)
141 elif value[:1] == '0':
142 return int(value, 8)
143 return int(value, 10)
144 except ValueError:
145 self.fail('%s is not a valid integer' % value, param, ctx)
146
147
148@click.argument('outfile')
149@click.argument('infile')
150@click.option('-E', '--encrypt', metavar='filename',
151 help='Encrypt image using the provided public key')
152@click.option('-e', '--endian', type=click.Choice(['little', 'big']),
153 default='little', help="Select little or big endian")
154@click.option('--overwrite-only', default=False, is_flag=True,
155 help='Use overwrite-only instead of swap upgrades')
156@click.option('-M', '--max-sectors', type=int,
157 help='When padding allow for this amount of sectors (defaults to 128)')
158@click.option('--pad', default=False, is_flag=True,
159 help='Pad image to --slot-size bytes, adding trailer magic')
160@click.option('-S', '--slot-size', type=BasedIntParamType(), required=True,
161 help='Size of the slot where the image will be written')
162@click.option('--pad-header', default=False, is_flag=True,
163 help='Add --header-size zeroed bytes at the beginning of the image')
164@click.option('-H', '--header-size', callback=validate_header_size,
165 type=BasedIntParamType(), required=True)
David Vinczeda8c9192019-03-26 17:17:41 +0100166@click.option('-d', '--dependencies', callback=get_dependencies,
167 required=False, help='''Add dependence on another image, format:
168 "(<image_ID>,<image_version>), ... "''')
Fabio Utzige89841d2018-12-21 11:19:06 -0200169@click.option('-v', '--version', callback=validate_version, required=True)
170@click.option('--align', type=click.Choice(['1', '2', '4', '8']),
171 required=True)
172@click.option('-k', '--key', metavar='filename')
Fabio Utzig7c00acd2019-01-07 09:54:20 -0200173@click.command(help='''Create a signed or unsigned image\n
174 INFILE and OUTFILE are parsed as Intel HEX if the params have
175 .hex extension, othewise binary format is used''')
Fabio Utzige89841d2018-12-21 11:19:06 -0200176def sign(key, align, version, header_size, pad_header, slot_size, pad,
David Vinczeda8c9192019-03-26 17:17:41 +0100177 max_sectors, overwrite_only, endian, encrypt, infile, outfile,
178 dependencies):
Fabio Utzig7c00acd2019-01-07 09:54:20 -0200179 img = image.Image(version=decode_version(version), header_size=header_size,
180 pad_header=pad_header, pad=pad, align=int(align),
181 slot_size=slot_size, max_sectors=max_sectors,
182 overwrite_only=overwrite_only, endian=endian)
183 img.load(infile)
Fabio Utzige89841d2018-12-21 11:19:06 -0200184 key = load_key(key) if key else None
185 enckey = load_key(encrypt) if encrypt else None
186 if enckey:
187 if not isinstance(enckey, (keys.RSA2048, keys.RSA2048Public)):
Chris Bittnerfda937a2019-03-29 10:11:31 +0100188 raise Exception("Encryption only available with RSA key")
189 if key and not isinstance(key, keys.RSA2048):
190 raise Exception("Signing only available with private RSA key")
David Vinczeda8c9192019-03-26 17:17:41 +0100191 img.create(key, enckey, dependencies)
Fabio Utzige89841d2018-12-21 11:19:06 -0200192 img.save(outfile)
193
194
195class AliasesGroup(click.Group):
196
197 _aliases = {
198 "create": "sign",
199 }
200
201 def list_commands(self, ctx):
202 cmds = [k for k in self.commands]
203 aliases = [k for k in self._aliases]
204 return sorted(cmds + aliases)
205
206 def get_command(self, ctx, cmd_name):
207 rv = click.Group.get_command(self, ctx, cmd_name)
208 if rv is not None:
209 return rv
210 if cmd_name in self._aliases:
211 return click.Group.get_command(self, ctx, self._aliases[cmd_name])
212 return None
213
214
215@click.command(cls=AliasesGroup,
216 context_settings=dict(help_option_names=['-h', '--help']))
217def imgtool():
218 pass
219
220
221imgtool.add_command(keygen)
222imgtool.add_command(getpub)
223imgtool.add_command(sign)
224
225
226if __name__ == '__main__':
227 imgtool()