host.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. * Host functions for signing
  6. */
  7. #include <unistd.h>
  8. #include "2sysincludes.h"
  9. #include "2common.h"
  10. #include "2sha.h"
  11. #include "bdb.h"
  12. #include "host.h"
  13. char *strzcpy(char *dest, const char *src, size_t size)
  14. {
  15. strncpy(dest, src, size);
  16. dest[size - 1] = 0;
  17. return dest;
  18. }
  19. uint8_t *read_file(const char *filename, uint32_t *size_ptr)
  20. {
  21. FILE *f;
  22. uint8_t *buf;
  23. long size;
  24. *size_ptr = 0;
  25. f = fopen(filename, "rb");
  26. if (!f) {
  27. fprintf(stderr, "Unable to open file %s\n", filename);
  28. return NULL;
  29. }
  30. fseek(f, 0, SEEK_END);
  31. size = ftell(f);
  32. rewind(f);
  33. if (size < 0 || size > UINT32_MAX) {
  34. fclose(f);
  35. return NULL;
  36. }
  37. buf = malloc(size);
  38. if (!buf) {
  39. fclose(f);
  40. return NULL;
  41. }
  42. if (1 != fread(buf, size, 1, f)) {
  43. fprintf(stderr, "Unable to read file %s\n", filename);
  44. fclose(f);
  45. free(buf);
  46. return NULL;
  47. }
  48. fclose(f);
  49. *size_ptr = size;
  50. return buf;
  51. }
  52. int write_file(const char *filename, const void *buf, uint32_t size)
  53. {
  54. FILE *f = fopen(filename, "wb");
  55. if (!f) {
  56. fprintf(stderr, "Unable to open file %s\n", filename);
  57. return 1;
  58. }
  59. if (1 != fwrite(buf, size, 1, f)) {
  60. fprintf(stderr, "Unable to write to file %s\n", filename);
  61. fclose(f);
  62. unlink(filename); /* Delete any partial file */
  63. return 1;
  64. }
  65. fclose(f);
  66. return 0;
  67. }
  68. struct rsa_st *read_pem(const char *filename)
  69. {
  70. struct rsa_st *pem;
  71. FILE *f;
  72. /* Read private key */
  73. f = fopen(filename, "rb");
  74. if (!f) {
  75. fprintf(stderr, "%s: unable to read key from %s\n",
  76. __func__, filename);
  77. return NULL;
  78. }
  79. pem = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL);
  80. fclose(f);
  81. return pem;
  82. }
  83. struct bdb_key *bdb_create_key(const char *filename,
  84. uint32_t key_version,
  85. const char *desc)
  86. {
  87. uint32_t sig_alg;
  88. size_t key_size = sizeof(struct bdb_key);
  89. struct bdb_key *k;
  90. uint8_t *kdata;
  91. uint32_t kdata_size = 0;
  92. /*
  93. * Read key data. Somewhat lame assumption that we can determine the
  94. * signature algorithm from the key size, but it's true right now.
  95. */
  96. kdata = read_file(filename, &kdata_size);
  97. if (kdata_size == BDB_RSA4096_KEY_DATA_SIZE) {
  98. sig_alg = BDB_SIG_ALG_RSA4096;
  99. } else if (kdata_size == BDB_RSA3072B_KEY_DATA_SIZE) {
  100. sig_alg = BDB_SIG_ALG_RSA3072B;
  101. } else {
  102. fprintf(stderr, "%s: bad key size from %s\n",
  103. __func__, filename);
  104. free(kdata);
  105. return NULL;
  106. }
  107. key_size += kdata_size;
  108. /* Allocate buffer */
  109. k = (struct bdb_key *)calloc(key_size, 1);
  110. if (!k) {
  111. free(kdata);
  112. return NULL;
  113. }
  114. k->struct_magic = BDB_KEY_MAGIC;
  115. k->struct_major_version = BDB_KEY_VERSION_MAJOR;
  116. k->struct_minor_version = BDB_KEY_VERSION_MINOR;
  117. k->struct_size = key_size;
  118. k->hash_alg = BDB_HASH_ALG_SHA256;
  119. k->sig_alg = sig_alg;
  120. k->key_version = key_version;
  121. /* Copy description, if any */
  122. if (desc)
  123. strzcpy(k->description, desc, sizeof(k->description));
  124. /* Copy key data */
  125. memcpy(k->key_data, kdata, kdata_size);
  126. free(kdata);
  127. return k;
  128. }
  129. struct bdb_sig *bdb_create_sig(const void *data,
  130. size_t size,
  131. struct rsa_st *key,
  132. uint32_t sig_alg,
  133. const char *desc)
  134. {
  135. static const uint8_t info[] = {
  136. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  137. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  138. 0x00, 0x04, 0x20
  139. };
  140. size_t sig_size = sizeof(struct bdb_sig);
  141. uint8_t digest[sizeof(info) + BDB_SHA256_DIGEST_SIZE];
  142. struct bdb_sig *sig;
  143. if (size >= UINT32_MAX)
  144. return NULL;
  145. switch(sig_alg) {
  146. case BDB_SIG_ALG_RSA4096:
  147. sig_size += BDB_RSA4096_SIG_SIZE;
  148. break;
  149. case BDB_SIG_ALG_RSA3072B:
  150. sig_size += BDB_RSA3072B_SIG_SIZE;
  151. break;
  152. default:
  153. fprintf(stderr, "%s: bad signature algorithm %d\n",
  154. __func__, sig_alg);
  155. return NULL;
  156. }
  157. /* Allocate buffer */
  158. sig = (struct bdb_sig *)calloc(sig_size, 1);
  159. if (!sig)
  160. return NULL;
  161. sig->struct_magic = BDB_SIG_MAGIC;
  162. sig->struct_major_version = BDB_SIG_VERSION_MAJOR;
  163. sig->struct_minor_version = BDB_SIG_VERSION_MINOR;
  164. sig->struct_size = sig_size;
  165. sig->hash_alg = BDB_HASH_ALG_SHA256;
  166. sig->sig_alg = sig_alg;
  167. sig->signed_size = size;
  168. /* Copy description, if any */
  169. if (desc)
  170. strzcpy(sig->description, desc, sizeof(sig->description));
  171. /* Calculate info-padded digest */
  172. memcpy(digest, info, sizeof(info));
  173. if (vb2_digest_buffer((uint8_t *)data, size,
  174. VB2_HASH_SHA256,
  175. digest + sizeof(info), BDB_SHA256_DIGEST_SIZE)) {
  176. free(sig);
  177. return NULL;
  178. }
  179. /* RSA-encrypt the signature */
  180. if (RSA_private_encrypt(sizeof(digest),
  181. digest,
  182. sig->sig_data,
  183. key,
  184. RSA_PKCS1_PADDING) == -1) {
  185. free(sig);
  186. return NULL;
  187. }
  188. return sig;
  189. }
  190. int bdb_sign_datakey(uint8_t **bdb, struct rsa_st *key)
  191. {
  192. const struct bdb_header *header = bdb_get_header(*bdb);
  193. const struct bdb_key *bdbkey = bdb_get_bdbkey(*bdb);
  194. const void *oem = bdb_get_oem_area_0(*bdb);
  195. const struct bdb_sig *sig = bdb_get_header_sig(*bdb);
  196. struct bdb_sig *new_sig;
  197. uint8_t *new_bdb, *src, *dst;
  198. size_t len;
  199. new_sig = bdb_create_sig(oem, header->signed_size,
  200. key, bdbkey->sig_alg, NULL);
  201. new_bdb = calloc(1, header->bdb_size
  202. + (new_sig->struct_size - sig->struct_size));
  203. if (!new_bdb)
  204. return BDB_ERROR_UNKNOWN;
  205. /* copy up to sig */
  206. src = *bdb;
  207. dst = new_bdb;
  208. len = bdb_offset_of_header_sig(*bdb);
  209. memcpy(dst, src, len);
  210. /* copy new sig */
  211. src += len;
  212. dst += len;
  213. memcpy(dst, new_sig, new_sig->struct_size);
  214. /* copy the rest */
  215. src += sig->struct_size;
  216. dst += new_sig->struct_size;
  217. len = bdb_size_of(*bdb) - vb2_offset_of(*bdb, src);
  218. memcpy(dst, src, len);
  219. free(*bdb);
  220. free(new_sig);
  221. *bdb = new_bdb;
  222. return BDB_SUCCESS;
  223. }
  224. int bdb_sign_data(uint8_t **bdb, struct rsa_st *key)
  225. {
  226. const struct bdb_key *datakey = bdb_get_datakey(*bdb);
  227. const struct bdb_data *data = bdb_get_data(*bdb);
  228. const uint64_t sig_offset = vb2_offset_of(*bdb, bdb_get_data_sig(*bdb));
  229. struct bdb_sig *new_sig;
  230. uint8_t *new_bdb;
  231. new_sig = bdb_create_sig(data, data->signed_size,
  232. key, datakey->sig_alg, NULL);
  233. new_bdb = calloc(1, sig_offset + new_sig->struct_size);
  234. if (!new_bdb)
  235. return BDB_ERROR_UNKNOWN;
  236. /* copy all data up to the data sig */
  237. memcpy(new_bdb, *bdb, sig_offset);
  238. /* copy the new signature */
  239. memcpy(new_bdb + sig_offset, new_sig, new_sig->struct_size);
  240. free(*bdb);
  241. free(new_sig);
  242. *bdb = new_bdb;
  243. return BDB_SUCCESS;
  244. }
  245. struct bdb_header *bdb_create(struct bdb_create_params *p)
  246. {
  247. size_t bdb_size = 0;
  248. size_t sig_size = sizeof(struct bdb_sig) + BDB_RSA4096_SIG_SIZE;
  249. size_t hashes_size = sizeof(struct bdb_hash) * p->num_hashes;
  250. uint8_t *buf, *bnext;
  251. struct bdb_header *h;
  252. struct bdb_sig *sig;
  253. struct bdb_data *data;
  254. const void *oem;
  255. /* We can do some checks before we even allocate the buffer */
  256. /* Make sure OEM sizes are aligned */
  257. if ((p->oem_area_0_size & 3) || (p->oem_area_1_size & 3)) {
  258. fprintf(stderr, "%s: OEM areas not 32-bit aligned\n",
  259. __func__);
  260. return NULL;
  261. }
  262. /* Hash count must fit in uint8_t */
  263. if (p->num_hashes > 255) {
  264. fprintf(stderr, "%s: too many hashes\n", __func__);
  265. return NULL;
  266. }
  267. /* Calculate BDB size */
  268. bdb_size = sizeof(struct bdb_header);
  269. bdb_size += p->bdbkey->struct_size;
  270. bdb_size += p->oem_area_0_size;
  271. bdb_size += p->datakey->struct_size;
  272. bdb_size += sig_size;
  273. bdb_size += sizeof(struct bdb_data);
  274. bdb_size += p->oem_area_1_size;
  275. bdb_size += sizeof(struct bdb_hash) * p->num_hashes;
  276. bdb_size += sig_size;
  277. /* Make sure it fits */
  278. if (bdb_size > UINT32_MAX) {
  279. fprintf(stderr, "%s: BDB size > UINT32_MAX\n", __func__);
  280. return NULL;
  281. }
  282. /* Allocate a buffer */
  283. bnext = buf = calloc(bdb_size, 1);
  284. if (!buf) {
  285. fprintf(stderr, "%s: can't allocate buffer\n", __func__);
  286. return NULL;
  287. }
  288. /* Fill in the header */
  289. h = (struct bdb_header *)bnext;
  290. h->struct_magic = BDB_HEADER_MAGIC;
  291. h->struct_major_version = BDB_HEADER_VERSION_MAJOR;
  292. h->struct_minor_version = BDB_HEADER_VERSION_MINOR;
  293. h->struct_size = sizeof(*h);
  294. h->bdb_load_address = p->bdb_load_address;
  295. h->bdb_size = bdb_size;
  296. h->signed_size = p->oem_area_0_size + p->datakey->struct_size;
  297. h->oem_area_0_size = p->oem_area_0_size;
  298. bnext += h->struct_size;
  299. /* Copy BDB key */
  300. memcpy(bnext, p->bdbkey, p->bdbkey->struct_size);
  301. bnext += p->bdbkey->struct_size;
  302. /* Copy OEM area 0 */
  303. oem = bnext;
  304. if (p->oem_area_0_size) {
  305. memcpy(bnext, p->oem_area_0, p->oem_area_0_size);
  306. bnext += p->oem_area_0_size;
  307. }
  308. /* Copy datakey */
  309. memcpy(bnext, p->datakey, p->datakey->struct_size);
  310. bnext += p->datakey->struct_size;
  311. /*
  312. * Create header signature using private BDB key.
  313. *
  314. * TODO: create the header signature in a totally separate step. That
  315. * way, the private BDB key is not required each time a BDB is created.
  316. */
  317. sig = bdb_create_sig(oem, h->signed_size, p->private_bdbkey,
  318. p->bdbkey->sig_alg, p->header_sig_description);
  319. memcpy(bnext, sig, sig->struct_size);
  320. bnext += sig->struct_size;
  321. /* Fill in the data */
  322. data = (struct bdb_data *)bnext;
  323. data->struct_magic = BDB_DATA_MAGIC;
  324. data->struct_major_version = BDB_DATA_VERSION_MAJOR;
  325. data->struct_minor_version = BDB_DATA_VERSION_MINOR;
  326. data->struct_size = sizeof(struct bdb_data);
  327. data->data_version = p->data_version;
  328. data->oem_area_1_size = p->oem_area_1_size;
  329. data->num_hashes = p->num_hashes;
  330. data->hash_entry_size = sizeof(struct bdb_hash);
  331. data->signed_size = data->struct_size + data->oem_area_1_size +
  332. hashes_size;
  333. if (p->data_description) {
  334. strzcpy(data->description, p->data_description,
  335. sizeof(data->description));
  336. }
  337. bnext += data->struct_size;
  338. /* Copy OEM area 1 */
  339. oem = bnext;
  340. if (p->oem_area_1_size) {
  341. memcpy(bnext, p->oem_area_1, p->oem_area_1_size);
  342. bnext += p->oem_area_1_size;
  343. }
  344. /* Copy hashes */
  345. memcpy(bnext, p->hash, hashes_size);
  346. bnext += hashes_size;
  347. /* Create data signature using private datakey */
  348. sig = bdb_create_sig(data, data->signed_size, p->private_datakey,
  349. p->datakey->sig_alg, p->data_sig_description);
  350. memcpy(bnext, sig, sig->struct_size);
  351. /* Return the BDB */
  352. return h;
  353. }