| 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 Deymo | 98e691c | 2016-02-04 21:05:45 -0800 | [diff] [blame] | 24 | #include <brillo/key_value_store.h> | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 25 | #include <brillo/secure_blob.h> | 
| Ben Chan | 05735a1 | 2014-09-03 07:48:22 -0700 | [diff] [blame] | 26 |  | 
| Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 27 | #include "update_engine/update_metadata.pb.h" | 
| Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 28 |  | 
| Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 29 | // This class encapsulates methods used for payload signing. | 
 | 30 | // See update_metadata.proto for more info. | 
| Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 31 |  | 
 | 32 | namespace chromeos_update_engine { | 
 | 33 |  | 
| Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 34 | class PayloadSigner { | 
 | 35 |  public: | 
| Sen Jiang | aef1c6f | 2015-10-07 10:05:32 -0700 | [diff] [blame] | 36 |   // Returns true if the payload in |payload_path| is signed and its hash can be | 
 | 37 |   // verified using the public key in |public_key_path| with the signature | 
 | 38 |   // of a given version in the signature blob. Returns false otherwise. | 
 | 39 |   static bool VerifySignedPayload(const std::string& payload_path, | 
 | 40 |                                   const std::string& public_key_path); | 
 | 41 |  | 
| Amin Hassani | 55c7541 | 2019-10-07 11:20:39 -0700 | [diff] [blame] | 42 |   // Adds specified signature offset/length to given |manifest|. | 
| Sen Jiang | 3e728fe | 2015-11-05 11:37:23 -0800 | [diff] [blame] | 43 |   static void AddSignatureToManifest(uint64_t signature_blob_offset, | 
 | 44 |                                      uint64_t signature_blob_length, | 
| Sen Jiang | 3e728fe | 2015-11-05 11:37:23 -0800 | [diff] [blame] | 45 |                                      DeltaArchiveManifest* manifest); | 
| Sen Jiang | aef1c6f | 2015-10-07 10:05:32 -0700 | [diff] [blame] | 46 |  | 
| Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 47 |   // Given a raw |hash| and a private key in |private_key_path| calculates the | 
 | 48 |   // raw signature in |out_signature|. Returns true on success, false otherwise. | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 49 |   static bool SignHash(const brillo::Blob& hash, | 
| Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 50 |                        const std::string& private_key_path, | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 51 |                        brillo::Blob* out_signature); | 
| Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 52 |  | 
| Sen Jiang | 720df3e | 2015-10-01 13:10:44 -0700 | [diff] [blame] | 53 |   // Sign |hash_data| blob with all private keys in |private_key_paths|, then | 
| Sen Jiang | 9b2f178 | 2019-01-24 14:27:50 -0800 | [diff] [blame] | 54 |   // convert the signatures to serialized protobuf. | 
| Sen Jiang | 720df3e | 2015-10-01 13:10:44 -0700 | [diff] [blame] | 55 |   static bool SignHashWithKeys( | 
 | 56 |       const brillo::Blob& hash_data, | 
 | 57 |       const std::vector<std::string>& private_key_paths, | 
| Sen Jiang | 9b2f178 | 2019-01-24 14:27:50 -0800 | [diff] [blame] | 58 |       std::string* out_serialized_signature); | 
| Sen Jiang | 720df3e | 2015-10-01 13:10:44 -0700 | [diff] [blame] | 59 |  | 
 | 60 |   // Given an unsigned payload in |unsigned_payload_path|, private keys in | 
 | 61 |   // |private_key_path|, metadata size in |metadata_size|, metadata signature | 
 | 62 |   // size in |metadata_signature_size| and signatures offset in | 
 | 63 |   // |signatures_offset|, calculates the payload signature blob into | 
| Sen Jiang | 9b2f178 | 2019-01-24 14:27:50 -0800 | [diff] [blame] | 64 |   // |out_serialized_signature|. Note that the payload must already have an | 
| Tianjie | d60dc39 | 2020-07-29 11:27:35 -0700 | [diff] [blame] | 65 |   // updated manifest that includes the fake signature op and correct metadata | 
| Sen Jiang | 720df3e | 2015-10-01 13:10:44 -0700 | [diff] [blame] | 66 |   // signature size in header. Returns true on success, false otherwise. | 
| Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 67 |   static bool SignPayload(const std::string& unsigned_payload_path, | 
| Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 68 |                           const std::vector<std::string>& private_key_paths, | 
| Sen Jiang | 720df3e | 2015-10-01 13:10:44 -0700 | [diff] [blame] | 69 |                           const uint64_t metadata_size, | 
 | 70 |                           const uint32_t metadata_signature_size, | 
 | 71 |                           const uint64_t signatures_offset, | 
| Sen Jiang | 9b2f178 | 2019-01-24 14:27:50 -0800 | [diff] [blame] | 72 |                           std::string* out_serialized_signature); | 
| Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 73 |  | 
| Sen Jiang | 9b2f178 | 2019-01-24 14:27:50 -0800 | [diff] [blame] | 74 |   // Returns the length of out_serialized_signature that will result in a call | 
| Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 75 |   // to SignPayload with the given private keys. Returns true on success. | 
 | 76 |   static bool SignatureBlobLength( | 
| Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 77 |       const std::vector<std::string>& private_key_paths, uint64_t* out_length); | 
| Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 78 |  | 
| Don Garrett | c2e9f5f | 2013-10-18 16:42:40 -0700 | [diff] [blame] | 79 |   // Given an unsigned payload in |payload_path|, | 
| Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 80 |   // this method does two things: | 
| Sen Jiang | 720df3e | 2015-10-01 13:10:44 -0700 | [diff] [blame] | 81 |   // 1. It loads the payload into memory, and inserts placeholder signature | 
 | 82 |   //    operations and placeholder metadata signature to make the header and | 
 | 83 |   //    the manifest match what the final signed payload will look like based | 
 | 84 |   //    on |signatures_sizes|, if needed. | 
 | 85 |   // 2. It calculates the raw SHA256 hash of the payload and the metadata in | 
 | 86 |   //    |payload_path| (except signatures) and returns the result in | 
 | 87 |   //    |out_hash_data| and |out_metadata_hash| respectively. | 
| Don Garrett | c2e9f5f | 2013-10-18 16:42:40 -0700 | [diff] [blame] | 88 |   // | 
| Sen Jiang | 720df3e | 2015-10-01 13:10:44 -0700 | [diff] [blame] | 89 |   // The changes to payload are not preserved or written to disk. | 
| Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 90 |   static bool HashPayloadForSigning(const std::string& payload_path, | 
| Tianjie Xu | dac04b7 | 2019-09-30 11:48:17 -0700 | [diff] [blame] | 91 |                                     const std::vector<size_t>& signature_sizes, | 
| Sen Jiang | 720df3e | 2015-10-01 13:10:44 -0700 | [diff] [blame] | 92 |                                     brillo::Blob* out_payload_hash_data, | 
 | 93 |                                     brillo::Blob* out_metadata_hash); | 
| Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 94 |  | 
| Tianjie | d60dc39 | 2020-07-29 11:27:35 -0700 | [diff] [blame] | 95 |   // Given an unsigned payload in |payload_path| (with no fake signature op) | 
| Sen Jiang | 644f618 | 2015-10-06 16:45:57 -0700 | [diff] [blame] | 96 |   // and the raw |payload_signatures| and |metadata_signatures| updates the | 
 | 97 |   // payload to include the signature thus turning it into a signed payload. The | 
 | 98 |   // new payload is stored in |signed_payload_path|. |payload_path| and | 
 | 99 |   // |signed_payload_path| can point to the same file. Populates | 
 | 100 |   // |out_metadata_size| with the size of the metadata after adding the | 
 | 101 |   // signature operation in the manifest. Returns true on success, false | 
 | 102 |   // otherwise. | 
| Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 103 |   static bool AddSignatureToPayload( | 
 | 104 |       const std::string& payload_path, | 
| Tianjie Xu | 7bbe015 | 2019-10-09 18:11:15 -0700 | [diff] [blame] | 105 |       const std::vector<size_t>& padded_signature_sizes, | 
| Sen Jiang | 644f618 | 2015-10-06 16:45:57 -0700 | [diff] [blame] | 106 |       const std::vector<brillo::Blob>& payload_signatures, | 
 | 107 |       const std::vector<brillo::Blob>& metadata_signatures, | 
| Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 108 |       const std::string& signed_payload_path, | 
 | 109 |       uint64_t* out_metadata_size); | 
| Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 110 |  | 
| Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 111 |   // Computes the SHA256 hash of the first metadata_size bytes of |metadata| | 
 | 112 |   // and signs the hash with the given private_key_path and writes the signed | 
 | 113 |   // hash in |out_signature|. Returns true if successful or false if there was | 
 | 114 |   // any error in the computations. | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 115 |   static bool GetMetadataSignature(const void* const metadata, | 
| Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 116 |                                    size_t metadata_size, | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 117 |                                    const std::string& private_key_path, | 
 | 118 |                                    std::string* out_signature); | 
 | 119 |  | 
| Alex Deymo | 98e691c | 2016-02-04 21:05:45 -0800 | [diff] [blame] | 120 |   static bool ExtractPayloadProperties(const std::string& payload_path, | 
 | 121 |                                        brillo::KeyValueStore* properties); | 
 | 122 |  | 
| Tianjie Xu | 7bbe015 | 2019-10-09 18:11:15 -0700 | [diff] [blame] | 123 |   // This function calculates the maximum size, in bytes, of a signature signed | 
 | 124 |   // by private_key_path. For an RSA key, this returns the number of bytes | 
 | 125 |   // needed to represent the modulus. For an EC key, this returns the maximum | 
 | 126 |   // size of a DER-encoded ECDSA signature. | 
 | 127 |   static bool GetMaximumSignatureSize(const std::string& private_key_path, | 
 | 128 |                                       size_t* signature_size); | 
 | 129 |  | 
| Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 130 |  private: | 
 | 131 |   // This should never be constructed | 
 | 132 |   DISALLOW_IMPLICIT_CONSTRUCTORS(PayloadSigner); | 
 | 133 | }; | 
 | 134 |  | 
 | 135 | }  // namespace chromeos_update_engine | 
 | 136 |  | 
| Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 137 | #endif  // UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_SIGNER_H_ |