vb21_host_key_tests.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. * Tests for host library vboot2 key functions
  6. */
  7. #include <stdio.h>
  8. #include <unistd.h>
  9. #include "2sysincludes.h"
  10. #include "2common.h"
  11. #include "2rsa.h"
  12. #include "vb21_common.h"
  13. #include "host_common.h"
  14. #include "host_key2.h"
  15. #include "test_common.h"
  16. /* Test only the algorithms we use */
  17. struct alg_combo {
  18. const char *name;
  19. enum vb2_signature_algorithm sig_alg;
  20. enum vb2_hash_algorithm hash_alg;
  21. };
  22. static const struct alg_combo test_algs[] = {
  23. {"RSA2048/SHA-256", VB2_SIG_RSA2048, VB2_HASH_SHA256},
  24. {"RSA4096/SHA-256", VB2_SIG_RSA4096, VB2_HASH_SHA256},
  25. {"RSA8192/SHA-512", VB2_SIG_RSA8192, VB2_HASH_SHA512},
  26. };
  27. static void private_key_tests(const struct alg_combo *combo,
  28. const char *pemfile, const char *temp_dir)
  29. {
  30. struct vb2_private_key *key, *k2;
  31. const struct vb2_private_key *ckey;
  32. struct vb21_packed_private_key *pkey;
  33. char *testfile;
  34. const char notapem[] = "not_a_pem";
  35. const char testdesc[] = "test desc";
  36. const struct vb2_id test_id = {.raw = {0xaa}};
  37. uint8_t *buf, *buf2;
  38. uint32_t bufsize;
  39. xasprintf(&testfile, "%s/test.vbprik2", temp_dir);
  40. TEST_SUCC(vb2_private_key_read_pem(&key, pemfile), "Read pem - good");
  41. TEST_PTR_NEQ(key, NULL, " key_ptr");
  42. TEST_PTR_NEQ(key->rsa_private_key, NULL, " rsa_private_key");
  43. TEST_PTR_EQ(key->desc, NULL, " desc");
  44. vb2_private_key_free(key);
  45. TEST_EQ(vb2_private_key_read_pem(&key, "no_such_key"),
  46. VB2_ERROR_READ_PEM_FILE_OPEN, "Read pem - no key");
  47. TEST_PTR_EQ(key, NULL, " key_ptr");
  48. vb2_write_file(testfile, (const uint8_t *)notapem, sizeof(notapem));
  49. TEST_EQ(vb2_private_key_read_pem(&key, testfile),
  50. VB2_ERROR_READ_PEM_RSA, "Read pem - not a pem");
  51. unlink(testfile);
  52. TEST_SUCC(vb2_private_key_read_pem(&key, pemfile), "Read pem - good2");
  53. TEST_SUCC(vb2_private_key_set_desc(key, testdesc), "Set desc");
  54. TEST_PTR_NEQ(key->desc, NULL, " desc");
  55. TEST_PTR_NEQ(key->desc, testdesc, " made a copy");
  56. TEST_EQ(strcmp(key->desc, testdesc), 0, " right contents");
  57. TEST_SUCC(vb2_private_key_set_desc(key, NULL), "Clear desc");
  58. TEST_PTR_EQ(key->desc, NULL, " desc");
  59. TEST_SUCC(vb2_private_key_set_desc(key, testdesc), "Set desc");
  60. vb2_private_key_free(key);
  61. TEST_SUCC(vb2_private_key_read_pem(&key, pemfile), "Read pem - good3");
  62. TEST_SUCC(vb2_private_key_set_desc(key, testdesc), "Set desc");
  63. key->hash_alg = combo->hash_alg;
  64. key->sig_alg = combo->sig_alg;
  65. key->id = test_id;
  66. unlink(testfile);
  67. TEST_EQ(vb21_private_key_read(&k2, testfile),
  68. VB2_ERROR_READ_FILE_OPEN, "Read key no file");
  69. TEST_EQ(vb21_private_key_write(key, "no/such/dir"),
  70. VB2_ERROR_PRIVATE_KEY_WRITE_FILE, "Write key to bad path");
  71. TEST_SUCC(vb21_private_key_write(key, testfile), "Write key good");
  72. TEST_SUCC(vb21_private_key_read(&k2, testfile), "Read key good");
  73. TEST_PTR_NEQ(k2, NULL, " key_ptr");
  74. TEST_EQ(k2->sig_alg, key->sig_alg, " sig alg");
  75. TEST_EQ(k2->hash_alg, key->hash_alg, " hash alg");
  76. TEST_EQ(memcmp(&k2->id, &key->id, sizeof(k2->id)), 0, " id");
  77. TEST_EQ(strcmp(k2->desc, testdesc), 0, " desc");
  78. vb2_private_key_free(k2);
  79. TEST_SUCC(vb2_read_file(testfile, &buf, &bufsize), "Read key raw");
  80. pkey = (struct vb21_packed_private_key *)buf;
  81. /* Make a backup of the good buffer so we can mangle it */
  82. buf2 = malloc(bufsize);
  83. memcpy(buf2, buf, bufsize);
  84. TEST_SUCC(vb21_private_key_unpack(&k2, buf, bufsize),
  85. "Unpack private key good");
  86. vb2_private_key_free(k2);
  87. memcpy(buf, buf2, bufsize);
  88. pkey->c.magic = VB21_MAGIC_PACKED_KEY;
  89. TEST_EQ(vb21_private_key_unpack(&k2, buf, bufsize),
  90. VB2_ERROR_UNPACK_PRIVATE_KEY_MAGIC,
  91. "Unpack private key bad magic");
  92. TEST_PTR_EQ(k2, NULL, " key_ptr");
  93. memcpy(buf, buf2, bufsize);
  94. pkey->c.desc_size++;
  95. TEST_EQ(vb21_private_key_unpack(&k2, buf, bufsize),
  96. VB2_ERROR_UNPACK_PRIVATE_KEY_HEADER,
  97. "Unpack private key bad header");
  98. memcpy(buf, buf2, bufsize);
  99. pkey->key_size += pkey->c.total_size;
  100. TEST_EQ(vb21_private_key_unpack(&k2, buf, bufsize),
  101. VB2_ERROR_UNPACK_PRIVATE_KEY_DATA,
  102. "Unpack private key bad data size");
  103. memcpy(buf, buf2, bufsize);
  104. pkey->c.struct_version_major++;
  105. TEST_EQ(vb21_private_key_unpack(&k2, buf, bufsize),
  106. VB2_ERROR_UNPACK_PRIVATE_KEY_STRUCT_VERSION,
  107. "Unpack private key bad struct version");
  108. memcpy(buf, buf2, bufsize);
  109. pkey->c.struct_version_minor++;
  110. TEST_SUCC(vb21_private_key_unpack(&k2, buf, bufsize),
  111. "Unpack private key minor version");
  112. vb2_private_key_free(k2);
  113. memcpy(buf, buf2, bufsize);
  114. pkey->key_size -= 32;
  115. TEST_EQ(vb21_private_key_unpack(&k2, buf, bufsize),
  116. VB2_ERROR_UNPACK_PRIVATE_KEY_RSA,
  117. "Unpack private key bad rsa data");
  118. memcpy(buf, buf2, bufsize);
  119. pkey->sig_alg = VB2_SIG_NONE;
  120. TEST_EQ(vb21_private_key_unpack(&k2, buf, bufsize),
  121. VB2_ERROR_UNPACK_PRIVATE_KEY_HASH,
  122. "Unpack private key hash but has data");
  123. free(buf);
  124. free(buf2);
  125. unlink(testfile);
  126. TEST_EQ(vb2_private_key_hash(&ckey, VB2_HASH_INVALID),
  127. VB2_ERROR_PRIVATE_KEY_HASH,
  128. "Hash key invalid");
  129. TEST_PTR_EQ(ckey, NULL, " key_ptr");
  130. TEST_SUCC(vb2_private_key_hash(&ckey, combo->hash_alg), "Hash key");
  131. TEST_PTR_NEQ(ckey, NULL, " key_ptr");
  132. TEST_EQ(ckey->hash_alg, combo->hash_alg, " hash_alg");
  133. TEST_EQ(ckey->sig_alg, VB2_SIG_NONE, " sig_alg");
  134. TEST_EQ(memcmp(&ckey->id, vb2_hash_id(combo->hash_alg),
  135. sizeof(ckey->id)), 0, " id");
  136. TEST_SUCC(vb21_private_key_write(ckey, testfile), "Write hash key");
  137. TEST_SUCC(vb21_private_key_read(&key, testfile), "Read hash key");
  138. unlink(testfile);
  139. }
  140. static void public_key_tests(const struct alg_combo *combo,
  141. const char *keybfile, const char *temp_dir)
  142. {
  143. struct vb2_public_key *key, k2;
  144. struct vb21_packed_key *pkey;
  145. char *testfile;
  146. const char *testdesc = "test desc";
  147. const struct vb2_id test_id = {.raw = {0xbb}};
  148. const uint32_t test_version = 0xcc01;
  149. uint8_t *buf;
  150. uint32_t bufsize;
  151. xasprintf(&testfile, "%s/test.vbprik2", temp_dir);
  152. TEST_EQ(vb2_public_key_read_keyb(&key, "no_such_key"),
  153. VB2_ERROR_READ_KEYB_DATA, "Read keyb - no file");
  154. TEST_PTR_EQ(key, NULL, " key_ptr");
  155. TEST_SUCC(vb2_public_key_read_keyb(&key, keybfile), "Read keyb - good");
  156. TEST_PTR_NEQ(key, NULL, " key_ptr");
  157. TEST_EQ(key->sig_alg, combo->sig_alg, " sig_alg");
  158. TEST_PTR_EQ(key->desc, NULL, " desc");
  159. vb2_public_key_free(key);
  160. bufsize = vb2_packed_key_size(combo->sig_alg);
  161. buf = calloc(1, bufsize);
  162. vb2_write_file(testfile, buf, bufsize - 1);
  163. TEST_EQ(vb2_public_key_read_keyb(&key, testfile),
  164. VB2_ERROR_READ_KEYB_SIZE, "Read keyb - bad size");
  165. unlink(testfile);
  166. vb2_write_file(testfile, buf, bufsize);
  167. free(buf);
  168. TEST_EQ(vb2_public_key_read_keyb(&key, testfile),
  169. VB2_ERROR_READ_KEYB_UNPACK, "Read keyb - unpack");
  170. unlink(testfile);
  171. TEST_SUCC(vb2_public_key_read_keyb(&key, keybfile), "Read keyb 2");
  172. TEST_SUCC(vb2_public_key_set_desc(key, testdesc), "Set desc");
  173. TEST_PTR_NEQ(key->desc, NULL, " desc");
  174. TEST_PTR_NEQ(key->desc, testdesc, " made a copy");
  175. TEST_EQ(strcmp(key->desc, testdesc), 0, " right contents");
  176. TEST_SUCC(vb2_public_key_set_desc(key, NULL), "Clear desc");
  177. TEST_PTR_EQ(key->desc, NULL, " desc");
  178. TEST_SUCC(vb2_public_key_set_desc(key, testdesc), "Set desc");
  179. vb2_public_key_free(key);
  180. TEST_SUCC(vb2_public_key_read_keyb(&key, keybfile), "Read keyb 3");
  181. TEST_SUCC(vb2_public_key_set_desc(key, testdesc), "Set desc");
  182. key->hash_alg = combo->hash_alg;
  183. key->id = &test_id;
  184. key->version = test_version;
  185. TEST_SUCC(vb21_public_key_pack(&pkey, key), "Pack public key");
  186. TEST_PTR_NEQ(pkey, NULL, " key_ptr");
  187. TEST_EQ(pkey->hash_alg, key->hash_alg, " hash_alg");
  188. TEST_EQ(pkey->sig_alg, key->sig_alg, " sig_alg");
  189. TEST_EQ(pkey->key_version, key->version, " version");
  190. TEST_EQ(memcmp(&pkey->id, key->id, sizeof(pkey->id)), 0,
  191. " id");
  192. TEST_EQ(strcmp(vb21_common_desc(pkey), key->desc), 0, " desc");
  193. TEST_SUCC(vb21_unpack_key(&k2, (uint8_t *)pkey, pkey->c.total_size),
  194. "Unpack public key");
  195. TEST_EQ(key->arrsize, k2.arrsize, " arrsize");
  196. TEST_EQ(key->n0inv, k2.n0inv, " n0inv");
  197. TEST_EQ(memcmp(key->n, k2.n, key->arrsize * sizeof(uint32_t)), 0,
  198. " n");
  199. TEST_EQ(memcmp(key->rr, k2.rr, key->arrsize * sizeof(uint32_t)), 0,
  200. " rr");
  201. TEST_SUCC(vb21_write_object(testfile, pkey), "Write packed key");
  202. free(pkey);
  203. TEST_SUCC(vb21_packed_key_read(&pkey, testfile), "Read packed key");
  204. TEST_PTR_NEQ(pkey, NULL, " key_ptr");
  205. unlink(testfile);
  206. pkey->hash_alg = VB2_HASH_INVALID;
  207. TEST_SUCC(vb21_write_object(testfile, pkey), "Write bad packed key");
  208. free(pkey);
  209. TEST_EQ(vb21_packed_key_read(&pkey, testfile),
  210. VB2_ERROR_READ_PACKED_KEY, "Read bad packed key");
  211. TEST_PTR_EQ(pkey, NULL, " key_ptr");
  212. unlink(testfile);
  213. TEST_EQ(vb21_packed_key_read(&pkey, testfile),
  214. VB2_ERROR_READ_PACKED_KEY_DATA, "Read missing packed key");
  215. key->sig_alg = VB2_SIG_INVALID;
  216. TEST_EQ(vb21_public_key_pack(&pkey, key),
  217. VB2_ERROR_PUBLIC_KEY_PACK_SIZE,
  218. "Pack invalid sig alg");
  219. vb2_public_key_free(key);
  220. TEST_EQ(vb2_public_key_hash(&k2, VB2_HASH_INVALID),
  221. VB2_ERROR_PUBLIC_KEY_HASH,
  222. "Hash key invalid");
  223. TEST_SUCC(vb2_public_key_hash(&k2, combo->hash_alg), "Hash key");
  224. TEST_EQ(k2.hash_alg, combo->hash_alg, " hash_alg");
  225. TEST_EQ(k2.sig_alg, VB2_SIG_NONE, " sig_alg");
  226. TEST_EQ(memcmp(k2.id, vb2_hash_id(combo->hash_alg),
  227. sizeof(*k2.id)), 0, " id");
  228. TEST_SUCC(vb21_public_key_pack(&pkey, &k2), "Pack public hash key");
  229. TEST_PTR_NEQ(pkey, NULL, " key_ptr");
  230. TEST_SUCC(vb21_unpack_key(&k2, (uint8_t *)pkey, pkey->c.total_size),
  231. "Unpack public hash key");
  232. free(pkey);
  233. }
  234. static int test_algorithm(const struct alg_combo *combo, const char *keys_dir,
  235. const char *temp_dir)
  236. {
  237. int rsa_bits = vb2_rsa_sig_size(combo->sig_alg) * 8;
  238. char *pemfile;
  239. char *keybfile;
  240. printf("***Testing algorithm: %s\n", combo->name);
  241. xasprintf(&pemfile, "%s/key_rsa%d.pem", keys_dir, rsa_bits);
  242. xasprintf(&keybfile, "%s/key_rsa%d.keyb", keys_dir, rsa_bits);
  243. private_key_tests(combo, pemfile, temp_dir);
  244. public_key_tests(combo, keybfile, temp_dir);
  245. free(pemfile);
  246. free(keybfile);
  247. return 0;
  248. }
  249. int main(int argc, char *argv[]) {
  250. if (argc == 3) {
  251. const char *keys_dir = argv[1];
  252. const char *temp_dir = argv[2];
  253. int i;
  254. for (i = 0; i < ARRAY_SIZE(test_algs); i++) {
  255. if (test_algorithm(test_algs + i, keys_dir, temp_dir))
  256. return 1;
  257. }
  258. } else {
  259. fprintf(stderr, "Usage: %s <keys_dir> <temp_dir>\n", argv[0]);
  260. return -1;
  261. }
  262. return gTestSuccess ? 0 : 255;
  263. }