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 a DTLS handshake would timeout
56 static void print_type(const unsigned char* buf
, int size
)
58 if (buf
[0] == 22 && size
>= 13) {
60 fprintf(stderr
, "Client Hello\n");
61 else if (buf
[13] == 2)
62 fprintf(stderr
, "Server Hello\n");
63 else if (buf
[13] == 12)
64 fprintf(stderr
, "Server Key exchange\n");
65 else if (buf
[13] == 14)
66 fprintf(stderr
, "Server Hello Done\n");
67 else if (buf
[13] == 11)
68 fprintf(stderr
, "Certificate\n");
69 else if (buf
[13] == 16)
70 fprintf(stderr
, "Client Key Exchange\n");
72 fprintf(stderr
, "Finished\n");
73 else if (buf
[13] == 11)
74 fprintf(stderr
, "Server Hello Done\n");
76 fprintf(stderr
, "Unknown handshake\n");
77 } else if (buf
[0] == 20) {
78 fprintf(stderr
, "Change Cipher Spec\n");
80 fprintf(stderr
, "Unknown\n");
84 server_log_func (int level
, const char *str
)
86 fprintf (stderr
, "server|<%d>| %s", level
, str
);
90 client_log_func (int level
, const char *str
)
92 fprintf (stderr
, "client|<%d>| %s", level
, str
);
95 /* A very basic TLS client, with anonymous authentication.
99 static int packet_to_lose
;
100 gnutls_session_t session
;
103 push (gnutls_transport_ptr_t tr
, const void *data
, size_t len
)
105 int fd
= (long int)tr
;
109 if (packet_to_lose
!= -1 && packet_to_lose
== counter
) {
112 fprintf(stderr
, "Discarding packet %d: ", counter
);
113 print_type(data
, len
);
120 return send(fd
, data
, len
, 0);
124 client (int fd
, int packet
)
127 gnutls_anon_client_credentials_t anoncred
;
128 /* Need to enable anonymous KX specifically. */
130 gnutls_global_init ();
134 gnutls_global_set_log_function (client_log_func
);
135 gnutls_global_set_log_level (4711);
138 gnutls_anon_allocate_client_credentials (&anoncred
);
140 /* Initialize TLS session
142 gnutls_init (&session
, GNUTLS_CLIENT
|GNUTLS_DATAGRAM
);
143 gnutls_dtls_set_mtu( session
, 1500);
145 /* Use default priorities */
146 gnutls_priority_set_direct (session
, "NONE:+VERS-DTLS1.0:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-ECDH:+CURVE-ALL", NULL
);
148 /* put the anonymous credentials to the current session
150 gnutls_credentials_set (session
, GNUTLS_CRD_ANON
, anoncred
);
153 packet_to_lose
= packet
;
155 gnutls_transport_set_ptr (session
, (gnutls_transport_ptr_t
) fd
);
156 gnutls_transport_set_push_function (session
, push
);
158 /* Perform the TLS handshake
162 ret
= gnutls_handshake (session
);
164 while (ret
< 0 && gnutls_error_is_fatal(ret
) == 0);
166 gnutls_deinit(session
);
167 gnutls_global_deinit();
171 if (ret
== GNUTLS_E_TIMEDOUT
) return;
172 fail ("client: Handshake failed\n");
179 success ("client: Handshake was completed\n");
186 /* These are global */
187 gnutls_anon_server_credentials_t anoncred
;
190 static gnutls_session_t
191 initialize_tls_session (void)
193 gnutls_session_t session
;
195 gnutls_init (&session
, GNUTLS_SERVER
|GNUTLS_DATAGRAM
);
196 gnutls_dtls_set_mtu( session
, 1500);
198 /* avoid calling all the priority functions, since the defaults
201 gnutls_priority_set_direct (session
, "NONE:+VERS-DTLS1.0:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-ECDH:+CURVE-ALL", NULL
);
203 gnutls_credentials_set (session
, GNUTLS_CRD_ANON
, anoncred
);
209 server (int fd
, int packet
)
212 /* this must be called once in the program
214 gnutls_global_init ();
218 gnutls_global_set_log_function (server_log_func
);
219 gnutls_global_set_log_level (4711);
222 gnutls_anon_allocate_server_credentials (&anoncred
);
224 session
= initialize_tls_session ();
227 packet_to_lose
= packet
;
229 gnutls_transport_set_ptr (session
, (gnutls_transport_ptr_t
) fd
);
230 gnutls_transport_set_push_function (session
, push
);
234 ret
= gnutls_handshake (session
);
236 while (ret
< 0 && gnutls_error_is_fatal(ret
) == 0);
238 gnutls_deinit (session
);
239 gnutls_global_deinit();
247 static void start (int server_packet
, int client_packet
)
253 fprintf(stderr
, "\nWill discard %s packet %d\n",
254 (client_packet
!=-1)?"client":"server", (client_packet
!=-1)?client_packet
:server_packet
);
256 ret
= socketpair(AF_UNIX
, SOCK_DGRAM
, 0, fd
);
259 perror("socketpair");
275 server (fd
[0], server_packet
);
277 kill(child
, SIGTERM
);
282 client (fd
[1], client_packet
);
288 static void ch_handler(int sig
)
292 if (WEXITSTATUS(status
) != 0)
293 fail("Child died with status %d\n", WEXITSTATUS(status
));
300 time_t tstart
, tstop
;
302 signal(SIGCHLD
, ch_handler
);
309 tstop
= tstop
- tstart
;
311 if (!(tstop
< 70 && tstop
> 55))
312 fail("Time difference: %u\n", (unsigned)tstop
);