blob: 4bbb15593265b76568f0e5729e315b5bb969a931 [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(
Gilad Arnolda6742b32014-01-11 00:18:34 -0800131 utils::MakeTempFile("signature.XXXXXX", &sig_path, NULL));
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(
Gilad Arnolda6742b32014-01-11 00:18:34 -0800136 utils::MakeTempFile("hash.XXXXXX", &hash_path, NULL));
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;
Darin Petkov85d02b72011-05-17 13:25:51 -0700161 TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &return_code, NULL));
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700162 TEST_AND_RETURN_FALSE(return_code == 0);
Darin Petkovd22cb292010-09-29 10:02:29 -0700163
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700164 vector<char> signature;
165 TEST_AND_RETURN_FALSE(utils::ReadFile(sig_path, &signature));
Darin Petkov9574f7e2011-01-13 10:48:12 -0800166 out_signature->swap(signature);
167 return true;
168}
Darin Petkovd22cb292010-09-29 10:02:29 -0700169
Darin Petkov9574f7e2011-01-13 10:48:12 -0800170bool PayloadSigner::SignPayload(const string& unsigned_payload_path,
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700171 const vector<string>& private_key_paths,
Darin Petkov9574f7e2011-01-13 10:48:12 -0800172 vector<char>* out_signature_blob) {
173 vector<char> hash_data;
174 TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfFile(
175 unsigned_payload_path, -1, &hash_data) ==
176 utils::FileSize(unsigned_payload_path));
Darin Petkovd22cb292010-09-29 10:02:29 -0700177
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700178 vector<vector<char> > signatures;
179 for (vector<string>::const_iterator it = private_key_paths.begin(),
180 e = private_key_paths.end(); it != e; ++it) {
181 vector<char> signature;
182 TEST_AND_RETURN_FALSE(SignHash(hash_data, *it, &signature));
183 signatures.push_back(signature);
184 }
185 TEST_AND_RETURN_FALSE(ConvertSignatureToProtobufBlob(signatures,
Darin Petkov9574f7e2011-01-13 10:48:12 -0800186 out_signature_blob));
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700187 return true;
188}
189
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700190bool PayloadSigner::SignatureBlobLength(const vector<string>& private_key_paths,
191 uint64_t* out_length) {
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700192 DCHECK(out_length);
Darin Petkovd22cb292010-09-29 10:02:29 -0700193
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700194 string x_path;
195 TEST_AND_RETURN_FALSE(
Gilad Arnolda6742b32014-01-11 00:18:34 -0800196 utils::MakeTempFile("signed_data.XXXXXX", &x_path, NULL));
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700197 ScopedPathUnlinker x_path_unlinker(x_path);
198 TEST_AND_RETURN_FALSE(utils::WriteFile(x_path.c_str(), "x", 1));
199
200 vector<char> sig_blob;
201 TEST_AND_RETURN_FALSE(PayloadSigner::SignPayload(x_path,
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700202 private_key_paths,
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700203 &sig_blob));
204 *out_length = sig_blob.size();
205 return true;
206}
207
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700208bool PayloadSigner::PrepPayloadForHashing(
209 const string& payload_path,
210 const vector<int>& signature_sizes,
211 vector<char>* payload_out,
212 uint64_t* metadata_size_out,
213 uint64_t* signatures_offset_out) {
Darin Petkov9574f7e2011-01-13 10:48:12 -0800214 // TODO(petkov): Reduce memory usage -- the payload is manipulated in memory.
215
216 // Loads the payload and adds the signature op to it.
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700217 vector<vector<char> > signatures;
218 for (vector<int>::const_iterator it = signature_sizes.begin(),
219 e = signature_sizes.end(); it != e; ++it) {
220 vector<char> signature(*it, 0);
221 signatures.push_back(signature);
222 }
Darin Petkov9574f7e2011-01-13 10:48:12 -0800223 vector<char> signature_blob;
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700224 TEST_AND_RETURN_FALSE(ConvertSignatureToProtobufBlob(signatures,
Darin Petkov9574f7e2011-01-13 10:48:12 -0800225 &signature_blob));
Darin Petkov9574f7e2011-01-13 10:48:12 -0800226 TEST_AND_RETURN_FALSE(AddSignatureOpToPayload(payload_path,
227 signature_blob.size(),
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700228 payload_out,
229 metadata_size_out,
230 signatures_offset_out));
Don Garrett2ae37872013-10-25 13:33:20 -0700231
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700232 return true;
233}
234
235bool PayloadSigner::HashPayloadForSigning(const string& payload_path,
236 const vector<int>& signature_sizes,
237 vector<char>* out_hash_data) {
238 vector<char> payload;
239 uint64_t metadata_size;
240 uint64_t signatures_offset;
241
242 TEST_AND_RETURN_FALSE(PrepPayloadForHashing(payload_path,
243 signature_sizes,
244 &payload,
245 &metadata_size,
246 &signatures_offset));
Don Garrett2ae37872013-10-25 13:33:20 -0700247
248 // Calculates the hash on the updated payload. Note that we stop calculating
249 // before we reach the signature information.
250 TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfBytes(&payload[0],
251 signatures_offset,
252 out_hash_data));
Darin Petkov9574f7e2011-01-13 10:48:12 -0800253 return true;
254}
255
Jay Srinivasanf4318702012-09-24 11:56:24 -0700256bool PayloadSigner::HashMetadataForSigning(const string& payload_path,
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700257 const vector<int>& signature_sizes,
Jay Srinivasanf4318702012-09-24 11:56:24 -0700258 vector<char>* out_metadata_hash) {
Jay Srinivasanf4318702012-09-24 11:56:24 -0700259 vector<char> payload;
Jay Srinivasanf4318702012-09-24 11:56:24 -0700260 uint64_t metadata_size;
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700261 uint64_t signatures_offset;
262
263 TEST_AND_RETURN_FALSE(PrepPayloadForHashing(payload_path,
264 signature_sizes,
265 &payload,
266 &metadata_size,
267 &signatures_offset));
Jay Srinivasanf4318702012-09-24 11:56:24 -0700268
269 // Calculates the hash on the manifest.
270 TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfBytes(&payload[0],
271 metadata_size,
272 out_metadata_hash));
273 return true;
274}
275
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700276bool PayloadSigner::AddSignatureToPayload(
277 const string& payload_path,
278 const vector<vector<char> >& signatures,
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800279 const string& signed_payload_path,
280 uint64_t *out_metadata_size) {
Darin Petkov9574f7e2011-01-13 10:48:12 -0800281 // TODO(petkov): Reduce memory usage -- the payload is manipulated in memory.
282
283 // Loads the payload and adds the signature op to it.
284 vector<char> signature_blob;
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700285 TEST_AND_RETURN_FALSE(ConvertSignatureToProtobufBlob(signatures,
Darin Petkov9574f7e2011-01-13 10:48:12 -0800286 &signature_blob));
287 vector<char> payload;
Don Garrett2ae37872013-10-25 13:33:20 -0700288 uint64_t signatures_offset;
Darin Petkov9574f7e2011-01-13 10:48:12 -0800289 TEST_AND_RETURN_FALSE(AddSignatureOpToPayload(payload_path,
290 signature_blob.size(),
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800291 &payload,
Don Garrett2ae37872013-10-25 13:33:20 -0700292 out_metadata_size,
293 &signatures_offset));
Darin Petkov9574f7e2011-01-13 10:48:12 -0800294 // Appends the signature blob to the end of the payload and writes the new
295 // payload.
Don Garrett2ae37872013-10-25 13:33:20 -0700296 LOG(INFO) << "Payload size before signatures: " << payload.size();
297 payload.resize(signatures_offset);
298 payload.insert(payload.begin() + signatures_offset,
299 signature_blob.begin(),
300 signature_blob.end());
Darin Petkov9574f7e2011-01-13 10:48:12 -0800301 LOG(INFO) << "Signed payload size: " << payload.size();
302 TEST_AND_RETURN_FALSE(utils::WriteFile(signed_payload_path.c_str(),
303 payload.data(),
304 payload.size()));
305 return true;
306}
307
Jay Srinivasanf4318702012-09-24 11:56:24 -0700308bool PayloadSigner::GetMetadataSignature(const char* const metadata,
309 size_t metadata_size,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700310 const string& private_key_path,
311 string* out_signature) {
312 // Calculates the hash on the updated payload. Note that the payload includes
313 // the signature op but doesn't include the signature blob at the end.
Jay Srinivasanf4318702012-09-24 11:56:24 -0700314 vector<char> metadata_hash;
315 TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfBytes(metadata,
316 metadata_size,
317 &metadata_hash));
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700318
319 vector<char> signature;
Jay Srinivasanf4318702012-09-24 11:56:24 -0700320 TEST_AND_RETURN_FALSE(SignHash(metadata_hash,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700321 private_key_path,
322 &signature));
323
324 TEST_AND_RETURN_FALSE(OmahaHashCalculator::Base64Encode(&signature[0],
325 signature.size(),
326 out_signature));
327 return true;
328}
329
330
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700331} // namespace chromeos_update_engine