Commit 43cda33c authored by David Brown's avatar David Brown Committed by David Brown
Browse files

Move key_id from header into TLV

Remove the key_id field from the image header.  There are two problems
with this field.  First, it is only an integer offset, and so causes an
unnecessarily tight coupling between the particular keys built into the
bootloader, and the key that is used to sign.  Second, it makes the
key_id part of the image header, which is included in the signature.
This makes it impossible to later sign the image with a different
signature.

Instead of the key-id, add a TLV KEYHASH entry.  This will hold the
SHA256 of the public key that the signature is against.  Each signature
placed in the TLV should be preceeded by this entry to indicate the
public key used.

The signature check will check each signature, and if the KEYHASH is
known and the signature type is supported, it will be checked.  As long
as at least one signature is considered valid, the image will be
considered signed.  This also allows the image to be signed with
multiple signatures to support having different devices with possibly
different keys compiled into the bootloaders.

Based on work by Marko Kiiskila <marko@runtime.io>

Signed-off-by: default avatarMarko Kiiskila <marko@runtime.io>
Signed-off-by: default avatarDavid Brown <david.brown@linaro.org>
parent 76528c44
Loading
Loading
Loading
Loading
+2 −6
Changes for boot/bootutil/include/bootutil/image.h: 2 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -37,12 +37,7 @@ struct flash_area;
 * Image header flags.
 */
#define IMAGE_F_PIC                      0x00000001 /* Not supported. */
#define IMAGE_F_SHA256                   0x00000002 /* Hash TLV is present */
#define IMAGE_F_PKCS15_RSA2048_SHA256    0x00000004 /* PKCS15 w/RSA and SHA */
#define IMAGE_F_ECDSA224_SHA256          0x00000008 /* ECDSA224 over SHA256 */
#define IMAGE_F_NON_BOOTABLE             0x00000010 /* Split image app. */
#define IMAGE_F_ECDSA256_SHA256          0x00000020 /* ECDSA256 over SHA256 */
#define IMAGE_F_PKCS1_PSS_RSA2048_SHA256 0x00000040 /* PKCS1 PSS */

/*
 * ECSDA224 is with NIST P-224
@@ -52,6 +47,7 @@ struct flash_area;
/*
 * Image trailer TLV types.
 */
#define IMAGE_TLV_KEYHASH           0x01   /* hash of the public key */
#define IMAGE_TLV_SHA256            0x10   /* SHA256 of image hdr and body */
#define IMAGE_TLV_RSA2048           0x20   /* RSA2048 of hash output */
#define IMAGE_TLV_ECDSA224          0x21   /* ECDSA of hash output */
@@ -71,7 +67,7 @@ struct image_version {
struct image_header {
    uint32_t ih_magic;
    uint16_t ih_tlv_size; /* Combined size of trailing TLVs (bytes). */
    uint8_t  ih_key_id;   /* Which key image is signed with (0xff=unsigned). */
    uint8_t  _pad0;
    uint8_t  _pad1;
    uint16_t ih_hdr_size; /* Size of image header (bytes). */
    uint16_t _pad2;
+95 −74
Changes for boot/bootutil/src/image_validate.c: 95 added lines, 74 removed lines.
Original line number Diff line number Diff line
@@ -87,6 +87,53 @@ bootutil_img_hash(struct image_header *hdr, const struct flash_area *fap,
    return 0;
}

/*
 * Currently, we only support being able to verify one type of
 * signature, because there is a single verification function that we
 * call.  List the type of TLV we are expecting.  If we aren't
 * configured for any signature, don't define this macro.
 */
#if defined(MCUBOOT_SIGN_RSA)
#    define EXPECTED_SIG_TLV IMAGE_TLV_RSA2048
#    define EXPECTED_SIG_LEN(x) ((x) == 256) /* 2048 bits */
#    if defined(MCUBOOT_SIGN_EC) || defined(MCUBOOT_SIGN_EC256)
#        error "Multiple signature types not yet supported"
#    endif
#elif defined(MCUBOOT_SIGN_EC)
#    define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA224
#    define EXPECTED_SIG_LEN(x) ((x) >= 64) /* oids + 2 * 28 bytes */
#    if defined(MCUBOOT_SIGN_EC256)
#        error "Multiple signature types not yet supported"
#    endif
#elif defined(MCUBOOT_SIGN_EC256)
#    define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA256
#    define EXPECTED_SIG_LEN(x) ((x) >= 72) /* oids + 2 * 32 bytes */
#endif

#ifdef EXPECTED_SIG_TLV
static int
bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
{
    bootutil_sha256_context sha256_ctx;
    int i;
    const struct bootutil_key *key;
    uint8_t hash[32];

    assert(keyhash_len == 32);

    for (i = 0; i < bootutil_key_cnt; i++) {
        key = &bootutil_keys[i];
        bootutil_sha256_init(&sha256_ctx);
        bootutil_sha256_update(&sha256_ctx, key->key, *key->len);
        bootutil_sha256_finish(&sha256_ctx, hash);
        if (!memcmp(hash, keyhash, keyhash_len)) {
            return i;
        }
    }
    return -1;
}
#endif

/*
 * Verify the integrity of the image.
 * Return non-zero if image could not be validated/does not validate.
@@ -98,42 +145,16 @@ bootutil_img_validate(struct image_header *hdr, const struct flash_area *fap,
{
    uint32_t off;
    uint32_t size;
    uint32_t sha_off = 0;
#if defined(MCUBOOT_SIGN_RSA) || defined(MCUBOOT_SIGN_EC) || \
    defined(MCUBOOT_SIGN_EC256)
    uint32_t sig_off = 0;
    uint32_t sig_len = 0;
    int sha256_valid = 0;
#ifdef EXPECTED_SIG_TLV
    int valid_signature = 0;
    int key_id = -1;
#endif
    struct image_tlv tlv;
    uint8_t buf[256];
    uint8_t hash[32];
    int rc;

#ifdef MCUBOOT_SIGN_RSA
#ifdef MCUBOOT_RSA_PKCS1_15
    if ((hdr->ih_flags & IMAGE_F_PKCS15_RSA2048_SHA256) == 0) {
        return -1;
    }
#else
    if ((hdr->ih_flags & IMAGE_F_PKCS1_PSS_RSA2048_SHA256) == 0) {
        return -1;
    }
#endif
#endif
#ifdef MCUBOOT_SIGN_EC
    if ((hdr->ih_flags & IMAGE_F_ECDSA224_SHA256) == 0) {
        return -1;
    }
#endif
#ifdef MCUBOOT_SIGN_EC256
    if ((hdr->ih_flags & IMAGE_F_ECDSA256_SHA256) == 0) {
        return -1;
    }
#endif
    if ((hdr->ih_flags & IMAGE_F_SHA256) == 0) {
        return -1;
    }

    rc = bootutil_img_hash(hdr, fap, tmp_buf, tmp_buf_sz, hash,
                           seed, seed_len);
    if (rc) {
@@ -148,81 +169,81 @@ bootutil_img_validate(struct image_header *hdr, const struct flash_area *fap,
    off = hdr->ih_img_size + hdr->ih_hdr_size;
    size = off + hdr->ih_tlv_size;

    /*
     * Traverse through all of the TLVs, performing any checks we know
     * and are able to do.
     */
    for (; off < size; off += sizeof(tlv) + tlv.it_len) {
        rc = flash_area_read(fap, off, &tlv, sizeof tlv);
        if (rc) {
            return rc;
        }

        if (tlv.it_type == IMAGE_TLV_SHA256) {
            /*
             * Verify the SHA256 image hash.  This must always be
             * present.
             */
            if (tlv.it_len != sizeof(hash)) {
                return -1;
            }
            sha_off = off + sizeof(tlv);
        }
#ifdef MCUBOOT_SIGN_RSA
        if (tlv.it_type == IMAGE_TLV_RSA2048) {
            if (tlv.it_len != 256) { /* 2048 bits */
                return -1;
            }
            sig_off = off + sizeof(tlv);
            sig_len = tlv.it_len;
        }
#endif
#ifdef MCUBOOT_SIGN_EC
        if (tlv.it_type == IMAGE_TLV_ECDSA224) {
            if (tlv.it_len < 64) { /* oids + 2 * 28 bytes */
                return -1;
            }
            sig_off = off + sizeof(tlv);
            sig_len = tlv.it_len;
            rc = flash_area_read(fap, off + sizeof(tlv), buf, sizeof hash);
            if (rc) {
                    return rc;
            }
#endif
#ifdef MCUBOOT_SIGN_EC256
        if (tlv.it_type == IMAGE_TLV_ECDSA256) {
            if (tlv.it_len < 72) { /* oids + 2 * 32 bytes */
            if (memcmp(hash, buf, sizeof(hash))) {
                    return -1;
            }
            sig_off = off + sizeof(tlv);
            sig_len = tlv.it_len;
        }
#endif
    }
    if (hdr->ih_flags & IMAGE_F_SHA256) {
        if (!sha_off) {

            sha256_valid = 1;
#ifdef EXPECTED_SIG_TLV
        } else if (tlv.it_type == IMAGE_TLV_KEYHASH) {
            /*
             * Header said there should be hash TLV, no TLV found.
             * Determine which key we should be checking.
             */
            if (tlv.it_len > 32) {
                return -1;
            }
        rc = flash_area_read(fap, sha_off, buf, sizeof hash);
            rc = flash_area_read(fap, off + sizeof tlv, buf, tlv.it_len);
            if (rc) {
                return rc;
            }
        if (memcmp(hash, buf, sizeof(hash))) {
            return -1;
        }
    }
#if defined(MCUBOOT_SIGN_RSA) || defined(MCUBOOT_SIGN_EC) || \
    defined(MCUBOOT_SIGN_EC256)
    if (!sig_off) {
            key_id = bootutil_find_key(buf, tlv.it_len);
            /*
         * Header said there should be PKCS1.v5 signature, no TLV
         * found.
             * The key may not be found, which is acceptable.  There
             * can be multiple signatures, each preceded by a key.
             */
        } else if (tlv.it_type == EXPECTED_SIG_TLV) {
            /* Ignore this signature if it is out of bounds. */
            if (key_id < 0 || key_id >= bootutil_key_cnt) {
                key_id = -1;
                continue;
            }
            if (!EXPECTED_SIG_LEN(tlv.it_len) || tlv.it_len > sizeof(buf)) {
                return -1;
            }
    rc = flash_area_read(fap, sig_off, buf, sig_len);
            rc = flash_area_read(fap, off + sizeof(tlv), buf, tlv.it_len);
            if (rc) {
                return -1;
            }
            rc = bootutil_verify_sig(hash, sizeof(hash), buf, tlv.it_len, key_id);
            if (rc == 0) {
                valid_signature = 1;
            }
            key_id = -1;
#endif
        }
    }

    if (hdr->ih_key_id >= bootutil_key_cnt) {
    if (!sha256_valid) {
        return -1;
    }
    rc = bootutil_verify_sig(hash, sizeof(hash), buf, sig_len, hdr->ih_key_id);
    if (rc) {

#ifdef EXPECTED_SIG_TLV
    if (!valid_signature) {
        return -1;
    }
#endif

    return 0;
}
+9 −8
Changes for scripts/imgtool/image.py: 9 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -12,14 +12,10 @@ IMAGE_HEADER_SIZE = 32
# Image header flags.
IMAGE_F = {
        'PIC':                   0x0000001,
        'SHA256':                0x0000002,
        'PKCS15_RSA2048_SHA256': 0x0000004,
        'ECDSA224_SHA256':       0x0000008,
        'NON_BOOTABLE':          0x0000010,
        'ECDSA256_SHA256':       0x0000020,
        'PKCS1_PSS_RSA2048_SHA256': 0x0000040, }
        'NON_BOOTABLE':          0x0000010, }

TLV_VALUES = {
        'KEYHASH': 0x01,
        'SHA256': 0x10,
        'RSA2048': 0x20,
        'ECDSA224': 0x21,
@@ -106,6 +102,12 @@ class Image():
        tlv.add('SHA256', digest)

        if key is not None:
            pub = key.get_public_bytes()
            sha = hashlib.sha256()
            sha.update(pub)
            pubbytes = sha.digest()
            tlv.add('KEYHASH', pubbytes)

            sig = key.sign(self.payload)
            tlv.add(key.sig_tlv(), sig)

@@ -120,10 +122,9 @@ class Image():
        flags = 0
        tlvsz = 0
        if key is not None:
            flags |= IMAGE_F[key.sig_type()]
            tlvsz += TLV_HEADER_SIZE + key.sig_len()

        flags |= IMAGE_F['SHA256']
        tlvsz += 4 + hashlib.sha256().digest_size
        tlvsz += 4 + hashlib.sha256().digest_size

        fmt = ('<' +
−7
Changes for sim/Cargo.lock: 0 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -2,7 +2,6 @@
name = "bootsim"
version = "0.1.0"
dependencies = [
 "bitflags 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)",
 "docopt 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
 "env_logger 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
 "gcc 0.3.51 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -57,11 +56,6 @@ dependencies = [
 "byteorder 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "bitflags"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "byteorder"
version = "1.1.0"
@@ -374,7 +368,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum backtrace 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "72f9b4182546f4b04ebc4ab7f84948953a118bd6021a1b6a6c909e3e94f6be76"
"checksum backtrace-sys 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "3a0d842ea781ce92be2bf78a9b38883948542749640b8378b3b2f03d1fd9f1ff"
"checksum base64 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "30e93c03064e7590d0466209155251b90c22e37fab1daf2771582598b5827557"
"checksum bitflags 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4efd02e230a02e18f92fc2735f44597385ed02ad8f831e7c1c1156ee5e1ab3a5"
"checksum byteorder 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ff81738b726f5d099632ceaffe7fb65b90212e8dce59d518729e7e8634032d3d"
"checksum cfg-if 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d4c819a1287eb618df47cc647173c5c4c66ba19d888a6e50d605672aed3140de"
"checksum coco 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c06169f5beb7e31c7c67ebf5540b8b472d23e3eade3b2ec7d1f5b504a85f91bd"
−1
Changes for sim/Cargo.toml: 0 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -21,7 +21,6 @@ log = "0.3"
env_logger = "0.4"
simflash = { path = "simflash" }
mcuboot-sys = { path = "mcuboot-sys" }
bitflags = "0.9"
ring = { version = "0.11", features = ["rsa_signing"] }
untrusted = "0.5"
pem = "0.4"
Loading