2 * Copyright (C) 2012-2013 Free Software Foundation, Inc.
3 * Copyright (C) 2013 Nikos Mavrogiannopoulos
5 * Author: Nikos Mavrogiannopoulos
7 * This file is part of GnuTLS.
9 * GnuTLS is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * GnuTLS is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with GnuTLS; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
42 #include <sys/types.h>
43 #include <netinet/in.h>
44 #include <sys/socket.h>
46 #include <arpa/inet.h>
48 #include <gnutls/gnutls.h>
49 #include <gnutls/dtls.h>
53 static int test_finished = 0;
54 static void terminate(void);
56 /* This program tests whether messages in DTLS are received
57 * with the expected sequence. That is whether the message
58 * sequence numbers returned correspond to the received messages.
63 tls_audit_log_func (gnutls_session_t session, const char *str)
65 fprintf (stderr, "|<%p>| %s", session, str);
69 static void server_log_func(int level, const char *str)
71 fprintf(stderr, "server|<%d>| %s", level, str);
74 static void client_log_func(int level, const char *str)
76 fprintf(stderr, "client|<%d>| %s", level, str);
79 /* These are global */
82 /* A test client/server app for DTLS duplicate packet detection.
89 static int msg_seq[] =
90 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 16, 5, 32, 11, 11, 11, 11, 12,
91 10, 13, 14, 15, 16, 17, 19, 20, 18, 22, 24, 23, 25, 26, 27, 29, 28,
92 29, 29, 30, 31, 32, 33, 34, 35, 37, 36, 38, 39, 42, 37, 40, 41, 41,
96 static unsigned int current = 0;
97 static unsigned int pos = 0;
99 unsigned char *stored_messages[MAX_SEQ];
100 unsigned int stored_sizes[MAX_SEQ];
102 static ssize_t odd_push(gnutls_transport_ptr_t tr, const void *data, size_t len)
107 if (msg_seq[current] == -1 || test_finished != 0) {
112 stored_messages[current] = malloc(len);
113 memcpy(stored_messages[current], data, len);
114 stored_sizes[current] = len;
116 if (pos != current) {
117 for (i = pos; i <= current; i++) {
118 if (stored_messages[msg_seq[i]] != NULL) {
123 stored_messages[msg_seq
125 stored_sizes[msg_seq[i]], 0);
127 while (ret == -1 && errno == EAGAIN);
132 } else if (msg_seq[current] == (int)current) {
134 ret = send((long int)tr, data, len, 0);
136 while (ret == -1 && errno == EAGAIN);
142 } else if (stored_messages[msg_seq[current]] != NULL) {
146 stored_messages[msg_seq[current]],
147 stored_sizes[msg_seq[current]], 0);
149 while (ret == -1 && errno == EAGAIN);
160 static ssize_t n_push(gnutls_transport_ptr_t tr, const void *data, size_t len)
162 return send((unsigned long)tr, data, len, 0);
165 /* The first five messages are handshake. Thus corresponds to msg_seq+5 */
166 static int recv_msg_seq[] =
167 { 1, 2, 3, 4, 5, 6, 12, 28, 7, 8, 9, 10, 11, 13, 15, 16, 14, 18, 20,
168 19, 21, 22, 23, 25, 24, 26, 27, 29, 30, 31, 33, 32, 34, 35, 38, 36, 37,
172 static void client(int fd)
174 gnutls_session_t session;
176 char buffer[MAX_BUF + 1];
177 gnutls_anon_client_credentials_t anoncred;
178 unsigned char seq[8];
180 unsigned current = 0;
182 memset(buffer, 0, sizeof(buffer));
184 /* Need to enable anonymous KX specifically. */
186 /* gnutls_global_set_audit_log_function (tls_audit_log_func); */
190 gnutls_global_set_log_function(client_log_func);
191 gnutls_global_set_log_level(2);
194 gnutls_anon_allocate_client_credentials(&anoncred);
196 /* Initialize TLS session
198 gnutls_init(&session, GNUTLS_CLIENT | GNUTLS_DATAGRAM);
199 gnutls_heartbeat_enable(session, GNUTLS_HB_PEER_ALLOWED_TO_SEND);
200 gnutls_dtls_set_mtu(session, 1500);
202 /* Use default priorities */
203 gnutls_priority_set_direct(session,
204 "NONE:+VERS-DTLS1.0:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-ECDH:+CURVE-ALL",
207 /* put the anonymous credentials to the current session
209 gnutls_credentials_set(session, GNUTLS_CRD_ANON, anoncred);
211 gnutls_transport_set_int(session, fd);
213 /* Perform the TLS handshake
216 ret = gnutls_handshake(session);
218 while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
221 fail("client: Handshake failed\n");
226 success("client: Handshake was completed\n");
229 gnutls_record_send(session, buffer, 1);
232 success("client: DTLS version is: %s\n",
233 gnutls_protocol_get_name
234 (gnutls_protocol_get_version(session)));
237 gnutls_record_recv_seq(session, buffer, sizeof(buffer),
242 seq[3] | (seq[2] << 8) | (seq[1] << 16) |
246 seq[7] | (seq[6] << 8) | (seq[5] << 16) |
249 if (recv_msg_seq[current] == -1) {
250 fail("received message sequence differs\n");
254 if ((uint32_t) recv_msg_seq[current] != (uint32_t) useq) {
255 fail("received message sequence differs (got: %u, expected: %u)\n", (unsigned)useq, (unsigned)recv_msg_seq[current]);
262 while ((ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED
265 gnutls_bye(session, GNUTLS_SHUT_WR);
269 gnutls_deinit(session);
271 gnutls_anon_free_client_credentials(anoncred);
273 gnutls_global_deinit();
276 static void terminate(void)
280 kill(child, SIGTERM);
285 static void server(int fd)
288 gnutls_session_t session;
289 gnutls_anon_server_credentials_t anoncred;
295 gnutls_global_set_log_function(server_log_func);
296 gnutls_global_set_log_level(2);
299 gnutls_anon_allocate_server_credentials(&anoncred);
301 gnutls_init(&session, GNUTLS_SERVER | GNUTLS_DATAGRAM);
302 gnutls_transport_set_push_function(session, odd_push);
303 gnutls_heartbeat_enable(session, GNUTLS_HB_PEER_ALLOWED_TO_SEND);
304 gnutls_dtls_set_mtu(session, 1500);
306 /* avoid calling all the priority functions, since the defaults
309 gnutls_priority_set_direct(session,
310 "NONE:+VERS-DTLS1.0:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-ECDH:+CURVE-ALL",
312 gnutls_credentials_set(session, GNUTLS_CRD_ANON, anoncred);
314 gnutls_transport_set_int(session, fd);
317 ret = gnutls_handshake(session);
319 while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
322 gnutls_deinit(session);
323 fail("server: Handshake has failed (%s)\n\n",
324 gnutls_strerror(ret));
328 success("server: Handshake was completed\n");
331 success("server: TLS version is: %s\n",
332 gnutls_protocol_get_name
333 (gnutls_protocol_get_version(session)));
335 gnutls_record_recv(session, &c, 1);
338 ret = gnutls_record_send(session, &c, 1);
340 while (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED);
343 fail("send: %s\n", gnutls_strerror(ret));
347 while (test_finished == 0);
349 gnutls_transport_set_push_function(session, n_push);
351 ret = gnutls_bye(session, GNUTLS_SHUT_WR);
353 while (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED);
356 gnutls_deinit(session);
358 gnutls_anon_free_server_credentials(anoncred);
360 gnutls_global_deinit();
363 success("server: finished\n");
366 static void start(void)
371 ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
373 perror("socketpair");
390 if (WEXITSTATUS(status) != 0)
391 fail("Child died with status %d\n",
392 WEXITSTATUS(status));