payload_signer.h revision f9cb98c0cacccca803db48502c7bd461d04cb6b0
1// 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 UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_SIGNER_H_ 6#define UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_SIGNER_H_ 7 8#include <string> 9#include <vector> 10 11#include <base/macros.h> 12 13#include "update_engine/update_metadata.pb.h" 14 15// This class encapsulates methods used for payload signing. 16// See update_metadata.proto for more info. 17 18namespace chromeos_update_engine { 19 20class PayloadSigner { 21 public: 22 // Given a raw |hash| and a private key in |private_key_path| calculates the 23 // raw signature in |out_signature|. Returns true on success, false otherwise. 24 static bool SignHash(const std::vector<char>& hash, 25 const std::string& private_key_path, 26 std::vector<char>* out_signature); 27 28 // Given an unsigned payload in |unsigned_payload_path| and private keys in 29 // |private_key_path|, calculates the signature blob into 30 // |out_signature_blob|. Note that the payload must already have an updated 31 // manifest that includes the dummy signature op. Returns true on success, 32 // false otherwise. 33 static bool SignPayload(const std::string& unsigned_payload_path, 34 const std::vector<std::string>& private_key_paths, 35 std::vector<char>* out_signature_blob); 36 37 // Returns the length of out_signature_blob that will result in a call 38 // to SignPayload with the given private keys. Returns true on success. 39 static bool SignatureBlobLength( 40 const std::vector<std::string>& private_key_paths, 41 uint64_t* out_length); 42 43 // This is a helper method for HashPayloadforSigning and 44 // HashMetadataForSigning. It loads the payload into memory, and inserts 45 // signature placeholders if Signatures aren't already present. 46 static bool PrepPayloadForHashing( 47 const std::string& payload_path, 48 const std::vector<int>& signature_sizes, 49 std::vector<char>* payload_out, 50 uint64_t* metadata_size_out, 51 uint64_t* signatures_offset_out); 52 53 // Given an unsigned payload in |payload_path|, 54 // this method does two things: 55 // 1. Uses PrepPayloadForHashing to inserts placeholder signature operations 56 // to make the manifest match what the final signed payload will look 57 // like based on |signatures_sizes|, if needed. 58 // 2. It calculates the raw SHA256 hash of the payload in |payload_path| 59 // (except signatures) and returns the result in |out_hash_data|. 60 // 61 // The dummy signatures are not preserved or written to disk. 62 static bool HashPayloadForSigning(const std::string& payload_path, 63 const std::vector<int>& signature_sizes, 64 std::vector<char>* out_hash_data); 65 66 // Given an unsigned payload in |payload_path|, 67 // this method does two things: 68 // 1. Uses PrepPayloadForHashing to inserts placeholder signature operations 69 // to make the manifest match what the final signed payload will look 70 // like based on |signatures_sizes|, if needed. 71 // 2. It calculates the raw SHA256 hash of the metadata from the payload in 72 // |payload_path| (except signatures) and returns the result in 73 // |out_metadata_hash|. 74 // 75 // The dummy signatures are not preserved or written to disk. 76 static bool HashMetadataForSigning(const std::string& payload_path, 77 const std::vector<int>& signature_sizes, 78 std::vector<char>* out_metadata_hash); 79 80 // Given an unsigned payload in |payload_path| (with no dummy signature op) 81 // and the raw |signatures| updates the payload to include the signature thus 82 // turning it into a signed payload. The new payload is stored in 83 // |signed_payload_path|. |payload_path| and |signed_payload_path| can point 84 // to the same file. Populates |out_metadata_size| with the size of the 85 // metadata after adding the signature operation in the manifest.Returns true 86 // on success, false otherwise. 87 static bool AddSignatureToPayload( 88 const std::string& payload_path, 89 const std::vector<std::vector<char>>& signatures, 90 const std::string& signed_payload_path, 91 uint64_t* out_metadata_size); 92 93 // Computes the SHA256 hash of the first metadata_size bytes of |metadata| 94 // and signs the hash with the given private_key_path and writes the signed 95 // hash in |out_signature|. Returns true if successful or false if there was 96 // any error in the computations. 97 static bool GetMetadataSignature(const char* const metadata, 98 size_t metadata_size, 99 const std::string& private_key_path, 100 std::string* out_signature); 101 102 private: 103 // This should never be constructed 104 DISALLOW_IMPLICIT_CONSTRUCTORS(PayloadSigner); 105}; 106 107} // namespace chromeos_update_engine 108 109#endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_SIGNER_H_ 110