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