Commit 4e2cdfe8 authored by Fabio Utzig's avatar Fabio Utzig Committed by Fabio Utzig
Browse files

imgtool: change getpub exporting format parameter



Update a previous PR were PEM exporting was added to the `--lang`
parameter, even though PEM is not a source code language per se.

This PR adds `--encoding/-e` to `getpub` command, for exporting
in formats other than a language source code. `--lang` is left with
a deprecation message, so it could be removed in a future version.
The default behavior of exporting source code in C was preserved.

Signed-off-by: default avatarFabio Utzig <utzig@apache.org>
parent 09cca381
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -152,9 +152,9 @@ occurs and the information is spread across multiple areas.
or `ed25519`. This will generate a keypair or private key.

To extract the public key in source file form, use
`imgtool getpub -k <input.pem> -l <lang>`, where lang can be one of `c` or
`rust` (defaults to `c`). To extract a public key in PEM form, use
`imgtool getpub -k <input.pem> -l pem`.
`imgtool getpub -k <input.pem> -e <encoding>`, where `encoding` can be one of
`lang-c` or `lang-rust` (defaults to `lang-c`). To extract a public key in PEM
format, use `imgtool getpub -k <input.pem> -e pem`.

If using AES-KW, follow the steps in the next section to generate the
required keys.
+21 −8
Original line number Diff line number Diff line
@@ -59,7 +59,8 @@ def gen_x25519(keyfile, passwd):
    keys.X25519.generate().export_private(path=keyfile, passwd=passwd)


valid_langs = ['c', 'rust', 'pem']
valid_langs = ['c', 'rust']
valid_encodings = ['lang-c', 'lang-rust', 'pem']
keygens = {
    'rsa-2048':   gen_rsa2048,
    'rsa-3072':   gen_rsa3072,
@@ -113,22 +114,34 @@ def keygen(type, key, password):
    keygens[type](key, password)


@click.option('-l', '--lang', metavar='lang', default=valid_langs[0],
              type=click.Choice(valid_langs))
@click.option('-l', '--lang', metavar='lang',
              type=click.Choice(valid_langs),
              help='This option is deprecated. Please use the '
                   '`--encoding` option. '
                   'Valid langs: {}'.format(', '.join(valid_langs)))
@click.option('-e', '--encoding', metavar='encoding',
              type=click.Choice(valid_encodings),
              help='Valid encodings: {}'.format(', '.join(valid_encodings)))
@click.option('-k', '--key', metavar='filename', required=True)
@click.command(help='Dump public key from keypair')
def getpub(key, lang):
def getpub(key, encoding, lang):
    if encoding and lang:
        raise click.UsageError('Please use only one of `--encoding/-e` or `--lang/-l`')
    elif not encoding and not lang:
        # Preserve old behavior defaulting to `c`. If `lang` is removed,
        # `default=valid_encodings[0]` should be added to `-e` param.
        lang = valid_langs[0]
    key = load_key(key)
    if key is None:
        print("Invalid passphrase")
    elif lang == 'c':
    elif lang == 'c' or encoding == 'lang-c':
        key.emit_c_public()
    elif lang == 'rust':
    elif lang == 'rust' or encoding == 'lang-rust':
        key.emit_rust_public()
    elif lang == 'pem':
    elif encoding == 'pem':
        key.emit_public_pem()
    else:
        raise ValueError("BUG: should never get here!")
        raise click.UsageError()


@click.option('--minimal', default=False, is_flag=True,