host_key.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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. * Host functions for keys.
  6. */
  7. #include <stdio.h>
  8. #include <openssl/pem.h>
  9. #include "2sysincludes.h"
  10. #include "2common.h"
  11. #include "2rsa.h"
  12. #include "2sha.h"
  13. #include "vb21_common.h"
  14. #include "host_common.h"
  15. #include "host_key2.h"
  16. #include "host_misc.h"
  17. const struct vb2_text_vs_enum vb2_text_vs_sig[] = {
  18. {"RSA1024", VB2_SIG_RSA1024},
  19. {"RSA2048", VB2_SIG_RSA2048},
  20. {"RSA4096", VB2_SIG_RSA4096},
  21. {"RSA8192", VB2_SIG_RSA8192},
  22. {0, 0}
  23. };
  24. const struct vb2_text_vs_enum vb2_text_vs_hash[] = {
  25. {"SHA1", VB2_HASH_SHA1},
  26. {"SHA256", VB2_HASH_SHA256},
  27. {"SHA512", VB2_HASH_SHA512},
  28. {0, 0}
  29. };
  30. const struct vb2_text_vs_enum vb2_text_vs_crypto[] = {
  31. {"RSA1024 SHA1", VB2_ALG_RSA1024_SHA1},
  32. {"RSA1024 SHA256", VB2_ALG_RSA1024_SHA256},
  33. {"RSA1024 SHA512", VB2_ALG_RSA1024_SHA512},
  34. {"RSA2048 SHA1", VB2_ALG_RSA2048_SHA1},
  35. {"RSA2048 SHA256", VB2_ALG_RSA2048_SHA256},
  36. {"RSA2048 SHA512", VB2_ALG_RSA2048_SHA512},
  37. {"RSA4096 SHA1", VB2_ALG_RSA4096_SHA1},
  38. {"RSA4096 SHA256", VB2_ALG_RSA4096_SHA256},
  39. {"RSA4096 SHA512", VB2_ALG_RSA4096_SHA512},
  40. {"RSA8192 SHA1", VB2_ALG_RSA8192_SHA1},
  41. {"RSA8192 SHA256", VB2_ALG_RSA8192_SHA256},
  42. {"RSA8192 SHA512", VB2_ALG_RSA8192_SHA512},
  43. {0, 0}
  44. };
  45. const struct vb2_text_vs_enum *vb2_lookup_by_num(
  46. const struct vb2_text_vs_enum *table,
  47. const unsigned int num)
  48. {
  49. for (; table->name; table++)
  50. if (table->num == num)
  51. return table;
  52. return 0;
  53. }
  54. const struct vb2_text_vs_enum *vb2_lookup_by_name(
  55. const struct vb2_text_vs_enum *table,
  56. const char *name)
  57. {
  58. for (; table->name; table++)
  59. if (!strcasecmp(table->name, name))
  60. return table;
  61. return 0;
  62. }
  63. const char *vb2_get_sig_algorithm_name(enum vb2_signature_algorithm sig_alg)
  64. {
  65. const struct vb2_text_vs_enum *entry =
  66. vb2_lookup_by_num(vb2_text_vs_sig, sig_alg);
  67. return entry ? entry->name : VB2_INVALID_ALG_NAME;
  68. }
  69. const char *vb2_get_crypto_algorithm_name(enum vb2_crypto_algorithm alg)
  70. {
  71. const struct vb2_text_vs_enum *entry =
  72. vb2_lookup_by_num(vb2_text_vs_crypto, alg);
  73. return entry ? entry->name : VB2_INVALID_ALG_NAME;
  74. }
  75. void vb2_private_key_free(struct vb2_private_key *key)
  76. {
  77. if (!key)
  78. return;
  79. if (key->rsa_private_key)
  80. RSA_free(key->rsa_private_key);
  81. if (key->desc)
  82. free(key->desc);
  83. free(key);
  84. }
  85. int vb21_private_key_unpack(struct vb2_private_key **key_ptr,
  86. const uint8_t *buf,
  87. uint32_t size)
  88. {
  89. const struct vb21_packed_private_key *pkey =
  90. (const struct vb21_packed_private_key *)buf;
  91. struct vb2_private_key *key;
  92. const unsigned char *start;
  93. uint32_t min_offset = 0;
  94. *key_ptr = NULL;
  95. /*
  96. * Check magic number.
  97. *
  98. * TODO: If it doesn't match, pass through to the old packed key format.
  99. */
  100. if (pkey->c.magic != VB21_MAGIC_PACKED_PRIVATE_KEY)
  101. return VB2_ERROR_UNPACK_PRIVATE_KEY_MAGIC;
  102. if (vb21_verify_common_header(buf, size))
  103. return VB2_ERROR_UNPACK_PRIVATE_KEY_HEADER;
  104. /* Make sure key data is inside */
  105. if (vb21_verify_common_member(pkey, &min_offset,
  106. pkey->key_offset, pkey->key_size))
  107. return VB2_ERROR_UNPACK_PRIVATE_KEY_DATA;
  108. /*
  109. * Check for compatible version. No need to check minor version, since
  110. * that's compatible across readers matching the major version, and we
  111. * haven't added any new fields.
  112. */
  113. if (pkey->c.struct_version_major !=
  114. VB21_PACKED_PRIVATE_KEY_VERSION_MAJOR)
  115. return VB2_ERROR_UNPACK_PRIVATE_KEY_STRUCT_VERSION;
  116. /* Allocate the new key */
  117. key = calloc(1, sizeof(*key));
  118. if (!key)
  119. return VB2_ERROR_UNPACK_PRIVATE_KEY_ALLOC;
  120. /* Copy key algorithms and ID */
  121. key->sig_alg = pkey->sig_alg;
  122. key->hash_alg = pkey->hash_alg;
  123. key->id = pkey->id;
  124. /* Unpack RSA key */
  125. if (pkey->sig_alg == VB2_SIG_NONE) {
  126. if (pkey->key_size != 0) {
  127. free(key);
  128. return VB2_ERROR_UNPACK_PRIVATE_KEY_HASH;
  129. }
  130. } else {
  131. start = (const unsigned char *)(buf + pkey->key_offset);
  132. key->rsa_private_key = d2i_RSAPrivateKey(0, &start,
  133. pkey->key_size);
  134. if (!key->rsa_private_key) {
  135. free(key);
  136. return VB2_ERROR_UNPACK_PRIVATE_KEY_RSA;
  137. }
  138. }
  139. /* Key description */
  140. if (pkey->c.desc_size) {
  141. if (vb2_private_key_set_desc(
  142. key, (const char *)(buf + pkey->c.fixed_size))) {
  143. vb2_private_key_free(key);
  144. return VB2_ERROR_UNPACK_PRIVATE_KEY_DESC;
  145. }
  146. }
  147. *key_ptr = key;
  148. return VB2_SUCCESS;
  149. }
  150. int vb21_private_key_read(struct vb2_private_key **key_ptr,
  151. const char *filename)
  152. {
  153. uint32_t size = 0;
  154. uint8_t *buf = NULL;
  155. int rv;
  156. *key_ptr = NULL;
  157. rv = vb2_read_file(filename, &buf, &size);
  158. if (rv)
  159. return rv;
  160. rv = vb21_private_key_unpack(key_ptr, buf, size);
  161. free(buf);
  162. return rv;
  163. }
  164. int vb2_private_key_read_pem(struct vb2_private_key **key_ptr,
  165. const char *filename)
  166. {
  167. struct vb2_private_key *key;
  168. FILE *f;
  169. *key_ptr = NULL;
  170. /* Allocate the new key */
  171. key = calloc(1, sizeof(*key));
  172. if (!key)
  173. return VB2_ERROR_READ_PEM_ALLOC;
  174. /* Read private key */
  175. f = fopen(filename, "rb");
  176. if (!f) {
  177. free(key);
  178. return VB2_ERROR_READ_PEM_FILE_OPEN;
  179. }
  180. key->rsa_private_key = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL);
  181. fclose(f);
  182. if (!key->rsa_private_key) {
  183. free(key);
  184. return VB2_ERROR_READ_PEM_RSA;
  185. }
  186. *key_ptr = key;
  187. return VB2_SUCCESS;
  188. }
  189. int vb2_private_key_set_desc(struct vb2_private_key *key, const char *desc)
  190. {
  191. if (key->desc)
  192. free(key->desc);
  193. if (desc) {
  194. key->desc = strdup(desc);
  195. if (!key->desc)
  196. return VB2_ERROR_PRIVATE_KEY_SET_DESC;
  197. } else {
  198. key->desc = NULL;
  199. }
  200. return VB2_SUCCESS;
  201. }
  202. int vb21_private_key_write(const struct vb2_private_key *key,
  203. const char *filename)
  204. {
  205. struct vb21_packed_private_key pkey = {
  206. .c.magic = VB21_MAGIC_PACKED_PRIVATE_KEY,
  207. .c.struct_version_major = VB21_PACKED_PRIVATE_KEY_VERSION_MAJOR,
  208. .c.struct_version_minor = VB21_PACKED_PRIVATE_KEY_VERSION_MINOR,
  209. .c.fixed_size = sizeof(pkey),
  210. .sig_alg = key->sig_alg,
  211. .hash_alg = key->hash_alg,
  212. .id = key->id,
  213. };
  214. uint8_t *buf;
  215. uint8_t *rsabuf = NULL;
  216. int rsalen = 0;
  217. int rv;
  218. memcpy(&pkey.id, &key->id, sizeof(pkey.id));
  219. pkey.c.desc_size = vb2_desc_size(key->desc);
  220. if (key->sig_alg != VB2_SIG_NONE) {
  221. /* Pack RSA key */
  222. rsalen = i2d_RSAPrivateKey(key->rsa_private_key, &rsabuf);
  223. if (rsalen <= 0 || !rsabuf)
  224. return VB2_ERROR_PRIVATE_KEY_WRITE_RSA;
  225. }
  226. pkey.key_offset = pkey.c.fixed_size + pkey.c.desc_size;
  227. pkey.key_size = roundup32(rsalen);
  228. pkey.c.total_size = pkey.key_offset + pkey.key_size;
  229. /* Pack private key */
  230. buf = calloc(1, pkey.c.total_size);
  231. if (!buf) {
  232. free(rsabuf);
  233. return VB2_ERROR_PRIVATE_KEY_WRITE_ALLOC;
  234. }
  235. memcpy(buf, &pkey, sizeof(pkey));
  236. /* strcpy() is ok here because we checked the length above */
  237. if (pkey.c.desc_size)
  238. strcpy((char *)buf + pkey.c.fixed_size, key->desc);
  239. if (rsabuf) {
  240. memcpy(buf + pkey.key_offset, rsabuf, rsalen);
  241. free(rsabuf);
  242. }
  243. rv = vb21_write_object(filename, buf);
  244. free(buf);
  245. return rv ? VB2_ERROR_PRIVATE_KEY_WRITE_FILE : VB2_SUCCESS;
  246. }
  247. int vb2_private_key_hash(const struct vb2_private_key **key_ptr,
  248. enum vb2_hash_algorithm hash_alg)
  249. {
  250. *key_ptr = NULL;
  251. switch (hash_alg) {
  252. #if VB2_SUPPORT_SHA1
  253. case VB2_HASH_SHA1:
  254. {
  255. static const struct vb2_private_key key = {
  256. .hash_alg = VB2_HASH_SHA1,
  257. .sig_alg = VB2_SIG_NONE,
  258. .desc = "Unsigned SHA1",
  259. .id = VB2_ID_NONE_SHA1,
  260. };
  261. *key_ptr = &key;
  262. return VB2_SUCCESS;
  263. }
  264. #endif
  265. #if VB2_SUPPORT_SHA256
  266. case VB2_HASH_SHA256:
  267. {
  268. static const struct vb2_private_key key = {
  269. .hash_alg = VB2_HASH_SHA256,
  270. .sig_alg = VB2_SIG_NONE,
  271. .desc = "Unsigned SHA-256",
  272. .id = VB2_ID_NONE_SHA256,
  273. };
  274. *key_ptr = &key;
  275. return VB2_SUCCESS;
  276. }
  277. #endif
  278. #if VB2_SUPPORT_SHA512
  279. case VB2_HASH_SHA512:
  280. {
  281. static const struct vb2_private_key key = {
  282. .hash_alg = VB2_HASH_SHA512,
  283. .sig_alg = VB2_SIG_NONE,
  284. .desc = "Unsigned SHA-512",
  285. .id = VB2_ID_NONE_SHA512,
  286. };
  287. *key_ptr = &key;
  288. return VB2_SUCCESS;
  289. }
  290. #endif
  291. default:
  292. return VB2_ERROR_PRIVATE_KEY_HASH;
  293. }
  294. }
  295. int vb2_public_key_alloc(struct vb2_public_key **key_ptr,
  296. enum vb2_signature_algorithm sig_alg)
  297. {
  298. struct vb2_public_key *key;
  299. uint32_t key_data_size = vb2_packed_key_size(sig_alg);
  300. /* The buffer contains the key, its ID, and its packed data */
  301. uint32_t buf_size = sizeof(*key) + sizeof(struct vb2_id) +
  302. key_data_size;
  303. if (!key_data_size)
  304. return VB2_ERROR_PUBLIC_KEY_ALLOC_SIZE;
  305. key = calloc(1, buf_size);
  306. if (!key)
  307. return VB2_ERROR_PUBLIC_KEY_ALLOC;
  308. key->id = (struct vb2_id *)(key + 1);
  309. key->sig_alg = sig_alg;
  310. *key_ptr = key;
  311. return VB2_SUCCESS;
  312. }
  313. void vb2_public_key_free(struct vb2_public_key *key)
  314. {
  315. if (!key)
  316. return;
  317. if (key->desc)
  318. free((void *)key->desc);
  319. free(key);
  320. }
  321. uint8_t *vb2_public_key_packed_data(struct vb2_public_key *key)
  322. {
  323. return (uint8_t *)(key->id + 1);
  324. }
  325. int vb2_public_key_read_keyb(struct vb2_public_key **key_ptr,
  326. const char *filename)
  327. {
  328. struct vb2_public_key *key = NULL;
  329. uint8_t *key_data, *key_buf;
  330. uint32_t key_size;
  331. enum vb2_signature_algorithm sig_alg;
  332. *key_ptr = NULL;
  333. if (vb2_read_file(filename, &key_data, &key_size))
  334. return VB2_ERROR_READ_KEYB_DATA;
  335. /* Guess the signature algorithm from the key size */
  336. for (sig_alg = VB2_SIG_RSA1024; sig_alg <= VB2_SIG_RSA8192; sig_alg++) {
  337. if (key_size == vb2_packed_key_size(sig_alg))
  338. break;
  339. }
  340. if (sig_alg > VB2_SIG_RSA8192) {
  341. free(key_data);
  342. return VB2_ERROR_READ_KEYB_SIZE;
  343. }
  344. if (vb2_public_key_alloc(&key, sig_alg)) {
  345. free(key_data);
  346. return VB2_ERROR_READ_KEYB_ALLOC;
  347. }
  348. /* Copy data from the file buffer to the public key buffer */
  349. key_buf = vb2_public_key_packed_data(key);
  350. memcpy(key_buf, key_data, key_size);
  351. free(key_data);
  352. if (vb2_unpack_key_data(key, key_buf, key_size)) {
  353. vb2_public_key_free(key);
  354. return VB2_ERROR_READ_KEYB_UNPACK;
  355. }
  356. *key_ptr = key;
  357. return VB2_SUCCESS;
  358. }
  359. int vb2_public_key_set_desc(struct vb2_public_key *key, const char *desc)
  360. {
  361. if (key->desc)
  362. free((void *)key->desc);
  363. if (desc) {
  364. key->desc = strdup(desc);
  365. if (!key->desc)
  366. return VB2_ERROR_PUBLIC_KEY_SET_DESC;
  367. } else {
  368. key->desc = NULL;
  369. }
  370. return VB2_SUCCESS;
  371. }
  372. int vb21_packed_key_read(struct vb21_packed_key **key_ptr,
  373. const char *filename)
  374. {
  375. struct vb2_public_key key;
  376. uint8_t *buf;
  377. uint32_t size;
  378. *key_ptr = NULL;
  379. if (vb2_read_file(filename, &buf, &size))
  380. return VB2_ERROR_READ_PACKED_KEY_DATA;
  381. /* Sanity check: make sure key unpacks properly */
  382. if (vb21_unpack_key(&key, buf, size))
  383. return VB2_ERROR_READ_PACKED_KEY;
  384. *key_ptr = (struct vb21_packed_key *)buf;
  385. return VB2_SUCCESS;
  386. }
  387. int vb21_public_key_pack(struct vb21_packed_key **key_ptr,
  388. const struct vb2_public_key *pubk)
  389. {
  390. struct vb21_packed_key key = {
  391. .c.magic = VB21_MAGIC_PACKED_KEY,
  392. .c.struct_version_major = VB21_PACKED_KEY_VERSION_MAJOR,
  393. .c.struct_version_minor = VB21_PACKED_KEY_VERSION_MINOR,
  394. };
  395. uint8_t *buf;
  396. uint32_t *buf32;
  397. *key_ptr = NULL;
  398. /* Calculate sizes and offsets */
  399. key.c.fixed_size = sizeof(key);
  400. key.c.desc_size = vb2_desc_size(pubk->desc);
  401. key.key_offset = key.c.fixed_size + key.c.desc_size;
  402. if (pubk->sig_alg != VB2_SIG_NONE) {
  403. key.key_size = vb2_packed_key_size(pubk->sig_alg);
  404. if (!key.key_size)
  405. return VB2_ERROR_PUBLIC_KEY_PACK_SIZE;
  406. }
  407. key.c.total_size = key.key_offset + key.key_size;
  408. /* Copy/initialize fields */
  409. key.key_version = pubk->version;
  410. key.sig_alg = pubk->sig_alg;
  411. key.hash_alg = pubk->hash_alg;
  412. key.id = *pubk->id;
  413. /* Allocate the new buffer */
  414. buf = calloc(1, key.c.total_size);
  415. /* Copy data into the buffer */
  416. memcpy(buf, &key, sizeof(key));
  417. /* strcpy() is safe because we allocated above based on strlen() */
  418. if (pubk->desc && *pubk->desc) {
  419. strcpy((char *)(buf + key.c.fixed_size), pubk->desc);
  420. buf[key.c.fixed_size + key.c.desc_size - 1] = 0;
  421. }
  422. if (pubk->sig_alg != VB2_SIG_NONE) {
  423. /* Re-pack the key arrays */
  424. buf32 = (uint32_t *)(buf + key.key_offset);
  425. buf32[0] = pubk->arrsize;
  426. buf32[1] = pubk->n0inv;
  427. memcpy(buf32 + 2, pubk->n, pubk->arrsize * sizeof(uint32_t));
  428. memcpy(buf32 + 2 + pubk->arrsize, pubk->rr,
  429. pubk->arrsize * sizeof(uint32_t));
  430. }
  431. *key_ptr = (struct vb21_packed_key *)buf;
  432. return VB2_SUCCESS;
  433. }
  434. int vb2_public_key_hash(struct vb2_public_key *key,
  435. enum vb2_hash_algorithm hash_alg)
  436. {
  437. switch (hash_alg) {
  438. #if VB2_SUPPORT_SHA1
  439. case VB2_HASH_SHA1:
  440. key->desc = "Unsigned SHA1";
  441. break;
  442. #endif
  443. #if VB2_SUPPORT_SHA256
  444. case VB2_HASH_SHA256:
  445. key->desc = "Unsigned SHA-256";
  446. break;
  447. #endif
  448. #if VB2_SUPPORT_SHA512
  449. case VB2_HASH_SHA512:
  450. key->desc = "Unsigned SHA-512";
  451. break;
  452. #endif
  453. default:
  454. return VB2_ERROR_PUBLIC_KEY_HASH;
  455. }
  456. key->sig_alg = VB2_SIG_NONE;
  457. key->hash_alg = hash_alg;
  458. key->id = vb2_hash_id(hash_alg);
  459. return VB2_SUCCESS;
  460. }
  461. enum vb2_signature_algorithm vb2_rsa_sig_alg(struct rsa_st *rsa)
  462. {
  463. int bits = BN_num_bits(rsa->n);
  464. switch (bits) {
  465. case 1024:
  466. return VB2_SIG_RSA1024;
  467. case 2048:
  468. return VB2_SIG_RSA2048;
  469. case 4096:
  470. return VB2_SIG_RSA4096;
  471. case 8192:
  472. return VB2_SIG_RSA8192;
  473. }
  474. /* no clue */
  475. return VB2_SIG_INVALID;
  476. }
  477. int vb21_public_key_write(const struct vb2_public_key *key,
  478. const char *filename)
  479. {
  480. struct vb21_packed_key *pkey;
  481. int ret;
  482. ret = vb21_public_key_pack(&pkey, key);
  483. if (ret)
  484. return ret;
  485. ret = vb21_write_object(filename, pkey);
  486. free(pkey);
  487. return ret;
  488. }