Skip to content

Commit 6b830b0

Browse files
committed
Add support for loading and verifying signed Wasm modules.
Signed-off-by: Piotr Sikora <[email protected]>
1 parent 8ccb826 commit 6b830b0

File tree

3 files changed

+162
-0
lines changed

3 files changed

+162
-0
lines changed

include/proxy-wasm/signature_util.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
17+
#include <string_view>
18+
19+
namespace proxy_wasm {
20+
21+
// Utility functions to verify Wasm signatures.
22+
class SignatureUtil {
23+
public:
24+
/**
25+
* verifySignature validates Wasm signature.
26+
* @param bytecode is the source bytecode.
27+
* @param message is the reference to store the message (success or error).
28+
* @return indicates whether the bytecode has a valid Wasm signature.
29+
*/
30+
static bool verifySignature(std::string_view bytecode, std::string &message);
31+
};
32+
33+
} // namespace proxy_wasm

src/signature_util.cc

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "include/proxy-wasm/signature_util.h"
16+
17+
#include <array>
18+
#include <string>
19+
20+
#include <openssl/curve25519.h>
21+
#include <openssl/sha.h>
22+
23+
#include "include/proxy-wasm/bytecode_util.h"
24+
25+
namespace {
26+
27+
#ifdef PROXY_WASM_VERIFY_WITH_ED25519_PUBKEY
28+
29+
static uint8_t hex2dec(const unsigned char c) {
30+
if (c >= '0' && c <= '9') {
31+
return c - '0';
32+
} else if (c >= 'a' && c <= 'f') {
33+
return c - 'a' + 10;
34+
} else if (c >= 'A' && c <= 'F') {
35+
return c - 'A' + 10;
36+
} else {
37+
throw std::logic_error{"invalid hex character"};
38+
}
39+
}
40+
41+
template <size_t N> constexpr std::array<uint8_t, N> hex2pubkey(const char (&hex)[2 * N + 1]) {
42+
std::array<uint8_t, N> pubkey;
43+
for (size_t i = 0; i < pubkey.size(); i++) {
44+
pubkey[i] = hex2dec(hex[2 * i]) << 4 | hex2dec(hex[2 * i + 1]);
45+
}
46+
return pubkey;
47+
}
48+
49+
#endif
50+
51+
} // namespace
52+
53+
namespace proxy_wasm {
54+
55+
bool SignatureUtil::verifySignature(std::string_view bytecode, std::string &message) {
56+
57+
#ifdef PROXY_WASM_VERIFY_WITH_ED25519_PUBKEY
58+
59+
/*
60+
* Ed25519 signature generated using https://github.com/jedisct1/wasmsign
61+
*/
62+
63+
std::string_view payload;
64+
if (!BytecodeUtil::getCustomSection(bytecode, "signature_wasmsign", payload)) {
65+
message = "Failed to parse corrupted Wasm module";
66+
return false;
67+
}
68+
69+
if (payload.empty()) {
70+
message = "Custom Section \"signature_wasmsign\" not found";
71+
return false;
72+
}
73+
74+
if (bytecode.data() + bytecode.size() != payload.data() + payload.size()) {
75+
message = "Custom Section \"signature_wasmsign\" not at the end of Wasm module";
76+
return false;
77+
}
78+
79+
if (payload.size() != 68) {
80+
message = "Signature has a wrong size (want: 68, is: " + std::to_string(payload.size()) + ")";
81+
return false;
82+
}
83+
84+
uint32_t alg_id;
85+
std::memcpy(&alg_id, payload.data(), sizeof(uint32_t));
86+
87+
if (alg_id != 2) {
88+
message = "Signature has a wrong alg_id (want: 2, is: " + std::to_string(alg_id) + ")";
89+
return false;
90+
}
91+
92+
const auto *signature = reinterpret_cast<const uint8_t *>(payload.data()) + sizeof(uint32_t);
93+
94+
SHA512_CTX ctx;
95+
SHA512_Init(&ctx);
96+
SHA512_Update(&ctx, "WasmSignature", sizeof("WasmSignature") - 1);
97+
const uint32_t ad_len = 0;
98+
SHA512_Update(&ctx, &ad_len, sizeof(uint32_t));
99+
const size_t section_len = 3 + sizeof("signature_wasmsign") - 1 + 68;
100+
SHA512_Update(&ctx, bytecode.data(), bytecode.size() - section_len);
101+
uint8_t hash[SHA512_DIGEST_LENGTH];
102+
SHA512_Final(hash, &ctx);
103+
104+
static const auto ed25519_pubkey = hex2pubkey<32>(PROXY_WASM_VERIFY_WITH_ED25519_PUBKEY);
105+
106+
if (!ED25519_verify(hash, sizeof(hash), signature, ed25519_pubkey.data())) {
107+
message = "Signature mismatch";
108+
return false;
109+
}
110+
111+
message = "Wasm signature OK (Ed25519)";
112+
return true;
113+
114+
#endif
115+
116+
return true;
117+
}
118+
119+
} // namespace proxy_wasm

src/wasm.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <unordered_map>
2828

2929
#include "include/proxy-wasm/bytecode_util.h"
30+
#include "include/proxy-wasm/signature_util.h"
3031
#include "include/proxy-wasm/vm_id_handle.h"
3132

3233
#include "src/third_party/base64.h"
@@ -250,6 +251,15 @@ bool WasmBase::load(const std::string &code, bool allow_precompiled) {
250251
return true;
251252
}
252253

254+
// Verify signature.
255+
std::string message;
256+
if (!SignatureUtil::verifySignature(code, message)) {
257+
fail(FailState::UnableToInitializeCode, message);
258+
return false;
259+
} else {
260+
wasm_vm_->integration()->trace(message);
261+
}
262+
253263
// Get ABI version from the module.
254264
if (!BytecodeUtil::getAbiVersion(code, abi_version_)) {
255265
fail(FailState::UnableToInitializeCode, "Failed to parse corrupted Wasm module");

0 commit comments

Comments
 (0)