blob: bc5231fa7865dd29d66dc2b9316fc84470f3dbf0 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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 Deymo923d8fa2014-07-15 17:58:51 -070016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#ifndef UPDATE_ENGINE_PAYLOAD_CONSUMER_PAYLOAD_VERIFIER_H_
18#define UPDATE_ENGINE_PAYLOAD_CONSUMER_PAYLOAD_VERIFIER_H_
Alex Deymo923d8fa2014-07-15 17:58:51 -070019
Tianjie Xu6cf830b2019-09-30 11:31:49 -070020#include <memory>
Alex Deymo923d8fa2014-07-15 17:58:51 -070021#include <string>
Tianjie Xu6cf830b2019-09-30 11:31:49 -070022#include <utility>
Tianjie Xu7a78d632019-10-08 16:32:39 -070023#include <vector>
Alex Deymo923d8fa2014-07-15 17:58:51 -070024
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070025#include <brillo/secure_blob.h>
Tianjie Xu6cf830b2019-09-30 11:31:49 -070026#include <openssl/evp.h>
Ben Chan05735a12014-09-03 07:48:22 -070027
Alex Deymo923d8fa2014-07-15 17:58:51 -070028#include "update_engine/update_metadata.pb.h"
29
Tianjie Xu7a78d632019-10-08 16:32:39 -070030// This class holds the public keys and implements methods used for payload
Tianjie Xu6cf830b2019-09-30 11:31:49 -070031// signature verification. See payload_generator/payload_signer.h for payload
32// signing.
Alex Deymo923d8fa2014-07-15 17:58:51 -070033
34namespace chromeos_update_engine {
35
Alex Deymo923d8fa2014-07-15 17:58:51 -070036class PayloadVerifier {
37 public:
xunchangcda3c032019-03-26 15:41:14 -070038 // 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 Deymo923d8fa2014-07-15 17:58:51 -070045
Tianjie Xu6cf830b2019-09-30 11:31:49 -070046 // 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 Xu7a78d632019-10-08 16:32:39 -070051 // 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 Xu6cf830b2019-09-30 11:31:49 -070056 // 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 Deymo923d8fa2014-07-15 17:58:51 -070072 private:
Tianjie Xu6cf830b2019-09-30 11:31:49 -070073 explicit PayloadVerifier(
Tianjie Xu7a78d632019-10-08 16:32:39 -070074 std::vector<std::unique_ptr<EVP_PKEY, decltype(&EVP_PKEY_free)>>&&
75 public_keys)
76 : public_keys_(std::move(public_keys)) {}
Tianjie Xu6cf830b2019-09-30 11:31:49 -070077
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 Xu7a78d632019-10-08 16:32:39 -070085 std::vector<std::unique_ptr<EVP_PKEY, decltype(&EVP_PKEY_free)>> public_keys_;
Alex Deymo923d8fa2014-07-15 17:58:51 -070086};
87
88} // namespace chromeos_update_engine
89
Alex Deymo39910dc2015-11-09 17:04:30 -080090#endif // UPDATE_ENGINE_PAYLOAD_CONSUMER_PAYLOAD_VERIFIER_H_