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 | |
| 5 | #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_SIGNER_H__ |
| 6 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_SIGNER_H__ |
| 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 | |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 45 | // Given an unsigned payload in |payload_path| (with no dummy signature op), |
| 46 | // this method does two things: |
| 47 | // 1. It calculates the raw SHA256 hash of the entire payload in |
| 48 | // |payload_path| and returns the result in |out_hash_data|. |
| 49 | // 2. But before calculating the hash, it also inserts a dummy signature |
| 50 | // operation at the end of the manifest. This signature operation points to a |
| 51 | // blob offset and length that'll be later filled in by paygen with the |
| 52 | // actual signature from signer. But since the hash includes the signature |
| 53 | // operation, the signature operation has to be inserted before we compute |
| 54 | // the hash. Note, the hash doesn't cover the signature itself - it covers |
| 55 | // only the signature operation in manifest. Since paygen may add multiple |
| 56 | // signatures of different sizes (1024, 2048, etc.) it specifies a list |
| 57 | // of those |signature_sizes| so that the signature blob length is |
| 58 | // calculated to be of the correct size before the hash is computed. |
| 59 | // Returns true on success, false otherwise. |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 60 | static bool HashPayloadForSigning(const std::string& payload_path, |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 61 | const std::vector<int>& signature_sizes, |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 62 | std::vector<char>* out_hash_data); |
| 63 | |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 64 | // Given an unsigned payload in |payload_path|, calculates the raw hash of |
| 65 | // just the metadata (not the entire payload) that needs to be signed in |
| 66 | // |out_hash_data|. Note: This method should be called only after calling the |
| 67 | // HashPayloadForSigning method so that the metadata hash is computed on the |
| 68 | // final metadata that will be in the payload (because HashPayloadForSigning |
| 69 | // adds a dummy signature operation to the manifest). |
| 70 | // Returns true on success, false otherwise. |
| 71 | static bool HashMetadataForSigning(const std::string& payload_path, |
| 72 | std::vector<char>* out_metadata_hash); |
| 73 | |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 74 | // 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] | 75 | // and the raw |signatures| updates the payload to include the signature thus |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 76 | // turning it into a signed payload. The new payload is stored in |
| 77 | // |signed_payload_path|. |payload_path| and |signed_payload_path| can point |
| 78 | // to the same file. Returns true on success, false otherwise. |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 79 | static bool AddSignatureToPayload( |
| 80 | const std::string& payload_path, |
| 81 | const std::vector<std::vector<char> >& signatures, |
| 82 | const std::string& signed_payload_path); |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 83 | |
Darin Petkov | adb3cef | 2011-01-13 16:16:08 -0800 | [diff] [blame] | 84 | // Returns false if the payload signature can't be verified. Returns true |
| 85 | // otherwise and sets |out_hash| to the signed payload hash. |
| 86 | static bool VerifySignature(const std::vector<char>& signature_blob, |
| 87 | const std::string& public_key_path, |
| 88 | std::vector<char>* out_hash_data); |
| 89 | |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 90 | // Interprets signature_blob as a protocol buffer containing the Signatures |
| 91 | // message and decrypts the signature data using the public_key_path and |
| 92 | // stores the resultant raw hash data in out_hash_data. Returns true if |
| 93 | // everything is successful. False otherwise. It also takes the client_version |
| 94 | // and interprets the signature blob according to that version. |
| 95 | static bool VerifySignatureBlob(const std::vector<char>& signature_blob, |
| 96 | const std::string& public_key_path, |
| 97 | uint32_t client_version, |
| 98 | std::vector<char>* out_hash_data); |
| 99 | |
| 100 | // Decrypts sig_data with the given public_key_path and populates |
| 101 | // out_hash_data with the decoded raw hash. Returns true if successful, |
| 102 | // false otherwise. |
| 103 | static bool GetRawHashFromSignature(const std::vector<char>& sig_data, |
| 104 | const std::string& public_key_path, |
| 105 | std::vector<char>* out_hash_data); |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 106 | |
Darin Petkov | adb3cef | 2011-01-13 16:16:08 -0800 | [diff] [blame] | 107 | // 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] | 108 | // verified using the public key in |public_key_path| with the signature |
| 109 | // of a given version in the signature blob. Returns false otherwise. |
Darin Petkov | adb3cef | 2011-01-13 16:16:08 -0800 | [diff] [blame] | 110 | static bool VerifySignedPayload(const std::string& payload_path, |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 111 | const std::string& public_key_path, |
| 112 | uint32_t client_key_check_version); |
Darin Petkov | adb3cef | 2011-01-13 16:16:08 -0800 | [diff] [blame] | 113 | |
Andrew de los Reyes | bdfaaf0 | 2011-03-30 10:35:12 -0700 | [diff] [blame] | 114 | // Pads a SHA256 hash so that it may be encrypted/signed with RSA2048 |
| 115 | // using the PKCS#1 v1.5 scheme. |
| 116 | // hash should be a pointer to vector of exactly 256 bits. The vector |
| 117 | // will be modified in place and will result in having a length of |
| 118 | // 2048 bits. Returns true on success, false otherwise. |
| 119 | static bool PadRSA2048SHA256Hash(std::vector<char>* hash); |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 120 | |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 121 | // Computes the SHA256 hash of the first metadata_size bytes of |metadata| |
| 122 | // and signs the hash with the given private_key_path and writes the signed |
| 123 | // hash in |out_signature|. Returns true if successful or false if there was |
| 124 | // any error in the computations. |
| 125 | static bool GetMetadataSignature(const char* const metadata, |
| 126 | size_t metadata_size, |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 127 | const std::string& private_key_path, |
| 128 | std::string* out_signature); |
| 129 | |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 130 | // Reads the payload from the given |payload_path| into the |out_payload| |
| 131 | // vector. It also parses the manifest protobuf in the payload and returns it |
| 132 | // in |out_manifest| along with the size of the entire metadata in |
| 133 | // |out_metadata_size|. |
| 134 | static bool LoadPayload(const std::string& payload_path, |
| 135 | std::vector<char>* out_payload, |
| 136 | DeltaArchiveManifest* out_manifest, |
| 137 | uint64_t* out_metadata_size); |
| 138 | |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 139 | private: |
| 140 | // This should never be constructed |
| 141 | DISALLOW_IMPLICIT_CONSTRUCTORS(PayloadSigner); |
| 142 | }; |
| 143 | |
| 144 | } // namespace chromeos_update_engine |
| 145 | |
| 146 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_SIGNER_H__ |