blob: 47d5811825c94cf851655f547b82b86e5ff31577 [file] [log] [blame]
Fabio Utzige89841d2018-12-21 11:19:06 -02001#! /usr/bin/env python3
2#
David Vincze71b8f982020-03-17 19:08:12 +01003# Copyright 2017-2020 Linaro Limited
David Vincze1a7a6902020-02-18 15:05:16 +01004# Copyright 2019-2020 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
Fabio Utzig4a5477a2019-05-27 15:45:08 -030022import sys
Fabio Utzig25c6a152019-09-10 12:52:26 -030023from imgtool import image, imgtool_version
Fabio Utzige89841d2018-12-21 11:19:06 -020024from imgtool.version import decode_version
Fabio Utzig1f508922020-01-15 11:37:51 -030025from .keys import RSAUsageError, ECDSAUsageError, Ed25519UsageError
Fabio Utzige89841d2018-12-21 11:19:06 -020026
David Vincze71b8f982020-03-17 19:08:12 +010027MIN_PYTHON_VERSION = (3, 6)
28if sys.version_info < MIN_PYTHON_VERSION:
29 sys.exit("Python %s.%s or newer is required by imgtool."
30 % MIN_PYTHON_VERSION)
31
Fabio Utzige89841d2018-12-21 11:19:06 -020032
33def gen_rsa2048(keyfile, passwd):
Fabio Utzig19fd79a2019-05-08 18:20:39 -030034 keys.RSA.generate().export_private(path=keyfile, passwd=passwd)
35
36
37def gen_rsa3072(keyfile, passwd):
38 keys.RSA.generate(key_size=3072).export_private(path=keyfile,
39 passwd=passwd)
Fabio Utzige89841d2018-12-21 11:19:06 -020040
41
42def gen_ecdsa_p256(keyfile, passwd):
43 keys.ECDSA256P1.generate().export_private(keyfile, passwd=passwd)
44
45
46def gen_ecdsa_p224(keyfile, passwd):
47 print("TODO: p-224 not yet implemented")
48
49
Fabio Utzig8101d1f2019-05-09 15:03:22 -030050def gen_ed25519(keyfile, passwd):
Fabio Utzig4bd4c7c2019-06-27 08:23:21 -030051 keys.Ed25519.generate().export_private(path=keyfile, passwd=passwd)
Fabio Utzig8101d1f2019-05-09 15:03:22 -030052
53
Fabio Utzige89841d2018-12-21 11:19:06 -020054valid_langs = ['c', 'rust']
55keygens = {
56 'rsa-2048': gen_rsa2048,
Fabio Utzig19fd79a2019-05-08 18:20:39 -030057 'rsa-3072': gen_rsa3072,
Fabio Utzige89841d2018-12-21 11:19:06 -020058 'ecdsa-p256': gen_ecdsa_p256,
59 'ecdsa-p224': gen_ecdsa_p224,
Fabio Utzig8101d1f2019-05-09 15:03:22 -030060 'ed25519': gen_ed25519,
Fabio Utzige89841d2018-12-21 11:19:06 -020061}
62
63
64def load_key(keyfile):
65 # TODO: better handling of invalid pass-phrase
66 key = keys.load(keyfile)
67 if key is not None:
68 return key
69 passwd = getpass.getpass("Enter key passphrase: ").encode('utf-8')
70 return keys.load(keyfile, passwd)
71
72
73def get_password():
74 while True:
75 passwd = getpass.getpass("Enter key passphrase: ")
76 passwd2 = getpass.getpass("Reenter passphrase: ")
77 if passwd == passwd2:
78 break
79 print("Passwords do not match, try again")
80
81 # Password must be bytes, always use UTF-8 for consistent
82 # encoding.
83 return passwd.encode('utf-8')
84
85
86@click.option('-p', '--password', is_flag=True,
87 help='Prompt for password to protect key')
88@click.option('-t', '--type', metavar='type', required=True,
Fabio Utzig7ca28552019-12-13 11:24:20 -030089 type=click.Choice(keygens.keys()), prompt=True,
90 help='{}'.format('One of: {}'.format(', '.join(keygens.keys()))))
Fabio Utzige89841d2018-12-21 11:19:06 -020091@click.option('-k', '--key', metavar='filename', required=True)
92@click.command(help='Generate pub/private keypair')
93def keygen(type, key, password):
94 password = get_password() if password else None
95 keygens[type](key, password)
96
97
98@click.option('-l', '--lang', metavar='lang', default=valid_langs[0],
99 type=click.Choice(valid_langs))
100@click.option('-k', '--key', metavar='filename', required=True)
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +0200101@click.command(help='Dump public key from keypair')
Fabio Utzige89841d2018-12-21 11:19:06 -0200102def getpub(key, lang):
103 key = load_key(key)
104 if key is None:
105 print("Invalid passphrase")
106 elif lang == 'c':
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +0200107 key.emit_c_public()
Fabio Utzige89841d2018-12-21 11:19:06 -0200108 elif lang == 'rust':
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +0200109 key.emit_rust_public()
Fabio Utzige89841d2018-12-21 11:19:06 -0200110 else:
111 raise ValueError("BUG: should never get here!")
112
113
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +0200114@click.option('--minimal', default=False, is_flag=True,
115 help='Reduce the size of the dumped private key to include only '
116 'the minimum amount of data required to decrypt. This '
117 'might require changes to the build config. Check the docs!'
118 )
119@click.option('-k', '--key', metavar='filename', required=True)
120@click.command(help='Dump private key from keypair')
121def getpriv(key, minimal):
122 key = load_key(key)
123 if key is None:
124 print("Invalid passphrase")
Fabio Utzig1f508922020-01-15 11:37:51 -0300125 try:
126 key.emit_private(minimal)
127 except (RSAUsageError, ECDSAUsageError, Ed25519UsageError) as e:
128 raise click.UsageError(e)
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +0200129
130
Fabio Utzig4a5477a2019-05-27 15:45:08 -0300131@click.argument('imgfile')
132@click.option('-k', '--key', metavar='filename')
133@click.command(help="Check that signed image can be verified by given key")
134def verify(key, imgfile):
135 key = load_key(key) if key else None
Marek Pietae9555102019-08-08 16:08:16 +0200136 ret, version = image.Image.verify(imgfile, key)
Fabio Utzig4a5477a2019-05-27 15:45:08 -0300137 if ret == image.VerifyResult.OK:
138 print("Image was correctly validated")
Marek Pietae9555102019-08-08 16:08:16 +0200139 print("Image version: {}.{}.{}+{}".format(*version))
Fabio Utzig4a5477a2019-05-27 15:45:08 -0300140 return
141 elif ret == image.VerifyResult.INVALID_MAGIC:
142 print("Invalid image magic; is this an MCUboot image?")
Christian Skubichf13db122019-07-31 11:34:15 +0200143 elif ret == image.VerifyResult.INVALID_TLV_INFO_MAGIC:
Fabio Utzig4a5477a2019-05-27 15:45:08 -0300144 print("Invalid TLV info magic; is this an MCUboot image?")
145 elif ret == image.VerifyResult.INVALID_HASH:
146 print("Image has an invalid sha256 digest")
147 elif ret == image.VerifyResult.INVALID_SIGNATURE:
148 print("No signature found for the given key")
Christian Skubichf13db122019-07-31 11:34:15 +0200149 else:
150 print("Unknown return code: {}".format(ret))
Fabio Utzig4a5477a2019-05-27 15:45:08 -0300151 sys.exit(1)
152
153
Fabio Utzige89841d2018-12-21 11:19:06 -0200154def validate_version(ctx, param, value):
155 try:
156 decode_version(value)
157 return value
158 except ValueError as e:
159 raise click.BadParameter("{}".format(e))
160
161
David Vincze1a7a6902020-02-18 15:05:16 +0100162def validate_security_counter(ctx, param, value):
163 if value is not None:
164 if value.lower() == 'auto':
165 return 'auto'
166 else:
167 try:
168 return int(value, 0)
169 except ValueError:
170 raise click.BadParameter(
171 "{} is not a valid integer. Please use code literals "
172 "prefixed with 0b/0B, 0o/0O, or 0x/0X as necessary."
173 .format(value))
174
175
Fabio Utzige89841d2018-12-21 11:19:06 -0200176def validate_header_size(ctx, param, value):
177 min_hdr_size = image.IMAGE_HEADER_SIZE
178 if value < min_hdr_size:
179 raise click.BadParameter(
180 "Minimum value for -H/--header-size is {}".format(min_hdr_size))
181 return value
182
183
David Vinczeda8c9192019-03-26 17:17:41 +0100184def get_dependencies(ctx, param, value):
185 if value is not None:
186 versions = []
187 images = re.findall(r"\((\d+)", value)
188 if len(images) == 0:
189 raise click.BadParameter(
190 "Image dependency format is invalid: {}".format(value))
191 raw_versions = re.findall(r",\s*([0-9.+]+)\)", value)
192 if len(images) != len(raw_versions):
193 raise click.BadParameter(
194 '''There's a mismatch between the number of dependency images
195 and versions in: {}'''.format(value))
196 for raw_version in raw_versions:
197 try:
198 versions.append(decode_version(raw_version))
199 except ValueError as e:
200 raise click.BadParameter("{}".format(e))
201 dependencies = dict()
202 dependencies[image.DEP_IMAGES_KEY] = images
203 dependencies[image.DEP_VERSIONS_KEY] = versions
204 return dependencies
205
206
Fabio Utzige89841d2018-12-21 11:19:06 -0200207class BasedIntParamType(click.ParamType):
208 name = 'integer'
209
210 def convert(self, value, param, ctx):
211 try:
David Vincze1a7a6902020-02-18 15:05:16 +0100212 return int(value, 0)
Fabio Utzige89841d2018-12-21 11:19:06 -0200213 except ValueError:
David Vincze1a7a6902020-02-18 15:05:16 +0100214 self.fail('%s is not a valid integer. Please use code literals '
215 'prefixed with 0b/0B, 0o/0O, or 0x/0X as necessary.'
216 % value, param, ctx)
Fabio Utzige89841d2018-12-21 11:19:06 -0200217
218
219@click.argument('outfile')
220@click.argument('infile')
Fabio Utzig9117fde2019-10-17 11:11:46 -0300221@click.option('-R', '--erased-val', type=click.Choice(['0', '0xff']),
222 required=False,
223 help='The value that is read back from erased flash.')
Fabio Utzigedbabcf2019-10-11 13:03:37 -0300224@click.option('-x', '--hex-addr', type=BasedIntParamType(), required=False,
225 help='Adjust address in hex output file.')
Håkon Øye Amundsendf8c8912019-08-26 12:15:28 +0000226@click.option('-L', '--load-addr', type=BasedIntParamType(), required=False,
227 help='Load address for image when it is in its primary slot.')
Fabio Utzig9a492d52020-01-15 11:31:52 -0300228@click.option('--save-enctlv', default=False, is_flag=True,
229 help='When upgrading, save encrypted key TLVs instead of plain '
230 'keys. Enable when BOOT_SWAP_SAVE_ENCTLV config option '
231 'was set.')
Fabio Utzige89841d2018-12-21 11:19:06 -0200232@click.option('-E', '--encrypt', metavar='filename',
233 help='Encrypt image using the provided public key')
234@click.option('-e', '--endian', type=click.Choice(['little', 'big']),
235 default='little', help="Select little or big endian")
236@click.option('--overwrite-only', default=False, is_flag=True,
237 help='Use overwrite-only instead of swap upgrades')
David Vincze71b8f982020-03-17 19:08:12 +0100238@click.option('--boot-record', metavar='sw_type', help='Create CBOR encoded '
239 'boot record TLV. The sw_type represents the role of the '
240 'software component (e.g. CoFM for coprocessor firmware). '
241 '[max. 12 characters]')
Fabio Utzige89841d2018-12-21 11:19:06 -0200242@click.option('-M', '--max-sectors', type=int,
Fabio Utzig9a492d52020-01-15 11:31:52 -0300243 help='When padding allow for this amount of sectors (defaults '
244 'to 128)')
Henrik Brix Andersen0ce958e2020-03-11 14:04:11 +0100245@click.option('--confirm', default=False, is_flag=True,
246 help='When padding the image, mark it as confirmed')
Fabio Utzige89841d2018-12-21 11:19:06 -0200247@click.option('--pad', default=False, is_flag=True,
248 help='Pad image to --slot-size bytes, adding trailer magic')
249@click.option('-S', '--slot-size', type=BasedIntParamType(), required=True,
250 help='Size of the slot where the image will be written')
251@click.option('--pad-header', default=False, is_flag=True,
Fabio Utzig9a492d52020-01-15 11:31:52 -0300252 help='Add --header-size zeroed bytes at the beginning of the '
253 'image')
Fabio Utzige89841d2018-12-21 11:19:06 -0200254@click.option('-H', '--header-size', callback=validate_header_size,
255 type=BasedIntParamType(), required=True)
David Brown4878c272020-03-10 16:23:56 -0600256@click.option('--pad-sig', default=False, is_flag=True,
257 help='Add 0-2 bytes of padding to ECDSA signature '
258 '(for mcuboot <1.5)')
David Vinczeda8c9192019-03-26 17:17:41 +0100259@click.option('-d', '--dependencies', callback=get_dependencies,
260 required=False, help='''Add dependence on another image, format:
261 "(<image_ID>,<image_version>), ... "''')
David Vincze1a7a6902020-02-18 15:05:16 +0100262@click.option('-s', '--security-counter', callback=validate_security_counter,
263 help='Specify the value of security counter. Use the `auto` '
264 'keyword to automatically generate it from the image version.')
Fabio Utzige89841d2018-12-21 11:19:06 -0200265@click.option('-v', '--version', callback=validate_version, required=True)
266@click.option('--align', type=click.Choice(['1', '2', '4', '8']),
267 required=True)
David Vinczedde178d2020-03-26 20:06:01 +0100268@click.option('--public-key-format', type=click.Choice(['hash', 'full']),
269 default='hash', help='In what format to add the public key to '
270 'the image manifest: full key or hash of the key.')
Fabio Utzige89841d2018-12-21 11:19:06 -0200271@click.option('-k', '--key', metavar='filename')
Fabio Utzig7c00acd2019-01-07 09:54:20 -0200272@click.command(help='''Create a signed or unsigned image\n
273 INFILE and OUTFILE are parsed as Intel HEX if the params have
Håkon Øye Amundsendf8c8912019-08-26 12:15:28 +0000274 .hex extension, otherwise binary format is used''')
David Vinczedde178d2020-03-26 20:06:01 +0100275def sign(key, public_key_format, align, version, pad_sig, header_size,
276 pad_header, slot_size, pad, confirm, max_sectors, overwrite_only,
277 endian, encrypt, infile, outfile, dependencies, load_addr, hex_addr,
278 erased_val, save_enctlv, security_counter, boot_record):
Fabio Utzig7c00acd2019-01-07 09:54:20 -0200279 img = image.Image(version=decode_version(version), header_size=header_size,
Henrik Brix Andersen0ce958e2020-03-11 14:04:11 +0100280 pad_header=pad_header, pad=pad, confirm=confirm,
281 align=int(align), slot_size=slot_size,
282 max_sectors=max_sectors, overwrite_only=overwrite_only,
283 endian=endian, load_addr=load_addr, erased_val=erased_val,
David Vincze1a7a6902020-02-18 15:05:16 +0100284 save_enctlv=save_enctlv,
285 security_counter=security_counter)
Fabio Utzig7c00acd2019-01-07 09:54:20 -0200286 img.load(infile)
Fabio Utzige89841d2018-12-21 11:19:06 -0200287 key = load_key(key) if key else None
288 enckey = load_key(encrypt) if encrypt else None
Fabio Utzig7a3b2602019-10-22 09:56:44 -0300289 if enckey and key:
290 if ((isinstance(key, keys.ECDSA256P1) and
291 not isinstance(enckey, keys.ECDSA256P1Public))
292 or (isinstance(key, keys.RSA) and
293 not isinstance(enckey, keys.RSAPublic))):
294 # FIXME
Fabio Utzig1f508922020-01-15 11:37:51 -0300295 raise click.UsageError("Signing and encryption must use the same "
296 "type of key")
David Brown4878c272020-03-10 16:23:56 -0600297
298 if pad_sig and hasattr(key, 'pad_sig'):
299 key.pad_sig = True
300
David Vinczedde178d2020-03-26 20:06:01 +0100301 img.create(key, public_key_format, enckey, dependencies, boot_record)
Fabio Utzigedbabcf2019-10-11 13:03:37 -0300302 img.save(outfile, hex_addr)
Fabio Utzige89841d2018-12-21 11:19:06 -0200303
304
305class AliasesGroup(click.Group):
306
307 _aliases = {
308 "create": "sign",
309 }
310
311 def list_commands(self, ctx):
312 cmds = [k for k in self.commands]
313 aliases = [k for k in self._aliases]
314 return sorted(cmds + aliases)
315
316 def get_command(self, ctx, cmd_name):
317 rv = click.Group.get_command(self, ctx, cmd_name)
318 if rv is not None:
319 return rv
320 if cmd_name in self._aliases:
321 return click.Group.get_command(self, ctx, self._aliases[cmd_name])
322 return None
323
324
Fabio Utzig25c6a152019-09-10 12:52:26 -0300325@click.command(help='Print imgtool version information')
326def version():
327 print(imgtool_version)
328
329
Fabio Utzige89841d2018-12-21 11:19:06 -0200330@click.command(cls=AliasesGroup,
331 context_settings=dict(help_option_names=['-h', '--help']))
332def imgtool():
333 pass
334
335
336imgtool.add_command(keygen)
337imgtool.add_command(getpub)
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +0200338imgtool.add_command(getpriv)
Fabio Utzig4a5477a2019-05-27 15:45:08 -0300339imgtool.add_command(verify)
Fabio Utzige89841d2018-12-21 11:19:06 -0200340imgtool.add_command(sign)
Fabio Utzig25c6a152019-09-10 12:52:26 -0300341imgtool.add_command(version)
Fabio Utzige89841d2018-12-21 11:19:06 -0200342
343
344if __name__ == '__main__':
345 imgtool()