| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2016 gRPC authors. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <grpc/grpc.h> |
| 20 |
#include <grpc/grpc_security.h> |
| 21 |
#include <grpc/support/log.h> |
| 22 |
|
| 23 |
#include "src/core/lib/iomgr/load_file.h" |
| 24 |
#include "src/core/lib/security/credentials/credentials.h" |
| 25 |
#include "src/core/lib/security/security_connector/security_connector.h" |
| 26 |
#include "test/core/util/mock_endpoint.h" |
| 27 |
|
| 28 |
#define CA_CERT_PATH "src/core/tsi/test_creds/ca.pem" |
| 29 |
#define SERVER_CERT_PATH "src/core/tsi/test_creds/server1.pem" |
| 30 |
#define SERVER_KEY_PATH "src/core/tsi/test_creds/server1.key" |
| 31 |
|
| 32 |
bool squelch = true; |
| 33 |
// ssl has an array of global gpr_mu's that are never released. |
| 34 |
// Turning this on will fail the leak check. |
| 35 |
bool leak_check = false; |
| 36 |
|
| 37 |
static void discard_write(grpc_slice /*slice*/) {} |
| 38 |
|
| 39 |
static void dont_log(gpr_log_func_args* /*args*/) {} |
| 40 |
|
| 41 |
struct handshake_state { |
| 42 |
bool done_callback_called; |
| 43 |
}; |
| 44 |
|
| 45 |
static void on_handshake_done(void* arg, grpc_error_handle error) { |
| 46 |
grpc_core::HandshakerArgs* args = |
| 47 |
static_cast<grpc_core::HandshakerArgs*>(arg); |
| 48 |
struct handshake_state* state = |
| 49 |
static_cast<struct handshake_state*>(args->user_data); |
| 50 |
GPR_ASSERT(state->done_callback_called == false); |
| 51 |
state->done_callback_called = true; |
| 52 |
// The fuzzer should not pass the handshake. |
| 53 |
GPR_ASSERT(!error.ok()); |
| 54 |
} |
| 55 |
|
| 56 |
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
| 57 |
if (squelch) gpr_set_log_function(dont_log); |
| 58 |
grpc_init(); |
| 59 |
{ |
| 60 |
grpc_core::ExecCtx exec_ctx; |
| 61 |
|
| 62 |
grpc_endpoint* mock_endpoint = grpc_mock_endpoint_create(discard_write); |
| 63 |
|
| 64 |
grpc_mock_endpoint_put_read( |
| 65 |
mock_endpoint, grpc_slice_from_copied_buffer((const char*)data, size)); |
| 66 |
|
| 67 |
// Load key pair and establish server SSL credentials. |
| 68 |
grpc_slice ca_slice, cert_slice, key_slice; |
| 69 |
GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file", |
| 70 |
grpc_load_file(CA_CERT_PATH, 1, &ca_slice))); |
| 71 |
GPR_ASSERT(GRPC_LOG_IF_ERROR( |
| 72 |
"load_file", grpc_load_file(SERVER_CERT_PATH, 1, &cert_slice))); |
| 73 |
GPR_ASSERT(GRPC_LOG_IF_ERROR( |
| 74 |
"load_file", grpc_load_file(SERVER_KEY_PATH, 1, &key_slice))); |
| 75 |
const char* ca_cert = |
| 76 |
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(ca_slice); |
| 77 |
const char* server_cert = |
| 78 |
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice); |
| 79 |
const char* server_key = |
| 80 |
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(key_slice); |
| 81 |
grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {server_key, server_cert}; |
| 82 |
grpc_server_credentials* creds = grpc_ssl_server_credentials_create( |
| 83 |
ca_cert, &pem_key_cert_pair, 1, 0, nullptr); |
| 84 |
grpc_slice_unref(cert_slice); |
| 85 |
grpc_slice_unref(key_slice); |
| 86 |
grpc_slice_unref(ca_slice); |
| 87 |
|
| 88 |
// Create security connector |
| 89 |
grpc_core::RefCountedPtr<grpc_server_security_connector> sc = |
| 90 |
creds->create_security_connector(grpc_core::ChannelArgs()); |
| 91 |
GPR_ASSERT(sc != nullptr); |
| 92 |
grpc_core::Timestamp deadline = |
| 93 |
grpc_core::Duration::Seconds(1) + grpc_core::Timestamp::Now(); |
| 94 |
|
| 95 |
struct handshake_state state; |
| 96 |
state.done_callback_called = false; |
| 97 |
auto handshake_mgr = |
| 98 |
grpc_core::MakeRefCounted<grpc_core::HandshakeManager>(); |
| 99 |
sc->add_handshakers(grpc_core::ChannelArgs(), nullptr, handshake_mgr.get()); |
| 100 |
handshake_mgr->DoHandshake(mock_endpoint, grpc_core::ChannelArgs(), |
| 101 |
deadline, nullptr /* acceptor */, |
| 102 |
on_handshake_done, &state); |
| 103 |
grpc_core::ExecCtx::Get()->Flush(); |
| 104 |
|
| 105 |
// If the given string happens to be part of the correct client hello, the |
| 106 |
// server will wait for more data. Explicitly fail the server by shutting |
| 107 |
// down the endpoint. |
| 108 |
if (!state.done_callback_called) { |
| 109 |
grpc_endpoint_shutdown(mock_endpoint, |
| 110 |
GRPC_ERROR_CREATE("Explicit close")); |
| 111 |
grpc_core::ExecCtx::Get()->Flush(); |
| 112 |
} |
| 113 |
GPR_ASSERT(state.done_callback_called); |
| 114 |
|
| 115 |
sc.reset(DEBUG_LOCATION, "test"); |
| 116 |
grpc_server_credentials_release(creds); |
| 117 |
grpc_core::ExecCtx::Get()->Flush(); |
| 118 |
} |
| 119 |
|
| 120 |
grpc_shutdown(); |
| 121 |
return 0; |
| 122 |
} |