payload_signer.h revision f68bbbc952aa9a71898e4939b5f36187fa564a50
1// 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
5#ifndef UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_SIGNER_H_
6#define UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_SIGNER_H_
7
8#include <string>
9#include <vector>
10
11#include <base/macros.h>
12#include <chromeos/secure_blob.h>
13
14#include "update_engine/update_metadata.pb.h"
15
16// This class encapsulates methods used for payload signing.
17// See update_metadata.proto for more info.
18
19namespace chromeos_update_engine {
20
21class PayloadSigner {
22 public:
23  // Given a raw |hash| and a private key in |private_key_path| calculates the
24  // raw signature in |out_signature|. Returns true on success, false otherwise.
25  static bool SignHash(const chromeos::Blob& hash,
26                       const std::string& private_key_path,
27                       chromeos::Blob* out_signature);
28
29  // Given an unsigned payload in |unsigned_payload_path| and private keys in
30  // |private_key_path|, calculates the signature blob into
31  // |out_signature_blob|. Note that the payload must already have an updated
32  // manifest that includes the dummy signature op. Returns true on success,
33  // false otherwise.
34  static bool SignPayload(const std::string& unsigned_payload_path,
35                          const std::vector<std::string>& private_key_paths,
36                          chromeos::Blob* out_signature_blob);
37
38  // Returns the length of out_signature_blob that will result in a call
39  // to SignPayload with the given private keys. Returns true on success.
40  static bool SignatureBlobLength(
41      const std::vector<std::string>& private_key_paths,
42      uint64_t* out_length);
43
44  // This is a helper method for HashPayloadforSigning and
45  // HashMetadataForSigning. It loads the payload into memory, and inserts
46  // signature placeholders if Signatures aren't already present.
47  static bool PrepPayloadForHashing(
48        const std::string& payload_path,
49        const std::vector<int>& signature_sizes,
50        chromeos::Blob* payload_out,
51        uint64_t* metadata_size_out,
52        uint64_t* signatures_offset_out);
53
54  // Given an unsigned payload in |payload_path|,
55  // this method does two things:
56  // 1. Uses PrepPayloadForHashing to inserts placeholder signature operations
57  //    to make the manifest match what the final signed payload will look
58  //    like based on |signatures_sizes|, if needed.
59  // 2. It calculates the raw SHA256 hash of the payload in |payload_path|
60  //    (except signatures) and returns the result in |out_hash_data|.
61  //
62  // The dummy signatures are not preserved or written to disk.
63  static bool HashPayloadForSigning(const std::string& payload_path,
64                                    const std::vector<int>& signature_sizes,
65                                    chromeos::Blob* out_hash_data);
66
67  // Given an unsigned payload in |payload_path|,
68  // this method does two things:
69  // 1. Uses PrepPayloadForHashing to inserts placeholder signature operations
70  //    to make the manifest match what the final signed payload will look
71  //    like based on |signatures_sizes|, if needed.
72  // 2. It calculates the raw SHA256 hash of the metadata from the payload in
73  //    |payload_path| (except signatures) and returns the result in
74  //    |out_metadata_hash|.
75  //
76  // The dummy signatures are not preserved or written to disk.
77  static bool HashMetadataForSigning(const std::string& payload_path,
78                                     const std::vector<int>& signature_sizes,
79                                     chromeos::Blob* out_metadata_hash);
80
81  // Given an unsigned payload in |payload_path| (with no dummy signature op)
82  // and the raw |signatures| updates the payload to include the signature thus
83  // turning it into a signed payload. The new payload is stored in
84  // |signed_payload_path|. |payload_path| and |signed_payload_path| can point
85  // to the same file. Populates |out_metadata_size| with the size of the
86  // metadata after adding the signature operation in the manifest.Returns true
87  // on success, false otherwise.
88  static bool AddSignatureToPayload(
89      const std::string& payload_path,
90      const std::vector<chromeos::Blob>& signatures,
91      const std::string& signed_payload_path,
92      uint64_t* out_metadata_size);
93
94  // Computes the SHA256 hash of the first metadata_size bytes of |metadata|
95  // and signs the hash with the given private_key_path and writes the signed
96  // hash in |out_signature|. Returns true if successful or false if there was
97  // any error in the computations.
98  static bool GetMetadataSignature(const void* const metadata,
99                                   size_t metadata_size,
100                                   const std::string& private_key_path,
101                                   std::string* out_signature);
102
103 private:
104  // This should never be constructed
105  DISALLOW_IMPLICIT_CONSTRUCTORS(PayloadSigner);
106};
107
108}  // namespace chromeos_update_engine
109
110#endif  // UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_SIGNER_H_
111