Blame tests/server-sign-md5-rep.c

Packit aea12f
/*
Packit aea12f
 * Copyright (C) 2017 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 Lesser General Public License
Packit aea12f
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
Packit aea12f
 *
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
/* This tests whether a server will reject a client advertising
Packit aea12f
 * MD5 signature algorithms only */
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
#include <sys/wait.h>
Packit aea12f
#include <unistd.h>
Packit aea12f
#include <gnutls/gnutls.h>
Packit aea12f
#include <signal.h>
Packit aea12f
#include <poll.h>
Packit aea12f
#include <errno.h>
Packit aea12f
#include <assert.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
Packit aea12f
static unsigned char tls1_hello[] = {
Packit aea12f
0x16, 0x03, 0x01, 0x01, 0x5E, 0x01, 0x00, 0x01, 0x5A, 0x03, 0x03, 0x59, 0x52, 0x41, 0x54, 0xD5,
Packit aea12f
0x52, 0x62, 0x63, 0x69, 0x1B, 0x46, 0xBE, 0x33, 0xCC, 0xC4, 0xC3, 0xB3, 0x6C, 0xCD, 0xEC, 0x96,
Packit aea12f
0xF7, 0x7A, 0xCA, 0xE9, 0xFB, 0x85, 0x95, 0x83, 0x51, 0xE4, 0x69, 0x00, 0x00, 0xD4, 0xC0, 0x30,
Packit aea12f
0xCC, 0xA8, 0xC0, 0x8B, 0xC0, 0x14, 0xC0, 0x28, 0xC0, 0x77, 0xC0, 0x2F, 0xC0, 0x8A, 0xC0, 0x13,
Packit aea12f
0xC0, 0x27, 0xC0, 0x76, 0xC0, 0x12, 0xC0, 0x2C, 0xC0, 0xAD, 0xCC, 0xA9, 0xC0, 0x87, 0xC0, 0x0A,
Packit aea12f
0xC0, 0x24, 0xC0, 0x73, 0xC0, 0x2B, 0xC0, 0xAC, 0xC0, 0x86, 0xC0, 0x09, 0xC0, 0x23, 0xC0, 0x72,
Packit aea12f
0xC0, 0x08, 0x00, 0x9D, 0xC0, 0x9D, 0xC0, 0x7B, 0x00, 0x35, 0x00, 0x3D, 0x00, 0x84, 0x00, 0xC0,
Packit aea12f
0x00, 0x9C, 0xC0, 0x9C, 0xC0, 0x7A, 0x00, 0x2F, 0x00, 0x3C, 0x00, 0x41, 0x00, 0xBA, 0x00, 0x0A,
Packit aea12f
0x00, 0x9F, 0xC0, 0x9F, 0xCC, 0xAA, 0xC0, 0x7D, 0x00, 0x39, 0x00, 0x6B, 0x00, 0x88, 0x00, 0xC4,
Packit aea12f
0x00, 0x9E, 0xC0, 0x9E, 0xC0, 0x7C, 0x00, 0x33, 0x00, 0x67, 0x00, 0x45, 0x00, 0xBE, 0x00, 0x16,
Packit aea12f
0x00, 0xA3, 0xC0, 0x81, 0x00, 0x38, 0x00, 0x6A, 0x00, 0x87, 0x00, 0xC3, 0x00, 0xA2, 0xC0, 0x80,
Packit aea12f
0x00, 0x32, 0x00, 0x40, 0x00, 0x44, 0x00, 0xBD, 0x00, 0x13, 0x00, 0xA9, 0xC0, 0xA5, 0xCC, 0xAB,
Packit aea12f
0xC0, 0x8F, 0x00, 0x8D, 0x00, 0xAF, 0xC0, 0x95, 0x00, 0xA8, 0xC0, 0xA4, 0xC0, 0x8E, 0x00, 0x8C,
Packit aea12f
0x00, 0xAE, 0xC0, 0x94, 0x00, 0x8B, 0x00, 0xAB, 0xC0, 0xA7, 0xCC, 0xAD, 0xC0, 0x91, 0x00, 0x91,
Packit aea12f
0x00, 0xB3, 0xC0, 0x97, 0x00, 0xAA, 0xC0, 0xA6, 0xC0, 0x90, 0x00, 0x90, 0x00, 0xB2, 0xC0, 0x96,
Packit aea12f
0x00, 0x8F, 0xCC, 0xAC, 0xC0, 0x36, 0xC0, 0x38, 0xC0, 0x9B, 0xC0, 0x35, 0xC0, 0x37, 0xC0, 0x9A,
Packit aea12f
0xC0, 0x34, 0x01, 0x00, 0x00, 0x5D, 0x00, 0x17, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x05,
Packit aea12f
0x00, 0x05, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0E,
Packit aea12f
0x77, 0x77, 0x77, 0x2E, 0x67, 0x6F, 0x6F, 0x67, 0x6C, 0x65, 0x2E, 0x63, 0x6F, 0x6D, 0xFF, 0x01,
Packit aea12f
0x00, 0x01, 0x00, 0x00, 0x23, 0x00, 0x00, 0x00, 0x0A, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00,
Packit aea12f
0x18, 0x00, 0x19, 0x00, 0x0B, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0D, 0x00, 0x16, 0x00, 0x14, 0x01,
Packit aea12f
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
Packit aea12f
0x01, 0x01, 0x01};
Packit aea12f
Packit aea12f
static void client(int sd)
Packit aea12f
{
Packit aea12f
	char buf[1024];
Packit aea12f
	int ret;
Packit aea12f
	struct pollfd pfd;
Packit aea12f
Packit aea12f
	/* send a TLS 1.x hello advertising RSA-MD5 */
Packit aea12f
Packit aea12f
	ret = send(sd, tls1_hello, sizeof(tls1_hello), 0);
Packit aea12f
	if (ret < 0)
Packit aea12f
		fail("error sending hello\n");
Packit aea12f
Packit aea12f
	pfd.fd = sd;
Packit aea12f
	pfd.events = POLLIN;
Packit aea12f
	pfd.revents = 0;
Packit aea12f
Packit aea12f
	do {
Packit aea12f
		ret = poll(&pfd, 1, 10000);
Packit aea12f
	} while (ret == -1 && errno == EINTR);
Packit aea12f
Packit aea12f
	if (ret == -1 || ret == 0) {
Packit aea12f
		fail("timeout waiting for reply\n");
Packit aea12f
	}
Packit aea12f
Packit aea12f
	success("sent hello\n");
Packit aea12f
	ret = recv(sd, buf, sizeof(buf), 0);
Packit aea12f
	if (ret < 0)
Packit aea12f
		fail("error receiving alert\n");
Packit aea12f
Packit aea12f
	success("received reply\n");
Packit aea12f
Packit aea12f
	if (ret < 7)
Packit aea12f
		fail("error in size of received alert\n");
Packit aea12f
Packit aea12f
	if (buf[0] != 0x15 || buf[1] != 0x03)
Packit aea12f
		fail("error in received alert data\n");
Packit aea12f
Packit aea12f
	success("all ok\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
	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
	assert(gnutls_priority_set_direct(session, "NORMAL:-VERS-ALL:+VERS-TLS1.2:-RSA", NULL)>=0);
Packit aea12f
	gnutls_handshake_set_timeout(session, 20 * 1000);
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
	do {
Packit aea12f
		ret = gnutls_handshake(session);
Packit aea12f
	} while(ret == GNUTLS_E_INTERRUPTED || ret == GNUTLS_E_AGAIN);
Packit aea12f
Packit aea12f
	if (ret != GNUTLS_E_NO_CIPHER_SUITES) {
Packit aea12f
		fail("server: Handshake succeeded unexpectedly: %s\n", gnutls_strerror(ret));
Packit aea12f
	}
Packit aea12f
Packit aea12f
	gnutls_alert_send_appropriate(session, ret);
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
	signal(SIGPIPE, SIG_IGN);
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
		client(sockets[1]);
Packit aea12f
		wait(&status);
Packit aea12f
		check_wait_status(status);
Packit aea12f
	} else {
Packit aea12f
		server(sockets[0]);
Packit aea12f
		_exit(0);
Packit aea12f
	}
Packit aea12f
}
Packit aea12f
Packit aea12f
#endif				/* _WIN32 */