blob: d702ccc395115c52a775164906446978a56059d8 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2010 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Andrew de los Reyes0c440052010-08-20 11:25:54 -070016
Alex Deymo923d8fa2014-07-15 17:58:51 -070017#ifndef UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_SIGNER_H_
18#define UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_SIGNER_H_
Andrew de los Reyes0c440052010-08-20 11:25:54 -070019
20#include <string>
21#include <vector>
Andrew de los Reyes0c440052010-08-20 11:25:54 -070022
Ben Chan05735a12014-09-03 07:48:22 -070023#include <base/macros.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070024#include <brillo/secure_blob.h>
Ben Chan05735a12014-09-03 07:48:22 -070025
Jay Srinivasanf4318702012-09-24 11:56:24 -070026#include "update_engine/update_metadata.pb.h"
Darin Petkov9574f7e2011-01-13 10:48:12 -080027
Alex Deymo923d8fa2014-07-15 17:58:51 -070028// This class encapsulates methods used for payload signing.
29// See update_metadata.proto for more info.
Andrew de los Reyes0c440052010-08-20 11:25:54 -070030
31namespace chromeos_update_engine {
32
Andrew de los Reyes0c440052010-08-20 11:25:54 -070033class PayloadSigner {
34 public:
Sen Jiangaef1c6f2015-10-07 10:05:32 -070035 // Reads the payload from the given |payload_path| into the |out_payload|
36 // vector. It also parses the manifest protobuf in the payload and returns it
37 // in |out_manifest| if not null, along with the major version of the payload
38 // in |out_major_version| if not null, the size of the entire metadata in
39 // |out_metadata_size| and the size of metadata signature in
40 // |out_metadata_signature_size| if not null.
41 static bool LoadPayload(const std::string& payload_path,
42 brillo::Blob* out_payload,
43 DeltaArchiveManifest* out_manifest,
44 uint64_t* out_major_version,
45 uint64_t* out_metadata_size,
46 uint32_t* out_metadata_signature_size);
47
48 // Returns true if the payload in |payload_path| is signed and its hash can be
49 // verified using the public key in |public_key_path| with the signature
50 // of a given version in the signature blob. Returns false otherwise.
51 static bool VerifySignedPayload(const std::string& payload_path,
52 const std::string& public_key_path);
53
54 // Adds a dummy operation that points to a signature blob located at the
55 // specified offset/length.
56 static void AddSignatureOp(uint64_t signature_blob_offset,
57 uint64_t signature_blob_length,
58 DeltaArchiveManifest* manifest);
59
Darin Petkov9574f7e2011-01-13 10:48:12 -080060 // Given a raw |hash| and a private key in |private_key_path| calculates the
61 // raw signature in |out_signature|. Returns true on success, false otherwise.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070062 static bool SignHash(const brillo::Blob& hash,
Darin Petkov9574f7e2011-01-13 10:48:12 -080063 const std::string& private_key_path,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070064 brillo::Blob* out_signature);
Darin Petkov9574f7e2011-01-13 10:48:12 -080065
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070066 // Given an unsigned payload in |unsigned_payload_path| and private keys in
Darin Petkov9574f7e2011-01-13 10:48:12 -080067 // |private_key_path|, calculates the signature blob into
68 // |out_signature_blob|. Note that the payload must already have an updated
69 // manifest that includes the dummy signature op. Returns true on success,
70 // false otherwise.
Andrew de los Reyes0c440052010-08-20 11:25:54 -070071 static bool SignPayload(const std::string& unsigned_payload_path,
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070072 const std::vector<std::string>& private_key_paths,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070073 brillo::Blob* out_signature_blob);
Andrew de los Reyes0c440052010-08-20 11:25:54 -070074
75 // Returns the length of out_signature_blob that will result in a call
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070076 // to SignPayload with the given private keys. Returns true on success.
77 static bool SignatureBlobLength(
78 const std::vector<std::string>& private_key_paths,
79 uint64_t* out_length);
Andrew de los Reyes0c440052010-08-20 11:25:54 -070080
Don Garrettc2e9f5f2013-10-18 16:42:40 -070081 // This is a helper method for HashPayloadforSigning and
82 // HashMetadataForSigning. It loads the payload into memory, and inserts
83 // signature placeholders if Signatures aren't already present.
84 static bool PrepPayloadForHashing(
85 const std::string& payload_path,
86 const std::vector<int>& signature_sizes,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070087 brillo::Blob* payload_out,
Don Garrettc2e9f5f2013-10-18 16:42:40 -070088 uint64_t* metadata_size_out,
89 uint64_t* signatures_offset_out);
90
91 // Given an unsigned payload in |payload_path|,
Jay Srinivasanf4318702012-09-24 11:56:24 -070092 // this method does two things:
Don Garrettc2e9f5f2013-10-18 16:42:40 -070093 // 1. Uses PrepPayloadForHashing to inserts placeholder signature operations
94 // to make the manifest match what the final signed payload will look
95 // like based on |signatures_sizes|, if needed.
96 // 2. It calculates the raw SHA256 hash of the payload in |payload_path|
97 // (except signatures) and returns the result in |out_hash_data|.
98 //
99 // The dummy signatures are not preserved or written to disk.
Darin Petkov9574f7e2011-01-13 10:48:12 -0800100 static bool HashPayloadForSigning(const std::string& payload_path,
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700101 const std::vector<int>& signature_sizes,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700102 brillo::Blob* out_hash_data);
Darin Petkov9574f7e2011-01-13 10:48:12 -0800103
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700104 // Given an unsigned payload in |payload_path|,
105 // this method does two things:
106 // 1. Uses PrepPayloadForHashing to inserts placeholder signature operations
107 // to make the manifest match what the final signed payload will look
108 // like based on |signatures_sizes|, if needed.
109 // 2. It calculates the raw SHA256 hash of the metadata from the payload in
110 // |payload_path| (except signatures) and returns the result in
111 // |out_metadata_hash|.
112 //
113 // The dummy signatures are not preserved or written to disk.
Jay Srinivasanf4318702012-09-24 11:56:24 -0700114 static bool HashMetadataForSigning(const std::string& payload_path,
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700115 const std::vector<int>& signature_sizes,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700116 brillo::Blob* out_metadata_hash);
Jay Srinivasanf4318702012-09-24 11:56:24 -0700117
Darin Petkov9574f7e2011-01-13 10:48:12 -0800118 // Given an unsigned payload in |payload_path| (with no dummy signature op)
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700119 // and the raw |signatures| updates the payload to include the signature thus
Darin Petkov9574f7e2011-01-13 10:48:12 -0800120 // turning it into a signed payload. The new payload is stored in
121 // |signed_payload_path|. |payload_path| and |signed_payload_path| can point
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800122 // to the same file. Populates |out_metadata_size| with the size of the
123 // metadata after adding the signature operation in the manifest.Returns true
124 // on success, false otherwise.
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700125 static bool AddSignatureToPayload(
126 const std::string& payload_path,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700127 const std::vector<brillo::Blob>& signatures,
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800128 const std::string& signed_payload_path,
129 uint64_t* out_metadata_size);
Darin Petkov9574f7e2011-01-13 10:48:12 -0800130
Jay Srinivasanf4318702012-09-24 11:56:24 -0700131 // Computes the SHA256 hash of the first metadata_size bytes of |metadata|
132 // and signs the hash with the given private_key_path and writes the signed
133 // hash in |out_signature|. Returns true if successful or false if there was
134 // any error in the computations.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800135 static bool GetMetadataSignature(const void* const metadata,
Jay Srinivasanf4318702012-09-24 11:56:24 -0700136 size_t metadata_size,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700137 const std::string& private_key_path,
138 std::string* out_signature);
139
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700140 private:
141 // This should never be constructed
142 DISALLOW_IMPLICIT_CONSTRUCTORS(PayloadSigner);
143};
144
145} // namespace chromeos_update_engine
146
Alex Deymo923d8fa2014-07-15 17:58:51 -0700147#endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_SIGNER_H_