blob: 4a3578544a7b90eb526514991280c3dcb104896b [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
Alex Deymo923d8fa2014-07-15 17:58:51 -07005#ifndef UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_SIGNER_H_
6#define UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_SIGNER_H_
Andrew de los Reyes0c440052010-08-20 11:25:54 -07007
8#include <string>
9#include <vector>
Andrew de los Reyes0c440052010-08-20 11:25:54 -070010
Ben Chan05735a12014-09-03 07:48:22 -070011#include <base/macros.h>
12
Jay Srinivasanf4318702012-09-24 11:56:24 -070013#include "update_engine/update_metadata.pb.h"
Darin Petkov9574f7e2011-01-13 10:48:12 -080014
Alex Deymo923d8fa2014-07-15 17:58:51 -070015// This class encapsulates methods used for payload signing.
16// See update_metadata.proto for more info.
Andrew de los Reyes0c440052010-08-20 11:25:54 -070017
18namespace chromeos_update_engine {
19
Andrew de los Reyes0c440052010-08-20 11:25:54 -070020class PayloadSigner {
21 public:
Darin Petkov9574f7e2011-01-13 10:48:12 -080022 // Given a raw |hash| and a private key in |private_key_path| calculates the
23 // raw signature in |out_signature|. Returns true on success, false otherwise.
24 static bool SignHash(const std::vector<char>& hash,
25 const std::string& private_key_path,
26 std::vector<char>* out_signature);
27
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070028 // Given an unsigned payload in |unsigned_payload_path| and private keys in
Darin Petkov9574f7e2011-01-13 10:48:12 -080029 // |private_key_path|, calculates the signature blob into
30 // |out_signature_blob|. Note that the payload must already have an updated
31 // manifest that includes the dummy signature op. Returns true on success,
32 // false otherwise.
Andrew de los Reyes0c440052010-08-20 11:25:54 -070033 static bool SignPayload(const std::string& unsigned_payload_path,
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070034 const std::vector<std::string>& private_key_paths,
Andrew de los Reyes0c440052010-08-20 11:25:54 -070035 std::vector<char>* out_signature_blob);
36
37 // Returns the length of out_signature_blob that will result in a call
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070038 // to SignPayload with the given private keys. Returns true on success.
39 static bool SignatureBlobLength(
40 const std::vector<std::string>& private_key_paths,
41 uint64_t* out_length);
Andrew de los Reyes0c440052010-08-20 11:25:54 -070042
Don Garrettc2e9f5f2013-10-18 16:42:40 -070043 // This is a helper method for HashPayloadforSigning and
44 // HashMetadataForSigning. It loads the payload into memory, and inserts
45 // signature placeholders if Signatures aren't already present.
46 static bool PrepPayloadForHashing(
47 const std::string& payload_path,
48 const std::vector<int>& signature_sizes,
49 std::vector<char>* payload_out,
50 uint64_t* metadata_size_out,
51 uint64_t* signatures_offset_out);
52
53 // Given an unsigned payload in |payload_path|,
Jay Srinivasanf4318702012-09-24 11:56:24 -070054 // this method does two things:
Don Garrettc2e9f5f2013-10-18 16:42:40 -070055 // 1. Uses PrepPayloadForHashing to inserts placeholder signature operations
56 // to make the manifest match what the final signed payload will look
57 // like based on |signatures_sizes|, if needed.
58 // 2. It calculates the raw SHA256 hash of the payload in |payload_path|
59 // (except signatures) and returns the result in |out_hash_data|.
60 //
61 // The dummy signatures are not preserved or written to disk.
Darin Petkov9574f7e2011-01-13 10:48:12 -080062 static bool HashPayloadForSigning(const std::string& payload_path,
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070063 const std::vector<int>& signature_sizes,
Darin Petkov9574f7e2011-01-13 10:48:12 -080064 std::vector<char>* out_hash_data);
65
Don Garrettc2e9f5f2013-10-18 16:42:40 -070066 // Given an unsigned payload in |payload_path|,
67 // this method does two things:
68 // 1. Uses PrepPayloadForHashing to inserts placeholder signature operations
69 // to make the manifest match what the final signed payload will look
70 // like based on |signatures_sizes|, if needed.
71 // 2. It calculates the raw SHA256 hash of the metadata from the payload in
72 // |payload_path| (except signatures) and returns the result in
73 // |out_metadata_hash|.
74 //
75 // The dummy signatures are not preserved or written to disk.
Jay Srinivasanf4318702012-09-24 11:56:24 -070076 static bool HashMetadataForSigning(const std::string& payload_path,
Don Garrettc2e9f5f2013-10-18 16:42:40 -070077 const std::vector<int>& signature_sizes,
Jay Srinivasanf4318702012-09-24 11:56:24 -070078 std::vector<char>* out_metadata_hash);
79
Darin Petkov9574f7e2011-01-13 10:48:12 -080080 // Given an unsigned payload in |payload_path| (with no dummy signature op)
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070081 // and the raw |signatures| updates the payload to include the signature thus
Darin Petkov9574f7e2011-01-13 10:48:12 -080082 // turning it into a signed payload. The new payload is stored in
83 // |signed_payload_path|. |payload_path| and |signed_payload_path| can point
Jay Srinivasan738fdf32012-12-07 17:40:54 -080084 // to the same file. Populates |out_metadata_size| with the size of the
85 // metadata after adding the signature operation in the manifest.Returns true
86 // on success, false otherwise.
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070087 static bool AddSignatureToPayload(
88 const std::string& payload_path,
Ben Chanf9cb98c2014-09-21 18:31:30 -070089 const std::vector<std::vector<char>>& signatures,
Jay Srinivasan738fdf32012-12-07 17:40:54 -080090 const std::string& signed_payload_path,
91 uint64_t* out_metadata_size);
Darin Petkov9574f7e2011-01-13 10:48:12 -080092
Jay Srinivasanf4318702012-09-24 11:56:24 -070093 // Computes the SHA256 hash of the first metadata_size bytes of |metadata|
94 // and signs the hash with the given private_key_path and writes the signed
95 // hash in |out_signature|. Returns true if successful or false if there was
96 // any error in the computations.
97 static bool GetMetadataSignature(const char* const metadata,
98 size_t metadata_size,
Jay Srinivasan51dcf262012-09-13 17:24:32 -070099 const std::string& private_key_path,
100 std::string* out_signature);
101
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700102 private:
103 // This should never be constructed
104 DISALLOW_IMPLICIT_CONSTRUCTORS(PayloadSigner);
105};
106
107} // namespace chromeos_update_engine
108
Alex Deymo923d8fa2014-07-15 17:58:51 -0700109#endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_SIGNER_H_