diff options
author | Randall Spangler <rspangler@chromium.org> | 2016-06-21 15:23:32 -0700 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2016-08-10 15:30:33 -0700 |
commit | 939cc3a5c25a3333fadafc7fc341d7e320f72fab (patch) | |
tree | bd7197c8950f44440459fc50531037d50719d3b7 /host | |
parent | 814aaf09ceecddb16a01e1cbe0df4299b83b5699 (diff) | |
download | vboot-939cc3a5c25a3333fadafc7fc341d7e320f72fab.tar.gz |
futility: Use only vboot 2.0 APIs for keyblocks
This refactors futility and the host library to use only vboot 2.0 APIs
to create and verify keyblocks.
BUG=chromium:611535
BRANCH=none
TEST=make runtests
Change-Id: Ia3cc1e24971b94f01bcb4890c8666a3af6f84841
Signed-off-by: Randall Spangler <rspangler@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/356129
Reviewed-by: Daisuke Nojiri <dnojiri@chromium.org>
Diffstat (limited to 'host')
-rw-r--r-- | host/lib/extract_vmlinuz.c | 10 | ||||
-rw-r--r-- | host/lib/host_key2.c | 41 | ||||
-rw-r--r-- | host/lib/host_keyblock.c | 293 | ||||
-rw-r--r-- | host/lib/host_signature2.c | 3 | ||||
-rw-r--r-- | host/lib/include/host_key.h | 21 | ||||
-rw-r--r-- | host/lib/include/host_keyblock.h | 75 | ||||
-rw-r--r-- | host/linktest/main.c | 5 |
7 files changed, 267 insertions, 181 deletions
diff --git a/host/lib/extract_vmlinuz.c b/host/lib/extract_vmlinuz.c index 81d3918e..dddbe561 100644 --- a/host/lib/extract_vmlinuz.c +++ b/host/lib/extract_vmlinuz.c @@ -8,13 +8,13 @@ #include <stdlib.h> #include <string.h> +#include "vb2_struct.h" #include "vboot_struct.h" int ExtractVmlinuz(void *kpart_data, size_t kpart_size, void **vmlinuz_out, size_t *vmlinuz_size) { - uint64_t now = 0; - VbKeyBlockHeader *keyblock = NULL; + size_t now = 0; VbKernelPreambleHeader *preamble = NULL; uint8_t *kblob_data = NULL; uint64_t kblob_size = 0; @@ -23,8 +23,8 @@ int ExtractVmlinuz(void *kpart_data, size_t kpart_size, uint64_t vmlinuz_header_offset = 0; void *vmlinuz = NULL; - keyblock = (VbKeyBlockHeader *)kpart_data; - now += keyblock->key_block_size; + struct vb2_keyblock *keyblock = (struct vb2_keyblock *)kpart_data; + now += keyblock->keyblock_size; if (now > kpart_size) return 1; @@ -55,7 +55,7 @@ int ExtractVmlinuz(void *kpart_data, size_t kpart_size, // the keyblock and preamble sections. vmlinuz_header_offset = vmlinuz_header_address - preamble->body_load_address + - keyblock->key_block_size + + keyblock->keyblock_size + preamble->preamble_size; vmlinuz = malloc(vmlinuz_header_size + kblob_size); diff --git a/host/lib/host_key2.c b/host/lib/host_key2.c index 28f02af5..a100455e 100644 --- a/host/lib/host_key2.c +++ b/host/lib/host_key2.c @@ -103,3 +103,44 @@ struct vb2_private_key *vb2_read_private_key_pem( /* Return the key */ return key; } + +void vb2_init_packed_key(struct vb2_packed_key *key, uint8_t *key_data, + uint32_t key_size) +{ + memset(key, 0, sizeof(*key)); + key->key_offset = vb2_offset_of(key, key_data); + key->key_size = key_size; + key->algorithm = VB2_ALG_COUNT; /* Key not present yet */ +} + +int vb2_copy_packed_key(struct vb2_packed_key *dest, + const struct vb2_packed_key *src) +{ + if (dest->key_size < src->key_size) + return VB2_ERROR_COPY_KEY_SIZE; + + dest->key_size = src->key_size; + dest->algorithm = src->algorithm; + dest->key_version = src->key_version; + memcpy((uint8_t *)vb2_packed_key_data(dest), + vb2_packed_key_data(src), src->key_size); + return VB2_SUCCESS; +} + +struct vb2_packed_key *vb2_read_packed_key(const char *filename) +{ + struct vb2_packed_key *key; + uint32_t file_size; + + if (VB2_SUCCESS != + vb2_read_file(filename, (uint8_t **)&key, &file_size)) { + return NULL; + } + + if (packed_key_looks_ok(key, file_size)) + return key; + + /* Error */ + free(key); + return NULL; +} diff --git a/host/lib/host_keyblock.c b/host/lib/host_keyblock.c index 982013fb..e4497727 100644 --- a/host/lib/host_keyblock.c +++ b/host/lib/host_keyblock.c @@ -5,167 +5,170 @@ * Host functions for verified boot. */ +#include <stdio.h> + #include "2sysincludes.h" +#include "2api.h" #include "2common.h" +#include "2rsa.h" #include "2sha.h" #include "cryptolib.h" #include "host_common.h" +#include "host_key.h" +#include "host_key2.h" #include "host_keyblock.h" +#include "vb2_common.h" +#include "vb2_struct.h" #include "vboot_common.h" - -VbKeyBlockHeader* KeyBlockCreate(const VbPublicKey* data_key, - const VbPrivateKey* signing_key, - uint64_t flags) { - - VbKeyBlockHeader* h; - uint64_t signed_size = sizeof(VbKeyBlockHeader) + data_key->key_size; - uint64_t block_size = (signed_size + VB2_SHA512_DIGEST_SIZE + - (signing_key ? - siglen_map[signing_key->algorithm] : 0)); - uint8_t* data_key_dest; - uint8_t* block_sig_dest; - uint8_t* block_chk_dest; - VbSignature *sigtmp; - - /* Allocate key block */ - h = (VbKeyBlockHeader*)malloc(block_size); - if (!h) - return NULL; - data_key_dest = (uint8_t*)(h + 1); - block_chk_dest = data_key_dest + data_key->key_size; - block_sig_dest = block_chk_dest + VB2_SHA512_DIGEST_SIZE; - - Memcpy(h->magic, KEY_BLOCK_MAGIC, KEY_BLOCK_MAGIC_SIZE); - h->header_version_major = KEY_BLOCK_HEADER_VERSION_MAJOR; - h->header_version_minor = KEY_BLOCK_HEADER_VERSION_MINOR; - h->key_block_size = block_size; - h->key_block_flags = flags; - - /* Copy data key */ - PublicKeyInit(&h->data_key, data_key_dest, data_key->key_size); - PublicKeyCopy(&h->data_key, data_key); - - /* Set up signature structs so we can calculate the signatures */ - SignatureInit(&h->key_block_checksum, block_chk_dest, - VB2_SHA512_DIGEST_SIZE, signed_size); - if (signing_key) - SignatureInit(&h->key_block_signature, block_sig_dest, - siglen_map[signing_key->algorithm], signed_size); - else - Memset(&h->key_block_signature, 0, sizeof(VbSignature)); - - /* Calculate checksum */ - struct vb2_signature *chk = vb2_sha512_signature((uint8_t*)h, signed_size); - SignatureCopy(&h->key_block_checksum, (VbSignature *)chk); - free(chk); - - /* Calculate signature */ - if (signing_key) { - sigtmp = CalculateSignature((uint8_t*)h, signed_size, signing_key); - SignatureCopy(&h->key_block_signature, sigtmp); - free(sigtmp); - } - - /* Return the header */ - return h; +struct vb2_keyblock *vb2_create_keyblock( + const struct vb2_packed_key *data_key, + const struct vb2_private_key *signing_key, + uint32_t flags) +{ + /* Allocate key block */ + uint32_t signed_size = sizeof(struct vb2_keyblock) + data_key->key_size; + uint32_t sig_data_size = + (signing_key ? vb2_rsa_sig_size(signing_key->sig_alg) : 0); + uint32_t block_size = + signed_size + VB2_SHA512_DIGEST_SIZE + sig_data_size; + struct vb2_keyblock *h = (struct vb2_keyblock *)calloc(block_size, 1); + if (!h) + return NULL; + + uint8_t *data_key_dest = (uint8_t *)(h + 1); + uint8_t *block_chk_dest = data_key_dest + data_key->key_size; + uint8_t *block_sig_dest = block_chk_dest + VB2_SHA512_DIGEST_SIZE; + + memcpy(h->magic, KEY_BLOCK_MAGIC, KEY_BLOCK_MAGIC_SIZE); + h->header_version_major = KEY_BLOCK_HEADER_VERSION_MAJOR; + h->header_version_minor = KEY_BLOCK_HEADER_VERSION_MINOR; + h->keyblock_size = block_size; + h->keyblock_flags = flags; + + /* Copy data key */ + vb2_init_packed_key(&h->data_key, data_key_dest, data_key->key_size); + vb2_copy_packed_key(&h->data_key, data_key); + + /* Set up signature structs so we can calculate the signatures */ + vb2_init_signature(&h->keyblock_hash, block_chk_dest, + VB2_SHA512_DIGEST_SIZE, signed_size); + if (signing_key) { + vb2_init_signature(&h->keyblock_signature, block_sig_dest, + sig_data_size, signed_size); + } else { + memset(&h->keyblock_signature, 0, + sizeof(h->keyblock_signature)); + } + + /* Calculate hash */ + struct vb2_signature *chk = + vb2_sha512_signature((uint8_t*)h, signed_size); + vb2_copy_signature(&h->keyblock_hash, chk); + free(chk); + + /* Calculate signature */ + if (signing_key) { + struct vb2_signature *sigtmp = + vb2_calculate_signature((uint8_t*)h, + signed_size, + signing_key); + vb2_copy_signature(&h->keyblock_signature, sigtmp); + free(sigtmp); + } + + /* Return the header */ + return h; } -/* TODO(gauravsh): This could easily be integrated into KeyBlockCreate() +/* TODO(gauravsh): This could easily be integrated into the function above * since the code is almost a mirror - I have kept it as such to avoid changing * the existing interface. */ -VbKeyBlockHeader* KeyBlockCreate_external(const VbPublicKey* data_key, - const char* signing_key_pem_file, - uint64_t algorithm, - uint64_t flags, - const char* external_signer) { - VbKeyBlockHeader* h; - uint64_t signed_size = sizeof(VbKeyBlockHeader) + data_key->key_size; - uint64_t block_size = (signed_size + VB2_SHA512_DIGEST_SIZE + - siglen_map[algorithm]); - uint8_t* data_key_dest; - uint8_t* block_sig_dest; - uint8_t* block_chk_dest; - VbSignature *sigtmp; - - /* Allocate key block */ - h = (VbKeyBlockHeader*)malloc(block_size); - if (!h) - return NULL; - if (!signing_key_pem_file || !data_key || !external_signer) - return NULL; - - data_key_dest = (uint8_t*)(h + 1); - block_chk_dest = data_key_dest + data_key->key_size; - block_sig_dest = block_chk_dest + VB2_SHA512_DIGEST_SIZE; - - Memcpy(h->magic, KEY_BLOCK_MAGIC, KEY_BLOCK_MAGIC_SIZE); - h->header_version_major = KEY_BLOCK_HEADER_VERSION_MAJOR; - h->header_version_minor = KEY_BLOCK_HEADER_VERSION_MINOR; - h->key_block_size = block_size; - h->key_block_flags = flags; - - /* Copy data key */ - PublicKeyInit(&h->data_key, data_key_dest, data_key->key_size); - PublicKeyCopy(&h->data_key, data_key); - - /* Set up signature structs so we can calculate the signatures */ - SignatureInit(&h->key_block_checksum, block_chk_dest, - VB2_SHA512_DIGEST_SIZE, signed_size); - SignatureInit(&h->key_block_signature, block_sig_dest, - siglen_map[algorithm], signed_size); - - /* Calculate checksum */ - struct vb2_signature *chk = vb2_sha512_signature((uint8_t*)h, signed_size); - SignatureCopy(&h->key_block_checksum, (VbSignature *)chk); - free(chk); - - /* Calculate signature */ - sigtmp = CalculateSignature_external((uint8_t*)h, signed_size, - signing_key_pem_file, algorithm, - external_signer); - SignatureCopy(&h->key_block_signature, sigtmp); - free(sigtmp); - - /* Return the header */ - return h; +struct vb2_keyblock *vb2_create_keyblock_external( + const struct vb2_packed_key *data_key, + const char *signing_key_pem_file, + uint32_t algorithm, + uint32_t flags, + const char *external_signer) +{ + uint32_t signed_size = sizeof(struct vb2_keyblock) + data_key->key_size; + uint32_t sig_data_size = vb2_rsa_sig_size(algorithm); + uint32_t block_size = + signed_size + VB2_SHA512_DIGEST_SIZE + sig_data_size; + + /* Allocate key block */ + struct vb2_keyblock *h = (struct vb2_keyblock *)calloc(block_size, 1); + if (!h) + return NULL; + if (!signing_key_pem_file || !data_key || !external_signer) + return NULL; + + uint8_t *data_key_dest = (uint8_t *)(h + 1); + uint8_t *block_chk_dest = data_key_dest + data_key->key_size; + uint8_t *block_sig_dest = block_chk_dest + VB2_SHA512_DIGEST_SIZE; + + memcpy(h->magic, KEY_BLOCK_MAGIC, KEY_BLOCK_MAGIC_SIZE); + h->header_version_major = KEY_BLOCK_HEADER_VERSION_MAJOR; + h->header_version_minor = KEY_BLOCK_HEADER_VERSION_MINOR; + h->keyblock_size = block_size; + h->keyblock_flags = flags; + + /* Copy data key */ + vb2_init_packed_key(&h->data_key, data_key_dest, data_key->key_size); + vb2_copy_packed_key(&h->data_key, data_key); + + /* Set up signature structs so we can calculate the signatures */ + vb2_init_signature(&h->keyblock_hash, block_chk_dest, + VB2_SHA512_DIGEST_SIZE, signed_size); + vb2_init_signature(&h->keyblock_signature, block_sig_dest, + sig_data_size, signed_size); + + /* Calculate checksum */ + struct vb2_signature *chk = + vb2_sha512_signature((uint8_t*)h, signed_size); + vb2_copy_signature(&h->keyblock_hash, chk); + free(chk); + + /* Calculate signature */ +struct vb2_signature *sigtmp = (struct vb2_signature *) + CalculateSignature_external((uint8_t*)h, signed_size, + signing_key_pem_file, algorithm, + external_signer); + free(sigtmp); + + /* Return the header */ + return h; } -/* Read a key block from a .keyblock file. Caller owns the returned - * pointer, and must free it with free(). - * - * Returns NULL if error. */ -VbKeyBlockHeader* KeyBlockRead(const char* filename) { - - VbKeyBlockHeader* block; - uint64_t file_size; - - block = (VbKeyBlockHeader*)ReadFile(filename, &file_size); - if (!block) { - VBDEBUG(("Error reading key block file: %s\n", filename)); - return NULL; - } - - /* Verify the hash of the key block, since we can do that without - * the public signing key. */ - if (0 != KeyBlockVerify(block, file_size, NULL, 1)) { - VBDEBUG(("Invalid key block file: %s\n", filename)); - free(block); - return NULL; - } - - return block; +struct vb2_keyblock *vb2_read_keyblock(const char *filename) +{ + uint8_t workbuf[VB2_WORKBUF_RECOMMENDED_SIZE]; + struct vb2_workbuf wb; + vb2_workbuf_init(&wb, workbuf, sizeof(workbuf)); + + struct vb2_keyblock *block; + uint32_t file_size; + if (VB2_SUCCESS != + vb2_read_file(filename, (uint8_t **)&block, &file_size)) { + fprintf(stderr, "Error reading key block file: %s\n", filename); + return NULL; + } + + /* Verify the hash of the key block, since we can do that without + * the public signing key. */ + if (VB2_SUCCESS != vb2_verify_keyblock_hash(block, file_size, &wb)) { + fprintf(stderr, "Invalid key block file: %s\n", filename); + free(block); + return NULL; + } + + return block; } -/* Write a key block to a file in .keyblock format. */ -int KeyBlockWrite(const char* filename, const VbKeyBlockHeader* key_block) { - - if (0 != WriteFile(filename, key_block, key_block->key_block_size)) { - VBDEBUG(("KeyBlockWrite() error writing key block\n")); - return 1; - } - - return 0; +int vb2_write_keyblock(const char *filename, + const struct vb2_keyblock *keyblock) +{ + return vb2_write_file(filename, keyblock, keyblock->keyblock_size); } diff --git a/host/lib/host_signature2.c b/host/lib/host_signature2.c index e07f3d06..8925e6a7 100644 --- a/host/lib/host_signature2.c +++ b/host/lib/host_signature2.c @@ -44,7 +44,8 @@ struct vb2_signature *vb2_alloc_signature(uint32_t sig_size, void vb2_init_signature(struct vb2_signature *sig, uint8_t *sig_data, uint32_t sig_size, uint32_t data_size) { - sig->sig_offset = OffsetOf(sig, sig_data); + memset(sig, 0, sizeof(*sig)); + sig->sig_offset = vb2_offset_of(sig, sig_data); sig->sig_size = sig_size; sig->data_size = data_size; } diff --git a/host/lib/include/host_key.h b/host/lib/include/host_key.h index 25b7f767..d355e228 100644 --- a/host/lib/include/host_key.h +++ b/host/lib/include/host_key.h @@ -48,12 +48,33 @@ struct vb2_private_key *vb2_read_private_key(const char *filename); VbPublicKey* PublicKeyAlloc(uint64_t key_size, uint64_t algorithm, uint64_t version); +/** + * Initialize a packed key structure. + * + * @param key Structure to initialize + * @param key_data Pointer to key data (following the structure) + * @param key_size Size of key + */ +void vb2_init_packed_key(struct vb2_packed_key *key, uint8_t *key_data, + uint32_t key_size); + +/** + * Copy a packed key. + * + * @param dest Destination packed key + * @param src Source packed key + * + * @return VB2_SUCCESS, or non-zero if error. + */ +int vb2_copy_packed_key(struct vb2_packed_key *dest, + const struct vb2_packed_key *src); /* Read a public key from a .vbpubk file. Caller owns the returned * pointer, and must free it with Free(). * * Returns NULL if error. */ VbPublicKey* PublicKeyRead(const char* filename); +struct vb2_packed_key *vb2_read_packed_key(const char *filename); /* Return true if the packed (public) key struct appears correct. */ int packed_key_looks_ok(const struct vb2_packed_key *key, uint32_t size); diff --git a/host/lib/include/host_keyblock.h b/host/lib/include/host_keyblock.h index ea88f19b..21257965 100644 --- a/host/lib/include/host_keyblock.h +++ b/host/lib/include/host_keyblock.h @@ -11,35 +11,60 @@ #include "host_key.h" #include "vboot_struct.h" +struct vb2_keyblock; -/* Create a key block header containing [data_key] and [flags], signed - * by private key the file [signing_key_pem_file] and algorithm [algorithm] - * using the external signer program [external_signer] for all private key - * operations. - * Caller owns the returned pointer, and must free - * it with Free(). */ -VbKeyBlockHeader* KeyBlockCreate_external(const VbPublicKey* data_key, - const char* signing_key_pem_file, - uint64_t algorithm, - uint64_t flags, - const char* external_signer); - -/* Create a key block header containing [data_key] and [flags], signed - * by [signing_key]. Caller owns the returned pointer, and must free - * it with Free(). */ -VbKeyBlockHeader* KeyBlockCreate(const VbPublicKey* data_key, - const VbPrivateKey* signing_key, - uint64_t flags); - +/** + * Create a keyblock header + * + * @param data_key Data key to store in keyblock + * @param signing_key Key to sign keyblock with. May be NULL if keyblock + * only needs a hash digest. + * @param flags Keyblock flags + * + * @return The keyblock, or NULL if error. Caller must free() it. + */ +struct vb2_keyblock *vb2_create_keyblock( + const struct vb2_packed_key *data_key, + const struct vb2_private_key *signing_key, + uint32_t flags); -/* Read a key block from a .keyblock file. Caller owns the returned - * pointer, and must free it with Free(). +/** + * Create a keyblock header using an external signer for all private key + * operations. + * + * @param data_key Data key to store in keyblock + * @param signing_key_pem_file Filename of private key + * @param algorithm Signing algorithm index + * @param flags Keyblock flags + * @param external_signer Path to external signer program * - * Returns NULL if error. */ -VbKeyBlockHeader* KeyBlockRead(const char* filename); + * @return The keyblock, or NULL if error. Caller must free() it. + */ +struct vb2_keyblock *vb2_create_keyblock_external( + const struct vb2_packed_key *data_key, + const char *signing_key_pem_file, + uint32_t algorithm, + uint32_t flags, + const char *external_signer); +/** + * Read a keyblock from a .keyblock file. + * + * @param filename File to read keyblock from + * + * @return The keyblock, or NULL if error. Caller must free() it. + */ +struct vb2_keyblock *vb2_read_keyblock(const char *filename); -/* Write a key block to a file in .keyblock format. */ -int KeyBlockWrite(const char* filename, const VbKeyBlockHeader* key_block); +/** + * Write a keyblock to a file in .keyblock format. + * + * @param filename Filename to write + * @param keyblock Keyblock to write + * + * @return VB2_SUCCESS, or non-zero if error. + */ +int vb2_write_keyblock(const char *filename, + const struct vb2_keyblock *keyblock); #endif /* VBOOT_REFERENCE_HOST_KEYBLOCK_H_ */ diff --git a/host/linktest/main.c b/host/linktest/main.c index 1631ec37..5d56b892 100644 --- a/host/linktest/main.c +++ b/host/linktest/main.c @@ -23,11 +23,6 @@ int main(void) PublicKeyReadKeyb(0, 0, 0); PublicKeyWrite(0, 0); - /* host_keyblock.h */ - KeyBlockCreate(0, 0, 0); - KeyBlockRead(0); - KeyBlockWrite(0, 0); - /* host_misc.h */ ReadFile(0, 0); WriteFile(0, 0, 0); |