@@ -69,6 +69,9 @@ inline std::string parsePemSignature(std::string_view pemSignature, std::pair<st
6969
7070} // namespace Detail
7171
72+ // / \brief The signature of the main verifySignature() function.
73+ using MainVerifyFunctionType = std::string (*)(std::string_view, std::string_view, std::string_view);
74+
7275/* !
7376 * \brief Verifies \a data with the specified public key \a publicKeyPem and signature \a signaturePem.
7477 * \returns Returns an empty string if \a data and \a signature are correct and an error message otherwise.
@@ -104,7 +107,6 @@ inline std::string parsePemSignature(std::string_view pemSignature, std::pair<st
104107inline std::string verifySignature (std::string_view publicKeyPem, std::string_view signaturePem, std::string_view data)
105108{
106109 auto error = std::string ();
107-
108110 auto derSignature = std::pair<std::unique_ptr<std::uint8_t []>, std::uint32_t >();
109111 if (error = Detail::parsePemSignature (signaturePem, derSignature); !error.empty ()) {
110112 return error;
@@ -149,6 +151,27 @@ inline std::string verifySignature(std::string_view publicKeyPem, std::string_vi
149151 return error;
150152}
151153
154+ /* !
155+ * \brief Verifies \a data with the specified public keys \a publicKeysPem and signature \a signaturePem.
156+ * \returns Returns an empty string if \a data and \a signature are correct and an error message otherwise.
157+ * \remarks
158+ * - This is a version of verifySignature() that takes more than one public key trying out different keys.
159+ * This allows rotating keys once in a while without breaking verification by temporarily allowing the
160+ * old and new key at the same time.
161+ */
162+ template <class Keys , class VerifyFunction = MainVerifyFunctionType>
163+ inline std::string verifySignature (Keys &&publicKeysPem, std::string_view signaturePem, std::string_view data,
164+ VerifyFunction &&verifyFunction = static_cast <MainVerifyFunctionType>(&verifySignature))
165+ {
166+ auto error = std::string (" no keys provided" );
167+ for (const auto publicKeyPem : publicKeysPem) {
168+ if ((error = verifyFunction (publicKeyPem, signaturePem, data)).empty ()) {
169+ return error;
170+ }
171+ }
172+ return error;
173+ }
174+
152175} // namespace CppUtilities
153176
154177#endif // CPP_UTILITIES_MISC_VERIFICATION_H
0 commit comments