OLD | NEW |
(Empty) | |
| 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 |
| 3 * found in the LICENSE file. |
| 4 * |
| 5 * Tests for checking firmware rollback-prevention logic. |
| 6 */ |
| 7 |
| 8 #include <stdio.h> |
| 9 #include <stdlib.h> |
| 10 |
| 11 #include "file_keys.h" |
| 12 #include "firmware_image.h" |
| 13 #include "rsa_utility.h" |
| 14 #include "utility.h" |
| 15 #include "rollback_index.h" |
| 16 #include "test_common.h" |
| 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 |
| 86 * and rollback prevention. */ |
| 87 void VerifyFirmwareDriverTest(void) { |
| 88 uint8_t* valid_firmwareA = NULL; |
| 89 uint8_t* valid_firmwareB = NULL; |
| 90 uint8_t* corrupt_firmwareA = NULL; |
| 91 uint8_t* corrupt_firmwareB = NULL; |
| 92 uint64_t len; |
| 93 uint8_t* root_key_pub = BufferFromFile("testkeys/key_rsa8192.keyb", |
| 94 &len); |
| 95 |
| 96 /* Initialize rollback index state. */ |
| 97 g_firmware_key_version = 1; |
| 98 g_firmware_version = 1; |
| 99 |
| 100 valid_firmwareA = GenerateRollbackTestImage(1, 1, 0); |
| 101 valid_firmwareB = GenerateRollbackTestImage(1, 1, 0); |
| 102 corrupt_firmwareA = GenerateRollbackTestImage(1, 1, 1); |
| 103 corrupt_firmwareB = GenerateRollbackTestImage(1, 1, 1); |
| 104 |
| 105 TEST_EQ(VerifyFirmwareDriver_f(root_key_pub, |
| 106 valid_firmwareA, valid_firmwareB), |
| 107 BOOT_FIRMWARE_A_CONTINUE, |
| 108 "Firmware A (Valid with current version), " |
| 109 "Firmware B (Valid with current version)"); |
| 110 TEST_EQ(VerifyFirmwareDriver_f(root_key_pub, |
| 111 corrupt_firmwareA, valid_firmwareB), |
| 112 BOOT_FIRMWARE_B_CONTINUE, |
| 113 "Firmware A (Corrupt with current version), " |
| 114 "FirmwareB (Valid with current version)"); |
| 115 TEST_EQ(VerifyFirmwareDriver_f(root_key_pub, |
| 116 valid_firmwareA, corrupt_firmwareB), |
| 117 BOOT_FIRMWARE_A_CONTINUE, |
| 118 "Firmware A (Valid with current version), " |
| 119 "FirmwareB (Corrupt with current version)"); |
| 120 TEST_EQ(VerifyFirmwareDriver_f(root_key_pub, |
| 121 corrupt_firmwareA, corrupt_firmwareB), |
| 122 BOOT_FIRMWARE_RECOVERY_CONTINUE, |
| 123 "Firmware A (Corrupt with current version), " |
| 124 "FirmwareB (Corrupt with current version"); |
| 125 g_firmware_key_version = 2; |
| 126 g_firmware_version = 2; |
| 127 TEST_EQ(VerifyFirmwareDriver_f(root_key_pub, valid_firmwareA, valid_firmwareB)
, |
| 128 BOOT_FIRMWARE_RECOVERY_CONTINUE, |
| 129 "Firmware A (Valid with old version), " |
| 130 "Old Firmware B (Valid with old version)"); |
| 131 |
| 132 Free(root_key_pub); |
| 133 Free(valid_firmwareA); |
| 134 Free(valid_firmwareB); |
| 135 Free(corrupt_firmwareA); |
| 136 Free(corrupt_firmwareB); |
| 137 } |
| 138 |
| 139 int main(int argc, char* argv[]) { |
| 140 int error_code = 0; |
| 141 VerifyFirmwareDriverTest(); |
| 142 if (!gTestSuccess) |
| 143 error_code = 255; |
| 144 return error_code; |
| 145 } |
OLD | NEW |