Blame tests/ssl2-hello.c

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