summaryrefslogtreecommitdiffstats
path: root/tests/multi-alerts.c
blob: 84a412c59c74cbe1f2f4a8af35def87a040cea02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
 * Copyright (C) 2016 Red Hat, Inc.
 *
 * Author: Nikos Mavrogiannopoulos
 *
 * This file is part of GnuTLS.
 *
 * GnuTLS is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * GnuTLS is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GnuTLS; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>

/* In this test we check whether the server will bail out after receiving
 * a bunch of warning alerts. That is to avoid DoS due to the asymmetry of
 * cost of sending an alert vs the cost of receiving.
 */

#if defined(_WIN32)

/* socketpair isn't supported on Win32. */
int main(int argc, char **argv)
{
	exit(77);
}

#else

#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#if !defined(_WIN32)
#include <sys/wait.h>
#endif
#include <unistd.h>
#include <assert.h>
#include <gnutls/gnutls.h>

#include "utils.h"
#include "cert-common.h"

pid_t child;

static void tls_log_func(int level, const char *str)
{
	fprintf(stderr, "%s |<%d>| %s", child ? "server" : "client", level,
		str);
}

static unsigned char tls_hello[] =
	"\x16\x03\x01\x01\x38\x01\x00\x01"
	"\x34\x03\x03\xfc\x77\xa8\xc7\x46"
	"\xf7\xfd\x04\x5b\x3c\xc6\xfa\xa4"
	"\xea\x3e\xfa\x76\x99\xfe\x1a\x2e"
	"\xe0\x79\x17\xb2\x27\x06\xc4\x5c"
	"\xd8\x78\x31\x00\x00\xb6\xc0\x30"
	"\xc0\x2c\xc0\x28\xc0\x24\xc0\x14"
	"\xc0\x0a\x00\xa5\x00\xa3\x00\xa1"
	"\x00\x9f\x00\x6b\x00\x6a\x00\x69"
	"\x00\x68\x00\x39\x00\x38\x00\x37"
	"\x00\x36\x00\x88\x00\x87\x00\x86"
	"\x00\x85\xc0\x32\xc0\x2e\xc0\x2a"
	"\xc0\x26\xc0\x0f\xc0\x05\x00\x9d"
	"\x00\x3d\x00\x35\x00\x84\xc0\x2f"
	"\xc0\x2b\xc0\x27\xc0\x23\xc0\x13"
	"\xc0\x09\x00\xa4\x00\xa2\x00\xa0"
	"\x00\x9e\x00\x67\x00\x40\x00\x3f"
	"\x00\x3e\x00\x33\x00\x32\x00\x31"
	"\x00\x30\x00\x9a\x00\x99\x00\x98"
	"\x00\x97\x00\x45\x00\x44\x00\x43"
	"\x00\x42\xc0\x31\xc0\x2d\xc0\x29"
	"\xc0\x25\xc0\x0e\xc0\x04\x00\x9c"
	"\x00\x3c\x00\x2f\x00\x96\x00\x41"
	"\x00\x07\xc0\x11\xc0\x07\xc0\x0c"
	"\xc0\x02\x00\x05\x00\x04\xc0\x12"
	"\xc0\x08\x00\x16\x00\x13\x00\x10"
	"\x00\x0d\xc0\x0d\xc0\x03\x00\x0a"
	"\x00\x15\x00\x12\x00\x0f\x00\x0c"
	"\x00\x09\x00\xff\x01\x00\x00\x55"
	"\x00\x0b\x00\x04\x03\x00\x01\x02"
	"\x00\x0a\x00\x1c\x00\x1a\x00\x17"
	"\x00\x19\x00\x1c\x00\x1b\x00\x18"
	"\x00\x1a\x00\x16\x00\x0e\x00\x0d"
	"\x00\x0b\x00\x0c\x00\x09\x00\x0a"
	"\x00\x23\x00\x00\x00\x0d\x00\x20"
	"\x00\x1e\x06\x01\x06\x02\x06\x03"
	"\x05\x01\x05\x02\x05\x03\x04\x01"
	"\x04\x02\x04\x03\x03\x01\x03\x02"
	"\x03\x03\x02\x01\x02\x02\x02\x03"
	"\x00\x0f\x00\x01\x01";

static unsigned char tls_alert[] = 
	"\x15\x03\x03\x00\x02\x00\x0A";

static void client(int sd)
{
	char buf[1024];
	int ret;
	unsigned i;

	/* send a TLS hello, and then a list of warning alerts */

	ret = send(sd, tls_hello, sizeof(tls_hello)-1, 0);
	if (ret < 0)
		fail("error sending hello\n");

	ret = recv(sd, buf, sizeof(buf), 0);
	if (ret < 0)
		fail("error receiving hello\n");

	for (i=0;i<128;i++) {
		ret = send(sd, tls_alert, sizeof(tls_alert)-1, 0);
		if (ret < 0)
			fail("error sending hello\n");
	}

	close(sd);
}

static void server(int sd)
{
	gnutls_certificate_credentials_t x509_cred;
	gnutls_session_t session;
	int ret;
	unsigned loops;

	/* this must be called once in the program
	 */
	global_init();

	gnutls_global_set_log_function(tls_log_func);
	if (debug)
		gnutls_global_set_log_level(6);

	gnutls_certificate_allocate_credentials(&x509_cred);
	gnutls_certificate_set_x509_trust_mem(x509_cred, &ca3_cert,
					      GNUTLS_X509_FMT_PEM);

	gnutls_certificate_set_x509_key_mem(x509_cred, &server_ca3_localhost_cert,
					    &server_ca3_key,
					    GNUTLS_X509_FMT_PEM);

	if (debug)
		success("Launched, generating DH parameters...\n");

	gnutls_init(&session, GNUTLS_SERVER);

	/* avoid calling all the priority functions, since the defaults
	 * are adequate.
	 */
	assert(gnutls_priority_set_direct(session, "NORMAL:-VERS-ALL:+VERS-TLS1.2", NULL)>=0);

	gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, x509_cred);

	gnutls_transport_set_int(session, sd);
	loops = 0;
	do {
		ret = gnutls_handshake(session);
		loops++;
		if (loops > 64)
			fail("Too many loops in the handshake!\n");
	} while (ret == GNUTLS_E_INTERRUPTED || ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_WARNING_ALERT_RECEIVED);

	if (ret >= 0) {
		fail("server: Handshake succeeded unexpectedly\n");
	}

	close(sd);
	gnutls_deinit(session);

	gnutls_certificate_free_credentials(x509_cred);

	gnutls_global_deinit();

	if (debug)
		success("server: finished\n");
}


void doit(void)
{
	int sockets[2];
	int err;

	err = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
	if (err == -1) {
		perror("socketpair");
		fail("socketpair failed\n");
		return;
	}

	child = fork();
	if (child < 0) {
		perror("fork");
		fail("fork");
		return;
	}

	if (child) {
		int status;

		server(sockets[0]);
		wait(&status);
		check_wait_status(status);
	} else {
		client(sockets[1]);
		exit(0);
	}
}

#endif				/* _WIN32 */