common.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. * Signature validation functions
  6. */
  7. #include "2sysincludes.h"
  8. #include "2common.h"
  9. #include "2rsa.h"
  10. #include "2sha.h"
  11. #include "vb21_common.h"
  12. const char *vb21_common_desc(const void *buf)
  13. {
  14. const struct vb21_struct_common *c = buf;
  15. return c->desc_size ? (const char *)c + c->fixed_size : "";
  16. }
  17. int vb21_verify_common_header(const void *parent, uint32_t parent_size)
  18. {
  19. const struct vb21_struct_common *c = parent;
  20. /* Parent buffer size must be at least the claimed total size */
  21. if (parent_size < c->total_size)
  22. return VB2_ERROR_COMMON_TOTAL_SIZE;
  23. /*
  24. * And big enough for the fixed size, which itself must be at least as
  25. * big as the common struct header.
  26. */
  27. if (c->total_size < c->fixed_size || c->fixed_size < sizeof(*c))
  28. return VB2_ERROR_COMMON_FIXED_SIZE;
  29. /* Make sure sizes are all multiples of 32 bits */
  30. if (!vb2_aligned(c->total_size, sizeof(uint32_t)))
  31. return VB2_ERROR_COMMON_TOTAL_UNALIGNED;
  32. if (!vb2_aligned(c->fixed_size, sizeof(uint32_t)))
  33. return VB2_ERROR_COMMON_FIXED_UNALIGNED;
  34. if (!vb2_aligned(c->desc_size, sizeof(uint32_t)))
  35. return VB2_ERROR_COMMON_DESC_UNALIGNED;
  36. /* Check description */
  37. if (c->desc_size > 0) {
  38. /* Make sure description fits and doesn't wrap */
  39. if (c->fixed_size + c->desc_size < c->fixed_size)
  40. return VB2_ERROR_COMMON_DESC_WRAPS;
  41. if (c->fixed_size + c->desc_size > c->total_size)
  42. return VB2_ERROR_COMMON_DESC_SIZE;
  43. /* Description must be null-terminated */
  44. if (vb21_common_desc(c)[c->desc_size - 1] != 0)
  45. return VB2_ERROR_COMMON_DESC_TERMINATOR;
  46. }
  47. return VB2_SUCCESS;
  48. }
  49. int vb21_verify_common_member(const void *parent,
  50. uint32_t *min_offset,
  51. uint32_t member_offset,
  52. uint32_t member_size)
  53. {
  54. const struct vb21_struct_common *c = parent;
  55. uint32_t member_end = member_offset + member_size;
  56. /* Make sure member doesn't wrap */
  57. if (member_end < member_offset)
  58. return VB2_ERROR_COMMON_MEMBER_WRAPS;
  59. /* Member offset and size must be 32-bit aligned */
  60. if (!vb2_aligned(member_offset, sizeof(uint32_t)) ||
  61. !vb2_aligned(member_size, sizeof(uint32_t)))
  62. return VB2_ERROR_COMMON_MEMBER_UNALIGNED;
  63. /* Initialize minimum offset if necessary */
  64. if (!*min_offset)
  65. *min_offset = c->fixed_size + c->desc_size;
  66. /* Member must be after minimum offset */
  67. if (member_offset < *min_offset)
  68. return VB2_ERROR_COMMON_MEMBER_OVERLAP;
  69. /* Member must end before total size */
  70. if (member_end > c->total_size)
  71. return VB2_ERROR_COMMON_MEMBER_SIZE;
  72. /* Update minimum offset for subsequent checks */
  73. *min_offset = member_end;
  74. return VB2_SUCCESS;
  75. }
  76. int vb21_verify_common_subobject(const void *parent,
  77. uint32_t *min_offset,
  78. uint32_t member_offset)
  79. {
  80. const struct vb21_struct_common *p = parent;
  81. const struct vb21_struct_common *m =
  82. (const struct vb21_struct_common *)
  83. ((const uint8_t *)parent + member_offset);
  84. int rv;
  85. /*
  86. * Verify the parent has space at the member offset for the common
  87. * header.
  88. */
  89. rv = vb21_verify_common_member(parent, min_offset, member_offset,
  90. sizeof(*m));
  91. if (rv)
  92. return rv;
  93. /*
  94. * Now it's safe to look at the member's header, and verify any
  95. * additional data for the object past its common header fits in the
  96. * parent.
  97. */
  98. rv = vb21_verify_common_header(m, p->total_size - member_offset);
  99. if (rv)
  100. return rv;
  101. /* Advance the min offset to the end of the subobject */
  102. *min_offset = member_offset + m->total_size;
  103. return VB2_SUCCESS;
  104. }
  105. uint32_t vb2_sig_size(enum vb2_signature_algorithm sig_alg,
  106. enum vb2_hash_algorithm hash_alg)
  107. {
  108. uint32_t digest_size = vb2_digest_size(hash_alg);
  109. /* Fail if we don't support the hash algorithm */
  110. if (!digest_size)
  111. return 0;
  112. /* Handle unsigned hashes */
  113. if (sig_alg == VB2_SIG_NONE)
  114. return digest_size;
  115. return vb2_rsa_sig_size(sig_alg);
  116. }
  117. const struct vb2_id *vb2_hash_id(enum vb2_hash_algorithm hash_alg)
  118. {
  119. switch(hash_alg) {
  120. #ifdef VB2_SUPPORT_SHA1
  121. case VB2_HASH_SHA1:
  122. {
  123. static const struct vb2_id id = VB2_ID_NONE_SHA1;
  124. return &id;
  125. }
  126. #endif
  127. #ifdef VB2_SUPPORT_SHA256
  128. case VB2_HASH_SHA256:
  129. {
  130. static const struct vb2_id id = VB2_ID_NONE_SHA256;
  131. return &id;
  132. }
  133. #endif
  134. #ifdef VB2_SUPPORT_SHA512
  135. case VB2_HASH_SHA512:
  136. {
  137. static const struct vb2_id id = VB2_ID_NONE_SHA512;
  138. return &id;
  139. }
  140. #endif
  141. default:
  142. return NULL;
  143. }
  144. }
  145. int vb21_verify_signature(const struct vb21_signature *sig, uint32_t size)
  146. {
  147. uint32_t min_offset = 0;
  148. uint32_t expect_sig_size;
  149. int rv;
  150. /* Check magic number */
  151. if (sig->c.magic != VB21_MAGIC_SIGNATURE)
  152. return VB2_ERROR_SIG_MAGIC;
  153. /* Make sure common header is good */
  154. rv = vb21_verify_common_header(sig, size);
  155. if (rv)
  156. return rv;
  157. /*
  158. * Check for compatible version. No need to check minor version, since
  159. * that's compatible across readers matching the major version, and we
  160. * haven't added any new fields.
  161. */
  162. if (sig->c.struct_version_major != VB21_SIGNATURE_VERSION_MAJOR)
  163. return VB2_ERROR_SIG_VERSION;
  164. /* Make sure header is big enough for signature */
  165. if (sig->c.fixed_size < sizeof(*sig))
  166. return VB2_ERROR_SIG_HEADER_SIZE;
  167. /* Make sure signature data is inside */
  168. rv = vb21_verify_common_member(sig, &min_offset,
  169. sig->sig_offset, sig->sig_size);
  170. if (rv)
  171. return rv;
  172. /* Make sure signature size is correct for the algorithm */
  173. expect_sig_size = vb2_sig_size(sig->sig_alg, sig->hash_alg);
  174. if (!expect_sig_size)
  175. return VB2_ERROR_SIG_ALGORITHM;
  176. if (sig->sig_size != expect_sig_size)
  177. return VB2_ERROR_SIG_SIZE;
  178. return VB2_SUCCESS;
  179. }
  180. /**
  181. * Return the signature data for a signature
  182. */
  183. static uint8_t *vb21_signature_data(struct vb21_signature *sig)
  184. {
  185. return (uint8_t *)sig + sig->sig_offset;
  186. }
  187. int vb21_verify_digest(const struct vb2_public_key *key,
  188. struct vb21_signature *sig,
  189. const uint8_t *digest,
  190. const struct vb2_workbuf *wb)
  191. {
  192. uint32_t key_sig_size = vb2_sig_size(key->sig_alg, key->hash_alg);
  193. /* If we can't figure out the signature size, key algorithm was bad */
  194. if (!key_sig_size)
  195. return VB2_ERROR_VDATA_ALGORITHM;
  196. /* Make sure the signature and key algorithms match */
  197. if (key->sig_alg != sig->sig_alg || key->hash_alg != sig->hash_alg)
  198. return VB2_ERROR_VDATA_ALGORITHM_MISMATCH;
  199. if (sig->sig_size != key_sig_size)
  200. return VB2_ERROR_VDATA_SIG_SIZE;
  201. if (key->sig_alg == VB2_SIG_NONE) {
  202. /* Bare hash */
  203. if (vb2_safe_memcmp(vb21_signature_data(sig),
  204. digest, key_sig_size))
  205. return VB2_ERROR_VDATA_VERIFY_DIGEST;
  206. return VB2_SUCCESS;
  207. } else {
  208. /* RSA-signed digest */
  209. return vb2_rsa_verify_digest(key,
  210. vb21_signature_data(sig),
  211. digest, wb);
  212. }
  213. }
  214. int vb21_verify_data(const void *data,
  215. uint32_t size,
  216. struct vb21_signature *sig,
  217. const struct vb2_public_key *key,
  218. const struct vb2_workbuf *wb)
  219. {
  220. struct vb2_workbuf wblocal = *wb;
  221. struct vb2_digest_context *dc;
  222. uint8_t *digest;
  223. uint32_t digest_size;
  224. int rv;
  225. if (sig->data_size != size) {
  226. VB2_DEBUG("Wrong amount of data signed.\n");
  227. return VB2_ERROR_VDATA_SIZE;
  228. }
  229. /* Digest goes at start of work buffer */
  230. digest_size = vb2_digest_size(key->hash_alg);
  231. if (!digest_size)
  232. return VB2_ERROR_VDATA_DIGEST_SIZE;
  233. digest = vb2_workbuf_alloc(&wblocal, digest_size);
  234. if (!digest)
  235. return VB2_ERROR_VDATA_WORKBUF_DIGEST;
  236. /* Hashing requires temp space for the context */
  237. dc = vb2_workbuf_alloc(&wblocal, sizeof(*dc));
  238. if (!dc)
  239. return VB2_ERROR_VDATA_WORKBUF_HASHING;
  240. rv = vb2_digest_init(dc, key->hash_alg);
  241. if (rv)
  242. return rv;
  243. rv = vb2_digest_extend(dc, data, size);
  244. if (rv)
  245. return rv;
  246. rv = vb2_digest_finalize(dc, digest, digest_size);
  247. if (rv)
  248. return rv;
  249. vb2_workbuf_free(&wblocal, sizeof(*dc));
  250. return vb21_verify_digest(key, sig, digest, &wblocal);
  251. }
  252. int vb21_verify_keyblock(struct vb21_keyblock *block,
  253. uint32_t size,
  254. const struct vb2_public_key *key,
  255. const struct vb2_workbuf *wb)
  256. {
  257. uint32_t min_offset = 0, sig_offset;
  258. int rv, i;
  259. /* Check magic number */
  260. if (block->c.magic != VB21_MAGIC_KEYBLOCK)
  261. return VB2_ERROR_KEYBLOCK_MAGIC;
  262. /* Make sure common header is good */
  263. rv = vb21_verify_common_header(block, size);
  264. if (rv)
  265. return rv;
  266. /*
  267. * Check for compatible version. No need to check minor version, since
  268. * that's compatible across readers matching the major version, and we
  269. * haven't added any new fields.
  270. */
  271. if (block->c.struct_version_major != VB21_KEYBLOCK_VERSION_MAJOR)
  272. return VB2_ERROR_KEYBLOCK_HEADER_VERSION;
  273. /* Make sure header is big enough */
  274. if (block->c.fixed_size < sizeof(*block))
  275. return VB2_ERROR_KEYBLOCK_SIZE;
  276. /* Make sure data key is inside */
  277. rv = vb21_verify_common_subobject(block, &min_offset,
  278. block->key_offset);
  279. if (rv)
  280. return rv;
  281. /* Loop over signatures */
  282. sig_offset = block->sig_offset;
  283. for (i = 0; i < block->sig_count; i++, sig_offset = min_offset) {
  284. struct vb21_signature *sig;
  285. /* Make sure signature is inside keyblock */
  286. rv = vb21_verify_common_subobject(block, &min_offset,
  287. sig_offset);
  288. if (rv)
  289. return rv;
  290. sig = (struct vb21_signature *)((uint8_t *)block + sig_offset);
  291. /* Verify the signature integrity */
  292. rv = vb21_verify_signature(sig,
  293. block->c.total_size - sig_offset);
  294. if (rv)
  295. return rv;
  296. /* Skip signature if it doesn't match the key ID */
  297. if (memcmp(&sig->id, key->id, VB2_ID_NUM_BYTES))
  298. continue;
  299. /* Make sure we signed the right amount of data */
  300. if (sig->data_size != block->sig_offset)
  301. return VB2_ERROR_KEYBLOCK_SIGNED_SIZE;
  302. return vb21_verify_data(block, block->sig_offset, sig, key, wb);
  303. }
  304. /* If we're still here, no signature matched the key ID */
  305. return VB2_ERROR_KEYBLOCK_SIG_ID;
  306. }
  307. int vb21_verify_fw_preamble(struct vb21_fw_preamble *preamble,
  308. uint32_t size,
  309. const struct vb2_public_key *key,
  310. const struct vb2_workbuf *wb)
  311. {
  312. struct vb21_signature *sig;
  313. uint32_t min_offset = 0, hash_offset;
  314. int rv, i;
  315. /* Check magic number */
  316. if (preamble->c.magic != VB21_MAGIC_FW_PREAMBLE)
  317. return VB2_ERROR_PREAMBLE_MAGIC;
  318. /* Make sure common header is good */
  319. rv = vb21_verify_common_header(preamble, size);
  320. if (rv)
  321. return rv;
  322. /*
  323. * Check for compatible version. No need to check minor version, since
  324. * that's compatible across readers matching the major version, and we
  325. * haven't added any new fields.
  326. */
  327. if (preamble->c.struct_version_major != VB21_FW_PREAMBLE_VERSION_MAJOR)
  328. return VB2_ERROR_PREAMBLE_HEADER_VERSION;
  329. /* Make sure header is big enough */
  330. if (preamble->c.fixed_size < sizeof(*preamble))
  331. return VB2_ERROR_PREAMBLE_SIZE;
  332. /* Make sure all hash signatures are inside */
  333. hash_offset = preamble->hash_offset;
  334. for (i = 0; i < preamble->hash_count; i++, hash_offset = min_offset) {
  335. /* Make sure signature is inside preamble */
  336. rv = vb21_verify_common_subobject(preamble, &min_offset,
  337. hash_offset);
  338. if (rv)
  339. return rv;
  340. sig = (struct vb21_signature *)
  341. ((uint8_t *)preamble + hash_offset);
  342. /* Verify the signature integrity */
  343. rv = vb21_verify_signature(
  344. sig, preamble->c.total_size - hash_offset);
  345. if (rv)
  346. return rv;
  347. /* Hashes must all be unsigned */
  348. if (sig->sig_alg != VB2_SIG_NONE)
  349. return VB2_ERROR_PREAMBLE_HASH_SIGNED;
  350. }
  351. /* Make sure signature is inside preamble */
  352. rv = vb21_verify_common_subobject(preamble, &min_offset,
  353. preamble->sig_offset);
  354. if (rv)
  355. return rv;
  356. /* Verify preamble signature */
  357. sig = (struct vb21_signature *)((uint8_t *)preamble +
  358. preamble->sig_offset);
  359. rv = vb21_verify_data(preamble, preamble->sig_offset, sig, key, wb);
  360. if (rv)
  361. return rv;
  362. return VB2_SUCCESS;
  363. }