2
* Copyright (C) 2013 Nikos Mavrogiannopoulos
4
* This file is part of GnuTLS.
6
* GnuTLS is free software; you can redistribute it and/or modify it
7
* under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 3 of the License, or
9
* (at your option) any later version.
11
* GnuTLS is distributed in the hope that it will be useful, but
12
* WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with GnuTLS; if not, write to the Free Software Foundation,
18
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
28
#if defined(_WIN32) || !defined(ENABLE_ALPN)
31
main (int argc, char** argv)
39
#include <sys/types.h>
40
#include <netinet/in.h>
41
#include <sys/socket.h>
43
#include <arpa/inet.h>
45
#include <gnutls/gnutls.h>
46
#include <gnutls/dtls.h>
50
static void terminate (void);
52
/* This program tests the rehandshake in DTLS
56
server_log_func (int level, const char *str)
58
fprintf (stderr, "server|<%d>| %s", level, str);
62
client_log_func (int level, const char *str)
64
fprintf (stderr, "client|<%d>| %s", level, str);
67
/* These are global */
70
/* A very basic DTLS client, with anonymous authentication, that negotiates SRTP
74
client (int fd, const char* protocol1, const char* protocol2)
76
gnutls_session_t session;
79
gnutls_anon_client_credentials_t anoncred;
80
/* 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);
96
/* Use default priorities */
97
gnutls_priority_set_direct (session,
98
"NONE:+VERS-TLS1.0:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-ECDH:+CURVE-ALL",
103
t[0].data = (void*)protocol1;
104
t[0].size = strlen(protocol1);
105
t[1].data = (void*)protocol2;
106
t[1].size = strlen(protocol2);
108
ret = gnutls_alpn_set_protocols(session, t, 2, 0);
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);
132
fail ("client: Handshake failed\n");
139
success ("client: Handshake was completed\n");
143
success ("client: TLS version is: %s\n",
144
gnutls_protocol_get_name (gnutls_protocol_get_version
147
ret = gnutls_alpn_get_selected_protocol(session, &proto);
156
fprintf(stderr, "selected protocol: %.*s\n", (int)proto.size, proto.data);
160
gnutls_bye (session, GNUTLS_SHUT_WR);
164
gnutls_deinit (session);
166
gnutls_anon_free_client_credentials (anoncred);
168
gnutls_global_deinit ();
176
kill (child, SIGTERM);
182
server (int fd, const char* protocol1, const char* protocol2)
185
gnutls_session_t session;
186
gnutls_anon_server_credentials_t anoncred;
189
/* this must be called once in the program
195
gnutls_global_set_log_function (server_log_func);
196
gnutls_global_set_log_level (4711);
199
gnutls_anon_allocate_server_credentials (&anoncred);
201
gnutls_init (&session, GNUTLS_SERVER);
203
/* avoid calling all the priority functions, since the defaults
206
gnutls_priority_set_direct (session,
207
"NONE:+VERS-TLS1.0:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-ECDH:+CURVE-ALL",
210
t[0].data = (void*)protocol1;
211
t[0].size = strlen(protocol1);
212
t[1].data = (void*)protocol2;
213
t[1].size = strlen(protocol2);
215
ret = gnutls_alpn_set_protocols(session, t, 2, 0);
222
gnutls_credentials_set (session, GNUTLS_CRD_ANON, anoncred);
224
gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) fd);
228
ret = gnutls_handshake (session);
230
while (ret < 0 && gnutls_error_is_fatal (ret) == 0);
234
gnutls_deinit (session);
235
fail ("server: Handshake has failed (%s)\n\n",
236
gnutls_strerror (ret));
240
success ("server: Handshake was completed\n");
243
success ("server: TLS version is: %s\n",
244
gnutls_protocol_get_name (gnutls_protocol_get_version
247
ret = gnutls_alpn_get_selected_protocol(session, &t[0]);
257
success ("Protocol: %.*s\n", (int)t[0].size, t[0].data);
261
/* do not wait for the peer to close the connection.
263
gnutls_bye (session, GNUTLS_SHUT_WR);
266
gnutls_deinit (session);
268
gnutls_anon_free_server_credentials (anoncred);
270
gnutls_global_deinit ();
273
success ("server: finished\n");
277
start (const char* p1, const char* p2)
282
ret = socketpair (AF_UNIX, SOCK_STREAM, 0, fd);
285
perror ("socketpair");
302
server (fd[0], p1, p2);
304
if (WEXITSTATUS (status) != 0)
305
fail ("Child died with status %d\n", WEXITSTATUS (status));
310
client (fd[1], p2, p1);
318
start ("spdy/2", "spdy/3");
319
start ("spdy/3", "spdy/2");