Skip to content

Commit 4540d32

Browse files
authored
Allow using system's crypto library instead of BoringSSL. (#219)
Use: bazel test --define crypto=system //test/... Signed-off-by: Piotr Sikora <piotrsikora@google.com>
1 parent fd40710 commit 4540d32

File tree

4 files changed

+51
-6
lines changed

4 files changed

+51
-6
lines changed

.github/workflows/cpp.yml

+14-2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ jobs:
6969
- name: 'V8 on Linux'
7070
runtime: 'v8'
7171
os: ubuntu-20.04
72+
flags: '--define crypto=system'
7273
- name: 'V8 on macOS'
7374
runtime: 'v8'
7475
os: macos-11
@@ -110,11 +111,22 @@ jobs:
110111

111112
- name: Test
112113
run: |
113-
bazel test --test_output=errors --define runtime=${{ matrix.runtime }} //test/...
114+
bazel test \
115+
--verbose_failures \
116+
--test_output=errors \
117+
--define runtime=${{ matrix.runtime }} \
118+
${{ matrix.flags }} \
119+
//test/...
114120
115121
- name: Test (signed Wasm module)
116122
run: |
117-
bazel test --test_output=errors --define runtime=${{ matrix.runtime }} --per_file_copt=src/signature_util.cc,test/signature_util_test.cc@-DPROXY_WASM_VERIFY_WITH_ED25519_PUBKEY=\"$(xxd -p -c 256 test/test_data/signature_key1.pub | cut -b9-)\" //test:signature_util_test
123+
bazel test \
124+
--verbose_failures \
125+
--test_output=errors \
126+
--define runtime=${{ matrix.runtime }} \
127+
${{ matrix.flags }} \
128+
--per_file_copt=src/signature_util.cc,test/signature_util_test.cc@-DPROXY_WASM_VERIFY_WITH_ED25519_PUBKEY=\"$(xxd -p -c 256 test/test_data/signature_key1.pub | cut -b9-)\" \
129+
//test:signature_util_test
118130
119131
- name: Cleanup Bazel cache
120132
if: matrix.runtime != 'wasmtime'

BUILD

+8-2
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,16 @@ cc_library(
5656
"include/proxy-wasm/bytecode_util.h",
5757
"include/proxy-wasm/signature_util.h",
5858
],
59+
linkopts = select({
60+
"//bazel:crypto_system": ["-lcrypto"],
61+
"//conditions:default": [],
62+
}),
5963
deps = [
6064
":headers",
61-
"@boringssl//:crypto",
62-
],
65+
] + select({
66+
"//bazel:crypto_system": [],
67+
"//conditions:default": ["@boringssl//:crypto"],
68+
}),
6369
)
6470

6571
cc_library(

bazel/BUILD

+5
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,8 @@ config_setting(
1717
name = "runtime_wavm",
1818
values = {"define": "runtime=wavm"},
1919
)
20+
21+
config_setting(
22+
name = "crypto_system",
23+
values = {"define": "crypto=system"},
24+
)

src/signature_util.cc

+24-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
#include <array>
1818
#include <cstring>
1919

20-
#include <openssl/curve25519.h>
20+
#ifdef PROXY_WASM_VERIFY_WITH_ED25519_PUBKEY
21+
#include <openssl/evp.h>
2122
#include <openssl/sha.h>
23+
#endif
2224

2325
#include "include/proxy-wasm/bytecode_util.h"
2426

@@ -103,7 +105,27 @@ bool SignatureUtil::verifySignature(std::string_view bytecode, std::string &mess
103105

104106
static const auto ed25519_pubkey = hex2pubkey<32>(PROXY_WASM_VERIFY_WITH_ED25519_PUBKEY);
105107

106-
if (!ED25519_verify(hash, sizeof(hash), signature, ed25519_pubkey.data())) {
108+
EVP_PKEY *pubkey = EVP_PKEY_new_raw_public_key(EVP_PKEY_ED25519, nullptr, ed25519_pubkey.data(),
109+
32 /* ED25519_PUBLIC_KEY_LEN */);
110+
if (!pubkey) {
111+
message = "Failed to load the public key";
112+
return false;
113+
}
114+
115+
EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
116+
if (!mdctx) {
117+
message = "Failed to allocate memory for EVP_MD_CTX";
118+
EVP_PKEY_free(pubkey);
119+
return false;
120+
}
121+
122+
bool ok = EVP_DigestVerifyInit(mdctx, nullptr, nullptr, nullptr, pubkey) &&
123+
EVP_DigestVerify(mdctx, signature, 64 /* ED25519_SIGNATURE_LEN */, hash, sizeof(hash));
124+
125+
EVP_MD_CTX_free(mdctx);
126+
EVP_PKEY_free(pubkey);
127+
128+
if (!ok) {
107129
message = "Signature mismatch";
108130
return false;
109131
}

0 commit comments

Comments
 (0)