blob: 6e7965c11c9382f393a52dc7ef061c9720ce85b1 [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
Jay Srinivasan738fdf32012-12-07 17:40:54 -080078 // to the same file. Populates |out_metadata_size| with the size of the
79 // metadata after adding the signature operation in the manifest.Returns true
80 // on success, false otherwise.
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070081 static bool AddSignatureToPayload(
82 const std::string& payload_path,
83 const std::vector<std::vector<char> >& signatures,
Jay Srinivasan738fdf32012-12-07 17:40:54 -080084 const std::string& signed_payload_path,
85 uint64_t* out_metadata_size);
Darin Petkov9574f7e2011-01-13 10:48:12 -080086
Darin Petkovadb3cef2011-01-13 16:16:08 -080087 // Returns false if the payload signature can't be verified. Returns true
88 // otherwise and sets |out_hash| to the signed payload hash.
89 static bool VerifySignature(const std::vector<char>& signature_blob,
90 const std::string& public_key_path,
91 std::vector<char>* out_hash_data);
92
Jay Srinivasan51dcf262012-09-13 17:24:32 -070093 // Interprets signature_blob as a protocol buffer containing the Signatures
94 // message and decrypts the signature data using the public_key_path and
95 // stores the resultant raw hash data in out_hash_data. Returns true if
96 // everything is successful. False otherwise. It also takes the client_version
97 // and interprets the signature blob according to that version.
98 static bool VerifySignatureBlob(const std::vector<char>& signature_blob,
99 const std::string& public_key_path,
100 uint32_t client_version,
101 std::vector<char>* out_hash_data);
102
103 // Decrypts sig_data with the given public_key_path and populates
104 // out_hash_data with the decoded raw hash. Returns true if successful,
105 // false otherwise.
106 static bool GetRawHashFromSignature(const std::vector<char>& sig_data,
107 const std::string& public_key_path,
108 std::vector<char>* out_hash_data);
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700109
Darin Petkovadb3cef2011-01-13 16:16:08 -0800110 // Returns true if the payload in |payload_path| is signed and its hash can be
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700111 // verified using the public key in |public_key_path| with the signature
112 // of a given version in the signature blob. Returns false otherwise.
Darin Petkovadb3cef2011-01-13 16:16:08 -0800113 static bool VerifySignedPayload(const std::string& payload_path,
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700114 const std::string& public_key_path,
115 uint32_t client_key_check_version);
Darin Petkovadb3cef2011-01-13 16:16:08 -0800116
Andrew de los Reyesbdfaaf02011-03-30 10:35:12 -0700117 // Pads a SHA256 hash so that it may be encrypted/signed with RSA2048
118 // using the PKCS#1 v1.5 scheme.
119 // hash should be a pointer to vector of exactly 256 bits. The vector
120 // will be modified in place and will result in having a length of
121 // 2048 bits. Returns true on success, false otherwise.
122 static bool PadRSA2048SHA256Hash(std::vector<char>* hash);
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700123
Jay Srinivasanf4318702012-09-24 11:56:24 -0700124 // Computes the SHA256 hash of the first metadata_size bytes of |metadata|
125 // and signs the hash with the given private_key_path and writes the signed
126 // hash in |out_signature|. Returns true if successful or false if there was
127 // any error in the computations.
128 static bool GetMetadataSignature(const char* const metadata,
129 size_t metadata_size,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700130 const std::string& private_key_path,
131 std::string* out_signature);
132
Jay Srinivasanf4318702012-09-24 11:56:24 -0700133 // Reads the payload from the given |payload_path| into the |out_payload|
134 // vector. It also parses the manifest protobuf in the payload and returns it
135 // in |out_manifest| along with the size of the entire metadata in
136 // |out_metadata_size|.
137 static bool LoadPayload(const std::string& payload_path,
138 std::vector<char>* out_payload,
139 DeltaArchiveManifest* out_manifest,
140 uint64_t* out_metadata_size);
141
Andrew de los Reyes0c440052010-08-20 11:25:54 -0700142 private:
143 // This should never be constructed
144 DISALLOW_IMPLICIT_CONSTRUCTORS(PayloadSigner);
145};
146
147} // namespace chromeos_update_engine
148
149#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_SIGNER_H__