blob: de80a5bcc6e2f317041da36f2173b17b8c37b0a5 [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
7#include "base/logging.h"
8#include "base/string_util.h"
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07009#include "update_engine/omaha_hash_calculator.h"
Andrew de los Reyes0c440052010-08-20 11:25:54 -070010#include "update_engine/subprocess.h"
11#include "update_engine/update_metadata.pb.h"
12#include "update_engine/utils.h"
13
14using std::string;
15using std::vector;
16
17namespace chromeos_update_engine {
18
19const uint32_t kSignatureMessageVersion = 1;
20
21bool PayloadSigner::SignPayload(const string& unsigned_payload_path,
22 const string& private_key_path,
23 vector<char>* out_signature_blob) {
24 string sig_path;
25 TEST_AND_RETURN_FALSE(
26 utils::MakeTempFile("/tmp/signature.XXXXXX", &sig_path, NULL));
27 ScopedPathUnlinker sig_path_unlinker(sig_path);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070028
29 string hash_path;
30 TEST_AND_RETURN_FALSE(
31 utils::MakeTempFile("/tmp/hash.XXXXXX", &hash_path, NULL));
32 ScopedPathUnlinker hash_path_unlinker(hash_path);
Darin Petkovd22cb292010-09-29 10:02:29 -070033
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070034 vector<char> hash_data;
35 {
36 vector<char> payload;
37 // TODO(adlr): Read file in chunks. Not urgent as this runs on the server.
38 TEST_AND_RETURN_FALSE(utils::ReadFile(unsigned_payload_path, &payload));
39 TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfData(payload,
40 &hash_data));
41 }
42 TEST_AND_RETURN_FALSE(utils::WriteFile(hash_path.c_str(),
43 &hash_data[0],
44 hash_data.size()));
Darin Petkovd22cb292010-09-29 10:02:29 -070045
Andrew de los Reyes0c440052010-08-20 11:25:54 -070046 // This runs on the server, so it's okay to cop out and call openssl
47 // executable rather than properly use the library
48 vector<string> cmd;
49 SplitString("/usr/bin/openssl rsautl -pkcs -sign -inkey x -in x -out x",
50 ' ',
51 &cmd);
52 cmd[cmd.size() - 5] = private_key_path;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070053 cmd[cmd.size() - 3] = hash_path;
Andrew de los Reyes0c440052010-08-20 11:25:54 -070054 cmd[cmd.size() - 1] = sig_path;
Darin Petkovd22cb292010-09-29 10:02:29 -070055
Andrew de los Reyes0c440052010-08-20 11:25:54 -070056 int return_code = 0;
57 TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &return_code));
58 TEST_AND_RETURN_FALSE(return_code == 0);
Darin Petkovd22cb292010-09-29 10:02:29 -070059
Andrew de los Reyes0c440052010-08-20 11:25:54 -070060 vector<char> signature;
61 TEST_AND_RETURN_FALSE(utils::ReadFile(sig_path, &signature));
Darin Petkovd22cb292010-09-29 10:02:29 -070062
Andrew de los Reyes0c440052010-08-20 11:25:54 -070063 // Pack it into a protobuf
64 Signatures out_message;
65 Signatures_Signature* sig_message = out_message.add_signatures();
66 sig_message->set_version(kSignatureMessageVersion);
67 sig_message->set_data(signature.data(), signature.size());
Darin Petkovd22cb292010-09-29 10:02:29 -070068
Andrew de los Reyes0c440052010-08-20 11:25:54 -070069 // Serialize protobuf
70 string serialized;
71 TEST_AND_RETURN_FALSE(out_message.AppendToString(&serialized));
72 out_signature_blob->insert(out_signature_blob->end(),
73 serialized.begin(),
74 serialized.end());
75 return true;
76}
77
78bool PayloadSigner::SignatureBlobLength(
79 const string& private_key_path,
80 uint64_t* out_length) {
81 DCHECK(out_length);
Darin Petkovd22cb292010-09-29 10:02:29 -070082
Andrew de los Reyes0c440052010-08-20 11:25:54 -070083 string x_path;
84 TEST_AND_RETURN_FALSE(
85 utils::MakeTempFile("/tmp/signed_data.XXXXXX", &x_path, NULL));
86 ScopedPathUnlinker x_path_unlinker(x_path);
87 TEST_AND_RETURN_FALSE(utils::WriteFile(x_path.c_str(), "x", 1));
88
89 vector<char> sig_blob;
90 TEST_AND_RETURN_FALSE(PayloadSigner::SignPayload(x_path,
91 private_key_path,
92 &sig_blob));
93 *out_length = sig_blob.size();
94 return true;
95}
96
Darin Petkovd7061ab2010-10-06 14:37:09 -070097bool PayloadSigner::VerifySignature(const std::vector<char>& signature_blob,
98 const std::string& public_key_path,
99 std::vector<char>* out_hash_data) {
100 TEST_AND_RETURN_FALSE(!public_key_path.empty());
101
102 Signatures signatures;
103 TEST_AND_RETURN_FALSE(signatures.ParseFromArray(&signature_blob[0],
104 signature_blob.size()));
105
106 // Finds a signature that matches the current version.
107 int sig_index = 0;
108 for (; sig_index < signatures.signatures_size(); sig_index++) {
109 const Signatures_Signature& signature = signatures.signatures(sig_index);
110 if (signature.has_version() &&
111 signature.version() == kSignatureMessageVersion) {
112 break;
113 }
114 }
115 TEST_AND_RETURN_FALSE(sig_index < signatures.signatures_size());
116
117 const Signatures_Signature& signature = signatures.signatures(sig_index);
118 const string sig_data = signature.data();
119 string sig_path;
120 TEST_AND_RETURN_FALSE(
121 utils::MakeTempFile("/var/run/signature.XXXXXX", &sig_path, NULL));
122 ScopedPathUnlinker sig_path_unlinker(sig_path);
123 TEST_AND_RETURN_FALSE(utils::WriteFile(sig_path.c_str(),
124 &sig_data[0],
125 sig_data.size()));
126 string hash_path;
127 TEST_AND_RETURN_FALSE(
128 utils::MakeTempFile("/var/run/hash.XXXXXX", &hash_path, NULL));
129 ScopedPathUnlinker hash_path_unlinker(hash_path);
130
131 // TODO(petkov): This runs on the client so it will be cleaner if it uses
132 // direct openssl library calls.
133 vector<string> cmd;
134 SplitString("/usr/bin/openssl rsautl -verify -pubin -inkey x -in x -out x",
135 ' ',
136 &cmd);
137 cmd[cmd.size() - 5] = public_key_path;
138 cmd[cmd.size() - 3] = sig_path;
139 cmd[cmd.size() - 1] = hash_path;
140
141 int return_code = 0;
142 TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &return_code));
143 TEST_AND_RETURN_FALSE(return_code == 0);
144
145 TEST_AND_RETURN_FALSE(utils::ReadFile(hash_path, out_hash_data));
146 return true;
147}
148
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700149} // namespace chromeos_update_engine