| 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 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_PAYLOAD_CONSUMER_PAYLOAD_VERIFIER_H_ | 
 | 18 | #define UPDATE_ENGINE_PAYLOAD_CONSUMER_PAYLOAD_VERIFIER_H_ | 
| Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 19 |  | 
| Tianjie Xu | 6cf830b | 2019-09-30 11:31:49 -0700 | [diff] [blame] | 20 | #include <memory> | 
| Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 21 | #include <string> | 
| Tianjie Xu | 6cf830b | 2019-09-30 11:31:49 -0700 | [diff] [blame] | 22 | #include <utility> | 
| Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame] | 23 | #include <vector> | 
| Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 24 |  | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 25 | #include <brillo/secure_blob.h> | 
| Tianjie Xu | 6cf830b | 2019-09-30 11:31:49 -0700 | [diff] [blame] | 26 | #include <openssl/evp.h> | 
| Ben Chan | 05735a1 | 2014-09-03 07:48:22 -0700 | [diff] [blame] | 27 |  | 
| Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 28 | #include "update_engine/update_metadata.pb.h" | 
 | 29 |  | 
| Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame] | 30 | // This class holds the public keys and implements methods used for payload | 
| Tianjie Xu | 6cf830b | 2019-09-30 11:31:49 -0700 | [diff] [blame] | 31 | // signature verification. See payload_generator/payload_signer.h for payload | 
 | 32 | // signing. | 
| Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 33 |  | 
 | 34 | namespace chromeos_update_engine { | 
 | 35 |  | 
| Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 36 | class PayloadVerifier { | 
 | 37 |  public: | 
| xunchang | cda3c03 | 2019-03-26 15:41:14 -0700 | [diff] [blame] | 38 |   // Pads a SHA256 hash so that it may be encrypted/signed with RSA2048 or | 
 | 39 |   // RSA4096 using the PKCS#1 v1.5 scheme. | 
 | 40 |   // hash should be a pointer to vector of exactly 256 bits. |rsa_size| must be | 
 | 41 |   // one of 256 or 512 bytes. The vector will be modified in place and will | 
 | 42 |   // result in having a length of 2048 or 4096 bits, depending on the rsa size. | 
 | 43 |   // Returns true on success, false otherwise. | 
 | 44 |   static bool PadRSASHA256Hash(brillo::Blob* hash, size_t rsa_size); | 
| Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 45 |  | 
| Tianjie Xu | 6cf830b | 2019-09-30 11:31:49 -0700 | [diff] [blame] | 46 |   // Parses the input as a PEM encoded public string. And creates a | 
 | 47 |   // PayloadVerifier with that public key for signature verification. | 
 | 48 |   static std::unique_ptr<PayloadVerifier> CreateInstance( | 
 | 49 |       const std::string& pem_public_key); | 
 | 50 |  | 
| Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame] | 51 |   // Extracts the public keys from the certificates contained in the input | 
 | 52 |   // zip file. And creates a PayloadVerifier with these public keys. | 
 | 53 |   static std::unique_ptr<PayloadVerifier> CreateInstanceFromZipPath( | 
 | 54 |       const std::string& certificate_zip_path); | 
 | 55 |  | 
| Tianjie Xu | 6cf830b | 2019-09-30 11:31:49 -0700 | [diff] [blame] | 56 |   // Interprets |signature_proto| as a protocol buffer containing the | 
 | 57 |   // |Signatures| message and decrypts each signature data using the stored | 
 | 58 |   // public key. Pads the 32 bytes |sha256_hash_data| to 256 or 512 bytes | 
 | 59 |   // according to the PKCS#1 v1.5 standard; and returns whether *any* of the | 
 | 60 |   // decrypted hashes matches the padded hash data. In case of any error parsing | 
 | 61 |   // the signatures, returns false. | 
 | 62 |   bool VerifySignature(const std::string& signature_proto, | 
 | 63 |                        const brillo::Blob& sha256_hash_data) const; | 
 | 64 |  | 
 | 65 |   // Verifies if |sig_data| is a raw signature of the hash |sha256_hash_data|. | 
 | 66 |   // If PayloadVerifier is using RSA as the public key, further puts the | 
 | 67 |   // decrypted data of |sig_data| into |decrypted_sig_data|. | 
 | 68 |   bool VerifyRawSignature(const brillo::Blob& sig_data, | 
 | 69 |                           const brillo::Blob& sha256_hash_data, | 
 | 70 |                           brillo::Blob* decrypted_sig_data) const; | 
 | 71 |  | 
| Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 72 |  private: | 
| Tianjie Xu | 6cf830b | 2019-09-30 11:31:49 -0700 | [diff] [blame] | 73 |   explicit PayloadVerifier( | 
| Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame] | 74 |       std::vector<std::unique_ptr<EVP_PKEY, decltype(&EVP_PKEY_free)>>&& | 
 | 75 |           public_keys) | 
 | 76 |       : public_keys_(std::move(public_keys)) {} | 
| Tianjie Xu | 6cf830b | 2019-09-30 11:31:49 -0700 | [diff] [blame] | 77 |  | 
 | 78 |   // Decrypts |sig_data| with the given |public_key| and populates | 
 | 79 |   // |out_hash_data| with the decoded raw hash. Returns true if successful, | 
 | 80 |   // false otherwise. | 
 | 81 |   bool GetRawHashFromSignature(const brillo::Blob& sig_data, | 
 | 82 |                                const EVP_PKEY* public_key, | 
 | 83 |                                brillo::Blob* out_hash_data) const; | 
 | 84 |  | 
| Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame] | 85 |   std::vector<std::unique_ptr<EVP_PKEY, decltype(&EVP_PKEY_free)>> public_keys_; | 
| Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 86 | }; | 
 | 87 |  | 
 | 88 | }  // namespace chromeos_update_engine | 
 | 89 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 90 | #endif  // UPDATE_ENGINE_PAYLOAD_CONSUMER_PAYLOAD_VERIFIER_H_ |