misc.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. * Misc functions which need access to vb2_context but are not public APIs
  6. */
  7. #include "2sysincludes.h"
  8. #include "2api.h"
  9. #include "2misc.h"
  10. #include "2nvstorage.h"
  11. #include "2secdata.h"
  12. #include "2sha.h"
  13. #include "2rsa.h"
  14. #include "vb2_common.h"
  15. /*
  16. * The blob below is the sha1 digest calculated over the packed developer
  17. * root public key structure.
  18. */
  19. static const uint8_t dev_key_digest[] = {
  20. 0xb1, 0x1d, 0x74, 0xed, 0xd2, 0x86, 0xc1, 0x44,
  21. 0xe1, 0x13, 0x5b, 0x49, 0xe7, 0xf0, 0xbc, 0x20,
  22. 0xcf, 0x04, 0x1f, 0x10,
  23. };
  24. /**
  25. * Determine if the root key is the developer key checked into the
  26. * vboot_reference repository. Has no effect on boot; just logs this to the
  27. * debug console.
  28. *
  29. * @param root Root key
  30. */
  31. static void vb2_report_dev_firmware(struct vb2_public_key *root)
  32. {
  33. struct vb2_digest_context dc;
  34. uint8_t digest[sizeof(dev_key_digest)];
  35. int size = root->arrsize * 4;
  36. if (!root->arrsize)
  37. return; /* Must be a test run. */
  38. if (vb2_digest_init(&dc, VB2_HASH_SHA1) != VB2_SUCCESS)
  39. return;
  40. if (vb2_digest_extend(&dc, (uint8_t *)&root->arrsize,
  41. sizeof(root->arrsize)) != VB2_SUCCESS)
  42. return;
  43. if (vb2_digest_extend(&dc, (uint8_t *)&root->n0inv,
  44. sizeof(root->n0inv)) != VB2_SUCCESS)
  45. return;
  46. if (vb2_digest_extend(&dc, (uint8_t *)root->n, size) != VB2_SUCCESS)
  47. return;
  48. if (vb2_digest_extend(&dc, (uint8_t *)root->rr, size) != VB2_SUCCESS)
  49. return;
  50. if (vb2_digest_finalize(&dc, digest, sizeof(digest)) != VB2_SUCCESS)
  51. return;
  52. if (!memcmp(digest, dev_key_digest, sizeof(dev_key_digest)))
  53. VB2_DEBUG("This is developer signed firmware\n");
  54. }
  55. int vb2_load_fw_keyblock(struct vb2_context *ctx)
  56. {
  57. struct vb2_shared_data *sd = vb2_get_sd(ctx);
  58. struct vb2_workbuf wb;
  59. uint8_t *key_data;
  60. uint32_t key_size;
  61. struct vb2_packed_key *packed_key;
  62. struct vb2_public_key root_key;
  63. struct vb2_keyblock *kb;
  64. uint32_t block_size;
  65. int rv;
  66. vb2_workbuf_from_ctx(ctx, &wb);
  67. /* Read the root key */
  68. key_size = sd->gbb_rootkey_size;
  69. key_data = vb2_workbuf_alloc(&wb, key_size);
  70. if (!key_data)
  71. return VB2_ERROR_FW_KEYBLOCK_WORKBUF_ROOT_KEY;
  72. rv = vb2ex_read_resource(ctx, VB2_RES_GBB, sd->gbb_rootkey_offset,
  73. key_data, key_size);
  74. if (rv)
  75. return rv;
  76. /* Unpack the root key */
  77. rv = vb2_unpack_key_buffer(&root_key, key_data, key_size);
  78. if (rv)
  79. return rv;
  80. /* If that's the checked-in root key, this is dev-signed firmware */
  81. vb2_report_dev_firmware(&root_key);
  82. /* Load the firmware keyblock header after the root key */
  83. kb = vb2_workbuf_alloc(&wb, sizeof(*kb));
  84. if (!kb)
  85. return VB2_ERROR_FW_KEYBLOCK_WORKBUF_HEADER;
  86. rv = vb2ex_read_resource(ctx, VB2_RES_FW_VBLOCK, 0, kb, sizeof(*kb));
  87. if (rv)
  88. return rv;
  89. block_size = kb->keyblock_size;
  90. /*
  91. * Load the entire keyblock, now that we know how big it is. Note that
  92. * we're loading the entire keyblock instead of just the piece after
  93. * the header. That means we re-read the header. But that's a tiny
  94. * amount of data, and it makes the code much more straightforward.
  95. */
  96. kb = vb2_workbuf_realloc(&wb, sizeof(*kb), block_size);
  97. if (!kb)
  98. return VB2_ERROR_FW_KEYBLOCK_WORKBUF;
  99. rv = vb2ex_read_resource(ctx, VB2_RES_FW_VBLOCK, 0, kb, block_size);
  100. if (rv)
  101. return rv;
  102. /* Verify the keyblock */
  103. rv = vb2_verify_keyblock(kb, block_size, &root_key, &wb);
  104. if (rv) {
  105. vb2_fail(ctx, VB2_RECOVERY_FW_KEYBLOCK, rv);
  106. return rv;
  107. }
  108. /* Key version is the upper 16 bits of the composite firmware version */
  109. if (kb->data_key.key_version > VB2_MAX_KEY_VERSION)
  110. rv = VB2_ERROR_FW_KEYBLOCK_VERSION_RANGE;
  111. if (!rv && kb->data_key.key_version < (sd->fw_version_secdata >> 16)) {
  112. if (sd->gbb_flags & VB2_GBB_FLAG_DISABLE_FW_ROLLBACK_CHECK)
  113. VB2_DEBUG("Ignoring FW key rollback due to GBB flag\n");
  114. else
  115. rv = VB2_ERROR_FW_KEYBLOCK_VERSION_ROLLBACK;
  116. }
  117. if (rv) {
  118. vb2_fail(ctx, VB2_RECOVERY_FW_KEY_ROLLBACK, rv);
  119. return rv;
  120. }
  121. sd->fw_version = kb->data_key.key_version << 16;
  122. /*
  123. * Save the data key in the work buffer. This overwrites the root key
  124. * we read above. That's ok, because now that we have the data key we
  125. * no longer need the root key.
  126. */
  127. packed_key = (struct vb2_packed_key *)key_data;
  128. packed_key->algorithm = kb->data_key.algorithm;
  129. packed_key->key_version = kb->data_key.key_version;
  130. packed_key->key_size = kb->data_key.key_size;
  131. /*
  132. * Use memmove() instead of memcpy(). In theory, the destination will
  133. * never overlap because with the source because the root key is likely
  134. * to be at least as large as the data key, but there's no harm here in
  135. * being paranoid.
  136. */
  137. memmove(key_data + packed_key->key_offset,
  138. (uint8_t*)&kb->data_key + kb->data_key.key_offset,
  139. packed_key->key_size);
  140. /* Save the packed key offset and size */
  141. sd->workbuf_data_key_offset = vb2_offset_of(ctx->workbuf, key_data);
  142. sd->workbuf_data_key_size =
  143. packed_key->key_offset + packed_key->key_size;
  144. /* Preamble follows the keyblock in the vblock */
  145. sd->vblock_preamble_offset = kb->keyblock_size;
  146. /*
  147. * Data key will persist in the workbuf after we return.
  148. *
  149. * Work buffer now contains:
  150. * - vb2_shared_data
  151. * - packed firmware data key
  152. */
  153. ctx->workbuf_used = sd->workbuf_data_key_offset +
  154. sd->workbuf_data_key_size;
  155. return VB2_SUCCESS;
  156. }
  157. int vb2_load_fw_preamble(struct vb2_context *ctx)
  158. {
  159. struct vb2_shared_data *sd = vb2_get_sd(ctx);
  160. struct vb2_workbuf wb;
  161. uint8_t *key_data = ctx->workbuf + sd->workbuf_data_key_offset;
  162. uint32_t key_size = sd->workbuf_data_key_size;
  163. struct vb2_public_key data_key;
  164. /* Preamble goes in the next unused chunk of work buffer */
  165. struct vb2_fw_preamble *pre;
  166. uint32_t pre_size;
  167. int rv;
  168. vb2_workbuf_from_ctx(ctx, &wb);
  169. /* Unpack the firmware data key */
  170. if (!sd->workbuf_data_key_size)
  171. return VB2_ERROR_FW_PREAMBLE2_DATA_KEY;
  172. rv = vb2_unpack_key_buffer(&data_key, key_data, key_size);
  173. if (rv)
  174. return rv;
  175. /* Load the firmware preamble header */
  176. pre = vb2_workbuf_alloc(&wb, sizeof(*pre));
  177. if (!pre)
  178. return VB2_ERROR_FW_PREAMBLE2_WORKBUF_HEADER;
  179. rv = vb2ex_read_resource(ctx, VB2_RES_FW_VBLOCK,
  180. sd->vblock_preamble_offset,
  181. pre, sizeof(*pre));
  182. if (rv)
  183. return rv;
  184. pre_size = pre->preamble_size;
  185. /* Load the entire firmware preamble, now that we know how big it is */
  186. pre = vb2_workbuf_realloc(&wb, sizeof(*pre), pre_size);
  187. if (!pre)
  188. return VB2_ERROR_FW_PREAMBLE2_WORKBUF;
  189. rv = vb2ex_read_resource(ctx, VB2_RES_FW_VBLOCK,
  190. sd->vblock_preamble_offset,
  191. pre, pre_size);
  192. if (rv)
  193. return rv;
  194. /* Work buffer now contains the data subkey data and the preamble */
  195. /* Verify the preamble */
  196. rv = vb2_verify_fw_preamble(pre, pre_size, &data_key, &wb);
  197. if (rv) {
  198. vb2_fail(ctx, VB2_RECOVERY_FW_PREAMBLE, rv);
  199. return rv;
  200. }
  201. /*
  202. * Firmware version is the lower 16 bits of the composite firmware
  203. * version.
  204. */
  205. if (pre->firmware_version > VB2_MAX_PREAMBLE_VERSION)
  206. rv = VB2_ERROR_FW_PREAMBLE_VERSION_RANGE;
  207. /* Combine with the key version from vb2_load_fw_keyblock() */
  208. sd->fw_version |= pre->firmware_version;
  209. if (!rv && sd->fw_version < sd->fw_version_secdata) {
  210. if (sd->gbb_flags & VB2_GBB_FLAG_DISABLE_FW_ROLLBACK_CHECK)
  211. VB2_DEBUG("Ignoring FW rollback due to GBB flag\n");
  212. else
  213. rv = VB2_ERROR_FW_PREAMBLE_VERSION_ROLLBACK;
  214. }
  215. if (rv) {
  216. vb2_fail(ctx, VB2_RECOVERY_FW_ROLLBACK, rv);
  217. return rv;
  218. }
  219. /*
  220. * If this is a newer version than in secure storage, and we
  221. * successfully booted the same slot last boot, roll forward the
  222. * version in secure storage.
  223. *
  224. * Note that this happens before we've verified the firmware data this
  225. * boot; we're relying on the indicator that the last boot was
  226. * successful. That's ok, because even if the firmware data has a
  227. * valid hash, the only way we can know if it's functional is to trust
  228. * the status from the last boot.
  229. */
  230. if (sd->fw_version > sd->fw_version_secdata &&
  231. sd->last_fw_slot == sd->fw_slot &&
  232. sd->last_fw_result == VB2_FW_RESULT_SUCCESS) {
  233. sd->fw_version_secdata = sd->fw_version;
  234. rv = vb2_secdata_set(ctx, VB2_SECDATA_VERSIONS, sd->fw_version);
  235. if (rv)
  236. return rv;
  237. }
  238. /* Keep track of where we put the preamble */
  239. sd->workbuf_preamble_offset = vb2_offset_of(ctx->workbuf, pre);
  240. sd->workbuf_preamble_size = pre_size;
  241. /*
  242. * Preamble will persist in work buffer after we return.
  243. *
  244. * Work buffer now contains:
  245. * - vb2_shared_data
  246. * - packed firmware data key
  247. * - firmware preamble
  248. *
  249. * TODO: we could move the preamble down over the firmware data key
  250. * since we don't need it anymore.
  251. */
  252. ctx->workbuf_used = sd->workbuf_preamble_offset + pre_size;
  253. return VB2_SUCCESS;
  254. }