blob: 9a9008fed175cff34eb6ca20ae93ce5e002e8805 [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.
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -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.";
39 for (vector<vector<char> >::const_iterator it = signatures.begin(),
40 e = signatures.end(); it != e; ++it) {
41 const vector<char>& signature = *it;
42 Signatures_Signature* sig_message = out_message.add_signatures();
43 sig_message->set_version(version++);
44 sig_message->set_data(signature.data(), signature.size());
45 }
Darin Petkov9574f7e2011-01-13 10:48:12 -080046
47 // Serialize protobuf
48 string serialized;
49 TEST_AND_RETURN_FALSE(out_message.AppendToString(&serialized));
50 out_signature_blob->insert(out_signature_blob->end(),
51 serialized.begin(),
52 serialized.end());
53 LOG(INFO) << "Signature blob size: " << out_signature_blob->size();
54 return true;
55}
56
57// Given an unsigned payload under |payload_path| and the |signature_blob_size|
58// generates an updated payload that includes a dummy signature op in its
Jay Srinivasan738fdf32012-12-07 17:40:54 -080059// manifest. It populates |out_metadata_size| with the size of the final
Don Garrett2ae37872013-10-25 13:33:20 -070060// manifest after adding the dummy signature operation, and
61// |out_signatures_offset| with the expected offset for the new blob. Returns
62// true on success, false otherwise.
Darin Petkovadb3cef2011-01-13 16:16:08 -080063bool AddSignatureOpToPayload(const string& payload_path,
Don Garrett2ae37872013-10-25 13:33:20 -070064 uint64_t signature_blob_size,
Jay Srinivasan738fdf32012-12-07 17:40:54 -080065 vector<char>* out_payload,
Don Garrett2ae37872013-10-25 13:33:20 -070066 uint64_t* out_metadata_size,
67 uint64_t* out_signatures_offset) {
Darin Petkov9574f7e2011-01-13 10:48:12 -080068 const int kProtobufOffset = 20;
69 const int kProtobufSizeOffset = 12;
70
Darin Petkovadb3cef2011-01-13 16:16:08 -080071 // Loads the payload.
Darin Petkov9574f7e2011-01-13 10:48:12 -080072 vector<char> payload;
Darin Petkov9574f7e2011-01-13 10:48:12 -080073 DeltaArchiveManifest manifest;
Darin Petkovadb3cef2011-01-13 16:16:08 -080074 uint64_t metadata_size;
Alex Deymo923d8fa2014-07-15 17:58:51 -070075 TEST_AND_RETURN_FALSE(PayloadVerifier::LoadPayload(
Darin Petkovadb3cef2011-01-13 16:16:08 -080076 payload_path, &payload, &manifest, &metadata_size));
Darin Petkov9574f7e2011-01-13 10:48:12 -080077
Don Garrett2ae37872013-10-25 13:33:20 -070078 // Is there already a signature op in place?
79 if (manifest.has_signatures_size()) {
Don Garrett2ae37872013-10-25 13:33:20 -070080 // The signature op is tied to the size of the signature blob, but not it's
81 // contents. We don't allow the manifest to change if there is already an op
82 // present, because that might invalidate previously generated
83 // hashes/signatures.
84 if (manifest.signatures_size() != signature_blob_size) {
85 LOG(ERROR) << "Attempt to insert different signature sized blob. "
86 << "(current:" << manifest.signatures_size()
87 << "new:" << signature_blob_size << ")";
88 return false;
89 }
Darin Petkov9574f7e2011-01-13 10:48:12 -080090
Don Garrett2ae37872013-10-25 13:33:20 -070091 LOG(INFO) << "Matching signature sizes already present.";
92 } else {
93 // Updates the manifest to include the signature operation.
94 DeltaDiffGenerator::AddSignatureOp(payload.size() - metadata_size,
95 signature_blob_size,
96 &manifest);
97
98 // Updates the payload to include the new manifest.
99 string serialized_manifest;
100 TEST_AND_RETURN_FALSE(manifest.AppendToString(&serialized_manifest));
101 LOG(INFO) << "Updated protobuf size: " << serialized_manifest.size();
102 payload.erase(payload.begin() + kProtobufOffset,
103 payload.begin() + metadata_size);
104 payload.insert(payload.begin() + kProtobufOffset,
105 serialized_manifest.begin(),
106 serialized_manifest.end());
107
108 // Updates the protobuf size.
109 uint64_t size_be = htobe64(serialized_manifest.size());
110 memcpy(&payload[kProtobufSizeOffset], &size_be, sizeof(size_be));
111 metadata_size = serialized_manifest.size() + kProtobufOffset;
112
113 LOG(INFO) << "Updated payload size: " << payload.size();
114 LOG(INFO) << "Updated metadata size: " << metadata_size;
115 }
116
Darin Petkov9574f7e2011-01-13 10:48:12 -0800117 out_payload->swap(payload);
Don Garrett2ae37872013-10-25 13:33:20 -0700118 *out_metadata_size = metadata_size;
119 *out_signatures_offset = metadata_size + manifest.signatures_offset();
120 LOG(INFO) << "Signature Blob Offset: " << *out_signatures_offset;
Darin Petkov9574f7e2011-01-13 10:48:12 -0800121 return true;
122}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700123} // namespace
Darin Petkov9574f7e2011-01-13 10:48:12 -0800124
125bool PayloadSigner::SignHash(const vector<char>& hash,
126 const string& private_key_path,
127 vector<char>* out_signature) {
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700128 LOG(INFO) << "Signing hash with private key: " << private_key_path;
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700129 string sig_path;
130 TEST_AND_RETURN_FALSE(
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700131 utils::MakeTempFile("signature.XXXXXX", &sig_path, nullptr));
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700132 ScopedPathUnlinker sig_path_unlinker(sig_path);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700133
134 string hash_path;
135 TEST_AND_RETURN_FALSE(
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700136 utils::MakeTempFile("hash.XXXXXX", &hash_path, nullptr));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700137 ScopedPathUnlinker hash_path_unlinker(hash_path);
Andrew de los Reyesbdfaaf02011-03-30 10:35:12 -0700138 // We expect unpadded SHA256 hash coming in
139 TEST_AND_RETURN_FALSE(hash.size() == 32);
140 vector<char> padded_hash(hash);
Alex Deymo923d8fa2014-07-15 17:58:51 -0700141 PayloadVerifier::PadRSA2048SHA256Hash(&padded_hash);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700142 TEST_AND_RETURN_FALSE(utils::WriteFile(hash_path.c_str(),
Andrew de los Reyesbdfaaf02011-03-30 10:35:12 -0700143 padded_hash.data(),
144 padded_hash.size()));
Darin Petkovd22cb292010-09-29 10:02:29 -0700145
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700146 // This runs on the server, so it's okay to cop out and call openssl
147 // executable rather than properly use the library
148 vector<string> cmd;
Mike Frysinger2149be42012-03-12 19:23:47 -0400149 base::SplitString("openssl rsautl -raw -sign -inkey x -in x -out x",
Chris Masoned903c3b2011-05-12 15:35:46 -0700150 ' ',
151 &cmd);
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700152 cmd[cmd.size() - 5] = private_key_path;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700153 cmd[cmd.size() - 3] = hash_path;
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700154 cmd[cmd.size() - 1] = sig_path;
Darin Petkovd22cb292010-09-29 10:02:29 -0700155
Alex Deymo719bfff2014-07-11 12:12:32 -0700156 // When running unittests, we need to use the openssl version from the
157 // SYSROOT instead of the one on the $PATH (host).
158 cmd[0] = utils::GetPathOnBoard("openssl");
159
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700160 int return_code = 0;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700161 TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &return_code,
162 nullptr));
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700163 TEST_AND_RETURN_FALSE(return_code == 0);
Darin Petkovd22cb292010-09-29 10:02:29 -0700164
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700165 vector<char> signature;
166 TEST_AND_RETURN_FALSE(utils::ReadFile(sig_path, &signature));
Darin Petkov9574f7e2011-01-13 10:48:12 -0800167 out_signature->swap(signature);
168 return true;
169}
Darin Petkovd22cb292010-09-29 10:02:29 -0700170
Darin Petkov9574f7e2011-01-13 10:48:12 -0800171bool PayloadSigner::SignPayload(const string& unsigned_payload_path,
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700172 const vector<string>& private_key_paths,
Darin Petkov9574f7e2011-01-13 10:48:12 -0800173 vector<char>* out_signature_blob) {
174 vector<char> hash_data;
175 TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfFile(
176 unsigned_payload_path, -1, &hash_data) ==
177 utils::FileSize(unsigned_payload_path));
Darin Petkovd22cb292010-09-29 10:02:29 -0700178
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700179 vector<vector<char> > signatures;
180 for (vector<string>::const_iterator it = private_key_paths.begin(),
181 e = private_key_paths.end(); it != e; ++it) {
182 vector<char> signature;
183 TEST_AND_RETURN_FALSE(SignHash(hash_data, *it, &signature));
184 signatures.push_back(signature);
185 }
186 TEST_AND_RETURN_FALSE(ConvertSignatureToProtobufBlob(signatures,
Darin Petkov9574f7e2011-01-13 10:48:12 -0800187 out_signature_blob));
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700188 return true;
189}
190
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700191bool PayloadSigner::SignatureBlobLength(const vector<string>& private_key_paths,
192 uint64_t* out_length) {
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700193 DCHECK(out_length);
Darin Petkovd22cb292010-09-29 10:02:29 -0700194
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700195 string x_path;
196 TEST_AND_RETURN_FALSE(
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700197 utils::MakeTempFile("signed_data.XXXXXX", &x_path, nullptr));
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700198 ScopedPathUnlinker x_path_unlinker(x_path);
199 TEST_AND_RETURN_FALSE(utils::WriteFile(x_path.c_str(), "x", 1));
200
201 vector<char> sig_blob;
202 TEST_AND_RETURN_FALSE(PayloadSigner::SignPayload(x_path,
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700203 private_key_paths,
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700204 &sig_blob));
205 *out_length = sig_blob.size();
206 return true;
207}
208
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700209bool PayloadSigner::PrepPayloadForHashing(
210 const string& payload_path,
211 const vector<int>& signature_sizes,
212 vector<char>* payload_out,
213 uint64_t* metadata_size_out,
214 uint64_t* signatures_offset_out) {
Darin Petkov9574f7e2011-01-13 10:48:12 -0800215 // TODO(petkov): Reduce memory usage -- the payload is manipulated in memory.
216
217 // Loads the payload and adds the signature op to it.
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700218 vector<vector<char> > signatures;
219 for (vector<int>::const_iterator it = signature_sizes.begin(),
220 e = signature_sizes.end(); it != e; ++it) {
221 vector<char> signature(*it, 0);
222 signatures.push_back(signature);
223 }
Darin Petkov9574f7e2011-01-13 10:48:12 -0800224 vector<char> signature_blob;
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700225 TEST_AND_RETURN_FALSE(ConvertSignatureToProtobufBlob(signatures,
Darin Petkov9574f7e2011-01-13 10:48:12 -0800226 &signature_blob));
Darin Petkov9574f7e2011-01-13 10:48:12 -0800227 TEST_AND_RETURN_FALSE(AddSignatureOpToPayload(payload_path,
228 signature_blob.size(),
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700229 payload_out,
230 metadata_size_out,
231 signatures_offset_out));
Don Garrett2ae37872013-10-25 13:33:20 -0700232
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700233 return true;
234}
235
236bool PayloadSigner::HashPayloadForSigning(const string& payload_path,
237 const vector<int>& signature_sizes,
238 vector<char>* out_hash_data) {
239 vector<char> payload;
240 uint64_t metadata_size;
241 uint64_t signatures_offset;
242
243 TEST_AND_RETURN_FALSE(PrepPayloadForHashing(payload_path,
244 signature_sizes,
245 &payload,
246 &metadata_size,
247 &signatures_offset));
Don Garrett2ae37872013-10-25 13:33:20 -0700248
249 // Calculates the hash on the updated payload. Note that we stop calculating
250 // before we reach the signature information.
251 TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfBytes(&payload[0],
252 signatures_offset,
253 out_hash_data));
Darin Petkov9574f7e2011-01-13 10:48:12 -0800254 return true;
255}
256
Jay Srinivasanf4318702012-09-24 11:56:24 -0700257bool PayloadSigner::HashMetadataForSigning(const string& payload_path,
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700258 const vector<int>& signature_sizes,
Jay Srinivasanf4318702012-09-24 11:56:24 -0700259 vector<char>* out_metadata_hash) {
Jay Srinivasanf4318702012-09-24 11:56:24 -0700260 vector<char> payload;
Jay Srinivasanf4318702012-09-24 11:56:24 -0700261 uint64_t metadata_size;
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700262 uint64_t signatures_offset;
263
264 TEST_AND_RETURN_FALSE(PrepPayloadForHashing(payload_path,
265 signature_sizes,
266 &payload,
267 &metadata_size,
268 &signatures_offset));
Jay Srinivasanf4318702012-09-24 11:56:24 -0700269
270 // Calculates the hash on the manifest.
271 TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfBytes(&payload[0],
272 metadata_size,
273 out_metadata_hash));
274 return true;
275}
276
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700277bool PayloadSigner::AddSignatureToPayload(
278 const string& payload_path,
279 const vector<vector<char> >& signatures,
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800280 const string& signed_payload_path,
281 uint64_t *out_metadata_size) {
Darin Petkov9574f7e2011-01-13 10:48:12 -0800282 // TODO(petkov): Reduce memory usage -- the payload is manipulated in memory.
283
284 // Loads the payload and adds the signature op to it.
285 vector<char> signature_blob;
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700286 TEST_AND_RETURN_FALSE(ConvertSignatureToProtobufBlob(signatures,
Darin Petkov9574f7e2011-01-13 10:48:12 -0800287 &signature_blob));
288 vector<char> payload;
Don Garrett2ae37872013-10-25 13:33:20 -0700289 uint64_t signatures_offset;
Darin Petkov9574f7e2011-01-13 10:48:12 -0800290 TEST_AND_RETURN_FALSE(AddSignatureOpToPayload(payload_path,
291 signature_blob.size(),
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800292 &payload,
Don Garrett2ae37872013-10-25 13:33:20 -0700293 out_metadata_size,
294 &signatures_offset));
Darin Petkov9574f7e2011-01-13 10:48:12 -0800295 // Appends the signature blob to the end of the payload and writes the new
296 // payload.
Don Garrett2ae37872013-10-25 13:33:20 -0700297 LOG(INFO) << "Payload size before signatures: " << payload.size();
298 payload.resize(signatures_offset);
299 payload.insert(payload.begin() + signatures_offset,
300 signature_blob.begin(),
301 signature_blob.end());
Darin Petkov9574f7e2011-01-13 10:48:12 -0800302 LOG(INFO) << "Signed payload size: " << payload.size();
303 TEST_AND_RETURN_FALSE(utils::WriteFile(signed_payload_path.c_str(),
304 payload.data(),
305 payload.size()));
306 return true;
307}
308
Jay Srinivasanf4318702012-09-24 11:56:24 -0700309bool PayloadSigner::GetMetadataSignature(const char* const metadata,
310 size_t metadata_size,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700311 const string& private_key_path,
312 string* out_signature) {
313 // Calculates the hash on the updated payload. Note that the payload includes
314 // the signature op but doesn't include the signature blob at the end.
Jay Srinivasanf4318702012-09-24 11:56:24 -0700315 vector<char> metadata_hash;
316 TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfBytes(metadata,
317 metadata_size,
318 &metadata_hash));
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700319
320 vector<char> signature;
Jay Srinivasanf4318702012-09-24 11:56:24 -0700321 TEST_AND_RETURN_FALSE(SignHash(metadata_hash,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700322 private_key_path,
323 &signature));
324
325 TEST_AND_RETURN_FALSE(OmahaHashCalculator::Base64Encode(&signature[0],
326 signature.size(),
327 out_signature));
328 return true;
329}
330
331
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700332} // namespace chromeos_update_engine