blob: b5d545724d8ef78dbaa22fdf124855d5eeebcee2 [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>
Alex Deymo923d8fa2014-07-15 17:58:51 -070023
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070024#include <brillo/secure_blob.h>
Tianjie Xu6cf830b2019-09-30 11:31:49 -070025#include <openssl/evp.h>
Ben Chan05735a12014-09-03 07:48:22 -070026
Alex Deymo923d8fa2014-07-15 17:58:51 -070027#include "update_engine/update_metadata.pb.h"
28
Tianjie Xu6cf830b2019-09-30 11:31:49 -070029// This class holds the public key and implements methods used for payload
30// signature verification. See payload_generator/payload_signer.h for payload
31// signing.
Alex Deymo923d8fa2014-07-15 17:58:51 -070032
33namespace chromeos_update_engine {
34
Alex Deymo923d8fa2014-07-15 17:58:51 -070035class PayloadVerifier {
36 public:
xunchangcda3c032019-03-26 15:41:14 -070037 // Pads a SHA256 hash so that it may be encrypted/signed with RSA2048 or
38 // RSA4096 using the PKCS#1 v1.5 scheme.
39 // hash should be a pointer to vector of exactly 256 bits. |rsa_size| must be
40 // one of 256 or 512 bytes. The vector will be modified in place and will
41 // result in having a length of 2048 or 4096 bits, depending on the rsa size.
42 // Returns true on success, false otherwise.
43 static bool PadRSASHA256Hash(brillo::Blob* hash, size_t rsa_size);
Alex Deymo923d8fa2014-07-15 17:58:51 -070044
Tianjie Xu6cf830b2019-09-30 11:31:49 -070045 // Parses the input as a PEM encoded public string. And creates a
46 // PayloadVerifier with that public key for signature verification.
47 static std::unique_ptr<PayloadVerifier> CreateInstance(
48 const std::string& pem_public_key);
49
50 // Interprets |signature_proto| as a protocol buffer containing the
51 // |Signatures| message and decrypts each signature data using the stored
52 // public key. Pads the 32 bytes |sha256_hash_data| to 256 or 512 bytes
53 // according to the PKCS#1 v1.5 standard; and returns whether *any* of the
54 // decrypted hashes matches the padded hash data. In case of any error parsing
55 // the signatures, returns false.
56 bool VerifySignature(const std::string& signature_proto,
57 const brillo::Blob& sha256_hash_data) const;
58
59 // Verifies if |sig_data| is a raw signature of the hash |sha256_hash_data|.
60 // If PayloadVerifier is using RSA as the public key, further puts the
61 // decrypted data of |sig_data| into |decrypted_sig_data|.
62 bool VerifyRawSignature(const brillo::Blob& sig_data,
63 const brillo::Blob& sha256_hash_data,
64 brillo::Blob* decrypted_sig_data) const;
65
Alex Deymo923d8fa2014-07-15 17:58:51 -070066 private:
Tianjie Xu6cf830b2019-09-30 11:31:49 -070067 explicit PayloadVerifier(
68 std::unique_ptr<EVP_PKEY, decltype(&EVP_PKEY_free)>&& public_key)
69 : public_key_(std::move(public_key)) {}
70
71 // Decrypts |sig_data| with the given |public_key| and populates
72 // |out_hash_data| with the decoded raw hash. Returns true if successful,
73 // false otherwise.
74 bool GetRawHashFromSignature(const brillo::Blob& sig_data,
75 const EVP_PKEY* public_key,
76 brillo::Blob* out_hash_data) const;
77
78 std::unique_ptr<EVP_PKEY, decltype(&EVP_PKEY_free)> public_key_{nullptr,
79 nullptr};
Alex Deymo923d8fa2014-07-15 17:58:51 -070080};
81
82} // namespace chromeos_update_engine
83
Alex Deymo39910dc2015-11-09 17:04:30 -080084#endif // UPDATE_ENGINE_PAYLOAD_CONSUMER_PAYLOAD_VERIFIER_H_