OLD | NEW |
1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
2 * Use of this source code is governed by a BSD-style license that can be | 2 * Use of this source code is governed by a BSD-style license that can be |
3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
4 * | 4 * |
5 * Tests for checking firmware rollback-prevention logic. | 5 * Tests for checking firmware rollback-prevention logic. |
6 */ | 6 */ |
7 | 7 |
8 #include <stdio.h> | 8 #include <stdio.h> |
9 #include <stdlib.h> | 9 #include <stdlib.h> |
10 | 10 |
11 #include "file_keys.h" | 11 #include "file_keys.h" |
12 #include "firmware_image.h" | 12 #include "firmware_image.h" |
13 #include "rsa_utility.h" | 13 #include "rsa_utility.h" |
14 #include "utility.h" | 14 #include "utility.h" |
15 #include "rollback_index.h" | 15 #include "rollback_index.h" |
16 #include "test_common.h" | 16 #include "test_common.h" |
17 | 17 |
18 /* Generates a test firmware image for rollback tests with a given | |
19 * [firmware_key_version] and [firmware_version]. If [is_corrupt] is 1, | |
20 * then the image has invalid signatures and will fail verification. */ | |
21 uint8_t* GenerateRollbackTestImage(int firmware_key_version, | |
22 int firmware_version, | |
23 int is_corrupt) { | |
24 FirmwareImage* image = NULL; | |
25 uint8_t* firmware_blob = NULL; | |
26 const char* firmare_sign_key_pub_file = "testkeys/key_rsa1024.keyb"; | |
27 uint8_t* firmware_sign_key = NULL; | |
28 const char* root_key_file = "testkeys/key_rsa8192.pem"; | |
29 const char* firmware_key_file = "testkeys/key_rsa1024.pem"; | |
30 uint64_t len; | |
31 firmware_sign_key = BufferFromFile(firmare_sign_key_pub_file, | |
32 &len); | |
33 if (!firmware_sign_key) | |
34 return NULL; | |
35 | |
36 image = FirmwareImageNew(); | |
37 if (!image) | |
38 return NULL; | |
39 | |
40 Memcpy(image->magic, FIRMWARE_MAGIC, FIRMWARE_MAGIC_SIZE); | |
41 image->firmware_sign_algorithm = 0; /* RSA1024/SHA1 */ | |
42 image->firmware_sign_key = (uint8_t*) Malloc( | |
43 RSAProcessedKeySize(image->firmware_sign_algorithm)); | |
44 Memcpy(image->firmware_sign_key, firmware_sign_key, | |
45 RSAProcessedKeySize(image->firmware_sign_algorithm)); | |
46 image->firmware_key_version = firmware_key_version; | |
47 Free(firmware_sign_key); | |
48 | |
49 /* Update correct header length. */ | |
50 image->header_len = GetFirmwareHeaderLen(image); | |
51 | |
52 /* Calculate SHA-512 digest on header and populate header_checksum. */ | |
53 CalculateFirmwareHeaderChecksum(image, image->header_checksum); | |
54 | |
55 /* Populate firmware and preamble with dummy data. */ | |
56 image->firmware_version = firmware_version; | |
57 image->firmware_len = 1; | |
58 image->preamble_signature = image->firmware_signature = NULL; | |
59 Memset(image->preamble, 'P', FIRMWARE_PREAMBLE_SIZE); | |
60 image->firmware_data = Malloc(image->firmware_len); | |
61 Memset(image->firmware_data, 'F', image->firmware_len); | |
62 | |
63 /* Generate and populate signatures. */ | |
64 if (!AddFirmwareKeySignature(image, root_key_file)) { | |
65 fprintf(stderr, "Couldn't create key signature.\n"); | |
66 FirmwareImageFree(image); | |
67 return NULL; | |
68 } | |
69 | |
70 if (!AddFirmwareSignature(image, firmware_key_file)) { | |
71 fprintf(stderr, "Couldn't create firmware and preamble signature.\n"); | |
72 FirmwareImageFree(image); | |
73 return NULL; | |
74 } | |
75 if (is_corrupt) { | |
76 /* Invalidate image. */ | |
77 Memset(image->firmware_data, 'X', image->firmware_len); | |
78 } | |
79 | |
80 firmware_blob = GetFirmwareBlob(image, &len); | |
81 FirmwareImageFree(image); | |
82 return firmware_blob; | |
83 } | |
84 | |
85 /* Tests that check for correctness of the VerifyFirmwareDriver_f() logic | 18 /* Tests that check for correctness of the VerifyFirmwareDriver_f() logic |
86 * and rollback prevention. */ | 19 * and rollback prevention. */ |
87 void VerifyFirmwareDriverTest(void) { | 20 void VerifyFirmwareDriverTest(void) { |
88 uint8_t* valid_firmwareA = NULL; | 21 uint8_t* valid_firmwareA = NULL; |
89 uint8_t* valid_firmwareB = NULL; | 22 uint8_t* valid_firmwareB = NULL; |
90 uint8_t* corrupt_firmwareA = NULL; | 23 uint8_t* corrupt_firmwareA = NULL; |
91 uint8_t* corrupt_firmwareB = NULL; | 24 uint8_t* corrupt_firmwareB = NULL; |
92 uint64_t len; | 25 uint64_t len; |
93 uint8_t* root_key_pub = BufferFromFile("testkeys/key_rsa8192.keyb", | 26 uint8_t* root_key_pub = BufferFromFile("testkeys/key_rsa8192.keyb", |
94 &len); | 27 &len); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 Free(corrupt_firmwareB); | 69 Free(corrupt_firmwareB); |
137 } | 70 } |
138 | 71 |
139 int main(int argc, char* argv[]) { | 72 int main(int argc, char* argv[]) { |
140 int error_code = 0; | 73 int error_code = 0; |
141 VerifyFirmwareDriverTest(); | 74 VerifyFirmwareDriverTest(); |
142 if (!gTestSuccess) | 75 if (!gTestSuccess) |
143 error_code = 255; | 76 error_code = 255; |
144 return error_code; | 77 return error_code; |
145 } | 78 } |
OLD | NEW |