Skip to content

Commit 9b122e7

Browse files
committed
Ported to OpenSSL
1 parent 0318597 commit 9b122e7

File tree

5 files changed

+47
-13
lines changed

5 files changed

+47
-13
lines changed

BUILD

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ cc_library(
3838
],
3939
)
4040

41+
cc_library(
42+
name = "openssl-crypto",
43+
srcs = [
44+
"libcrypto.so.1.1",
45+
],
46+
visibility = ["//visibility:public"],
47+
linkstatic=False,
48+
)
49+
4150
cc_library(
4251
name = "base_lib",
4352
srcs = [
@@ -58,7 +67,7 @@ cc_library(
5867
],
5968
deps = [
6069
":headers",
61-
"@boringssl//:crypto",
70+
"@openssl//:openssl-crypto",
6271
],
6372
)
6473

WORKSPACE

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,9 @@ proxy_wasm_cpp_host_dependencies()
1111
load("@rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_dependencies")
1212

1313
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 & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,6 @@ def proxy_wasm_cpp_host_repositories():
2222
urls = ["https://github.com/proxy-wasm/proxy-wasm-cpp-sdk/archive/fd0be8405db25de0264bdb78fae3a82668c03782.tar.gz"],
2323
)
2424

25-
http_archive(
26-
name = "boringssl",
27-
sha256 = "bb55b0ed2f0cb548b5dce6a6b8307ce37f7f748eb9f1be6bfe2d266ff2b4d52b",
28-
strip_prefix = "boringssl-2192bbc878822cf6ab5977d4257a1339453d9d39",
29-
urls = ["https://github.com/google/boringssl/archive/2192bbc878822cf6ab5977d4257a1339453d9d39.tar.gz"],
30-
)
31-
3225
http_archive(
3326
name = "com_google_googletest",
3427
sha256 = "9dc9157a9a1551ec7a7e43daea9a694a0bb5fb8bec81235d8a1e6ef64c716dcb",

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: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include <array>
1818
#include <cstring>
1919

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

2323
#include "include/proxy-wasm/bytecode_util.h"
@@ -90,6 +90,7 @@ bool SignatureUtil::verifySignature(std::string_view bytecode, std::string &mess
9090
}
9191

9292
const auto *signature = reinterpret_cast<const uint8_t *>(payload.data()) + sizeof(uint32_t);
93+
const auto sig_len = payload.size() - sizeof(uint32_t);
9394

9495
SHA512_CTX ctx;
9596
SHA512_Init(&ctx);
@@ -103,13 +104,28 @@ bool SignatureUtil::verifySignature(std::string_view bytecode, std::string &mess
103104

104105
static const auto ed25519_pubkey = hex2pubkey<32>(PROXY_WASM_VERIFY_WITH_ED25519_PUBKEY);
105106

106-
if (!ED25519_verify(hash, sizeof(hash), signature, ed25519_pubkey.data())) {
107+
bool retval = true;
108+
EVP_MD_CTX* mctx(EVP_MD_CTX_new());
109+
EVP_PKEY* key(EVP_PKEY_new_raw_public_key(EVP_PKEY_ED25519, NULL, static_cast<const unsigned char*>(ed25519_pubkey.data()), ed25519_pubkey.size()));
110+
111+
if (key == nullptr) {
112+
message = "Failed to load ed25519 public key";
113+
retval = false;
114+
}
115+
if (retval && (1 != EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, key))) {
116+
message = "Failed to initialize ed25519 digest verify";
117+
retval = false;
118+
}
119+
if (retval && !EVP_DigestVerify(mctx, signature, sig_len, hash, sizeof(hash))) {
107120
message = "Signature mismatch";
108-
return false;
121+
retval = false;
109122
}
110123

111-
message = "Wasm signature OK (Ed25519)";
112-
return true;
124+
EVP_PKEY_free(key);
125+
EVP_MD_CTX_free(mctx);
126+
127+
if (retval) message = "Wasm signature OK (Ed25519)";
128+
return retval;
113129

114130
#endif
115131

0 commit comments

Comments
 (0)