bdb.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /* Copyright 2015 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. * Boot descriptor block firmware functions
  6. */
  7. #include "2sysincludes.h"
  8. #include "2common.h"
  9. #include "2sha.h"
  10. #include "bdb.h"
  11. /*****************************************************************************/
  12. /**
  13. * Check if string contains a null terminator.
  14. *
  15. * Bytes after the null terminator do not need to be null.
  16. *
  17. * @param s String to check
  18. * @param size Size of string buffer in characters
  19. * @return 1 if string has a null terminator, 0 if not
  20. */
  21. int string_has_null(const char *s, size_t size)
  22. {
  23. for (; size; size--) {
  24. if (*s++ == 0)
  25. return 1;
  26. }
  27. return 0;
  28. }
  29. int bdb_check_header(const struct bdb_header *p, size_t size)
  30. {
  31. if (size < sizeof(*p) || size < p->struct_size)
  32. return BDB_ERROR_BUF_SIZE;
  33. if (p->struct_magic != BDB_HEADER_MAGIC)
  34. return BDB_ERROR_STRUCT_MAGIC;
  35. if (p->struct_major_version != BDB_HEADER_VERSION_MAJOR)
  36. return BDB_ERROR_STRUCT_VERSION;
  37. /* Note that minor version doesn't matter yet */
  38. if (p->struct_size < sizeof(*p))
  39. return BDB_ERROR_STRUCT_SIZE;
  40. if (p->oem_area_0_size & 3)
  41. return BDB_ERROR_OEM_AREA_SIZE; /* Not 32-bit aligned */
  42. /*
  43. * Make sure the BDB is at least big enough for us. At this point, all
  44. * the caller may have loaded is this header We'll check if there's
  45. * space for everything else after we load it.
  46. */
  47. if (p->bdb_size < sizeof(*p))
  48. return BDB_ERROR_BDB_SIZE;
  49. /*
  50. * The rest of the fields don't matter yet; we'll check them when we
  51. * check the BDB itself.
  52. */
  53. return BDB_SUCCESS;
  54. }
  55. int bdb_check_key(const struct bdb_key *p, size_t size)
  56. {
  57. size_t expect_key_size = 0;
  58. if (size < sizeof(*p) || size < p->struct_size)
  59. return BDB_ERROR_BUF_SIZE;
  60. if (p->struct_magic != BDB_KEY_MAGIC)
  61. return BDB_ERROR_STRUCT_MAGIC;
  62. if (p->struct_major_version != BDB_KEY_VERSION_MAJOR)
  63. return BDB_ERROR_STRUCT_VERSION;
  64. /* Note that minor version doesn't matter yet */
  65. if (!string_has_null(p->description, sizeof(p->description)))
  66. return BDB_ERROR_DESCRIPTION;
  67. /* We currently only support SHA-256 */
  68. if (p->hash_alg != BDB_HASH_ALG_SHA256)
  69. return BDB_ERROR_HASH_ALG;
  70. /* Make sure signature algorithm and size are correct */
  71. switch (p->sig_alg) {
  72. case BDB_SIG_ALG_RSA4096:
  73. expect_key_size = BDB_RSA4096_KEY_DATA_SIZE;
  74. break;
  75. case BDB_SIG_ALG_ECSDSA521:
  76. expect_key_size = BDB_ECDSA521_KEY_DATA_SIZE;
  77. break;
  78. case BDB_SIG_ALG_RSA3072B:
  79. expect_key_size = BDB_RSA3072B_KEY_DATA_SIZE;
  80. break;
  81. default:
  82. return BDB_ERROR_SIG_ALG;
  83. }
  84. if (p->struct_size < sizeof(*p) + expect_key_size)
  85. return BDB_ERROR_STRUCT_SIZE;
  86. return BDB_SUCCESS;
  87. }
  88. int bdb_check_sig(const struct bdb_sig *p, size_t size)
  89. {
  90. size_t expect_sig_size = 0;
  91. if (size < sizeof(*p) || size < p->struct_size)
  92. return BDB_ERROR_BUF_SIZE;
  93. if (p->struct_magic != BDB_SIG_MAGIC)
  94. return BDB_ERROR_STRUCT_MAGIC;
  95. if (p->struct_major_version != BDB_SIG_VERSION_MAJOR)
  96. return BDB_ERROR_STRUCT_VERSION;
  97. /* Note that minor version doesn't matter yet */
  98. if (!string_has_null(p->description, sizeof(p->description)))
  99. return BDB_ERROR_DESCRIPTION;
  100. /* We currently only support SHA-256 */
  101. if (p->hash_alg != BDB_HASH_ALG_SHA256)
  102. return BDB_ERROR_HASH_ALG;
  103. /* Make sure signature algorithm and size are correct */
  104. switch (p->sig_alg) {
  105. case BDB_SIG_ALG_RSA4096:
  106. expect_sig_size = BDB_RSA4096_SIG_SIZE;
  107. break;
  108. case BDB_SIG_ALG_ECSDSA521:
  109. expect_sig_size = BDB_ECDSA521_SIG_SIZE;
  110. break;
  111. case BDB_SIG_ALG_RSA3072B:
  112. expect_sig_size = BDB_RSA3072B_SIG_SIZE;
  113. break;
  114. default:
  115. return BDB_ERROR_SIG_ALG;
  116. }
  117. if (p->struct_size < sizeof(*p) + expect_sig_size)
  118. return BDB_ERROR_STRUCT_SIZE;
  119. return BDB_SUCCESS;
  120. }
  121. int bdb_check_data(const struct bdb_data *p, size_t size)
  122. {
  123. size_t need_size;
  124. if (size < sizeof(*p) || size < p->signed_size)
  125. return BDB_ERROR_BUF_SIZE;
  126. if (p->struct_magic != BDB_DATA_MAGIC)
  127. return BDB_ERROR_STRUCT_MAGIC;
  128. if (p->struct_major_version != BDB_DATA_VERSION_MAJOR)
  129. return BDB_ERROR_STRUCT_VERSION;
  130. /* Note that minor version doesn't matter yet */
  131. if (!string_has_null(p->description, sizeof(p->description)))
  132. return BDB_ERROR_DESCRIPTION;
  133. if (p->struct_size < sizeof(*p))
  134. return BDB_ERROR_STRUCT_SIZE;
  135. if (p->hash_entry_size < sizeof(struct bdb_hash))
  136. return BDB_ERROR_HASH_ENTRY_SIZE;
  137. /* Calculate expected size */
  138. need_size = p->struct_size + p->num_hashes * p->hash_entry_size;
  139. /* Make sure OEM area size doesn't cause wraparound */
  140. if (need_size + p->oem_area_1_size < need_size)
  141. return BDB_ERROR_OEM_AREA_SIZE;
  142. if (p->oem_area_1_size & 3)
  143. return BDB_ERROR_OEM_AREA_SIZE; /* Not 32-bit aligned */
  144. need_size += p->oem_area_1_size;
  145. if (p->signed_size < need_size)
  146. return BDB_ERROR_SIGNED_SIZE;
  147. return BDB_SUCCESS;
  148. }
  149. /*****************************************************************************/
  150. const struct bdb_header *bdb_get_header(const void *buf)
  151. {
  152. return buf;
  153. }
  154. uint32_t bdb_size_of(const void *buf)
  155. {
  156. return bdb_get_header(buf)->bdb_size;
  157. }
  158. const struct bdb_key *bdb_get_bdbkey(const void *buf)
  159. {
  160. const struct bdb_header *h = bdb_get_header(buf);
  161. const uint8_t *b8 = buf;
  162. /* BDB key follows header */
  163. return (const struct bdb_key *)(b8 + h->struct_size);
  164. }
  165. const void *bdb_get_oem_area_0(const void *buf)
  166. {
  167. const struct bdb_key *k = bdb_get_bdbkey(buf);
  168. const uint8_t *b8 = (const uint8_t *)k;
  169. /* OEM area 0 follows BDB key */
  170. return b8 + k->struct_size;
  171. }
  172. const struct bdb_key *bdb_get_datakey(const void *buf)
  173. {
  174. const struct bdb_header *h = bdb_get_header(buf);
  175. const uint8_t *b8 = bdb_get_oem_area_0(buf);
  176. /* datakey follows OEM area 0 */
  177. return (const struct bdb_key *)(b8 + h->oem_area_0_size);
  178. }
  179. ptrdiff_t bdb_offset_of_datakey(const void *buf)
  180. {
  181. return vb2_offset_of(buf, bdb_get_datakey(buf));
  182. }
  183. const struct bdb_sig *bdb_get_header_sig(const void *buf)
  184. {
  185. const struct bdb_header *h = bdb_get_header(buf);
  186. const uint8_t *b8 = bdb_get_oem_area_0(buf);
  187. /* Header signature starts after signed data */
  188. return (const struct bdb_sig *)(b8 + h->signed_size);
  189. }
  190. ptrdiff_t bdb_offset_of_header_sig(const void *buf)
  191. {
  192. return vb2_offset_of(buf, bdb_get_header_sig(buf));
  193. }
  194. const struct bdb_data *bdb_get_data(const void *buf)
  195. {
  196. const struct bdb_sig *s = bdb_get_header_sig(buf);
  197. const uint8_t *b8 = (const uint8_t *)s;
  198. /* Data follows header signature */
  199. return (const struct bdb_data *)(b8 + s->struct_size);
  200. }
  201. ptrdiff_t bdb_offset_of_data(const void *buf)
  202. {
  203. return vb2_offset_of(buf, bdb_get_data(buf));
  204. }
  205. const void *bdb_get_oem_area_1(const void *buf)
  206. {
  207. const struct bdb_data *p = bdb_get_data(buf);
  208. const uint8_t *b8 = (const uint8_t *)p;
  209. /* OEM area 1 follows BDB data */
  210. return b8 + p->struct_size;
  211. }
  212. static const void *bdb_get_hash(const void *buf)
  213. {
  214. const struct bdb_data *data = bdb_get_data(buf);
  215. const uint8_t *b8 = bdb_get_oem_area_1(buf);
  216. /* Hashes follow OEM area 0 */
  217. return b8 + data->oem_area_1_size;
  218. }
  219. const struct bdb_hash *bdb_get_hash_by_type(const void *buf,
  220. enum bdb_data_type type)
  221. {
  222. const struct bdb_data *data = bdb_get_data(buf);
  223. const uint8_t *b8 = bdb_get_hash(buf);
  224. int i;
  225. /* Search for a matching hash */
  226. for (i = 0; i < data->num_hashes; i++, b8 += data->hash_entry_size) {
  227. const struct bdb_hash *h = (const struct bdb_hash *)b8;
  228. if (h->type == type)
  229. return h;
  230. }
  231. return NULL;
  232. }
  233. const struct bdb_hash *bdb_get_hash_by_index(const void *buf, int index)
  234. {
  235. const struct bdb_data *data = bdb_get_data(buf);
  236. const uint8_t *p = bdb_get_hash(buf);
  237. const struct bdb_hash *h = NULL;
  238. int i;
  239. /* Search for a matching hash */
  240. for (i = 0; i < data->num_hashes; i++, p += data->hash_entry_size) {
  241. if (i == index) {
  242. h = (const struct bdb_hash *)p;
  243. break;
  244. }
  245. }
  246. return h;
  247. }
  248. const struct bdb_sig *bdb_get_data_sig(const void *buf)
  249. {
  250. const struct bdb_data *data = bdb_get_data(buf);
  251. const uint8_t *b8 = (const uint8_t *)data;
  252. /* Data signature starts after signed data */
  253. return (const struct bdb_sig *)(b8 + data->signed_size);
  254. }
  255. /*****************************************************************************/
  256. int bdb_verify_sig(const struct bdb_key *key,
  257. const struct bdb_sig *sig,
  258. const uint8_t *digest)
  259. {
  260. /* Key and signature algorithms must match */
  261. if (key->sig_alg != sig->sig_alg)
  262. return BDB_ERROR_SIG_ALG;
  263. switch (key->sig_alg) {
  264. case BDB_SIG_ALG_RSA4096:
  265. if (bdb_rsa4096_verify(key->key_data, sig->sig_data, digest))
  266. return BDB_ERROR_VERIFY_SIG;
  267. break;
  268. case BDB_SIG_ALG_ECSDSA521:
  269. if (bdb_ecdsa521_verify(key->key_data, sig->sig_data, digest))
  270. return BDB_ERROR_VERIFY_SIG;
  271. break;
  272. case BDB_SIG_ALG_RSA3072B:
  273. if (bdb_rsa3072b_verify(key->key_data, sig->sig_data, digest))
  274. return BDB_ERROR_VERIFY_SIG;
  275. break;
  276. default:
  277. return BDB_ERROR_VERIFY_SIG;
  278. }
  279. return BDB_SUCCESS;
  280. }
  281. int bdb_verify(const void *buf, size_t size, const uint8_t *bdb_key_digest)
  282. {
  283. const uint8_t *end = (const uint8_t *)buf + size;
  284. const struct bdb_header *h;
  285. const struct bdb_key *bdbkey, *datakey;
  286. const struct bdb_sig *sig;
  287. const struct bdb_data *data;
  288. const void *oem;
  289. uint8_t digest[BDB_SHA256_DIGEST_SIZE];
  290. int bdb_digest_mismatch = -1;
  291. /* Make sure buffer doesn't wrap around address space */
  292. if (end < (const uint8_t *)buf)
  293. return BDB_ERROR_BUF_SIZE;
  294. /*
  295. * Check header now that we've actually loaded it. We can't guarantee
  296. * this is the same header which was checked before.
  297. */
  298. h = bdb_get_header(buf);
  299. if (bdb_check_header(h, size))
  300. return BDB_ERROR_HEADER;
  301. /* Sanity-check BDB key */
  302. bdbkey = bdb_get_bdbkey(buf);
  303. if (bdb_check_key(bdbkey, end - (const uint8_t *)bdbkey))
  304. return BDB_ERROR_BDBKEY;
  305. /* Calculate BDB key digest and compare with expected */
  306. if (vb2_digest_buffer((uint8_t *)bdbkey, bdbkey->struct_size,
  307. VB2_HASH_SHA256, digest, BDB_SHA256_DIGEST_SIZE))
  308. return BDB_ERROR_DIGEST;
  309. if (bdb_key_digest)
  310. bdb_digest_mismatch = memcmp(digest,
  311. bdb_key_digest, sizeof(digest));
  312. /* Make sure OEM area 0 fits */
  313. oem = bdb_get_oem_area_0(buf);
  314. if (h->oem_area_0_size > end - (const uint8_t *)oem)
  315. return BDB_ERROR_OEM_AREA_0;
  316. /* Sanity-check datakey */
  317. datakey = bdb_get_datakey(buf);
  318. if (bdb_check_key(datakey, end - (const uint8_t *)datakey))
  319. return BDB_ERROR_DATAKEY;
  320. /* Make sure enough data was signed, and the signed data fits */
  321. if (h->oem_area_0_size + datakey->struct_size > h->signed_size ||
  322. h->signed_size > end - (const uint8_t *)oem)
  323. return BDB_ERROR_BDB_SIGNED_SIZE;
  324. /* Sanity-check header signature */
  325. sig = bdb_get_header_sig(buf);
  326. if (bdb_check_sig(sig, end - (const uint8_t *)sig))
  327. return BDB_ERROR_HEADER_SIG;
  328. /* Make sure it signed the right amount of data */
  329. if (sig->signed_size != h->signed_size)
  330. return BDB_ERROR_HEADER_SIG;
  331. /* Calculate header digest and compare with expected signature */
  332. if (vb2_digest_buffer((uint8_t *)oem, h->signed_size,
  333. VB2_HASH_SHA256, digest, BDB_SHA256_DIGEST_SIZE))
  334. return BDB_ERROR_DIGEST;
  335. if (bdb_verify_sig(bdbkey, sig, digest))
  336. return BDB_ERROR_HEADER_SIG;
  337. /*
  338. * Sanity-check data struct. This also checks that OEM area 1 and the
  339. * hashes fit in the remaining buffer.
  340. */
  341. data = bdb_get_data(buf);
  342. if (bdb_check_data(data, end - (const uint8_t *)data))
  343. return BDB_ERROR_DATA;
  344. /* Sanity-check data signature */
  345. sig = bdb_get_data_sig(buf);
  346. if (bdb_check_sig(sig, end - (const uint8_t *)sig))
  347. return BDB_ERROR_DATA_CHECK_SIG;
  348. if (sig->signed_size != data->signed_size)
  349. return BDB_ERROR_DATA_SIGNED_SIZE;
  350. /* Calculate data digest and compare with expected signature */
  351. if (vb2_digest_buffer((uint8_t *)data, data->signed_size,
  352. VB2_HASH_SHA256, digest, BDB_SHA256_DIGEST_SIZE))
  353. return BDB_ERROR_DIGEST;
  354. if (bdb_verify_sig(datakey, sig, digest))
  355. return BDB_ERROR_DATA_SIG;
  356. /* Return success or success-other-than-BDB-key-mismatch */
  357. return bdb_digest_mismatch ? BDB_GOOD_OTHER_THAN_KEY : BDB_SUCCESS;
  358. }