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.
56 server_log_func (int level
, const char *str
)
58 fprintf (stderr
, "server|<%d>| %s", level
, str
);
62 client_log_func (int level
, const char *str
)
64 fprintf (stderr
, "client|<%d>| %s", level
, str
);
67 /* A very basic TLS client, with anonymous authentication.
71 static int packet_to_lose
;
72 gnutls_session_t session
;
75 client (int fd
, int wait
)
78 gnutls_anon_client_credentials_t anoncred
;
79 /* Need to enable anonymous KX specifically. */
81 gnutls_global_init ();
85 gnutls_global_set_log_function (client_log_func
);
86 gnutls_global_set_log_level (4711);
89 gnutls_anon_allocate_client_credentials (&anoncred
);
91 /* Initialize TLS session
93 gnutls_init (&session
, GNUTLS_CLIENT
);
94 gnutls_handshake_set_timeout( session
, 10*1000);
96 /* Use default priorities */
97 gnutls_priority_set_direct (session
, "NORMAL:+ANON-ECDH", NULL
);
99 /* put the anonymous credentials to the current session
101 gnutls_credentials_set (session
, GNUTLS_CRD_ANON
, anoncred
);
105 gnutls_transport_set_ptr (session
, (gnutls_transport_ptr_t
) fd
);
107 /* Perform the TLS handshake
111 ret
= gnutls_handshake (session
);
113 while (ret
< 0 && gnutls_error_is_fatal(ret
) == 0);
115 gnutls_deinit(session
);
116 gnutls_anon_free_client_credentials(anoncred
);
117 gnutls_global_deinit();
121 if (ret
!= GNUTLS_E_TIMEDOUT
|| wait
== 0)
123 if (debug
) fail("client: unexpected error: %s\n", gnutls_strerror(ret
));
126 if (debug
) success("client: expected timeout occured\n");
133 fail ("client: handshake was completed unexpectedly\n");
143 /* These are global */
144 gnutls_anon_server_credentials_t anoncred
;
147 static gnutls_session_t
148 initialize_tls_session (void)
150 gnutls_session_t session
;
152 gnutls_init (&session
, GNUTLS_SERVER
);
154 /* avoid calling all the priority functions, since the defaults
157 gnutls_priority_set_direct (session
, "NORMAL:+ANON-ECDH", NULL
);
159 gnutls_credentials_set (session
, GNUTLS_CRD_ANON
, anoncred
);
165 server (int fd
, int wait
)
168 /* this must be called once in the program
170 gnutls_global_init ();
174 gnutls_global_set_log_function (server_log_func
);
175 gnutls_global_set_log_level (4711);
178 gnutls_anon_allocate_server_credentials (&anoncred
);
180 session
= initialize_tls_session ();
184 gnutls_transport_set_ptr (session
, (gnutls_transport_ptr_t
) fd
);
189 ret
= gnutls_handshake (session
);
191 while (ret
< 0 && gnutls_error_is_fatal(ret
) == 0);
193 gnutls_deinit (session
);
194 gnutls_anon_free_server_credentials(anoncred
);
195 gnutls_global_deinit();
203 static void start (int wait
)
209 fprintf(stderr
, "\nWill test timeout\n");
211 ret
= socketpair(AF_UNIX
, SOCK_STREAM
, 0, fd
);
214 perror("socketpair");
230 server (fd
[0], wait
);
236 client (fd
[1], wait
);
242 static void ch_handler(int sig
)
246 if (WEXITSTATUS(status
) != 0)
247 fail("Child died with status %d\n", WEXITSTATUS(status
));
254 signal(SIGCHLD
, ch_handler
);
256 /* make sure that normal handshake occurs */
259 /* check the handshake with an expected timeout */