Blame tests/ssl2-hello.c

Packit aea12f
/*
Packit aea12f
 * Copyright (C) 2016 Red Hat, Inc.
Packit aea12f
 *
Packit aea12f
 * Author: Nikos Mavrogiannopoulos
Packit aea12f
 *
Packit aea12f
 * This file is part of GnuTLS.
Packit aea12f
 *
Packit aea12f
 * GnuTLS is free software; you can redistribute it and/or modify it
Packit aea12f
 * under the terms of the GNU General Public License as published by
Packit aea12f
 * the Free Software Foundation; either version 3 of the License, or
Packit aea12f
 * (at your option) any later version.
Packit aea12f
 *
Packit aea12f
 * GnuTLS is distributed in the hope that it will be useful, but
Packit aea12f
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit aea12f
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit aea12f
 * General Public License for more details.
Packit aea12f
 *
Packit aea12f
 * You should have received a copy of the GNU General Public License
Packit aea12f
 * along with GnuTLS; if not, write to the Free Software Foundation,
Packit aea12f
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
Packit aea12f
 */
Packit aea12f
Packit aea12f
#ifdef HAVE_CONFIG_H
Packit aea12f
#include <config.h>
Packit aea12f
#endif
Packit aea12f
Packit aea12f
#include <stdio.h>
Packit aea12f
#include <stdlib.h>
Packit aea12f
Packit aea12f
/* In this test we check the parsing of SSL 2.0 client hellos under
Packit aea12f
 * the default protocols.
Packit aea12f
 * As we can only read but not generate we use a fixed hello message
Packit aea12f
 * followed by an alert. That way we detect whether the handshake
Packit aea12f
 * completed hello parsing and reached the alert message.
Packit aea12f
 */
Packit aea12f
Packit aea12f
#if defined(_WIN32) || !defined(ENABLE_SSL2)
Packit aea12f
Packit aea12f
/* socketpair isn't supported on Win32. */
Packit aea12f
int main(int argc, char **argv)
Packit aea12f
{
Packit aea12f
	exit(77);
Packit aea12f
}
Packit aea12f
Packit aea12f
#else
Packit aea12f
Packit aea12f
#include <string.h>
Packit aea12f
#include <sys/types.h>
Packit aea12f
#include <sys/socket.h>
Packit aea12f
#if !defined(_WIN32)
Packit aea12f
#include <sys/wait.h>
Packit aea12f
#endif
Packit aea12f
#include <unistd.h>
Packit aea12f
#include <gnutls/gnutls.h>
Packit aea12f
Packit aea12f
#include "utils.h"
Packit aea12f
#include "cert-common.h"
Packit aea12f
Packit aea12f
pid_t child;
Packit aea12f
Packit aea12f
static void tls_log_func(int level, const char *str)
Packit aea12f
{
Packit aea12f
	fprintf(stderr, "%s |<%d>| %s", child ? "server" : "client", level,
Packit aea12f
		str);
Packit aea12f
}
Packit aea12f
Packit aea12f
/* A very basic TLS client, with anonymous authentication.
Packit aea12f
 */
Packit aea12f
Packit aea12f
Packit aea12f
static unsigned char ssl2_hello[] =
Packit aea12f
	"\x80\x59\x01\x03\x01\x00\x30\x00\x00\x00\x20\x00\x00\x39\x00\x00"
Packit aea12f
	"\x38\x00\x00\x35\x00\x00\x16\x00\x00\x13\x00\x00\x0a\x00\x00\x33"
Packit aea12f
	"\x00\x00\x32\x00\x00\x2f\x00\x00\x07\x00\x00\x05\x00\x00\x04\x00"
Packit aea12f
	"\x00\x15\x00\x00\x12\x00\x00\x09\x00\x00\xff\xb1\xc9\x95\x1a\x02"
Packit aea12f
	"\x6c\xd6\x42\x11\x6e\x99\xe2\x84\x97\xc9\x17\x53\xaf\x53\xf7\xfc"
Packit aea12f
	"\x8d\x1e\x72\x87\x18\x53\xee\xa6\x7d\x18\xc6";
Packit aea12f
Packit aea12f
static unsigned char tls_alert[] = 
Packit aea12f
	"\x15\x03\x01\x00\x02\x02\x5A";
Packit aea12f
Packit aea12f
static void client(int sd)
Packit aea12f
{
Packit aea12f
	char buf[1024];
Packit aea12f
	int ret;
Packit aea12f
Packit aea12f
	/* send an SSL 2.0 hello, and then an alert */
Packit aea12f
	
Packit aea12f
	ret = send(sd, ssl2_hello, sizeof(ssl2_hello)-1, 0);
Packit aea12f
	if (ret < 0)
Packit aea12f
		fail("error sending hello\n");
Packit aea12f
Packit aea12f
	ret = recv(sd, buf, sizeof(buf), 0);
Packit aea12f
	if (ret < 0)
Packit aea12f
		fail("error receiving hello\n");
Packit aea12f
Packit aea12f
	ret = send(sd, tls_alert, sizeof(tls_alert)-1, 0);
Packit aea12f
	if (ret < 0)
Packit aea12f
		fail("error sending hello\n");
Packit aea12f
Packit aea12f
	close(sd);
Packit aea12f
}
Packit aea12f
Packit aea12f
static void server(int sd)
Packit aea12f
{
Packit aea12f
	gnutls_certificate_credentials_t x509_cred;
Packit aea12f
	gnutls_session_t session;
Packit aea12f
	int ret;
Packit aea12f
Packit aea12f
	/* this must be called once in the program
Packit aea12f
	 */
Packit aea12f
	global_init();
Packit aea12f
Packit aea12f
	gnutls_global_set_log_function(tls_log_func);
Packit aea12f
	if (debug)
Packit aea12f
		gnutls_global_set_log_level(6);
Packit aea12f
Packit aea12f
	gnutls_certificate_allocate_credentials(&x509_cred);
Packit aea12f
	gnutls_certificate_set_x509_trust_mem(x509_cred, &ca3_cert,
Packit aea12f
					      GNUTLS_X509_FMT_PEM);
Packit aea12f
Packit aea12f
	gnutls_certificate_set_x509_key_mem(x509_cred, &server_ca3_localhost_cert,
Packit aea12f
					    &server_ca3_key,
Packit aea12f
					    GNUTLS_X509_FMT_PEM);
Packit aea12f
Packit aea12f
	if (debug)
Packit aea12f
		success("Launched, generating DH parameters...\n");
Packit aea12f
Packit aea12f
	gnutls_init(&session, GNUTLS_SERVER);
Packit aea12f
Packit aea12f
	/* avoid calling all the priority functions, since the defaults
Packit aea12f
	 * are adequate.
Packit aea12f
	 */
Packit aea12f
	gnutls_priority_set_direct(session, "NORMAL", NULL);
Packit aea12f
Packit aea12f
	gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, x509_cred);
Packit aea12f
Packit aea12f
	gnutls_transport_set_int(session, sd);
Packit aea12f
	ret = gnutls_handshake(session);
Packit aea12f
	if (ret != GNUTLS_E_FATAL_ALERT_RECEIVED || gnutls_alert_get(session) != GNUTLS_A_USER_CANCELED) {
Packit aea12f
		fail("server: Handshake failed unexpectedly (%s)\n\n",
Packit aea12f
		     gnutls_strerror(ret));
Packit aea12f
		return;
Packit aea12f
	}
Packit aea12f
Packit aea12f
	if (debug) {
Packit aea12f
		success("server: Handshake parsed the SSL2.0 client hello\n");
Packit aea12f
	}
Packit aea12f
Packit aea12f
	close(sd);
Packit aea12f
	gnutls_deinit(session);
Packit aea12f
Packit aea12f
	gnutls_certificate_free_credentials(x509_cred);
Packit aea12f
Packit aea12f
	gnutls_global_deinit();
Packit aea12f
Packit aea12f
	if (debug)
Packit aea12f
		success("server: finished\n");
Packit aea12f
}
Packit aea12f
Packit aea12f
Packit aea12f
void doit(void)
Packit aea12f
{
Packit aea12f
	int sockets[2];
Packit aea12f
	int err;
Packit aea12f
Packit aea12f
	err = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
Packit aea12f
	if (err == -1) {
Packit aea12f
		perror("socketpair");
Packit aea12f
		fail("socketpair failed\n");
Packit aea12f
		return;
Packit aea12f
	}
Packit aea12f
Packit aea12f
	child = fork();
Packit aea12f
	if (child < 0) {
Packit aea12f
		perror("fork");
Packit aea12f
		fail("fork");
Packit aea12f
		return;
Packit aea12f
	}
Packit aea12f
Packit aea12f
	if (child) {
Packit aea12f
		int status;
Packit aea12f
Packit aea12f
		close(sockets[1]);
Packit aea12f
		server(sockets[0]);
Packit aea12f
		wait(&status);
Packit aea12f
		check_wait_status(status);
Packit aea12f
	} else {
Packit aea12f
		close(sockets[0]);
Packit aea12f
		client(sockets[1]);
Packit aea12f
		exit(0);
Packit aea12f
	}
Packit aea12f
}
Packit aea12f
Packit aea12f
#endif				/* _WIN32 */