2 * Copyright (C) 2013 Nikos Mavrogiannopoulos
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 client hello verify in DTLS
56 static void server_log_func(int level, const char *str)
58 fprintf(stderr, "server|<%d>| %s", level, str);
61 static void client_log_func(int level, const char *str)
63 fprintf(stderr, "client|<%d>| %s", level, str);
66 /* A very basic TLS client, with anonymous authentication.
72 push(gnutls_transport_ptr_t tr, const void *data, size_t len)
74 int fd = (long int) tr;
76 return send(fd, data, len, 0);
79 static void client(int fd)
82 char buffer[MAX_BUF + 1];
83 gnutls_anon_client_credentials_t anoncred;
84 gnutls_session_t session;
85 /* Need to enable anonymous KX specifically. */
90 gnutls_global_set_log_function(client_log_func);
91 gnutls_global_set_log_level(4711);
94 gnutls_anon_allocate_client_credentials(&anoncred);
96 /* Initialize TLS session
98 gnutls_init(&session, GNUTLS_CLIENT | GNUTLS_DATAGRAM);
99 gnutls_dtls_set_mtu(session, 1500);
100 gnutls_handshake_set_timeout(session, 20 * 1000);
102 /* Use default priorities */
103 gnutls_priority_set_direct(session,
104 "NONE:+VERS-DTLS-ALL:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-ECDH:+CURVE-ALL",
107 /* put the anonymous credentials to the current session
109 gnutls_credentials_set(session, GNUTLS_CRD_ANON, anoncred);
111 gnutls_transport_set_int(session, fd);
112 gnutls_transport_set_push_function(session, push);
114 /* Perform the TLS handshake
117 ret = gnutls_handshake(session);
119 while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
122 fail("client: Handshake failed\n");
127 success("client: Handshake was completed\n");
131 success("client: TLS version is: %s\n",
132 gnutls_protocol_get_name
133 (gnutls_protocol_get_version(session)));
136 ret = gnutls_record_recv(session, buffer, MAX_BUF);
137 } while (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED);
142 ("client: Peer has closed the TLS connection\n");
144 } else if (ret < 0) {
145 fail("client: Error: %s\n", gnutls_strerror(ret));
149 gnutls_bye(session, GNUTLS_SHUT_WR);
155 gnutls_deinit(session);
157 gnutls_anon_free_client_credentials(anoncred);
159 gnutls_global_deinit();
163 /* These are global */
166 static void terminate(void)
170 kill(child, SIGTERM);
175 #define CLI_ADDR (void*)"test"
176 #define CLI_ADDR_LEN 4
178 static void server(int fd)
181 gnutls_anon_server_credentials_t anoncred;
182 char buffer[MAX_BUF + 1];
183 gnutls_datum_t cookie_key;
184 gnutls_dtls_prestate_st prestate;
185 gnutls_session_t session;
187 /* this must be called once in the program
192 gnutls_global_set_log_function(server_log_func);
193 gnutls_global_set_log_level(4711);
196 ret = gnutls_key_generate(&cookie_key, GNUTLS_COOKIE_KEY_SIZE);
198 fail("Cannot generate key: %s\n", gnutls_strerror(ret));
202 gnutls_anon_allocate_server_credentials(&anoncred);
204 gnutls_init(&session, GNUTLS_SERVER | GNUTLS_DATAGRAM);
205 gnutls_handshake_set_timeout(session, 20 * 1000);
206 gnutls_dtls_set_mtu(session, 1500);
208 /* avoid calling all the priority functions, since the defaults
211 gnutls_priority_set_direct(session,
212 "NONE:+VERS-DTLS1.0:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-ECDH:+CURVE-ALL",
215 gnutls_credentials_set(session, GNUTLS_CRD_ANON, anoncred);
217 gnutls_transport_set_int(session, fd);
218 gnutls_transport_set_push_function(session, push);
221 ret = recv(fd, buffer, sizeof(buffer), MSG_PEEK);
223 fail("Cannot receive data\n");
227 memset(&prestate, 0, sizeof(prestate));
229 gnutls_dtls_cookie_verify(&cookie_key, CLI_ADDR,
230 CLI_ADDR_LEN, buffer, ret,
232 if (ret < 0) { /* cookie not valid */
234 success("Sending hello verify request\n");
237 gnutls_dtls_cookie_send(&cookie_key, CLI_ADDR,
240 (gnutls_transport_ptr_t)
243 fail("Cannot send data\n");
247 /* discard peeked data */
248 recv(fd, buffer, sizeof(buffer), 0);
252 fail("too many cookies sent\n");
263 gnutls_dtls_prestate_set(session, &prestate);
266 ret = gnutls_handshake(session);
268 while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
271 gnutls_deinit(session);
272 fail("server: Handshake has failed (%s)\n\n",
273 gnutls_strerror(ret));
277 success("server: Handshake was completed\n");
280 success("server: TLS version is: %s\n",
281 gnutls_protocol_get_name
282 (gnutls_protocol_get_version(session)));
284 /* see the Getting peer's information example */
285 /* print_info(session); */
288 ret = gnutls_record_send(session, buffer, sizeof(buffer));
289 } while (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED);
293 gnutls_deinit(session);
294 fail("server: data sending has failed (%s)\n\n",
295 gnutls_strerror(ret));
300 /* do not wait for the peer to close the connection.
302 gnutls_bye(session, GNUTLS_SHUT_WR);
305 gnutls_deinit(session);
307 gnutls_anon_free_server_credentials(anoncred);
308 gnutls_free(cookie_key.data);
310 gnutls_global_deinit();
313 success("server: finished\n");
321 ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
323 perror("socketpair");
340 if (WEXITSTATUS(status) != 0)
341 fail("Child died with status %d\n",
342 WEXITSTATUS(status));