Commit 1d5bea1c authored by David Brown's avatar David Brown Committed by David Brown
Browse files

imgtool: Add support for password protected RSA keys

The keygen command allows the `-p` argument which will prompt for a
password, and protect the private key with this password.  When loading
keys, it will prompt for a password if it detects a password protected
key.

Signed-off-by: default avatarDavid Brown <david.brown@linaro.org>
parent 47b77c52
Loading
Loading
Loading
Loading
+28 −3
Changes for scripts/imgtool.py: 28 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -15,13 +15,28 @@
# limitations under the License.

import argparse
import getpass
from imgtool import keys
from imgtool import image
from imgtool import version
import sys

def gen_rsa2048(args):
    keys.RSA2048.generate().export_private(args.key)
    if args.password:
        while True:
            passwd = getpass.getpass("Enter key passphrase: ")
            passwd2 = getpass.getpass("Reenter passphrase: ")
            if passwd == passwd2:
                break
            print("Passwords do not match, try again")

        # Password must be bytes, always use UTF-8 for consistent
        # encoding.
        return passwd.encode('utf-8')
    else:
        passwd = None
    keys.RSA2048.generate().export_private(path=args.key, passwd=passwd)

def gen_ecdsa_p256(args):
    keys.ECDSA256P1.generate().export_private(args.key)
def gen_ecdsa_p224(args):
@@ -38,8 +53,16 @@ def do_keygen(args):
        raise argparse.ArgumentTypeError(msg)
    keygens[args.type](args)

def do_getpub(args):
def load_key(args):
    key = keys.load(args.key)
    if key is not None:
        return key
    passwd = getpass.getpass("Enter key passphrase: ")
    passwd = passwd.encode('utf-8')
    return keys.load(args.key, passwd)

def do_getpub(args):
    key = load_key(args)
    if args.lang == 'c':
        key.emit_c()
    elif args.lang == 'rust':
@@ -53,7 +76,7 @@ def do_sign(args):
            header_size=args.header_size,
            included_header=args.included_header,
            pad=args.pad)
    key = keys.load(args.key) if args.key else None
    key = load_key(args) if args.key else None
    img.sign(key)

    if args.pad:
@@ -87,6 +110,8 @@ def args():
    keygenp.add_argument('-k', '--key', metavar='filename', required=True)
    keygenp.add_argument('-t', '--type', metavar='type',
            choices=keygens.keys(), required=True)
    keygenp.add_argument('-p', '--password', default=False, action='store_true',
            help='Prompt for password to protect key')

    getpub = subs.add_parser('getpub', help='Get public key from keypair')
    getpub.add_argument('-k', '--key', metavar='filename', required=True)
+6 −3
Changes for scripts/imgtool/keys/__init__.py: 6 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -102,10 +102,13 @@ def load(path, passwd=None):
                raw_pem,
                password=passwd,
                backend=default_backend())
    # This is a bit nonsensical of an exception, but it is what
    # cryptography seems to currently raise if the password is needed.
    except TypeError:
    # Unfortunately, the crypto library raises unhelpful exceptions,
    # so we have to look at the text.
    except TypeError as e:
        msg = str(e)
        if "private key is encrypted" in msg:
            return None
        raise e
    except ValueError:
        # This seems to happen if the key is a public key, let's try
        # loading it as a public key.
+3
Changes for scripts/imgtool/keys/rsa.py: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -50,6 +50,9 @@ class RSA2048Public(KeyClass):
    def sig_tlv(self):
        return "RSA2048"

    def sig_len(self):
        return 256

class RSA2048(RSA2048Public):
    """
    Wrapper around an 2048-bit RSA key, with imgtool support.