blob: faea31d121c134e8df44d9118a77986f667936bd [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
17#ifndef UPDATE_ENGINE_PAYLOAD_VERIFIER_H_
18#define UPDATE_ENGINE_PAYLOAD_VERIFIER_H_
19
20#include <string>
21#include <vector>
22
Ben Chan05735a12014-09-03 07:48:22 -070023#include <base/macros.h>
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080024#include <chromeos/secure_blob.h>
Ben Chan05735a12014-09-03 07:48:22 -070025
Alex Deymo923d8fa2014-07-15 17:58:51 -070026#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
31namespace chromeos_update_engine {
32
Alex Deymo923d8fa2014-07-15 17:58:51 -070033class PayloadVerifier {
34 public:
Alex Deymob552a682015-09-30 09:36:49 -070035 // Interprets |signature_blob| as a protocol buffer containing the Signatures
36 // message and decrypts each signature data using the |public_key_path|.
37 // Returns whether *any* of the decrypted hashes matches the |hash_data|.
38 // In case of any error parsing the signatures or the public key, returns
39 // false.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080040 static bool VerifySignature(const chromeos::Blob& signature_blob,
Alex Deymo923d8fa2014-07-15 17:58:51 -070041 const std::string& public_key_path,
Alex Deymob552a682015-09-30 09:36:49 -070042 const chromeos::Blob& hash_data);
Alex Deymo923d8fa2014-07-15 17:58:51 -070043
44 // Decrypts sig_data with the given public_key_path and populates
45 // out_hash_data with the decoded raw hash. Returns true if successful,
46 // false otherwise.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080047 static bool GetRawHashFromSignature(const chromeos::Blob& sig_data,
Alex Deymo923d8fa2014-07-15 17:58:51 -070048 const std::string& public_key_path,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080049 chromeos::Blob* out_hash_data);
Alex Deymo923d8fa2014-07-15 17:58:51 -070050
51 // Returns true if the payload in |payload_path| is signed and its hash can be
52 // verified using the public key in |public_key_path| with the signature
53 // of a given version in the signature blob. Returns false otherwise.
54 static bool VerifySignedPayload(const std::string& payload_path,
Alex Deymob552a682015-09-30 09:36:49 -070055 const std::string& public_key_path);
Alex Deymo923d8fa2014-07-15 17:58:51 -070056
57 // Pads a SHA256 hash so that it may be encrypted/signed with RSA2048
58 // using the PKCS#1 v1.5 scheme.
59 // hash should be a pointer to vector of exactly 256 bits. The vector
60 // will be modified in place and will result in having a length of
61 // 2048 bits. Returns true on success, false otherwise.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080062 static bool PadRSA2048SHA256Hash(chromeos::Blob* hash);
Alex Deymo923d8fa2014-07-15 17:58:51 -070063
64 // Reads the payload from the given |payload_path| into the |out_payload|
65 // vector. It also parses the manifest protobuf in the payload and returns it
66 // in |out_manifest| along with the size of the entire metadata in
67 // |out_metadata_size|.
68 static bool LoadPayload(const std::string& payload_path,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080069 chromeos::Blob* out_payload,
Alex Deymo923d8fa2014-07-15 17:58:51 -070070 DeltaArchiveManifest* out_manifest,
71 uint64_t* out_metadata_size);
72
73 private:
74 // This should never be constructed
75 DISALLOW_IMPLICIT_CONSTRUCTORS(PayloadVerifier);
76};
77
78} // namespace chromeos_update_engine
79
80#endif // UPDATE_ENGINE_PAYLOAD_VERIFIER_H_