| 1 |
/* Copyright (c) 2016, Google Inc. |
| 2 |
* |
| 3 |
* Permission to use, copy, modify, and/or distribute this software for any |
| 4 |
* purpose with or without fee is hereby granted, provided that the above |
| 5 |
* copyright notice and this permission notice appear in all copies. |
| 6 |
* |
| 7 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 |
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 |
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 |
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 |
|
| 15 |
#include <assert.h> |
| 16 |
|
| 17 |
#include <openssl/rand.h> |
| 18 |
#include <openssl/ssl.h> |
| 19 |
|
| 20 |
struct GlobalState { |
| 21 |
GlobalState() : ctx(SSL_CTX_new(SSLv23_method())) {} |
| 22 |
|
| 23 |
~GlobalState() { |
| 24 |
SSL_CTX_free(ctx); |
| 25 |
} |
| 26 |
|
| 27 |
SSL_CTX *const ctx; |
| 28 |
}; |
| 29 |
|
| 30 |
static GlobalState g_state; |
| 31 |
|
| 32 |
extern "C" int LLVMFuzzerTestOneInput(uint8_t *buf, size_t len) { |
| 33 |
RAND_reset_for_fuzzing(); |
| 34 |
|
| 35 |
SSL *client = SSL_new(g_state.ctx); |
| 36 |
BIO *in = BIO_new(BIO_s_mem()); |
| 37 |
BIO *out = BIO_new(BIO_s_mem()); |
| 38 |
SSL_set_bio(client, in, out); |
| 39 |
SSL_set_connect_state(client); |
| 40 |
SSL_set_renegotiate_mode(client, ssl_renegotiate_freely); |
| 41 |
|
| 42 |
BIO_write(in, buf, len); |
| 43 |
if (SSL_do_handshake(client) == 1) { |
| 44 |
// Keep reading application data until error or EOF. |
| 45 |
uint8_t tmp[1024]; |
| 46 |
for (;;) { |
| 47 |
if (SSL_read(client, tmp, sizeof(tmp)) <= 0) { |
| 48 |
break; |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
SSL_free(client); |
| 53 |
|
| 54 |
return 0; |
| 55 |
} |