2017-02-13 16:27:06 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include "blapi.h"
|
|
|
|
#include "prinit.h"
|
|
|
|
#include "ssl.h"
|
|
|
|
|
|
|
|
#include "shared.h"
|
2017-02-21 15:55:41 +03:00
|
|
|
#include "tls_client_config.h"
|
2017-03-15 21:46:22 +03:00
|
|
|
#include "tls_common.h"
|
2017-03-29 07:01:13 +03:00
|
|
|
#include "tls_mutators.h"
|
2017-03-15 21:46:22 +03:00
|
|
|
#include "tls_socket.h"
|
2017-02-13 16:27:06 +03:00
|
|
|
|
2017-04-03 07:00:54 +03:00
|
|
|
#ifdef IS_DTLS
|
|
|
|
__attribute__((constructor)) static void set_is_dtls() {
|
|
|
|
TlsMutators::SetIsDTLS();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
PRFileDesc* ImportFD(PRFileDesc* model, PRFileDesc* fd) {
|
|
|
|
#ifdef IS_DTLS
|
|
|
|
return DTLS_ImportFD(model, fd);
|
|
|
|
#else
|
|
|
|
return SSL_ImportFD(model, fd);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-02-13 16:27:06 +03:00
|
|
|
static SECStatus AuthCertificateHook(void* arg, PRFileDesc* fd, PRBool checksig,
|
|
|
|
PRBool isServer) {
|
2017-02-21 15:55:41 +03:00
|
|
|
assert(!isServer);
|
|
|
|
auto config = reinterpret_cast<ClientConfig*>(arg);
|
|
|
|
return config->FailCertificateAuthentication() ? SECFailure : SECSuccess;
|
2017-02-13 16:27:06 +03:00
|
|
|
}
|
|
|
|
|
2017-02-21 15:55:41 +03:00
|
|
|
static void SetSocketOptions(PRFileDesc* fd,
|
|
|
|
std::unique_ptr<ClientConfig>& config) {
|
2017-03-15 21:46:22 +03:00
|
|
|
SECStatus rv = SSL_OptionSet(fd, SSL_NO_CACHE, config->EnableCache());
|
2017-02-13 16:27:06 +03:00
|
|
|
assert(rv == SECSuccess);
|
|
|
|
|
2017-02-21 15:55:41 +03:00
|
|
|
rv = SSL_OptionSet(fd, SSL_ENABLE_EXTENDED_MASTER_SECRET,
|
|
|
|
config->EnableExtendedMasterSecret());
|
2017-02-13 16:27:06 +03:00
|
|
|
assert(rv == SECSuccess);
|
|
|
|
|
2017-02-21 15:55:41 +03:00
|
|
|
rv = SSL_OptionSet(fd, SSL_REQUIRE_DH_NAMED_GROUPS,
|
|
|
|
config->RequireDhNamedGroups());
|
2017-02-13 16:27:06 +03:00
|
|
|
assert(rv == SECSuccess);
|
|
|
|
|
2017-02-21 15:55:41 +03:00
|
|
|
rv = SSL_OptionSet(fd, SSL_ENABLE_FALSE_START, config->EnableFalseStart());
|
2017-02-13 16:27:06 +03:00
|
|
|
assert(rv == SECSuccess);
|
|
|
|
|
2017-03-15 21:46:22 +03:00
|
|
|
rv = SSL_OptionSet(fd, SSL_ENABLE_DEFLATE, config->EnableDeflate());
|
|
|
|
assert(rv == SECSuccess);
|
|
|
|
|
|
|
|
rv = SSL_OptionSet(fd, SSL_CBC_RANDOM_IV, config->EnableCbcRandomIv());
|
|
|
|
assert(rv == SECSuccess);
|
|
|
|
|
|
|
|
rv = SSL_OptionSet(fd, SSL_REQUIRE_SAFE_NEGOTIATION,
|
|
|
|
config->RequireSafeNegotiation());
|
|
|
|
assert(rv == SECSuccess);
|
|
|
|
|
2017-04-03 07:00:54 +03:00
|
|
|
#ifndef IS_DTLS
|
2017-02-13 16:27:06 +03:00
|
|
|
rv =
|
|
|
|
SSL_OptionSet(fd, SSL_ENABLE_RENEGOTIATION, SSL_RENEGOTIATE_UNRESTRICTED);
|
|
|
|
assert(rv == SECSuccess);
|
2017-04-03 07:00:54 +03:00
|
|
|
#endif
|
2017-02-13 16:27:06 +03:00
|
|
|
}
|
|
|
|
|
2017-02-21 15:55:41 +03:00
|
|
|
// This is only called when we set SSL_ENABLE_FALSE_START=1,
|
|
|
|
// so we can always just set *canFalseStart=true.
|
|
|
|
static SECStatus CanFalseStartCallback(PRFileDesc* fd, void* arg,
|
|
|
|
PRBool* canFalseStart) {
|
|
|
|
*canFalseStart = true;
|
|
|
|
return SECSuccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SetupCallbacks(PRFileDesc* fd, ClientConfig* config) {
|
|
|
|
SECStatus rv = SSL_AuthCertificateHook(fd, AuthCertificateHook, config);
|
|
|
|
assert(rv == SECSuccess);
|
|
|
|
|
|
|
|
rv = SSL_SetCanFalseStartCallback(fd, CanFalseStartCallback, nullptr);
|
2017-02-13 16:27:06 +03:00
|
|
|
assert(rv == SECSuccess);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t len) {
|
|
|
|
static std::unique_ptr<NSSDatabase> db(new NSSDatabase());
|
|
|
|
assert(db != nullptr);
|
|
|
|
|
|
|
|
EnableAllProtocolVersions();
|
2017-02-21 15:55:41 +03:00
|
|
|
std::unique_ptr<ClientConfig> config(new ClientConfig(data, len));
|
2017-02-13 16:27:06 +03:00
|
|
|
|
2017-03-15 21:46:22 +03:00
|
|
|
// Clear the cache. We never want to resume as we couldn't reproduce that.
|
|
|
|
SSL_ClearSessionCache();
|
|
|
|
|
2017-02-13 16:27:06 +03:00
|
|
|
// Reset the RNG state.
|
2017-03-24 10:38:05 +03:00
|
|
|
assert(RNG_RandomUpdate(NULL, 0) == SECSuccess);
|
2017-02-13 16:27:06 +03:00
|
|
|
|
|
|
|
// Create and import dummy socket.
|
|
|
|
std::unique_ptr<DummyPrSocket> socket(new DummyPrSocket(data, len));
|
|
|
|
static PRDescIdentity id = PR_GetUniqueIdentity("fuzz-client");
|
|
|
|
ScopedPRFileDesc fd(DummyIOLayerMethods::CreateFD(id, socket.get()));
|
2017-04-03 07:00:54 +03:00
|
|
|
PRFileDesc* ssl_fd = ImportFD(nullptr, fd.get());
|
2017-02-13 16:27:06 +03:00
|
|
|
assert(ssl_fd == fd.get());
|
|
|
|
|
|
|
|
// Probably not too important for clients.
|
|
|
|
SSL_SetURL(ssl_fd, "server");
|
|
|
|
|
2017-02-21 15:55:41 +03:00
|
|
|
SetSocketOptions(ssl_fd, config);
|
2017-02-13 16:27:06 +03:00
|
|
|
EnableAllCipherSuites(ssl_fd);
|
2017-02-21 15:55:41 +03:00
|
|
|
SetupCallbacks(ssl_fd, config.get());
|
2017-03-15 21:46:22 +03:00
|
|
|
DoHandshake(ssl_fd, false);
|
2017-02-13 16:27:06 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2017-03-29 07:01:13 +03:00
|
|
|
|
|
|
|
extern "C" size_t LLVMFuzzerCustomMutator(uint8_t* data, size_t size,
|
|
|
|
size_t max_size, unsigned int seed) {
|
2017-04-03 07:00:54 +03:00
|
|
|
using namespace TlsMutators;
|
|
|
|
return CustomMutate({DropRecord, ShuffleRecords, DuplicateRecord,
|
|
|
|
TruncateRecord, FragmentRecord},
|
2017-03-29 07:01:13 +03:00
|
|
|
data, size, max_size, seed);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" size_t LLVMFuzzerCustomCrossOver(const uint8_t* data1, size_t size1,
|
|
|
|
const uint8_t* data2, size_t size2,
|
|
|
|
uint8_t* out, size_t max_out_size,
|
|
|
|
unsigned int seed) {
|
2017-04-03 07:00:54 +03:00
|
|
|
return TlsMutators::CrossOver(data1, size1, data2, size2, out, max_out_size,
|
|
|
|
seed);
|
2017-03-29 07:01:13 +03:00
|
|
|
}
|