blob: 5b7d677a27cdda8339ccaad62f70f23eda8b05ef [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2011 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//
Andrew de los Reyes0c440052010-08-20 11:25:54 -070016
Alex Deymo923d8fa2014-07-15 17:58:51 -070017#include "update_engine/payload_generator/payload_signer.h"
Andrew de los Reyes0c440052010-08-20 11:25:54 -070018
Darin Petkovb039d502010-12-03 09:08:04 -080019#include <base/logging.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070020#include <base/strings/string_split.h>
21#include <base/strings/string_util.h>
Alex Vakulenko981a9fb2015-02-09 12:51:24 -080022#include <chromeos/data_encoding.h>
Darin Petkovb039d502010-12-03 09:08:04 -080023#include <openssl/pem.h>
24
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070025#include "update_engine/omaha_hash_calculator.h"
Alex Deymo14158572015-06-13 03:37:08 -070026#include "update_engine/payload_generator/payload_file.h"
Alex Deymo923d8fa2014-07-15 17:58:51 -070027#include "update_engine/payload_verifier.h"
Andrew de los Reyes0c440052010-08-20 11:25:54 -070028#include "update_engine/subprocess.h"
29#include "update_engine/update_metadata.pb.h"
30#include "update_engine/utils.h"
31
32using std::string;
33using std::vector;
34
35namespace chromeos_update_engine {
36
Darin Petkov9574f7e2011-01-13 10:48:12 -080037namespace {
Andrew de los Reyesbdfaaf02011-03-30 10:35:12 -070038
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070039// Given raw |signatures|, packs them into a protobuf and serializes it into a
Darin Petkov9574f7e2011-01-13 10:48:12 -080040// binary blob. Returns true on success, false otherwise.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080041bool ConvertSignatureToProtobufBlob(const vector<chromeos::Blob>& signatures,
42 chromeos::Blob* out_signature_blob) {
Darin Petkov9574f7e2011-01-13 10:48:12 -080043 // Pack it into a protobuf
44 Signatures out_message;
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070045 uint32_t version = kSignatureMessageOriginalVersion;
46 LOG_IF(WARNING, kSignatureMessageCurrentVersion -
47 kSignatureMessageOriginalVersion + 1 < signatures.size())
Jay Srinivasan51dcf262012-09-13 17:24:32 -070048 << "You may want to support clients in the range ["
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070049 << kSignatureMessageOriginalVersion << ", "
50 << kSignatureMessageCurrentVersion << "] inclusive, but you only "
51 << "provided " << signatures.size() << " signatures.";
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080052 for (const chromeos::Blob& signature : signatures) {
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070053 Signatures_Signature* sig_message = out_message.add_signatures();
54 sig_message->set_version(version++);
55 sig_message->set_data(signature.data(), signature.size());
56 }
Darin Petkov9574f7e2011-01-13 10:48:12 -080057
58 // Serialize protobuf
59 string serialized;
60 TEST_AND_RETURN_FALSE(out_message.AppendToString(&serialized));
61 out_signature_blob->insert(out_signature_blob->end(),
62 serialized.begin(),
63 serialized.end());
64 LOG(INFO) << "Signature blob size: " << out_signature_blob->size();
65 return true;
66}
67
68// Given an unsigned payload under |payload_path| and the |signature_blob_size|
69// generates an updated payload that includes a dummy signature op in its
Jay Srinivasan738fdf32012-12-07 17:40:54 -080070// manifest. It populates |out_metadata_size| with the size of the final
Don Garrett2ae37872013-10-25 13:33:20 -070071// manifest after adding the dummy signature operation, and
72// |out_signatures_offset| with the expected offset for the new blob. Returns
73// true on success, false otherwise.
Darin Petkovadb3cef2011-01-13 16:16:08 -080074bool AddSignatureOpToPayload(const string& payload_path,
Don Garrett2ae37872013-10-25 13:33:20 -070075 uint64_t signature_blob_size,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080076 chromeos::Blob* out_payload,
Don Garrett2ae37872013-10-25 13:33:20 -070077 uint64_t* out_metadata_size,
78 uint64_t* out_signatures_offset) {
Darin Petkov9574f7e2011-01-13 10:48:12 -080079 const int kProtobufOffset = 20;
80 const int kProtobufSizeOffset = 12;
81
Darin Petkovadb3cef2011-01-13 16:16:08 -080082 // Loads the payload.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080083 chromeos::Blob payload;
Darin Petkov9574f7e2011-01-13 10:48:12 -080084 DeltaArchiveManifest manifest;
Darin Petkovadb3cef2011-01-13 16:16:08 -080085 uint64_t metadata_size;
Alex Deymo923d8fa2014-07-15 17:58:51 -070086 TEST_AND_RETURN_FALSE(PayloadVerifier::LoadPayload(
Darin Petkovadb3cef2011-01-13 16:16:08 -080087 payload_path, &payload, &manifest, &metadata_size));
Darin Petkov9574f7e2011-01-13 10:48:12 -080088
Don Garrett2ae37872013-10-25 13:33:20 -070089 // Is there already a signature op in place?
90 if (manifest.has_signatures_size()) {
Don Garrett2ae37872013-10-25 13:33:20 -070091 // The signature op is tied to the size of the signature blob, but not it's
92 // contents. We don't allow the manifest to change if there is already an op
93 // present, because that might invalidate previously generated
94 // hashes/signatures.
95 if (manifest.signatures_size() != signature_blob_size) {
96 LOG(ERROR) << "Attempt to insert different signature sized blob. "
97 << "(current:" << manifest.signatures_size()
98 << "new:" << signature_blob_size << ")";
99 return false;
100 }
Darin Petkov9574f7e2011-01-13 10:48:12 -0800101
Don Garrett2ae37872013-10-25 13:33:20 -0700102 LOG(INFO) << "Matching signature sizes already present.";
103 } else {
104 // Updates the manifest to include the signature operation.
Alex Deymo14158572015-06-13 03:37:08 -0700105 AddSignatureOp(payload.size() - metadata_size,
106 signature_blob_size,
107 &manifest);
Don Garrett2ae37872013-10-25 13:33:20 -0700108
109 // Updates the payload to include the new manifest.
110 string serialized_manifest;
111 TEST_AND_RETURN_FALSE(manifest.AppendToString(&serialized_manifest));
112 LOG(INFO) << "Updated protobuf size: " << serialized_manifest.size();
113 payload.erase(payload.begin() + kProtobufOffset,
114 payload.begin() + metadata_size);
115 payload.insert(payload.begin() + kProtobufOffset,
116 serialized_manifest.begin(),
117 serialized_manifest.end());
118
119 // Updates the protobuf size.
120 uint64_t size_be = htobe64(serialized_manifest.size());
121 memcpy(&payload[kProtobufSizeOffset], &size_be, sizeof(size_be));
122 metadata_size = serialized_manifest.size() + kProtobufOffset;
123
124 LOG(INFO) << "Updated payload size: " << payload.size();
125 LOG(INFO) << "Updated metadata size: " << metadata_size;
126 }
127
Darin Petkov9574f7e2011-01-13 10:48:12 -0800128 out_payload->swap(payload);
Don Garrett2ae37872013-10-25 13:33:20 -0700129 *out_metadata_size = metadata_size;
130 *out_signatures_offset = metadata_size + manifest.signatures_offset();
131 LOG(INFO) << "Signature Blob Offset: " << *out_signatures_offset;
Darin Petkov9574f7e2011-01-13 10:48:12 -0800132 return true;
133}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700134} // namespace
Darin Petkov9574f7e2011-01-13 10:48:12 -0800135
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800136bool PayloadSigner::SignHash(const chromeos::Blob& hash,
Darin Petkov9574f7e2011-01-13 10:48:12 -0800137 const string& private_key_path,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800138 chromeos::Blob* out_signature) {
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700139 LOG(INFO) << "Signing hash with private key: " << private_key_path;
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700140 string sig_path;
141 TEST_AND_RETURN_FALSE(
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700142 utils::MakeTempFile("signature.XXXXXX", &sig_path, nullptr));
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700143 ScopedPathUnlinker sig_path_unlinker(sig_path);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700144
145 string hash_path;
146 TEST_AND_RETURN_FALSE(
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700147 utils::MakeTempFile("hash.XXXXXX", &hash_path, nullptr));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700148 ScopedPathUnlinker hash_path_unlinker(hash_path);
Andrew de los Reyesbdfaaf02011-03-30 10:35:12 -0700149 // We expect unpadded SHA256 hash coming in
150 TEST_AND_RETURN_FALSE(hash.size() == 32);
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800151 chromeos::Blob padded_hash(hash);
Alex Deymo923d8fa2014-07-15 17:58:51 -0700152 PayloadVerifier::PadRSA2048SHA256Hash(&padded_hash);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700153 TEST_AND_RETURN_FALSE(utils::WriteFile(hash_path.c_str(),
Andrew de los Reyesbdfaaf02011-03-30 10:35:12 -0700154 padded_hash.data(),
155 padded_hash.size()));
Darin Petkovd22cb292010-09-29 10:02:29 -0700156
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700157 // This runs on the server, so it's okay to cop out and call openssl
158 // executable rather than properly use the library
159 vector<string> cmd;
Mike Frysinger2149be42012-03-12 19:23:47 -0400160 base::SplitString("openssl rsautl -raw -sign -inkey x -in x -out x",
Chris Masoned903c3b2011-05-12 15:35:46 -0700161 ' ',
162 &cmd);
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700163 cmd[cmd.size() - 5] = private_key_path;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700164 cmd[cmd.size() - 3] = hash_path;
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700165 cmd[cmd.size() - 1] = sig_path;
Darin Petkovd22cb292010-09-29 10:02:29 -0700166
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700167 int return_code = 0;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700168 TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &return_code,
169 nullptr));
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700170 TEST_AND_RETURN_FALSE(return_code == 0);
Darin Petkovd22cb292010-09-29 10:02:29 -0700171
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800172 chromeos::Blob signature;
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700173 TEST_AND_RETURN_FALSE(utils::ReadFile(sig_path, &signature));
Darin Petkov9574f7e2011-01-13 10:48:12 -0800174 out_signature->swap(signature);
175 return true;
176}
Darin Petkovd22cb292010-09-29 10:02:29 -0700177
Darin Petkov9574f7e2011-01-13 10:48:12 -0800178bool PayloadSigner::SignPayload(const string& unsigned_payload_path,
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700179 const vector<string>& private_key_paths,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800180 chromeos::Blob* out_signature_blob) {
181 chromeos::Blob hash_data;
Darin Petkov9574f7e2011-01-13 10:48:12 -0800182 TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfFile(
183 unsigned_payload_path, -1, &hash_data) ==
184 utils::FileSize(unsigned_payload_path));
Darin Petkovd22cb292010-09-29 10:02:29 -0700185
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800186 vector<chromeos::Blob> signatures;
Alex Deymo020600d2014-11-05 21:05:55 -0800187 for (const string& path : private_key_paths) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800188 chromeos::Blob signature;
Alex Deymo020600d2014-11-05 21:05:55 -0800189 TEST_AND_RETURN_FALSE(SignHash(hash_data, path, &signature));
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700190 signatures.push_back(signature);
191 }
192 TEST_AND_RETURN_FALSE(ConvertSignatureToProtobufBlob(signatures,
Darin Petkov9574f7e2011-01-13 10:48:12 -0800193 out_signature_blob));
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700194 return true;
195}
196
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700197bool PayloadSigner::SignatureBlobLength(const vector<string>& private_key_paths,
198 uint64_t* out_length) {
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700199 DCHECK(out_length);
Darin Petkovd22cb292010-09-29 10:02:29 -0700200
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700201 string x_path;
202 TEST_AND_RETURN_FALSE(
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700203 utils::MakeTempFile("signed_data.XXXXXX", &x_path, nullptr));
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700204 ScopedPathUnlinker x_path_unlinker(x_path);
205 TEST_AND_RETURN_FALSE(utils::WriteFile(x_path.c_str(), "x", 1));
206
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800207 chromeos::Blob sig_blob;
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700208 TEST_AND_RETURN_FALSE(PayloadSigner::SignPayload(x_path,
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700209 private_key_paths,
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700210 &sig_blob));
211 *out_length = sig_blob.size();
212 return true;
213}
214
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700215bool PayloadSigner::PrepPayloadForHashing(
216 const string& payload_path,
217 const vector<int>& signature_sizes,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800218 chromeos::Blob* payload_out,
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700219 uint64_t* metadata_size_out,
220 uint64_t* signatures_offset_out) {
Darin Petkov9574f7e2011-01-13 10:48:12 -0800221 // TODO(petkov): Reduce memory usage -- the payload is manipulated in memory.
222
223 // Loads the payload and adds the signature op to it.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800224 vector<chromeos::Blob> signatures;
Alex Deymo020600d2014-11-05 21:05:55 -0800225 for (int signature_size : signature_sizes) {
226 signatures.emplace_back(signature_size, 0);
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700227 }
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800228 chromeos::Blob signature_blob;
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700229 TEST_AND_RETURN_FALSE(ConvertSignatureToProtobufBlob(signatures,
Darin Petkov9574f7e2011-01-13 10:48:12 -0800230 &signature_blob));
Darin Petkov9574f7e2011-01-13 10:48:12 -0800231 TEST_AND_RETURN_FALSE(AddSignatureOpToPayload(payload_path,
232 signature_blob.size(),
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700233 payload_out,
234 metadata_size_out,
235 signatures_offset_out));
Don Garrett2ae37872013-10-25 13:33:20 -0700236
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700237 return true;
238}
239
240bool PayloadSigner::HashPayloadForSigning(const string& payload_path,
241 const vector<int>& signature_sizes,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800242 chromeos::Blob* out_hash_data) {
243 chromeos::Blob payload;
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700244 uint64_t metadata_size;
245 uint64_t signatures_offset;
246
247 TEST_AND_RETURN_FALSE(PrepPayloadForHashing(payload_path,
248 signature_sizes,
249 &payload,
250 &metadata_size,
251 &signatures_offset));
Don Garrett2ae37872013-10-25 13:33:20 -0700252
253 // Calculates the hash on the updated payload. Note that we stop calculating
254 // before we reach the signature information.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800255 TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfBytes(payload.data(),
Don Garrett2ae37872013-10-25 13:33:20 -0700256 signatures_offset,
257 out_hash_data));
Darin Petkov9574f7e2011-01-13 10:48:12 -0800258 return true;
259}
260
Jay Srinivasanf4318702012-09-24 11:56:24 -0700261bool PayloadSigner::HashMetadataForSigning(const string& payload_path,
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700262 const vector<int>& signature_sizes,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800263 chromeos::Blob* out_metadata_hash) {
264 chromeos::Blob payload;
Jay Srinivasanf4318702012-09-24 11:56:24 -0700265 uint64_t metadata_size;
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700266 uint64_t signatures_offset;
267
268 TEST_AND_RETURN_FALSE(PrepPayloadForHashing(payload_path,
269 signature_sizes,
270 &payload,
271 &metadata_size,
272 &signatures_offset));
Jay Srinivasanf4318702012-09-24 11:56:24 -0700273
274 // Calculates the hash on the manifest.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800275 TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfBytes(payload.data(),
Jay Srinivasanf4318702012-09-24 11:56:24 -0700276 metadata_size,
277 out_metadata_hash));
278 return true;
279}
280
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700281bool PayloadSigner::AddSignatureToPayload(
282 const string& payload_path,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800283 const vector<chromeos::Blob>& signatures,
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800284 const string& signed_payload_path,
285 uint64_t *out_metadata_size) {
Darin Petkov9574f7e2011-01-13 10:48:12 -0800286 // TODO(petkov): Reduce memory usage -- the payload is manipulated in memory.
287
288 // Loads the payload and adds the signature op to it.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800289 chromeos::Blob signature_blob;
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700290 TEST_AND_RETURN_FALSE(ConvertSignatureToProtobufBlob(signatures,
Darin Petkov9574f7e2011-01-13 10:48:12 -0800291 &signature_blob));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800292 chromeos::Blob payload;
Don Garrett2ae37872013-10-25 13:33:20 -0700293 uint64_t signatures_offset;
Darin Petkov9574f7e2011-01-13 10:48:12 -0800294 TEST_AND_RETURN_FALSE(AddSignatureOpToPayload(payload_path,
295 signature_blob.size(),
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800296 &payload,
Don Garrett2ae37872013-10-25 13:33:20 -0700297 out_metadata_size,
298 &signatures_offset));
Darin Petkov9574f7e2011-01-13 10:48:12 -0800299 // Appends the signature blob to the end of the payload and writes the new
300 // payload.
Don Garrett2ae37872013-10-25 13:33:20 -0700301 LOG(INFO) << "Payload size before signatures: " << payload.size();
302 payload.resize(signatures_offset);
303 payload.insert(payload.begin() + signatures_offset,
304 signature_blob.begin(),
305 signature_blob.end());
Darin Petkov9574f7e2011-01-13 10:48:12 -0800306 LOG(INFO) << "Signed payload size: " << payload.size();
307 TEST_AND_RETURN_FALSE(utils::WriteFile(signed_payload_path.c_str(),
308 payload.data(),
309 payload.size()));
310 return true;
311}
312
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800313bool PayloadSigner::GetMetadataSignature(const void* const metadata,
Jay Srinivasanf4318702012-09-24 11:56:24 -0700314 size_t metadata_size,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700315 const string& private_key_path,
316 string* out_signature) {
317 // Calculates the hash on the updated payload. Note that the payload includes
318 // the signature op but doesn't include the signature blob at the end.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800319 chromeos::Blob metadata_hash;
Jay Srinivasanf4318702012-09-24 11:56:24 -0700320 TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfBytes(metadata,
321 metadata_size,
322 &metadata_hash));
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700323
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800324 chromeos::Blob signature;
Jay Srinivasanf4318702012-09-24 11:56:24 -0700325 TEST_AND_RETURN_FALSE(SignHash(metadata_hash,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700326 private_key_path,
327 &signature));
328
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800329 *out_signature = chromeos::data_encoding::Base64Encode(signature);
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700330 return true;
331}
332
333
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700334} // namespace chromeos_update_engine