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> |
Alex Vakulenko | 981a9fb | 2015-02-09 12:51:24 -0800 | [diff] [blame] | 10 | #include <chromeos/data_encoding.h> |
Darin Petkov | b039d50 | 2010-12-03 09:08:04 -0800 | [diff] [blame] | 11 | #include <openssl/pem.h> |
| 12 | |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 13 | #include "update_engine/omaha_hash_calculator.h" |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 14 | #include "update_engine/payload_generator/payload_file.h" |
Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 15 | #include "update_engine/payload_verifier.h" |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 16 | #include "update_engine/subprocess.h" |
| 17 | #include "update_engine/update_metadata.pb.h" |
| 18 | #include "update_engine/utils.h" |
| 19 | |
| 20 | using std::string; |
| 21 | using std::vector; |
| 22 | |
| 23 | namespace chromeos_update_engine { |
| 24 | |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 25 | namespace { |
Andrew de los Reyes | bdfaaf0 | 2011-03-30 10:35:12 -0700 | [diff] [blame] | 26 | |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 27 | // 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] | 28 | // binary blob. Returns true on success, false otherwise. |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 29 | bool ConvertSignatureToProtobufBlob(const vector<chromeos::Blob>& signatures, |
| 30 | chromeos::Blob* out_signature_blob) { |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 31 | // Pack it into a protobuf |
| 32 | Signatures out_message; |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 33 | uint32_t version = kSignatureMessageOriginalVersion; |
| 34 | LOG_IF(WARNING, kSignatureMessageCurrentVersion - |
| 35 | kSignatureMessageOriginalVersion + 1 < signatures.size()) |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 36 | << "You may want to support clients in the range [" |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 37 | << kSignatureMessageOriginalVersion << ", " |
| 38 | << kSignatureMessageCurrentVersion << "] inclusive, but you only " |
| 39 | << "provided " << signatures.size() << " signatures."; |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 40 | for (const chromeos::Blob& signature : signatures) { |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 41 | Signatures_Signature* sig_message = out_message.add_signatures(); |
| 42 | sig_message->set_version(version++); |
| 43 | sig_message->set_data(signature.data(), signature.size()); |
| 44 | } |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 45 | |
| 46 | // Serialize protobuf |
| 47 | string serialized; |
| 48 | TEST_AND_RETURN_FALSE(out_message.AppendToString(&serialized)); |
| 49 | out_signature_blob->insert(out_signature_blob->end(), |
| 50 | serialized.begin(), |
| 51 | serialized.end()); |
| 52 | LOG(INFO) << "Signature blob size: " << out_signature_blob->size(); |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | // Given an unsigned payload under |payload_path| and the |signature_blob_size| |
| 57 | // generates an updated payload that includes a dummy signature op in its |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 58 | // manifest. It populates |out_metadata_size| with the size of the final |
Don Garrett | 2ae3787 | 2013-10-25 13:33:20 -0700 | [diff] [blame] | 59 | // manifest after adding the dummy signature operation, and |
| 60 | // |out_signatures_offset| with the expected offset for the new blob. Returns |
| 61 | // true on success, false otherwise. |
Darin Petkov | adb3cef | 2011-01-13 16:16:08 -0800 | [diff] [blame] | 62 | bool AddSignatureOpToPayload(const string& payload_path, |
Don Garrett | 2ae3787 | 2013-10-25 13:33:20 -0700 | [diff] [blame] | 63 | uint64_t signature_blob_size, |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 64 | chromeos::Blob* out_payload, |
Don Garrett | 2ae3787 | 2013-10-25 13:33:20 -0700 | [diff] [blame] | 65 | uint64_t* out_metadata_size, |
| 66 | uint64_t* out_signatures_offset) { |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 67 | const int kProtobufOffset = 20; |
| 68 | const int kProtobufSizeOffset = 12; |
| 69 | |
Darin Petkov | adb3cef | 2011-01-13 16:16:08 -0800 | [diff] [blame] | 70 | // Loads the payload. |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 71 | chromeos::Blob payload; |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 72 | DeltaArchiveManifest manifest; |
Darin Petkov | adb3cef | 2011-01-13 16:16:08 -0800 | [diff] [blame] | 73 | uint64_t metadata_size; |
Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 74 | TEST_AND_RETURN_FALSE(PayloadVerifier::LoadPayload( |
Darin Petkov | adb3cef | 2011-01-13 16:16:08 -0800 | [diff] [blame] | 75 | payload_path, &payload, &manifest, &metadata_size)); |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 76 | |
Don Garrett | 2ae3787 | 2013-10-25 13:33:20 -0700 | [diff] [blame] | 77 | // Is there already a signature op in place? |
| 78 | if (manifest.has_signatures_size()) { |
Don Garrett | 2ae3787 | 2013-10-25 13:33:20 -0700 | [diff] [blame] | 79 | // The signature op is tied to the size of the signature blob, but not it's |
| 80 | // contents. We don't allow the manifest to change if there is already an op |
| 81 | // present, because that might invalidate previously generated |
| 82 | // hashes/signatures. |
| 83 | if (manifest.signatures_size() != signature_blob_size) { |
| 84 | LOG(ERROR) << "Attempt to insert different signature sized blob. " |
| 85 | << "(current:" << manifest.signatures_size() |
| 86 | << "new:" << signature_blob_size << ")"; |
| 87 | return false; |
| 88 | } |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 89 | |
Don Garrett | 2ae3787 | 2013-10-25 13:33:20 -0700 | [diff] [blame] | 90 | LOG(INFO) << "Matching signature sizes already present."; |
| 91 | } else { |
| 92 | // Updates the manifest to include the signature operation. |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 93 | AddSignatureOp(payload.size() - metadata_size, |
| 94 | signature_blob_size, |
| 95 | &manifest); |
Don Garrett | 2ae3787 | 2013-10-25 13:33:20 -0700 | [diff] [blame] | 96 | |
| 97 | // Updates the payload to include the new manifest. |
| 98 | string serialized_manifest; |
| 99 | TEST_AND_RETURN_FALSE(manifest.AppendToString(&serialized_manifest)); |
| 100 | LOG(INFO) << "Updated protobuf size: " << serialized_manifest.size(); |
| 101 | payload.erase(payload.begin() + kProtobufOffset, |
| 102 | payload.begin() + metadata_size); |
| 103 | payload.insert(payload.begin() + kProtobufOffset, |
| 104 | serialized_manifest.begin(), |
| 105 | serialized_manifest.end()); |
| 106 | |
| 107 | // Updates the protobuf size. |
| 108 | uint64_t size_be = htobe64(serialized_manifest.size()); |
| 109 | memcpy(&payload[kProtobufSizeOffset], &size_be, sizeof(size_be)); |
| 110 | metadata_size = serialized_manifest.size() + kProtobufOffset; |
| 111 | |
| 112 | LOG(INFO) << "Updated payload size: " << payload.size(); |
| 113 | LOG(INFO) << "Updated metadata size: " << metadata_size; |
| 114 | } |
| 115 | |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 116 | out_payload->swap(payload); |
Don Garrett | 2ae3787 | 2013-10-25 13:33:20 -0700 | [diff] [blame] | 117 | *out_metadata_size = metadata_size; |
| 118 | *out_signatures_offset = metadata_size + manifest.signatures_offset(); |
| 119 | LOG(INFO) << "Signature Blob Offset: " << *out_signatures_offset; |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 120 | return true; |
| 121 | } |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 122 | } // namespace |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 123 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 124 | bool PayloadSigner::SignHash(const chromeos::Blob& hash, |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 125 | const string& private_key_path, |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 126 | chromeos::Blob* out_signature) { |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 127 | LOG(INFO) << "Signing hash with private key: " << private_key_path; |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 128 | string sig_path; |
| 129 | TEST_AND_RETURN_FALSE( |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 130 | utils::MakeTempFile("signature.XXXXXX", &sig_path, nullptr)); |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 131 | ScopedPathUnlinker sig_path_unlinker(sig_path); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 132 | |
| 133 | string hash_path; |
| 134 | TEST_AND_RETURN_FALSE( |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 135 | utils::MakeTempFile("hash.XXXXXX", &hash_path, nullptr)); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 136 | ScopedPathUnlinker hash_path_unlinker(hash_path); |
Andrew de los Reyes | bdfaaf0 | 2011-03-30 10:35:12 -0700 | [diff] [blame] | 137 | // We expect unpadded SHA256 hash coming in |
| 138 | TEST_AND_RETURN_FALSE(hash.size() == 32); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 139 | chromeos::Blob padded_hash(hash); |
Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 140 | PayloadVerifier::PadRSA2048SHA256Hash(&padded_hash); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 141 | TEST_AND_RETURN_FALSE(utils::WriteFile(hash_path.c_str(), |
Andrew de los Reyes | bdfaaf0 | 2011-03-30 10:35:12 -0700 | [diff] [blame] | 142 | padded_hash.data(), |
| 143 | padded_hash.size())); |
Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 144 | |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 145 | // This runs on the server, so it's okay to cop out and call openssl |
| 146 | // executable rather than properly use the library |
| 147 | vector<string> cmd; |
Mike Frysinger | 2149be4 | 2012-03-12 19:23:47 -0400 | [diff] [blame] | 148 | base::SplitString("openssl rsautl -raw -sign -inkey x -in x -out x", |
Chris Masone | d903c3b | 2011-05-12 15:35:46 -0700 | [diff] [blame] | 149 | ' ', |
| 150 | &cmd); |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 151 | cmd[cmd.size() - 5] = private_key_path; |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 152 | cmd[cmd.size() - 3] = hash_path; |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 153 | cmd[cmd.size() - 1] = sig_path; |
Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 154 | |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 155 | int return_code = 0; |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 156 | TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &return_code, |
| 157 | nullptr)); |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 158 | TEST_AND_RETURN_FALSE(return_code == 0); |
Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 159 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 160 | chromeos::Blob signature; |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 161 | TEST_AND_RETURN_FALSE(utils::ReadFile(sig_path, &signature)); |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 162 | out_signature->swap(signature); |
| 163 | return true; |
| 164 | } |
Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 165 | |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 166 | bool PayloadSigner::SignPayload(const string& unsigned_payload_path, |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 167 | const vector<string>& private_key_paths, |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 168 | chromeos::Blob* out_signature_blob) { |
| 169 | chromeos::Blob hash_data; |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 170 | TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfFile( |
| 171 | unsigned_payload_path, -1, &hash_data) == |
| 172 | utils::FileSize(unsigned_payload_path)); |
Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 173 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 174 | vector<chromeos::Blob> signatures; |
Alex Deymo | 020600d | 2014-11-05 21:05:55 -0800 | [diff] [blame] | 175 | for (const string& path : private_key_paths) { |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 176 | chromeos::Blob signature; |
Alex Deymo | 020600d | 2014-11-05 21:05:55 -0800 | [diff] [blame] | 177 | TEST_AND_RETURN_FALSE(SignHash(hash_data, path, &signature)); |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 178 | signatures.push_back(signature); |
| 179 | } |
| 180 | TEST_AND_RETURN_FALSE(ConvertSignatureToProtobufBlob(signatures, |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 181 | out_signature_blob)); |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 182 | return true; |
| 183 | } |
| 184 | |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 185 | bool PayloadSigner::SignatureBlobLength(const vector<string>& private_key_paths, |
| 186 | uint64_t* out_length) { |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 187 | DCHECK(out_length); |
Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 188 | |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 189 | string x_path; |
| 190 | TEST_AND_RETURN_FALSE( |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 191 | utils::MakeTempFile("signed_data.XXXXXX", &x_path, nullptr)); |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 192 | ScopedPathUnlinker x_path_unlinker(x_path); |
| 193 | TEST_AND_RETURN_FALSE(utils::WriteFile(x_path.c_str(), "x", 1)); |
| 194 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 195 | chromeos::Blob sig_blob; |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 196 | TEST_AND_RETURN_FALSE(PayloadSigner::SignPayload(x_path, |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 197 | private_key_paths, |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 198 | &sig_blob)); |
| 199 | *out_length = sig_blob.size(); |
| 200 | return true; |
| 201 | } |
| 202 | |
Don Garrett | c2e9f5f | 2013-10-18 16:42:40 -0700 | [diff] [blame] | 203 | bool PayloadSigner::PrepPayloadForHashing( |
| 204 | const string& payload_path, |
| 205 | const vector<int>& signature_sizes, |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 206 | chromeos::Blob* payload_out, |
Don Garrett | c2e9f5f | 2013-10-18 16:42:40 -0700 | [diff] [blame] | 207 | uint64_t* metadata_size_out, |
| 208 | uint64_t* signatures_offset_out) { |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 209 | // TODO(petkov): Reduce memory usage -- the payload is manipulated in memory. |
| 210 | |
| 211 | // Loads the payload and adds the signature op to it. |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 212 | vector<chromeos::Blob> signatures; |
Alex Deymo | 020600d | 2014-11-05 21:05:55 -0800 | [diff] [blame] | 213 | for (int signature_size : signature_sizes) { |
| 214 | signatures.emplace_back(signature_size, 0); |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 215 | } |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 216 | chromeos::Blob signature_blob; |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 217 | TEST_AND_RETURN_FALSE(ConvertSignatureToProtobufBlob(signatures, |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 218 | &signature_blob)); |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 219 | TEST_AND_RETURN_FALSE(AddSignatureOpToPayload(payload_path, |
| 220 | signature_blob.size(), |
Don Garrett | c2e9f5f | 2013-10-18 16:42:40 -0700 | [diff] [blame] | 221 | payload_out, |
| 222 | metadata_size_out, |
| 223 | signatures_offset_out)); |
Don Garrett | 2ae3787 | 2013-10-25 13:33:20 -0700 | [diff] [blame] | 224 | |
Don Garrett | c2e9f5f | 2013-10-18 16:42:40 -0700 | [diff] [blame] | 225 | return true; |
| 226 | } |
| 227 | |
| 228 | bool PayloadSigner::HashPayloadForSigning(const string& payload_path, |
| 229 | const vector<int>& signature_sizes, |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 230 | chromeos::Blob* out_hash_data) { |
| 231 | chromeos::Blob payload; |
Don Garrett | c2e9f5f | 2013-10-18 16:42:40 -0700 | [diff] [blame] | 232 | uint64_t metadata_size; |
| 233 | uint64_t signatures_offset; |
| 234 | |
| 235 | TEST_AND_RETURN_FALSE(PrepPayloadForHashing(payload_path, |
| 236 | signature_sizes, |
| 237 | &payload, |
| 238 | &metadata_size, |
| 239 | &signatures_offset)); |
Don Garrett | 2ae3787 | 2013-10-25 13:33:20 -0700 | [diff] [blame] | 240 | |
| 241 | // Calculates the hash on the updated payload. Note that we stop calculating |
| 242 | // before we reach the signature information. |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 243 | TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfBytes(payload.data(), |
Don Garrett | 2ae3787 | 2013-10-25 13:33:20 -0700 | [diff] [blame] | 244 | signatures_offset, |
| 245 | out_hash_data)); |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 246 | return true; |
| 247 | } |
| 248 | |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 249 | bool PayloadSigner::HashMetadataForSigning(const string& payload_path, |
Don Garrett | c2e9f5f | 2013-10-18 16:42:40 -0700 | [diff] [blame] | 250 | const vector<int>& signature_sizes, |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 251 | chromeos::Blob* out_metadata_hash) { |
| 252 | chromeos::Blob payload; |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 253 | uint64_t metadata_size; |
Don Garrett | c2e9f5f | 2013-10-18 16:42:40 -0700 | [diff] [blame] | 254 | uint64_t signatures_offset; |
| 255 | |
| 256 | TEST_AND_RETURN_FALSE(PrepPayloadForHashing(payload_path, |
| 257 | signature_sizes, |
| 258 | &payload, |
| 259 | &metadata_size, |
| 260 | &signatures_offset)); |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 261 | |
| 262 | // Calculates the hash on the manifest. |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 263 | TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfBytes(payload.data(), |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 264 | metadata_size, |
| 265 | out_metadata_hash)); |
| 266 | return true; |
| 267 | } |
| 268 | |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 269 | bool PayloadSigner::AddSignatureToPayload( |
| 270 | const string& payload_path, |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 271 | const vector<chromeos::Blob>& signatures, |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 272 | const string& signed_payload_path, |
| 273 | uint64_t *out_metadata_size) { |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 274 | // TODO(petkov): Reduce memory usage -- the payload is manipulated in memory. |
| 275 | |
| 276 | // Loads the payload and adds the signature op to it. |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 277 | chromeos::Blob signature_blob; |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 278 | TEST_AND_RETURN_FALSE(ConvertSignatureToProtobufBlob(signatures, |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 279 | &signature_blob)); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 280 | chromeos::Blob payload; |
Don Garrett | 2ae3787 | 2013-10-25 13:33:20 -0700 | [diff] [blame] | 281 | uint64_t signatures_offset; |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 282 | TEST_AND_RETURN_FALSE(AddSignatureOpToPayload(payload_path, |
| 283 | signature_blob.size(), |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 284 | &payload, |
Don Garrett | 2ae3787 | 2013-10-25 13:33:20 -0700 | [diff] [blame] | 285 | out_metadata_size, |
| 286 | &signatures_offset)); |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 287 | // Appends the signature blob to the end of the payload and writes the new |
| 288 | // payload. |
Don Garrett | 2ae3787 | 2013-10-25 13:33:20 -0700 | [diff] [blame] | 289 | LOG(INFO) << "Payload size before signatures: " << payload.size(); |
| 290 | payload.resize(signatures_offset); |
| 291 | payload.insert(payload.begin() + signatures_offset, |
| 292 | signature_blob.begin(), |
| 293 | signature_blob.end()); |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 294 | LOG(INFO) << "Signed payload size: " << payload.size(); |
| 295 | TEST_AND_RETURN_FALSE(utils::WriteFile(signed_payload_path.c_str(), |
| 296 | payload.data(), |
| 297 | payload.size())); |
| 298 | return true; |
| 299 | } |
| 300 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 301 | bool PayloadSigner::GetMetadataSignature(const void* const metadata, |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 302 | size_t metadata_size, |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 303 | const string& private_key_path, |
| 304 | string* out_signature) { |
| 305 | // Calculates the hash on the updated payload. Note that the payload includes |
| 306 | // the signature op but doesn't include the signature blob at the end. |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 307 | chromeos::Blob metadata_hash; |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 308 | TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfBytes(metadata, |
| 309 | metadata_size, |
| 310 | &metadata_hash)); |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 311 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 312 | chromeos::Blob signature; |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 313 | TEST_AND_RETURN_FALSE(SignHash(metadata_hash, |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 314 | private_key_path, |
| 315 | &signature)); |
| 316 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 317 | *out_signature = chromeos::data_encoding::Base64Encode(signature); |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 318 | return true; |
| 319 | } |
| 320 | |
| 321 | |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 322 | } // namespace chromeos_update_engine |