payload_signer.h revision 3e728feb92bfd5514bff55aa7dc455da80e18906
1//
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//
16
17#ifndef UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_SIGNER_H_
18#define UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_SIGNER_H_
19
20#include <string>
21#include <vector>
22
23#include <base/macros.h>
24#include <brillo/secure_blob.h>
25
26#include "update_engine/update_metadata.pb.h"
27
28// This class encapsulates methods used for payload signing.
29// See update_metadata.proto for more info.
30
31namespace chromeos_update_engine {
32
33class PayloadSigner {
34 public:
35  // 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 specified signature offset/length to given |manifest|, also adds a
55  // dummy operation that points to a signature blob located at the specified
56  // offset/length if |add_dummy_op| is true.
57  static void AddSignatureToManifest(uint64_t signature_blob_offset,
58                                     uint64_t signature_blob_length,
59                                     bool add_dummy_op,
60                                     DeltaArchiveManifest* manifest);
61
62  // Given a raw |hash| and a private key in |private_key_path| calculates the
63  // raw signature in |out_signature|. Returns true on success, false otherwise.
64  static bool SignHash(const brillo::Blob& hash,
65                       const std::string& private_key_path,
66                       brillo::Blob* out_signature);
67
68  // Sign |hash_data| blob with all private keys in |private_key_paths|, then
69  // convert the signatures to protobuf blob.
70  static bool SignHashWithKeys(
71      const brillo::Blob& hash_data,
72      const std::vector<std::string>& private_key_paths,
73      brillo::Blob* out_signature_blob);
74
75  // Given an unsigned payload in |unsigned_payload_path|, private keys in
76  // |private_key_path|, metadata size in |metadata_size|, metadata signature
77  // size in |metadata_signature_size| and signatures offset in
78  // |signatures_offset|, calculates the payload signature blob into
79  // |out_signature_blob|. Note that the payload must already have an
80  // updated manifest that includes the dummy signature op and correct metadata
81  // signature size in header. Returns true on success, false otherwise.
82  static bool SignPayload(const std::string& unsigned_payload_path,
83                          const std::vector<std::string>& private_key_paths,
84                          const uint64_t metadata_size,
85                          const uint32_t metadata_signature_size,
86                          const uint64_t signatures_offset,
87                          brillo::Blob* out_signature_blob);
88
89  // Returns the length of out_signature_blob that will result in a call
90  // to SignPayload with the given private keys. Returns true on success.
91  static bool SignatureBlobLength(
92      const std::vector<std::string>& private_key_paths,
93      uint64_t* out_length);
94
95  // Given an unsigned payload in |payload_path|,
96  // this method does two things:
97  // 1. It loads the payload into memory, and inserts placeholder signature
98  //    operations and placeholder metadata signature to make the header and
99  //    the manifest match what the final signed payload will look like based
100  //    on |signatures_sizes|, if needed.
101  // 2. It calculates the raw SHA256 hash of the payload and the metadata in
102  //    |payload_path| (except signatures) and returns the result in
103  //    |out_hash_data| and |out_metadata_hash| respectively.
104  //
105  // The changes to payload are not preserved or written to disk.
106  static bool HashPayloadForSigning(const std::string& payload_path,
107                                    const std::vector<int>& signature_sizes,
108                                    brillo::Blob* out_payload_hash_data,
109                                    brillo::Blob* out_metadata_hash);
110
111  // Given an unsigned payload in |payload_path| (with no dummy signature op)
112  // and the raw |payload_signatures| and |metadata_signatures| updates the
113  // payload to include the signature thus turning it into a signed payload. The
114  // new payload is stored in |signed_payload_path|. |payload_path| and
115  // |signed_payload_path| can point to the same file. Populates
116  // |out_metadata_size| with the size of the metadata after adding the
117  // signature operation in the manifest. Returns true on success, false
118  // otherwise.
119  static bool AddSignatureToPayload(
120      const std::string& payload_path,
121      const std::vector<brillo::Blob>& payload_signatures,
122      const std::vector<brillo::Blob>& metadata_signatures,
123      const std::string& signed_payload_path,
124      uint64_t* out_metadata_size);
125
126  // Computes the SHA256 hash of the first metadata_size bytes of |metadata|
127  // and signs the hash with the given private_key_path and writes the signed
128  // hash in |out_signature|. Returns true if successful or false if there was
129  // any error in the computations.
130  static bool GetMetadataSignature(const void* const metadata,
131                                   size_t metadata_size,
132                                   const std::string& private_key_path,
133                                   std::string* out_signature);
134
135 private:
136  // This should never be constructed
137  DISALLOW_IMPLICIT_CONSTRUCTORS(PayloadSigner);
138};
139
140}  // namespace chromeos_update_engine
141
142#endif  // UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_SIGNER_H_
143