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 client (int fd
, int wait
)
74 gnutls_anon_client_credentials_t anoncred
;
75 gnutls_session_t session
;
76 /* Need to enable anonymous KX specifically. */
78 gnutls_global_init ();
82 gnutls_global_set_log_function (client_log_func
);
83 gnutls_global_set_log_level (4711);
86 gnutls_anon_allocate_client_credentials (&anoncred
);
88 /* Initialize TLS session
90 gnutls_init (&session
, GNUTLS_CLIENT
);
91 gnutls_handshake_set_timeout( session
, 20*1000);
93 /* Use default priorities */
94 gnutls_priority_set_direct (session
, "NORMAL:+ANON-ECDH", NULL
);
96 /* put the anonymous credentials to the current session
98 gnutls_credentials_set (session
, GNUTLS_CRD_ANON
, anoncred
);
100 gnutls_transport_set_ptr (session
, (gnutls_transport_ptr_t
) fd
);
102 /* Perform the TLS handshake
106 ret
= gnutls_handshake (session
);
108 while (ret
< 0 && gnutls_error_is_fatal(ret
) == 0);
110 gnutls_deinit(session
);
111 gnutls_anon_free_client_credentials(anoncred
);
112 gnutls_global_deinit();
116 if (ret
!= GNUTLS_E_TIMEDOUT
|| wait
== 0)
118 if (debug
) fail("client: unexpected error: %s\n", gnutls_strerror(ret
));
121 if (debug
) success("client: expected timeout occured\n");
128 fail ("client: handshake was completed unexpectedly\n");
138 initialize_tls_session (gnutls_session_t
* session
)
140 gnutls_init (session
, GNUTLS_SERVER
);
142 /* avoid calling all the priority functions, since the defaults
145 gnutls_priority_set_direct (*session
, "NORMAL:+ANON-ECDH", NULL
);
149 server (int fd
, int wait
)
152 gnutls_session_t session
;
153 gnutls_anon_server_credentials_t anoncred
;
155 /* this must be called once in the program
157 gnutls_global_init ();
161 gnutls_global_set_log_function (server_log_func
);
162 gnutls_global_set_log_level (4711);
165 gnutls_anon_allocate_server_credentials (&anoncred
);
167 initialize_tls_session (&session
);
168 gnutls_credentials_set (session
, GNUTLS_CRD_ANON
, anoncred
);
170 gnutls_transport_set_ptr (session
, (gnutls_transport_ptr_t
) fd
);
175 ret
= gnutls_handshake (session
);
177 while (ret
< 0 && gnutls_error_is_fatal(ret
) == 0);
179 gnutls_deinit (session
);
180 gnutls_anon_free_server_credentials(anoncred
);
181 gnutls_global_deinit();
184 static void start (int wait
)
191 fprintf(stderr
, "\nWill test timeout\n");
193 ret
= socketpair(AF_UNIX
, SOCK_STREAM
, 0, fd
);
196 perror("socketpair");
212 server (fd
[0], wait
);
218 client (fd
[1], wait
);
224 static void ch_handler(int sig
)
228 if (WEXITSTATUS(status
) != 0)
229 fail("Child died with status %d\n", WEXITSTATUS(status
));
236 signal(SIGCHLD
, ch_handler
);
238 /* make sure that normal handshake occurs */
241 /* check the handshake with an expected timeout */