blob: 08135d29b3b5b00d2094655371a4b7c0c7d37232 [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>
Jay Srinivasanf4318702012-09-24 11:56:24 -070012#include "update_engine/update_metadata.pb.h"
Darin Petkov9574f7e2011-01-13 10:48:12 -080013
14// This class encapsulates methods used for payload signing and signature
15// verification. See update_metadata.proto for more info.
Andrew de los Reyes0c440052010-08-20 11:25:54 -070016
17namespace chromeos_update_engine {
18
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070019extern const uint32_t kSignatureMessageOriginalVersion;
20extern const uint32_t kSignatureMessageCurrentVersion;
Andrew de los Reyes0c440052010-08-20 11:25:54 -070021
22class PayloadSigner {
23 public:
Darin Petkov9574f7e2011-01-13 10:48:12 -080024 // Given a raw |hash| and a private key in |private_key_path| calculates the
25 // raw signature in |out_signature|. Returns true on success, false otherwise.
26 static bool SignHash(const std::vector<char>& hash,
27 const std::string& private_key_path,
28 std::vector<char>* out_signature);
29
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070030 // Given an unsigned payload in |unsigned_payload_path| and private keys in
Darin Petkov9574f7e2011-01-13 10:48:12 -080031 // |private_key_path|, calculates the signature blob into
32 // |out_signature_blob|. Note that the payload must already have an updated
33 // manifest that includes the dummy signature op. Returns true on success,
34 // false otherwise.
Andrew de los Reyes0c440052010-08-20 11:25:54 -070035 static bool SignPayload(const std::string& unsigned_payload_path,
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070036 const std::vector<std::string>& private_key_paths,
Andrew de los Reyes0c440052010-08-20 11:25:54 -070037 std::vector<char>* out_signature_blob);
38
39 // Returns the length of out_signature_blob that will result in a call
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070040 // to SignPayload with the given private keys. Returns true on success.
41 static bool SignatureBlobLength(
42 const std::vector<std::string>& private_key_paths,
43 uint64_t* out_length);
Andrew de los Reyes0c440052010-08-20 11:25:54 -070044
Jay Srinivasanf4318702012-09-24 11:56:24 -070045 // Given an unsigned payload in |payload_path| (with no dummy signature op),
46 // this method does two things:
47 // 1. It calculates the raw SHA256 hash of the entire payload in
48 // |payload_path| and returns the result in |out_hash_data|.
49 // 2. But before calculating the hash, it also inserts a dummy signature
50 // operation at the end of the manifest. This signature operation points to a
51 // blob offset and length that'll be later filled in by paygen with the
52 // actual signature from signer. But since the hash includes the signature
53 // operation, the signature operation has to be inserted before we compute
54 // the hash. Note, the hash doesn't cover the signature itself - it covers
55 // only the signature operation in manifest. Since paygen may add multiple
56 // signatures of different sizes (1024, 2048, etc.) it specifies a list
57 // of those |signature_sizes| so that the signature blob length is
58 // calculated to be of the correct size before the hash is computed.
59 // Returns true on success, false otherwise.
Darin Petkov9574f7e2011-01-13 10:48:12 -080060 static bool HashPayloadForSigning(const std::string& payload_path,
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070061 const std::vector<int>& signature_sizes,
Darin Petkov9574f7e2011-01-13 10:48:12 -080062 std::vector<char>* out_hash_data);
63
Jay Srinivasanf4318702012-09-24 11:56:24 -070064 // Given an unsigned payload in |payload_path|, calculates the raw hash of
65 // just the metadata (not the entire payload) that needs to be signed in
66 // |out_hash_data|. Note: This method should be called only after calling the
67 // HashPayloadForSigning method so that the metadata hash is computed on the
68 // final metadata that will be in the payload (because HashPayloadForSigning
69 // adds a dummy signature operation to the manifest).
70 // Returns true on success, false otherwise.
71 static bool HashMetadataForSigning(const std::string& payload_path,
72 std::vector<char>* out_metadata_hash);
73
Darin Petkov9574f7e2011-01-13 10:48:12 -080074 // Given an unsigned payload in |payload_path| (with no dummy signature op)
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070075 // and the raw |signatures| updates the payload to include the signature thus
Darin Petkov9574f7e2011-01-13 10:48:12 -080076 // turning it into a signed payload. The new payload is stored in
77 // |signed_payload_path|. |payload_path| and |signed_payload_path| can point
78 // to the same file. Returns true on success, false otherwise.
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070079 static bool AddSignatureToPayload(
80 const std::string& payload_path,
81 const std::vector<std::vector<char> >& signatures,
82 const std::string& signed_payload_path);
Darin Petkov9574f7e2011-01-13 10:48:12 -080083
Darin Petkovadb3cef2011-01-13 16:16:08 -080084 // Returns false if the payload signature can't be verified. Returns true
85 // otherwise and sets |out_hash| to the signed payload hash.
86 static bool VerifySignature(const std::vector<char>& signature_blob,
87 const std::string& public_key_path,
88 std::vector<char>* out_hash_data);
89
Jay Srinivasan51dcf262012-09-13 17:24:32 -070090 // Interprets signature_blob as a protocol buffer containing the Signatures
91 // message and decrypts the signature data using the public_key_path and
92 // stores the resultant raw hash data in out_hash_data. Returns true if
93 // everything is successful. False otherwise. It also takes the client_version
94 // and interprets the signature blob according to that version.
95 static bool VerifySignatureBlob(const std::vector<char>& signature_blob,
96 const std::string& public_key_path,
97 uint32_t client_version,
98 std::vector<char>* out_hash_data);
99
100 // Decrypts sig_data with the given public_key_path and populates
101 // out_hash_data with the decoded raw hash. Returns true if successful,
102 // false otherwise.
103 static bool GetRawHashFromSignature(const std::vector<char>& sig_data,
104 const std::string& public_key_path,
105 std::vector<char>* out_hash_data);
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700106
Darin Petkovadb3cef2011-01-13 16:16:08 -0800107 // Returns true if the payload in |payload_path| is signed and its hash can be
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700108 // verified using the public key in |public_key_path| with the signature
109 // of a given version in the signature blob. Returns false otherwise.
Darin Petkovadb3cef2011-01-13 16:16:08 -0800110 static bool VerifySignedPayload(const std::string& payload_path,
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700111 const std::string& public_key_path,
112 uint32_t client_key_check_version);
Darin Petkovadb3cef2011-01-13 16:16:08 -0800113
Andrew de los Reyesbdfaaf02011-03-30 10:35:12 -0700114 // Pads a SHA256 hash so that it may be encrypted/signed with RSA2048
115 // using the PKCS#1 v1.5 scheme.
116 // hash should be a pointer to vector of exactly 256 bits. The vector
117 // will be modified in place and will result in having a length of
118 // 2048 bits. Returns true on success, false otherwise.
119 static bool PadRSA2048SHA256Hash(std::vector<char>* hash);
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700120
Jay Srinivasanf4318702012-09-24 11:56:24 -0700121 // Computes the SHA256 hash of the first metadata_size bytes of |metadata|
122 // and signs the hash with the given private_key_path and writes the signed
123 // hash in |out_signature|. Returns true if successful or false if there was
124 // any error in the computations.
125 static bool GetMetadataSignature(const char* const metadata,
126 size_t metadata_size,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700127 const std::string& private_key_path,
128 std::string* out_signature);
129
Jay Srinivasanf4318702012-09-24 11:56:24 -0700130 // Reads the payload from the given |payload_path| into the |out_payload|
131 // vector. It also parses the manifest protobuf in the payload and returns it
132 // in |out_manifest| along with the size of the entire metadata in
133 // |out_metadata_size|.
134 static bool LoadPayload(const std::string& payload_path,
135 std::vector<char>* out_payload,
136 DeltaArchiveManifest* out_manifest,
137 uint64_t* out_metadata_size);
138
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700139 private:
140 // This should never be constructed
141 DISALLOW_IMPLICIT_CONSTRUCTORS(PayloadSigner);
142};
143
144} // namespace chromeos_update_engine
145
146#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_SIGNER_H__