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> |
| 12 | |
| 13 | // This class encapsulates methods used for payload signing and signature |
| 14 | // verification. See update_metadata.proto for more info. |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 15 | |
| 16 | namespace chromeos_update_engine { |
| 17 | |
| 18 | extern const uint32_t kSignatureMessageVersion; |
| 19 | |
| 20 | class PayloadSigner { |
| 21 | public: |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 22 | // Given a raw |hash| and a private key in |private_key_path| calculates the |
| 23 | // raw signature in |out_signature|. Returns true on success, false otherwise. |
| 24 | static bool SignHash(const std::vector<char>& hash, |
| 25 | const std::string& private_key_path, |
| 26 | std::vector<char>* out_signature); |
| 27 | |
| 28 | // Given an unsigned payload in |unsigned_payload_path| and a private key in |
| 29 | // |private_key_path|, calculates the signature blob into |
| 30 | // |out_signature_blob|. Note that the payload must already have an updated |
| 31 | // manifest that includes the dummy signature op. Returns true on success, |
| 32 | // false otherwise. |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 33 | static bool SignPayload(const std::string& unsigned_payload_path, |
| 34 | const std::string& private_key_path, |
| 35 | std::vector<char>* out_signature_blob); |
| 36 | |
| 37 | // Returns the length of out_signature_blob that will result in a call |
| 38 | // to SignPayload with a given private key. Returns true on success. |
| 39 | static bool SignatureBlobLength(const std::string& private_key_path, |
| 40 | uint64_t* out_length); |
| 41 | |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 42 | // Given an unsigned payload in |payload_path| (with no dummy signature op) |
| 43 | // and the raw |signature_size| calculates the raw hash that needs to be |
| 44 | // signed in |out_hash_data|. Returns true on success, false otherwise. |
| 45 | static bool HashPayloadForSigning(const std::string& payload_path, |
| 46 | int signature_size, |
| 47 | std::vector<char>* out_hash_data); |
| 48 | |
| 49 | // Given an unsigned payload in |payload_path| (with no dummy signature op) |
| 50 | // and the raw |signature| updates the payload to include the signature thus |
| 51 | // turning it into a signed payload. The new payload is stored in |
| 52 | // |signed_payload_path|. |payload_path| and |signed_payload_path| can point |
| 53 | // to the same file. Returns true on success, false otherwise. |
| 54 | static bool AddSignatureToPayload(const std::string& payload_path, |
| 55 | const std::vector<char>& signature, |
| 56 | const std::string& signed_payload_path); |
| 57 | |
Darin Petkov | adb3cef | 2011-01-13 16:16:08 -0800 | [diff] [blame] | 58 | // Returns false if the payload signature can't be verified. Returns true |
| 59 | // otherwise and sets |out_hash| to the signed payload hash. |
| 60 | static bool VerifySignature(const std::vector<char>& signature_blob, |
| 61 | const std::string& public_key_path, |
| 62 | std::vector<char>* out_hash_data); |
| 63 | |
| 64 | // Returns true if the payload in |payload_path| is signed and its hash can be |
| 65 | // verified using the public key in |public_key_path|. Returns false |
| 66 | // otherwise. |
| 67 | static bool VerifySignedPayload(const std::string& payload_path, |
| 68 | const std::string& public_key_path); |
| 69 | |
Andrew de los Reyes | bdfaaf0 | 2011-03-30 10:35:12 -0700 | [diff] [blame] | 70 | // Pads a SHA256 hash so that it may be encrypted/signed with RSA2048 |
| 71 | // using the PKCS#1 v1.5 scheme. |
| 72 | // hash should be a pointer to vector of exactly 256 bits. The vector |
| 73 | // will be modified in place and will result in having a length of |
| 74 | // 2048 bits. Returns true on success, false otherwise. |
| 75 | static bool PadRSA2048SHA256Hash(std::vector<char>* hash); |
Andrew de los Reyes | 0c44005 | 2010-08-20 11:25:54 -0700 | [diff] [blame] | 76 | private: |
| 77 | // This should never be constructed |
| 78 | DISALLOW_IMPLICIT_CONSTRUCTORS(PayloadSigner); |
| 79 | }; |
| 80 | |
| 81 | } // namespace chromeos_update_engine |
| 82 | |
| 83 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_SIGNER_H__ |