1919#include < memory>
2020
2121#ifdef PROXY_WASM_VERIFY_WITH_ED25519_PUBKEY
22+ #include " absl/cleanup/cleanup.h"
2223#include < openssl/evp.h>
2324#endif
2425
@@ -306,22 +307,28 @@ bool SignatureUtil::verifySignature(std::string_view bytecode, std::string &mess
306307 return false ;
307308 }
308309
309- uint8_t computed_hash[32 ]; // SHA-256 produces 32 bytes
310+ absl::Cleanup free_hash_ctx = [hash_ctx] { EVP_MD_CTX_free (hash_ctx); };
311+
312+ uint8_t computed_hash[EVP_MAX_MD_SIZE];
310313 unsigned int hash_len = 0 ;
311314
312- bool hash_ok = (EVP_DigestInit_ex (hash_ctx, EVP_sha256 (), nullptr ) != 0 ) &&
313- (EVP_DigestUpdate (hash_ctx, content_start, content_len) != 0 ) &&
314- (EVP_DigestFinal_ex (hash_ctx, computed_hash, &hash_len) != 0 );
315+ if (EVP_DigestInit_ex (hash_ctx, EVP_sha256 (), nullptr ) == 0 ) {
316+ message = " Failed to compute SHA-256 hash" ;
317+ return false ;
318+ }
315319
316- EVP_MD_CTX_free (hash_ctx);
320+ if (EVP_DigestUpdate (hash_ctx, content_start, content_len) == 0 ) {
321+ message = " Failed to compute SHA-256 hash" ;
322+ return false ;
323+ }
317324
318- if (!hash_ok || hash_len != 32 ) {
325+ if (EVP_DigestFinal_ex (hash_ctx, computed_hash, & hash_len) == 0 ) {
319326 message = " Failed to compute SHA-256 hash" ;
320327 return false ;
321328 }
322329
323330 // Verify the computed hash matches the expected hash
324- if (std::memcmp (computed_hash, expected_hash, 32 ) != 0 ) {
331+ if (std::memcmp (computed_hash, expected_hash, hash_len ) != 0 ) {
325332 message = " Hash mismatch" ;
326333 return false ;
327334 }
@@ -333,14 +340,14 @@ bool SignatureUtil::verifySignature(std::string_view bytecode, std::string &mess
333340 // https://github.com/wasm-signatures/wasmsign2/blob/0.2.6/src/lib/src/signature/multi.rs#L268-L278
334341 const char *domain = " wasmsig" ;
335342 size_t domain_len = 7 ;
336- size_t msg_len = domain_len + 3 + 32 ; // domain + 3 bytes (spec/content/hash) + 32 bytes ( hash)
343+ size_t msg_len = domain_len + 3 + hash_len ; // domain + 3 bytes (spec/content/hash) + hash
337344 auto signature_msg = std::make_unique<uint8_t []>(msg_len);
338345
339346 std::memcpy (signature_msg.get (), domain, domain_len);
340347 signature_msg[domain_len] = spec_version;
341348 signature_msg[domain_len + 1 ] = content_type;
342349 signature_msg[domain_len + 2 ] = hash_fn;
343- std::memcpy (signature_msg.get () + domain_len + 3 , expected_hash, 32 );
350+ std::memcpy (signature_msg.get () + domain_len + 3 , expected_hash, hash_len );
344351
345352 static const auto ed25519_pubkey = hex2pubkey<32 >(PROXY_WASM_VERIFY_WITH_ED25519_PUBKEY);
346353
@@ -351,21 +358,23 @@ bool SignatureUtil::verifySignature(std::string_view bytecode, std::string &mess
351358 return false ;
352359 }
353360
361+ absl::Cleanup free_pubkey = [pubkey] { EVP_PKEY_free (pubkey); };
362+
354363 EVP_MD_CTX *mdctx = EVP_MD_CTX_new ();
355364 if (mdctx == nullptr ) {
356365 message = " Failed to allocate memory for EVP_MD_CTX" ;
357- EVP_PKEY_free (pubkey);
358366 return false ;
359367 }
360368
361- bool ok = (EVP_DigestVerifyInit (mdctx, nullptr , nullptr , nullptr , pubkey) != 0 ) &&
362- (EVP_DigestVerify (mdctx, signature, 64 /* ED25519_SIGNATURE_LEN */ , signature_msg.get (),
363- msg_len) != 0 );
369+ absl::Cleanup free_mdctx = [mdctx] { EVP_MD_CTX_free (mdctx); };
364370
365- EVP_MD_CTX_free (mdctx);
366- EVP_PKEY_free (pubkey);
371+ if (EVP_DigestVerifyInit (mdctx, nullptr , nullptr , nullptr , pubkey) == 0 ) {
372+ message = " Failed to initialize signature verification" ;
373+ return false ;
374+ }
367375
368- if (!ok) {
376+ if (EVP_DigestVerify (mdctx, signature, 64 /* ED25519_SIGNATURE_LEN */ , signature_msg.get (),
377+ msg_len) == 0 ) {
369378 message = " Signature mismatch" ;
370379 return false ;
371380 }
0 commit comments