| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // | 
|  | 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 Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 16 |  | 
| Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 17 | #include "update_engine/payload_generator/payload_signer.h" | 
| Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 18 |  | 
| Alex Deymo | 6f20dd4 | 2015-08-18 16:42:46 -0700 | [diff] [blame] | 19 | #include <endian.h> | 
|  | 20 |  | 
| Darin Petkov | b039d50 | 2010-12-03 09:08:04 -0800 | [diff] [blame] | 21 | #include <base/logging.h> | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 22 | #include <base/strings/string_split.h> | 
|  | 23 | #include <base/strings/string_util.h> | 
| Alex Vakulenko | 981a9fb | 2015-02-09 12:51:24 -0800 | [diff] [blame] | 24 | #include <chromeos/data_encoding.h> | 
| Darin Petkov | b039d50 | 2010-12-03 09:08:04 -0800 | [diff] [blame] | 25 | #include <openssl/pem.h> | 
|  | 26 |  | 
| Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 27 | #include "update_engine/omaha_hash_calculator.h" | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 28 | #include "update_engine/payload_generator/payload_file.h" | 
| Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 29 | #include "update_engine/payload_verifier.h" | 
| Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 30 | #include "update_engine/subprocess.h" | 
|  | 31 | #include "update_engine/update_metadata.pb.h" | 
|  | 32 | #include "update_engine/utils.h" | 
|  | 33 |  | 
|  | 34 | using std::string; | 
|  | 35 | using std::vector; | 
|  | 36 |  | 
|  | 37 | namespace chromeos_update_engine { | 
|  | 38 |  | 
| Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 39 | namespace { | 
| Andrew de los Reyes | bdfaaf0 | 2011-03-30 10:35:12 -0700 | [diff] [blame] | 40 |  | 
| Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 41 | // 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] | 42 | // binary blob. Returns true on success, false otherwise. | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 43 | bool ConvertSignatureToProtobufBlob(const vector<chromeos::Blob>& signatures, | 
|  | 44 | chromeos::Blob* out_signature_blob) { | 
| Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 45 | // Pack it into a protobuf | 
|  | 46 | Signatures out_message; | 
| Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 47 | uint32_t version = kSignatureMessageOriginalVersion; | 
|  | 48 | LOG_IF(WARNING, kSignatureMessageCurrentVersion - | 
|  | 49 | kSignatureMessageOriginalVersion + 1 < signatures.size()) | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 50 | << "You may want to support clients in the range [" | 
| Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 51 | << kSignatureMessageOriginalVersion << ", " | 
|  | 52 | << kSignatureMessageCurrentVersion << "] inclusive, but you only " | 
|  | 53 | << "provided " << signatures.size() << " signatures."; | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 54 | for (const chromeos::Blob& signature : signatures) { | 
| Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 55 | 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 Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 59 |  | 
|  | 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 Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 72 | // manifest. It populates |out_metadata_size| with the size of the final | 
| Don Garrett | 2ae3787 | 2013-10-25 13:33:20 -0700 | [diff] [blame] | 73 | // 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 Petkov | adb3cef | 2011-01-13 16:16:08 -0800 | [diff] [blame] | 76 | bool AddSignatureOpToPayload(const string& payload_path, | 
| Don Garrett | 2ae3787 | 2013-10-25 13:33:20 -0700 | [diff] [blame] | 77 | uint64_t signature_blob_size, | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 78 | chromeos::Blob* out_payload, | 
| Don Garrett | 2ae3787 | 2013-10-25 13:33:20 -0700 | [diff] [blame] | 79 | uint64_t* out_metadata_size, | 
|  | 80 | uint64_t* out_signatures_offset) { | 
| Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 81 | const int kProtobufOffset = 20; | 
|  | 82 | const int kProtobufSizeOffset = 12; | 
|  | 83 |  | 
| Darin Petkov | adb3cef | 2011-01-13 16:16:08 -0800 | [diff] [blame] | 84 | // Loads the payload. | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 85 | chromeos::Blob payload; | 
| Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 86 | DeltaArchiveManifest manifest; | 
| Darin Petkov | adb3cef | 2011-01-13 16:16:08 -0800 | [diff] [blame] | 87 | uint64_t metadata_size; | 
| Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 88 | TEST_AND_RETURN_FALSE(PayloadVerifier::LoadPayload( | 
| Darin Petkov | adb3cef | 2011-01-13 16:16:08 -0800 | [diff] [blame] | 89 | payload_path, &payload, &manifest, &metadata_size)); | 
| 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 | // Is there already a signature op in place? | 
|  | 92 | if (manifest.has_signatures_size()) { | 
| Don Garrett | 2ae3787 | 2013-10-25 13:33:20 -0700 | [diff] [blame] | 93 | // 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 Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 103 |  | 
| Don Garrett | 2ae3787 | 2013-10-25 13:33:20 -0700 | [diff] [blame] | 104 | LOG(INFO) << "Matching signature sizes already present."; | 
|  | 105 | } else { | 
|  | 106 | // Updates the manifest to include the signature operation. | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 107 | AddSignatureOp(payload.size() - metadata_size, | 
|  | 108 | signature_blob_size, | 
|  | 109 | &manifest); | 
| Don Garrett | 2ae3787 | 2013-10-25 13:33:20 -0700 | [diff] [blame] | 110 |  | 
|  | 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 Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 130 | out_payload->swap(payload); | 
| Don Garrett | 2ae3787 | 2013-10-25 13:33:20 -0700 | [diff] [blame] | 131 | *out_metadata_size = metadata_size; | 
|  | 132 | *out_signatures_offset = metadata_size + manifest.signatures_offset(); | 
|  | 133 | LOG(INFO) << "Signature Blob Offset: " << *out_signatures_offset; | 
| Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 134 | return true; | 
|  | 135 | } | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 136 | }  // namespace | 
| Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 137 |  | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 138 | bool PayloadSigner::SignHash(const chromeos::Blob& hash, | 
| Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 139 | const string& private_key_path, | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 140 | chromeos::Blob* out_signature) { | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 141 | LOG(INFO) << "Signing hash with private key: " << private_key_path; | 
| Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 142 | string sig_path; | 
|  | 143 | TEST_AND_RETURN_FALSE( | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 144 | utils::MakeTempFile("signature.XXXXXX", &sig_path, nullptr)); | 
| Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 145 | ScopedPathUnlinker sig_path_unlinker(sig_path); | 
| Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 146 |  | 
|  | 147 | string hash_path; | 
|  | 148 | TEST_AND_RETURN_FALSE( | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 149 | utils::MakeTempFile("hash.XXXXXX", &hash_path, nullptr)); | 
| Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 150 | ScopedPathUnlinker hash_path_unlinker(hash_path); | 
| Andrew de los Reyes | bdfaaf0 | 2011-03-30 10:35:12 -0700 | [diff] [blame] | 151 | // We expect unpadded SHA256 hash coming in | 
|  | 152 | TEST_AND_RETURN_FALSE(hash.size() == 32); | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 153 | chromeos::Blob padded_hash(hash); | 
| Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 154 | PayloadVerifier::PadRSA2048SHA256Hash(&padded_hash); | 
| Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 155 | TEST_AND_RETURN_FALSE(utils::WriteFile(hash_path.c_str(), | 
| Andrew de los Reyes | bdfaaf0 | 2011-03-30 10:35:12 -0700 | [diff] [blame] | 156 | padded_hash.data(), | 
|  | 157 | padded_hash.size())); | 
| Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 158 |  | 
| Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 159 | // 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 Frysinger | 2149be4 | 2012-03-12 19:23:47 -0400 | [diff] [blame] | 162 | base::SplitString("openssl rsautl -raw -sign -inkey x -in x -out x", | 
| Chris Masone | d903c3b | 2011-05-12 15:35:46 -0700 | [diff] [blame] | 163 | ' ', | 
|  | 164 | &cmd); | 
| Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 165 | cmd[cmd.size() - 5] = private_key_path; | 
| Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 166 | cmd[cmd.size() - 3] = hash_path; | 
| Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 167 | cmd[cmd.size() - 1] = sig_path; | 
| Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 168 |  | 
| Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 169 | int return_code = 0; | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 170 | TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &return_code, | 
|  | 171 | nullptr)); | 
| Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 172 | TEST_AND_RETURN_FALSE(return_code == 0); | 
| 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 | chromeos::Blob signature; | 
| Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 175 | TEST_AND_RETURN_FALSE(utils::ReadFile(sig_path, &signature)); | 
| Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 176 | out_signature->swap(signature); | 
|  | 177 | return true; | 
|  | 178 | } | 
| Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 179 |  | 
| Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 180 | bool PayloadSigner::SignPayload(const string& unsigned_payload_path, | 
| Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 181 | const vector<string>& private_key_paths, | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 182 | chromeos::Blob* out_signature_blob) { | 
|  | 183 | chromeos::Blob hash_data; | 
| Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 184 | TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfFile( | 
|  | 185 | unsigned_payload_path, -1, &hash_data) == | 
|  | 186 | utils::FileSize(unsigned_payload_path)); | 
| Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 187 |  | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 188 | vector<chromeos::Blob> signatures; | 
| Alex Deymo | 020600d | 2014-11-05 21:05:55 -0800 | [diff] [blame] | 189 | for (const string& path : private_key_paths) { | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 190 | chromeos::Blob signature; | 
| Alex Deymo | 020600d | 2014-11-05 21:05:55 -0800 | [diff] [blame] | 191 | TEST_AND_RETURN_FALSE(SignHash(hash_data, path, &signature)); | 
| Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 192 | signatures.push_back(signature); | 
|  | 193 | } | 
|  | 194 | TEST_AND_RETURN_FALSE(ConvertSignatureToProtobufBlob(signatures, | 
| Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 195 | out_signature_blob)); | 
| Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 196 | return true; | 
|  | 197 | } | 
|  | 198 |  | 
| Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 199 | bool PayloadSigner::SignatureBlobLength(const vector<string>& private_key_paths, | 
|  | 200 | uint64_t* out_length) { | 
| Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 201 | DCHECK(out_length); | 
| Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 202 |  | 
| Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 203 | string x_path; | 
|  | 204 | TEST_AND_RETURN_FALSE( | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 205 | utils::MakeTempFile("signed_data.XXXXXX", &x_path, nullptr)); | 
| Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 206 | ScopedPathUnlinker x_path_unlinker(x_path); | 
|  | 207 | TEST_AND_RETURN_FALSE(utils::WriteFile(x_path.c_str(), "x", 1)); | 
|  | 208 |  | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 209 | chromeos::Blob sig_blob; | 
| Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 210 | TEST_AND_RETURN_FALSE(PayloadSigner::SignPayload(x_path, | 
| Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 211 | private_key_paths, | 
| Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 212 | &sig_blob)); | 
|  | 213 | *out_length = sig_blob.size(); | 
|  | 214 | return true; | 
|  | 215 | } | 
|  | 216 |  | 
| Don Garrett | c2e9f5f | 2013-10-18 16:42:40 -0700 | [diff] [blame] | 217 | bool PayloadSigner::PrepPayloadForHashing( | 
|  | 218 | const string& payload_path, | 
|  | 219 | const vector<int>& signature_sizes, | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 220 | chromeos::Blob* payload_out, | 
| Don Garrett | c2e9f5f | 2013-10-18 16:42:40 -0700 | [diff] [blame] | 221 | uint64_t* metadata_size_out, | 
|  | 222 | uint64_t* signatures_offset_out) { | 
| Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 223 | // TODO(petkov): Reduce memory usage -- the payload is manipulated in memory. | 
|  | 224 |  | 
|  | 225 | // Loads the payload and adds the signature op to it. | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 226 | vector<chromeos::Blob> signatures; | 
| Alex Deymo | 020600d | 2014-11-05 21:05:55 -0800 | [diff] [blame] | 227 | for (int signature_size : signature_sizes) { | 
|  | 228 | signatures.emplace_back(signature_size, 0); | 
| Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 229 | } | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 230 | chromeos::Blob signature_blob; | 
| Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 231 | TEST_AND_RETURN_FALSE(ConvertSignatureToProtobufBlob(signatures, | 
| Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 232 | &signature_blob)); | 
| Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 233 | TEST_AND_RETURN_FALSE(AddSignatureOpToPayload(payload_path, | 
|  | 234 | signature_blob.size(), | 
| Don Garrett | c2e9f5f | 2013-10-18 16:42:40 -0700 | [diff] [blame] | 235 | payload_out, | 
|  | 236 | metadata_size_out, | 
|  | 237 | signatures_offset_out)); | 
| Don Garrett | 2ae3787 | 2013-10-25 13:33:20 -0700 | [diff] [blame] | 238 |  | 
| Don Garrett | c2e9f5f | 2013-10-18 16:42:40 -0700 | [diff] [blame] | 239 | return true; | 
|  | 240 | } | 
|  | 241 |  | 
|  | 242 | bool PayloadSigner::HashPayloadForSigning(const string& payload_path, | 
|  | 243 | const vector<int>& signature_sizes, | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 244 | chromeos::Blob* out_hash_data) { | 
|  | 245 | chromeos::Blob payload; | 
| Don Garrett | c2e9f5f | 2013-10-18 16:42:40 -0700 | [diff] [blame] | 246 | 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 Garrett | 2ae3787 | 2013-10-25 13:33:20 -0700 | [diff] [blame] | 254 |  | 
|  | 255 | // Calculates the hash on the updated payload. Note that we stop calculating | 
|  | 256 | // before we reach the signature information. | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 257 | TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfBytes(payload.data(), | 
| Don Garrett | 2ae3787 | 2013-10-25 13:33:20 -0700 | [diff] [blame] | 258 | signatures_offset, | 
|  | 259 | out_hash_data)); | 
| Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 260 | return true; | 
|  | 261 | } | 
|  | 262 |  | 
| Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 263 | bool PayloadSigner::HashMetadataForSigning(const string& payload_path, | 
| Don Garrett | c2e9f5f | 2013-10-18 16:42:40 -0700 | [diff] [blame] | 264 | const vector<int>& signature_sizes, | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 265 | chromeos::Blob* out_metadata_hash) { | 
|  | 266 | chromeos::Blob payload; | 
| Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 267 | uint64_t metadata_size; | 
| Don Garrett | c2e9f5f | 2013-10-18 16:42:40 -0700 | [diff] [blame] | 268 | 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 Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 275 |  | 
|  | 276 | // Calculates the hash on the manifest. | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 277 | TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfBytes(payload.data(), | 
| Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 278 | metadata_size, | 
|  | 279 | out_metadata_hash)); | 
|  | 280 | return true; | 
|  | 281 | } | 
|  | 282 |  | 
| Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 283 | bool PayloadSigner::AddSignatureToPayload( | 
|  | 284 | const string& payload_path, | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 285 | const vector<chromeos::Blob>& signatures, | 
| Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 286 | const string& signed_payload_path, | 
|  | 287 | uint64_t *out_metadata_size) { | 
| Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 288 | // TODO(petkov): Reduce memory usage -- the payload is manipulated in memory. | 
|  | 289 |  | 
|  | 290 | // Loads the payload and adds the signature op to it. | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 291 | chromeos::Blob signature_blob; | 
| Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 292 | TEST_AND_RETURN_FALSE(ConvertSignatureToProtobufBlob(signatures, | 
| Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 293 | &signature_blob)); | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 294 | chromeos::Blob payload; | 
| Don Garrett | 2ae3787 | 2013-10-25 13:33:20 -0700 | [diff] [blame] | 295 | uint64_t signatures_offset; | 
| Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 296 | TEST_AND_RETURN_FALSE(AddSignatureOpToPayload(payload_path, | 
|  | 297 | signature_blob.size(), | 
| Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 298 | &payload, | 
| Don Garrett | 2ae3787 | 2013-10-25 13:33:20 -0700 | [diff] [blame] | 299 | out_metadata_size, | 
|  | 300 | &signatures_offset)); | 
| Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 301 | // Appends the signature blob to the end of the payload and writes the new | 
|  | 302 | // payload. | 
| Don Garrett | 2ae3787 | 2013-10-25 13:33:20 -0700 | [diff] [blame] | 303 | 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 Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 308 | 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 Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 315 | bool PayloadSigner::GetMetadataSignature(const void* const metadata, | 
| Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 316 | size_t metadata_size, | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 317 | 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 Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 321 | chromeos::Blob metadata_hash; | 
| Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 322 | TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfBytes(metadata, | 
|  | 323 | metadata_size, | 
|  | 324 | &metadata_hash)); | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 325 |  | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 326 | chromeos::Blob signature; | 
| Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 327 | TEST_AND_RETURN_FALSE(SignHash(metadata_hash, | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 328 | private_key_path, | 
|  | 329 | &signature)); | 
|  | 330 |  | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 331 | *out_signature = chromeos::data_encoding::Base64Encode(signature); | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 332 | return true; | 
|  | 333 | } | 
|  | 334 |  | 
|  | 335 |  | 
| Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 336 | }  // namespace chromeos_update_engine |