Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame^] | 1 | // |
| 2 | // Copyright (C) 2014 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 | // |
Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 16 | |
| 17 | #ifndef UPDATE_ENGINE_PAYLOAD_VERIFIER_H_ |
| 18 | #define UPDATE_ENGINE_PAYLOAD_VERIFIER_H_ |
| 19 | |
| 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
Ben Chan | 05735a1 | 2014-09-03 07:48:22 -0700 | [diff] [blame] | 23 | #include <base/macros.h> |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 24 | #include <chromeos/secure_blob.h> |
Ben Chan | 05735a1 | 2014-09-03 07:48:22 -0700 | [diff] [blame] | 25 | |
Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 26 | #include "update_engine/update_metadata.pb.h" |
| 27 | |
| 28 | // This class encapsulates methods used for payload signature verification. |
| 29 | // See payload_generator/payload_signer.h for payload signing. |
| 30 | |
| 31 | namespace chromeos_update_engine { |
| 32 | |
| 33 | extern const uint32_t kSignatureMessageOriginalVersion; |
| 34 | extern const uint32_t kSignatureMessageCurrentVersion; |
| 35 | |
| 36 | class PayloadVerifier { |
| 37 | public: |
| 38 | // Returns false if the payload signature can't be verified. Returns true |
| 39 | // otherwise and sets |out_hash| to the signed payload hash. |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 40 | static bool VerifySignature(const chromeos::Blob& signature_blob, |
Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 41 | const std::string& public_key_path, |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 42 | chromeos::Blob* out_hash_data); |
Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 43 | |
| 44 | // Interprets signature_blob as a protocol buffer containing the Signatures |
| 45 | // message and decrypts the signature data using the public_key_path and |
| 46 | // stores the resultant raw hash data in out_hash_data. Returns true if |
| 47 | // everything is successful. False otherwise. It also takes the client_version |
| 48 | // and interprets the signature blob according to that version. |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 49 | static bool VerifySignatureBlob(const chromeos::Blob& signature_blob, |
Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 50 | const std::string& public_key_path, |
| 51 | uint32_t client_version, |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 52 | chromeos::Blob* out_hash_data); |
Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 53 | |
| 54 | // Decrypts sig_data with the given public_key_path and populates |
| 55 | // out_hash_data with the decoded raw hash. Returns true if successful, |
| 56 | // false otherwise. |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 57 | static bool GetRawHashFromSignature(const chromeos::Blob& sig_data, |
Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 58 | const std::string& public_key_path, |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 59 | chromeos::Blob* out_hash_data); |
Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 60 | |
| 61 | // Returns true if the payload in |payload_path| is signed and its hash can be |
| 62 | // verified using the public key in |public_key_path| with the signature |
| 63 | // of a given version in the signature blob. Returns false otherwise. |
| 64 | static bool VerifySignedPayload(const std::string& payload_path, |
| 65 | const std::string& public_key_path, |
| 66 | uint32_t client_key_check_version); |
| 67 | |
| 68 | // Pads a SHA256 hash so that it may be encrypted/signed with RSA2048 |
| 69 | // using the PKCS#1 v1.5 scheme. |
| 70 | // hash should be a pointer to vector of exactly 256 bits. The vector |
| 71 | // will be modified in place and will result in having a length of |
| 72 | // 2048 bits. Returns true on success, false otherwise. |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 73 | static bool PadRSA2048SHA256Hash(chromeos::Blob* hash); |
Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 74 | |
| 75 | // Reads the payload from the given |payload_path| into the |out_payload| |
| 76 | // vector. It also parses the manifest protobuf in the payload and returns it |
| 77 | // in |out_manifest| along with the size of the entire metadata in |
| 78 | // |out_metadata_size|. |
| 79 | static bool LoadPayload(const std::string& payload_path, |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 80 | chromeos::Blob* out_payload, |
Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 81 | DeltaArchiveManifest* out_manifest, |
| 82 | uint64_t* out_metadata_size); |
| 83 | |
| 84 | private: |
| 85 | // This should never be constructed |
| 86 | DISALLOW_IMPLICIT_CONSTRUCTORS(PayloadVerifier); |
| 87 | }; |
| 88 | |
| 89 | } // namespace chromeos_update_engine |
| 90 | |
| 91 | #endif // UPDATE_ENGINE_PAYLOAD_VERIFIER_H_ |