xref: /aosp_15_r20/system/update_engine/payload_consumer/payload_verifier.h (revision 5a9231315b4521097b8dc3750bc806fcafe0c72f)
1*5a923131SAndroid Build Coastguard Worker //
2*5a923131SAndroid Build Coastguard Worker // Copyright (C) 2014 The Android Open Source Project
3*5a923131SAndroid Build Coastguard Worker //
4*5a923131SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
5*5a923131SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
6*5a923131SAndroid Build Coastguard Worker // You may obtain a copy of the License at
7*5a923131SAndroid Build Coastguard Worker //
8*5a923131SAndroid Build Coastguard Worker //      http://www.apache.org/licenses/LICENSE-2.0
9*5a923131SAndroid Build Coastguard Worker //
10*5a923131SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
11*5a923131SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
12*5a923131SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*5a923131SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
14*5a923131SAndroid Build Coastguard Worker // limitations under the License.
15*5a923131SAndroid Build Coastguard Worker //
16*5a923131SAndroid Build Coastguard Worker 
17*5a923131SAndroid Build Coastguard Worker #ifndef UPDATE_ENGINE_PAYLOAD_CONSUMER_PAYLOAD_VERIFIER_H_
18*5a923131SAndroid Build Coastguard Worker #define UPDATE_ENGINE_PAYLOAD_CONSUMER_PAYLOAD_VERIFIER_H_
19*5a923131SAndroid Build Coastguard Worker 
20*5a923131SAndroid Build Coastguard Worker #include <memory>
21*5a923131SAndroid Build Coastguard Worker #include <string>
22*5a923131SAndroid Build Coastguard Worker #include <utility>
23*5a923131SAndroid Build Coastguard Worker #include <vector>
24*5a923131SAndroid Build Coastguard Worker 
25*5a923131SAndroid Build Coastguard Worker #include <brillo/secure_blob.h>
26*5a923131SAndroid Build Coastguard Worker #include <openssl/evp.h>
27*5a923131SAndroid Build Coastguard Worker 
28*5a923131SAndroid Build Coastguard Worker #include "update_engine/update_metadata.pb.h"
29*5a923131SAndroid Build Coastguard Worker 
30*5a923131SAndroid Build Coastguard Worker // This class holds the public keys and implements methods used for payload
31*5a923131SAndroid Build Coastguard Worker // signature verification. See payload_generator/payload_signer.h for payload
32*5a923131SAndroid Build Coastguard Worker // signing.
33*5a923131SAndroid Build Coastguard Worker 
34*5a923131SAndroid Build Coastguard Worker namespace chromeos_update_engine {
35*5a923131SAndroid Build Coastguard Worker 
36*5a923131SAndroid Build Coastguard Worker class PayloadVerifier {
37*5a923131SAndroid Build Coastguard Worker  public:
38*5a923131SAndroid Build Coastguard Worker   // Pads a SHA256 hash so that it may be encrypted/signed with RSA2048 or
39*5a923131SAndroid Build Coastguard Worker   // RSA4096 using the PKCS#1 v1.5 scheme.
40*5a923131SAndroid Build Coastguard Worker   // hash should be a pointer to vector of exactly 256 bits. |rsa_size| must be
41*5a923131SAndroid Build Coastguard Worker   // one of 256 or 512 bytes. The vector will be modified in place and will
42*5a923131SAndroid Build Coastguard Worker   // result in having a length of 2048 or 4096 bits, depending on the rsa size.
43*5a923131SAndroid Build Coastguard Worker   // Returns true on success, false otherwise.
44*5a923131SAndroid Build Coastguard Worker   static bool PadRSASHA256Hash(brillo::Blob* hash, size_t rsa_size);
45*5a923131SAndroid Build Coastguard Worker 
46*5a923131SAndroid Build Coastguard Worker   // Parses the input as a PEM encoded public string. And creates a
47*5a923131SAndroid Build Coastguard Worker   // PayloadVerifier with that public key for signature verification.
48*5a923131SAndroid Build Coastguard Worker   static std::unique_ptr<PayloadVerifier> CreateInstance(
49*5a923131SAndroid Build Coastguard Worker       const std::string& pem_public_key);
50*5a923131SAndroid Build Coastguard Worker 
51*5a923131SAndroid Build Coastguard Worker   // Extracts the public keys from the certificates contained in the input
52*5a923131SAndroid Build Coastguard Worker   // zip file. And creates a PayloadVerifier with these public keys.
53*5a923131SAndroid Build Coastguard Worker   static std::unique_ptr<PayloadVerifier> CreateInstanceFromZipPath(
54*5a923131SAndroid Build Coastguard Worker       const std::string& certificate_zip_path);
55*5a923131SAndroid Build Coastguard Worker 
56*5a923131SAndroid Build Coastguard Worker   // Interprets |signature_proto| as a protocol buffer containing the
57*5a923131SAndroid Build Coastguard Worker   // |Signatures| message and decrypts each signature data using the stored
58*5a923131SAndroid Build Coastguard Worker   // public key. Pads the 32 bytes |sha256_hash_data| to 256 or 512 bytes
59*5a923131SAndroid Build Coastguard Worker   // according to the PKCS#1 v1.5 standard; and returns whether *any* of the
60*5a923131SAndroid Build Coastguard Worker   // decrypted hashes matches the padded hash data. In case of any error parsing
61*5a923131SAndroid Build Coastguard Worker   // the signatures, returns false.
62*5a923131SAndroid Build Coastguard Worker   bool VerifySignature(const std::string& signature_proto,
63*5a923131SAndroid Build Coastguard Worker                        const brillo::Blob& sha256_hash_data) const;
64*5a923131SAndroid Build Coastguard Worker 
65*5a923131SAndroid Build Coastguard Worker   // Verifies if |sig_data| is a raw signature of the hash |sha256_hash_data|.
66*5a923131SAndroid Build Coastguard Worker   // If PayloadVerifier is using RSA as the public key, further puts the
67*5a923131SAndroid Build Coastguard Worker   // decrypted data of |sig_data| into |decrypted_sig_data|.
68*5a923131SAndroid Build Coastguard Worker   bool VerifyRawSignature(const brillo::Blob& sig_data,
69*5a923131SAndroid Build Coastguard Worker                           const brillo::Blob& sha256_hash_data,
70*5a923131SAndroid Build Coastguard Worker                           brillo::Blob* decrypted_sig_data) const;
71*5a923131SAndroid Build Coastguard Worker 
72*5a923131SAndroid Build Coastguard Worker  private:
PayloadVerifier(std::vector<std::unique_ptr<EVP_PKEY,decltype (& EVP_PKEY_free)>> && public_keys)73*5a923131SAndroid Build Coastguard Worker   explicit PayloadVerifier(
74*5a923131SAndroid Build Coastguard Worker       std::vector<std::unique_ptr<EVP_PKEY, decltype(&EVP_PKEY_free)>>&&
75*5a923131SAndroid Build Coastguard Worker           public_keys)
76*5a923131SAndroid Build Coastguard Worker       : public_keys_(std::move(public_keys)) {}
77*5a923131SAndroid Build Coastguard Worker 
78*5a923131SAndroid Build Coastguard Worker   // Decrypts |sig_data| with the given |public_key| and populates
79*5a923131SAndroid Build Coastguard Worker   // |out_hash_data| with the decoded raw hash. Returns true if successful,
80*5a923131SAndroid Build Coastguard Worker   // false otherwise.
81*5a923131SAndroid Build Coastguard Worker   bool GetRawHashFromSignature(const brillo::Blob& sig_data,
82*5a923131SAndroid Build Coastguard Worker                                const EVP_PKEY* public_key,
83*5a923131SAndroid Build Coastguard Worker                                brillo::Blob* out_hash_data) const;
84*5a923131SAndroid Build Coastguard Worker 
85*5a923131SAndroid Build Coastguard Worker   std::vector<std::unique_ptr<EVP_PKEY, decltype(&EVP_PKEY_free)>> public_keys_;
86*5a923131SAndroid Build Coastguard Worker };
87*5a923131SAndroid Build Coastguard Worker 
88*5a923131SAndroid Build Coastguard Worker }  // namespace chromeos_update_engine
89*5a923131SAndroid Build Coastguard Worker 
90*5a923131SAndroid Build Coastguard Worker #endif  // UPDATE_ENGINE_PAYLOAD_CONSUMER_PAYLOAD_VERIFIER_H_
91