blob: 03ff391a9e8f6e764be086208a092c8222c12e5e [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"
9#include "update_engine/subprocess.h"
10#include "update_engine/update_metadata.pb.h"
11#include "update_engine/utils.h"
12
13using std::string;
14using std::vector;
15
16namespace chromeos_update_engine {
17
18const uint32_t kSignatureMessageVersion = 1;
19
20bool PayloadSigner::SignPayload(const string& unsigned_payload_path,
21 const string& private_key_path,
22 vector<char>* out_signature_blob) {
23 string sig_path;
24 TEST_AND_RETURN_FALSE(
25 utils::MakeTempFile("/tmp/signature.XXXXXX", &sig_path, NULL));
26 ScopedPathUnlinker sig_path_unlinker(sig_path);
27
28 // This runs on the server, so it's okay to cop out and call openssl
29 // executable rather than properly use the library
30 vector<string> cmd;
31 SplitString("/usr/bin/openssl rsautl -pkcs -sign -inkey x -in x -out x",
32 ' ',
33 &cmd);
34 cmd[cmd.size() - 5] = private_key_path;
35 cmd[cmd.size() - 3] = unsigned_payload_path;
36 cmd[cmd.size() - 1] = sig_path;
37
38 int return_code = 0;
39 TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &return_code));
40 TEST_AND_RETURN_FALSE(return_code == 0);
41
42 vector<char> signature;
43 TEST_AND_RETURN_FALSE(utils::ReadFile(sig_path, &signature));
44
45 // Pack it into a protobuf
46 Signatures out_message;
47 Signatures_Signature* sig_message = out_message.add_signatures();
48 sig_message->set_version(kSignatureMessageVersion);
49 sig_message->set_data(signature.data(), signature.size());
50
51 // Serialize protobuf
52 string serialized;
53 TEST_AND_RETURN_FALSE(out_message.AppendToString(&serialized));
54 out_signature_blob->insert(out_signature_blob->end(),
55 serialized.begin(),
56 serialized.end());
57 return true;
58}
59
60bool PayloadSigner::SignatureBlobLength(
61 const string& private_key_path,
62 uint64_t* out_length) {
63 DCHECK(out_length);
64
65 string x_path;
66 TEST_AND_RETURN_FALSE(
67 utils::MakeTempFile("/tmp/signed_data.XXXXXX", &x_path, NULL));
68 ScopedPathUnlinker x_path_unlinker(x_path);
69 TEST_AND_RETURN_FALSE(utils::WriteFile(x_path.c_str(), "x", 1));
70
71 vector<char> sig_blob;
72 TEST_AND_RETURN_FALSE(PayloadSigner::SignPayload(x_path,
73 private_key_path,
74 &sig_blob));
75 *out_length = sig_blob.size();
76 return true;
77}
78
79} // namespace chromeos_update_engine