2 * Copyright (C) 2012 Free Software Foundation, Inc.
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GnuTLS.
8 * GnuTLS is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * GnuTLS is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with GnuTLS; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
30 #if defined(_WIN32) || !defined(ENABLE_HEARTBEAT)
40 #include <sys/types.h>
41 #include <netinet/in.h>
42 #include <sys/socket.h>
44 #include <arpa/inet.h>
46 #include <gnutls/gnutls.h>
47 #include <gnutls/dtls.h>
51 static void terminate(void);
53 /* This program tests the rehandshake in DTLS
56 static void server_log_func(int level, const char *str)
58 fprintf(stderr, "server|<%d>| %s", level, str);
61 static void client_log_func(int level, const char *str)
63 fprintf(stderr, "client|<%d>| %s", level, str);
66 /* These are global */
69 /* A very basic DTLS client, with anonymous authentication, that exchanges heartbeats.
75 static void client(int fd, int server_init)
77 gnutls_session_t session;
79 char buffer[MAX_BUF + 1];
80 gnutls_anon_client_credentials_t anoncred;
81 /* Need to enable anonymous KX specifically. */
86 gnutls_global_set_log_function(client_log_func);
87 gnutls_global_set_log_level(4711);
90 gnutls_anon_allocate_client_credentials(&anoncred);
92 /* Initialize TLS session
94 gnutls_init(&session, GNUTLS_CLIENT | GNUTLS_DATAGRAM);
95 gnutls_heartbeat_enable(session, GNUTLS_HB_PEER_ALLOWED_TO_SEND);
96 gnutls_dtls_set_mtu(session, 1500);
98 /* Use default priorities */
99 gnutls_priority_set_direct(session,
100 "NONE:+VERS-DTLS1.0:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-ECDH:+CURVE-ALL",
103 /* put the anonymous credentials to the current session
105 gnutls_credentials_set(session, GNUTLS_CRD_ANON, anoncred);
107 gnutls_transport_set_int(session, fd);
109 /* Perform the TLS handshake
112 ret = gnutls_handshake(session);
114 while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
117 fail("client: Handshake failed\n");
122 success("client: Handshake was completed\n");
126 success("client: DTLS version is: %s\n",
127 gnutls_protocol_get_name
128 (gnutls_protocol_get_version(session)));
133 gnutls_record_recv(session, buffer,
136 if (ret == GNUTLS_E_HEARTBEAT_PING_RECEIVED) {
139 ("Ping received. Replying with pong.\n");
140 ret2 = gnutls_heartbeat_pong(session, 0);
143 gnutls_strerror(ret));
148 while (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED
149 || ret == GNUTLS_E_HEARTBEAT_PING_RECEIVED);
152 fail("recv: %s\n", gnutls_strerror(ret));
158 gnutls_heartbeat_ping(session, 256, 5,
159 GNUTLS_HEARTBEAT_WAIT);
162 success("Ping sent.\n");
164 while (ret == GNUTLS_E_AGAIN
165 || ret == GNUTLS_E_INTERRUPTED);
168 fail("ping: %s\n", gnutls_strerror(ret));
173 gnutls_bye(session, GNUTLS_SHUT_WR);
177 gnutls_deinit(session);
179 gnutls_anon_free_client_credentials(anoncred);
181 gnutls_global_deinit();
186 static gnutls_session_t initialize_tls_session(void)
188 gnutls_session_t session;
190 gnutls_init(&session, GNUTLS_SERVER | GNUTLS_DATAGRAM);
191 gnutls_heartbeat_enable(session, GNUTLS_HB_PEER_ALLOWED_TO_SEND);
192 gnutls_dtls_set_mtu(session, 1500);
194 /* avoid calling all the priority functions, since the defaults
197 gnutls_priority_set_direct(session,
198 "NONE:+VERS-DTLS1.0:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-ECDH:+CURVE-ALL",
204 static void terminate(void)
208 kill(child, SIGTERM);
213 static void server(int fd, int server_init)
216 char buffer[MAX_BUF + 1];
217 gnutls_session_t session;
218 gnutls_anon_server_credentials_t anoncred;
219 /* this must be called once in the program
224 gnutls_global_set_log_function(server_log_func);
225 gnutls_global_set_log_level(4711);
228 gnutls_anon_allocate_server_credentials(&anoncred);
230 session = initialize_tls_session();
231 gnutls_credentials_set(session, GNUTLS_CRD_ANON, anoncred);
233 gnutls_transport_set_int(session, fd);
236 ret = gnutls_handshake(session);
238 while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
241 gnutls_deinit(session);
242 fail("server: Handshake has failed (%s)\n\n",
243 gnutls_strerror(ret));
247 success("server: Handshake was completed\n");
250 success("server: TLS version is: %s\n",
251 gnutls_protocol_get_name
252 (gnutls_protocol_get_version(session)));
254 /* see the Getting peer's information example */
255 /* print_info(session); */
260 gnutls_record_recv(session, buffer,
263 if (ret == GNUTLS_E_HEARTBEAT_PING_RECEIVED) {
266 ("Ping received. Replying with pong.\n");
267 ret2 = gnutls_heartbeat_pong(session, 0);
270 gnutls_strerror(ret));
275 while (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED
276 || ret == GNUTLS_E_HEARTBEAT_PING_RECEIVED);
280 gnutls_heartbeat_ping(session, 256, 5,
281 GNUTLS_HEARTBEAT_WAIT);
284 success("Ping sent.\n");
286 while (ret == GNUTLS_E_AGAIN
287 || ret == GNUTLS_E_INTERRUPTED);
290 fail("ping: %s\n", gnutls_strerror(ret));
295 /* do not wait for the peer to close the connection.
297 gnutls_bye(session, GNUTLS_SHUT_WR);
300 gnutls_deinit(session);
302 gnutls_anon_free_server_credentials(anoncred);
304 gnutls_global_deinit();
307 success("server: finished\n");
310 static void start(int server_initiated)
315 ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
317 perror("socketpair");
332 server(fd[0], server_initiated);
334 if (WEXITSTATUS(status) != 0)
335 fail("Child died with status %d\n",
336 WEXITSTATUS(status));
339 client(fd[1], server_initiated);