Skip to content

Commit cb87620

Browse files
authored
Migrate from deprecated SHA256_* to EVP API (#489)
* Migrate hash.cc from deprecated SHA256_* to EVP API --------- Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
1 parent 6196f97 commit cb87620

4 files changed

Lines changed: 53 additions & 25 deletions

File tree

BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ cc_library(
8989
}),
9090
deps = [
9191
":headers",
92+
"@com_google_absl//absl/cleanup",
9293
] + select({
9394
"//bazel:crypto_system": [],
9495
"//conditions:default": ["@boringssl//:crypto"],

src/hash.cc

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
#include <string>
1818
#include <vector>
1919

20-
#include <openssl/sha.h>
20+
#include "absl/cleanup/cleanup.h"
21+
#include <openssl/evp.h>
2122

2223
namespace proxy_wasm {
2324

@@ -37,14 +38,31 @@ std::string BytesToHex(const std::vector<uint8_t> &bytes) {
3738
} // namespace
3839

3940
std::vector<uint8_t> Sha256(const std::vector<std::string_view> &parts) {
40-
uint8_t sha256[SHA256_DIGEST_LENGTH];
41-
SHA256_CTX sha_ctx;
42-
SHA256_Init(&sha_ctx);
43-
for (auto part : parts) {
44-
SHA256_Update(&sha_ctx, part.data(), part.size());
41+
uint8_t sha256[EVP_MAX_MD_SIZE];
42+
unsigned int hash_len = 0;
43+
44+
EVP_MD_CTX *hash_ctx = EVP_MD_CTX_new();
45+
if (hash_ctx == nullptr) {
46+
return std::vector<uint8_t>();
4547
}
46-
SHA256_Final(sha256, &sha_ctx);
47-
return std::vector<uint8_t>(std::begin(sha256), std::end(sha256));
48+
49+
absl::Cleanup free_ctx = [hash_ctx] { EVP_MD_CTX_free(hash_ctx); };
50+
51+
if (EVP_DigestInit_ex(hash_ctx, EVP_sha256(), nullptr) == 0) {
52+
return std::vector<uint8_t>();
53+
}
54+
55+
for (const auto &part : parts) {
56+
if (EVP_DigestUpdate(hash_ctx, part.data(), part.size()) == 0) {
57+
return std::vector<uint8_t>();
58+
}
59+
}
60+
61+
if (EVP_DigestFinal_ex(hash_ctx, sha256, &hash_len) == 0) {
62+
return std::vector<uint8_t>();
63+
}
64+
65+
return std::vector<uint8_t>(sha256, sha256 + hash_len);
4866
}
4967

5068
std::string Sha256String(const std::vector<std::string_view> &parts) {

src/hash.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include <string>
1818
#include <vector>
1919

20-
#include <openssl/sha.h>
20+
#include <openssl/evp.h>
2121

2222
namespace proxy_wasm {
2323

src/signature_util.cc

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
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

Comments
 (0)