payload_signer.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // Copyright (C) 2010 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. //
  16. #ifndef UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_SIGNER_H_
  17. #define UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_SIGNER_H_
  18. #include <string>
  19. #include <vector>
  20. #include <base/macros.h>
  21. #include <brillo/key_value_store.h>
  22. #include <brillo/secure_blob.h>
  23. #include "update_engine/update_metadata.pb.h"
  24. // This class encapsulates methods used for payload signing.
  25. // See update_metadata.proto for more info.
  26. namespace chromeos_update_engine {
  27. class PayloadSigner {
  28. public:
  29. // Returns true if the payload in |payload_path| is signed and its hash can be
  30. // verified using the public key in |public_key_path| with the signature
  31. // of a given version in the signature blob. Returns false otherwise.
  32. static bool VerifySignedPayload(const std::string& payload_path,
  33. const std::string& public_key_path);
  34. // Adds specified signature offset/length to given |manifest|, also adds a
  35. // dummy operation that points to a signature blob located at the specified
  36. // offset/length if |add_dummy_op| is true.
  37. static void AddSignatureToManifest(uint64_t signature_blob_offset,
  38. uint64_t signature_blob_length,
  39. bool add_dummy_op,
  40. DeltaArchiveManifest* manifest);
  41. // Given a raw |hash| and a private key in |private_key_path| calculates the
  42. // raw signature in |out_signature|. Returns true on success, false otherwise.
  43. static bool SignHash(const brillo::Blob& hash,
  44. const std::string& private_key_path,
  45. brillo::Blob* out_signature);
  46. // Sign |hash_data| blob with all private keys in |private_key_paths|, then
  47. // convert the signatures to serialized protobuf.
  48. static bool SignHashWithKeys(
  49. const brillo::Blob& hash_data,
  50. const std::vector<std::string>& private_key_paths,
  51. std::string* out_serialized_signature);
  52. // Given an unsigned payload in |unsigned_payload_path|, private keys in
  53. // |private_key_path|, metadata size in |metadata_size|, metadata signature
  54. // size in |metadata_signature_size| and signatures offset in
  55. // |signatures_offset|, calculates the payload signature blob into
  56. // |out_serialized_signature|. Note that the payload must already have an
  57. // updated manifest that includes the dummy signature op and correct metadata
  58. // signature size in header. Returns true on success, false otherwise.
  59. static bool SignPayload(const std::string& unsigned_payload_path,
  60. const std::vector<std::string>& private_key_paths,
  61. const uint64_t metadata_size,
  62. const uint32_t metadata_signature_size,
  63. const uint64_t signatures_offset,
  64. std::string* out_serialized_signature);
  65. // Returns the length of out_serialized_signature that will result in a call
  66. // to SignPayload with the given private keys. Returns true on success.
  67. static bool SignatureBlobLength(
  68. const std::vector<std::string>& private_key_paths, uint64_t* out_length);
  69. // Given an unsigned payload in |payload_path|,
  70. // this method does two things:
  71. // 1. It loads the payload into memory, and inserts placeholder signature
  72. // operations and placeholder metadata signature to make the header and
  73. // the manifest match what the final signed payload will look like based
  74. // on |signatures_sizes|, if needed.
  75. // 2. It calculates the raw SHA256 hash of the payload and the metadata in
  76. // |payload_path| (except signatures) and returns the result in
  77. // |out_hash_data| and |out_metadata_hash| respectively.
  78. //
  79. // The changes to payload are not preserved or written to disk.
  80. static bool HashPayloadForSigning(const std::string& payload_path,
  81. const std::vector<int>& signature_sizes,
  82. brillo::Blob* out_payload_hash_data,
  83. brillo::Blob* out_metadata_hash);
  84. // Given an unsigned payload in |payload_path| (with no dummy signature op)
  85. // and the raw |payload_signatures| and |metadata_signatures| updates the
  86. // payload to include the signature thus turning it into a signed payload. The
  87. // new payload is stored in |signed_payload_path|. |payload_path| and
  88. // |signed_payload_path| can point to the same file. Populates
  89. // |out_metadata_size| with the size of the metadata after adding the
  90. // signature operation in the manifest. Returns true on success, false
  91. // otherwise.
  92. static bool AddSignatureToPayload(
  93. const std::string& payload_path,
  94. const std::vector<brillo::Blob>& payload_signatures,
  95. const std::vector<brillo::Blob>& metadata_signatures,
  96. const std::string& signed_payload_path,
  97. uint64_t* out_metadata_size);
  98. // Computes the SHA256 hash of the first metadata_size bytes of |metadata|
  99. // and signs the hash with the given private_key_path and writes the signed
  100. // hash in |out_signature|. Returns true if successful or false if there was
  101. // any error in the computations.
  102. static bool GetMetadataSignature(const void* const metadata,
  103. size_t metadata_size,
  104. const std::string& private_key_path,
  105. std::string* out_signature);
  106. static bool ExtractPayloadProperties(const std::string& payload_path,
  107. brillo::KeyValueStore* properties);
  108. private:
  109. // This should never be constructed
  110. DISALLOW_IMPLICIT_CONSTRUCTORS(PayloadSigner);
  111. };
  112. } // namespace chromeos_update_engine
  113. #endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_SIGNER_H_