Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 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 | // |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 16 | |
Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_SIGNER_H_ |
| 18 | #define UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_SIGNER_H_ |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 19 | |
| 20 | #include <string> |
| 21 | #include <vector> |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 22 | |
Ben Chan | 05735a1 | 2014-09-03 07:48:22 -0700 | [diff] [blame] | 23 | #include <base/macros.h> |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 24 | #include <brillo/secure_blob.h> |
Ben Chan | 05735a1 | 2014-09-03 07:48:22 -0700 | [diff] [blame] | 25 | |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 26 | #include "update_engine/update_metadata.pb.h" |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 27 | |
Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 28 | // This class encapsulates methods used for payload signing. |
| 29 | // See update_metadata.proto for more info. |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 30 | |
| 31 | namespace chromeos_update_engine { |
| 32 | |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 33 | class PayloadSigner { |
| 34 | public: |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 35 | // 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 Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 37 | static bool SignHash(const brillo::Blob& hash, |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 38 | const std::string& private_key_path, |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 39 | brillo::Blob* out_signature); |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 40 | |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 41 | // Given an unsigned payload in |unsigned_payload_path| and private keys in |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 42 | // |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 Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 46 | static bool SignPayload(const std::string& unsigned_payload_path, |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 47 | const std::vector<std::string>& private_key_paths, |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 48 | brillo::Blob* out_signature_blob); |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 49 | |
| 50 | // 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] | 51 | // 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 Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 55 | |
Don Garrett | c2e9f5f | 2013-10-18 16:42:40 -0700 | [diff] [blame] | 56 | // 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 Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 62 | brillo::Blob* payload_out, |
Don Garrett | c2e9f5f | 2013-10-18 16:42:40 -0700 | [diff] [blame] | 63 | uint64_t* metadata_size_out, |
| 64 | uint64_t* signatures_offset_out); |
| 65 | |
| 66 | // Given an unsigned payload in |payload_path|, |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 67 | // this method does two things: |
Don Garrett | c2e9f5f | 2013-10-18 16:42:40 -0700 | [diff] [blame] | 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 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 Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 75 | static bool HashPayloadForSigning(const std::string& payload_path, |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 76 | const std::vector<int>& signature_sizes, |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 77 | brillo::Blob* out_hash_data); |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 78 | |
Don Garrett | c2e9f5f | 2013-10-18 16:42:40 -0700 | [diff] [blame] | 79 | // 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 Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 89 | static bool HashMetadataForSigning(const std::string& payload_path, |
Don Garrett | c2e9f5f | 2013-10-18 16:42:40 -0700 | [diff] [blame] | 90 | const std::vector<int>& signature_sizes, |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 91 | brillo::Blob* out_metadata_hash); |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 92 | |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 93 | // 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] | 94 | // and the raw |signatures| updates the payload to include the signature thus |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 95 | // 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 Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 97 | // 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 Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 100 | static bool AddSignatureToPayload( |
| 101 | const std::string& payload_path, |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 102 | const std::vector<brillo::Blob>& signatures, |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 103 | const std::string& signed_payload_path, |
| 104 | uint64_t* out_metadata_size); |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 105 | |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 106 | // 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 Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 110 | static bool GetMetadataSignature(const void* const metadata, |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 111 | size_t metadata_size, |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 112 | const std::string& private_key_path, |
| 113 | std::string* out_signature); |
| 114 | |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 115 | private: |
| 116 | // This should never be constructed |
| 117 | DISALLOW_IMPLICIT_CONSTRUCTORS(PayloadSigner); |
| 118 | }; |
| 119 | |
| 120 | } // namespace chromeos_update_engine |
| 121 | |
Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 122 | #endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_SIGNER_H_ |