Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(158)

Side by Side Diff: payload_signer.cc

Issue 6265001: AU: Add support for signing of update payloads after they're generated. (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/update_engine.git@master
Patch Set: rename function Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « payload_signer.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. 1 // 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "update_engine/payload_signer.h" 5 #include "update_engine/payload_signer.h"
6 6
7 #include <base/logging.h> 7 #include <base/logging.h>
8 #include <base/string_util.h> 8 #include <base/string_util.h>
9 #include <openssl/pem.h> 9 #include <openssl/pem.h>
10 10
11 #include "update_engine/delta_diff_generator.h"
12 #include "update_engine/delta_performer.h"
11 #include "update_engine/omaha_hash_calculator.h" 13 #include "update_engine/omaha_hash_calculator.h"
12 #include "update_engine/subprocess.h" 14 #include "update_engine/subprocess.h"
13 #include "update_engine/update_metadata.pb.h" 15 #include "update_engine/update_metadata.pb.h"
14 #include "update_engine/utils.h" 16 #include "update_engine/utils.h"
15 17
16 using std::string; 18 using std::string;
17 using std::vector; 19 using std::vector;
18 20
19 namespace chromeos_update_engine { 21 namespace chromeos_update_engine {
20 22
21 const uint32_t kSignatureMessageVersion = 1; 23 const uint32_t kSignatureMessageVersion = 1;
22 24
23 bool PayloadSigner::SignPayload(const string& unsigned_payload_path, 25 namespace {
24 const string& private_key_path, 26 // Given a raw |signature|, packs it into a protobuf and serializes it into a
25 vector<char>* out_signature_blob) { 27 // binary blob. Returns true on success, false otherwise.
28 bool 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
46 // Given an unsigned payload under |payload_path| and the |signature_blob_size|
47 // generates an updated payload that includes a dummy signature op in its
48 // manifest. Returns true on success, false otherwise.
49 bool AddSignatureOpToPayload(const std::string& payload_path,
50 int signature_blob_size,
51 vector<char>* out_payload) {
52 const int kProtobufOffset = 20;
53 const int kProtobufSizeOffset = 12;
54
55 vector<char> payload;
56 // Loads the payload and parses the manifest.
57 TEST_AND_RETURN_FALSE(utils::ReadFile(payload_path, &payload));
58 LOG(INFO) << "Original payload size: " << payload.size();
59 uint64_t metadata_size;
60 DeltaArchiveManifest manifest;
61 TEST_AND_RETURN_FALSE(DeltaPerformer::ParsePayloadMetadata(
62 payload, &manifest, &metadata_size) ==
63 DeltaPerformer::kMetadataParseSuccess);
64 LOG(INFO) << "Metadata size: " << metadata_size;
65 TEST_AND_RETURN_FALSE(!manifest.has_signatures_offset() &&
66 !manifest.has_signatures_size());
67
68 // Updates the manifest to include the signature operation.
69 DeltaDiffGenerator::AddSignatureOp(payload.size() - metadata_size,
70 signature_blob_size,
71 &manifest);
72
73 // Updates the payload to include the new manifest.
74 string serialized_manifest;
75 TEST_AND_RETURN_FALSE(manifest.AppendToString(&serialized_manifest));
76 LOG(INFO) << "Updated protobuf size: " << serialized_manifest.size();
77 payload.erase(payload.begin() + kProtobufOffset,
78 payload.begin() + metadata_size);
79 payload.insert(payload.begin() + kProtobufOffset,
80 serialized_manifest.begin(),
81 serialized_manifest.end());
82
83 // Updates the protobuf size.
84 uint64_t size_be = htobe64(serialized_manifest.size());
85 memcpy(&payload[kProtobufSizeOffset], &size_be, sizeof(size_be));
86 LOG(INFO) << "Updated payload size: " << payload.size();
87 out_payload->swap(payload);
88 return true;
89 }
90 } // namespace {}
91
92 bool PayloadSigner::SignHash(const vector<char>& hash,
93 const string& private_key_path,
94 vector<char>* out_signature) {
26 string sig_path; 95 string sig_path;
27 TEST_AND_RETURN_FALSE( 96 TEST_AND_RETURN_FALSE(
28 utils::MakeTempFile("/tmp/signature.XXXXXX", &sig_path, NULL)); 97 utils::MakeTempFile("/tmp/signature.XXXXXX", &sig_path, NULL));
29 ScopedPathUnlinker sig_path_unlinker(sig_path); 98 ScopedPathUnlinker sig_path_unlinker(sig_path);
30 99
31 string hash_path; 100 string hash_path;
32 TEST_AND_RETURN_FALSE( 101 TEST_AND_RETURN_FALSE(
33 utils::MakeTempFile("/tmp/hash.XXXXXX", &hash_path, NULL)); 102 utils::MakeTempFile("/tmp/hash.XXXXXX", &hash_path, NULL));
34 ScopedPathUnlinker hash_path_unlinker(hash_path); 103 ScopedPathUnlinker hash_path_unlinker(hash_path);
35
36 vector<char> hash_data;
37 {
38 vector<char> payload;
39 // TODO(adlr): Read file in chunks. Not urgent as this runs on the server.
40 TEST_AND_RETURN_FALSE(utils::ReadFile(unsigned_payload_path, &payload));
41 TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfData(payload,
42 &hash_data));
43 }
44 TEST_AND_RETURN_FALSE(utils::WriteFile(hash_path.c_str(), 104 TEST_AND_RETURN_FALSE(utils::WriteFile(hash_path.c_str(),
45 &hash_data[0], 105 hash.data(),
46 hash_data.size())); 106 hash.size()));
47 107
48 // This runs on the server, so it's okay to cop out and call openssl 108 // This runs on the server, so it's okay to cop out and call openssl
49 // executable rather than properly use the library 109 // executable rather than properly use the library
50 vector<string> cmd; 110 vector<string> cmd;
51 SplitString("/usr/bin/openssl rsautl -pkcs -sign -inkey x -in x -out x", 111 SplitString("/usr/bin/openssl rsautl -pkcs -sign -inkey x -in x -out x",
52 ' ', 112 ' ',
53 &cmd); 113 &cmd);
54 cmd[cmd.size() - 5] = private_key_path; 114 cmd[cmd.size() - 5] = private_key_path;
55 cmd[cmd.size() - 3] = hash_path; 115 cmd[cmd.size() - 3] = hash_path;
56 cmd[cmd.size() - 1] = sig_path; 116 cmd[cmd.size() - 1] = sig_path;
57 117
58 int return_code = 0; 118 int return_code = 0;
59 TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &return_code)); 119 TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &return_code));
60 TEST_AND_RETURN_FALSE(return_code == 0); 120 TEST_AND_RETURN_FALSE(return_code == 0);
61 121
62 vector<char> signature; 122 vector<char> signature;
63 TEST_AND_RETURN_FALSE(utils::ReadFile(sig_path, &signature)); 123 TEST_AND_RETURN_FALSE(utils::ReadFile(sig_path, &signature));
64 124 out_signature->swap(signature);
65 // Pack it into a protobuf
66 Signatures out_message;
67 Signatures_Signature* sig_message = out_message.add_signatures();
68 sig_message->set_version(kSignatureMessageVersion);
69 sig_message->set_data(signature.data(), signature.size());
70
71 // Serialize protobuf
72 string serialized;
73 TEST_AND_RETURN_FALSE(out_message.AppendToString(&serialized));
74 out_signature_blob->insert(out_signature_blob->end(),
75 serialized.begin(),
76 serialized.end());
77 return true; 125 return true;
78 } 126 }
79 127
128 bool PayloadSigner::SignPayload(const string& unsigned_payload_path,
129 const string& private_key_path,
130 vector<char>* out_signature_blob) {
131 vector<char> hash_data;
132 TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfFile(
133 unsigned_payload_path, -1, &hash_data) ==
134 utils::FileSize(unsigned_payload_path));
135
136 vector<char> signature;
137 TEST_AND_RETURN_FALSE(SignHash(hash_data, private_key_path, &signature));
138 TEST_AND_RETURN_FALSE(ConvertSignatureToProtobufBlob(signature,
139 out_signature_blob));
140 return true;
141 }
142
80 bool PayloadSigner::SignatureBlobLength( 143 bool PayloadSigner::SignatureBlobLength(
81 const string& private_key_path, 144 const string& private_key_path,
82 uint64_t* out_length) { 145 uint64_t* out_length) {
83 DCHECK(out_length); 146 DCHECK(out_length);
84 147
85 string x_path; 148 string x_path;
86 TEST_AND_RETURN_FALSE( 149 TEST_AND_RETURN_FALSE(
87 utils::MakeTempFile("/tmp/signed_data.XXXXXX", &x_path, NULL)); 150 utils::MakeTempFile("/tmp/signed_data.XXXXXX", &x_path, NULL));
88 ScopedPathUnlinker x_path_unlinker(x_path); 151 ScopedPathUnlinker x_path_unlinker(x_path);
89 TEST_AND_RETURN_FALSE(utils::WriteFile(x_path.c_str(), "x", 1)); 152 TEST_AND_RETURN_FALSE(utils::WriteFile(x_path.c_str(), "x", 1));
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 rsa, 210 rsa,
148 RSA_PKCS1_PADDING); 211 RSA_PKCS1_PADDING);
149 RSA_free(rsa); 212 RSA_free(rsa);
150 TEST_AND_RETURN_FALSE(decrypt_size > 0 && 213 TEST_AND_RETURN_FALSE(decrypt_size > 0 &&
151 decrypt_size <= static_cast<int>(hash_data.size())); 214 decrypt_size <= static_cast<int>(hash_data.size()));
152 hash_data.resize(decrypt_size); 215 hash_data.resize(decrypt_size);
153 out_hash_data->swap(hash_data); 216 out_hash_data->swap(hash_data);
154 return true; 217 return true;
155 } 218 }
156 219
220 bool PayloadSigner::HashPayloadForSigning(const std::string& payload_path,
221 int signature_size,
222 vector<char>* out_hash_data) {
223 // TODO(petkov): Reduce memory usage -- the payload is manipulated in memory.
224
225 // Loads the payload and adds the signature op to it.
226 vector<char> signature(signature_size, 0);
227 vector<char> signature_blob;
228 TEST_AND_RETURN_FALSE(ConvertSignatureToProtobufBlob(signature,
229 &signature_blob));
230 vector<char> payload;
231 TEST_AND_RETURN_FALSE(AddSignatureOpToPayload(payload_path,
232 signature_blob.size(),
233 &payload));
234 // Calculates the hash on the updated payload. Note that the payload includes
235 // the signature op but doesn't include the signature blob at the end.
236 TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfData(payload,
237 out_hash_data));
238 return true;
239 }
240
241 bool PayloadSigner::AddSignatureToPayload(const string& payload_path,
242 const vector<char>& signature,
243 const string& signed_payload_path) {
244 // TODO(petkov): Reduce memory usage -- the payload is manipulated in memory.
245
246 // Loads the payload and adds the signature op to it.
247 vector<char> signature_blob;
248 TEST_AND_RETURN_FALSE(ConvertSignatureToProtobufBlob(signature,
249 &signature_blob));
250 vector<char> payload;
251 TEST_AND_RETURN_FALSE(AddSignatureOpToPayload(payload_path,
252 signature_blob.size(),
253 &payload));
254 // Appends the signature blob to the end of the payload and writes the new
255 // payload.
256 payload.insert(payload.end(), signature_blob.begin(), signature_blob.end());
257 LOG(INFO) << "Signed payload size: " << payload.size();
258 TEST_AND_RETURN_FALSE(utils::WriteFile(signed_payload_path.c_str(),
259 payload.data(),
260 payload.size()));
261 return true;
262 }
263
157 } // namespace chromeos_update_engine 264 } // namespace chromeos_update_engine
OLDNEW
« no previous file with comments | « payload_signer.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698