blob: 218b4328bb6a6b8e9f84bf6fc03fd3928eb480bc [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
Alex Deymo6f20dd42015-08-18 16:42:46 -070019#include <endian.h>
20
Darin Petkovb039d502010-12-03 09:08:04 -080021#include <base/logging.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070022#include <base/strings/string_split.h>
23#include <base/strings/string_util.h>
Alex Vakulenko981a9fb2015-02-09 12:51:24 -080024#include <chromeos/data_encoding.h>
Darin Petkovb039d502010-12-03 09:08:04 -080025#include <openssl/pem.h>
26
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070027#include "update_engine/omaha_hash_calculator.h"
Alex Deymo14158572015-06-13 03:37:08 -070028#include "update_engine/payload_generator/payload_file.h"
Alex Deymo923d8fa2014-07-15 17:58:51 -070029#include "update_engine/payload_verifier.h"
Andrew de los Reyes0c440052010-08-20 11:25:54 -070030#include "update_engine/subprocess.h"
31#include "update_engine/update_metadata.pb.h"
32#include "update_engine/utils.h"
33
34using std::string;
35using std::vector;
36
37namespace chromeos_update_engine {
38
Darin Petkov9574f7e2011-01-13 10:48:12 -080039namespace {
Andrew de los Reyesbdfaaf02011-03-30 10:35:12 -070040
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070041// Given raw |signatures|, packs them into a protobuf and serializes it into a
Darin Petkov9574f7e2011-01-13 10:48:12 -080042// binary blob. Returns true on success, false otherwise.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080043bool ConvertSignatureToProtobufBlob(const vector<chromeos::Blob>& signatures,
44 chromeos::Blob* out_signature_blob) {
Darin Petkov9574f7e2011-01-13 10:48:12 -080045 // Pack it into a protobuf
46 Signatures out_message;
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070047 uint32_t version = kSignatureMessageOriginalVersion;
48 LOG_IF(WARNING, kSignatureMessageCurrentVersion -
49 kSignatureMessageOriginalVersion + 1 < signatures.size())
Jay Srinivasan51dcf262012-09-13 17:24:32 -070050 << "You may want to support clients in the range ["
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070051 << kSignatureMessageOriginalVersion << ", "
52 << kSignatureMessageCurrentVersion << "] inclusive, but you only "
53 << "provided " << signatures.size() << " signatures.";
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080054 for (const chromeos::Blob& signature : signatures) {
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070055 Signatures_Signature* sig_message = out_message.add_signatures();
56 sig_message->set_version(version++);
57 sig_message->set_data(signature.data(), signature.size());
58 }
Darin Petkov9574f7e2011-01-13 10:48:12 -080059
60 // Serialize protobuf
61 string serialized;
62 TEST_AND_RETURN_FALSE(out_message.AppendToString(&serialized));
63 out_signature_blob->insert(out_signature_blob->end(),
64 serialized.begin(),
65 serialized.end());
66 LOG(INFO) << "Signature blob size: " << out_signature_blob->size();
67 return true;
68}
69
70// Given an unsigned payload under |payload_path| and the |signature_blob_size|
71// generates an updated payload that includes a dummy signature op in its
Jay Srinivasan738fdf32012-12-07 17:40:54 -080072// manifest. It populates |out_metadata_size| with the size of the final
Don Garrett2ae37872013-10-25 13:33:20 -070073// manifest after adding the dummy signature operation, and
74// |out_signatures_offset| with the expected offset for the new blob. Returns
75// true on success, false otherwise.
Darin Petkovadb3cef2011-01-13 16:16:08 -080076bool AddSignatureOpToPayload(const string& payload_path,
Don Garrett2ae37872013-10-25 13:33:20 -070077 uint64_t signature_blob_size,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080078 chromeos::Blob* out_payload,
Don Garrett2ae37872013-10-25 13:33:20 -070079 uint64_t* out_metadata_size,
80 uint64_t* out_signatures_offset) {
Darin Petkov9574f7e2011-01-13 10:48:12 -080081 const int kProtobufOffset = 20;
82 const int kProtobufSizeOffset = 12;
83
Darin Petkovadb3cef2011-01-13 16:16:08 -080084 // Loads the payload.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080085 chromeos::Blob payload;
Darin Petkov9574f7e2011-01-13 10:48:12 -080086 DeltaArchiveManifest manifest;
Darin Petkovadb3cef2011-01-13 16:16:08 -080087 uint64_t metadata_size;
Alex Deymo923d8fa2014-07-15 17:58:51 -070088 TEST_AND_RETURN_FALSE(PayloadVerifier::LoadPayload(
Darin Petkovadb3cef2011-01-13 16:16:08 -080089 payload_path, &payload, &manifest, &metadata_size));
Darin Petkov9574f7e2011-01-13 10:48:12 -080090
Don Garrett2ae37872013-10-25 13:33:20 -070091 // Is there already a signature op in place?
92 if (manifest.has_signatures_size()) {
Don Garrett2ae37872013-10-25 13:33:20 -070093 // The signature op is tied to the size of the signature blob, but not it's
94 // contents. We don't allow the manifest to change if there is already an op
95 // present, because that might invalidate previously generated
96 // hashes/signatures.
97 if (manifest.signatures_size() != signature_blob_size) {
98 LOG(ERROR) << "Attempt to insert different signature sized blob. "
99 << "(current:" << manifest.signatures_size()
100 << "new:" << signature_blob_size << ")";
101 return false;
102 }
Darin Petkov9574f7e2011-01-13 10:48:12 -0800103
Don Garrett2ae37872013-10-25 13:33:20 -0700104 LOG(INFO) << "Matching signature sizes already present.";
105 } else {
106 // Updates the manifest to include the signature operation.
Alex Deymo14158572015-06-13 03:37:08 -0700107 AddSignatureOp(payload.size() - metadata_size,
108 signature_blob_size,
109 &manifest);
Don Garrett2ae37872013-10-25 13:33:20 -0700110
111 // Updates the payload to include the new manifest.
112 string serialized_manifest;
113 TEST_AND_RETURN_FALSE(manifest.AppendToString(&serialized_manifest));
114 LOG(INFO) << "Updated protobuf size: " << serialized_manifest.size();
115 payload.erase(payload.begin() + kProtobufOffset,
116 payload.begin() + metadata_size);
117 payload.insert(payload.begin() + kProtobufOffset,
118 serialized_manifest.begin(),
119 serialized_manifest.end());
120
121 // Updates the protobuf size.
122 uint64_t size_be = htobe64(serialized_manifest.size());
123 memcpy(&payload[kProtobufSizeOffset], &size_be, sizeof(size_be));
124 metadata_size = serialized_manifest.size() + kProtobufOffset;
125
126 LOG(INFO) << "Updated payload size: " << payload.size();
127 LOG(INFO) << "Updated metadata size: " << metadata_size;
128 }
129
Darin Petkov9574f7e2011-01-13 10:48:12 -0800130 out_payload->swap(payload);
Don Garrett2ae37872013-10-25 13:33:20 -0700131 *out_metadata_size = metadata_size;
132 *out_signatures_offset = metadata_size + manifest.signatures_offset();
133 LOG(INFO) << "Signature Blob Offset: " << *out_signatures_offset;
Darin Petkov9574f7e2011-01-13 10:48:12 -0800134 return true;
135}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700136} // namespace
Darin Petkov9574f7e2011-01-13 10:48:12 -0800137
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800138bool PayloadSigner::SignHash(const chromeos::Blob& hash,
Darin Petkov9574f7e2011-01-13 10:48:12 -0800139 const string& private_key_path,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800140 chromeos::Blob* out_signature) {
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700141 LOG(INFO) << "Signing hash with private key: " << private_key_path;
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700142 string sig_path;
143 TEST_AND_RETURN_FALSE(
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700144 utils::MakeTempFile("signature.XXXXXX", &sig_path, nullptr));
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700145 ScopedPathUnlinker sig_path_unlinker(sig_path);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700146
147 string hash_path;
148 TEST_AND_RETURN_FALSE(
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700149 utils::MakeTempFile("hash.XXXXXX", &hash_path, nullptr));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700150 ScopedPathUnlinker hash_path_unlinker(hash_path);
Andrew de los Reyesbdfaaf02011-03-30 10:35:12 -0700151 // We expect unpadded SHA256 hash coming in
152 TEST_AND_RETURN_FALSE(hash.size() == 32);
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800153 chromeos::Blob padded_hash(hash);
Alex Deymo923d8fa2014-07-15 17:58:51 -0700154 PayloadVerifier::PadRSA2048SHA256Hash(&padded_hash);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700155 TEST_AND_RETURN_FALSE(utils::WriteFile(hash_path.c_str(),
Andrew de los Reyesbdfaaf02011-03-30 10:35:12 -0700156 padded_hash.data(),
157 padded_hash.size()));
Darin Petkovd22cb292010-09-29 10:02:29 -0700158
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700159 // This runs on the server, so it's okay to cop out and call openssl
160 // executable rather than properly use the library
161 vector<string> cmd;
Mike Frysinger2149be42012-03-12 19:23:47 -0400162 base::SplitString("openssl rsautl -raw -sign -inkey x -in x -out x",
Chris Masoned903c3b2011-05-12 15:35:46 -0700163 ' ',
164 &cmd);
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700165 cmd[cmd.size() - 5] = private_key_path;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700166 cmd[cmd.size() - 3] = hash_path;
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700167 cmd[cmd.size() - 1] = sig_path;
Darin Petkovd22cb292010-09-29 10:02:29 -0700168
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700169 int return_code = 0;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700170 TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &return_code,
171 nullptr));
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700172 TEST_AND_RETURN_FALSE(return_code == 0);
Darin Petkovd22cb292010-09-29 10:02:29 -0700173
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800174 chromeos::Blob signature;
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700175 TEST_AND_RETURN_FALSE(utils::ReadFile(sig_path, &signature));
Darin Petkov9574f7e2011-01-13 10:48:12 -0800176 out_signature->swap(signature);
177 return true;
178}
Darin Petkovd22cb292010-09-29 10:02:29 -0700179
Darin Petkov9574f7e2011-01-13 10:48:12 -0800180bool PayloadSigner::SignPayload(const string& unsigned_payload_path,
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700181 const vector<string>& private_key_paths,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800182 chromeos::Blob* out_signature_blob) {
183 chromeos::Blob hash_data;
Darin Petkov9574f7e2011-01-13 10:48:12 -0800184 TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfFile(
185 unsigned_payload_path, -1, &hash_data) ==
186 utils::FileSize(unsigned_payload_path));
Darin Petkovd22cb292010-09-29 10:02:29 -0700187
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800188 vector<chromeos::Blob> signatures;
Alex Deymo020600d2014-11-05 21:05:55 -0800189 for (const string& path : private_key_paths) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800190 chromeos::Blob signature;
Alex Deymo020600d2014-11-05 21:05:55 -0800191 TEST_AND_RETURN_FALSE(SignHash(hash_data, path, &signature));
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700192 signatures.push_back(signature);
193 }
194 TEST_AND_RETURN_FALSE(ConvertSignatureToProtobufBlob(signatures,
Darin Petkov9574f7e2011-01-13 10:48:12 -0800195 out_signature_blob));
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700196 return true;
197}
198
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700199bool PayloadSigner::SignatureBlobLength(const vector<string>& private_key_paths,
200 uint64_t* out_length) {
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700201 DCHECK(out_length);
Darin Petkovd22cb292010-09-29 10:02:29 -0700202
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700203 string x_path;
204 TEST_AND_RETURN_FALSE(
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700205 utils::MakeTempFile("signed_data.XXXXXX", &x_path, nullptr));
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700206 ScopedPathUnlinker x_path_unlinker(x_path);
207 TEST_AND_RETURN_FALSE(utils::WriteFile(x_path.c_str(), "x", 1));
208
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800209 chromeos::Blob sig_blob;
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700210 TEST_AND_RETURN_FALSE(PayloadSigner::SignPayload(x_path,
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700211 private_key_paths,
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700212 &sig_blob));
213 *out_length = sig_blob.size();
214 return true;
215}
216
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700217bool PayloadSigner::PrepPayloadForHashing(
218 const string& payload_path,
219 const vector<int>& signature_sizes,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800220 chromeos::Blob* payload_out,
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700221 uint64_t* metadata_size_out,
222 uint64_t* signatures_offset_out) {
Darin Petkov9574f7e2011-01-13 10:48:12 -0800223 // TODO(petkov): Reduce memory usage -- the payload is manipulated in memory.
224
225 // Loads the payload and adds the signature op to it.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800226 vector<chromeos::Blob> signatures;
Alex Deymo020600d2014-11-05 21:05:55 -0800227 for (int signature_size : signature_sizes) {
228 signatures.emplace_back(signature_size, 0);
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700229 }
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800230 chromeos::Blob signature_blob;
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700231 TEST_AND_RETURN_FALSE(ConvertSignatureToProtobufBlob(signatures,
Darin Petkov9574f7e2011-01-13 10:48:12 -0800232 &signature_blob));
Darin Petkov9574f7e2011-01-13 10:48:12 -0800233 TEST_AND_RETURN_FALSE(AddSignatureOpToPayload(payload_path,
234 signature_blob.size(),
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700235 payload_out,
236 metadata_size_out,
237 signatures_offset_out));
Don Garrett2ae37872013-10-25 13:33:20 -0700238
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700239 return true;
240}
241
242bool PayloadSigner::HashPayloadForSigning(const string& payload_path,
243 const vector<int>& signature_sizes,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800244 chromeos::Blob* out_hash_data) {
245 chromeos::Blob payload;
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700246 uint64_t metadata_size;
247 uint64_t signatures_offset;
248
249 TEST_AND_RETURN_FALSE(PrepPayloadForHashing(payload_path,
250 signature_sizes,
251 &payload,
252 &metadata_size,
253 &signatures_offset));
Don Garrett2ae37872013-10-25 13:33:20 -0700254
255 // Calculates the hash on the updated payload. Note that we stop calculating
256 // before we reach the signature information.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800257 TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfBytes(payload.data(),
Don Garrett2ae37872013-10-25 13:33:20 -0700258 signatures_offset,
259 out_hash_data));
Darin Petkov9574f7e2011-01-13 10:48:12 -0800260 return true;
261}
262
Jay Srinivasanf4318702012-09-24 11:56:24 -0700263bool PayloadSigner::HashMetadataForSigning(const string& payload_path,
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700264 const vector<int>& signature_sizes,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800265 chromeos::Blob* out_metadata_hash) {
266 chromeos::Blob payload;
Jay Srinivasanf4318702012-09-24 11:56:24 -0700267 uint64_t metadata_size;
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700268 uint64_t signatures_offset;
269
270 TEST_AND_RETURN_FALSE(PrepPayloadForHashing(payload_path,
271 signature_sizes,
272 &payload,
273 &metadata_size,
274 &signatures_offset));
Jay Srinivasanf4318702012-09-24 11:56:24 -0700275
276 // Calculates the hash on the manifest.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800277 TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfBytes(payload.data(),
Jay Srinivasanf4318702012-09-24 11:56:24 -0700278 metadata_size,
279 out_metadata_hash));
280 return true;
281}
282
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700283bool PayloadSigner::AddSignatureToPayload(
284 const string& payload_path,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800285 const vector<chromeos::Blob>& signatures,
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800286 const string& signed_payload_path,
287 uint64_t *out_metadata_size) {
Darin Petkov9574f7e2011-01-13 10:48:12 -0800288 // TODO(petkov): Reduce memory usage -- the payload is manipulated in memory.
289
290 // Loads the payload and adds the signature op to it.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800291 chromeos::Blob signature_blob;
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700292 TEST_AND_RETURN_FALSE(ConvertSignatureToProtobufBlob(signatures,
Darin Petkov9574f7e2011-01-13 10:48:12 -0800293 &signature_blob));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800294 chromeos::Blob payload;
Don Garrett2ae37872013-10-25 13:33:20 -0700295 uint64_t signatures_offset;
Darin Petkov9574f7e2011-01-13 10:48:12 -0800296 TEST_AND_RETURN_FALSE(AddSignatureOpToPayload(payload_path,
297 signature_blob.size(),
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800298 &payload,
Don Garrett2ae37872013-10-25 13:33:20 -0700299 out_metadata_size,
300 &signatures_offset));
Darin Petkov9574f7e2011-01-13 10:48:12 -0800301 // Appends the signature blob to the end of the payload and writes the new
302 // payload.
Don Garrett2ae37872013-10-25 13:33:20 -0700303 LOG(INFO) << "Payload size before signatures: " << payload.size();
304 payload.resize(signatures_offset);
305 payload.insert(payload.begin() + signatures_offset,
306 signature_blob.begin(),
307 signature_blob.end());
Darin Petkov9574f7e2011-01-13 10:48:12 -0800308 LOG(INFO) << "Signed payload size: " << payload.size();
309 TEST_AND_RETURN_FALSE(utils::WriteFile(signed_payload_path.c_str(),
310 payload.data(),
311 payload.size()));
312 return true;
313}
314
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800315bool PayloadSigner::GetMetadataSignature(const void* const metadata,
Jay Srinivasanf4318702012-09-24 11:56:24 -0700316 size_t metadata_size,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700317 const string& private_key_path,
318 string* out_signature) {
319 // Calculates the hash on the updated payload. Note that the payload includes
320 // the signature op but doesn't include the signature blob at the end.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800321 chromeos::Blob metadata_hash;
Jay Srinivasanf4318702012-09-24 11:56:24 -0700322 TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfBytes(metadata,
323 metadata_size,
324 &metadata_hash));
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700325
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800326 chromeos::Blob signature;
Jay Srinivasanf4318702012-09-24 11:56:24 -0700327 TEST_AND_RETURN_FALSE(SignHash(metadata_hash,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700328 private_key_path,
329 &signature));
330
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800331 *out_signature = chromeos::data_encoding::Base64Encode(signature);
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700332 return true;
333}
334
335
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700336} // namespace chromeos_update_engine