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
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>
52 /* This program tests whether the handshake timeout value is enforced.
55 static void server_log_func(int level, const char *str)
57 fprintf(stderr, "server|<%d>| %s", level, str);
60 static void client_log_func(int level, const char *str)
62 fprintf(stderr, "client|<%d>| %s", level, str);
65 /* A very basic TLS client, with anonymous authentication.
68 static void client(int fd, int wait)
71 gnutls_anon_client_credentials_t anoncred;
72 gnutls_session_t session;
73 /* Need to enable anonymous KX specifically. */
78 gnutls_global_set_log_function(client_log_func);
79 gnutls_global_set_log_level(4711);
82 gnutls_anon_allocate_client_credentials(&anoncred);
84 /* Initialize TLS session
86 gnutls_init(&session, GNUTLS_CLIENT);
87 gnutls_handshake_set_timeout(session, 20 * 1000);
89 /* Use default priorities */
90 gnutls_priority_set_direct(session, "NORMAL:+ANON-ECDH", NULL);
92 /* put the anonymous credentials to the current session
94 gnutls_credentials_set(session, GNUTLS_CRD_ANON, anoncred);
96 gnutls_transport_set_int(session, fd);
98 /* Perform the TLS handshake
101 ret = gnutls_handshake(session);
103 while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
105 gnutls_deinit(session);
106 gnutls_anon_free_client_credentials(anoncred);
107 gnutls_global_deinit();
110 if (ret != GNUTLS_E_TIMEDOUT || wait == 0) {
112 fail("client: unexpected error: %s\n",
113 gnutls_strerror(ret));
117 success("client: expected timeout occured\n");
121 fail("client: handshake was completed unexpectedly\n");
130 static void initialize_tls_session(gnutls_session_t * session)
132 gnutls_init(session, GNUTLS_SERVER);
134 /* avoid calling all the priority functions, since the defaults
137 gnutls_priority_set_direct(*session, "NORMAL:+ANON-ECDH", NULL);
140 static void server(int fd, int wait)
143 gnutls_session_t session;
144 gnutls_anon_server_credentials_t anoncred;
146 /* this must be called once in the program
151 gnutls_global_set_log_function(server_log_func);
152 gnutls_global_set_log_level(4711);
155 gnutls_anon_allocate_server_credentials(&anoncred);
157 initialize_tls_session(&session);
158 gnutls_credentials_set(session, GNUTLS_CRD_ANON, anoncred);
160 gnutls_transport_set_int(session, fd);
166 ret = gnutls_handshake(session);
168 while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
171 gnutls_bye(session, GNUTLS_SHUT_RDWR);
174 gnutls_deinit(session);
175 gnutls_anon_free_server_credentials(anoncred);
176 gnutls_global_deinit();
179 static void start(int wait)
186 fprintf(stderr, "\nWill test timeout\n");
188 ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
190 perror("socketpair");
214 static void ch_handler(int sig)
218 if (WEXITSTATUS(status) != 0)
219 fail("Child died with status %d\n", WEXITSTATUS(status));
225 signal(SIGCHLD, ch_handler);
226 signal(SIGPIPE, SIG_IGN);
228 /* make sure that normal handshake occurs */
231 /* check the handshake with an expected timeout */