blob: a33c24f9d470940a6f926119ad3b1121c4e8d9bc [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>
Darin Petkovb039d502010-12-03 09:08:04 -080010#include <openssl/pem.h>
11
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070012#include "update_engine/omaha_hash_calculator.h"
Alex Deymo161c4a12014-05-16 15:56:21 -070013#include "update_engine/payload_generator/delta_diff_generator.h"
Alex Deymo923d8fa2014-07-15 17:58:51 -070014#include "update_engine/payload_verifier.h"
Andrew de los Reyes0c440052010-08-20 11:25:54 -070015#include "update_engine/subprocess.h"
16#include "update_engine/update_metadata.pb.h"
17#include "update_engine/utils.h"
18
19using std::string;
20using std::vector;
21
22namespace chromeos_update_engine {
23
Darin Petkov9574f7e2011-01-13 10:48:12 -080024namespace {
Andrew de los Reyesbdfaaf02011-03-30 10:35:12 -070025
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070026// Given raw |signatures|, packs them into a protobuf and serializes it into a
Darin Petkov9574f7e2011-01-13 10:48:12 -080027// binary blob. Returns true on success, false otherwise.
Ben Chanf9cb98c2014-09-21 18:31:30 -070028bool ConvertSignatureToProtobufBlob(const vector<vector<char>>& signatures,
Darin Petkov9574f7e2011-01-13 10:48:12 -080029 vector<char>* out_signature_blob) {
30 // Pack it into a protobuf
31 Signatures out_message;
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070032 uint32_t version = kSignatureMessageOriginalVersion;
33 LOG_IF(WARNING, kSignatureMessageCurrentVersion -
34 kSignatureMessageOriginalVersion + 1 < signatures.size())
Jay Srinivasan51dcf262012-09-13 17:24:32 -070035 << "You may want to support clients in the range ["
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070036 << kSignatureMessageOriginalVersion << ", "
37 << kSignatureMessageCurrentVersion << "] inclusive, but you only "
38 << "provided " << signatures.size() << " signatures.";
Alex Deymo020600d2014-11-05 21:05:55 -080039 for (const vector<char>& signature : signatures) {
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070040 Signatures_Signature* sig_message = out_message.add_signatures();
41 sig_message->set_version(version++);
42 sig_message->set_data(signature.data(), signature.size());
43 }
Darin Petkov9574f7e2011-01-13 10:48:12 -080044
45 // Serialize protobuf
46 string serialized;
47 TEST_AND_RETURN_FALSE(out_message.AppendToString(&serialized));
48 out_signature_blob->insert(out_signature_blob->end(),
49 serialized.begin(),
50 serialized.end());
51 LOG(INFO) << "Signature blob size: " << out_signature_blob->size();
52 return true;
53}
54
55// Given an unsigned payload under |payload_path| and the |signature_blob_size|
56// generates an updated payload that includes a dummy signature op in its
Jay Srinivasan738fdf32012-12-07 17:40:54 -080057// manifest. It populates |out_metadata_size| with the size of the final
Don Garrett2ae37872013-10-25 13:33:20 -070058// manifest after adding the dummy signature operation, and
59// |out_signatures_offset| with the expected offset for the new blob. Returns
60// true on success, false otherwise.
Darin Petkovadb3cef2011-01-13 16:16:08 -080061bool AddSignatureOpToPayload(const string& payload_path,
Don Garrett2ae37872013-10-25 13:33:20 -070062 uint64_t signature_blob_size,
Jay Srinivasan738fdf32012-12-07 17:40:54 -080063 vector<char>* out_payload,
Don Garrett2ae37872013-10-25 13:33:20 -070064 uint64_t* out_metadata_size,
65 uint64_t* out_signatures_offset) {
Darin Petkov9574f7e2011-01-13 10:48:12 -080066 const int kProtobufOffset = 20;
67 const int kProtobufSizeOffset = 12;
68
Darin Petkovadb3cef2011-01-13 16:16:08 -080069 // Loads the payload.
Darin Petkov9574f7e2011-01-13 10:48:12 -080070 vector<char> payload;
Darin Petkov9574f7e2011-01-13 10:48:12 -080071 DeltaArchiveManifest manifest;
Darin Petkovadb3cef2011-01-13 16:16:08 -080072 uint64_t metadata_size;
Alex Deymo923d8fa2014-07-15 17:58:51 -070073 TEST_AND_RETURN_FALSE(PayloadVerifier::LoadPayload(
Darin Petkovadb3cef2011-01-13 16:16:08 -080074 payload_path, &payload, &manifest, &metadata_size));
Darin Petkov9574f7e2011-01-13 10:48:12 -080075
Don Garrett2ae37872013-10-25 13:33:20 -070076 // Is there already a signature op in place?
77 if (manifest.has_signatures_size()) {
Don Garrett2ae37872013-10-25 13:33:20 -070078 // The signature op is tied to the size of the signature blob, but not it's
79 // contents. We don't allow the manifest to change if there is already an op
80 // present, because that might invalidate previously generated
81 // hashes/signatures.
82 if (manifest.signatures_size() != signature_blob_size) {
83 LOG(ERROR) << "Attempt to insert different signature sized blob. "
84 << "(current:" << manifest.signatures_size()
85 << "new:" << signature_blob_size << ")";
86 return false;
87 }
Darin Petkov9574f7e2011-01-13 10:48:12 -080088
Don Garrett2ae37872013-10-25 13:33:20 -070089 LOG(INFO) << "Matching signature sizes already present.";
90 } else {
91 // Updates the manifest to include the signature operation.
92 DeltaDiffGenerator::AddSignatureOp(payload.size() - metadata_size,
93 signature_blob_size,
94 &manifest);
95
96 // Updates the payload to include the new manifest.
97 string serialized_manifest;
98 TEST_AND_RETURN_FALSE(manifest.AppendToString(&serialized_manifest));
99 LOG(INFO) << "Updated protobuf size: " << serialized_manifest.size();
100 payload.erase(payload.begin() + kProtobufOffset,
101 payload.begin() + metadata_size);
102 payload.insert(payload.begin() + kProtobufOffset,
103 serialized_manifest.begin(),
104 serialized_manifest.end());
105
106 // Updates the protobuf size.
107 uint64_t size_be = htobe64(serialized_manifest.size());
108 memcpy(&payload[kProtobufSizeOffset], &size_be, sizeof(size_be));
109 metadata_size = serialized_manifest.size() + kProtobufOffset;
110
111 LOG(INFO) << "Updated payload size: " << payload.size();
112 LOG(INFO) << "Updated metadata size: " << metadata_size;
113 }
114
Darin Petkov9574f7e2011-01-13 10:48:12 -0800115 out_payload->swap(payload);
Don Garrett2ae37872013-10-25 13:33:20 -0700116 *out_metadata_size = metadata_size;
117 *out_signatures_offset = metadata_size + manifest.signatures_offset();
118 LOG(INFO) << "Signature Blob Offset: " << *out_signatures_offset;
Darin Petkov9574f7e2011-01-13 10:48:12 -0800119 return true;
120}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700121} // namespace
Darin Petkov9574f7e2011-01-13 10:48:12 -0800122
123bool PayloadSigner::SignHash(const vector<char>& hash,
124 const string& private_key_path,
125 vector<char>* out_signature) {
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700126 LOG(INFO) << "Signing hash with private key: " << private_key_path;
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700127 string sig_path;
128 TEST_AND_RETURN_FALSE(
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700129 utils::MakeTempFile("signature.XXXXXX", &sig_path, nullptr));
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700130 ScopedPathUnlinker sig_path_unlinker(sig_path);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700131
132 string hash_path;
133 TEST_AND_RETURN_FALSE(
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700134 utils::MakeTempFile("hash.XXXXXX", &hash_path, nullptr));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700135 ScopedPathUnlinker hash_path_unlinker(hash_path);
Andrew de los Reyesbdfaaf02011-03-30 10:35:12 -0700136 // We expect unpadded SHA256 hash coming in
137 TEST_AND_RETURN_FALSE(hash.size() == 32);
138 vector<char> padded_hash(hash);
Alex Deymo923d8fa2014-07-15 17:58:51 -0700139 PayloadVerifier::PadRSA2048SHA256Hash(&padded_hash);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700140 TEST_AND_RETURN_FALSE(utils::WriteFile(hash_path.c_str(),
Andrew de los Reyesbdfaaf02011-03-30 10:35:12 -0700141 padded_hash.data(),
142 padded_hash.size()));
Darin Petkovd22cb292010-09-29 10:02:29 -0700143
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700144 // This runs on the server, so it's okay to cop out and call openssl
145 // executable rather than properly use the library
146 vector<string> cmd;
Mike Frysinger2149be42012-03-12 19:23:47 -0400147 base::SplitString("openssl rsautl -raw -sign -inkey x -in x -out x",
Chris Masoned903c3b2011-05-12 15:35:46 -0700148 ' ',
149 &cmd);
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700150 cmd[cmd.size() - 5] = private_key_path;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700151 cmd[cmd.size() - 3] = hash_path;
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700152 cmd[cmd.size() - 1] = sig_path;
Darin Petkovd22cb292010-09-29 10:02:29 -0700153
Alex Deymo719bfff2014-07-11 12:12:32 -0700154 // When running unittests, we need to use the openssl version from the
155 // SYSROOT instead of the one on the $PATH (host).
156 cmd[0] = utils::GetPathOnBoard("openssl");
157
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700158 int return_code = 0;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700159 TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &return_code,
160 nullptr));
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700161 TEST_AND_RETURN_FALSE(return_code == 0);
Darin Petkovd22cb292010-09-29 10:02:29 -0700162
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700163 vector<char> signature;
164 TEST_AND_RETURN_FALSE(utils::ReadFile(sig_path, &signature));
Darin Petkov9574f7e2011-01-13 10:48:12 -0800165 out_signature->swap(signature);
166 return true;
167}
Darin Petkovd22cb292010-09-29 10:02:29 -0700168
Darin Petkov9574f7e2011-01-13 10:48:12 -0800169bool PayloadSigner::SignPayload(const string& unsigned_payload_path,
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700170 const vector<string>& private_key_paths,
Darin Petkov9574f7e2011-01-13 10:48:12 -0800171 vector<char>* out_signature_blob) {
172 vector<char> hash_data;
173 TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfFile(
174 unsigned_payload_path, -1, &hash_data) ==
175 utils::FileSize(unsigned_payload_path));
Darin Petkovd22cb292010-09-29 10:02:29 -0700176
Ben Chanf9cb98c2014-09-21 18:31:30 -0700177 vector<vector<char>> signatures;
Alex Deymo020600d2014-11-05 21:05:55 -0800178 for (const string& path : private_key_paths) {
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700179 vector<char> signature;
Alex Deymo020600d2014-11-05 21:05:55 -0800180 TEST_AND_RETURN_FALSE(SignHash(hash_data, path, &signature));
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700181 signatures.push_back(signature);
182 }
183 TEST_AND_RETURN_FALSE(ConvertSignatureToProtobufBlob(signatures,
Darin Petkov9574f7e2011-01-13 10:48:12 -0800184 out_signature_blob));
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700185 return true;
186}
187
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700188bool PayloadSigner::SignatureBlobLength(const vector<string>& private_key_paths,
189 uint64_t* out_length) {
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700190 DCHECK(out_length);
Darin Petkovd22cb292010-09-29 10:02:29 -0700191
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700192 string x_path;
193 TEST_AND_RETURN_FALSE(
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700194 utils::MakeTempFile("signed_data.XXXXXX", &x_path, nullptr));
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700195 ScopedPathUnlinker x_path_unlinker(x_path);
196 TEST_AND_RETURN_FALSE(utils::WriteFile(x_path.c_str(), "x", 1));
197
198 vector<char> sig_blob;
199 TEST_AND_RETURN_FALSE(PayloadSigner::SignPayload(x_path,
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700200 private_key_paths,
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700201 &sig_blob));
202 *out_length = sig_blob.size();
203 return true;
204}
205
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700206bool PayloadSigner::PrepPayloadForHashing(
207 const string& payload_path,
208 const vector<int>& signature_sizes,
209 vector<char>* payload_out,
210 uint64_t* metadata_size_out,
211 uint64_t* signatures_offset_out) {
Darin Petkov9574f7e2011-01-13 10:48:12 -0800212 // TODO(petkov): Reduce memory usage -- the payload is manipulated in memory.
213
214 // Loads the payload and adds the signature op to it.
Ben Chanf9cb98c2014-09-21 18:31:30 -0700215 vector<vector<char>> signatures;
Alex Deymo020600d2014-11-05 21:05:55 -0800216 for (int signature_size : signature_sizes) {
217 signatures.emplace_back(signature_size, 0);
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700218 }
Darin Petkov9574f7e2011-01-13 10:48:12 -0800219 vector<char> signature_blob;
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700220 TEST_AND_RETURN_FALSE(ConvertSignatureToProtobufBlob(signatures,
Darin Petkov9574f7e2011-01-13 10:48:12 -0800221 &signature_blob));
Darin Petkov9574f7e2011-01-13 10:48:12 -0800222 TEST_AND_RETURN_FALSE(AddSignatureOpToPayload(payload_path,
223 signature_blob.size(),
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700224 payload_out,
225 metadata_size_out,
226 signatures_offset_out));
Don Garrett2ae37872013-10-25 13:33:20 -0700227
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700228 return true;
229}
230
231bool PayloadSigner::HashPayloadForSigning(const string& payload_path,
232 const vector<int>& signature_sizes,
233 vector<char>* out_hash_data) {
234 vector<char> payload;
235 uint64_t metadata_size;
236 uint64_t signatures_offset;
237
238 TEST_AND_RETURN_FALSE(PrepPayloadForHashing(payload_path,
239 signature_sizes,
240 &payload,
241 &metadata_size,
242 &signatures_offset));
Don Garrett2ae37872013-10-25 13:33:20 -0700243
244 // Calculates the hash on the updated payload. Note that we stop calculating
245 // before we reach the signature information.
246 TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfBytes(&payload[0],
247 signatures_offset,
248 out_hash_data));
Darin Petkov9574f7e2011-01-13 10:48:12 -0800249 return true;
250}
251
Jay Srinivasanf4318702012-09-24 11:56:24 -0700252bool PayloadSigner::HashMetadataForSigning(const string& payload_path,
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700253 const vector<int>& signature_sizes,
Jay Srinivasanf4318702012-09-24 11:56:24 -0700254 vector<char>* out_metadata_hash) {
Jay Srinivasanf4318702012-09-24 11:56:24 -0700255 vector<char> payload;
Jay Srinivasanf4318702012-09-24 11:56:24 -0700256 uint64_t metadata_size;
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700257 uint64_t signatures_offset;
258
259 TEST_AND_RETURN_FALSE(PrepPayloadForHashing(payload_path,
260 signature_sizes,
261 &payload,
262 &metadata_size,
263 &signatures_offset));
Jay Srinivasanf4318702012-09-24 11:56:24 -0700264
265 // Calculates the hash on the manifest.
266 TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfBytes(&payload[0],
267 metadata_size,
268 out_metadata_hash));
269 return true;
270}
271
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700272bool PayloadSigner::AddSignatureToPayload(
273 const string& payload_path,
Ben Chanf9cb98c2014-09-21 18:31:30 -0700274 const vector<vector<char>>& signatures,
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800275 const string& signed_payload_path,
276 uint64_t *out_metadata_size) {
Darin Petkov9574f7e2011-01-13 10:48:12 -0800277 // TODO(petkov): Reduce memory usage -- the payload is manipulated in memory.
278
279 // Loads the payload and adds the signature op to it.
280 vector<char> signature_blob;
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700281 TEST_AND_RETURN_FALSE(ConvertSignatureToProtobufBlob(signatures,
Darin Petkov9574f7e2011-01-13 10:48:12 -0800282 &signature_blob));
283 vector<char> payload;
Don Garrett2ae37872013-10-25 13:33:20 -0700284 uint64_t signatures_offset;
Darin Petkov9574f7e2011-01-13 10:48:12 -0800285 TEST_AND_RETURN_FALSE(AddSignatureOpToPayload(payload_path,
286 signature_blob.size(),
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800287 &payload,
Don Garrett2ae37872013-10-25 13:33:20 -0700288 out_metadata_size,
289 &signatures_offset));
Darin Petkov9574f7e2011-01-13 10:48:12 -0800290 // Appends the signature blob to the end of the payload and writes the new
291 // payload.
Don Garrett2ae37872013-10-25 13:33:20 -0700292 LOG(INFO) << "Payload size before signatures: " << payload.size();
293 payload.resize(signatures_offset);
294 payload.insert(payload.begin() + signatures_offset,
295 signature_blob.begin(),
296 signature_blob.end());
Darin Petkov9574f7e2011-01-13 10:48:12 -0800297 LOG(INFO) << "Signed payload size: " << payload.size();
298 TEST_AND_RETURN_FALSE(utils::WriteFile(signed_payload_path.c_str(),
299 payload.data(),
300 payload.size()));
301 return true;
302}
303
Jay Srinivasanf4318702012-09-24 11:56:24 -0700304bool PayloadSigner::GetMetadataSignature(const char* const metadata,
305 size_t metadata_size,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700306 const string& private_key_path,
307 string* out_signature) {
308 // Calculates the hash on the updated payload. Note that the payload includes
309 // the signature op but doesn't include the signature blob at the end.
Jay Srinivasanf4318702012-09-24 11:56:24 -0700310 vector<char> metadata_hash;
311 TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfBytes(metadata,
312 metadata_size,
313 &metadata_hash));
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700314
315 vector<char> signature;
Jay Srinivasanf4318702012-09-24 11:56:24 -0700316 TEST_AND_RETURN_FALSE(SignHash(metadata_hash,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700317 private_key_path,
318 &signature));
319
320 TEST_AND_RETURN_FALSE(OmahaHashCalculator::Base64Encode(&signature[0],
321 signature.size(),
322 out_signature));
323 return true;
324}
325
326
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700327} // namespace chromeos_update_engine