Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame^] | 5 | #include "update_engine/payload_generator/payload_signer.h" |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 6 | |
Darin Petkov | b039d50 | 2010-12-03 09:08:04 -0800 | [diff] [blame] | 7 | #include <base/logging.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 8 | #include <base/strings/string_split.h> |
| 9 | #include <base/strings/string_util.h> |
Darin Petkov | b039d50 | 2010-12-03 09:08:04 -0800 | [diff] [blame] | 10 | #include <openssl/pem.h> |
| 11 | |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 12 | #include "update_engine/omaha_hash_calculator.h" |
Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 13 | #include "update_engine/payload_generator/delta_diff_generator.h" |
Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame^] | 14 | #include "update_engine/payload_verifier.h" |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 15 | #include "update_engine/subprocess.h" |
| 16 | #include "update_engine/update_metadata.pb.h" |
| 17 | #include "update_engine/utils.h" |
| 18 | |
| 19 | using std::string; |
| 20 | using std::vector; |
| 21 | |
| 22 | namespace chromeos_update_engine { |
| 23 | |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 24 | namespace { |
Andrew de los Reyes | bdfaaf0 | 2011-03-30 10:35:12 -0700 | [diff] [blame] | 25 | |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 26 | // Given raw |signatures|, packs them into a protobuf and serializes it into a |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 27 | // binary blob. Returns true on success, false otherwise. |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 28 | bool ConvertSignatureToProtobufBlob(const vector<vector<char> >& signatures, |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 29 | vector<char>* out_signature_blob) { |
| 30 | // Pack it into a protobuf |
| 31 | Signatures out_message; |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 32 | uint32_t version = kSignatureMessageOriginalVersion; |
| 33 | LOG_IF(WARNING, kSignatureMessageCurrentVersion - |
| 34 | kSignatureMessageOriginalVersion + 1 < signatures.size()) |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 35 | << "You may want to support clients in the range [" |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 36 | << 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 Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 46 | |
| 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 Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 59 | // manifest. It populates |out_metadata_size| with the size of the final |
Don Garrett | 2ae3787 | 2013-10-25 13:33:20 -0700 | [diff] [blame] | 60 | // 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 Petkov | adb3cef | 2011-01-13 16:16:08 -0800 | [diff] [blame] | 63 | bool AddSignatureOpToPayload(const string& payload_path, |
Don Garrett | 2ae3787 | 2013-10-25 13:33:20 -0700 | [diff] [blame] | 64 | uint64_t signature_blob_size, |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 65 | vector<char>* out_payload, |
Don Garrett | 2ae3787 | 2013-10-25 13:33:20 -0700 | [diff] [blame] | 66 | uint64_t* out_metadata_size, |
| 67 | uint64_t* out_signatures_offset) { |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 68 | const int kProtobufOffset = 20; |
| 69 | const int kProtobufSizeOffset = 12; |
| 70 | |
Darin Petkov | adb3cef | 2011-01-13 16:16:08 -0800 | [diff] [blame] | 71 | // Loads the payload. |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 72 | vector<char> payload; |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 73 | DeltaArchiveManifest manifest; |
Darin Petkov | adb3cef | 2011-01-13 16:16:08 -0800 | [diff] [blame] | 74 | uint64_t metadata_size; |
Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame^] | 75 | TEST_AND_RETURN_FALSE(PayloadVerifier::LoadPayload( |
Darin Petkov | adb3cef | 2011-01-13 16:16:08 -0800 | [diff] [blame] | 76 | payload_path, &payload, &manifest, &metadata_size)); |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 77 | |
Don Garrett | 2ae3787 | 2013-10-25 13:33:20 -0700 | [diff] [blame] | 78 | // Is there already a signature op in place? |
| 79 | if (manifest.has_signatures_size()) { |
Don Garrett | 2ae3787 | 2013-10-25 13:33:20 -0700 | [diff] [blame] | 80 | // 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 Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 90 | |
Don Garrett | 2ae3787 | 2013-10-25 13:33:20 -0700 | [diff] [blame] | 91 | 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 Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 117 | out_payload->swap(payload); |
Don Garrett | 2ae3787 | 2013-10-25 13:33:20 -0700 | [diff] [blame] | 118 | *out_metadata_size = metadata_size; |
| 119 | *out_signatures_offset = metadata_size + manifest.signatures_offset(); |
| 120 | LOG(INFO) << "Signature Blob Offset: " << *out_signatures_offset; |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 121 | return true; |
| 122 | } |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 123 | } // namespace |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 124 | |
| 125 | bool PayloadSigner::SignHash(const vector<char>& hash, |
| 126 | const string& private_key_path, |
| 127 | vector<char>* out_signature) { |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 128 | LOG(INFO) << "Signing hash with private key: " << private_key_path; |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 129 | string sig_path; |
| 130 | TEST_AND_RETURN_FALSE( |
Gilad Arnold | a6742b3 | 2014-01-11 00:18:34 -0800 | [diff] [blame] | 131 | utils::MakeTempFile("signature.XXXXXX", &sig_path, NULL)); |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 132 | ScopedPathUnlinker sig_path_unlinker(sig_path); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 133 | |
| 134 | string hash_path; |
| 135 | TEST_AND_RETURN_FALSE( |
Gilad Arnold | a6742b3 | 2014-01-11 00:18:34 -0800 | [diff] [blame] | 136 | utils::MakeTempFile("hash.XXXXXX", &hash_path, NULL)); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 137 | ScopedPathUnlinker hash_path_unlinker(hash_path); |
Andrew de los Reyes | bdfaaf0 | 2011-03-30 10:35:12 -0700 | [diff] [blame] | 138 | // We expect unpadded SHA256 hash coming in |
| 139 | TEST_AND_RETURN_FALSE(hash.size() == 32); |
| 140 | vector<char> padded_hash(hash); |
Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame^] | 141 | PayloadVerifier::PadRSA2048SHA256Hash(&padded_hash); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 142 | TEST_AND_RETURN_FALSE(utils::WriteFile(hash_path.c_str(), |
Andrew de los Reyes | bdfaaf0 | 2011-03-30 10:35:12 -0700 | [diff] [blame] | 143 | padded_hash.data(), |
| 144 | padded_hash.size())); |
Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 145 | |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 146 | // 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 Frysinger | 2149be4 | 2012-03-12 19:23:47 -0400 | [diff] [blame] | 149 | base::SplitString("openssl rsautl -raw -sign -inkey x -in x -out x", |
Chris Masone | d903c3b | 2011-05-12 15:35:46 -0700 | [diff] [blame] | 150 | ' ', |
| 151 | &cmd); |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 152 | cmd[cmd.size() - 5] = private_key_path; |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 153 | cmd[cmd.size() - 3] = hash_path; |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 154 | cmd[cmd.size() - 1] = sig_path; |
Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 155 | |
Alex Deymo | 719bfff | 2014-07-11 12:12:32 -0700 | [diff] [blame] | 156 | // 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 Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 160 | int return_code = 0; |
Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 161 | TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &return_code, NULL)); |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 162 | TEST_AND_RETURN_FALSE(return_code == 0); |
Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 163 | |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 164 | vector<char> signature; |
| 165 | TEST_AND_RETURN_FALSE(utils::ReadFile(sig_path, &signature)); |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 166 | out_signature->swap(signature); |
| 167 | return true; |
| 168 | } |
Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 169 | |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 170 | bool PayloadSigner::SignPayload(const string& unsigned_payload_path, |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 171 | const vector<string>& private_key_paths, |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 172 | 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 Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 177 | |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 178 | 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 Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 186 | out_signature_blob)); |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 187 | return true; |
| 188 | } |
| 189 | |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 190 | bool PayloadSigner::SignatureBlobLength(const vector<string>& private_key_paths, |
| 191 | uint64_t* out_length) { |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 192 | DCHECK(out_length); |
Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 193 | |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 194 | string x_path; |
| 195 | TEST_AND_RETURN_FALSE( |
Gilad Arnold | a6742b3 | 2014-01-11 00:18:34 -0800 | [diff] [blame] | 196 | utils::MakeTempFile("signed_data.XXXXXX", &x_path, NULL)); |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 197 | 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 Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 202 | private_key_paths, |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 203 | &sig_blob)); |
| 204 | *out_length = sig_blob.size(); |
| 205 | return true; |
| 206 | } |
| 207 | |
Don Garrett | c2e9f5f | 2013-10-18 16:42:40 -0700 | [diff] [blame] | 208 | bool 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 Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 214 | // 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 Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 217 | 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 Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 223 | vector<char> signature_blob; |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 224 | TEST_AND_RETURN_FALSE(ConvertSignatureToProtobufBlob(signatures, |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 225 | &signature_blob)); |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 226 | TEST_AND_RETURN_FALSE(AddSignatureOpToPayload(payload_path, |
| 227 | signature_blob.size(), |
Don Garrett | c2e9f5f | 2013-10-18 16:42:40 -0700 | [diff] [blame] | 228 | payload_out, |
| 229 | metadata_size_out, |
| 230 | signatures_offset_out)); |
Don Garrett | 2ae3787 | 2013-10-25 13:33:20 -0700 | [diff] [blame] | 231 | |
Don Garrett | c2e9f5f | 2013-10-18 16:42:40 -0700 | [diff] [blame] | 232 | return true; |
| 233 | } |
| 234 | |
| 235 | bool 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 Garrett | 2ae3787 | 2013-10-25 13:33:20 -0700 | [diff] [blame] | 247 | |
| 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 Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 253 | return true; |
| 254 | } |
| 255 | |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 256 | bool PayloadSigner::HashMetadataForSigning(const string& payload_path, |
Don Garrett | c2e9f5f | 2013-10-18 16:42:40 -0700 | [diff] [blame] | 257 | const vector<int>& signature_sizes, |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 258 | vector<char>* out_metadata_hash) { |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 259 | vector<char> payload; |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 260 | uint64_t metadata_size; |
Don Garrett | c2e9f5f | 2013-10-18 16:42:40 -0700 | [diff] [blame] | 261 | 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 Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 268 | |
| 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 Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 276 | bool PayloadSigner::AddSignatureToPayload( |
| 277 | const string& payload_path, |
| 278 | const vector<vector<char> >& signatures, |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 279 | const string& signed_payload_path, |
| 280 | uint64_t *out_metadata_size) { |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 281 | // 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 Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 285 | TEST_AND_RETURN_FALSE(ConvertSignatureToProtobufBlob(signatures, |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 286 | &signature_blob)); |
| 287 | vector<char> payload; |
Don Garrett | 2ae3787 | 2013-10-25 13:33:20 -0700 | [diff] [blame] | 288 | uint64_t signatures_offset; |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 289 | TEST_AND_RETURN_FALSE(AddSignatureOpToPayload(payload_path, |
| 290 | signature_blob.size(), |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 291 | &payload, |
Don Garrett | 2ae3787 | 2013-10-25 13:33:20 -0700 | [diff] [blame] | 292 | out_metadata_size, |
| 293 | &signatures_offset)); |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 294 | // Appends the signature blob to the end of the payload and writes the new |
| 295 | // payload. |
Don Garrett | 2ae3787 | 2013-10-25 13:33:20 -0700 | [diff] [blame] | 296 | 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 Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 301 | 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 Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 308 | bool PayloadSigner::GetMetadataSignature(const char* const metadata, |
| 309 | size_t metadata_size, |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 310 | 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 Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 314 | vector<char> metadata_hash; |
| 315 | TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfBytes(metadata, |
| 316 | metadata_size, |
| 317 | &metadata_hash)); |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 318 | |
| 319 | vector<char> signature; |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 320 | TEST_AND_RETURN_FALSE(SignHash(metadata_hash, |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 321 | 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 Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 331 | } // namespace chromeos_update_engine |