• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2010 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #include "update_engine/payload_generator/payload_signer.h"
18 
19 #include <string>
20 #include <vector>
21 
22 #include <base/logging.h>
23 #include <base/stl_util.h>
24 #include <gtest/gtest.h>
25 
26 #include "update_engine/common/hash_calculator.h"
27 #include "update_engine/common/test_utils.h"
28 #include "update_engine/common/testing_constants.h"
29 #include "update_engine/common/utils.h"
30 #include "update_engine/payload_consumer/payload_constants.h"
31 #include "update_engine/payload_consumer/payload_verifier.h"
32 #include "update_engine/payload_generator/payload_file.h"
33 #include "update_engine/update_metadata.pb.h"
34 
35 using chromeos_update_engine::test_utils::GetBuildArtifactsPath;
36 using std::string;
37 using std::vector;
38 
39 // Note: the test key was generated with the following command:
40 // openssl genrsa -out unittest_key.pem 2048
41 // The public-key version is created by the build system.
42 
43 namespace chromeos_update_engine {
44 
45 // Some data and its corresponding hash and signature:
46 const char kDataToSign[] = "This is some data to sign.";
47 
48 // Generated by:
49 // echo -n 'This is some data to sign.' | openssl dgst -sha256 -binary |
50 //   hexdump -v -e '" " 8/1 "0x%02x, " "\n"'
51 const uint8_t kDataHash[] = {0x7a, 0x07, 0xa6, 0x44, 0x08, 0x86, 0x20, 0xa6,
52                              0xc1, 0xf8, 0xd9, 0x02, 0x05, 0x63, 0x0d, 0xb7,
53                              0xfc, 0x2b, 0xa0, 0xa9, 0x7c, 0x9d, 0x1d, 0x8c,
54                              0x01, 0xf5, 0x78, 0x6d, 0xc5, 0x11, 0xb4, 0x06};
55 
56 // Generated with openssl 1.0, which at the time of this writing, you need
57 // to download and install yourself. Here's my command:
58 // echo -n 'This is some data to sign.' | openssl dgst -sha256 -binary |
59 //    ~/local/bin/openssl pkeyutl -sign -inkey unittest_key.pem -pkeyopt
60 //    digest:sha256 | hexdump -v -e '" " 8/1 "0x%02x, " "\n"'
61 const uint8_t kDataSignature[] = {
62     0x9f, 0x86, 0x25, 0x8b, 0xf3, 0xcc, 0xe3, 0x95, 0x5f, 0x45, 0x83, 0xb2,
63     0x66, 0xf0, 0x2a, 0xcf, 0xb7, 0xaa, 0x52, 0x25, 0x7a, 0xdd, 0x9d, 0x65,
64     0xe5, 0xd6, 0x02, 0x4b, 0x37, 0x99, 0x53, 0x06, 0xc2, 0xc9, 0x37, 0x36,
65     0x25, 0x62, 0x09, 0x4f, 0x6b, 0x22, 0xf8, 0xb3, 0x89, 0x14, 0x98, 0x1a,
66     0xbc, 0x30, 0x90, 0x4a, 0x43, 0xf5, 0xea, 0x2e, 0xf0, 0xa4, 0xba, 0xc3,
67     0xa7, 0xa3, 0x44, 0x70, 0xd6, 0xc4, 0x89, 0xd8, 0x45, 0x71, 0xbb, 0xee,
68     0x59, 0x87, 0x3d, 0xd5, 0xe5, 0x40, 0x22, 0x3d, 0x73, 0x7e, 0x2a, 0x58,
69     0x93, 0x8e, 0xcb, 0x9c, 0xf2, 0xbb, 0x4a, 0xc9, 0xd2, 0x2c, 0x52, 0x42,
70     0xb0, 0xd1, 0x13, 0x22, 0xa4, 0x78, 0xc7, 0xc6, 0x3e, 0xf1, 0xdc, 0x4c,
71     0x7b, 0x2d, 0x40, 0xda, 0x58, 0xac, 0x4a, 0x11, 0x96, 0x3d, 0xa0, 0x01,
72     0xf6, 0x96, 0x74, 0xf6, 0x6c, 0x0c, 0x49, 0x69, 0x4e, 0xc1, 0x7e, 0x9f,
73     0x2a, 0x42, 0xdd, 0x15, 0x6b, 0x37, 0x2e, 0x3a, 0xa7, 0xa7, 0x6d, 0x91,
74     0x13, 0xe8, 0x59, 0xde, 0xfe, 0x99, 0x07, 0xd9, 0x34, 0x0f, 0x17, 0xb3,
75     0x05, 0x4c, 0xd2, 0xc6, 0x82, 0xb7, 0x38, 0x36, 0x63, 0x1d, 0x9e, 0x21,
76     0xa6, 0x32, 0xef, 0xf1, 0x65, 0xe6, 0xed, 0x95, 0x25, 0x9b, 0x61, 0xe0,
77     0xba, 0x86, 0xa1, 0x7f, 0xf8, 0xa5, 0x4a, 0x32, 0x1f, 0x15, 0x20, 0x8a,
78     0x41, 0xc5, 0xb0, 0xd9, 0x4a, 0xda, 0x85, 0xf3, 0xdc, 0xa0, 0x98, 0x5d,
79     0x1d, 0x18, 0x9d, 0x2e, 0x42, 0xea, 0x69, 0x13, 0x74, 0x3c, 0x74, 0xf7,
80     0x6d, 0x43, 0xb0, 0x63, 0x90, 0xdb, 0x04, 0xd5, 0x05, 0xc9, 0x73, 0x1f,
81     0x6c, 0xd6, 0xfa, 0x46, 0x4e, 0x0f, 0x33, 0x58, 0x5b, 0x0d, 0x1b, 0x55,
82     0x39, 0xb9, 0x0f, 0x43, 0x37, 0xc0, 0x06, 0x0c, 0x29, 0x93, 0x43, 0xc7,
83     0x43, 0xb9, 0xab, 0x7d};
84 
85 namespace {
SignSampleData(string * out_signature,const vector<string> & private_keys)86 void SignSampleData(string* out_signature, const vector<string>& private_keys) {
87   uint64_t length = 0;
88   EXPECT_TRUE(PayloadSigner::SignatureBlobLength(private_keys, &length));
89   EXPECT_GT(length, 0U);
90   brillo::Blob hash_blob;
91   EXPECT_TRUE(HashCalculator::RawHashOfBytes(
92       kDataToSign, strlen(kDataToSign), &hash_blob));
93   EXPECT_TRUE(
94       PayloadSigner::SignHashWithKeys(hash_blob, private_keys, out_signature));
95   EXPECT_EQ(length, out_signature->size());
96 }
97 }  // namespace
98 
99 class PayloadSignerTest : public ::testing::Test {
100  protected:
101   brillo::Blob hash_data_{std::begin(kDataHash), std::end(kDataHash)};
102 };
103 
TEST_F(PayloadSignerTest,SignSimpleTextTest)104 TEST_F(PayloadSignerTest, SignSimpleTextTest) {
105   string signature;
106   SignSampleData(&signature, {GetBuildArtifactsPath(kUnittestPrivateKeyPath)});
107 
108   // Check the signature itself
109   Signatures signatures;
110   EXPECT_TRUE(signatures.ParseFromString(signature));
111   EXPECT_EQ(1, signatures.signatures_size());
112   const Signatures::Signature& sig = signatures.signatures(0);
113   const string& sig_data = sig.data();
114   ASSERT_EQ(base::size(kDataSignature), sig_data.size());
115   for (size_t i = 0; i < base::size(kDataSignature); i++) {
116     EXPECT_EQ(kDataSignature[i], static_cast<uint8_t>(sig_data[i]));
117   }
118 }
119 
TEST_F(PayloadSignerTest,VerifyAllSignatureTest)120 TEST_F(PayloadSignerTest, VerifyAllSignatureTest) {
121   string signature;
122   SignSampleData(&signature,
123                  {GetBuildArtifactsPath(kUnittestPrivateKeyPath),
124                   GetBuildArtifactsPath(kUnittestPrivateKey2Path),
125                   GetBuildArtifactsPath(kUnittestPrivateKeyRSA4096Path),
126                   GetBuildArtifactsPath(kUnittestPrivateKeyECPath)});
127 
128   // Either public key should pass the verification.
129   for (const auto& path : {kUnittestPublicKeyPath,
130                            kUnittestPublicKey2Path,
131                            kUnittestPublicKeyRSA4096Path,
132                            kUnittestPublicKeyECPath}) {
133     string public_key;
134     EXPECT_TRUE(utils::ReadFile(GetBuildArtifactsPath(path), &public_key));
135     auto payload_verifier = PayloadVerifier::CreateInstance(public_key);
136     EXPECT_TRUE(payload_verifier != nullptr);
137     EXPECT_TRUE(payload_verifier->VerifySignature(signature, hash_data_));
138   }
139 }
140 
TEST_F(PayloadSignerTest,VerifySignatureTest)141 TEST_F(PayloadSignerTest, VerifySignatureTest) {
142   string signature;
143   SignSampleData(&signature, {GetBuildArtifactsPath(kUnittestPrivateKeyPath)});
144 
145   string public_key;
146   EXPECT_TRUE(utils::ReadFile(GetBuildArtifactsPath(kUnittestPublicKeyPath),
147                               &public_key));
148   auto payload_verifier = PayloadVerifier::CreateInstance(public_key);
149   EXPECT_TRUE(payload_verifier != nullptr);
150   EXPECT_TRUE(payload_verifier->VerifySignature(signature, hash_data_));
151 
152   // Passing the invalid key should fail the verification.
153   public_key.clear();
154   EXPECT_TRUE(utils::ReadFile(GetBuildArtifactsPath(kUnittestPublicKey2Path),
155                               &public_key));
156   payload_verifier = PayloadVerifier::CreateInstance(public_key);
157   EXPECT_TRUE(payload_verifier != nullptr);
158   EXPECT_FALSE(payload_verifier->VerifySignature(signature, hash_data_));
159 }
160 
TEST_F(PayloadSignerTest,VerifySignedPayloadTest)161 TEST_F(PayloadSignerTest, VerifySignedPayloadTest) {
162   ScopedTempFile payload_file("payload.XXXXXX");
163   PayloadGenerationConfig config;
164   config.version.major = kBrilloMajorPayloadVersion;
165   PayloadFile payload;
166   EXPECT_TRUE(payload.Init(config));
167   uint64_t metadata_size;
168   EXPECT_TRUE(
169       payload.WritePayload(payload_file.path(),
170                            "/dev/null",
171                            GetBuildArtifactsPath(kUnittestPrivateKeyPath),
172                            &metadata_size));
173   EXPECT_TRUE(PayloadSigner::VerifySignedPayload(
174       payload_file.path(), GetBuildArtifactsPath(kUnittestPublicKeyPath)));
175 }
176 
177 }  // namespace chromeos_update_engine
178