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-07-01 00:11:22 +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>
|
|
|
|
|
2017-07-01 00:11:22 +01:00
|
|
|
#include "../ssl/test/fuzzer.h"
|
2016-09-22 15:14:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
static const uint8_t kALPNProtocols[] = {
|
|
|
|
0x01, 'a', 0x02, 'a', 'a', 0x03, 'a', 'a', 'a',
|
|
|
|
};
|
|
|
|
|
|
|
|
static const uint8_t kP256KeyPKCS8[] = {
|
|
|
|
0x30, 0x81, 0x87, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86,
|
|
|
|
0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
|
|
|
|
0x03, 0x01, 0x07, 0x04, 0x6d, 0x30, 0x6b, 0x02, 0x01, 0x01, 0x04, 0x20,
|
|
|
|
0x43, 0x09, 0xc0, 0x67, 0x75, 0x21, 0x47, 0x9d, 0xa8, 0xfa, 0x16, 0xdf,
|
|
|
|
0x15, 0x73, 0x61, 0x34, 0x68, 0x6f, 0xe3, 0x8e, 0x47, 0x91, 0x95, 0xab,
|
|
|
|
0x79, 0x4a, 0x72, 0x14, 0xcb, 0xe2, 0x49, 0x4f, 0xa1, 0x44, 0x03, 0x42,
|
|
|
|
0x00, 0x04, 0xde, 0x09, 0x08, 0x07, 0x03, 0x2e, 0x8f, 0x37, 0x9a, 0xd5,
|
|
|
|
0xad, 0xe5, 0xc6, 0x9d, 0xd4, 0x63, 0xc7, 0x4a, 0xe7, 0x20, 0xcb, 0x90,
|
|
|
|
0xa0, 0x1f, 0x18, 0x18, 0x72, 0xb5, 0x21, 0x88, 0x38, 0xc0, 0xdb, 0xba,
|
|
|
|
0xf6, 0x99, 0xd8, 0xa5, 0x3b, 0x83, 0xe9, 0xe3, 0xd5, 0x61, 0x99, 0x73,
|
|
|
|
0x42, 0xc6, 0x6c, 0xe8, 0x0a, 0x95, 0x40, 0x41, 0x3b, 0x0d, 0x10, 0xa7,
|
|
|
|
0x4a, 0x93, 0xdb, 0x5a, 0xe7, 0xec,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int NPNSelectCallback(SSL *ssl, uint8_t **out, uint8_t *out_len,
|
|
|
|
const uint8_t *in, unsigned in_len, void *arg) {
|
|
|
|
static const uint8_t kProtocol[] = {'a', 'a'};
|
|
|
|
*out = const_cast<uint8_t*>(kProtocol);
|
|
|
|
*out_len = sizeof(kProtocol);
|
|
|
|
return SSL_TLSEXT_ERR_OK;
|
|
|
|
}
|
2015-11-09 21:57:26 +00:00
|
|
|
|
|
|
|
struct GlobalState {
|
2016-09-22 15:14:35 +01:00
|
|
|
GlobalState() : ctx(SSL_CTX_new(SSLv23_method())) {
|
2017-07-01 00:11:22 +01:00
|
|
|
debug = getenv("BORINGSSL_FUZZER_DEBUG") != nullptr;
|
|
|
|
|
2016-09-22 15:14:35 +01: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);
|
|
|
|
|
|
|
|
SSL_CTX_set_next_proto_select_cb(ctx, NPNSelectCallback, nullptr);
|
|
|
|
|
2017-07-01 00:11:22 +01:00
|
|
|
SSL_CTX_set_early_data_enabled(ctx, 1);
|
|
|
|
|
2016-09-22 15:14:35 +01:00
|
|
|
CBS cbs;
|
|
|
|
CBS_init(&cbs, kP256KeyPKCS8, sizeof(kP256KeyPKCS8));
|
|
|
|
pkey = EVP_parse_private_key(&cbs);
|
|
|
|
assert(pkey != nullptr);
|
|
|
|
SSL_CTX_set1_tls_channel_id(ctx, pkey);
|
|
|
|
EVP_PKEY_free(pkey);
|
|
|
|
}
|
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> client = SetupTest(&cbs, g_state.ctx, false);
|
|
|
|
if (!client) {
|
|
|
|
if (g_state.debug) {
|
|
|
|
fprintf(stderr, "Error parsing parameters.\n");
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SSL_set_renegotiate_mode(client.get(), ssl_renegotiate_freely);
|
|
|
|
SSL_set_max_proto_version(client.get(), TLS1_3_VERSION);
|
2017-07-01 05:39:14 +01:00
|
|
|
SSL_set_min_proto_version(client.get(), SSL3_VERSION);
|
2017-07-01 00:11:22 +01:00
|
|
|
SSL_enable_ocsp_stapling(client.get());
|
|
|
|
SSL_enable_signed_cert_timestamps(client.get());
|
|
|
|
SSL_set_tlsext_host_name(client.get(), "hostname");
|
|
|
|
SSL_set_alpn_protos(client.get(), kALPNProtocols, sizeof(kALPNProtocols));
|
|
|
|
|
|
|
|
// Enable ciphers that are off by default.
|
|
|
|
SSL_set_strict_cipher_list(client.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(client.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(client.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(client.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;
|
|
|
|
}
|