blob: 38c673cc4615c348ad08bf4540276a0eccee1138 [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 Deymo98e691c2016-02-04 21:05:45 -080024#include <brillo/key_value_store.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070025#include <brillo/secure_blob.h>
Ben Chan05735a12014-09-03 07:48:22 -070026
Jay Srinivasanf4318702012-09-24 11:56:24 -070027#include "update_engine/update_metadata.pb.h"
Darin Petkov9574f7e2011-01-13 10:48:12 -080028
Alex Deymo923d8fa2014-07-15 17:58:51 -070029// This class encapsulates methods used for payload signing.
30// See update_metadata.proto for more info.
Andrew de los Reyes0c440052010-08-20 11:25:54 -070031
32namespace chromeos_update_engine {
33
Andrew de los Reyes0c440052010-08-20 11:25:54 -070034class PayloadSigner {
35 public:
Sen Jiangaef1c6f2015-10-07 10:05:32 -070036 // Returns true if the payload in |payload_path| is signed and its hash can be
37 // verified using the public key in |public_key_path| with the signature
38 // of a given version in the signature blob. Returns false otherwise.
39 static bool VerifySignedPayload(const std::string& payload_path,
40 const std::string& public_key_path);
41
Sen Jiang3e728fe2015-11-05 11:37:23 -080042 // Adds specified signature offset/length to given |manifest|, also adds a
43 // dummy operation that points to a signature blob located at the specified
44 // offset/length if |add_dummy_op| is true.
45 static void AddSignatureToManifest(uint64_t signature_blob_offset,
46 uint64_t signature_blob_length,
47 bool add_dummy_op,
48 DeltaArchiveManifest* manifest);
Sen Jiangaef1c6f2015-10-07 10:05:32 -070049
Darin Petkov9574f7e2011-01-13 10:48:12 -080050 // Given a raw |hash| and a private key in |private_key_path| calculates the
51 // raw signature in |out_signature|. Returns true on success, false otherwise.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070052 static bool SignHash(const brillo::Blob& hash,
Darin Petkov9574f7e2011-01-13 10:48:12 -080053 const std::string& private_key_path,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070054 brillo::Blob* out_signature);
Darin Petkov9574f7e2011-01-13 10:48:12 -080055
Sen Jiang720df3e2015-10-01 13:10:44 -070056 // Sign |hash_data| blob with all private keys in |private_key_paths|, then
57 // convert the signatures to protobuf blob.
58 static bool SignHashWithKeys(
59 const brillo::Blob& hash_data,
60 const std::vector<std::string>& private_key_paths,
61 brillo::Blob* out_signature_blob);
62
63 // Given an unsigned payload in |unsigned_payload_path|, private keys in
64 // |private_key_path|, metadata size in |metadata_size|, metadata signature
65 // size in |metadata_signature_size| and signatures offset in
66 // |signatures_offset|, calculates the payload signature blob into
67 // |out_signature_blob|. Note that the payload must already have an
68 // updated manifest that includes the dummy signature op and correct metadata
69 // signature size in header. Returns true on success, false otherwise.
Andrew de los Reyes0c440052010-08-20 11:25:54 -070070 static bool SignPayload(const std::string& unsigned_payload_path,
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070071 const std::vector<std::string>& private_key_paths,
Sen Jiang720df3e2015-10-01 13:10:44 -070072 const uint64_t metadata_size,
73 const uint32_t metadata_signature_size,
74 const uint64_t signatures_offset,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070075 brillo::Blob* out_signature_blob);
Andrew de los Reyes0c440052010-08-20 11:25:54 -070076
77 // Returns the length of out_signature_blob that will result in a call
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070078 // to SignPayload with the given private keys. Returns true on success.
79 static bool SignatureBlobLength(
80 const std::vector<std::string>& private_key_paths,
81 uint64_t* out_length);
Andrew de los Reyes0c440052010-08-20 11:25:54 -070082
Don Garrettc2e9f5f2013-10-18 16:42:40 -070083 // Given an unsigned payload in |payload_path|,
Jay Srinivasanf4318702012-09-24 11:56:24 -070084 // this method does two things:
Sen Jiang720df3e2015-10-01 13:10:44 -070085 // 1. It loads the payload into memory, and inserts placeholder signature
86 // operations and placeholder metadata signature to make the header and
87 // the manifest match what the final signed payload will look like based
88 // on |signatures_sizes|, if needed.
89 // 2. It calculates the raw SHA256 hash of the payload and the metadata in
90 // |payload_path| (except signatures) and returns the result in
91 // |out_hash_data| and |out_metadata_hash| respectively.
Don Garrettc2e9f5f2013-10-18 16:42:40 -070092 //
Sen Jiang720df3e2015-10-01 13:10:44 -070093 // The changes to payload are not preserved or written to disk.
Darin Petkov9574f7e2011-01-13 10:48:12 -080094 static bool HashPayloadForSigning(const std::string& payload_path,
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070095 const std::vector<int>& signature_sizes,
Sen Jiang720df3e2015-10-01 13:10:44 -070096 brillo::Blob* out_payload_hash_data,
97 brillo::Blob* out_metadata_hash);
Jay Srinivasanf4318702012-09-24 11:56:24 -070098
Darin Petkov9574f7e2011-01-13 10:48:12 -080099 // Given an unsigned payload in |payload_path| (with no dummy signature op)
Sen Jiang644f6182015-10-06 16:45:57 -0700100 // and the raw |payload_signatures| and |metadata_signatures| updates the
101 // payload to include the signature thus turning it into a signed payload. The
102 // new payload is stored in |signed_payload_path|. |payload_path| and
103 // |signed_payload_path| can point to the same file. Populates
104 // |out_metadata_size| with the size of the metadata after adding the
105 // signature operation in the manifest. Returns true on success, false
106 // otherwise.
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700107 static bool AddSignatureToPayload(
108 const std::string& payload_path,
Sen Jiang644f6182015-10-06 16:45:57 -0700109 const std::vector<brillo::Blob>& payload_signatures,
110 const std::vector<brillo::Blob>& metadata_signatures,
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800111 const std::string& signed_payload_path,
112 uint64_t* out_metadata_size);
Darin Petkov9574f7e2011-01-13 10:48:12 -0800113
Jay Srinivasanf4318702012-09-24 11:56:24 -0700114 // Computes the SHA256 hash of the first metadata_size bytes of |metadata|
115 // and signs the hash with the given private_key_path and writes the signed
116 // hash in |out_signature|. Returns true if successful or false if there was
117 // any error in the computations.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800118 static bool GetMetadataSignature(const void* const metadata,
Jay Srinivasanf4318702012-09-24 11:56:24 -0700119 size_t metadata_size,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700120 const std::string& private_key_path,
121 std::string* out_signature);
122
Alex Deymo98e691c2016-02-04 21:05:45 -0800123 static bool ExtractPayloadProperties(const std::string& payload_path,
124 brillo::KeyValueStore* properties);
125
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700126 private:
127 // This should never be constructed
128 DISALLOW_IMPLICIT_CONSTRUCTORS(PayloadSigner);
129};
130
131} // namespace chromeos_update_engine
132
Alex Deymo923d8fa2014-07-15 17:58:51 -0700133#endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_SIGNER_H_