Blame fuzz/gnutls_server_rawpk_fuzzer.c

Packit Service 4684c1
/*
Packit Service 4684c1
 * Copyright (C) 2017 Nikos Mavrogiannopoulos
Packit Service 4684c1
 * Copyright (C) 2019 Tom Vrancken (dev@tomvrancken.nl)
Packit Service 4684c1
 *
Packit Service 4684c1
 * Permission is hereby granted, free of charge, to any person obtaining a
Packit Service 4684c1
 * copy of this software and associated documentation files (the "Software"),
Packit Service 4684c1
 * to deal in the Software without restriction, including without limitation
Packit Service 4684c1
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
Packit Service 4684c1
 * and/or sell copies of the Software, and to permit persons to whom the
Packit Service 4684c1
 * Software is furnished to do so, subject to the following conditions:
Packit Service 4684c1
 *
Packit Service 4684c1
 * The above copyright notice and this permission notice shall be included in
Packit Service 4684c1
 * all copies or substantial portions of the Software.
Packit Service 4684c1
 *
Packit Service 4684c1
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit Service 4684c1
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit Service 4684c1
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Packit Service 4684c1
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Packit Service 4684c1
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
Packit Service 4684c1
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
Packit Service 4684c1
 * DEALINGS IN THE SOFTWARE.
Packit Service 4684c1
 *
Packit Service 4684c1
 */
Packit Service 4684c1
Packit Service 4684c1
/***
Packit Service 4684c1
 * This fuzzer tests the behavior of the GnuTLS library in server mode,
Packit Service 4684c1
 * specifically dealing with raw public keys during the handshake.
Packit Service 4684c1
 * 
Packit Service 4684c1
 * The fuzzer corpus generated as first input was generated with the
Packit Service 4684c1
 * following parameters for gnutls-serv and gnutls-cli:
Packit Service 4684c1
 * 
Packit Service 4684c1
 * gnutls-serv --priority NORMAL:+CTYPE-CLI-RAWPK:+CTYPE-SRV-RAWPK
Packit Service 4684c1
 * gnutls-cli localhost:5556 --priority NORMAL:-CTYPE-ALL:+CTYPE-CLI-RAWPK:+CTYPE-SRV-RAWPK --no-ca-verification
Packit Service 4684c1
 * 
Packit Service 4684c1
 * The above yields a handshake where both the client and server present
Packit Service 4684c1
 * a raw public-key to eachother.
Packit Service 4684c1
 */
Packit Service 4684c1
Packit Service 4684c1
#include <assert.h>
Packit Service 4684c1
#include <stdint.h>
Packit Service 4684c1
#include <unistd.h>
Packit Service 4684c1
#include <string.h>
Packit Service 4684c1
#include <stdlib.h>
Packit Service 4684c1
Packit Service 4684c1
#include <gnutls/gnutls.h>
Packit Service 4684c1
Packit Service 4684c1
#include "certs.h"
Packit Service 4684c1
#include "mem.h"
Packit Service 4684c1
#include "fuzzer.h"
Packit Service 4684c1
Packit Service 4684c1
int LLVMFuzzerTestOneInput(const uint8_t * data, size_t size)
Packit Service 4684c1
{
Packit Service 4684c1
	IGNORE_CERTS;
Packit Service 4684c1
	int res;
Packit Service 4684c1
	gnutls_session_t session;
Packit Service 4684c1
	gnutls_certificate_credentials_t rawpk_cred;
Packit Service 4684c1
	struct mem_st memdata;
Packit Service 4684c1
Packit Service 4684c1
	res = gnutls_init(&session, GNUTLS_SERVER | GNUTLS_ENABLE_RAWPK);
Packit Service 4684c1
	assert(res >= 0);
Packit Service 4684c1
Packit Service 4684c1
	res = gnutls_certificate_allocate_credentials(&rawpk_cred);
Packit Service 4684c1
	assert(res >= 0);
Packit Service 4684c1
Packit Service 4684c1
	res =
Packit Service 4684c1
		gnutls_certificate_set_rawpk_key_mem(rawpk_cred,
Packit Service 4684c1
		                                     &rawpk_public_key1,
Packit Service 4684c1
		                                     &rawpk_private_key1,
Packit Service 4684c1
		                                     GNUTLS_X509_FMT_PEM,
Packit Service 4684c1
		                                     NULL, 0, NULL, 0, 0);
Packit Service 4684c1
	assert(res >= 0);
Packit Service 4684c1
Packit Service 4684c1
	gnutls_certificate_set_known_dh_params(rawpk_cred, GNUTLS_SEC_PARAM_MEDIUM);
Packit Service 4684c1
Packit Service 4684c1
	res = gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, rawpk_cred);
Packit Service 4684c1
	assert(res >= 0);
Packit Service 4684c1
Packit Service 4684c1
	res = gnutls_priority_set_direct(session, "NORMAL:"VERS_STR":-CTYPE-ALL:+CTYPE-CLI-RAWPK:+CTYPE-SRV-RAWPK", NULL);
Packit Service 4684c1
	assert(res >= 0);
Packit Service 4684c1
Packit Service 4684c1
	memdata.data = data;
Packit Service 4684c1
	memdata.size = size;
Packit Service 4684c1
Packit Service 4684c1
	gnutls_transport_set_push_function(session, mem_push);
Packit Service 4684c1
	gnutls_transport_set_pull_function(session, mem_pull);
Packit Service 4684c1
	gnutls_transport_set_pull_timeout_function(session, mem_pull_timeout);
Packit Service 4684c1
	gnutls_transport_set_ptr(session, &memdata);
Packit Service 4684c1
Packit Service 4684c1
	do {
Packit Service 4684c1
		res = gnutls_handshake(session);
Packit Service 4684c1
	} while (res < 0 && gnutls_error_is_fatal(res) == 0);
Packit Service 4684c1
	if (res >= 0) {
Packit Service 4684c1
		for (;;) {
Packit Service 4684c1
			char buf[16384];
Packit Service 4684c1
			res = gnutls_record_recv(session, buf, sizeof(buf));
Packit Service 4684c1
			if (res <= 0) {
Packit Service 4684c1
				break;
Packit Service 4684c1
			}
Packit Service 4684c1
		}
Packit Service 4684c1
	}
Packit Service 4684c1
Packit Service 4684c1
	gnutls_deinit(session);
Packit Service 4684c1
	gnutls_certificate_free_credentials(rawpk_cred);
Packit Service 4684c1
	return 0;
Packit Service 4684c1
}