blob: 8b34f1073d1f194f533a5854029dffc2594c55a1 [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 Deymo759c2752014-03-17 21:09:36 -07005#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_SIGNER_H_
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_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
14// This class encapsulates methods used for payload signing and signature
15// verification. 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 Reyesc24e3f32011-08-30 15:45:20 -070019extern const uint32_t kSignatureMessageOriginalVersion;
20extern const uint32_t kSignatureMessageCurrentVersion;
Andrew de los Reyes0c440052010-08-20 11:25:54 -070021
22class PayloadSigner {
23 public:
Darin Petkov9574f7e2011-01-13 10:48:12 -080024 // Given a raw |hash| and a private key in |private_key_path| calculates the
25 // raw signature in |out_signature|. Returns true on success, false otherwise.
26 static bool SignHash(const std::vector<char>& hash,
27 const std::string& private_key_path,
28 std::vector<char>* out_signature);
29
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070030 // Given an unsigned payload in |unsigned_payload_path| and private keys in
Darin Petkov9574f7e2011-01-13 10:48:12 -080031 // |private_key_path|, calculates the signature blob into
32 // |out_signature_blob|. Note that the payload must already have an updated
33 // manifest that includes the dummy signature op. Returns true on success,
34 // false otherwise.
Andrew de los Reyes0c440052010-08-20 11:25:54 -070035 static bool SignPayload(const std::string& unsigned_payload_path,
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070036 const std::vector<std::string>& private_key_paths,
Andrew de los Reyes0c440052010-08-20 11:25:54 -070037 std::vector<char>* out_signature_blob);
38
39 // Returns the length of out_signature_blob that will result in a call
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070040 // to SignPayload with the given private keys. Returns true on success.
41 static bool SignatureBlobLength(
42 const std::vector<std::string>& private_key_paths,
43 uint64_t* out_length);
Andrew de los Reyes0c440052010-08-20 11:25:54 -070044
Don Garrettc2e9f5f2013-10-18 16:42:40 -070045 // This is a helper method for HashPayloadforSigning and
46 // HashMetadataForSigning. It loads the payload into memory, and inserts
47 // signature placeholders if Signatures aren't already present.
48 static bool PrepPayloadForHashing(
49 const std::string& payload_path,
50 const std::vector<int>& signature_sizes,
51 std::vector<char>* payload_out,
52 uint64_t* metadata_size_out,
53 uint64_t* signatures_offset_out);
54
55 // Given an unsigned payload in |payload_path|,
Jay Srinivasanf4318702012-09-24 11:56:24 -070056 // this method does two things:
Don Garrettc2e9f5f2013-10-18 16:42:40 -070057 // 1. Uses PrepPayloadForHashing to inserts placeholder signature operations
58 // to make the manifest match what the final signed payload will look
59 // like based on |signatures_sizes|, if needed.
60 // 2. It calculates the raw SHA256 hash of the payload in |payload_path|
61 // (except signatures) and returns the result in |out_hash_data|.
62 //
63 // The dummy signatures are not preserved or written to disk.
Darin Petkov9574f7e2011-01-13 10:48:12 -080064 static bool HashPayloadForSigning(const std::string& payload_path,
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070065 const std::vector<int>& signature_sizes,
Darin Petkov9574f7e2011-01-13 10:48:12 -080066 std::vector<char>* out_hash_data);
67
Don Garrettc2e9f5f2013-10-18 16:42:40 -070068 // Given an unsigned payload in |payload_path|,
69 // this method does two things:
70 // 1. Uses PrepPayloadForHashing to inserts placeholder signature operations
71 // to make the manifest match what the final signed payload will look
72 // like based on |signatures_sizes|, if needed.
73 // 2. It calculates the raw SHA256 hash of the metadata from the payload in
74 // |payload_path| (except signatures) and returns the result in
75 // |out_metadata_hash|.
76 //
77 // The dummy signatures are not preserved or written to disk.
Jay Srinivasanf4318702012-09-24 11:56:24 -070078 static bool HashMetadataForSigning(const std::string& payload_path,
Don Garrettc2e9f5f2013-10-18 16:42:40 -070079 const std::vector<int>& signature_sizes,
Jay Srinivasanf4318702012-09-24 11:56:24 -070080 std::vector<char>* out_metadata_hash);
81
Darin Petkov9574f7e2011-01-13 10:48:12 -080082 // Given an unsigned payload in |payload_path| (with no dummy signature op)
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070083 // and the raw |signatures| updates the payload to include the signature thus
Darin Petkov9574f7e2011-01-13 10:48:12 -080084 // turning it into a signed payload. The new payload is stored in
85 // |signed_payload_path|. |payload_path| and |signed_payload_path| can point
Jay Srinivasan738fdf32012-12-07 17:40:54 -080086 // to the same file. Populates |out_metadata_size| with the size of the
87 // metadata after adding the signature operation in the manifest.Returns true
88 // on success, false otherwise.
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070089 static bool AddSignatureToPayload(
90 const std::string& payload_path,
91 const std::vector<std::vector<char> >& signatures,
Jay Srinivasan738fdf32012-12-07 17:40:54 -080092 const std::string& signed_payload_path,
93 uint64_t* out_metadata_size);
Darin Petkov9574f7e2011-01-13 10:48:12 -080094
Darin Petkovadb3cef2011-01-13 16:16:08 -080095 // Returns false if the payload signature can't be verified. Returns true
96 // otherwise and sets |out_hash| to the signed payload hash.
97 static bool VerifySignature(const std::vector<char>& signature_blob,
98 const std::string& public_key_path,
99 std::vector<char>* out_hash_data);
100
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700101 // Interprets signature_blob as a protocol buffer containing the Signatures
102 // message and decrypts the signature data using the public_key_path and
103 // stores the resultant raw hash data in out_hash_data. Returns true if
104 // everything is successful. False otherwise. It also takes the client_version
105 // and interprets the signature blob according to that version.
106 static bool VerifySignatureBlob(const std::vector<char>& signature_blob,
107 const std::string& public_key_path,
108 uint32_t client_version,
109 std::vector<char>* out_hash_data);
110
111 // Decrypts sig_data with the given public_key_path and populates
112 // out_hash_data with the decoded raw hash. Returns true if successful,
113 // false otherwise.
114 static bool GetRawHashFromSignature(const std::vector<char>& sig_data,
115 const std::string& public_key_path,
116 std::vector<char>* out_hash_data);
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700117
Darin Petkovadb3cef2011-01-13 16:16:08 -0800118 // Returns true if the payload in |payload_path| is signed and its hash can be
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700119 // verified using the public key in |public_key_path| with the signature
120 // of a given version in the signature blob. Returns false otherwise.
Darin Petkovadb3cef2011-01-13 16:16:08 -0800121 static bool VerifySignedPayload(const std::string& payload_path,
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700122 const std::string& public_key_path,
123 uint32_t client_key_check_version);
Darin Petkovadb3cef2011-01-13 16:16:08 -0800124
Andrew de los Reyesbdfaaf02011-03-30 10:35:12 -0700125 // Pads a SHA256 hash so that it may be encrypted/signed with RSA2048
126 // using the PKCS#1 v1.5 scheme.
127 // hash should be a pointer to vector of exactly 256 bits. The vector
128 // will be modified in place and will result in having a length of
129 // 2048 bits. Returns true on success, false otherwise.
130 static bool PadRSA2048SHA256Hash(std::vector<char>* hash);
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700131
Jay Srinivasanf4318702012-09-24 11:56:24 -0700132 // Computes the SHA256 hash of the first metadata_size bytes of |metadata|
133 // and signs the hash with the given private_key_path and writes the signed
134 // hash in |out_signature|. Returns true if successful or false if there was
135 // any error in the computations.
136 static bool GetMetadataSignature(const char* const metadata,
137 size_t metadata_size,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700138 const std::string& private_key_path,
139 std::string* out_signature);
140
Jay Srinivasanf4318702012-09-24 11:56:24 -0700141 // Reads the payload from the given |payload_path| into the |out_payload|
142 // vector. It also parses the manifest protobuf in the payload and returns it
143 // in |out_manifest| along with the size of the entire metadata in
144 // |out_metadata_size|.
145 static bool LoadPayload(const std::string& payload_path,
146 std::vector<char>* out_payload,
147 DeltaArchiveManifest* out_manifest,
148 uint64_t* out_metadata_size);
149
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700150 private:
151 // This should never be constructed
152 DISALLOW_IMPLICIT_CONSTRUCTORS(PayloadSigner);
153};
154
155} // namespace chromeos_update_engine
156
Alex Deymo759c2752014-03-17 21:09:36 -0700157#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_SIGNER_H_