Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 1 | #! /usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright 2017 Linaro Limited |
David Vincze | da8c919 | 2019-03-26 17:17:41 +0100 | [diff] [blame] | 4 | # Copyright 2019 Arm Limited |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 5 | # |
| 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 Vincze | da8c919 | 2019-03-26 17:17:41 +0100 | [diff] [blame] | 18 | import re |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 19 | import click |
| 20 | import getpass |
| 21 | import imgtool.keys as keys |
Fabio Utzig | 4a5477a | 2019-05-27 15:45:08 -0300 | [diff] [blame] | 22 | import sys |
Fabio Utzig | 25c6a15 | 2019-09-10 12:52:26 -0300 | [diff] [blame] | 23 | from imgtool import image, imgtool_version |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 24 | from imgtool.version import decode_version |
| 25 | |
| 26 | |
| 27 | def gen_rsa2048(keyfile, passwd): |
Fabio Utzig | 19fd79a | 2019-05-08 18:20:39 -0300 | [diff] [blame] | 28 | keys.RSA.generate().export_private(path=keyfile, passwd=passwd) |
| 29 | |
| 30 | |
| 31 | def gen_rsa3072(keyfile, passwd): |
| 32 | keys.RSA.generate(key_size=3072).export_private(path=keyfile, |
| 33 | passwd=passwd) |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 34 | |
| 35 | |
| 36 | def gen_ecdsa_p256(keyfile, passwd): |
| 37 | keys.ECDSA256P1.generate().export_private(keyfile, passwd=passwd) |
| 38 | |
| 39 | |
| 40 | def gen_ecdsa_p224(keyfile, passwd): |
| 41 | print("TODO: p-224 not yet implemented") |
| 42 | |
| 43 | |
Fabio Utzig | 8101d1f | 2019-05-09 15:03:22 -0300 | [diff] [blame] | 44 | def gen_ed25519(keyfile, passwd): |
Fabio Utzig | 4bd4c7c | 2019-06-27 08:23:21 -0300 | [diff] [blame] | 45 | keys.Ed25519.generate().export_private(path=keyfile, passwd=passwd) |
Fabio Utzig | 8101d1f | 2019-05-09 15:03:22 -0300 | [diff] [blame] | 46 | |
| 47 | |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 48 | valid_langs = ['c', 'rust'] |
| 49 | keygens = { |
| 50 | 'rsa-2048': gen_rsa2048, |
Fabio Utzig | 19fd79a | 2019-05-08 18:20:39 -0300 | [diff] [blame] | 51 | 'rsa-3072': gen_rsa3072, |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 52 | 'ecdsa-p256': gen_ecdsa_p256, |
| 53 | 'ecdsa-p224': gen_ecdsa_p224, |
Fabio Utzig | 8101d1f | 2019-05-09 15:03:22 -0300 | [diff] [blame] | 54 | 'ed25519': gen_ed25519, |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | |
| 58 | def load_key(keyfile): |
| 59 | # TODO: better handling of invalid pass-phrase |
| 60 | key = keys.load(keyfile) |
| 61 | if key is not None: |
| 62 | return key |
| 63 | passwd = getpass.getpass("Enter key passphrase: ").encode('utf-8') |
| 64 | return keys.load(keyfile, passwd) |
| 65 | |
| 66 | |
| 67 | def get_password(): |
| 68 | while True: |
| 69 | passwd = getpass.getpass("Enter key passphrase: ") |
| 70 | passwd2 = getpass.getpass("Reenter passphrase: ") |
| 71 | if passwd == passwd2: |
| 72 | break |
| 73 | print("Passwords do not match, try again") |
| 74 | |
| 75 | # Password must be bytes, always use UTF-8 for consistent |
| 76 | # encoding. |
| 77 | return passwd.encode('utf-8') |
| 78 | |
| 79 | |
| 80 | @click.option('-p', '--password', is_flag=True, |
| 81 | help='Prompt for password to protect key') |
| 82 | @click.option('-t', '--type', metavar='type', required=True, |
| 83 | type=click.Choice(keygens.keys())) |
| 84 | @click.option('-k', '--key', metavar='filename', required=True) |
| 85 | @click.command(help='Generate pub/private keypair') |
| 86 | def keygen(type, key, password): |
| 87 | password = get_password() if password else None |
| 88 | keygens[type](key, password) |
| 89 | |
| 90 | |
| 91 | @click.option('-l', '--lang', metavar='lang', default=valid_langs[0], |
| 92 | type=click.Choice(valid_langs)) |
| 93 | @click.option('-k', '--key', metavar='filename', required=True) |
| 94 | @click.command(help='Get public key from keypair') |
| 95 | def getpub(key, lang): |
| 96 | key = load_key(key) |
| 97 | if key is None: |
| 98 | print("Invalid passphrase") |
| 99 | elif lang == 'c': |
| 100 | key.emit_c() |
| 101 | elif lang == 'rust': |
| 102 | key.emit_rust() |
| 103 | else: |
| 104 | raise ValueError("BUG: should never get here!") |
| 105 | |
| 106 | |
Fabio Utzig | 4a5477a | 2019-05-27 15:45:08 -0300 | [diff] [blame] | 107 | @click.argument('imgfile') |
| 108 | @click.option('-k', '--key', metavar='filename') |
| 109 | @click.command(help="Check that signed image can be verified by given key") |
| 110 | def verify(key, imgfile): |
| 111 | key = load_key(key) if key else None |
Marek Pieta | e955510 | 2019-08-08 16:08:16 +0200 | [diff] [blame] | 112 | ret, version = image.Image.verify(imgfile, key) |
Fabio Utzig | 4a5477a | 2019-05-27 15:45:08 -0300 | [diff] [blame] | 113 | if ret == image.VerifyResult.OK: |
| 114 | print("Image was correctly validated") |
Marek Pieta | e955510 | 2019-08-08 16:08:16 +0200 | [diff] [blame] | 115 | print("Image version: {}.{}.{}+{}".format(*version)) |
Fabio Utzig | 4a5477a | 2019-05-27 15:45:08 -0300 | [diff] [blame] | 116 | return |
| 117 | elif ret == image.VerifyResult.INVALID_MAGIC: |
| 118 | print("Invalid image magic; is this an MCUboot image?") |
Christian Skubich | f13db12 | 2019-07-31 11:34:15 +0200 | [diff] [blame] | 119 | elif ret == image.VerifyResult.INVALID_TLV_INFO_MAGIC: |
Fabio Utzig | 4a5477a | 2019-05-27 15:45:08 -0300 | [diff] [blame] | 120 | print("Invalid TLV info magic; is this an MCUboot image?") |
| 121 | elif ret == image.VerifyResult.INVALID_HASH: |
| 122 | print("Image has an invalid sha256 digest") |
| 123 | elif ret == image.VerifyResult.INVALID_SIGNATURE: |
| 124 | print("No signature found for the given key") |
Christian Skubich | f13db12 | 2019-07-31 11:34:15 +0200 | [diff] [blame] | 125 | else: |
| 126 | print("Unknown return code: {}".format(ret)) |
Fabio Utzig | 4a5477a | 2019-05-27 15:45:08 -0300 | [diff] [blame] | 127 | sys.exit(1) |
| 128 | |
| 129 | |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 130 | def validate_version(ctx, param, value): |
| 131 | try: |
| 132 | decode_version(value) |
| 133 | return value |
| 134 | except ValueError as e: |
| 135 | raise click.BadParameter("{}".format(e)) |
| 136 | |
| 137 | |
| 138 | def validate_header_size(ctx, param, value): |
| 139 | min_hdr_size = image.IMAGE_HEADER_SIZE |
| 140 | if value < min_hdr_size: |
| 141 | raise click.BadParameter( |
| 142 | "Minimum value for -H/--header-size is {}".format(min_hdr_size)) |
| 143 | return value |
| 144 | |
| 145 | |
David Vincze | da8c919 | 2019-03-26 17:17:41 +0100 | [diff] [blame] | 146 | def get_dependencies(ctx, param, value): |
| 147 | if value is not None: |
| 148 | versions = [] |
| 149 | images = re.findall(r"\((\d+)", value) |
| 150 | if len(images) == 0: |
| 151 | raise click.BadParameter( |
| 152 | "Image dependency format is invalid: {}".format(value)) |
| 153 | raw_versions = re.findall(r",\s*([0-9.+]+)\)", value) |
| 154 | if len(images) != len(raw_versions): |
| 155 | raise click.BadParameter( |
| 156 | '''There's a mismatch between the number of dependency images |
| 157 | and versions in: {}'''.format(value)) |
| 158 | for raw_version in raw_versions: |
| 159 | try: |
| 160 | versions.append(decode_version(raw_version)) |
| 161 | except ValueError as e: |
| 162 | raise click.BadParameter("{}".format(e)) |
| 163 | dependencies = dict() |
| 164 | dependencies[image.DEP_IMAGES_KEY] = images |
| 165 | dependencies[image.DEP_VERSIONS_KEY] = versions |
| 166 | return dependencies |
| 167 | |
| 168 | |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 169 | class BasedIntParamType(click.ParamType): |
| 170 | name = 'integer' |
| 171 | |
| 172 | def convert(self, value, param, ctx): |
| 173 | try: |
| 174 | if value[:2].lower() == '0x': |
| 175 | return int(value[2:], 16) |
| 176 | elif value[:1] == '0': |
| 177 | return int(value, 8) |
| 178 | return int(value, 10) |
| 179 | except ValueError: |
| 180 | self.fail('%s is not a valid integer' % value, param, ctx) |
| 181 | |
| 182 | |
| 183 | @click.argument('outfile') |
| 184 | @click.argument('infile') |
Fabio Utzig | edbabcf | 2019-10-11 13:03:37 -0300 | [diff] [blame^] | 185 | @click.option('-x', '--hex-addr', type=BasedIntParamType(), required=False, |
| 186 | help='Adjust address in hex output file.') |
Håkon Øye Amundsen | df8c891 | 2019-08-26 12:15:28 +0000 | [diff] [blame] | 187 | @click.option('-L', '--load-addr', type=BasedIntParamType(), required=False, |
| 188 | help='Load address for image when it is in its primary slot.') |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 189 | @click.option('-E', '--encrypt', metavar='filename', |
| 190 | help='Encrypt image using the provided public key') |
| 191 | @click.option('-e', '--endian', type=click.Choice(['little', 'big']), |
| 192 | default='little', help="Select little or big endian") |
| 193 | @click.option('--overwrite-only', default=False, is_flag=True, |
| 194 | help='Use overwrite-only instead of swap upgrades') |
| 195 | @click.option('-M', '--max-sectors', type=int, |
| 196 | help='When padding allow for this amount of sectors (defaults to 128)') |
| 197 | @click.option('--pad', default=False, is_flag=True, |
| 198 | help='Pad image to --slot-size bytes, adding trailer magic') |
| 199 | @click.option('-S', '--slot-size', type=BasedIntParamType(), required=True, |
| 200 | help='Size of the slot where the image will be written') |
| 201 | @click.option('--pad-header', default=False, is_flag=True, |
| 202 | help='Add --header-size zeroed bytes at the beginning of the image') |
| 203 | @click.option('-H', '--header-size', callback=validate_header_size, |
| 204 | type=BasedIntParamType(), required=True) |
David Vincze | da8c919 | 2019-03-26 17:17:41 +0100 | [diff] [blame] | 205 | @click.option('-d', '--dependencies', callback=get_dependencies, |
| 206 | required=False, help='''Add dependence on another image, format: |
| 207 | "(<image_ID>,<image_version>), ... "''') |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 208 | @click.option('-v', '--version', callback=validate_version, required=True) |
| 209 | @click.option('--align', type=click.Choice(['1', '2', '4', '8']), |
| 210 | required=True) |
| 211 | @click.option('-k', '--key', metavar='filename') |
Fabio Utzig | 7c00acd | 2019-01-07 09:54:20 -0200 | [diff] [blame] | 212 | @click.command(help='''Create a signed or unsigned image\n |
| 213 | INFILE and OUTFILE are parsed as Intel HEX if the params have |
Håkon Øye Amundsen | df8c891 | 2019-08-26 12:15:28 +0000 | [diff] [blame] | 214 | .hex extension, otherwise binary format is used''') |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 215 | def sign(key, align, version, header_size, pad_header, slot_size, pad, |
David Vincze | da8c919 | 2019-03-26 17:17:41 +0100 | [diff] [blame] | 216 | max_sectors, overwrite_only, endian, encrypt, infile, outfile, |
Fabio Utzig | edbabcf | 2019-10-11 13:03:37 -0300 | [diff] [blame^] | 217 | dependencies, load_addr, hex_addr): |
Fabio Utzig | 7c00acd | 2019-01-07 09:54:20 -0200 | [diff] [blame] | 218 | img = image.Image(version=decode_version(version), header_size=header_size, |
| 219 | pad_header=pad_header, pad=pad, align=int(align), |
| 220 | slot_size=slot_size, max_sectors=max_sectors, |
Fabio Utzig | 4f0ea74 | 2019-09-10 12:53:18 -0300 | [diff] [blame] | 221 | overwrite_only=overwrite_only, endian=endian, |
| 222 | load_addr=load_addr) |
Fabio Utzig | 7c00acd | 2019-01-07 09:54:20 -0200 | [diff] [blame] | 223 | img.load(infile) |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 224 | key = load_key(key) if key else None |
| 225 | enckey = load_key(encrypt) if encrypt else None |
| 226 | if enckey: |
Fabio Utzig | 19fd79a | 2019-05-08 18:20:39 -0300 | [diff] [blame] | 227 | if not isinstance(enckey, (keys.RSA, keys.RSAPublic)): |
Chris Bittner | fda937a | 2019-03-29 10:11:31 +0100 | [diff] [blame] | 228 | raise Exception("Encryption only available with RSA key") |
Fabio Utzig | 19fd79a | 2019-05-08 18:20:39 -0300 | [diff] [blame] | 229 | if key and not isinstance(key, keys.RSA): |
Chris Bittner | fda937a | 2019-03-29 10:11:31 +0100 | [diff] [blame] | 230 | raise Exception("Signing only available with private RSA key") |
David Vincze | da8c919 | 2019-03-26 17:17:41 +0100 | [diff] [blame] | 231 | img.create(key, enckey, dependencies) |
Fabio Utzig | edbabcf | 2019-10-11 13:03:37 -0300 | [diff] [blame^] | 232 | img.save(outfile, hex_addr) |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 233 | |
| 234 | |
| 235 | class AliasesGroup(click.Group): |
| 236 | |
| 237 | _aliases = { |
| 238 | "create": "sign", |
| 239 | } |
| 240 | |
| 241 | def list_commands(self, ctx): |
| 242 | cmds = [k for k in self.commands] |
| 243 | aliases = [k for k in self._aliases] |
| 244 | return sorted(cmds + aliases) |
| 245 | |
| 246 | def get_command(self, ctx, cmd_name): |
| 247 | rv = click.Group.get_command(self, ctx, cmd_name) |
| 248 | if rv is not None: |
| 249 | return rv |
| 250 | if cmd_name in self._aliases: |
| 251 | return click.Group.get_command(self, ctx, self._aliases[cmd_name]) |
| 252 | return None |
| 253 | |
| 254 | |
Fabio Utzig | 25c6a15 | 2019-09-10 12:52:26 -0300 | [diff] [blame] | 255 | @click.command(help='Print imgtool version information') |
| 256 | def version(): |
| 257 | print(imgtool_version) |
| 258 | |
| 259 | |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 260 | @click.command(cls=AliasesGroup, |
| 261 | context_settings=dict(help_option_names=['-h', '--help'])) |
| 262 | def imgtool(): |
| 263 | pass |
| 264 | |
| 265 | |
| 266 | imgtool.add_command(keygen) |
| 267 | imgtool.add_command(getpub) |
Fabio Utzig | 4a5477a | 2019-05-27 15:45:08 -0300 | [diff] [blame] | 268 | imgtool.add_command(verify) |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 269 | imgtool.add_command(sign) |
Fabio Utzig | 25c6a15 | 2019-09-10 12:52:26 -0300 | [diff] [blame] | 270 | imgtool.add_command(version) |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 271 | |
| 272 | |
| 273 | if __name__ == '__main__': |
| 274 | imgtool() |