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
30 #if defined(_WIN32) || !defined(ENABLE_DTLS_SRTP)
32 int main(int argc, char **argv)
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
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 /* These are global */
69 #define MAX_KEY_MATERIAL 64*4
70 /* A very basic DTLS client, with anonymous authentication, that negotiates SRTP
73 static void client(int fd, int profile)
75 gnutls_session_t session;
77 gnutls_anon_client_credentials_t anoncred;
78 uint8_t km[MAX_KEY_MATERIAL];
79 char buf[2 * MAX_KEY_MATERIAL];
80 gnutls_datum_t cli_key, cli_salt, server_key, server_salt;
81 /* Need to enable anonymous KX specifically. */
86 gnutls_global_set_log_function(client_log_func);
87 gnutls_global_set_log_level(4711);
90 gnutls_anon_allocate_client_credentials(&anoncred);
92 /* Initialize TLS session
94 gnutls_init(&session, GNUTLS_CLIENT | GNUTLS_DATAGRAM);
95 gnutls_heartbeat_enable(session, GNUTLS_HB_PEER_ALLOWED_TO_SEND);
96 gnutls_dtls_set_mtu(session, 1500);
98 /* Use default priorities */
99 gnutls_priority_set_direct(session,
100 "NONE:+VERS-DTLS1.0:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-ECDH:+CURVE-ALL",
104 gnutls_srtp_set_profile_direct(session,
105 "SRTP_AES128_CM_HMAC_SHA1_80",
109 gnutls_srtp_set_profile_direct(session,
110 "SRTP_NULL_HMAC_SHA1_80",
118 /* put the anonymous credentials to the current session
120 gnutls_credentials_set(session, GNUTLS_CRD_ANON, anoncred);
122 gnutls_transport_set_int(session, fd);
124 /* Perform the TLS handshake
127 ret = gnutls_handshake(session);
129 while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
132 fail("client: Handshake failed\n");
137 success("client: Handshake was completed\n");
141 success("client: DTLS version is: %s\n",
142 gnutls_protocol_get_name
143 (gnutls_protocol_get_version(session)));
146 gnutls_srtp_get_keys(session, km, sizeof(km), &cli_key,
147 &cli_salt, &server_key, &server_salt);
154 size_t size = sizeof(buf);
155 gnutls_hex_encode(&cli_key, buf, &size);
156 success("Client key: %s\n", buf);
159 gnutls_hex_encode(&cli_salt, buf, &size);
160 success("Client salt: %s\n", buf);
163 gnutls_hex_encode(&server_key, buf, &size);
164 success("Server key: %s\n", buf);
167 gnutls_hex_encode(&server_salt, buf, &size);
168 success("Server salt: %s\n", buf);
172 gnutls_bye(session, GNUTLS_SHUT_WR);
176 gnutls_deinit(session);
178 gnutls_anon_free_client_credentials(anoncred);
180 gnutls_global_deinit();
183 static void terminate(void)
187 kill(child, SIGTERM);
192 static void server(int fd, int profile)
195 gnutls_session_t session;
196 gnutls_anon_server_credentials_t anoncred;
197 uint8_t km[MAX_KEY_MATERIAL];
198 char buf[2 * MAX_KEY_MATERIAL];
199 gnutls_datum_t cli_key, cli_salt, server_key, server_salt;
201 /* this must be called once in the program
206 gnutls_global_set_log_function(server_log_func);
207 gnutls_global_set_log_level(4711);
210 gnutls_anon_allocate_server_credentials(&anoncred);
212 gnutls_init(&session, GNUTLS_SERVER | GNUTLS_DATAGRAM);
213 gnutls_heartbeat_enable(session, GNUTLS_HB_PEER_ALLOWED_TO_SEND);
214 gnutls_dtls_set_mtu(session, 1500);
216 /* avoid calling all the priority functions, since the defaults
219 gnutls_priority_set_direct(session,
220 "NONE:+VERS-DTLS1.0:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-ECDH:+CURVE-ALL",
225 gnutls_srtp_set_profile_direct(session,
226 "SRTP_AES128_CM_HMAC_SHA1_80",
230 gnutls_srtp_set_profile_direct(session,
231 "SRTP_NULL_HMAC_SHA1_80",
238 gnutls_credentials_set(session, GNUTLS_CRD_ANON, anoncred);
240 gnutls_transport_set_int(session, fd);
243 ret = gnutls_handshake(session);
245 while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
248 gnutls_deinit(session);
249 fail("server: Handshake has failed (%s)\n\n",
250 gnutls_strerror(ret));
254 success("server: Handshake was completed\n");
257 success("server: TLS version is: %s\n",
258 gnutls_protocol_get_name
259 (gnutls_protocol_get_version(session)));
262 gnutls_srtp_get_keys(session, km, sizeof(km), &cli_key,
263 &cli_salt, &server_key, &server_salt);
270 size_t size = sizeof(buf);
271 gnutls_hex_encode(&cli_key, buf, &size);
272 success("Client key: %s\n", buf);
275 gnutls_hex_encode(&cli_salt, buf, &size);
276 success("Client salt: %s\n", buf);
279 gnutls_hex_encode(&server_key, buf, &size);
280 success("Server key: %s\n", buf);
283 gnutls_hex_encode(&server_salt, buf, &size);
284 success("Server salt: %s\n", buf);
287 /* do not wait for the peer to close the connection.
289 gnutls_bye(session, GNUTLS_SHUT_WR);
292 gnutls_deinit(session);
294 gnutls_anon_free_server_credentials(anoncred);
296 gnutls_global_deinit();
299 success("server: finished\n");
302 static void start(int profile)
307 ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
309 perror("socketpair");
324 server(fd[0], profile);
326 if (WEXITSTATUS(status) != 0)
327 fail("Child died with status %d\n",
328 WEXITSTATUS(status));
331 client(fd[1], profile);