Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 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 | #include <string> |
| 6 | #include <vector> |
| 7 | #include <gtest/gtest.h> |
| 8 | #include "base/logging.h" |
| 9 | #include "update_engine/payload_signer.h" |
| 10 | #include "update_engine/update_metadata.pb.h" |
| 11 | #include "update_engine/utils.h" |
| 12 | |
| 13 | using std::string; |
| 14 | using std::vector; |
| 15 | |
| 16 | // Note: the test key was generated with the following command: |
Andrew de los Reyes | bdfaaf0 | 2011-03-30 10:35:12 -0700 | [diff] [blame] | 17 | // openssl genrsa -out unittest_key.pem 2048 |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 18 | // The public-key version is created by the build system. |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 19 | |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 20 | namespace chromeos_update_engine { |
| 21 | |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 22 | const char* kUnittestPrivateKeyPath = "unittest_key.pem"; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 23 | const char* kUnittestPublicKeyPath = "unittest_key.pub.pem"; |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 24 | const char* kUnittestPrivateKey2Path = "unittest_key2.pem"; |
| 25 | const char* kUnittestPublicKey2Path = "unittest_key2.pub.pem"; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 26 | |
| 27 | // Some data and its corresponding hash and signature: |
| 28 | const char kDataToSign[] = "This is some data to sign."; |
Andrew de los Reyes | bdfaaf0 | 2011-03-30 10:35:12 -0700 | [diff] [blame] | 29 | |
| 30 | // Generated by: |
| 31 | // echo -n 'This is some data to sign.' | openssl dgst -sha256 -binary | |
| 32 | // hexdump -v -e '" " 8/1 "0x%02x, " "\n"' |
Han Shen | 2643cb7 | 2012-06-26 14:45:33 -0700 | [diff] [blame] | 33 | const unsigned char kDataHash[] = { |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 34 | 0x7a, 0x07, 0xa6, 0x44, 0x08, 0x86, 0x20, 0xa6, |
| 35 | 0xc1, 0xf8, 0xd9, 0x02, 0x05, 0x63, 0x0d, 0xb7, |
| 36 | 0xfc, 0x2b, 0xa0, 0xa9, 0x7c, 0x9d, 0x1d, 0x8c, |
| 37 | 0x01, 0xf5, 0x78, 0x6d, 0xc5, 0x11, 0xb4, 0x06 |
| 38 | }; |
Andrew de los Reyes | bdfaaf0 | 2011-03-30 10:35:12 -0700 | [diff] [blame] | 39 | |
| 40 | // Generated with openssl 1.0, which at the time of this writing, you need |
| 41 | // to download and install yourself. Here's my command: |
| 42 | // echo -n 'This is some data to sign.' | openssl dgst -sha256 -binary | |
| 43 | // ~/local/bin/openssl pkeyutl -sign -inkey unittest_key.pem -pkeyopt |
| 44 | // digest:sha256 | hexdump -v -e '" " 8/1 "0x%02x, " "\n"' |
Han Shen | 2643cb7 | 2012-06-26 14:45:33 -0700 | [diff] [blame] | 45 | const unsigned char kDataSignature[] = { |
Andrew de los Reyes | bdfaaf0 | 2011-03-30 10:35:12 -0700 | [diff] [blame] | 46 | 0x9f, 0x86, 0x25, 0x8b, 0xf3, 0xcc, 0xe3, 0x95, |
| 47 | 0x5f, 0x45, 0x83, 0xb2, 0x66, 0xf0, 0x2a, 0xcf, |
| 48 | 0xb7, 0xaa, 0x52, 0x25, 0x7a, 0xdd, 0x9d, 0x65, |
| 49 | 0xe5, 0xd6, 0x02, 0x4b, 0x37, 0x99, 0x53, 0x06, |
| 50 | 0xc2, 0xc9, 0x37, 0x36, 0x25, 0x62, 0x09, 0x4f, |
| 51 | 0x6b, 0x22, 0xf8, 0xb3, 0x89, 0x14, 0x98, 0x1a, |
| 52 | 0xbc, 0x30, 0x90, 0x4a, 0x43, 0xf5, 0xea, 0x2e, |
| 53 | 0xf0, 0xa4, 0xba, 0xc3, 0xa7, 0xa3, 0x44, 0x70, |
| 54 | 0xd6, 0xc4, 0x89, 0xd8, 0x45, 0x71, 0xbb, 0xee, |
| 55 | 0x59, 0x87, 0x3d, 0xd5, 0xe5, 0x40, 0x22, 0x3d, |
| 56 | 0x73, 0x7e, 0x2a, 0x58, 0x93, 0x8e, 0xcb, 0x9c, |
| 57 | 0xf2, 0xbb, 0x4a, 0xc9, 0xd2, 0x2c, 0x52, 0x42, |
| 58 | 0xb0, 0xd1, 0x13, 0x22, 0xa4, 0x78, 0xc7, 0xc6, |
| 59 | 0x3e, 0xf1, 0xdc, 0x4c, 0x7b, 0x2d, 0x40, 0xda, |
| 60 | 0x58, 0xac, 0x4a, 0x11, 0x96, 0x3d, 0xa0, 0x01, |
| 61 | 0xf6, 0x96, 0x74, 0xf6, 0x6c, 0x0c, 0x49, 0x69, |
| 62 | 0x4e, 0xc1, 0x7e, 0x9f, 0x2a, 0x42, 0xdd, 0x15, |
| 63 | 0x6b, 0x37, 0x2e, 0x3a, 0xa7, 0xa7, 0x6d, 0x91, |
| 64 | 0x13, 0xe8, 0x59, 0xde, 0xfe, 0x99, 0x07, 0xd9, |
| 65 | 0x34, 0x0f, 0x17, 0xb3, 0x05, 0x4c, 0xd2, 0xc6, |
| 66 | 0x82, 0xb7, 0x38, 0x36, 0x63, 0x1d, 0x9e, 0x21, |
| 67 | 0xa6, 0x32, 0xef, 0xf1, 0x65, 0xe6, 0xed, 0x95, |
| 68 | 0x25, 0x9b, 0x61, 0xe0, 0xba, 0x86, 0xa1, 0x7f, |
| 69 | 0xf8, 0xa5, 0x4a, 0x32, 0x1f, 0x15, 0x20, 0x8a, |
| 70 | 0x41, 0xc5, 0xb0, 0xd9, 0x4a, 0xda, 0x85, 0xf3, |
| 71 | 0xdc, 0xa0, 0x98, 0x5d, 0x1d, 0x18, 0x9d, 0x2e, |
| 72 | 0x42, 0xea, 0x69, 0x13, 0x74, 0x3c, 0x74, 0xf7, |
| 73 | 0x6d, 0x43, 0xb0, 0x63, 0x90, 0xdb, 0x04, 0xd5, |
| 74 | 0x05, 0xc9, 0x73, 0x1f, 0x6c, 0xd6, 0xfa, 0x46, |
| 75 | 0x4e, 0x0f, 0x33, 0x58, 0x5b, 0x0d, 0x1b, 0x55, |
| 76 | 0x39, 0xb9, 0x0f, 0x43, 0x37, 0xc0, 0x06, 0x0c, |
| 77 | 0x29, 0x93, 0x43, 0xc7, 0x43, 0xb9, 0xab, 0x7d |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 78 | }; |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 79 | |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 80 | //class PayloadSignerTest : public ::testing::Test {}; |
| 81 | |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 82 | namespace { |
| 83 | void SignSampleData(vector<char>* out_signature_blob) { |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 84 | string data_path; |
| 85 | ASSERT_TRUE( |
Gilad Arnold | a6742b3 | 2014-01-11 00:18:34 -0800 | [diff] [blame] | 86 | utils::MakeTempFile("data.XXXXXX", &data_path, NULL)); |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 87 | ScopedPathUnlinker data_path_unlinker(data_path); |
| 88 | ASSERT_TRUE(utils::WriteFile(data_path.c_str(), |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 89 | kDataToSign, |
| 90 | strlen(kDataToSign))); |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 91 | uint64_t length = 0; |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 92 | EXPECT_TRUE(PayloadSigner::SignatureBlobLength( |
| 93 | vector<string> (1, kUnittestPrivateKeyPath), |
| 94 | &length)); |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 95 | EXPECT_GT(length, 0); |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 96 | EXPECT_TRUE(PayloadSigner::SignPayload( |
| 97 | data_path, |
| 98 | vector<string>(1, kUnittestPrivateKeyPath), |
| 99 | out_signature_blob)); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 100 | EXPECT_EQ(length, out_signature_blob->size()); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | TEST(PayloadSignerTest, SimpleTest) { |
| 105 | vector<char> signature_blob; |
| 106 | SignSampleData(&signature_blob); |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 107 | |
| 108 | // Check the signature itself |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 109 | Signatures signatures; |
| 110 | EXPECT_TRUE(signatures.ParseFromArray(&signature_blob[0], |
| 111 | signature_blob.size())); |
| 112 | EXPECT_EQ(1, signatures.signatures_size()); |
| 113 | const Signatures_Signature& signature = signatures.signatures(0); |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 114 | EXPECT_EQ(kSignatureMessageOriginalVersion, signature.version()); |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 115 | const string sig_data = signature.data(); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 116 | ASSERT_EQ(arraysize(kDataSignature), sig_data.size()); |
| 117 | for (size_t i = 0; i < arraysize(kDataSignature); i++) { |
Han Shen | 2643cb7 | 2012-06-26 14:45:33 -0700 | [diff] [blame] | 118 | EXPECT_EQ(static_cast<char>(kDataSignature[i]), sig_data[i]); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
Darin Petkov | b039d50 | 2010-12-03 09:08:04 -0800 | [diff] [blame] | 122 | TEST(PayloadSignerTest, VerifySignatureTest) { |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 123 | vector<char> signature_blob; |
| 124 | SignSampleData(&signature_blob); |
| 125 | |
| 126 | vector<char> hash_data; |
| 127 | EXPECT_TRUE(PayloadSigner::VerifySignature(signature_blob, |
| 128 | kUnittestPublicKeyPath, |
| 129 | &hash_data)); |
Han Shen | 2643cb7 | 2012-06-26 14:45:33 -0700 | [diff] [blame] | 130 | vector<char> padded_hash_data(reinterpret_cast<const char *>(kDataHash), |
| 131 | reinterpret_cast<const char *>(kDataHash + |
| 132 | sizeof(kDataHash))); |
Andrew de los Reyes | bdfaaf0 | 2011-03-30 10:35:12 -0700 | [diff] [blame] | 133 | PayloadSigner::PadRSA2048SHA256Hash(&padded_hash_data); |
| 134 | ASSERT_EQ(padded_hash_data.size(), hash_data.size()); |
| 135 | for (size_t i = 0; i < padded_hash_data.size(); i++) { |
| 136 | EXPECT_EQ(padded_hash_data[i], hash_data[i]); |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
| 140 | } // namespace chromeos_update_engine |