blob: 1c4a3eb48a2724a2eb073cd627fd034100f92459 [file] [log] [blame]
Andrew de los Reyes0c440052010-08-20 11:25:54 -07001// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_SIGNER_H__
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_SIGNER_H__
7
8#include <string>
9#include <vector>
Andrew de los Reyes0c440052010-08-20 11:25:54 -070010
Darin Petkov9574f7e2011-01-13 10:48:12 -080011#include <base/basictypes.h>
12
13// This class encapsulates methods used for payload signing and signature
14// verification. See update_metadata.proto for more info.
Andrew de los Reyes0c440052010-08-20 11:25:54 -070015
16namespace chromeos_update_engine {
17
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070018extern const uint32_t kSignatureMessageOriginalVersion;
19extern const uint32_t kSignatureMessageCurrentVersion;
Andrew de los Reyes0c440052010-08-20 11:25:54 -070020
21class PayloadSigner {
22 public:
Darin Petkov9574f7e2011-01-13 10:48:12 -080023 // Given a raw |hash| and a private key in |private_key_path| calculates the
24 // raw signature in |out_signature|. Returns true on success, false otherwise.
25 static bool SignHash(const std::vector<char>& hash,
26 const std::string& private_key_path,
27 std::vector<char>* out_signature);
28
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070029 // Given an unsigned payload in |unsigned_payload_path| and private keys in
Darin Petkov9574f7e2011-01-13 10:48:12 -080030 // |private_key_path|, calculates the signature blob into
31 // |out_signature_blob|. Note that the payload must already have an updated
32 // manifest that includes the dummy signature op. Returns true on success,
33 // false otherwise.
Andrew de los Reyes0c440052010-08-20 11:25:54 -070034 static bool SignPayload(const std::string& unsigned_payload_path,
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070035 const std::vector<std::string>& private_key_paths,
Andrew de los Reyes0c440052010-08-20 11:25:54 -070036 std::vector<char>* out_signature_blob);
37
38 // Returns the length of out_signature_blob that will result in a call
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070039 // to SignPayload with the given private keys. Returns true on success.
40 static bool SignatureBlobLength(
41 const std::vector<std::string>& private_key_paths,
42 uint64_t* out_length);
Andrew de los Reyes0c440052010-08-20 11:25:54 -070043
Darin Petkov9574f7e2011-01-13 10:48:12 -080044 // Given an unsigned payload in |payload_path| (with no dummy signature op)
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070045 // and the raw |signature_sizes| calculates the raw hash that needs to be
Darin Petkov9574f7e2011-01-13 10:48:12 -080046 // signed in |out_hash_data|. Returns true on success, false otherwise.
47 static bool HashPayloadForSigning(const std::string& payload_path,
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070048 const std::vector<int>& signature_sizes,
Darin Petkov9574f7e2011-01-13 10:48:12 -080049 std::vector<char>* out_hash_data);
50
51 // Given an unsigned payload in |payload_path| (with no dummy signature op)
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070052 // and the raw |signatures| updates the payload to include the signature thus
Darin Petkov9574f7e2011-01-13 10:48:12 -080053 // turning it into a signed payload. The new payload is stored in
54 // |signed_payload_path|. |payload_path| and |signed_payload_path| can point
55 // to the same file. Returns true on success, false otherwise.
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070056 static bool AddSignatureToPayload(
57 const std::string& payload_path,
58 const std::vector<std::vector<char> >& signatures,
59 const std::string& signed_payload_path);
Darin Petkov9574f7e2011-01-13 10:48:12 -080060
Darin Petkovadb3cef2011-01-13 16:16:08 -080061 // Returns false if the payload signature can't be verified. Returns true
62 // otherwise and sets |out_hash| to the signed payload hash.
63 static bool VerifySignature(const std::vector<char>& signature_blob,
64 const std::string& public_key_path,
65 std::vector<char>* out_hash_data);
66
Jay Srinivasan51dcf262012-09-13 17:24:32 -070067 // Interprets signature_blob as a protocol buffer containing the Signatures
68 // message and decrypts the signature data using the public_key_path and
69 // stores the resultant raw hash data in out_hash_data. Returns true if
70 // everything is successful. False otherwise. It also takes the client_version
71 // and interprets the signature blob according to that version.
72 static bool VerifySignatureBlob(const std::vector<char>& signature_blob,
73 const std::string& public_key_path,
74 uint32_t client_version,
75 std::vector<char>* out_hash_data);
76
77 // Decrypts sig_data with the given public_key_path and populates
78 // out_hash_data with the decoded raw hash. Returns true if successful,
79 // false otherwise.
80 static bool GetRawHashFromSignature(const std::vector<char>& sig_data,
81 const std::string& public_key_path,
82 std::vector<char>* out_hash_data);
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070083
Darin Petkovadb3cef2011-01-13 16:16:08 -080084 // Returns true if the payload in |payload_path| is signed and its hash can be
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070085 // verified using the public key in |public_key_path| with the signature
86 // of a given version in the signature blob. Returns false otherwise.
Darin Petkovadb3cef2011-01-13 16:16:08 -080087 static bool VerifySignedPayload(const std::string& payload_path,
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070088 const std::string& public_key_path,
89 uint32_t client_key_check_version);
Darin Petkovadb3cef2011-01-13 16:16:08 -080090
Andrew de los Reyesbdfaaf02011-03-30 10:35:12 -070091 // Pads a SHA256 hash so that it may be encrypted/signed with RSA2048
92 // using the PKCS#1 v1.5 scheme.
93 // hash should be a pointer to vector of exactly 256 bits. The vector
94 // will be modified in place and will result in having a length of
95 // 2048 bits. Returns true on success, false otherwise.
96 static bool PadRSA2048SHA256Hash(std::vector<char>* hash);
Jay Srinivasan51dcf262012-09-13 17:24:32 -070097
98 static bool GetManifestSignature(const char* manifest,
99 size_t manifest_size,
100 const std::string& private_key_path,
101 std::string* out_signature);
102
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700103 private:
104 // This should never be constructed
105 DISALLOW_IMPLICIT_CONSTRUCTORS(PayloadSigner);
106};
107
108} // namespace chromeos_update_engine
109
110#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_SIGNER_H__