common.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. * Common functions between firmware and kernel verified boot.
  6. * (Firmware portion)
  7. */
  8. #include "2sysincludes.h"
  9. #include "2rsa.h"
  10. #include "2sha.h"
  11. #include "vb2_common.h"
  12. uint8_t *vb2_signature_data(struct vb2_signature *sig)
  13. {
  14. return (uint8_t *)sig + sig->sig_offset;
  15. }
  16. int vb2_verify_member_inside(const void *parent, size_t parent_size,
  17. const void *member, size_t member_size,
  18. ptrdiff_t member_data_offset,
  19. size_t member_data_size)
  20. {
  21. const uintptr_t parent_end = (uintptr_t)parent + parent_size;
  22. const ptrdiff_t member_offs = vb2_offset_of(parent, member);
  23. const ptrdiff_t member_end_offs = member_offs + member_size;
  24. const ptrdiff_t data_offs = member_offs + member_data_offset;
  25. const ptrdiff_t data_end_offs = data_offs + member_data_size;
  26. /* Make sure parent doesn't wrap */
  27. if (parent_end < (uintptr_t)parent)
  28. return VB2_ERROR_INSIDE_PARENT_WRAPS;
  29. /*
  30. * Make sure the member is fully contained in the parent and doesn't
  31. * wrap. Use >, not >=, since member_size = 0 is possible.
  32. */
  33. if (member_end_offs < member_offs)
  34. return VB2_ERROR_INSIDE_MEMBER_WRAPS;
  35. if (member_offs < 0 || member_offs > parent_size ||
  36. member_end_offs > parent_size)
  37. return VB2_ERROR_INSIDE_MEMBER_OUTSIDE;
  38. /* Make sure the member data is after the member */
  39. if (member_data_size > 0 && data_offs < member_end_offs)
  40. return VB2_ERROR_INSIDE_DATA_OVERLAP;
  41. /* Make sure parent fully contains member data, if any */
  42. if (data_end_offs < data_offs)
  43. return VB2_ERROR_INSIDE_DATA_WRAPS;
  44. if (data_offs < 0 || data_offs > parent_size ||
  45. data_end_offs > parent_size)
  46. return VB2_ERROR_INSIDE_DATA_OUTSIDE;
  47. return VB2_SUCCESS;
  48. }
  49. int vb2_verify_signature_inside(const void *parent,
  50. uint32_t parent_size,
  51. const struct vb2_signature *sig)
  52. {
  53. return vb2_verify_member_inside(parent, parent_size,
  54. sig, sizeof(*sig),
  55. sig->sig_offset, sig->sig_size);
  56. }
  57. int vb2_verify_digest(const struct vb2_public_key *key,
  58. struct vb2_signature *sig,
  59. const uint8_t *digest,
  60. const struct vb2_workbuf *wb)
  61. {
  62. uint8_t *sig_data = vb2_signature_data(sig);
  63. if (sig->sig_size != vb2_rsa_sig_size(key->sig_alg)) {
  64. VB2_DEBUG("Wrong data signature size for algorithm, "
  65. "sig_size=%d, expected %d for algorithm %d.\n",
  66. sig->sig_size, vb2_rsa_sig_size(key->sig_alg),
  67. key->sig_alg);
  68. return VB2_ERROR_VDATA_SIG_SIZE;
  69. }
  70. return vb2_rsa_verify_digest(key, sig_data, digest, wb);
  71. }
  72. int vb2_verify_data(const uint8_t *data,
  73. uint32_t size,
  74. struct vb2_signature *sig,
  75. const struct vb2_public_key *key,
  76. const struct vb2_workbuf *wb)
  77. {
  78. struct vb2_workbuf wblocal = *wb;
  79. struct vb2_digest_context *dc;
  80. uint8_t *digest;
  81. uint32_t digest_size;
  82. int rv;
  83. if (sig->data_size > size) {
  84. VB2_DEBUG("Data buffer smaller than length of signed data.\n");
  85. return VB2_ERROR_VDATA_NOT_ENOUGH_DATA;
  86. }
  87. /* Digest goes at start of work buffer */
  88. digest_size = vb2_digest_size(key->hash_alg);
  89. if (!digest_size)
  90. return VB2_ERROR_VDATA_DIGEST_SIZE;
  91. digest = vb2_workbuf_alloc(&wblocal, digest_size);
  92. if (!digest)
  93. return VB2_ERROR_VDATA_WORKBUF_DIGEST;
  94. /* Hashing requires temp space for the context */
  95. dc = vb2_workbuf_alloc(&wblocal, sizeof(*dc));
  96. if (!dc)
  97. return VB2_ERROR_VDATA_WORKBUF_HASHING;
  98. rv = vb2_digest_init(dc, key->hash_alg);
  99. if (rv)
  100. return rv;
  101. rv = vb2_digest_extend(dc, data, sig->data_size);
  102. if (rv)
  103. return rv;
  104. rv = vb2_digest_finalize(dc, digest, digest_size);
  105. if (rv)
  106. return rv;
  107. vb2_workbuf_free(&wblocal, sizeof(*dc));
  108. return vb2_verify_digest(key, sig, digest, &wblocal);
  109. }
  110. int vb2_check_keyblock(const struct vb2_keyblock *block,
  111. uint32_t size,
  112. const struct vb2_signature *sig)
  113. {
  114. if(size < sizeof(*block)) {
  115. VB2_DEBUG("Not enough space for key block header.\n");
  116. return VB2_ERROR_KEYBLOCK_TOO_SMALL_FOR_HEADER;
  117. }
  118. if (memcmp(block->magic, KEY_BLOCK_MAGIC, KEY_BLOCK_MAGIC_SIZE)) {
  119. VB2_DEBUG("Not a valid verified boot key block.\n");
  120. return VB2_ERROR_KEYBLOCK_MAGIC;
  121. }
  122. if (block->header_version_major != KEY_BLOCK_HEADER_VERSION_MAJOR) {
  123. VB2_DEBUG("Incompatible key block header version.\n");
  124. return VB2_ERROR_KEYBLOCK_HEADER_VERSION;
  125. }
  126. if (size < block->keyblock_size) {
  127. VB2_DEBUG("Not enough data for key block.\n");
  128. return VB2_ERROR_KEYBLOCK_SIZE;
  129. }
  130. if (vb2_verify_signature_inside(block, block->keyblock_size, sig)) {
  131. VB2_DEBUG("Key block signature off end of block\n");
  132. return VB2_ERROR_KEYBLOCK_SIG_OUTSIDE;
  133. }
  134. /* Make sure advertised signature data sizes are sane. */
  135. if (block->keyblock_size < sig->data_size) {
  136. VB2_DEBUG("Signature calculated past end of block\n");
  137. return VB2_ERROR_KEYBLOCK_SIGNED_TOO_MUCH;
  138. }
  139. /* Verify we signed enough data */
  140. if (sig->data_size < sizeof(struct vb2_keyblock)) {
  141. VB2_DEBUG("Didn't sign enough data\n");
  142. return VB2_ERROR_KEYBLOCK_SIGNED_TOO_LITTLE;
  143. }
  144. /* Verify data key is inside the block and inside signed data */
  145. if (vb2_verify_packed_key_inside(block, block->keyblock_size,
  146. &block->data_key)) {
  147. VB2_DEBUG("Data key off end of key block\n");
  148. return VB2_ERROR_KEYBLOCK_DATA_KEY_OUTSIDE;
  149. }
  150. if (vb2_verify_packed_key_inside(block, sig->data_size,
  151. &block->data_key)) {
  152. VB2_DEBUG("Data key off end of signed data\n");
  153. return VB2_ERROR_KEYBLOCK_DATA_KEY_UNSIGNED;
  154. }
  155. return VB2_SUCCESS;
  156. }
  157. int vb2_verify_keyblock(struct vb2_keyblock *block,
  158. uint32_t size,
  159. const struct vb2_public_key *key,
  160. const struct vb2_workbuf *wb)
  161. {
  162. struct vb2_signature *sig = &block->keyblock_signature;
  163. int rv;
  164. /* Sanity check keyblock before attempting signature check of data */
  165. rv = vb2_check_keyblock(block, size, sig);
  166. if (rv)
  167. return rv;
  168. VB2_DEBUG("Checking key block signature...\n");
  169. rv = vb2_verify_data((const uint8_t *)block, size, sig, key, wb);
  170. if (rv) {
  171. VB2_DEBUG("Invalid key block signature.\n");
  172. return VB2_ERROR_KEYBLOCK_SIG_INVALID;
  173. }
  174. /* Success */
  175. return VB2_SUCCESS;
  176. }
  177. int vb2_verify_fw_preamble(struct vb2_fw_preamble *preamble,
  178. uint32_t size,
  179. const struct vb2_public_key *key,
  180. const struct vb2_workbuf *wb)
  181. {
  182. struct vb2_signature *sig = &preamble->preamble_signature;
  183. VB2_DEBUG("Verifying preamble.\n");
  184. /* Sanity checks before attempting signature of data */
  185. if(size < sizeof(*preamble)) {
  186. VB2_DEBUG("Not enough data for preamble header\n");
  187. return VB2_ERROR_PREAMBLE_TOO_SMALL_FOR_HEADER;
  188. }
  189. if (preamble->header_version_major !=
  190. FIRMWARE_PREAMBLE_HEADER_VERSION_MAJOR) {
  191. VB2_DEBUG("Incompatible firmware preamble header version.\n");
  192. return VB2_ERROR_PREAMBLE_HEADER_VERSION;
  193. }
  194. if (preamble->header_version_minor < 1) {
  195. VB2_DEBUG("Only preamble header 2.1+ supported\n");
  196. return VB2_ERROR_PREAMBLE_HEADER_OLD;
  197. }
  198. if (size < preamble->preamble_size) {
  199. VB2_DEBUG("Not enough data for preamble.\n");
  200. return VB2_ERROR_PREAMBLE_SIZE;
  201. }
  202. /* Check signature */
  203. if (vb2_verify_signature_inside(preamble, preamble->preamble_size,
  204. sig)) {
  205. VB2_DEBUG("Preamble signature off end of preamble\n");
  206. return VB2_ERROR_PREAMBLE_SIG_OUTSIDE;
  207. }
  208. /* Make sure advertised signature data sizes are sane. */
  209. if (preamble->preamble_size < sig->data_size) {
  210. VB2_DEBUG("Signature calculated past end of the block\n");
  211. return VB2_ERROR_PREAMBLE_SIGNED_TOO_MUCH;
  212. }
  213. if (vb2_verify_data((const uint8_t *)preamble, size, sig, key, wb)) {
  214. VB2_DEBUG("Preamble signature validation failed\n");
  215. return VB2_ERROR_PREAMBLE_SIG_INVALID;
  216. }
  217. /* Verify we signed enough data */
  218. if (sig->data_size < sizeof(struct vb2_fw_preamble)) {
  219. VB2_DEBUG("Didn't sign enough data\n");
  220. return VB2_ERROR_PREAMBLE_SIGNED_TOO_LITTLE;
  221. }
  222. /* Verify body signature is inside the signed data */
  223. if (vb2_verify_signature_inside(preamble, sig->data_size,
  224. &preamble->body_signature)) {
  225. VB2_DEBUG("Firmware body signature off end of preamble\n");
  226. return VB2_ERROR_PREAMBLE_BODY_SIG_OUTSIDE;
  227. }
  228. /* Verify kernel subkey is inside the signed data */
  229. if (vb2_verify_packed_key_inside(preamble, sig->data_size,
  230. &preamble->kernel_subkey)) {
  231. VB2_DEBUG("Kernel subkey off end of preamble\n");
  232. return VB2_ERROR_PREAMBLE_KERNEL_SUBKEY_OUTSIDE;
  233. }
  234. /* Success */
  235. return VB2_SUCCESS;
  236. }