blob: 40bfcfcdaf1da40aa3efc1eb6833b256be3e8768 [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
Darin Petkovb039d502010-12-03 09:08:04 -08007#include <base/logging.h>
8#include <base/string_util.h>
9#include <openssl/pem.h>
10
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070011#include "update_engine/omaha_hash_calculator.h"
Andrew de los Reyes0c440052010-08-20 11:25:54 -070012#include "update_engine/subprocess.h"
13#include "update_engine/update_metadata.pb.h"
14#include "update_engine/utils.h"
15
16using std::string;
17using std::vector;
18
19namespace chromeos_update_engine {
20
21const uint32_t kSignatureMessageVersion = 1;
22
23bool PayloadSigner::SignPayload(const string& unsigned_payload_path,
24 const string& private_key_path,
25 vector<char>* out_signature_blob) {
26 string sig_path;
27 TEST_AND_RETURN_FALSE(
28 utils::MakeTempFile("/tmp/signature.XXXXXX", &sig_path, NULL));
29 ScopedPathUnlinker sig_path_unlinker(sig_path);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070030
31 string hash_path;
32 TEST_AND_RETURN_FALSE(
33 utils::MakeTempFile("/tmp/hash.XXXXXX", &hash_path, NULL));
34 ScopedPathUnlinker hash_path_unlinker(hash_path);
Darin Petkovd22cb292010-09-29 10:02:29 -070035
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070036 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(),
45 &hash_data[0],
46 hash_data.size()));
Darin Petkovd22cb292010-09-29 10:02:29 -070047
Andrew de los Reyes0c440052010-08-20 11:25:54 -070048 // This runs on the server, so it's okay to cop out and call openssl
49 // executable rather than properly use the library
50 vector<string> cmd;
51 SplitString("/usr/bin/openssl rsautl -pkcs -sign -inkey x -in x -out x",
52 ' ',
53 &cmd);
54 cmd[cmd.size() - 5] = private_key_path;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070055 cmd[cmd.size() - 3] = hash_path;
Andrew de los Reyes0c440052010-08-20 11:25:54 -070056 cmd[cmd.size() - 1] = sig_path;
Darin Petkovd22cb292010-09-29 10:02:29 -070057
Andrew de los Reyes0c440052010-08-20 11:25:54 -070058 int return_code = 0;
59 TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &return_code));
60 TEST_AND_RETURN_FALSE(return_code == 0);
Darin Petkovd22cb292010-09-29 10:02:29 -070061
Andrew de los Reyes0c440052010-08-20 11:25:54 -070062 vector<char> signature;
63 TEST_AND_RETURN_FALSE(utils::ReadFile(sig_path, &signature));
Darin Petkovd22cb292010-09-29 10:02:29 -070064
Andrew de los Reyes0c440052010-08-20 11:25:54 -070065 // 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());
Darin Petkovd22cb292010-09-29 10:02:29 -070070
Andrew de los Reyes0c440052010-08-20 11:25:54 -070071 // 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;
78}
79
80bool PayloadSigner::SignatureBlobLength(
81 const string& private_key_path,
82 uint64_t* out_length) {
83 DCHECK(out_length);
Darin Petkovd22cb292010-09-29 10:02:29 -070084
Andrew de los Reyes0c440052010-08-20 11:25:54 -070085 string x_path;
86 TEST_AND_RETURN_FALSE(
87 utils::MakeTempFile("/tmp/signed_data.XXXXXX", &x_path, NULL));
88 ScopedPathUnlinker x_path_unlinker(x_path);
89 TEST_AND_RETURN_FALSE(utils::WriteFile(x_path.c_str(), "x", 1));
90
91 vector<char> sig_blob;
92 TEST_AND_RETURN_FALSE(PayloadSigner::SignPayload(x_path,
93 private_key_path,
94 &sig_blob));
95 *out_length = sig_blob.size();
96 return true;
97}
98
Darin Petkovd7061ab2010-10-06 14:37:09 -070099bool PayloadSigner::VerifySignature(const std::vector<char>& signature_blob,
100 const std::string& public_key_path,
101 std::vector<char>* out_hash_data) {
102 TEST_AND_RETURN_FALSE(!public_key_path.empty());
103
104 Signatures signatures;
105 TEST_AND_RETURN_FALSE(signatures.ParseFromArray(&signature_blob[0],
106 signature_blob.size()));
107
108 // Finds a signature that matches the current version.
109 int sig_index = 0;
110 for (; sig_index < signatures.signatures_size(); sig_index++) {
111 const Signatures_Signature& signature = signatures.signatures(sig_index);
112 if (signature.has_version() &&
113 signature.version() == kSignatureMessageVersion) {
114 break;
115 }
116 }
117 TEST_AND_RETURN_FALSE(sig_index < signatures.signatures_size());
118
119 const Signatures_Signature& signature = signatures.signatures(sig_index);
Darin Petkovb039d502010-12-03 09:08:04 -0800120 const string& sig_data = signature.data();
Darin Petkovd7061ab2010-10-06 14:37:09 -0700121
Darin Petkovb039d502010-12-03 09:08:04 -0800122 // The code below executes the equivalent of:
123 //
124 // openssl rsautl -verify -pubin -inkey |public_key_path|
125 // -in |sig_data| -out |out_hash_data|
Darin Petkovd7061ab2010-10-06 14:37:09 -0700126
Darin Petkovb039d502010-12-03 09:08:04 -0800127 // Loads the public key.
128 FILE* fpubkey = fopen(public_key_path.c_str(), "rb");
129 TEST_AND_RETURN_FALSE(fpubkey != NULL);
130 char dummy_password[] = { ' ', 0 }; // Ensure no password is read from stdin.
131 RSA* rsa = PEM_read_RSA_PUBKEY(fpubkey, NULL, NULL, dummy_password);
132 fclose(fpubkey);
133 TEST_AND_RETURN_FALSE(rsa != NULL);
134 unsigned int keysize = RSA_size(rsa);
135 if (sig_data.size() > 2 * keysize) {
136 LOG(ERROR) << "Signature size is too big for public key size.";
137 RSA_free(rsa);
138 return false;
139 }
Darin Petkovd7061ab2010-10-06 14:37:09 -0700140
Darin Petkovb039d502010-12-03 09:08:04 -0800141 // Decrypts the signature.
142 vector<char> hash_data(keysize);
143 int decrypt_size = RSA_public_decrypt(
144 sig_data.size(),
145 reinterpret_cast<const unsigned char*>(sig_data.data()),
146 reinterpret_cast<unsigned char*>(hash_data.data()),
147 rsa,
148 RSA_PKCS1_PADDING);
149 RSA_free(rsa);
150 TEST_AND_RETURN_FALSE(decrypt_size > 0 &&
151 decrypt_size <= static_cast<int>(hash_data.size()));
152 hash_data.resize(decrypt_size);
153 out_hash_data->swap(hash_data);
Darin Petkovd7061ab2010-10-06 14:37:09 -0700154 return true;
155}
156
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700157} // namespace chromeos_update_engine