Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 1 | #! /usr/bin/env python3 |
| 2 | # |
David Vincze | 71b8f98 | 2020-03-17 19:08:12 +0100 | [diff] [blame] | 3 | # Copyright 2017-2020 Linaro Limited |
David Vincze | 1a7a690 | 2020-02-18 15:05:16 +0100 | [diff] [blame] | 4 | # Copyright 2019-2020 Arm Limited |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 5 | # |
David Brown | 79c4fcf | 2021-01-26 15:04:05 -0700 | [diff] [blame] | 6 | # SPDX-License-Identifier: Apache-2.0 |
| 7 | # |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 8 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | # you may not use this file except in compliance with the License. |
| 10 | # You may obtain a copy of the License at |
| 11 | # |
| 12 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | # |
| 14 | # Unless required by applicable law or agreed to in writing, software |
| 15 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | # See the License for the specific language governing permissions and |
| 18 | # limitations under the License. |
| 19 | |
David Vincze | da8c919 | 2019-03-26 17:17:41 +0100 | [diff] [blame] | 20 | import re |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 21 | import click |
| 22 | import getpass |
| 23 | import imgtool.keys as keys |
Fabio Utzig | 4a5477a | 2019-05-27 15:45:08 -0300 | [diff] [blame] | 24 | import sys |
Fabio Utzig | 25c6a15 | 2019-09-10 12:52:26 -0300 | [diff] [blame] | 25 | from imgtool import image, imgtool_version |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 26 | from imgtool.version import decode_version |
Fabio Utzig | 4facd1b | 2020-04-02 13:17:38 -0300 | [diff] [blame] | 27 | from .keys import ( |
| 28 | RSAUsageError, ECDSAUsageError, Ed25519UsageError, X25519UsageError) |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 29 | |
David Vincze | 71b8f98 | 2020-03-17 19:08:12 +0100 | [diff] [blame] | 30 | MIN_PYTHON_VERSION = (3, 6) |
| 31 | if sys.version_info < MIN_PYTHON_VERSION: |
| 32 | sys.exit("Python %s.%s or newer is required by imgtool." |
| 33 | % MIN_PYTHON_VERSION) |
| 34 | |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 35 | |
| 36 | def gen_rsa2048(keyfile, passwd): |
Fabio Utzig | 19fd79a | 2019-05-08 18:20:39 -0300 | [diff] [blame] | 37 | keys.RSA.generate().export_private(path=keyfile, passwd=passwd) |
| 38 | |
| 39 | |
| 40 | def gen_rsa3072(keyfile, passwd): |
| 41 | keys.RSA.generate(key_size=3072).export_private(path=keyfile, |
| 42 | passwd=passwd) |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 43 | |
| 44 | |
| 45 | def gen_ecdsa_p256(keyfile, passwd): |
| 46 | keys.ECDSA256P1.generate().export_private(keyfile, passwd=passwd) |
| 47 | |
| 48 | |
| 49 | def gen_ecdsa_p224(keyfile, passwd): |
| 50 | print("TODO: p-224 not yet implemented") |
| 51 | |
| 52 | |
Fabio Utzig | 8101d1f | 2019-05-09 15:03:22 -0300 | [diff] [blame] | 53 | def gen_ed25519(keyfile, passwd): |
Fabio Utzig | 4bd4c7c | 2019-06-27 08:23:21 -0300 | [diff] [blame] | 54 | keys.Ed25519.generate().export_private(path=keyfile, passwd=passwd) |
Fabio Utzig | 8101d1f | 2019-05-09 15:03:22 -0300 | [diff] [blame] | 55 | |
| 56 | |
Fabio Utzig | 4facd1b | 2020-04-02 13:17:38 -0300 | [diff] [blame] | 57 | def gen_x25519(keyfile, passwd): |
| 58 | keys.X25519.generate().export_private(path=keyfile, passwd=passwd) |
| 59 | |
| 60 | |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 61 | valid_langs = ['c', 'rust'] |
| 62 | keygens = { |
| 63 | 'rsa-2048': gen_rsa2048, |
Fabio Utzig | 19fd79a | 2019-05-08 18:20:39 -0300 | [diff] [blame] | 64 | 'rsa-3072': gen_rsa3072, |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 65 | 'ecdsa-p256': gen_ecdsa_p256, |
| 66 | 'ecdsa-p224': gen_ecdsa_p224, |
Fabio Utzig | 4facd1b | 2020-04-02 13:17:38 -0300 | [diff] [blame] | 67 | 'ed25519': gen_ed25519, |
| 68 | 'x25519': gen_x25519, |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | |
| 72 | def load_key(keyfile): |
| 73 | # TODO: better handling of invalid pass-phrase |
| 74 | key = keys.load(keyfile) |
| 75 | if key is not None: |
| 76 | return key |
| 77 | passwd = getpass.getpass("Enter key passphrase: ").encode('utf-8') |
| 78 | return keys.load(keyfile, passwd) |
| 79 | |
| 80 | |
| 81 | def get_password(): |
| 82 | while True: |
| 83 | passwd = getpass.getpass("Enter key passphrase: ") |
| 84 | passwd2 = getpass.getpass("Reenter passphrase: ") |
| 85 | if passwd == passwd2: |
| 86 | break |
| 87 | print("Passwords do not match, try again") |
| 88 | |
| 89 | # Password must be bytes, always use UTF-8 for consistent |
| 90 | # encoding. |
| 91 | return passwd.encode('utf-8') |
| 92 | |
| 93 | |
| 94 | @click.option('-p', '--password', is_flag=True, |
| 95 | help='Prompt for password to protect key') |
| 96 | @click.option('-t', '--type', metavar='type', required=True, |
Fabio Utzig | 7ca2855 | 2019-12-13 11:24:20 -0300 | [diff] [blame] | 97 | type=click.Choice(keygens.keys()), prompt=True, |
| 98 | help='{}'.format('One of: {}'.format(', '.join(keygens.keys())))) |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 99 | @click.option('-k', '--key', metavar='filename', required=True) |
| 100 | @click.command(help='Generate pub/private keypair') |
| 101 | def keygen(type, key, password): |
| 102 | password = get_password() if password else None |
| 103 | keygens[type](key, password) |
| 104 | |
| 105 | |
| 106 | @click.option('-l', '--lang', metavar='lang', default=valid_langs[0], |
| 107 | type=click.Choice(valid_langs)) |
| 108 | @click.option('-k', '--key', metavar='filename', required=True) |
Ioannis Konstantelias | 78e57c7 | 2019-11-28 16:06:12 +0200 | [diff] [blame] | 109 | @click.command(help='Dump public key from keypair') |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 110 | def getpub(key, lang): |
| 111 | key = load_key(key) |
| 112 | if key is None: |
| 113 | print("Invalid passphrase") |
| 114 | elif lang == 'c': |
Ioannis Konstantelias | 78e57c7 | 2019-11-28 16:06:12 +0200 | [diff] [blame] | 115 | key.emit_c_public() |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 116 | elif lang == 'rust': |
Ioannis Konstantelias | 78e57c7 | 2019-11-28 16:06:12 +0200 | [diff] [blame] | 117 | key.emit_rust_public() |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 118 | else: |
| 119 | raise ValueError("BUG: should never get here!") |
| 120 | |
| 121 | |
Ioannis Konstantelias | 78e57c7 | 2019-11-28 16:06:12 +0200 | [diff] [blame] | 122 | @click.option('--minimal', default=False, is_flag=True, |
| 123 | help='Reduce the size of the dumped private key to include only ' |
| 124 | 'the minimum amount of data required to decrypt. This ' |
| 125 | 'might require changes to the build config. Check the docs!' |
| 126 | ) |
| 127 | @click.option('-k', '--key', metavar='filename', required=True) |
| 128 | @click.command(help='Dump private key from keypair') |
| 129 | def getpriv(key, minimal): |
| 130 | key = load_key(key) |
| 131 | if key is None: |
| 132 | print("Invalid passphrase") |
Fabio Utzig | 1f50892 | 2020-01-15 11:37:51 -0300 | [diff] [blame] | 133 | try: |
| 134 | key.emit_private(minimal) |
Fabio Utzig | 4facd1b | 2020-04-02 13:17:38 -0300 | [diff] [blame] | 135 | except (RSAUsageError, ECDSAUsageError, Ed25519UsageError, |
| 136 | X25519UsageError) as e: |
Fabio Utzig | 1f50892 | 2020-01-15 11:37:51 -0300 | [diff] [blame] | 137 | raise click.UsageError(e) |
Ioannis Konstantelias | 78e57c7 | 2019-11-28 16:06:12 +0200 | [diff] [blame] | 138 | |
| 139 | |
Fabio Utzig | 4a5477a | 2019-05-27 15:45:08 -0300 | [diff] [blame] | 140 | @click.argument('imgfile') |
| 141 | @click.option('-k', '--key', metavar='filename') |
| 142 | @click.command(help="Check that signed image can be verified by given key") |
| 143 | def verify(key, imgfile): |
| 144 | key = load_key(key) if key else None |
Casper Meijn | 2a01f3f | 2020-08-22 13:51:40 +0200 | [diff] [blame] | 145 | ret, version, digest = image.Image.verify(imgfile, key) |
Fabio Utzig | 4a5477a | 2019-05-27 15:45:08 -0300 | [diff] [blame] | 146 | if ret == image.VerifyResult.OK: |
| 147 | print("Image was correctly validated") |
Marek Pieta | e955510 | 2019-08-08 16:08:16 +0200 | [diff] [blame] | 148 | print("Image version: {}.{}.{}+{}".format(*version)) |
Casper Meijn | 2a01f3f | 2020-08-22 13:51:40 +0200 | [diff] [blame] | 149 | print("Image digest: {}".format(digest.hex())) |
Fabio Utzig | 4a5477a | 2019-05-27 15:45:08 -0300 | [diff] [blame] | 150 | return |
| 151 | elif ret == image.VerifyResult.INVALID_MAGIC: |
| 152 | print("Invalid image magic; is this an MCUboot image?") |
Christian Skubich | f13db12 | 2019-07-31 11:34:15 +0200 | [diff] [blame] | 153 | elif ret == image.VerifyResult.INVALID_TLV_INFO_MAGIC: |
Fabio Utzig | 4a5477a | 2019-05-27 15:45:08 -0300 | [diff] [blame] | 154 | print("Invalid TLV info magic; is this an MCUboot image?") |
| 155 | elif ret == image.VerifyResult.INVALID_HASH: |
| 156 | print("Image has an invalid sha256 digest") |
| 157 | elif ret == image.VerifyResult.INVALID_SIGNATURE: |
| 158 | print("No signature found for the given key") |
Christian Skubich | f13db12 | 2019-07-31 11:34:15 +0200 | [diff] [blame] | 159 | else: |
| 160 | print("Unknown return code: {}".format(ret)) |
Fabio Utzig | 4a5477a | 2019-05-27 15:45:08 -0300 | [diff] [blame] | 161 | sys.exit(1) |
| 162 | |
| 163 | |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 164 | def validate_version(ctx, param, value): |
| 165 | try: |
| 166 | decode_version(value) |
| 167 | return value |
| 168 | except ValueError as e: |
| 169 | raise click.BadParameter("{}".format(e)) |
| 170 | |
| 171 | |
David Vincze | 1a7a690 | 2020-02-18 15:05:16 +0100 | [diff] [blame] | 172 | def validate_security_counter(ctx, param, value): |
| 173 | if value is not None: |
| 174 | if value.lower() == 'auto': |
| 175 | return 'auto' |
| 176 | else: |
| 177 | try: |
| 178 | return int(value, 0) |
| 179 | except ValueError: |
| 180 | raise click.BadParameter( |
| 181 | "{} is not a valid integer. Please use code literals " |
| 182 | "prefixed with 0b/0B, 0o/0O, or 0x/0X as necessary." |
| 183 | .format(value)) |
| 184 | |
| 185 | |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 186 | def validate_header_size(ctx, param, value): |
| 187 | min_hdr_size = image.IMAGE_HEADER_SIZE |
| 188 | if value < min_hdr_size: |
| 189 | raise click.BadParameter( |
| 190 | "Minimum value for -H/--header-size is {}".format(min_hdr_size)) |
| 191 | return value |
| 192 | |
| 193 | |
David Vincze | da8c919 | 2019-03-26 17:17:41 +0100 | [diff] [blame] | 194 | def get_dependencies(ctx, param, value): |
| 195 | if value is not None: |
| 196 | versions = [] |
| 197 | images = re.findall(r"\((\d+)", value) |
| 198 | if len(images) == 0: |
| 199 | raise click.BadParameter( |
| 200 | "Image dependency format is invalid: {}".format(value)) |
| 201 | raw_versions = re.findall(r",\s*([0-9.+]+)\)", value) |
| 202 | if len(images) != len(raw_versions): |
| 203 | raise click.BadParameter( |
| 204 | '''There's a mismatch between the number of dependency images |
| 205 | and versions in: {}'''.format(value)) |
| 206 | for raw_version in raw_versions: |
| 207 | try: |
| 208 | versions.append(decode_version(raw_version)) |
| 209 | except ValueError as e: |
| 210 | raise click.BadParameter("{}".format(e)) |
| 211 | dependencies = dict() |
| 212 | dependencies[image.DEP_IMAGES_KEY] = images |
| 213 | dependencies[image.DEP_VERSIONS_KEY] = versions |
| 214 | return dependencies |
| 215 | |
| 216 | |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 217 | class BasedIntParamType(click.ParamType): |
| 218 | name = 'integer' |
| 219 | |
| 220 | def convert(self, value, param, ctx): |
| 221 | try: |
David Vincze | 1a7a690 | 2020-02-18 15:05:16 +0100 | [diff] [blame] | 222 | return int(value, 0) |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 223 | except ValueError: |
David Vincze | 1a7a690 | 2020-02-18 15:05:16 +0100 | [diff] [blame] | 224 | self.fail('%s is not a valid integer. Please use code literals ' |
| 225 | 'prefixed with 0b/0B, 0o/0O, or 0x/0X as necessary.' |
| 226 | % value, param, ctx) |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 227 | |
| 228 | |
| 229 | @click.argument('outfile') |
| 230 | @click.argument('infile') |
Ihor Slabkyy | 24d9373 | 2020-03-10 15:33:57 +0200 | [diff] [blame] | 231 | @click.option('--custom-tlv', required=False, nargs=2, default=[], |
| 232 | multiple=True, metavar='[tag] [value]', |
| 233 | help='Custom TLV that will be placed into protected area. ' |
| 234 | 'Add "0x" prefix if the value should be interpreted as an ' |
| 235 | 'integer, otherwise it will be interpreted as a string. ' |
| 236 | 'Specify the option multiple times to add multiple TLVs.') |
Fabio Utzig | 9117fde | 2019-10-17 11:11:46 -0300 | [diff] [blame] | 237 | @click.option('-R', '--erased-val', type=click.Choice(['0', '0xff']), |
| 238 | required=False, |
| 239 | help='The value that is read back from erased flash.') |
Fabio Utzig | edbabcf | 2019-10-11 13:03:37 -0300 | [diff] [blame] | 240 | @click.option('-x', '--hex-addr', type=BasedIntParamType(), required=False, |
| 241 | help='Adjust address in hex output file.') |
Håkon Øye Amundsen | df8c891 | 2019-08-26 12:15:28 +0000 | [diff] [blame] | 242 | @click.option('-L', '--load-addr', type=BasedIntParamType(), required=False, |
David Vincze | 1e0c544 | 2020-04-07 14:12:33 +0200 | [diff] [blame] | 243 | help='Load address for image when it should run from RAM.') |
Dominik Ermel | 50820b1 | 2020-12-14 13:16:46 +0000 | [diff] [blame] | 244 | @click.option('-F', '--rom-fixed', type=BasedIntParamType(), required=False, |
| 245 | help='Set flash address the image is built for.') |
Fabio Utzig | 9a492d5 | 2020-01-15 11:31:52 -0300 | [diff] [blame] | 246 | @click.option('--save-enctlv', default=False, is_flag=True, |
| 247 | help='When upgrading, save encrypted key TLVs instead of plain ' |
| 248 | 'keys. Enable when BOOT_SWAP_SAVE_ENCTLV config option ' |
| 249 | 'was set.') |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 250 | @click.option('-E', '--encrypt', metavar='filename', |
David Vincze | e574f2d | 2020-07-10 11:42:03 +0200 | [diff] [blame] | 251 | help='Encrypt image using the provided public key. ' |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 252 | '(Not supported in direct-xip or ram-load mode.)') |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 253 | @click.option('-e', '--endian', type=click.Choice(['little', 'big']), |
| 254 | default='little', help="Select little or big endian") |
| 255 | @click.option('--overwrite-only', default=False, is_flag=True, |
| 256 | help='Use overwrite-only instead of swap upgrades') |
David Vincze | 71b8f98 | 2020-03-17 19:08:12 +0100 | [diff] [blame] | 257 | @click.option('--boot-record', metavar='sw_type', help='Create CBOR encoded ' |
| 258 | 'boot record TLV. The sw_type represents the role of the ' |
| 259 | 'software component (e.g. CoFM for coprocessor firmware). ' |
| 260 | '[max. 12 characters]') |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 261 | @click.option('-M', '--max-sectors', type=int, |
Fabio Utzig | 9a492d5 | 2020-01-15 11:31:52 -0300 | [diff] [blame] | 262 | help='When padding allow for this amount of sectors (defaults ' |
| 263 | 'to 128)') |
Henrik Brix Andersen | 0ce958e | 2020-03-11 14:04:11 +0100 | [diff] [blame] | 264 | @click.option('--confirm', default=False, is_flag=True, |
Martí Bolívar | 009a150 | 2020-09-04 14:23:39 -0700 | [diff] [blame] | 265 | help='When padding the image, mark it as confirmed (implies ' |
| 266 | '--pad)') |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 267 | @click.option('--pad', default=False, is_flag=True, |
| 268 | help='Pad image to --slot-size bytes, adding trailer magic') |
| 269 | @click.option('-S', '--slot-size', type=BasedIntParamType(), required=True, |
Fabio Utzig | 826abf4 | 2020-07-13 20:56:35 -0300 | [diff] [blame] | 270 | help='Size of the slot. If the slots have different sizes, use ' |
| 271 | 'the size of the secondary slot.') |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 272 | @click.option('--pad-header', default=False, is_flag=True, |
Fabio Utzig | 9a492d5 | 2020-01-15 11:31:52 -0300 | [diff] [blame] | 273 | help='Add --header-size zeroed bytes at the beginning of the ' |
| 274 | 'image') |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 275 | @click.option('-H', '--header-size', callback=validate_header_size, |
| 276 | type=BasedIntParamType(), required=True) |
David Brown | 4878c27 | 2020-03-10 16:23:56 -0600 | [diff] [blame] | 277 | @click.option('--pad-sig', default=False, is_flag=True, |
| 278 | help='Add 0-2 bytes of padding to ECDSA signature ' |
| 279 | '(for mcuboot <1.5)') |
David Vincze | da8c919 | 2019-03-26 17:17:41 +0100 | [diff] [blame] | 280 | @click.option('-d', '--dependencies', callback=get_dependencies, |
| 281 | required=False, help='''Add dependence on another image, format: |
| 282 | "(<image_ID>,<image_version>), ... "''') |
David Vincze | 1a7a690 | 2020-02-18 15:05:16 +0100 | [diff] [blame] | 283 | @click.option('-s', '--security-counter', callback=validate_security_counter, |
| 284 | help='Specify the value of security counter. Use the `auto` ' |
| 285 | 'keyword to automatically generate it from the image version.') |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 286 | @click.option('-v', '--version', callback=validate_version, required=True) |
| 287 | @click.option('--align', type=click.Choice(['1', '2', '4', '8']), |
| 288 | required=True) |
David Vincze | dde178d | 2020-03-26 20:06:01 +0100 | [diff] [blame] | 289 | @click.option('--public-key-format', type=click.Choice(['hash', 'full']), |
| 290 | default='hash', help='In what format to add the public key to ' |
| 291 | 'the image manifest: full key or hash of the key.') |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 292 | @click.option('-k', '--key', metavar='filename') |
Fabio Utzig | 7c00acd | 2019-01-07 09:54:20 -0200 | [diff] [blame] | 293 | @click.command(help='''Create a signed or unsigned image\n |
| 294 | 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] | 295 | .hex extension, otherwise binary format is used''') |
David Vincze | dde178d | 2020-03-26 20:06:01 +0100 | [diff] [blame] | 296 | def sign(key, public_key_format, align, version, pad_sig, header_size, |
| 297 | pad_header, slot_size, pad, confirm, max_sectors, overwrite_only, |
| 298 | endian, encrypt, infile, outfile, dependencies, load_addr, hex_addr, |
Dominik Ermel | 50820b1 | 2020-12-14 13:16:46 +0000 | [diff] [blame] | 299 | erased_val, save_enctlv, security_counter, boot_record, custom_tlv, |
| 300 | rom_fixed): |
Martí Bolívar | 009a150 | 2020-09-04 14:23:39 -0700 | [diff] [blame] | 301 | |
| 302 | if confirm: |
| 303 | # Confirmed but non-padded images don't make much sense, because |
| 304 | # otherwise there's no trailer area for writing the confirmed status. |
| 305 | pad = True |
Fabio Utzig | 7c00acd | 2019-01-07 09:54:20 -0200 | [diff] [blame] | 306 | img = image.Image(version=decode_version(version), header_size=header_size, |
Henrik Brix Andersen | 0ce958e | 2020-03-11 14:04:11 +0100 | [diff] [blame] | 307 | pad_header=pad_header, pad=pad, confirm=confirm, |
| 308 | align=int(align), slot_size=slot_size, |
| 309 | max_sectors=max_sectors, overwrite_only=overwrite_only, |
Dominik Ermel | 50820b1 | 2020-12-14 13:16:46 +0000 | [diff] [blame] | 310 | endian=endian, load_addr=load_addr, rom_fixed=rom_fixed, |
| 311 | erased_val=erased_val, save_enctlv=save_enctlv, |
David Vincze | 1a7a690 | 2020-02-18 15:05:16 +0100 | [diff] [blame] | 312 | security_counter=security_counter) |
Fabio Utzig | 7c00acd | 2019-01-07 09:54:20 -0200 | [diff] [blame] | 313 | img.load(infile) |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 314 | key = load_key(key) if key else None |
| 315 | enckey = load_key(encrypt) if encrypt else None |
Fabio Utzig | 7a3b260 | 2019-10-22 09:56:44 -0300 | [diff] [blame] | 316 | if enckey and key: |
| 317 | if ((isinstance(key, keys.ECDSA256P1) and |
| 318 | not isinstance(enckey, keys.ECDSA256P1Public)) |
| 319 | or (isinstance(key, keys.RSA) and |
| 320 | not isinstance(enckey, keys.RSAPublic))): |
| 321 | # FIXME |
Fabio Utzig | 1f50892 | 2020-01-15 11:37:51 -0300 | [diff] [blame] | 322 | raise click.UsageError("Signing and encryption must use the same " |
| 323 | "type of key") |
David Brown | 4878c27 | 2020-03-10 16:23:56 -0600 | [diff] [blame] | 324 | |
| 325 | if pad_sig and hasattr(key, 'pad_sig'): |
| 326 | key.pad_sig = True |
| 327 | |
Ihor Slabkyy | 24d9373 | 2020-03-10 15:33:57 +0200 | [diff] [blame] | 328 | # Get list of custom protected TLVs from the command-line |
| 329 | custom_tlvs = {} |
| 330 | for tlv in custom_tlv: |
| 331 | tag = int(tlv[0], 0) |
| 332 | if tag in custom_tlvs: |
| 333 | raise click.UsageError('Custom TLV %s already exists.' % hex(tag)) |
| 334 | if tag in image.TLV_VALUES.values(): |
| 335 | raise click.UsageError( |
| 336 | 'Custom TLV %s conflicts with predefined TLV.' % hex(tag)) |
| 337 | |
| 338 | value = tlv[1] |
| 339 | if value.startswith('0x'): |
| 340 | if len(value[2:]) % 2: |
| 341 | raise click.UsageError('Custom TLV length is odd.') |
| 342 | custom_tlvs[tag] = bytes.fromhex(value[2:]) |
| 343 | else: |
| 344 | custom_tlvs[tag] = value.encode('utf-8') |
| 345 | |
| 346 | img.create(key, public_key_format, enckey, dependencies, boot_record, |
| 347 | custom_tlvs) |
Fabio Utzig | edbabcf | 2019-10-11 13:03:37 -0300 | [diff] [blame] | 348 | img.save(outfile, hex_addr) |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 349 | |
| 350 | |
| 351 | class AliasesGroup(click.Group): |
| 352 | |
| 353 | _aliases = { |
| 354 | "create": "sign", |
| 355 | } |
| 356 | |
| 357 | def list_commands(self, ctx): |
| 358 | cmds = [k for k in self.commands] |
| 359 | aliases = [k for k in self._aliases] |
| 360 | return sorted(cmds + aliases) |
| 361 | |
| 362 | def get_command(self, ctx, cmd_name): |
| 363 | rv = click.Group.get_command(self, ctx, cmd_name) |
| 364 | if rv is not None: |
| 365 | return rv |
| 366 | if cmd_name in self._aliases: |
| 367 | return click.Group.get_command(self, ctx, self._aliases[cmd_name]) |
| 368 | return None |
| 369 | |
| 370 | |
Fabio Utzig | 25c6a15 | 2019-09-10 12:52:26 -0300 | [diff] [blame] | 371 | @click.command(help='Print imgtool version information') |
| 372 | def version(): |
| 373 | print(imgtool_version) |
| 374 | |
| 375 | |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 376 | @click.command(cls=AliasesGroup, |
| 377 | context_settings=dict(help_option_names=['-h', '--help'])) |
| 378 | def imgtool(): |
| 379 | pass |
| 380 | |
| 381 | |
| 382 | imgtool.add_command(keygen) |
| 383 | imgtool.add_command(getpub) |
Ioannis Konstantelias | 78e57c7 | 2019-11-28 16:06:12 +0200 | [diff] [blame] | 384 | imgtool.add_command(getpriv) |
Fabio Utzig | 4a5477a | 2019-05-27 15:45:08 -0300 | [diff] [blame] | 385 | imgtool.add_command(verify) |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 386 | imgtool.add_command(sign) |
Fabio Utzig | 25c6a15 | 2019-09-10 12:52:26 -0300 | [diff] [blame] | 387 | imgtool.add_command(version) |
Fabio Utzig | e89841d | 2018-12-21 11:19:06 -0200 | [diff] [blame] | 388 | |
| 389 | |
| 390 | if __name__ == '__main__': |
| 391 | imgtool() |