blob: 2fa9616a1f104b51f62ef73feef24e92c1aaa281 [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);
33
34 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()));
Andrew de los Reyes0c440052010-08-20 11:25:54 -070045
46 // 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;
55
56 int return_code = 0;
57 TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &return_code));
58 TEST_AND_RETURN_FALSE(return_code == 0);
59
60 vector<char> signature;
61 TEST_AND_RETURN_FALSE(utils::ReadFile(sig_path, &signature));
62
63 // 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());
68
69 // 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);
82
83 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
97} // namespace chromeos_update_engine