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 | 759c275 | 2014-03-17 21:09:36 -0700 | [diff] [blame^] | 5 | #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_SIGNER_H_ |
| 6 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_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 | |
| 14 | // This class encapsulates methods used for payload signing and signature |
| 15 | // verification. 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 | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 19 | extern const uint32_t kSignatureMessageOriginalVersion; |
| 20 | extern const uint32_t kSignatureMessageCurrentVersion; |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 21 | |
| 22 | class PayloadSigner { |
| 23 | public: |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 24 | // Given a raw |hash| and a private key in |private_key_path| calculates the |
| 25 | // raw signature in |out_signature|. Returns true on success, false otherwise. |
| 26 | static bool SignHash(const std::vector<char>& hash, |
| 27 | const std::string& private_key_path, |
| 28 | std::vector<char>* out_signature); |
| 29 | |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 30 | // Given an unsigned payload in |unsigned_payload_path| and private keys in |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 31 | // |private_key_path|, calculates the signature blob into |
| 32 | // |out_signature_blob|. Note that the payload must already have an updated |
| 33 | // manifest that includes the dummy signature op. Returns true on success, |
| 34 | // false otherwise. |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 35 | static bool SignPayload(const std::string& unsigned_payload_path, |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 36 | const std::vector<std::string>& private_key_paths, |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 37 | std::vector<char>* out_signature_blob); |
| 38 | |
| 39 | // 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] | 40 | // to SignPayload with the given private keys. Returns true on success. |
| 41 | static bool SignatureBlobLength( |
| 42 | const std::vector<std::string>& private_key_paths, |
| 43 | uint64_t* out_length); |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 44 | |
Don Garrett | c2e9f5f | 2013-10-18 16:42:40 -0700 | [diff] [blame] | 45 | // This is a helper method for HashPayloadforSigning and |
| 46 | // HashMetadataForSigning. It loads the payload into memory, and inserts |
| 47 | // signature placeholders if Signatures aren't already present. |
| 48 | static bool PrepPayloadForHashing( |
| 49 | const std::string& payload_path, |
| 50 | const std::vector<int>& signature_sizes, |
| 51 | std::vector<char>* payload_out, |
| 52 | uint64_t* metadata_size_out, |
| 53 | uint64_t* signatures_offset_out); |
| 54 | |
| 55 | // Given an unsigned payload in |payload_path|, |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 56 | // this method does two things: |
Don Garrett | c2e9f5f | 2013-10-18 16:42:40 -0700 | [diff] [blame] | 57 | // 1. Uses PrepPayloadForHashing to inserts placeholder signature operations |
| 58 | // to make the manifest match what the final signed payload will look |
| 59 | // like based on |signatures_sizes|, if needed. |
| 60 | // 2. It calculates the raw SHA256 hash of the payload in |payload_path| |
| 61 | // (except signatures) and returns the result in |out_hash_data|. |
| 62 | // |
| 63 | // The dummy signatures are not preserved or written to disk. |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 64 | static bool HashPayloadForSigning(const std::string& payload_path, |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 65 | const std::vector<int>& signature_sizes, |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 66 | std::vector<char>* out_hash_data); |
| 67 | |
Don Garrett | c2e9f5f | 2013-10-18 16:42:40 -0700 | [diff] [blame] | 68 | // Given an unsigned payload in |payload_path|, |
| 69 | // this method does two things: |
| 70 | // 1. Uses PrepPayloadForHashing to inserts placeholder signature operations |
| 71 | // to make the manifest match what the final signed payload will look |
| 72 | // like based on |signatures_sizes|, if needed. |
| 73 | // 2. It calculates the raw SHA256 hash of the metadata from the payload in |
| 74 | // |payload_path| (except signatures) and returns the result in |
| 75 | // |out_metadata_hash|. |
| 76 | // |
| 77 | // The dummy signatures are not preserved or written to disk. |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 78 | static bool HashMetadataForSigning(const std::string& payload_path, |
Don Garrett | c2e9f5f | 2013-10-18 16:42:40 -0700 | [diff] [blame] | 79 | const std::vector<int>& signature_sizes, |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 80 | std::vector<char>* out_metadata_hash); |
| 81 | |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 82 | // 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] | 83 | // and the raw |signatures| updates the payload to include the signature thus |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 84 | // turning it into a signed payload. The new payload is stored in |
| 85 | // |signed_payload_path|. |payload_path| and |signed_payload_path| can point |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 86 | // to the same file. Populates |out_metadata_size| with the size of the |
| 87 | // metadata after adding the signature operation in the manifest.Returns true |
| 88 | // on success, false otherwise. |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 89 | static bool AddSignatureToPayload( |
| 90 | const std::string& payload_path, |
| 91 | const std::vector<std::vector<char> >& signatures, |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 92 | const std::string& signed_payload_path, |
| 93 | uint64_t* out_metadata_size); |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 94 | |
Darin Petkov | adb3cef | 2011-01-13 16:16:08 -0800 | [diff] [blame] | 95 | // Returns false if the payload signature can't be verified. Returns true |
| 96 | // otherwise and sets |out_hash| to the signed payload hash. |
| 97 | static bool VerifySignature(const std::vector<char>& signature_blob, |
| 98 | const std::string& public_key_path, |
| 99 | std::vector<char>* out_hash_data); |
| 100 | |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 101 | // Interprets signature_blob as a protocol buffer containing the Signatures |
| 102 | // message and decrypts the signature data using the public_key_path and |
| 103 | // stores the resultant raw hash data in out_hash_data. Returns true if |
| 104 | // everything is successful. False otherwise. It also takes the client_version |
| 105 | // and interprets the signature blob according to that version. |
| 106 | static bool VerifySignatureBlob(const std::vector<char>& signature_blob, |
| 107 | const std::string& public_key_path, |
| 108 | uint32_t client_version, |
| 109 | std::vector<char>* out_hash_data); |
| 110 | |
| 111 | // Decrypts sig_data with the given public_key_path and populates |
| 112 | // out_hash_data with the decoded raw hash. Returns true if successful, |
| 113 | // false otherwise. |
| 114 | static bool GetRawHashFromSignature(const std::vector<char>& sig_data, |
| 115 | const std::string& public_key_path, |
| 116 | std::vector<char>* out_hash_data); |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 117 | |
Darin Petkov | adb3cef | 2011-01-13 16:16:08 -0800 | [diff] [blame] | 118 | // Returns true if the payload in |payload_path| is signed and its hash can be |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 119 | // verified using the public key in |public_key_path| with the signature |
| 120 | // of a given version in the signature blob. Returns false otherwise. |
Darin Petkov | adb3cef | 2011-01-13 16:16:08 -0800 | [diff] [blame] | 121 | static bool VerifySignedPayload(const std::string& payload_path, |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 122 | const std::string& public_key_path, |
| 123 | uint32_t client_key_check_version); |
Darin Petkov | adb3cef | 2011-01-13 16:16:08 -0800 | [diff] [blame] | 124 | |
Andrew de los Reyes | bdfaaf0 | 2011-03-30 10:35:12 -0700 | [diff] [blame] | 125 | // Pads a SHA256 hash so that it may be encrypted/signed with RSA2048 |
| 126 | // using the PKCS#1 v1.5 scheme. |
| 127 | // hash should be a pointer to vector of exactly 256 bits. The vector |
| 128 | // will be modified in place and will result in having a length of |
| 129 | // 2048 bits. Returns true on success, false otherwise. |
| 130 | static bool PadRSA2048SHA256Hash(std::vector<char>* hash); |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 131 | |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 132 | // Computes the SHA256 hash of the first metadata_size bytes of |metadata| |
| 133 | // and signs the hash with the given private_key_path and writes the signed |
| 134 | // hash in |out_signature|. Returns true if successful or false if there was |
| 135 | // any error in the computations. |
| 136 | static bool GetMetadataSignature(const char* const metadata, |
| 137 | size_t metadata_size, |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 138 | const std::string& private_key_path, |
| 139 | std::string* out_signature); |
| 140 | |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 141 | // Reads the payload from the given |payload_path| into the |out_payload| |
| 142 | // vector. It also parses the manifest protobuf in the payload and returns it |
| 143 | // in |out_manifest| along with the size of the entire metadata in |
| 144 | // |out_metadata_size|. |
| 145 | static bool LoadPayload(const std::string& payload_path, |
| 146 | std::vector<char>* out_payload, |
| 147 | DeltaArchiveManifest* out_manifest, |
| 148 | uint64_t* out_metadata_size); |
| 149 | |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 150 | private: |
| 151 | // This should never be constructed |
| 152 | DISALLOW_IMPLICIT_CONSTRUCTORS(PayloadSigner); |
| 153 | }; |
| 154 | |
| 155 | } // namespace chromeos_update_engine |
| 156 | |
Alex Deymo | 759c275 | 2014-03-17 21:09:36 -0700 | [diff] [blame^] | 157 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_SIGNER_H_ |