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