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