|
| 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 |
0 commit comments