blob: 0c540e6e220259d40363d5b4d32cc1e9a5798127 [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
Darin Petkov9574f7e2011-01-13 10:48:12 -080011#include <base/basictypes.h>
Jay Srinivasanf4318702012-09-24 11:56:24 -070012#include "update_engine/update_metadata.pb.h"
Darin Petkov9574f7e2011-01-13 10:48:12 -080013
Alex Deymo923d8fa2014-07-15 17:58:51 -070014// This class encapsulates methods used for payload signing.
15// See update_metadata.proto for more info.
Andrew de los Reyes0c440052010-08-20 11:25:54 -070016
17namespace chromeos_update_engine {
18
Andrew de los Reyes0c440052010-08-20 11:25:54 -070019class PayloadSigner {
20 public:
Darin Petkov9574f7e2011-01-13 10:48:12 -080021 // Given a raw |hash| and a private key in |private_key_path| calculates the
22 // raw signature in |out_signature|. Returns true on success, false otherwise.
23 static bool SignHash(const std::vector<char>& hash,
24 const std::string& private_key_path,
25 std::vector<char>* out_signature);
26
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070027 // Given an unsigned payload in |unsigned_payload_path| and private keys in
Darin Petkov9574f7e2011-01-13 10:48:12 -080028 // |private_key_path|, calculates the signature blob into
29 // |out_signature_blob|. Note that the payload must already have an updated
30 // manifest that includes the dummy signature op. Returns true on success,
31 // false otherwise.
Andrew de los Reyes0c440052010-08-20 11:25:54 -070032 static bool SignPayload(const std::string& unsigned_payload_path,
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070033 const std::vector<std::string>& private_key_paths,
Andrew de los Reyes0c440052010-08-20 11:25:54 -070034 std::vector<char>* out_signature_blob);
35
36 // Returns the length of out_signature_blob that will result in a call
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070037 // to SignPayload with the given private keys. Returns true on success.
38 static bool SignatureBlobLength(
39 const std::vector<std::string>& private_key_paths,
40 uint64_t* out_length);
Andrew de los Reyes0c440052010-08-20 11:25:54 -070041
Don Garrettc2e9f5f2013-10-18 16:42:40 -070042 // This is a helper method for HashPayloadforSigning and
43 // HashMetadataForSigning. It loads the payload into memory, and inserts
44 // signature placeholders if Signatures aren't already present.
45 static bool PrepPayloadForHashing(
46 const std::string& payload_path,
47 const std::vector<int>& signature_sizes,
48 std::vector<char>* payload_out,
49 uint64_t* metadata_size_out,
50 uint64_t* signatures_offset_out);
51
52 // Given an unsigned payload in |payload_path|,
Jay Srinivasanf4318702012-09-24 11:56:24 -070053 // this method does two things:
Don Garrettc2e9f5f2013-10-18 16:42:40 -070054 // 1. Uses PrepPayloadForHashing to inserts placeholder signature operations
55 // to make the manifest match what the final signed payload will look
56 // like based on |signatures_sizes|, if needed.
57 // 2. It calculates the raw SHA256 hash of the payload in |payload_path|
58 // (except signatures) and returns the result in |out_hash_data|.
59 //
60 // The dummy signatures are not preserved or written to disk.
Darin Petkov9574f7e2011-01-13 10:48:12 -080061 static bool HashPayloadForSigning(const std::string& payload_path,
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070062 const std::vector<int>& signature_sizes,
Darin Petkov9574f7e2011-01-13 10:48:12 -080063 std::vector<char>* out_hash_data);
64
Don Garrettc2e9f5f2013-10-18 16:42:40 -070065 // Given an unsigned payload in |payload_path|,
66 // this method does two things:
67 // 1. Uses PrepPayloadForHashing to inserts placeholder signature operations
68 // to make the manifest match what the final signed payload will look
69 // like based on |signatures_sizes|, if needed.
70 // 2. It calculates the raw SHA256 hash of the metadata from the payload in
71 // |payload_path| (except signatures) and returns the result in
72 // |out_metadata_hash|.
73 //
74 // The dummy signatures are not preserved or written to disk.
Jay Srinivasanf4318702012-09-24 11:56:24 -070075 static bool HashMetadataForSigning(const std::string& payload_path,
Don Garrettc2e9f5f2013-10-18 16:42:40 -070076 const std::vector<int>& signature_sizes,
Jay Srinivasanf4318702012-09-24 11:56:24 -070077 std::vector<char>* out_metadata_hash);
78
Darin Petkov9574f7e2011-01-13 10:48:12 -080079 // Given an unsigned payload in |payload_path| (with no dummy signature op)
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070080 // and the raw |signatures| updates the payload to include the signature thus
Darin Petkov9574f7e2011-01-13 10:48:12 -080081 // turning it into a signed payload. The new payload is stored in
82 // |signed_payload_path|. |payload_path| and |signed_payload_path| can point
Jay Srinivasan738fdf32012-12-07 17:40:54 -080083 // to the same file. Populates |out_metadata_size| with the size of the
84 // metadata after adding the signature operation in the manifest.Returns true
85 // on success, false otherwise.
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070086 static bool AddSignatureToPayload(
87 const std::string& payload_path,
88 const std::vector<std::vector<char> >& signatures,
Jay Srinivasan738fdf32012-12-07 17:40:54 -080089 const std::string& signed_payload_path,
90 uint64_t* out_metadata_size);
Darin Petkov9574f7e2011-01-13 10:48:12 -080091
Jay Srinivasanf4318702012-09-24 11:56:24 -070092 // Computes the SHA256 hash of the first metadata_size bytes of |metadata|
93 // and signs the hash with the given private_key_path and writes the signed
94 // hash in |out_signature|. Returns true if successful or false if there was
95 // any error in the computations.
96 static bool GetMetadataSignature(const char* const metadata,
97 size_t metadata_size,
Jay Srinivasan51dcf262012-09-13 17:24:32 -070098 const std::string& private_key_path,
99 std::string* out_signature);
100
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700101 private:
102 // This should never be constructed
103 DISALLOW_IMPLICIT_CONSTRUCTORS(PayloadSigner);
104};
105
106} // namespace chromeos_update_engine
107
Alex Deymo923d8fa2014-07-15 17:58:51 -0700108#endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_SIGNER_H_