Skip to content

Commit 141498a

Browse files
dmitri-dOtto van der Schaaf
authored andcommitted
Ported to OpenSSL
1 parent 694a0b0 commit 141498a

File tree

5 files changed

+48
-34
lines changed

5 files changed

+48
-34
lines changed

BUILD

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ cc_library(
4444
],
4545
)
4646

47+
cc_library(
48+
name = "openssl-crypto",
49+
srcs = [
50+
"libcrypto.so.1.1",
51+
],
52+
visibility = ["//visibility:public"],
53+
linkstatic=False,
54+
)
55+
4756
cc_library(
4857
name = "base_lib",
4958
srcs = [
@@ -70,7 +79,7 @@ cc_library(
7079
":headers",
7180
] + select({
7281
"//bazel:crypto_system": [],
73-
"//conditions:default": ["@boringssl//:crypto"],
82+
"//conditions:default": ["@openssl//:openssl-crypto"],
7483
}),
7584
alwayslink = 1,
7685
)

WORKSPACE

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,13 @@ proxy_wasm_cpp_host_repositories()
77
load("@proxy_wasm_cpp_host//bazel:dependencies.bzl", "proxy_wasm_cpp_host_dependencies")
88

99
proxy_wasm_cpp_host_dependencies()
10+
11+
load("@rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_dependencies")
12+
13+
rules_foreign_cc_dependencies()
14+
15+
new_local_repository(
16+
name = "openssl",
17+
path = "/usr/lib64/",
18+
build_file = "openssl.BUILD"
19+
)

bazel/repositories.bzl

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,6 @@ def proxy_wasm_cpp_host_repositories():
7676

7777
# Core.
7878

79-
maybe(
80-
http_archive,
81-
name = "boringssl",
82-
# 2022-02-07 (master-with-bazel)
83-
sha256 = "7dec97795a7ac7e3832228e4440ee06cceb18d3663f4580b0840e685281e28a0",
84-
strip_prefix = "boringssl-eaa29f431f71b8121e1da76bcd3ddc2248238ade",
85-
urls = ["https://github.com/google/boringssl/archive/eaa29f431f71b8121e1da76bcd3ddc2248238ade.tar.gz"],
86-
)
87-
8879
maybe(
8980
http_archive,
9081
name = "proxy_wasm_cpp_sdk",

openssl.BUILD

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
licenses(["notice"]) # Apache 2
2+
3+
cc_library(
4+
name = "openssl-crypto",
5+
srcs = [
6+
"libcrypto.so.1.1",
7+
],
8+
visibility = ["//visibility:public"],
9+
linkstatic=False,
10+
)

src/signature_util.cc

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include <array>
1818
#include <cstring>
1919

20-
#ifdef PROXY_WASM_VERIFY_WITH_ED25519_PUBKEY
2120
#include <openssl/evp.h>
2221
#include <openssl/sha.h>
2322
#endif
@@ -94,6 +93,7 @@ bool SignatureUtil::verifySignature(std::string_view bytecode, std::string &mess
9493
}
9594

9695
const auto *signature = reinterpret_cast<const uint8_t *>(payload.data()) + sizeof(uint32_t);
96+
const auto sig_len = payload.size() - sizeof(uint32_t);
9797

9898
SHA512_CTX ctx;
9999
SHA512_Init(&ctx);
@@ -107,34 +107,28 @@ bool SignatureUtil::verifySignature(std::string_view bytecode, std::string &mess
107107

108108
static const auto ed25519_pubkey = hex2pubkey<32>(PROXY_WASM_VERIFY_WITH_ED25519_PUBKEY);
109109

110-
EVP_PKEY *pubkey = EVP_PKEY_new_raw_public_key(EVP_PKEY_ED25519, nullptr, ed25519_pubkey.data(),
111-
32 /* ED25519_PUBLIC_KEY_LEN */);
112-
if (pubkey == nullptr) {
113-
message = "Failed to load the public key";
114-
return false;
115-
}
110+
bool retval = true;
111+
EVP_MD_CTX* mctx(EVP_MD_CTX_new());
112+
EVP_PKEY* key(EVP_PKEY_new_raw_public_key(EVP_PKEY_ED25519, NULL, static_cast<const unsigned char*>(ed25519_pubkey.data()), ed25519_pubkey.size()));
116113

117-
EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
118-
if (mdctx == nullptr) {
119-
message = "Failed to allocate memory for EVP_MD_CTX";
120-
EVP_PKEY_free(pubkey);
121-
return false;
114+
if (key == nullptr) {
115+
message = "Failed to load ed25519 public key";
116+
retval = false;
122117
}
123-
124-
bool ok =
125-
(EVP_DigestVerifyInit(mdctx, nullptr, nullptr, nullptr, pubkey) != 0) &&
126-
(EVP_DigestVerify(mdctx, signature, 64 /* ED25519_SIGNATURE_LEN */, hash, sizeof(hash)) != 0);
127-
128-
EVP_MD_CTX_free(mdctx);
129-
EVP_PKEY_free(pubkey);
130-
131-
if (!ok) {
118+
if (retval && (1 != EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, key))) {
119+
message = "Failed to initialize ed25519 digest verify";
120+
retval = false;
121+
}
122+
if (retval && !EVP_DigestVerify(mctx, signature, sig_len, hash, sizeof(hash))) {
132123
message = "Signature mismatch";
133-
return false;
124+
retval = false;
134125
}
135126

136-
message = "Wasm signature OK (Ed25519)";
137-
return true;
127+
EVP_PKEY_free(key);
128+
EVP_MD_CTX_free(mctx);
129+
130+
if (retval) message = "Wasm signature OK (Ed25519)";
131+
return retval;
138132

139133
#endif
140134

0 commit comments

Comments
 (0)