Home | History | Annotate | Download | only in payload_generator
      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 <gtest/gtest.h>
     24 
     25 #include "update_engine/common/hash_calculator.h"
     26 #include "update_engine/common/test_utils.h"
     27 #include "update_engine/common/utils.h"
     28 #include "update_engine/payload_consumer/payload_constants.h"
     29 #include "update_engine/payload_consumer/payload_verifier.h"
     30 #include "update_engine/payload_generator/payload_file.h"
     31 #include "update_engine/update_metadata.pb.h"
     32 
     33 using chromeos_update_engine::test_utils::GetBuildArtifactsPath;
     34 using std::string;
     35 using std::vector;
     36 
     37 // Note: the test key was generated with the following command:
     38 // openssl genrsa -out unittest_key.pem 2048
     39 // The public-key version is created by the build system.
     40 
     41 namespace chromeos_update_engine {
     42 
     43 const char* kUnittestPrivateKeyPath = "unittest_key.pem";
     44 const char* kUnittestPublicKeyPath = "unittest_key.pub.pem";
     45 const char* kUnittestPrivateKey2Path = "unittest_key2.pem";
     46 const char* kUnittestPublicKey2Path = "unittest_key2.pub.pem";
     47 
     48 // Some data and its corresponding hash and signature:
     49 const char kDataToSign[] = "This is some data to sign.";
     50 
     51 // Generated by:
     52 // echo -n 'This is some data to sign.' | openssl dgst -sha256 -binary |
     53 //   hexdump -v -e '" " 8/1 "0x%02x, " "\n"'
     54 const uint8_t kDataHash[] = {
     55   0x7a, 0x07, 0xa6, 0x44, 0x08, 0x86, 0x20, 0xa6,
     56   0xc1, 0xf8, 0xd9, 0x02, 0x05, 0x63, 0x0d, 0xb7,
     57   0xfc, 0x2b, 0xa0, 0xa9, 0x7c, 0x9d, 0x1d, 0x8c,
     58   0x01, 0xf5, 0x78, 0x6d, 0xc5, 0x11, 0xb4, 0x06
     59 };
     60 
     61 // Generated with openssl 1.0, which at the time of this writing, you need
     62 // to download and install yourself. Here's my command:
     63 // echo -n 'This is some data to sign.' | openssl dgst -sha256 -binary |
     64 //    ~/local/bin/openssl pkeyutl -sign -inkey unittest_key.pem -pkeyopt
     65 //    digest:sha256 | hexdump -v -e '" " 8/1 "0x%02x, " "\n"'
     66 const uint8_t kDataSignature[] = {
     67   0x9f, 0x86, 0x25, 0x8b, 0xf3, 0xcc, 0xe3, 0x95,
     68   0x5f, 0x45, 0x83, 0xb2, 0x66, 0xf0, 0x2a, 0xcf,
     69   0xb7, 0xaa, 0x52, 0x25, 0x7a, 0xdd, 0x9d, 0x65,
     70   0xe5, 0xd6, 0x02, 0x4b, 0x37, 0x99, 0x53, 0x06,
     71   0xc2, 0xc9, 0x37, 0x36, 0x25, 0x62, 0x09, 0x4f,
     72   0x6b, 0x22, 0xf8, 0xb3, 0x89, 0x14, 0x98, 0x1a,
     73   0xbc, 0x30, 0x90, 0x4a, 0x43, 0xf5, 0xea, 0x2e,
     74   0xf0, 0xa4, 0xba, 0xc3, 0xa7, 0xa3, 0x44, 0x70,
     75   0xd6, 0xc4, 0x89, 0xd8, 0x45, 0x71, 0xbb, 0xee,
     76   0x59, 0x87, 0x3d, 0xd5, 0xe5, 0x40, 0x22, 0x3d,
     77   0x73, 0x7e, 0x2a, 0x58, 0x93, 0x8e, 0xcb, 0x9c,
     78   0xf2, 0xbb, 0x4a, 0xc9, 0xd2, 0x2c, 0x52, 0x42,
     79   0xb0, 0xd1, 0x13, 0x22, 0xa4, 0x78, 0xc7, 0xc6,
     80   0x3e, 0xf1, 0xdc, 0x4c, 0x7b, 0x2d, 0x40, 0xda,
     81   0x58, 0xac, 0x4a, 0x11, 0x96, 0x3d, 0xa0, 0x01,
     82   0xf6, 0x96, 0x74, 0xf6, 0x6c, 0x0c, 0x49, 0x69,
     83   0x4e, 0xc1, 0x7e, 0x9f, 0x2a, 0x42, 0xdd, 0x15,
     84   0x6b, 0x37, 0x2e, 0x3a, 0xa7, 0xa7, 0x6d, 0x91,
     85   0x13, 0xe8, 0x59, 0xde, 0xfe, 0x99, 0x07, 0xd9,
     86   0x34, 0x0f, 0x17, 0xb3, 0x05, 0x4c, 0xd2, 0xc6,
     87   0x82, 0xb7, 0x38, 0x36, 0x63, 0x1d, 0x9e, 0x21,
     88   0xa6, 0x32, 0xef, 0xf1, 0x65, 0xe6, 0xed, 0x95,
     89   0x25, 0x9b, 0x61, 0xe0, 0xba, 0x86, 0xa1, 0x7f,
     90   0xf8, 0xa5, 0x4a, 0x32, 0x1f, 0x15, 0x20, 0x8a,
     91   0x41, 0xc5, 0xb0, 0xd9, 0x4a, 0xda, 0x85, 0xf3,
     92   0xdc, 0xa0, 0x98, 0x5d, 0x1d, 0x18, 0x9d, 0x2e,
     93   0x42, 0xea, 0x69, 0x13, 0x74, 0x3c, 0x74, 0xf7,
     94   0x6d, 0x43, 0xb0, 0x63, 0x90, 0xdb, 0x04, 0xd5,
     95   0x05, 0xc9, 0x73, 0x1f, 0x6c, 0xd6, 0xfa, 0x46,
     96   0x4e, 0x0f, 0x33, 0x58, 0x5b, 0x0d, 0x1b, 0x55,
     97   0x39, 0xb9, 0x0f, 0x43, 0x37, 0xc0, 0x06, 0x0c,
     98   0x29, 0x93, 0x43, 0xc7, 0x43, 0xb9, 0xab, 0x7d
     99 };
    100 
    101 namespace {
    102 void SignSampleData(brillo::Blob* out_signature_blob,
    103                     const vector<string>& private_keys) {
    104   brillo::Blob data_blob(std::begin(kDataToSign),
    105                          std::begin(kDataToSign) + strlen(kDataToSign));
    106   uint64_t length = 0;
    107   EXPECT_TRUE(PayloadSigner::SignatureBlobLength(private_keys, &length));
    108   EXPECT_GT(length, 0U);
    109   brillo::Blob hash_blob;
    110   EXPECT_TRUE(HashCalculator::RawHashOfBytes(data_blob.data(),
    111                                              data_blob.size(),
    112                                              &hash_blob));
    113   EXPECT_TRUE(PayloadSigner::SignHashWithKeys(
    114       hash_blob,
    115       private_keys,
    116       out_signature_blob));
    117   EXPECT_EQ(length, out_signature_blob->size());
    118 }
    119 }  // namespace
    120 
    121 class PayloadSignerTest : public ::testing::Test {
    122  protected:
    123   void SetUp() override {
    124     PayloadVerifier::PadRSA2048SHA256Hash(&padded_hash_data_);
    125   }
    126 
    127   void DoWriteAndLoadPayloadTest(const PayloadGenerationConfig& config) {
    128     PayloadFile payload;
    129     payload.Init(config);
    130     string payload_path;
    131     EXPECT_TRUE(utils::MakeTempFile("payload.XXXXXX", &payload_path, nullptr));
    132     ScopedPathUnlinker payload_path_unlinker(payload_path);
    133     uint64_t metadata_size;
    134     EXPECT_TRUE(
    135         payload.WritePayload(payload_path, "/dev/null", "", &metadata_size));
    136     brillo::Blob payload_metadata_blob;
    137     DeltaArchiveManifest manifest;
    138     uint64_t load_metadata_size, load_major_version;
    139     EXPECT_TRUE(PayloadSigner::LoadPayloadMetadata(payload_path,
    140                                                    &payload_metadata_blob,
    141                                                    &manifest,
    142                                                    &load_major_version,
    143                                                    &load_metadata_size,
    144                                                    nullptr));
    145     EXPECT_EQ(metadata_size, payload_metadata_blob.size());
    146     EXPECT_EQ(config.version.major, load_major_version);
    147     EXPECT_EQ(metadata_size, load_metadata_size);
    148   }
    149 
    150   brillo::Blob padded_hash_data_{std::begin(kDataHash), std::end(kDataHash)};
    151 };
    152 
    153 TEST_F(PayloadSignerTest, LoadPayloadV1Test) {
    154   PayloadGenerationConfig config;
    155   config.version.major = kChromeOSMajorPayloadVersion;
    156   DoWriteAndLoadPayloadTest(config);
    157 }
    158 
    159 TEST_F(PayloadSignerTest, LoadPayloadV2Test) {
    160   PayloadGenerationConfig config;
    161   config.version.major = kBrilloMajorPayloadVersion;
    162   DoWriteAndLoadPayloadTest(config);
    163 }
    164 
    165 TEST_F(PayloadSignerTest, SignSimpleTextTest) {
    166   brillo::Blob signature_blob;
    167   SignSampleData(&signature_blob,
    168                  {GetBuildArtifactsPath(kUnittestPrivateKeyPath)});
    169 
    170   // Check the signature itself
    171   Signatures signatures;
    172   EXPECT_TRUE(signatures.ParseFromArray(signature_blob.data(),
    173                                         signature_blob.size()));
    174   EXPECT_EQ(1, signatures.signatures_size());
    175   const Signatures_Signature& signature = signatures.signatures(0);
    176   EXPECT_EQ(1U, signature.version());
    177   const string& sig_data = signature.data();
    178   ASSERT_EQ(arraysize(kDataSignature), sig_data.size());
    179   for (size_t i = 0; i < arraysize(kDataSignature); i++) {
    180     EXPECT_EQ(kDataSignature[i], static_cast<uint8_t>(sig_data[i]));
    181   }
    182 }
    183 
    184 TEST_F(PayloadSignerTest, VerifyAllSignatureTest) {
    185   brillo::Blob signature_blob;
    186   SignSampleData(&signature_blob,
    187                  {GetBuildArtifactsPath(kUnittestPrivateKeyPath),
    188                   GetBuildArtifactsPath(kUnittestPrivateKey2Path)});
    189 
    190   // Either public key should pass the verification.
    191   EXPECT_TRUE(PayloadVerifier::VerifySignature(
    192       signature_blob,
    193       GetBuildArtifactsPath(kUnittestPublicKeyPath),
    194       padded_hash_data_));
    195   EXPECT_TRUE(PayloadVerifier::VerifySignature(
    196       signature_blob,
    197       GetBuildArtifactsPath(kUnittestPublicKey2Path),
    198       padded_hash_data_));
    199 }
    200 
    201 TEST_F(PayloadSignerTest, VerifySignatureTest) {
    202   brillo::Blob signature_blob;
    203   SignSampleData(&signature_blob,
    204                  {GetBuildArtifactsPath(kUnittestPrivateKeyPath)});
    205 
    206   EXPECT_TRUE(PayloadVerifier::VerifySignature(
    207       signature_blob,
    208       GetBuildArtifactsPath(kUnittestPublicKeyPath),
    209       padded_hash_data_));
    210   // Passing the invalid key should fail the verification.
    211   EXPECT_FALSE(PayloadVerifier::VerifySignature(
    212       signature_blob,
    213       GetBuildArtifactsPath(kUnittestPublicKey2Path),
    214       padded_hash_data_));
    215 }
    216 
    217 TEST_F(PayloadSignerTest, SkipMetadataSignatureTest) {
    218   string payload_path;
    219   EXPECT_TRUE(utils::MakeTempFile("payload.XXXXXX", &payload_path, nullptr));
    220   ScopedPathUnlinker payload_path_unlinker(payload_path);
    221 
    222   PayloadGenerationConfig config;
    223   config.version.major = kBrilloMajorPayloadVersion;
    224   PayloadFile payload;
    225   EXPECT_TRUE(payload.Init(config));
    226   uint64_t metadata_size;
    227   EXPECT_TRUE(
    228       payload.WritePayload(payload_path, "/dev/null", "", &metadata_size));
    229   const vector<int> sizes = {256};
    230   brillo::Blob unsigned_payload_hash, unsigned_metadata_hash;
    231   EXPECT_TRUE(PayloadSigner::HashPayloadForSigning(
    232       payload_path, sizes, &unsigned_payload_hash, &unsigned_metadata_hash));
    233   EXPECT_TRUE(
    234       payload.WritePayload(payload_path,
    235                            "/dev/null",
    236                            GetBuildArtifactsPath(kUnittestPrivateKeyPath),
    237                            &metadata_size));
    238   brillo::Blob signed_payload_hash, signed_metadata_hash;
    239   EXPECT_TRUE(PayloadSigner::HashPayloadForSigning(
    240       payload_path, sizes, &signed_payload_hash, &signed_metadata_hash));
    241   EXPECT_EQ(unsigned_payload_hash, signed_payload_hash);
    242   EXPECT_EQ(unsigned_metadata_hash, signed_metadata_hash);
    243 }
    244 
    245 TEST_F(PayloadSignerTest, VerifySignedPayloadTest) {
    246   string payload_path;
    247   EXPECT_TRUE(utils::MakeTempFile("payload.XXXXXX", &payload_path, nullptr));
    248   ScopedPathUnlinker payload_path_unlinker(payload_path);
    249 
    250   PayloadGenerationConfig config;
    251   config.version.major = kBrilloMajorPayloadVersion;
    252   PayloadFile payload;
    253   EXPECT_TRUE(payload.Init(config));
    254   uint64_t metadata_size;
    255   EXPECT_TRUE(
    256       payload.WritePayload(payload_path,
    257                            "/dev/null",
    258                            GetBuildArtifactsPath(kUnittestPrivateKeyPath),
    259                            &metadata_size));
    260   EXPECT_TRUE(PayloadSigner::VerifySignedPayload(
    261       payload_path, GetBuildArtifactsPath(kUnittestPublicKeyPath)));
    262 }
    263 
    264 }  // namespace chromeos_update_engine
    265