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>
51 /* This program simulates packet loss in DTLS datagrams using
52 * the blocking functions. Idea taken from test app of
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 #define MSG "Hello TLS"
102 static int packet_to_lose
;
103 gnutls_session_t session
;
106 push (gnutls_transport_ptr_t tr
, const void *data
, size_t len
)
108 int fd
= (long int)tr
;
112 if (packet_to_lose
!= -1 && packet_to_lose
== counter
) {
115 fprintf(stderr
, "Discarding packet %d: ", counter
);
116 print_type(data
, len
);
120 return send(fd
, data
, len
, 0);
124 client (int fd
, int packet
)
127 char buffer
[MAX_BUF
+ 1];
128 gnutls_anon_client_credentials_t anoncred
;
129 /* Need to enable anonymous KX specifically. */
131 gnutls_global_init ();
135 gnutls_global_set_log_function (client_log_func
);
136 gnutls_global_set_log_level (4711);
139 gnutls_anon_allocate_client_credentials (&anoncred
);
141 /* Initialize TLS session
143 gnutls_init (&session
, GNUTLS_CLIENT
|GNUTLS_DATAGRAM
);
144 gnutls_dtls_set_mtu( session
, 1500);
146 /* Use default priorities */
147 gnutls_priority_set_direct (session
, "NONE:+VERS-DTLS1.0:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-ECDH:+CURVE-ALL", NULL
);
149 /* put the anonymous credentials to the current session
151 gnutls_credentials_set (session
, GNUTLS_CRD_ANON
, anoncred
);
154 packet_to_lose
= packet
;
156 gnutls_transport_set_ptr (session
, (gnutls_transport_ptr_t
) fd
);
157 gnutls_transport_set_push_function (session
, push
);
159 /* Perform the TLS handshake
163 ret
= gnutls_handshake (session
);
165 while (ret
< 0 && gnutls_error_is_fatal(ret
) == 0);
169 fail ("client: Handshake failed\n");
176 success ("client: Handshake was completed\n");
180 success ("client: TLS version is: %s\n",
181 gnutls_protocol_get_name (gnutls_protocol_get_version
185 ret
= gnutls_record_send (session
, MSG
, strlen (MSG
));
186 } while (ret
== GNUTLS_E_AGAIN
|| ret
== GNUTLS_E_INTERRUPTED
);
189 ret
= gnutls_record_recv (session
, buffer
, MAX_BUF
);
190 } while (ret
== GNUTLS_E_AGAIN
|| ret
== GNUTLS_E_INTERRUPTED
);
195 success ("client: Peer has closed the TLS connection\n");
200 fail ("client: Error: %s\n", gnutls_strerror (ret
));
206 printf ("- Received %d bytes: ", ret
);
207 for (ii
= 0; ii
< ret
; ii
++)
209 fputc (buffer
[ii
], stdout
);
211 fputs ("\n", stdout
);
214 gnutls_bye (session
, GNUTLS_SHUT_RDWR
);
220 gnutls_deinit (session
);
222 gnutls_anon_free_client_credentials (anoncred
);
224 gnutls_global_deinit ();
228 /* These are global */
229 gnutls_anon_server_credentials_t anoncred
;
232 static gnutls_session_t
233 initialize_tls_session (void)
235 gnutls_session_t session
;
237 gnutls_init (&session
, GNUTLS_SERVER
|GNUTLS_DATAGRAM
);
238 gnutls_dtls_set_mtu( session
, 1500);
240 /* avoid calling all the priority functions, since the defaults
243 gnutls_priority_set_direct (session
, "NONE:+VERS-DTLS1.0:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-ECDH:+CURVE-ALL", NULL
);
245 gnutls_credentials_set (session
, GNUTLS_CRD_ANON
, anoncred
);
250 static void terminate(void)
254 kill(child
, SIGTERM
);
260 server (int fd
, int packet
)
263 char buffer
[MAX_BUF
+ 1];
264 /* this must be called once in the program
266 gnutls_global_init ();
270 gnutls_global_set_log_function (server_log_func
);
271 gnutls_global_set_log_level (4711);
274 gnutls_anon_allocate_server_credentials (&anoncred
);
276 session
= initialize_tls_session ();
279 packet_to_lose
= packet
;
281 gnutls_transport_set_ptr (session
, (gnutls_transport_ptr_t
) fd
);
282 gnutls_transport_set_push_function (session
, push
);
286 ret
= gnutls_handshake (session
);
288 while (ret
< 0 && gnutls_error_is_fatal(ret
) == 0);
292 gnutls_deinit (session
);
293 fail ("server: Handshake has failed (%s)\n\n", gnutls_strerror (ret
));
297 success ("server: Handshake was completed\n");
300 success ("server: TLS version is: %s\n",
301 gnutls_protocol_get_name (gnutls_protocol_get_version
304 /* see the Getting peer's information example */
305 /* print_info(session); */
309 memset (buffer
, 0, MAX_BUF
+ 1);
312 ret
= gnutls_record_recv (session
, buffer
, MAX_BUF
);
313 } while (ret
== GNUTLS_E_AGAIN
|| ret
== GNUTLS_E_INTERRUPTED
);
318 success ("server: Peer has closed the GnuTLS connection\n");
323 fail ("server: Received corrupted data(%d). Closing...\n", ret
);
328 /* echo data back to the client
331 ret
= gnutls_record_send (session
, buffer
, strlen (buffer
));
332 } while (ret
== GNUTLS_E_AGAIN
|| ret
== GNUTLS_E_INTERRUPTED
);
335 /* do not wait for the peer to close the connection.
337 gnutls_bye (session
, GNUTLS_SHUT_WR
);
340 gnutls_deinit (session
);
342 gnutls_anon_free_server_credentials (anoncred
);
344 gnutls_global_deinit ();
347 success ("server: finished\n");
350 static void start (int server_packet
, int client_packet
)
356 fprintf(stderr
, "\nWill discard %s packet %d\n",
357 (client_packet
!=-1)?"client":"server", (client_packet
!=-1)?client_packet
:server_packet
);
359 ret
= socketpair(AF_LOCAL
, SOCK_DGRAM
, 0, fd
);
362 perror("socketpair");
378 server (fd
[0], server_packet
);
380 if (WEXITSTATUS(status
) != 0)
381 fail("Child died with status %d\n", WEXITSTATUS(status
));
385 client (fd
[1], client_packet
);