2016-04-25 18:48:19 +01:00
|
|
|
/* Copyright (c) 2016, Google Inc.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
|
|
|
|
2015-11-09 21:57:26 +00:00
|
|
|
#include <assert.h>
|
2017-03-29 22:12:47 +01:00
|
|
|
#include <stdlib.h>
|
2015-11-09 21:57:26 +00:00
|
|
|
|
2016-09-22 15:14:35 +01:00
|
|
|
#include <openssl/bio.h>
|
2017-07-01 00:11:22 +01:00
|
|
|
#include <openssl/bytestring.h>
|
2016-10-14 00:03:17 +01:00
|
|
|
#include <openssl/err.h>
|
2016-09-22 15:14:35 +01:00
|
|
|
#include <openssl/evp.h>
|
2016-03-02 03:57:32 +00:00
|
|
|
#include <openssl/rand.h>
|
2016-09-22 15:14:35 +01:00
|
|
|
#include <openssl/rsa.h>
|
2015-11-09 21:57:26 +00:00
|
|
|
#include <openssl/ssl.h>
|
2016-09-22 15:14:35 +01:00
|
|
|
#include <openssl/x509.h>
|
2015-11-09 21:57:26 +00:00
|
|
|
|
2017-07-01 00:11:22 +01:00
|
|
|
#include "../ssl/test/fuzzer.h"
|
2015-11-09 21:57:26 +00:00
|
|
|
|
|
|
|
|
2016-09-22 15:14:35 +01:00
|
|
|
static const uint8_t kOCSPResponse[] = {0x01, 0x02, 0x03, 0x04};
|
2017-03-29 22:12:47 +01:00
|
|
|
static const uint8_t kSCT[] = {0x00, 0x06, 0x00, 0x04, 0x05, 0x06, 0x07, 0x08};
|
2016-09-22 15:14:35 +01:00
|
|
|
|
|
|
|
static int ALPNSelectCallback(SSL *ssl, const uint8_t **out, uint8_t *out_len,
|
|
|
|
const uint8_t *in, unsigned in_len, void *arg) {
|
|
|
|
static const uint8_t kProtocol[] = {'a', 'a'};
|
|
|
|
*out = kProtocol;
|
|
|
|
*out_len = sizeof(kProtocol);
|
|
|
|
return SSL_TLSEXT_ERR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int NPNAdvertiseCallback(SSL *ssl, const uint8_t **out,
|
|
|
|
unsigned *out_len, void *arg) {
|
|
|
|
static const uint8_t kProtocols[] = {
|
|
|
|
0x01, 'a', 0x02, 'a', 'a', 0x03, 'a', 'a', 'a',
|
|
|
|
};
|
|
|
|
*out = kProtocols;
|
|
|
|
*out_len = sizeof(kProtocols);
|
|
|
|
return SSL_TLSEXT_ERR_OK;
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:57:26 +00:00
|
|
|
struct GlobalState {
|
|
|
|
GlobalState()
|
|
|
|
: ctx(SSL_CTX_new(SSLv23_method())) {
|
2017-07-01 00:11:22 +01:00
|
|
|
debug = getenv("BORINGSSL_FUZZER_DEBUG") != nullptr;
|
|
|
|
|
2015-11-09 21:57:26 +00:00
|
|
|
const uint8_t *bufp = kRSAPrivateKeyDER;
|
|
|
|
RSA *privkey = d2i_RSAPrivateKey(NULL, &bufp, sizeof(kRSAPrivateKeyDER));
|
|
|
|
assert(privkey != nullptr);
|
|
|
|
|
|
|
|
EVP_PKEY *pkey = EVP_PKEY_new();
|
|
|
|
EVP_PKEY_assign_RSA(pkey, privkey);
|
|
|
|
|
|
|
|
SSL_CTX_use_PrivateKey(ctx, pkey);
|
|
|
|
EVP_PKEY_free(pkey);
|
|
|
|
|
|
|
|
bufp = kCertificateDER;
|
|
|
|
X509 *cert = d2i_X509(NULL, &bufp, sizeof(kCertificateDER));
|
|
|
|
assert(cert != nullptr);
|
|
|
|
|
|
|
|
SSL_CTX_use_certificate(ctx, cert);
|
|
|
|
X509_free(cert);
|
2016-09-22 15:14:35 +01:00
|
|
|
|
2017-03-29 22:12:47 +01:00
|
|
|
if (!SSL_CTX_set_ocsp_response(ctx, kOCSPResponse, sizeof(kOCSPResponse)) ||
|
|
|
|
!SSL_CTX_set_signed_cert_timestamp_list(ctx, kSCT, sizeof(kSCT))) {
|
|
|
|
abort();
|
|
|
|
}
|
2016-09-22 15:14:35 +01:00
|
|
|
|
|
|
|
SSL_CTX_set_alpn_select_cb(ctx, ALPNSelectCallback, nullptr);
|
|
|
|
SSL_CTX_set_next_protos_advertised_cb(ctx, NPNAdvertiseCallback, nullptr);
|
2017-03-29 22:09:09 +01:00
|
|
|
SSL_CTX_set_early_data_enabled(ctx, 1);
|
2017-07-01 00:11:22 +01:00
|
|
|
|
|
|
|
// If accepting client certificates, allow any certificate.
|
|
|
|
SSL_CTX_set_cert_verify_callback(
|
|
|
|
ctx, [](X509_STORE_CTX *store_ctx, void *arg) -> int { return 1; },
|
|
|
|
nullptr);
|
2015-11-09 21:57:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~GlobalState() {
|
|
|
|
SSL_CTX_free(ctx);
|
|
|
|
}
|
|
|
|
|
2017-07-01 00:11:22 +01:00
|
|
|
bool debug;
|
2015-11-09 21:57:26 +00:00
|
|
|
SSL_CTX *const ctx;
|
|
|
|
};
|
|
|
|
|
|
|
|
static GlobalState g_state;
|
|
|
|
|
2016-10-12 15:35:18 +01:00
|
|
|
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len) {
|
2016-03-02 03:57:32 +00:00
|
|
|
RAND_reset_for_fuzzing();
|
|
|
|
|
2017-07-01 00:11:22 +01:00
|
|
|
CBS cbs;
|
|
|
|
CBS_init(&cbs, buf, len);
|
|
|
|
bssl::UniquePtr<SSL> server = SetupTest(&cbs, g_state.ctx, true);
|
|
|
|
if (!server) {
|
|
|
|
if (g_state.debug) {
|
|
|
|
fprintf(stderr, "Error parsing parameters.\n");
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2016-09-22 15:14:35 +01:00
|
|
|
|
2017-07-01 00:11:22 +01:00
|
|
|
SSL_set_max_proto_version(server.get(), TLS1_3_VERSION);
|
|
|
|
SSL_set_tls_channel_id_enabled(server.get(), 1);
|
|
|
|
|
|
|
|
// Enable ciphers that are off by default.
|
|
|
|
SSL_set_strict_cipher_list(server.get(), "ALL:NULL-SHA");
|
2016-09-22 15:14:35 +01:00
|
|
|
|
2015-11-09 21:57:26 +00:00
|
|
|
BIO *in = BIO_new(BIO_s_mem());
|
|
|
|
BIO *out = BIO_new(BIO_s_mem());
|
2017-07-01 00:11:22 +01:00
|
|
|
SSL_set_bio(server.get(), in, out); // Takes ownership of |in| and |out|.
|
2016-09-22 15:14:35 +01:00
|
|
|
|
2017-07-01 00:11:22 +01:00
|
|
|
BIO_write(in, CBS_data(&cbs), CBS_len(&cbs));
|
|
|
|
if (SSL_do_handshake(server.get()) == 1) {
|
2016-03-02 19:53:11 +00:00
|
|
|
// Keep reading application data until error or EOF.
|
|
|
|
uint8_t tmp[1024];
|
|
|
|
for (;;) {
|
2017-07-01 00:11:22 +01:00
|
|
|
if (SSL_read(server.get(), tmp, sizeof(tmp)) <= 0) {
|
2016-03-02 19:53:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-07-01 00:11:22 +01:00
|
|
|
} else if (g_state.debug) {
|
|
|
|
fprintf(stderr, "Handshake failed.\n");
|
2016-03-02 19:53:11 +00:00
|
|
|
}
|
2015-11-09 21:57:26 +00:00
|
|
|
|
2017-07-01 00:11:22 +01:00
|
|
|
if (g_state.debug) {
|
|
|
|
ERR_print_errors_fp(stderr);
|
|
|
|
}
|
2016-10-14 00:03:17 +01:00
|
|
|
ERR_clear_error();
|
2015-11-09 21:57:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|