Added new functions to convert types to strings.
[gnutls.git] / tests / mini-dtls-srtp.c
blob8f41d619e4443a591b7305d25fab577674e99e85
1 /*
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
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
27 #include <stdio.h>
28 #include <stdlib.h>
30 #if defined(_WIN32)
32 int
33 main ()
35 exit (77);
38 #else
40 #include <string.h>
41 #include <sys/types.h>
42 #include <netinet/in.h>
43 #include <sys/socket.h>
44 #include <sys/wait.h>
45 #include <arpa/inet.h>
46 #include <unistd.h>
47 #include <gnutls/gnutls.h>
48 #include <gnutls/dtls.h>
50 #include "utils.h"
52 static void terminate (void);
54 /* This program tests the rehandshake in DTLS
57 static void
58 server_log_func (int level, const char *str)
60 fprintf (stderr, "server|<%d>| %s", level, str);
63 static void
64 client_log_func (int level, const char *str)
66 fprintf (stderr, "client|<%d>| %s", level, str);
69 /* These are global */
70 static pid_t child;
72 /* A very basic DTLS client, with anonymous authentication, that exchanges heartbeats.
75 static void
76 client (int fd, int profile)
78 gnutls_session_t session;
79 int ret;
80 gnutls_anon_client_credentials_t anoncred;
81 /* Need to enable anonymous KX specifically. */
83 gnutls_global_init ();
85 if (debug)
87 gnutls_global_set_log_function (client_log_func);
88 gnutls_global_set_log_level (4711);
91 gnutls_anon_allocate_client_credentials (&anoncred);
93 /* Initialize TLS session
95 gnutls_init (&session, GNUTLS_CLIENT | GNUTLS_DATAGRAM);
96 gnutls_heartbeat_enable (session, GNUTLS_HB_PEER_ALLOWED_TO_SEND);
97 gnutls_dtls_set_mtu (session, 1500);
99 /* Use default priorities */
100 gnutls_priority_set_direct (session,
101 "NONE:+VERS-DTLS1.0:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-ECDH:+CURVE-ALL",
102 NULL);
103 if (profile)
104 ret = gnutls_srtp_set_profile_direct(session, "SRTP_AES128_CM_SHA1_80",
105 NULL);
106 else
107 ret = gnutls_srtp_set_profile_direct(session, "SRTP_NULL_SHA1_80",
108 NULL);
109 if (ret < 0)
111 gnutls_perror(ret);
112 exit(1);
116 /* put the anonymous credentials to the current session
118 gnutls_credentials_set (session, GNUTLS_CRD_ANON, anoncred);
120 gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) fd);
122 /* Perform the TLS handshake
126 ret = gnutls_handshake (session);
128 while (ret < 0 && gnutls_error_is_fatal (ret) == 0);
130 if (ret < 0)
132 fail ("client: Handshake failed\n");
133 gnutls_perror (ret);
134 exit (1);
136 else
138 if (debug)
139 success ("client: Handshake was completed\n");
142 if (debug)
143 success ("client: DTLS version is: %s\n",
144 gnutls_protocol_get_name (gnutls_protocol_get_version
145 (session)));
148 ret = gnutls_prf(session, sizeof("EXTRACTOR-dtls_srtp")-1, "EXTRACTOR-dtls_srtp", 0, ctx_len, ctx, 32, out);
149 if (ret < 0)
151 gnutls_perror(ret);
152 exit(1);
155 gnutls_bye (session, GNUTLS_SHUT_WR);
157 close (fd);
159 gnutls_deinit (session);
161 gnutls_anon_free_client_credentials (anoncred);
163 gnutls_global_deinit ();
166 static void
167 terminate (void)
169 int status;
171 kill (child, SIGTERM);
172 wait (&status);
173 exit (1);
176 static void
177 server (int fd, int profile)
179 int ret;
180 gnutls_session_t session;
181 gnutls_anon_server_credentials_t anoncred;
182 /* this must be called once in the program
184 gnutls_global_init ();
186 if (debug)
188 gnutls_global_set_log_function (server_log_func);
189 gnutls_global_set_log_level (4711);
192 gnutls_anon_allocate_server_credentials (&anoncred);
194 gnutls_init (&session, GNUTLS_SERVER | GNUTLS_DATAGRAM);
195 gnutls_heartbeat_enable (session, GNUTLS_HB_PEER_ALLOWED_TO_SEND);
196 gnutls_dtls_set_mtu (session, 1500);
198 /* avoid calling all the priority functions, since the defaults
199 * are adequate.
201 gnutls_priority_set_direct (session,
202 "NONE:+VERS-DTLS1.0:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-ECDH:+CURVE-ALL",
203 NULL);
205 if (profile)
206 ret = gnutls_srtp_set_profile_direct(session, "SRTP_AES128_CM_SHA1_80",
207 NULL);
208 else
209 ret = gnutls_srtp_set_profile_direct(session, "SRTP_NULL_SHA1_80",
210 NULL);
211 if (ret < 0)
213 gnutls_perror(ret);
214 exit(1);
217 gnutls_credentials_set (session, GNUTLS_CRD_ANON, anoncred);
219 gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) fd);
223 ret = gnutls_handshake (session);
225 while (ret < 0 && gnutls_error_is_fatal (ret) == 0);
226 if (ret < 0)
228 close (fd);
229 gnutls_deinit (session);
230 fail ("server: Handshake has failed (%s)\n\n",
231 gnutls_strerror (ret));
232 terminate ();
234 if (debug)
235 success ("server: Handshake was completed\n");
237 if (debug)
238 success ("server: TLS version is: %s\n",
239 gnutls_protocol_get_name (gnutls_protocol_get_version
240 (session)));
243 ret = gnutls_prf(session, sizeof("EXTRACTOR-dtls_srtp")-1, "EXTRACTOR-dtls_srtp", 0, ctx_len, ctx, 32, out);
244 if (ret < 0)
246 gnutls_perror(ret);
247 exit(1);
251 /* do not wait for the peer to close the connection.
253 gnutls_bye (session, GNUTLS_SHUT_WR);
255 close (fd);
256 gnutls_deinit (session);
258 gnutls_anon_free_server_credentials (anoncred);
260 gnutls_global_deinit ();
262 if (debug)
263 success ("server: finished\n");
266 static void
267 start (int profile)
269 int fd[2];
270 int ret;
272 ret = socketpair (AF_UNIX, SOCK_DGRAM, 0, fd);
273 if (ret < 0)
275 perror ("socketpair");
276 exit (1);
279 child = fork ();
280 if (child < 0)
282 perror ("fork");
283 fail ("fork");
284 exit (1);
287 if (child)
289 int status;
290 /* parent */
292 server (fd[0], profile);
293 wait (&status);
294 if (WEXITSTATUS (status) != 0)
295 fail ("Child died with status %d\n", WEXITSTATUS (status));
297 else
299 close (fd[0]);
300 client (fd[1], profile);
301 exit (0);
305 void
306 doit (void)
308 start (0);
309 start (1);
312 #endif /* _WIN32 */