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 static void terminate(void);
53 /* This program tests the rehandshake in DTLS
57 server_log_func (int level
, const char *str
)
59 fprintf (stderr
, "server|<%d>| %s", level
, str
);
63 client_log_func (int level
, const char *str
)
65 fprintf (stderr
, "client|<%d>| %s", level
, str
);
68 /* A very basic TLS client, with anonymous authentication.
72 #define MSG "Hello TLS"
74 gnutls_session_t session
;
77 push (gnutls_transport_ptr_t tr
, const void *data
, size_t len
)
79 int fd
= (long int)tr
;
81 return send(fd
, data
, len
, 0);
85 client (int fd
, int server_init
)
88 char buffer
[MAX_BUF
+ 1];
89 gnutls_anon_client_credentials_t anoncred
;
90 /* Need to enable anonymous KX specifically. */
92 gnutls_global_init ();
96 gnutls_global_set_log_function (client_log_func
);
97 gnutls_global_set_log_level (4711);
100 gnutls_anon_allocate_client_credentials (&anoncred
);
102 /* Initialize TLS session
104 gnutls_init (&session
, GNUTLS_CLIENT
|GNUTLS_DATAGRAM
);
105 gnutls_dtls_set_mtu( session
, 1500);
107 /* Use default priorities */
108 gnutls_priority_set_direct (session
, "NONE:+VERS-DTLS1.0:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-ECDH:+CURVE-ALL", NULL
);
110 /* put the anonymous credentials to the current session
112 gnutls_credentials_set (session
, GNUTLS_CRD_ANON
, anoncred
);
114 gnutls_transport_set_ptr (session
, (gnutls_transport_ptr_t
) fd
);
115 gnutls_transport_set_push_function (session
, push
);
117 /* Perform the TLS handshake
121 ret
= gnutls_handshake (session
);
123 while (ret
< 0 && gnutls_error_is_fatal(ret
) == 0);
127 fail ("client: Handshake failed\n");
134 success ("client: Handshake was completed\n");
138 success ("client: TLS version is: %s\n",
139 gnutls_protocol_get_name (gnutls_protocol_get_version
144 if (debug
) success("Initiating client rehandshake\n");
147 ret
= gnutls_handshake (session
);
149 while (ret
< 0 && gnutls_error_is_fatal(ret
) == 0);
153 fail ("2nd client gnutls_handshake: %s\n", gnutls_strerror(ret
));
160 ret
= gnutls_record_recv (session
, buffer
, MAX_BUF
);
161 } while (ret
== GNUTLS_E_AGAIN
|| ret
== GNUTLS_E_INTERRUPTED
);
167 success ("client: Peer has closed the TLS connection\n");
172 if (server_init
&& ret
== GNUTLS_E_REHANDSHAKE
)
174 if (debug
) success("Initiating rehandshake due to server request\n");
177 ret
= gnutls_handshake (session
);
179 while (ret
< 0 && gnutls_error_is_fatal(ret
) == 0);
184 fail ("client: Error: %s\n", gnutls_strerror (ret
));
190 ret
= gnutls_record_send (session
, MSG
, strlen (MSG
));
191 } while (ret
== GNUTLS_E_AGAIN
|| ret
== GNUTLS_E_INTERRUPTED
);
192 gnutls_bye (session
, GNUTLS_SHUT_WR
);
198 gnutls_deinit (session
);
200 gnutls_anon_free_client_credentials (anoncred
);
202 gnutls_global_deinit ();
206 /* These are global */
207 gnutls_anon_server_credentials_t anoncred
;
210 static gnutls_session_t
211 initialize_tls_session (void)
213 gnutls_session_t session
;
215 gnutls_init (&session
, GNUTLS_SERVER
|GNUTLS_DATAGRAM
);
216 gnutls_dtls_set_mtu( session
, 1500);
218 /* avoid calling all the priority functions, since the defaults
221 gnutls_priority_set_direct (session
, "NONE:+VERS-DTLS1.0:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-ECDH:+CURVE-ALL", NULL
);
223 gnutls_credentials_set (session
, GNUTLS_CRD_ANON
, anoncred
);
228 static void terminate(void)
232 kill(child
, SIGTERM
);
238 server (int fd
, int server_init
)
241 char buffer
[MAX_BUF
+ 1];
242 /* this must be called once in the program
244 gnutls_global_init ();
248 gnutls_global_set_log_function (server_log_func
);
249 gnutls_global_set_log_level (4711);
252 gnutls_anon_allocate_server_credentials (&anoncred
);
254 session
= initialize_tls_session ();
256 gnutls_transport_set_ptr (session
, (gnutls_transport_ptr_t
) fd
);
257 gnutls_transport_set_push_function (session
, push
);
261 ret
= gnutls_handshake (session
);
263 while (ret
< 0 && gnutls_error_is_fatal(ret
) == 0);
267 gnutls_deinit (session
);
268 fail ("server: Handshake has failed (%s)\n\n", gnutls_strerror (ret
));
272 success ("server: Handshake was completed\n");
275 success ("server: TLS version is: %s\n",
276 gnutls_protocol_get_name (gnutls_protocol_get_version
279 /* see the Getting peer's information example */
280 /* print_info(session); */
284 if (debug
) success("server: Sending dummy packet\n");
285 ret
= gnutls_rehandshake(session
);
288 fail ("gnutls_rehandshake: %s\n", gnutls_strerror(ret
));
292 if (debug
) success("server: Initiating rehandshake\n");
295 ret
= gnutls_handshake (session
);
297 while (ret
< 0 && gnutls_error_is_fatal(ret
) == 0);
301 fail ("server: 2nd gnutls_handshake: %s\n", gnutls_strerror(ret
));
308 memset (buffer
, 0, MAX_BUF
+ 1);
311 ret
= gnutls_record_recv (session
, buffer
, MAX_BUF
);
312 } while (ret
== GNUTLS_E_AGAIN
|| ret
== GNUTLS_E_INTERRUPTED
);
317 success ("server: Peer has closed the GnuTLS connection\n");
322 if (!server_init
&& ret
== GNUTLS_E_REHANDSHAKE
)
324 if (debug
) success("Initiating rehandshake due to client request\n");
327 ret
= gnutls_handshake (session
);
329 while (ret
< 0 && gnutls_error_is_fatal(ret
) == 0);
333 fail ("server: Received corrupted data(%s). Closing...\n", gnutls_strerror(ret
));
338 /* echo data back to the client
341 ret
= gnutls_record_send (session
, buffer
, strlen (buffer
));
342 } while (ret
== GNUTLS_E_AGAIN
|| ret
== GNUTLS_E_INTERRUPTED
);
347 /* do not wait for the peer to close the connection.
349 gnutls_bye (session
, GNUTLS_SHUT_WR
);
352 gnutls_deinit (session
);
354 gnutls_anon_free_server_credentials (anoncred
);
356 gnutls_global_deinit ();
359 success ("server: finished\n");
362 static void start (int server_initiated
)
367 ret
= socketpair(AF_UNIX
, SOCK_DGRAM
, 0, fd
);
370 perror("socketpair");
387 server (fd
[0], server_initiated
);
389 if (WEXITSTATUS(status
) != 0)
390 fail("Child died with status %d\n", WEXITSTATUS(status
));
395 client (fd
[1], server_initiated
);