mirror of
https://github.com/Telecominfraproject/OpenCellular.git
synced 2025-11-24 10:14:55 +00:00
Originally, we designed the vboot data structures so that some of them
had sub-structures. Then the variable-length data for each of the
structures was at the end. So:
struct vb2_keyblock {
struct vb2_packed_key
struct vb2_signature
}
// Followed by variable-length data for keyblock
// Followed by variable-length data for packed key
// Followed by variable-length data for signature
This had the weird side effect that the header and data for the
sub-structs were not contiguous. That wasn't too bad before, but it
gets more complicated with the new data structures. Each structure
now can also have a description. And keyblocks can have a list of
signatures.
Structures also couldn't really know their own size, since a
sub-struct might have a 20-byte header, but then 2K of other data in
between that and the data for the sub-struct itself.
So, un-nest all the data structures. That is, the keyblock now
contains the offset of the signature struct, rather than the signature
struct itself. And then all the variable-length data for each struct
immediately follows the struct itself. So:
struct vb2_keyblock2 {
// Offset of packed key
// Offset of first signature
}
// Followed by variable-length data for keyblock
struct vb2_packed_key
// Followed by variable-length data for packed key
struct vb2_signature2
// Followed by variable-length data for signature (desc, sig data)
Verifying and traversing these objects is much more straightforward.
And each struct can now know its own size.
This first change rearranges the structures. Descriptions now
immediately follow the fixed size structure headers.
The next change adds better verification of the structures, using the
fixed_size and total_size fields in the common header.
BUG=chromium:423882
BRANCH=none
TEST=VBOOT2=1 make runtests
Change-Id: Ieb9148d6f26c3e59ea542f3a95e59d8019ccee21
Reviewed-on: https://chromium-review.googlesource.com/226824
Tested-by: Randall Spangler <rspangler@chromium.org>
Reviewed-by: Bill Richardson <wfrichar@chromium.org>
Commit-Queue: Randall Spangler <rspangler@chromium.org>
401 lines
11 KiB
C
401 lines
11 KiB
C
/* Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
* found in the LICENSE file.
|
|
*
|
|
* Common functions between firmware and kernel verified boot.
|
|
* (Firmware portion)
|
|
*/
|
|
|
|
#include "2sysincludes.h"
|
|
#include "2common.h"
|
|
#include "2rsa.h"
|
|
#include "2sha.h"
|
|
|
|
int vb2_safe_memcmp(const void *s1, const void *s2, size_t size)
|
|
{
|
|
const unsigned char *us1 = s1;
|
|
const unsigned char *us2 = s2;
|
|
int result = 0;
|
|
|
|
if (0 == size)
|
|
return 0;
|
|
|
|
/*
|
|
* Code snippet without data-dependent branch due to Nate Lawson
|
|
* (nate@root.org) of Root Labs.
|
|
*/
|
|
while (size--)
|
|
result |= *us1++ ^ *us2++;
|
|
|
|
return result != 0;
|
|
}
|
|
|
|
int vb2_align(uint8_t **ptr, uint32_t *size, uint32_t align, uint32_t want_size)
|
|
{
|
|
uintptr_t p = (uintptr_t)*ptr;
|
|
uintptr_t offs = p & (align - 1);
|
|
|
|
if (offs) {
|
|
offs = align - offs;
|
|
|
|
if (*size < offs)
|
|
return VB2_ERROR_ALIGN_BIGGER_THAN_SIZE;
|
|
|
|
*ptr += offs;
|
|
*size -= offs;
|
|
}
|
|
|
|
if (*size < want_size)
|
|
return VB2_ERROR_ALIGN_SIZE;
|
|
|
|
return VB2_SUCCESS;
|
|
}
|
|
|
|
void vb2_workbuf_init(struct vb2_workbuf *wb, uint8_t *buf, uint32_t size)
|
|
{
|
|
wb->buf = buf;
|
|
wb->size = size;
|
|
|
|
/* Align the buffer so allocations will be aligned */
|
|
if (vb2_align(&wb->buf, &wb->size, VB2_WORKBUF_ALIGN, 0))
|
|
wb->size = 0;
|
|
}
|
|
|
|
/**
|
|
* Round up a number to a multiple of VB2_WORKBUF_ALIGN
|
|
*
|
|
* @param v Number to round up
|
|
* @return The number, rounded up.
|
|
*/
|
|
static __inline uint32_t wb_round_up(uint32_t v)
|
|
{
|
|
return (v + VB2_WORKBUF_ALIGN - 1) & ~(VB2_WORKBUF_ALIGN - 1);
|
|
}
|
|
|
|
void *vb2_workbuf_alloc(struct vb2_workbuf *wb, uint32_t size)
|
|
{
|
|
uint8_t *ptr = wb->buf;
|
|
|
|
/* Round up size to work buffer alignment */
|
|
size = wb_round_up(size);
|
|
|
|
if (size > wb->size)
|
|
return NULL;
|
|
|
|
wb->buf += size;
|
|
wb->size -= size;
|
|
|
|
return ptr;
|
|
}
|
|
|
|
void *vb2_workbuf_realloc(struct vb2_workbuf *wb,
|
|
uint32_t oldsize,
|
|
uint32_t newsize)
|
|
{
|
|
/*
|
|
* Just free and allocate to update the size. No need to move/copy
|
|
* memory, since the new pointer is guaranteed to be the same as the
|
|
* old one. The new allocation can fail, if the new size is too big.
|
|
*/
|
|
vb2_workbuf_free(wb, oldsize);
|
|
return vb2_workbuf_alloc(wb, newsize);
|
|
}
|
|
|
|
void vb2_workbuf_free(struct vb2_workbuf *wb, uint32_t size)
|
|
{
|
|
/* Round up size to work buffer alignment */
|
|
size = wb_round_up(size);
|
|
|
|
wb->buf -= size;
|
|
wb->size += size;
|
|
}
|
|
|
|
uint8_t *vb2_signature_data(struct vb2_signature *sig)
|
|
{
|
|
return (uint8_t *)sig + sig->sig_offset;
|
|
}
|
|
|
|
ptrdiff_t vb2_offset_of(const void *base, const void *ptr)
|
|
{
|
|
return (uintptr_t)ptr - (uintptr_t)base;
|
|
}
|
|
|
|
int vb2_verify_member_inside(const void *parent, size_t parent_size,
|
|
const void *member, size_t member_size,
|
|
ptrdiff_t member_data_offset,
|
|
size_t member_data_size)
|
|
{
|
|
const uintptr_t parent_end = (uintptr_t)parent + parent_size;
|
|
const ptrdiff_t member_offs = vb2_offset_of(parent, member);
|
|
const ptrdiff_t member_end_offs = member_offs + member_size;
|
|
const ptrdiff_t data_offs = member_offs + member_data_offset;
|
|
const ptrdiff_t data_end_offs = data_offs + member_data_size;
|
|
|
|
/* Make sure parent doesn't wrap */
|
|
if (parent_size < 0 || parent_end < (uintptr_t)parent)
|
|
return VB2_ERROR_INSIDE_PARENT_WRAPS;
|
|
|
|
/*
|
|
* Make sure the member is fully contained in the parent and doesn't
|
|
* wrap. Use >, not >=, since member_size = 0 is possible.
|
|
*/
|
|
if (member_size < 0 || member_end_offs < member_offs)
|
|
return VB2_ERROR_INSIDE_MEMBER_WRAPS;
|
|
if (member_offs < 0 || member_offs > parent_size ||
|
|
member_end_offs > parent_size)
|
|
return VB2_ERROR_INSIDE_MEMBER_OUTSIDE;
|
|
|
|
/* Make sure the member data is after the member */
|
|
if (member_data_size > 0 && data_offs < member_end_offs)
|
|
return VB2_ERROR_INSIDE_DATA_OVERLAP;
|
|
|
|
/* Make sure parent fully contains member data, if any */
|
|
if (member_data_size < 0 || data_end_offs < data_offs)
|
|
return VB2_ERROR_INSIDE_DATA_WRAPS;
|
|
if (data_offs < 0 || data_offs > parent_size ||
|
|
data_end_offs > parent_size)
|
|
return VB2_ERROR_INSIDE_DATA_OUTSIDE;
|
|
|
|
return VB2_SUCCESS;
|
|
}
|
|
|
|
int vb2_verify_common_header(const void *parent,
|
|
uint32_t parent_size,
|
|
const struct vb2_struct_common *c)
|
|
{
|
|
int rv;
|
|
|
|
/* Make sure common data and description are inside parent */
|
|
rv = vb2_verify_member_inside(parent, parent_size,
|
|
c, sizeof(*c),
|
|
c->fixed_size, c->desc_size);
|
|
if (rv)
|
|
return rv;
|
|
|
|
/* Check description */
|
|
if (c->desc_size > 0) {
|
|
/* Description must be null-terminated */
|
|
const uint8_t *desc = (const uint8_t *)c + c->fixed_size;
|
|
if (desc[c->desc_size - 1] != 0)
|
|
return VB2_ERROR_DESC_TERMINATOR;
|
|
}
|
|
|
|
return VB2_SUCCESS;
|
|
}
|
|
|
|
int vb2_verify_signature_inside(const void *parent,
|
|
uint32_t parent_size,
|
|
const struct vb2_signature *sig)
|
|
{
|
|
return vb2_verify_member_inside(parent, parent_size,
|
|
sig, sizeof(*sig),
|
|
sig->sig_offset, sig->sig_size);
|
|
}
|
|
|
|
int vb2_verify_digest(const struct vb2_public_key *key,
|
|
struct vb2_signature *sig,
|
|
const uint8_t *digest,
|
|
struct vb2_workbuf *wb)
|
|
{
|
|
uint8_t *sig_data = vb2_signature_data(sig);
|
|
|
|
if (sig->sig_size != vb2_rsa_sig_size(key->sig_alg)) {
|
|
VB2_DEBUG("Wrong data signature size for algorithm, "
|
|
"sig_size=%d, expected %d for algorithm %d.\n",
|
|
sig->sig_size, vb2_rsa_sig_size(key->sig_alg),
|
|
key->sig_alg);
|
|
return VB2_ERROR_VDATA_SIG_SIZE;
|
|
}
|
|
|
|
return vb2_rsa_verify_digest(key, sig_data, digest, wb);
|
|
}
|
|
|
|
int vb2_verify_data(const uint8_t *data,
|
|
uint32_t size,
|
|
struct vb2_signature *sig,
|
|
const struct vb2_public_key *key,
|
|
struct vb2_workbuf *wb)
|
|
{
|
|
struct vb2_workbuf wblocal = *wb;
|
|
struct vb2_digest_context *dc;
|
|
uint8_t *digest;
|
|
uint32_t digest_size;
|
|
int rv;
|
|
|
|
if (sig->data_size > size) {
|
|
VB2_DEBUG("Data buffer smaller than length of signed data.\n");
|
|
return VB2_ERROR_VDATA_NOT_ENOUGH_DATA;
|
|
}
|
|
|
|
/* Digest goes at start of work buffer */
|
|
digest_size = vb2_digest_size(key->hash_alg);
|
|
if (!digest_size)
|
|
return VB2_ERROR_VDATA_DIGEST_SIZE;
|
|
|
|
digest = vb2_workbuf_alloc(&wblocal, digest_size);
|
|
if (!digest)
|
|
return VB2_ERROR_VDATA_WORKBUF_DIGEST;
|
|
|
|
/* Hashing requires temp space for the context */
|
|
dc = vb2_workbuf_alloc(&wblocal, sizeof(*dc));
|
|
if (!dc)
|
|
return VB2_ERROR_VDATA_WORKBUF_HASHING;
|
|
|
|
rv = vb2_digest_init(dc, key->hash_alg);
|
|
if (rv)
|
|
return rv;
|
|
|
|
rv = vb2_digest_extend(dc, data, sig->data_size);
|
|
if (rv)
|
|
return rv;
|
|
|
|
rv = vb2_digest_finalize(dc, digest, digest_size);
|
|
if (rv)
|
|
return rv;
|
|
|
|
vb2_workbuf_free(&wblocal, sizeof(*dc));
|
|
|
|
return vb2_verify_digest(key, sig, digest, &wblocal);
|
|
}
|
|
|
|
int vb2_verify_keyblock(struct vb2_keyblock *block,
|
|
uint32_t size,
|
|
const struct vb2_public_key *key,
|
|
struct vb2_workbuf *wb)
|
|
{
|
|
struct vb2_signature *sig;
|
|
int rv;
|
|
|
|
/* Sanity checks before attempting signature of data */
|
|
if(size < sizeof(*block)) {
|
|
VB2_DEBUG("Not enough space for key block header.\n");
|
|
return VB2_ERROR_KEYBLOCK_TOO_SMALL_FOR_HEADER;
|
|
}
|
|
if (memcmp(block->magic, KEY_BLOCK_MAGIC, KEY_BLOCK_MAGIC_SIZE)) {
|
|
VB2_DEBUG("Not a valid verified boot key block.\n");
|
|
return VB2_ERROR_KEYBLOCK_MAGIC;
|
|
}
|
|
if (block->header_version_major != KEY_BLOCK_HEADER_VERSION_MAJOR) {
|
|
VB2_DEBUG("Incompatible key block header version.\n");
|
|
return VB2_ERROR_KEYBLOCK_HEADER_VERSION;
|
|
}
|
|
if (size < block->keyblock_size) {
|
|
VB2_DEBUG("Not enough data for key block.\n");
|
|
return VB2_ERROR_KEYBLOCK_SIZE;
|
|
}
|
|
|
|
/* Check signature */
|
|
sig = &block->keyblock_signature;
|
|
|
|
if (vb2_verify_signature_inside(block, block->keyblock_size, sig)) {
|
|
VB2_DEBUG("Key block signature off end of block\n");
|
|
return VB2_ERROR_KEYBLOCK_SIG_OUTSIDE;
|
|
}
|
|
|
|
/* Make sure advertised signature data sizes are sane. */
|
|
if (block->keyblock_size < sig->data_size) {
|
|
VB2_DEBUG("Signature calculated past end of block\n");
|
|
return VB2_ERROR_KEYBLOCK_SIGNED_TOO_MUCH;
|
|
}
|
|
|
|
VB2_DEBUG("Checking key block signature...\n");
|
|
rv = vb2_verify_data((const uint8_t *)block, size, sig, key, wb);
|
|
if (rv) {
|
|
VB2_DEBUG("Invalid key block signature.\n");
|
|
return VB2_ERROR_KEYBLOCK_SIG_INVALID;
|
|
}
|
|
|
|
/* Verify we signed enough data */
|
|
if (sig->data_size < sizeof(struct vb2_keyblock)) {
|
|
VB2_DEBUG("Didn't sign enough data\n");
|
|
return VB2_ERROR_KEYBLOCK_SIGNED_TOO_LITTLE;
|
|
}
|
|
|
|
/* Verify data key is inside the block and inside signed data */
|
|
if (vb2_verify_packed_key_inside(block, block->keyblock_size,
|
|
&block->data_key)) {
|
|
VB2_DEBUG("Data key off end of key block\n");
|
|
return VB2_ERROR_KEYBLOCK_DATA_KEY_OUTSIDE;
|
|
}
|
|
if (vb2_verify_packed_key_inside(block, sig->data_size,
|
|
&block->data_key)) {
|
|
VB2_DEBUG("Data key off end of signed data\n");
|
|
return VB2_ERROR_KEYBLOCK_DATA_KEY_UNSIGNED;
|
|
}
|
|
|
|
/* Success */
|
|
return VB2_SUCCESS;
|
|
}
|
|
|
|
int vb2_verify_fw_preamble(struct vb2_fw_preamble *preamble,
|
|
uint32_t size,
|
|
const struct vb2_public_key *key,
|
|
struct vb2_workbuf *wb)
|
|
{
|
|
struct vb2_signature *sig = &preamble->preamble_signature;
|
|
|
|
VB2_DEBUG("Verifying preamble.\n");
|
|
|
|
/* Sanity checks before attempting signature of data */
|
|
if(size < sizeof(*preamble)) {
|
|
VB2_DEBUG("Not enough data for preamble header\n");
|
|
return VB2_ERROR_PREAMBLE_TOO_SMALL_FOR_HEADER;
|
|
}
|
|
if (preamble->header_version_major !=
|
|
FIRMWARE_PREAMBLE_HEADER_VERSION_MAJOR) {
|
|
VB2_DEBUG("Incompatible firmware preamble header version.\n");
|
|
return VB2_ERROR_PREAMBLE_HEADER_VERSION;
|
|
}
|
|
|
|
if (preamble->header_version_minor < 1) {
|
|
VB2_DEBUG("Only preamble header 2.1+ supported\n");
|
|
return VB2_ERROR_PREAMBLE_HEADER_OLD;
|
|
}
|
|
|
|
if (size < preamble->preamble_size) {
|
|
VB2_DEBUG("Not enough data for preamble.\n");
|
|
return VB2_ERROR_PREAMBLE_SIZE;
|
|
}
|
|
|
|
/* Check signature */
|
|
if (vb2_verify_signature_inside(preamble, preamble->preamble_size,
|
|
sig)) {
|
|
VB2_DEBUG("Preamble signature off end of preamble\n");
|
|
return VB2_ERROR_PREAMBLE_SIG_OUTSIDE;
|
|
}
|
|
|
|
/* Make sure advertised signature data sizes are sane. */
|
|
if (preamble->preamble_size < sig->data_size) {
|
|
VB2_DEBUG("Signature calculated past end of the block\n");
|
|
return VB2_ERROR_PREAMBLE_SIGNED_TOO_MUCH;
|
|
}
|
|
|
|
if (vb2_verify_data((const uint8_t *)preamble, size, sig, key, wb)) {
|
|
VB2_DEBUG("Preamble signature validation failed\n");
|
|
return VB2_ERROR_PREAMBLE_SIG_INVALID;
|
|
}
|
|
|
|
/* Verify we signed enough data */
|
|
if (sig->data_size < sizeof(struct vb2_fw_preamble)) {
|
|
VB2_DEBUG("Didn't sign enough data\n");
|
|
return VB2_ERROR_PREAMBLE_SIGNED_TOO_LITTLE;
|
|
}
|
|
|
|
/* Verify body signature is inside the signed data */
|
|
if (vb2_verify_signature_inside(preamble, sig->data_size,
|
|
&preamble->body_signature)) {
|
|
VB2_DEBUG("Firmware body signature off end of preamble\n");
|
|
return VB2_ERROR_PREAMBLE_BODY_SIG_OUTSIDE;
|
|
}
|
|
|
|
/* Verify kernel subkey is inside the signed data */
|
|
if (vb2_verify_packed_key_inside(preamble, sig->data_size,
|
|
&preamble->kernel_subkey)) {
|
|
VB2_DEBUG("Kernel subkey off end of preamble\n");
|
|
return VB2_ERROR_PREAMBLE_KERNEL_SUBKEY_OUTSIDE;
|
|
}
|
|
|
|
/* Success */
|
|
return VB2_SUCCESS;
|
|
}
|