blob: 8c3bc28a02cba5e3bc3646353552a03d47f864ee [file] [log] [blame]
Darin Petkov85d02b72011-05-17 13:25:51 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
Andrew de los Reyes0c440052010-08-20 11:25:54 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Alex Deymo923d8fa2014-07-15 17:58:51 -07005#include "update_engine/payload_generator/payload_signer.h"
Andrew de los Reyes0c440052010-08-20 11:25:54 -07006
Darin Petkovb039d502010-12-03 09:08:04 -08007#include <base/logging.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -07008#include <base/strings/string_split.h>
9#include <base/strings/string_util.h>
Alex Vakulenko981a9fb2015-02-09 12:51:24 -080010#include <chromeos/data_encoding.h>
Darin Petkovb039d502010-12-03 09:08:04 -080011#include <openssl/pem.h>
12
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070013#include "update_engine/omaha_hash_calculator.h"
Alex Deymo14158572015-06-13 03:37:08 -070014#include "update_engine/payload_generator/payload_file.h"
Alex Deymo923d8fa2014-07-15 17:58:51 -070015#include "update_engine/payload_verifier.h"
Andrew de los Reyes0c440052010-08-20 11:25:54 -070016#include "update_engine/subprocess.h"
17#include "update_engine/update_metadata.pb.h"
18#include "update_engine/utils.h"
19
20using std::string;
21using std::vector;
22
23namespace chromeos_update_engine {
24
Darin Petkov9574f7e2011-01-13 10:48:12 -080025namespace {
Andrew de los Reyesbdfaaf02011-03-30 10:35:12 -070026
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070027// Given raw |signatures|, packs them into a protobuf and serializes it into a
Darin Petkov9574f7e2011-01-13 10:48:12 -080028// binary blob. Returns true on success, false otherwise.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080029bool ConvertSignatureToProtobufBlob(const vector<chromeos::Blob>& signatures,
30 chromeos::Blob* out_signature_blob) {
Darin Petkov9574f7e2011-01-13 10:48:12 -080031 // Pack it into a protobuf
32 Signatures out_message;
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070033 uint32_t version = kSignatureMessageOriginalVersion;
34 LOG_IF(WARNING, kSignatureMessageCurrentVersion -
35 kSignatureMessageOriginalVersion + 1 < signatures.size())
Jay Srinivasan51dcf262012-09-13 17:24:32 -070036 << "You may want to support clients in the range ["
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070037 << kSignatureMessageOriginalVersion << ", "
38 << kSignatureMessageCurrentVersion << "] inclusive, but you only "
39 << "provided " << signatures.size() << " signatures.";
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080040 for (const chromeos::Blob& signature : signatures) {
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070041 Signatures_Signature* sig_message = out_message.add_signatures();
42 sig_message->set_version(version++);
43 sig_message->set_data(signature.data(), signature.size());
44 }
Darin Petkov9574f7e2011-01-13 10:48:12 -080045
46 // Serialize protobuf
47 string serialized;
48 TEST_AND_RETURN_FALSE(out_message.AppendToString(&serialized));
49 out_signature_blob->insert(out_signature_blob->end(),
50 serialized.begin(),
51 serialized.end());
52 LOG(INFO) << "Signature blob size: " << out_signature_blob->size();
53 return true;
54}
55
56// Given an unsigned payload under |payload_path| and the |signature_blob_size|
57// generates an updated payload that includes a dummy signature op in its
Jay Srinivasan738fdf32012-12-07 17:40:54 -080058// manifest. It populates |out_metadata_size| with the size of the final
Don Garrett2ae37872013-10-25 13:33:20 -070059// manifest after adding the dummy signature operation, and
60// |out_signatures_offset| with the expected offset for the new blob. Returns
61// true on success, false otherwise.
Darin Petkovadb3cef2011-01-13 16:16:08 -080062bool AddSignatureOpToPayload(const string& payload_path,
Don Garrett2ae37872013-10-25 13:33:20 -070063 uint64_t signature_blob_size,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080064 chromeos::Blob* out_payload,
Don Garrett2ae37872013-10-25 13:33:20 -070065 uint64_t* out_metadata_size,
66 uint64_t* out_signatures_offset) {
Darin Petkov9574f7e2011-01-13 10:48:12 -080067 const int kProtobufOffset = 20;
68 const int kProtobufSizeOffset = 12;
69
Darin Petkovadb3cef2011-01-13 16:16:08 -080070 // Loads the payload.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080071 chromeos::Blob payload;
Darin Petkov9574f7e2011-01-13 10:48:12 -080072 DeltaArchiveManifest manifest;
Darin Petkovadb3cef2011-01-13 16:16:08 -080073 uint64_t metadata_size;
Alex Deymo923d8fa2014-07-15 17:58:51 -070074 TEST_AND_RETURN_FALSE(PayloadVerifier::LoadPayload(
Darin Petkovadb3cef2011-01-13 16:16:08 -080075 payload_path, &payload, &manifest, &metadata_size));
Darin Petkov9574f7e2011-01-13 10:48:12 -080076
Don Garrett2ae37872013-10-25 13:33:20 -070077 // Is there already a signature op in place?
78 if (manifest.has_signatures_size()) {
Don Garrett2ae37872013-10-25 13:33:20 -070079 // The signature op is tied to the size of the signature blob, but not it's
80 // contents. We don't allow the manifest to change if there is already an op
81 // present, because that might invalidate previously generated
82 // hashes/signatures.
83 if (manifest.signatures_size() != signature_blob_size) {
84 LOG(ERROR) << "Attempt to insert different signature sized blob. "
85 << "(current:" << manifest.signatures_size()
86 << "new:" << signature_blob_size << ")";
87 return false;
88 }
Darin Petkov9574f7e2011-01-13 10:48:12 -080089
Don Garrett2ae37872013-10-25 13:33:20 -070090 LOG(INFO) << "Matching signature sizes already present.";
91 } else {
92 // Updates the manifest to include the signature operation.
Alex Deymo14158572015-06-13 03:37:08 -070093 AddSignatureOp(payload.size() - metadata_size,
94 signature_blob_size,
95 &manifest);
Don Garrett2ae37872013-10-25 13:33:20 -070096
97 // Updates the payload to include the new manifest.
98 string serialized_manifest;
99 TEST_AND_RETURN_FALSE(manifest.AppendToString(&serialized_manifest));
100 LOG(INFO) << "Updated protobuf size: " << serialized_manifest.size();
101 payload.erase(payload.begin() + kProtobufOffset,
102 payload.begin() + metadata_size);
103 payload.insert(payload.begin() + kProtobufOffset,
104 serialized_manifest.begin(),
105 serialized_manifest.end());
106
107 // Updates the protobuf size.
108 uint64_t size_be = htobe64(serialized_manifest.size());
109 memcpy(&payload[kProtobufSizeOffset], &size_be, sizeof(size_be));
110 metadata_size = serialized_manifest.size() + kProtobufOffset;
111
112 LOG(INFO) << "Updated payload size: " << payload.size();
113 LOG(INFO) << "Updated metadata size: " << metadata_size;
114 }
115
Darin Petkov9574f7e2011-01-13 10:48:12 -0800116 out_payload->swap(payload);
Don Garrett2ae37872013-10-25 13:33:20 -0700117 *out_metadata_size = metadata_size;
118 *out_signatures_offset = metadata_size + manifest.signatures_offset();
119 LOG(INFO) << "Signature Blob Offset: " << *out_signatures_offset;
Darin Petkov9574f7e2011-01-13 10:48:12 -0800120 return true;
121}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700122} // namespace
Darin Petkov9574f7e2011-01-13 10:48:12 -0800123
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800124bool PayloadSigner::SignHash(const chromeos::Blob& hash,
Darin Petkov9574f7e2011-01-13 10:48:12 -0800125 const string& private_key_path,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800126 chromeos::Blob* out_signature) {
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700127 LOG(INFO) << "Signing hash with private key: " << private_key_path;
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700128 string sig_path;
129 TEST_AND_RETURN_FALSE(
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700130 utils::MakeTempFile("signature.XXXXXX", &sig_path, nullptr));
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700131 ScopedPathUnlinker sig_path_unlinker(sig_path);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700132
133 string hash_path;
134 TEST_AND_RETURN_FALSE(
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700135 utils::MakeTempFile("hash.XXXXXX", &hash_path, nullptr));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700136 ScopedPathUnlinker hash_path_unlinker(hash_path);
Andrew de los Reyesbdfaaf02011-03-30 10:35:12 -0700137 // We expect unpadded SHA256 hash coming in
138 TEST_AND_RETURN_FALSE(hash.size() == 32);
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800139 chromeos::Blob padded_hash(hash);
Alex Deymo923d8fa2014-07-15 17:58:51 -0700140 PayloadVerifier::PadRSA2048SHA256Hash(&padded_hash);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700141 TEST_AND_RETURN_FALSE(utils::WriteFile(hash_path.c_str(),
Andrew de los Reyesbdfaaf02011-03-30 10:35:12 -0700142 padded_hash.data(),
143 padded_hash.size()));
Darin Petkovd22cb292010-09-29 10:02:29 -0700144
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700145 // This runs on the server, so it's okay to cop out and call openssl
146 // executable rather than properly use the library
147 vector<string> cmd;
Mike Frysinger2149be42012-03-12 19:23:47 -0400148 base::SplitString("openssl rsautl -raw -sign -inkey x -in x -out x",
Chris Masoned903c3b2011-05-12 15:35:46 -0700149 ' ',
150 &cmd);
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700151 cmd[cmd.size() - 5] = private_key_path;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700152 cmd[cmd.size() - 3] = hash_path;
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700153 cmd[cmd.size() - 1] = sig_path;
Darin Petkovd22cb292010-09-29 10:02:29 -0700154
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700155 int return_code = 0;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700156 TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &return_code,
157 nullptr));
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700158 TEST_AND_RETURN_FALSE(return_code == 0);
Darin Petkovd22cb292010-09-29 10:02:29 -0700159
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800160 chromeos::Blob signature;
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700161 TEST_AND_RETURN_FALSE(utils::ReadFile(sig_path, &signature));
Darin Petkov9574f7e2011-01-13 10:48:12 -0800162 out_signature->swap(signature);
163 return true;
164}
Darin Petkovd22cb292010-09-29 10:02:29 -0700165
Darin Petkov9574f7e2011-01-13 10:48:12 -0800166bool PayloadSigner::SignPayload(const string& unsigned_payload_path,
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700167 const vector<string>& private_key_paths,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800168 chromeos::Blob* out_signature_blob) {
169 chromeos::Blob hash_data;
Darin Petkov9574f7e2011-01-13 10:48:12 -0800170 TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfFile(
171 unsigned_payload_path, -1, &hash_data) ==
172 utils::FileSize(unsigned_payload_path));
Darin Petkovd22cb292010-09-29 10:02:29 -0700173
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800174 vector<chromeos::Blob> signatures;
Alex Deymo020600d2014-11-05 21:05:55 -0800175 for (const string& path : private_key_paths) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800176 chromeos::Blob signature;
Alex Deymo020600d2014-11-05 21:05:55 -0800177 TEST_AND_RETURN_FALSE(SignHash(hash_data, path, &signature));
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700178 signatures.push_back(signature);
179 }
180 TEST_AND_RETURN_FALSE(ConvertSignatureToProtobufBlob(signatures,
Darin Petkov9574f7e2011-01-13 10:48:12 -0800181 out_signature_blob));
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700182 return true;
183}
184
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700185bool PayloadSigner::SignatureBlobLength(const vector<string>& private_key_paths,
186 uint64_t* out_length) {
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700187 DCHECK(out_length);
Darin Petkovd22cb292010-09-29 10:02:29 -0700188
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700189 string x_path;
190 TEST_AND_RETURN_FALSE(
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700191 utils::MakeTempFile("signed_data.XXXXXX", &x_path, nullptr));
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700192 ScopedPathUnlinker x_path_unlinker(x_path);
193 TEST_AND_RETURN_FALSE(utils::WriteFile(x_path.c_str(), "x", 1));
194
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800195 chromeos::Blob sig_blob;
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700196 TEST_AND_RETURN_FALSE(PayloadSigner::SignPayload(x_path,
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700197 private_key_paths,
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700198 &sig_blob));
199 *out_length = sig_blob.size();
200 return true;
201}
202
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700203bool PayloadSigner::PrepPayloadForHashing(
204 const string& payload_path,
205 const vector<int>& signature_sizes,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800206 chromeos::Blob* payload_out,
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700207 uint64_t* metadata_size_out,
208 uint64_t* signatures_offset_out) {
Darin Petkov9574f7e2011-01-13 10:48:12 -0800209 // TODO(petkov): Reduce memory usage -- the payload is manipulated in memory.
210
211 // Loads the payload and adds the signature op to it.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800212 vector<chromeos::Blob> signatures;
Alex Deymo020600d2014-11-05 21:05:55 -0800213 for (int signature_size : signature_sizes) {
214 signatures.emplace_back(signature_size, 0);
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700215 }
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800216 chromeos::Blob signature_blob;
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700217 TEST_AND_RETURN_FALSE(ConvertSignatureToProtobufBlob(signatures,
Darin Petkov9574f7e2011-01-13 10:48:12 -0800218 &signature_blob));
Darin Petkov9574f7e2011-01-13 10:48:12 -0800219 TEST_AND_RETURN_FALSE(AddSignatureOpToPayload(payload_path,
220 signature_blob.size(),
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700221 payload_out,
222 metadata_size_out,
223 signatures_offset_out));
Don Garrett2ae37872013-10-25 13:33:20 -0700224
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700225 return true;
226}
227
228bool PayloadSigner::HashPayloadForSigning(const string& payload_path,
229 const vector<int>& signature_sizes,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800230 chromeos::Blob* out_hash_data) {
231 chromeos::Blob payload;
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700232 uint64_t metadata_size;
233 uint64_t signatures_offset;
234
235 TEST_AND_RETURN_FALSE(PrepPayloadForHashing(payload_path,
236 signature_sizes,
237 &payload,
238 &metadata_size,
239 &signatures_offset));
Don Garrett2ae37872013-10-25 13:33:20 -0700240
241 // Calculates the hash on the updated payload. Note that we stop calculating
242 // before we reach the signature information.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800243 TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfBytes(payload.data(),
Don Garrett2ae37872013-10-25 13:33:20 -0700244 signatures_offset,
245 out_hash_data));
Darin Petkov9574f7e2011-01-13 10:48:12 -0800246 return true;
247}
248
Jay Srinivasanf4318702012-09-24 11:56:24 -0700249bool PayloadSigner::HashMetadataForSigning(const string& payload_path,
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700250 const vector<int>& signature_sizes,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800251 chromeos::Blob* out_metadata_hash) {
252 chromeos::Blob payload;
Jay Srinivasanf4318702012-09-24 11:56:24 -0700253 uint64_t metadata_size;
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700254 uint64_t signatures_offset;
255
256 TEST_AND_RETURN_FALSE(PrepPayloadForHashing(payload_path,
257 signature_sizes,
258 &payload,
259 &metadata_size,
260 &signatures_offset));
Jay Srinivasanf4318702012-09-24 11:56:24 -0700261
262 // Calculates the hash on the manifest.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800263 TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfBytes(payload.data(),
Jay Srinivasanf4318702012-09-24 11:56:24 -0700264 metadata_size,
265 out_metadata_hash));
266 return true;
267}
268
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700269bool PayloadSigner::AddSignatureToPayload(
270 const string& payload_path,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800271 const vector<chromeos::Blob>& signatures,
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800272 const string& signed_payload_path,
273 uint64_t *out_metadata_size) {
Darin Petkov9574f7e2011-01-13 10:48:12 -0800274 // TODO(petkov): Reduce memory usage -- the payload is manipulated in memory.
275
276 // Loads the payload and adds the signature op to it.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800277 chromeos::Blob signature_blob;
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700278 TEST_AND_RETURN_FALSE(ConvertSignatureToProtobufBlob(signatures,
Darin Petkov9574f7e2011-01-13 10:48:12 -0800279 &signature_blob));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800280 chromeos::Blob payload;
Don Garrett2ae37872013-10-25 13:33:20 -0700281 uint64_t signatures_offset;
Darin Petkov9574f7e2011-01-13 10:48:12 -0800282 TEST_AND_RETURN_FALSE(AddSignatureOpToPayload(payload_path,
283 signature_blob.size(),
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800284 &payload,
Don Garrett2ae37872013-10-25 13:33:20 -0700285 out_metadata_size,
286 &signatures_offset));
Darin Petkov9574f7e2011-01-13 10:48:12 -0800287 // Appends the signature blob to the end of the payload and writes the new
288 // payload.
Don Garrett2ae37872013-10-25 13:33:20 -0700289 LOG(INFO) << "Payload size before signatures: " << payload.size();
290 payload.resize(signatures_offset);
291 payload.insert(payload.begin() + signatures_offset,
292 signature_blob.begin(),
293 signature_blob.end());
Darin Petkov9574f7e2011-01-13 10:48:12 -0800294 LOG(INFO) << "Signed payload size: " << payload.size();
295 TEST_AND_RETURN_FALSE(utils::WriteFile(signed_payload_path.c_str(),
296 payload.data(),
297 payload.size()));
298 return true;
299}
300
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800301bool PayloadSigner::GetMetadataSignature(const void* const metadata,
Jay Srinivasanf4318702012-09-24 11:56:24 -0700302 size_t metadata_size,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700303 const string& private_key_path,
304 string* out_signature) {
305 // Calculates the hash on the updated payload. Note that the payload includes
306 // the signature op but doesn't include the signature blob at the end.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800307 chromeos::Blob metadata_hash;
Jay Srinivasanf4318702012-09-24 11:56:24 -0700308 TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfBytes(metadata,
309 metadata_size,
310 &metadata_hash));
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700311
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800312 chromeos::Blob signature;
Jay Srinivasanf4318702012-09-24 11:56:24 -0700313 TEST_AND_RETURN_FALSE(SignHash(metadata_hash,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700314 private_key_path,
315 &signature));
316
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800317 *out_signature = chromeos::data_encoding::Base64Encode(signature);
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700318 return true;
319}
320
321
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700322} // namespace chromeos_update_engine