blob: 11116a36dbabee719ecb6d961a65fde3dc7fcdd9 [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:
Darin Petkov9574f7e2011-01-13 10:48:12 -080035 // Given a raw |hash| and a private key in |private_key_path| calculates the
36 // raw signature in |out_signature|. Returns true on success, false otherwise.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070037 static bool SignHash(const brillo::Blob& hash,
Darin Petkov9574f7e2011-01-13 10:48:12 -080038 const std::string& private_key_path,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070039 brillo::Blob* out_signature);
Darin Petkov9574f7e2011-01-13 10:48:12 -080040
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070041 // Given an unsigned payload in |unsigned_payload_path| and private keys in
Darin Petkov9574f7e2011-01-13 10:48:12 -080042 // |private_key_path|, calculates the signature blob into
43 // |out_signature_blob|. Note that the payload must already have an updated
44 // manifest that includes the dummy signature op. Returns true on success,
45 // false otherwise.
Andrew de los Reyes0c440052010-08-20 11:25:54 -070046 static bool SignPayload(const std::string& unsigned_payload_path,
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070047 const std::vector<std::string>& private_key_paths,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070048 brillo::Blob* out_signature_blob);
Andrew de los Reyes0c440052010-08-20 11:25:54 -070049
50 // Returns the length of out_signature_blob that will result in a call
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070051 // to SignPayload with the given private keys. Returns true on success.
52 static bool SignatureBlobLength(
53 const std::vector<std::string>& private_key_paths,
54 uint64_t* out_length);
Andrew de los Reyes0c440052010-08-20 11:25:54 -070055
Don Garrettc2e9f5f2013-10-18 16:42:40 -070056 // This is a helper method for HashPayloadforSigning and
57 // HashMetadataForSigning. It loads the payload into memory, and inserts
58 // signature placeholders if Signatures aren't already present.
59 static bool PrepPayloadForHashing(
60 const std::string& payload_path,
61 const std::vector<int>& signature_sizes,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070062 brillo::Blob* payload_out,
Don Garrettc2e9f5f2013-10-18 16:42:40 -070063 uint64_t* metadata_size_out,
64 uint64_t* signatures_offset_out);
65
66 // Given an unsigned payload in |payload_path|,
Jay Srinivasanf4318702012-09-24 11:56:24 -070067 // this method does two things:
Don Garrettc2e9f5f2013-10-18 16:42:40 -070068 // 1. Uses PrepPayloadForHashing to inserts placeholder signature operations
69 // to make the manifest match what the final signed payload will look
70 // like based on |signatures_sizes|, if needed.
71 // 2. It calculates the raw SHA256 hash of the payload in |payload_path|
72 // (except signatures) and returns the result in |out_hash_data|.
73 //
74 // The dummy signatures are not preserved or written to disk.
Darin Petkov9574f7e2011-01-13 10:48:12 -080075 static bool HashPayloadForSigning(const std::string& payload_path,
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070076 const std::vector<int>& signature_sizes,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070077 brillo::Blob* out_hash_data);
Darin Petkov9574f7e2011-01-13 10:48:12 -080078
Don Garrettc2e9f5f2013-10-18 16:42:40 -070079 // Given an unsigned payload in |payload_path|,
80 // this method does two things:
81 // 1. Uses PrepPayloadForHashing to inserts placeholder signature operations
82 // to make the manifest match what the final signed payload will look
83 // like based on |signatures_sizes|, if needed.
84 // 2. It calculates the raw SHA256 hash of the metadata from the payload in
85 // |payload_path| (except signatures) and returns the result in
86 // |out_metadata_hash|.
87 //
88 // The dummy signatures are not preserved or written to disk.
Jay Srinivasanf4318702012-09-24 11:56:24 -070089 static bool HashMetadataForSigning(const std::string& payload_path,
Don Garrettc2e9f5f2013-10-18 16:42:40 -070090 const std::vector<int>& signature_sizes,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070091 brillo::Blob* out_metadata_hash);
Jay Srinivasanf4318702012-09-24 11:56:24 -070092
Darin Petkov9574f7e2011-01-13 10:48:12 -080093 // Given an unsigned payload in |payload_path| (with no dummy signature op)
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070094 // and the raw |signatures| updates the payload to include the signature thus
Darin Petkov9574f7e2011-01-13 10:48:12 -080095 // turning it into a signed payload. The new payload is stored in
96 // |signed_payload_path|. |payload_path| and |signed_payload_path| can point
Jay Srinivasan738fdf32012-12-07 17:40:54 -080097 // to the same file. Populates |out_metadata_size| with the size of the
98 // metadata after adding the signature operation in the manifest.Returns true
99 // on success, false otherwise.
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700100 static bool AddSignatureToPayload(
101 const std::string& payload_path,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700102 const std::vector<brillo::Blob>& signatures,
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800103 const std::string& signed_payload_path,
104 uint64_t* out_metadata_size);
Darin Petkov9574f7e2011-01-13 10:48:12 -0800105
Jay Srinivasanf4318702012-09-24 11:56:24 -0700106 // Computes the SHA256 hash of the first metadata_size bytes of |metadata|
107 // and signs the hash with the given private_key_path and writes the signed
108 // hash in |out_signature|. Returns true if successful or false if there was
109 // any error in the computations.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800110 static bool GetMetadataSignature(const void* const metadata,
Jay Srinivasanf4318702012-09-24 11:56:24 -0700111 size_t metadata_size,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700112 const std::string& private_key_path,
113 std::string* out_signature);
114
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700115 private:
116 // This should never be constructed
117 DISALLOW_IMPLICIT_CONSTRUCTORS(PayloadSigner);
118};
119
120} // namespace chromeos_update_engine
121
Alex Deymo923d8fa2014-07-15 17:58:51 -0700122#endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_SIGNER_H_