Home | History | Annotate | Download | only in lib21
      1 /* Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
      2  * Use of this source code is governed by a BSD-style license that can be
      3  * found in the LICENSE file.
      4  *
      5  * Key unpacking functions
      6  */
      7 
      8 #include "2sysincludes.h"
      9 #include "2common.h"
     10 #include "2rsa.h"
     11 #include "vb2_common.h"
     12 
     13 int vb2_unpack_key_data(struct vb2_public_key *key,
     14 			 const uint8_t *key_data,
     15 			 uint32_t key_size)
     16 {
     17 	const uint32_t *buf32 = (const uint32_t *)key_data;
     18 	uint32_t expected_key_size = vb2_packed_key_size(key->sig_alg);
     19 
     20 	/* Make sure buffer is the correct length */
     21 	if (!expected_key_size || expected_key_size != key_size) {
     22 		VB2_DEBUG("Wrong key size for algorithm\n");
     23 		return VB2_ERROR_UNPACK_KEY_SIZE;
     24 	}
     25 
     26 	/* Check for alignment */
     27 	if (!vb2_aligned(buf32, sizeof(uint32_t)))
     28 		return VB2_ERROR_UNPACK_KEY_ALIGN;
     29 
     30 	key->arrsize = buf32[0];
     31 
     32 	/* Sanity check key array size */
     33 	if (key->arrsize * sizeof(uint32_t) != vb2_rsa_sig_size(key->sig_alg))
     34 		return VB2_ERROR_UNPACK_KEY_ARRAY_SIZE;
     35 
     36 	key->n0inv = buf32[1];
     37 
     38 	/* Arrays point inside the key data */
     39 	key->n = buf32 + 2;
     40 	key->rr = buf32 + 2 + key->arrsize;
     41 
     42 	return VB2_SUCCESS;
     43 }
     44 
     45 int vb2_unpack_key(struct vb2_public_key *key,
     46 		    const uint8_t *buf,
     47 		    uint32_t size)
     48 {
     49 	const struct vb2_packed_key *pkey =
     50 		(const struct vb2_packed_key *)buf;
     51 	uint32_t sig_size;
     52 	uint32_t min_offset = 0;
     53 	int rv;
     54 
     55 	/* Check magic number */
     56 	if (pkey->c.magic != VB2_MAGIC_PACKED_KEY)
     57 		return VB2_ERROR_UNPACK_KEY_MAGIC;
     58 
     59 	rv = vb2_verify_common_header(buf, size);
     60 	if (rv)
     61 		return rv;
     62 
     63 	/* Make sure key data is inside */
     64 	rv = vb2_verify_common_member(pkey, &min_offset,
     65 				      pkey->key_offset, pkey->key_size);
     66 	if (rv)
     67 		return rv;
     68 
     69 	/*
     70 	 * Check for compatible version.  No need to check minor version, since
     71 	 * that's compatible across readers matching the major version, and we
     72 	 * haven't added any new fields.
     73 	 */
     74 	if (pkey->c.struct_version_major != VB2_PACKED_KEY_VERSION_MAJOR)
     75 		return VB2_ERROR_UNPACK_KEY_STRUCT_VERSION;
     76 
     77 	/* Copy key algorithms */
     78 	key->hash_alg = pkey->hash_alg;
     79 	if (!vb2_digest_size(key->hash_alg))
     80 		return VB2_ERROR_UNPACK_KEY_HASH_ALGORITHM;
     81 
     82 	key->sig_alg = pkey->sig_alg;
     83 	if (key->sig_alg != VB2_SIG_NONE) {
     84 		sig_size = vb2_rsa_sig_size(key->sig_alg);
     85 		if (!sig_size)
     86 			return VB2_ERROR_UNPACK_KEY_SIG_ALGORITHM;
     87 		rv = vb2_unpack_key_data(
     88 				key,
     89 				(const uint8_t *)pkey + pkey->key_offset,
     90 				pkey->key_size);
     91 		if (rv)
     92 			return rv;
     93 	}
     94 
     95 	/* Key description */
     96 	key->desc = vb2_common_desc(pkey);
     97 	key->version = pkey->key_version;
     98 	key->guid = &pkey->guid;
     99 
    100 	return VB2_SUCCESS;
    101 }
    102