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