blob: a17a9846f0f6eb058afcae6d58cc559e9ea829a3 [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>
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080012#include <chromeos/secure_blob.h>
Ben Chan05735a12014-09-03 07:48:22 -070013
Jay Srinivasanf4318702012-09-24 11:56:24 -070014#include "update_engine/update_metadata.pb.h"
Darin Petkov9574f7e2011-01-13 10:48:12 -080015
Alex Deymo923d8fa2014-07-15 17:58:51 -070016// This class encapsulates methods used for payload signing.
17// See update_metadata.proto for more info.
Andrew de los Reyes0c440052010-08-20 11:25:54 -070018
19namespace chromeos_update_engine {
20
Andrew de los Reyes0c440052010-08-20 11:25:54 -070021class PayloadSigner {
22 public:
Darin Petkov9574f7e2011-01-13 10:48:12 -080023 // Given a raw |hash| and a private key in |private_key_path| calculates the
24 // raw signature in |out_signature|. Returns true on success, false otherwise.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080025 static bool SignHash(const chromeos::Blob& hash,
Darin Petkov9574f7e2011-01-13 10:48:12 -080026 const std::string& private_key_path,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080027 chromeos::Blob* out_signature);
Darin Petkov9574f7e2011-01-13 10:48:12 -080028
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070029 // Given an unsigned payload in |unsigned_payload_path| and private keys in
Darin Petkov9574f7e2011-01-13 10:48:12 -080030 // |private_key_path|, calculates the signature blob into
31 // |out_signature_blob|. Note that the payload must already have an updated
32 // manifest that includes the dummy signature op. Returns true on success,
33 // false otherwise.
Andrew de los Reyes0c440052010-08-20 11:25:54 -070034 static bool SignPayload(const std::string& unsigned_payload_path,
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070035 const std::vector<std::string>& private_key_paths,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080036 chromeos::Blob* out_signature_blob);
Andrew de los Reyes0c440052010-08-20 11:25:54 -070037
38 // Returns the length of out_signature_blob that will result in a call
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070039 // to SignPayload with the given private keys. Returns true on success.
40 static bool SignatureBlobLength(
41 const std::vector<std::string>& private_key_paths,
42 uint64_t* out_length);
Andrew de los Reyes0c440052010-08-20 11:25:54 -070043
Don Garrettc2e9f5f2013-10-18 16:42:40 -070044 // This is a helper method for HashPayloadforSigning and
45 // HashMetadataForSigning. It loads the payload into memory, and inserts
46 // signature placeholders if Signatures aren't already present.
47 static bool PrepPayloadForHashing(
48 const std::string& payload_path,
49 const std::vector<int>& signature_sizes,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080050 chromeos::Blob* payload_out,
Don Garrettc2e9f5f2013-10-18 16:42:40 -070051 uint64_t* metadata_size_out,
52 uint64_t* signatures_offset_out);
53
54 // Given an unsigned payload in |payload_path|,
Jay Srinivasanf4318702012-09-24 11:56:24 -070055 // this method does two things:
Don Garrettc2e9f5f2013-10-18 16:42:40 -070056 // 1. Uses PrepPayloadForHashing to inserts placeholder signature operations
57 // to make the manifest match what the final signed payload will look
58 // like based on |signatures_sizes|, if needed.
59 // 2. It calculates the raw SHA256 hash of the payload in |payload_path|
60 // (except signatures) and returns the result in |out_hash_data|.
61 //
62 // The dummy signatures are not preserved or written to disk.
Darin Petkov9574f7e2011-01-13 10:48:12 -080063 static bool HashPayloadForSigning(const std::string& payload_path,
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070064 const std::vector<int>& signature_sizes,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080065 chromeos::Blob* out_hash_data);
Darin Petkov9574f7e2011-01-13 10:48:12 -080066
Don Garrettc2e9f5f2013-10-18 16:42:40 -070067 // Given an unsigned payload in |payload_path|,
68 // this method does two things:
69 // 1. Uses PrepPayloadForHashing to inserts placeholder signature operations
70 // to make the manifest match what the final signed payload will look
71 // like based on |signatures_sizes|, if needed.
72 // 2. It calculates the raw SHA256 hash of the metadata from the payload in
73 // |payload_path| (except signatures) and returns the result in
74 // |out_metadata_hash|.
75 //
76 // The dummy signatures are not preserved or written to disk.
Jay Srinivasanf4318702012-09-24 11:56:24 -070077 static bool HashMetadataForSigning(const std::string& payload_path,
Don Garrettc2e9f5f2013-10-18 16:42:40 -070078 const std::vector<int>& signature_sizes,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080079 chromeos::Blob* out_metadata_hash);
Jay Srinivasanf4318702012-09-24 11:56:24 -070080
Darin Petkov9574f7e2011-01-13 10:48:12 -080081 // Given an unsigned payload in |payload_path| (with no dummy signature op)
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070082 // and the raw |signatures| updates the payload to include the signature thus
Darin Petkov9574f7e2011-01-13 10:48:12 -080083 // turning it into a signed payload. The new payload is stored in
84 // |signed_payload_path|. |payload_path| and |signed_payload_path| can point
Jay Srinivasan738fdf32012-12-07 17:40:54 -080085 // to the same file. Populates |out_metadata_size| with the size of the
86 // metadata after adding the signature operation in the manifest.Returns true
87 // on success, false otherwise.
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070088 static bool AddSignatureToPayload(
89 const std::string& payload_path,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080090 const std::vector<chromeos::Blob>& signatures,
Jay Srinivasan738fdf32012-12-07 17:40:54 -080091 const std::string& signed_payload_path,
92 uint64_t* out_metadata_size);
Darin Petkov9574f7e2011-01-13 10:48:12 -080093
Jay Srinivasanf4318702012-09-24 11:56:24 -070094 // Computes the SHA256 hash of the first metadata_size bytes of |metadata|
95 // and signs the hash with the given private_key_path and writes the signed
96 // hash in |out_signature|. Returns true if successful or false if there was
97 // any error in the computations.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080098 static bool GetMetadataSignature(const void* const metadata,
Jay Srinivasanf4318702012-09-24 11:56:24 -070099 size_t metadata_size,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700100 const std::string& private_key_path,
101 std::string* out_signature);
102
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700103 private:
104 // This should never be constructed
105 DISALLOW_IMPLICIT_CONSTRUCTORS(PayloadSigner);
106};
107
108} // namespace chromeos_update_engine
109
Alex Deymo923d8fa2014-07-15 17:58:51 -0700110#endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_SIGNER_H_