blob: 60a80d1af051df2a15b341e21d03ede8ff1db723 [file] [log] [blame]
Andrew de los Reyes0c440052010-08-20 11:25:54 -07001// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "update_engine/payload_signer.h"
6
Darin Petkovb039d502010-12-03 09:08:04 -08007#include <base/logging.h>
8#include <base/string_util.h>
9#include <openssl/pem.h>
10
Darin Petkov9574f7e2011-01-13 10:48:12 -080011#include "update_engine/delta_diff_generator.h"
12#include "update_engine/delta_performer.h"
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070013#include "update_engine/omaha_hash_calculator.h"
Andrew de los Reyes0c440052010-08-20 11:25:54 -070014#include "update_engine/subprocess.h"
15#include "update_engine/update_metadata.pb.h"
16#include "update_engine/utils.h"
17
18using std::string;
19using std::vector;
20
21namespace chromeos_update_engine {
22
23const uint32_t kSignatureMessageVersion = 1;
24
Darin Petkov9574f7e2011-01-13 10:48:12 -080025namespace {
26// Given a raw |signature|, packs it into a protobuf and serializes it into a
27// binary blob. Returns true on success, false otherwise.
28bool ConvertSignatureToProtobufBlob(const vector<char> signature,
29 vector<char>* out_signature_blob) {
30 // Pack it into a protobuf
31 Signatures out_message;
32 Signatures_Signature* sig_message = out_message.add_signatures();
33 sig_message->set_version(kSignatureMessageVersion);
34 sig_message->set_data(signature.data(), signature.size());
35
36 // Serialize protobuf
37 string serialized;
38 TEST_AND_RETURN_FALSE(out_message.AppendToString(&serialized));
39 out_signature_blob->insert(out_signature_blob->end(),
40 serialized.begin(),
41 serialized.end());
42 LOG(INFO) << "Signature blob size: " << out_signature_blob->size();
43 return true;
44}
45
Darin Petkovadb3cef2011-01-13 16:16:08 -080046bool LoadPayload(const string& payload_path,
47 vector<char>* out_payload,
48 DeltaArchiveManifest* out_manifest,
49 uint64_t* out_metadata_size) {
50 vector<char> payload;
51 // Loads the payload and parses the manifest.
52 TEST_AND_RETURN_FALSE(utils::ReadFile(payload_path, &payload));
53 LOG(INFO) << "Payload size: " << payload.size();
54 TEST_AND_RETURN_FALSE(DeltaPerformer::ParsePayloadMetadata(
55 payload, out_manifest, out_metadata_size) ==
56 DeltaPerformer::kMetadataParseSuccess);
57 LOG(INFO) << "Metadata size: " << *out_metadata_size;
58 out_payload->swap(payload);
59 return true;
60}
61
Darin Petkov9574f7e2011-01-13 10:48:12 -080062// Given an unsigned payload under |payload_path| and the |signature_blob_size|
63// generates an updated payload that includes a dummy signature op in its
64// manifest. Returns true on success, false otherwise.
Darin Petkovadb3cef2011-01-13 16:16:08 -080065bool AddSignatureOpToPayload(const string& payload_path,
Darin Petkov9574f7e2011-01-13 10:48:12 -080066 int signature_blob_size,
67 vector<char>* out_payload) {
68 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;
75 TEST_AND_RETURN_FALSE(LoadPayload(
76 payload_path, &payload, &manifest, &metadata_size));
Darin Petkov9574f7e2011-01-13 10:48:12 -080077 TEST_AND_RETURN_FALSE(!manifest.has_signatures_offset() &&
78 !manifest.has_signatures_size());
79
80 // Updates the manifest to include the signature operation.
81 DeltaDiffGenerator::AddSignatureOp(payload.size() - metadata_size,
82 signature_blob_size,
83 &manifest);
84
85 // Updates the payload to include the new manifest.
86 string serialized_manifest;
87 TEST_AND_RETURN_FALSE(manifest.AppendToString(&serialized_manifest));
88 LOG(INFO) << "Updated protobuf size: " << serialized_manifest.size();
89 payload.erase(payload.begin() + kProtobufOffset,
90 payload.begin() + metadata_size);
91 payload.insert(payload.begin() + kProtobufOffset,
92 serialized_manifest.begin(),
93 serialized_manifest.end());
94
95 // Updates the protobuf size.
96 uint64_t size_be = htobe64(serialized_manifest.size());
97 memcpy(&payload[kProtobufSizeOffset], &size_be, sizeof(size_be));
98 LOG(INFO) << "Updated payload size: " << payload.size();
99 out_payload->swap(payload);
100 return true;
101}
102} // namespace {}
103
104bool PayloadSigner::SignHash(const vector<char>& hash,
105 const string& private_key_path,
106 vector<char>* out_signature) {
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700107 string sig_path;
108 TEST_AND_RETURN_FALSE(
109 utils::MakeTempFile("/tmp/signature.XXXXXX", &sig_path, NULL));
110 ScopedPathUnlinker sig_path_unlinker(sig_path);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700111
112 string hash_path;
113 TEST_AND_RETURN_FALSE(
114 utils::MakeTempFile("/tmp/hash.XXXXXX", &hash_path, NULL));
115 ScopedPathUnlinker hash_path_unlinker(hash_path);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700116 TEST_AND_RETURN_FALSE(utils::WriteFile(hash_path.c_str(),
Darin Petkov9574f7e2011-01-13 10:48:12 -0800117 hash.data(),
118 hash.size()));
Darin Petkovd22cb292010-09-29 10:02:29 -0700119
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700120 // This runs on the server, so it's okay to cop out and call openssl
121 // executable rather than properly use the library
122 vector<string> cmd;
123 SplitString("/usr/bin/openssl rsautl -pkcs -sign -inkey x -in x -out x",
124 ' ',
125 &cmd);
126 cmd[cmd.size() - 5] = private_key_path;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700127 cmd[cmd.size() - 3] = hash_path;
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700128 cmd[cmd.size() - 1] = sig_path;
Darin Petkovd22cb292010-09-29 10:02:29 -0700129
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700130 int return_code = 0;
131 TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &return_code));
132 TEST_AND_RETURN_FALSE(return_code == 0);
Darin Petkovd22cb292010-09-29 10:02:29 -0700133
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700134 vector<char> signature;
135 TEST_AND_RETURN_FALSE(utils::ReadFile(sig_path, &signature));
Darin Petkov9574f7e2011-01-13 10:48:12 -0800136 out_signature->swap(signature);
137 return true;
138}
Darin Petkovd22cb292010-09-29 10:02:29 -0700139
Darin Petkov9574f7e2011-01-13 10:48:12 -0800140bool PayloadSigner::SignPayload(const string& unsigned_payload_path,
141 const string& private_key_path,
142 vector<char>* out_signature_blob) {
143 vector<char> hash_data;
144 TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfFile(
145 unsigned_payload_path, -1, &hash_data) ==
146 utils::FileSize(unsigned_payload_path));
Darin Petkovd22cb292010-09-29 10:02:29 -0700147
Darin Petkov9574f7e2011-01-13 10:48:12 -0800148 vector<char> signature;
149 TEST_AND_RETURN_FALSE(SignHash(hash_data, private_key_path, &signature));
150 TEST_AND_RETURN_FALSE(ConvertSignatureToProtobufBlob(signature,
151 out_signature_blob));
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700152 return true;
153}
154
155bool PayloadSigner::SignatureBlobLength(
156 const string& private_key_path,
157 uint64_t* out_length) {
158 DCHECK(out_length);
Darin Petkovd22cb292010-09-29 10:02:29 -0700159
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700160 string x_path;
161 TEST_AND_RETURN_FALSE(
162 utils::MakeTempFile("/tmp/signed_data.XXXXXX", &x_path, NULL));
163 ScopedPathUnlinker x_path_unlinker(x_path);
164 TEST_AND_RETURN_FALSE(utils::WriteFile(x_path.c_str(), "x", 1));
165
166 vector<char> sig_blob;
167 TEST_AND_RETURN_FALSE(PayloadSigner::SignPayload(x_path,
168 private_key_path,
169 &sig_blob));
170 *out_length = sig_blob.size();
171 return true;
172}
173
Darin Petkovd7061ab2010-10-06 14:37:09 -0700174bool PayloadSigner::VerifySignature(const std::vector<char>& signature_blob,
175 const std::string& public_key_path,
176 std::vector<char>* out_hash_data) {
177 TEST_AND_RETURN_FALSE(!public_key_path.empty());
178
179 Signatures signatures;
180 TEST_AND_RETURN_FALSE(signatures.ParseFromArray(&signature_blob[0],
181 signature_blob.size()));
182
183 // Finds a signature that matches the current version.
184 int sig_index = 0;
185 for (; sig_index < signatures.signatures_size(); sig_index++) {
186 const Signatures_Signature& signature = signatures.signatures(sig_index);
187 if (signature.has_version() &&
188 signature.version() == kSignatureMessageVersion) {
189 break;
190 }
191 }
192 TEST_AND_RETURN_FALSE(sig_index < signatures.signatures_size());
193
194 const Signatures_Signature& signature = signatures.signatures(sig_index);
Darin Petkovb039d502010-12-03 09:08:04 -0800195 const string& sig_data = signature.data();
Darin Petkovd7061ab2010-10-06 14:37:09 -0700196
Darin Petkovb039d502010-12-03 09:08:04 -0800197 // The code below executes the equivalent of:
198 //
199 // openssl rsautl -verify -pubin -inkey |public_key_path|
200 // -in |sig_data| -out |out_hash_data|
Darin Petkovd7061ab2010-10-06 14:37:09 -0700201
Darin Petkovb039d502010-12-03 09:08:04 -0800202 // Loads the public key.
203 FILE* fpubkey = fopen(public_key_path.c_str(), "rb");
204 TEST_AND_RETURN_FALSE(fpubkey != NULL);
205 char dummy_password[] = { ' ', 0 }; // Ensure no password is read from stdin.
206 RSA* rsa = PEM_read_RSA_PUBKEY(fpubkey, NULL, NULL, dummy_password);
207 fclose(fpubkey);
208 TEST_AND_RETURN_FALSE(rsa != NULL);
209 unsigned int keysize = RSA_size(rsa);
210 if (sig_data.size() > 2 * keysize) {
211 LOG(ERROR) << "Signature size is too big for public key size.";
212 RSA_free(rsa);
213 return false;
214 }
Darin Petkovd7061ab2010-10-06 14:37:09 -0700215
Darin Petkovb039d502010-12-03 09:08:04 -0800216 // Decrypts the signature.
217 vector<char> hash_data(keysize);
218 int decrypt_size = RSA_public_decrypt(
219 sig_data.size(),
220 reinterpret_cast<const unsigned char*>(sig_data.data()),
221 reinterpret_cast<unsigned char*>(hash_data.data()),
222 rsa,
223 RSA_PKCS1_PADDING);
224 RSA_free(rsa);
225 TEST_AND_RETURN_FALSE(decrypt_size > 0 &&
226 decrypt_size <= static_cast<int>(hash_data.size()));
227 hash_data.resize(decrypt_size);
228 out_hash_data->swap(hash_data);
Darin Petkovd7061ab2010-10-06 14:37:09 -0700229 return true;
230}
231
Darin Petkovadb3cef2011-01-13 16:16:08 -0800232bool PayloadSigner::VerifySignedPayload(const std::string& payload_path,
233 const std::string& public_key_path) {
234 vector<char> payload;
235 DeltaArchiveManifest manifest;
236 uint64_t metadata_size;
237 TEST_AND_RETURN_FALSE(LoadPayload(
238 payload_path, &payload, &manifest, &metadata_size));
239 TEST_AND_RETURN_FALSE(manifest.has_signatures_offset() &&
240 manifest.has_signatures_size());
241 CHECK_EQ(payload.size(),
242 metadata_size + manifest.signatures_offset() +
243 manifest.signatures_size());
244 vector<char> signature_blob(
245 payload.begin() + metadata_size + manifest.signatures_offset(),
246 payload.end());
247 vector<char> signed_hash;
248 TEST_AND_RETURN_FALSE(VerifySignature(
249 signature_blob, public_key_path, &signed_hash));
250 TEST_AND_RETURN_FALSE(!signed_hash.empty());
251 vector<char> hash;
252 TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfBytes(
253 payload.data(), metadata_size + manifest.signatures_offset(), &hash));
254 TEST_AND_RETURN_FALSE(hash == signed_hash);
255 return true;
256}
257
Darin Petkov9574f7e2011-01-13 10:48:12 -0800258bool PayloadSigner::HashPayloadForSigning(const std::string& payload_path,
259 int signature_size,
260 vector<char>* out_hash_data) {
261 // TODO(petkov): Reduce memory usage -- the payload is manipulated in memory.
262
263 // Loads the payload and adds the signature op to it.
264 vector<char> signature(signature_size, 0);
265 vector<char> signature_blob;
266 TEST_AND_RETURN_FALSE(ConvertSignatureToProtobufBlob(signature,
267 &signature_blob));
268 vector<char> payload;
269 TEST_AND_RETURN_FALSE(AddSignatureOpToPayload(payload_path,
270 signature_blob.size(),
271 &payload));
272 // Calculates the hash on the updated payload. Note that the payload includes
273 // the signature op but doesn't include the signature blob at the end.
274 TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfData(payload,
275 out_hash_data));
276 return true;
277}
278
279bool PayloadSigner::AddSignatureToPayload(const string& payload_path,
280 const vector<char>& signature,
281 const string& signed_payload_path) {
282 // 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;
286 TEST_AND_RETURN_FALSE(ConvertSignatureToProtobufBlob(signature,
287 &signature_blob));
288 vector<char> payload;
289 TEST_AND_RETURN_FALSE(AddSignatureOpToPayload(payload_path,
290 signature_blob.size(),
291 &payload));
292 // Appends the signature blob to the end of the payload and writes the new
293 // payload.
294 payload.insert(payload.end(), signature_blob.begin(), signature_blob.end());
295 LOG(INFO) << "Signed payload size: " << payload.size();
296 TEST_AND_RETURN_FALSE(utils::WriteFile(signed_payload_path.c_str(),
297 payload.data(),
298 payload.size()));
299 return true;
300}
301
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700302} // namespace chromeos_update_engine