~ubuntu-branches/ubuntu/saucy/gnutls28/saucy

« back to all changes in this revision

Viewing changes to tests/mini-alpn.c

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-07-30 21:40:07 UTC
  • mfrom: (14.1.9 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130730214007-9mrd08xo61kla008
Tags: 3.2.3-1ubuntu1
* Sync with Debian (LP: #1068029). Remaining change:
  - Drop gnutls-bin and -doc since we want to use the versions
    in gnutls26 as the defaults instead

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2013 Nikos Mavrogiannopoulos
 
3
 *
 
4
 * This file is part of GnuTLS.
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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
 
19
 */
 
20
 
 
21
#ifdef HAVE_CONFIG_H
 
22
#include <config.h>
 
23
#endif
 
24
 
 
25
#include <stdio.h>
 
26
#include <stdlib.h>
 
27
 
 
28
#if defined(_WIN32) || !defined(ENABLE_ALPN)
 
29
 
 
30
int
 
31
main (int argc, char** argv)
 
32
{
 
33
    exit (77);
 
34
}
 
35
 
 
36
#else
 
37
 
 
38
#include <string.h>
 
39
#include <sys/types.h>
 
40
#include <netinet/in.h>
 
41
#include <sys/socket.h>
 
42
#include <sys/wait.h>
 
43
#include <arpa/inet.h>
 
44
#include <unistd.h>
 
45
#include <gnutls/gnutls.h>
 
46
#include <gnutls/dtls.h>
 
47
 
 
48
#include "utils.h"
 
49
 
 
50
static void terminate (void);
 
51
 
 
52
/* This program tests the rehandshake in DTLS
 
53
 */
 
54
 
 
55
static void
 
56
server_log_func (int level, const char *str)
 
57
{
 
58
    fprintf (stderr, "server|<%d>| %s", level, str);
 
59
}
 
60
 
 
61
static void
 
62
client_log_func (int level, const char *str)
 
63
{
 
64
    fprintf (stderr, "client|<%d>| %s", level, str);
 
65
}
 
66
 
 
67
/* These are global */
 
68
static pid_t child;
 
69
 
 
70
/* A very basic DTLS client, with anonymous authentication, that negotiates SRTP
 
71
 */
 
72
 
 
73
static void
 
74
client (int fd, const char* protocol1, const char* protocol2)
 
75
{
 
76
    gnutls_session_t session;
 
77
    int ret;
 
78
    gnutls_datum_t proto;
 
79
    gnutls_anon_client_credentials_t anoncred;
 
80
    /* Need to enable anonymous KX specifically. */
 
81
 
 
82
    global_init ();
 
83
 
 
84
    if (debug)
 
85
      {
 
86
          gnutls_global_set_log_function (client_log_func);
 
87
          gnutls_global_set_log_level (4711);
 
88
      }
 
89
 
 
90
    gnutls_anon_allocate_client_credentials (&anoncred);
 
91
 
 
92
    /* Initialize TLS session
 
93
     */
 
94
    gnutls_init (&session, GNUTLS_CLIENT);
 
95
 
 
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",
 
99
                                NULL);
 
100
    if (protocol1)
 
101
      {
 
102
        gnutls_datum_t t[2];
 
103
        t[0].data = (void*)protocol1;
 
104
        t[0].size = strlen(protocol1);
 
105
        t[1].data = (void*)protocol2;
 
106
        t[1].size = strlen(protocol2);
 
107
        
 
108
        ret = gnutls_alpn_set_protocols(session, t, 2, 0);
 
109
        if (ret < 0)
 
110
          {
 
111
            gnutls_perror(ret);
 
112
            exit(1);
 
113
          }
 
114
      }
 
115
 
 
116
    /* put the anonymous credentials to the current session
 
117
     */
 
118
    gnutls_credentials_set (session, GNUTLS_CRD_ANON, anoncred);
 
119
 
 
120
    gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) fd);
 
121
 
 
122
    /* Perform the TLS handshake
 
123
     */
 
124
    do
 
125
      {
 
126
          ret = gnutls_handshake (session);
 
127
      }
 
128
    while (ret < 0 && gnutls_error_is_fatal (ret) == 0);
 
129
 
 
130
    if (ret < 0)
 
131
      {
 
132
          fail ("client: Handshake failed\n");
 
133
          gnutls_perror (ret);
 
134
          exit (1);
 
135
      }
 
136
    else
 
137
      {
 
138
          if (debug)
 
139
              success ("client: Handshake was completed\n");
 
140
      }
 
141
 
 
142
    if (debug)
 
143
        success ("client: TLS version is: %s\n",
 
144
                 gnutls_protocol_get_name (gnutls_protocol_get_version
 
145
                                           (session)));
 
146
    
 
147
    ret = gnutls_alpn_get_selected_protocol(session, &proto);
 
148
    if (ret < 0)
 
149
      {
 
150
        gnutls_perror(ret);
 
151
        exit(1);
 
152
      }
 
153
 
 
154
    if (debug)
 
155
      {
 
156
        fprintf(stderr, "selected protocol: %.*s\n", (int)proto.size, proto.data);
 
157
      }
 
158
 
 
159
 
 
160
    gnutls_bye (session, GNUTLS_SHUT_WR);
 
161
 
 
162
    close (fd);
 
163
 
 
164
    gnutls_deinit (session);
 
165
 
 
166
    gnutls_anon_free_client_credentials (anoncred);
 
167
 
 
168
    gnutls_global_deinit ();
 
169
}
 
170
 
 
171
static void
 
172
terminate (void)
 
173
{
 
174
    int status;
 
175
 
 
176
    kill (child, SIGTERM);
 
177
    wait (&status);
 
178
    exit (1);
 
179
}
 
180
 
 
181
static void
 
182
server (int fd, const char* protocol1, const char* protocol2)
 
183
{
 
184
    int ret;
 
185
    gnutls_session_t session;
 
186
    gnutls_anon_server_credentials_t anoncred;
 
187
    gnutls_datum_t t[2];
 
188
 
 
189
    /* this must be called once in the program
 
190
     */
 
191
    global_init ();
 
192
 
 
193
    if (debug)
 
194
      {
 
195
          gnutls_global_set_log_function (server_log_func);
 
196
          gnutls_global_set_log_level (4711);
 
197
      }
 
198
 
 
199
    gnutls_anon_allocate_server_credentials (&anoncred);
 
200
 
 
201
    gnutls_init (&session, GNUTLS_SERVER);
 
202
 
 
203
    /* avoid calling all the priority functions, since the defaults
 
204
     * are adequate.
 
205
     */
 
206
    gnutls_priority_set_direct (session,
 
207
                                "NONE:+VERS-TLS1.0:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-ECDH:+CURVE-ALL",
 
208
                                NULL);
 
209
 
 
210
    t[0].data = (void*)protocol1;
 
211
    t[0].size = strlen(protocol1);
 
212
    t[1].data = (void*)protocol2;
 
213
    t[1].size = strlen(protocol2);
 
214
    
 
215
    ret = gnutls_alpn_set_protocols(session, t, 2, 0);
 
216
    if (ret < 0)
 
217
      {
 
218
        gnutls_perror(ret);
 
219
        exit(1);
 
220
      }
 
221
 
 
222
    gnutls_credentials_set (session, GNUTLS_CRD_ANON, anoncred);
 
223
 
 
224
    gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) fd);
 
225
 
 
226
    do
 
227
      {
 
228
          ret = gnutls_handshake (session);
 
229
      }
 
230
    while (ret < 0 && gnutls_error_is_fatal (ret) == 0);
 
231
    if (ret < 0)
 
232
      {
 
233
          close (fd);
 
234
          gnutls_deinit (session);
 
235
          fail ("server: Handshake has failed (%s)\n\n",
 
236
                gnutls_strerror (ret));
 
237
          terminate ();
 
238
      }
 
239
    if (debug)
 
240
        success ("server: Handshake was completed\n");
 
241
 
 
242
    if (debug)
 
243
        success ("server: TLS version is: %s\n",
 
244
                 gnutls_protocol_get_name (gnutls_protocol_get_version
 
245
                                           (session)));
 
246
 
 
247
    ret = gnutls_alpn_get_selected_protocol(session, &t[0]);
 
248
    if (ret < 0)
 
249
      {
 
250
        gnutls_perror(ret);
 
251
        exit(1);
 
252
      }
 
253
    
 
254
#if 0
 
255
    if (debug)
 
256
      {
 
257
        success ("Protocol: %.*s\n", (int)t[0].size, t[0].data);
 
258
      }
 
259
#endif
 
260
 
 
261
    /* do not wait for the peer to close the connection.
 
262
     */
 
263
    gnutls_bye (session, GNUTLS_SHUT_WR);
 
264
 
 
265
    close (fd);
 
266
    gnutls_deinit (session);
 
267
 
 
268
    gnutls_anon_free_server_credentials (anoncred);
 
269
 
 
270
    gnutls_global_deinit ();
 
271
 
 
272
    if (debug)
 
273
        success ("server: finished\n");
 
274
}
 
275
 
 
276
static void
 
277
start (const char* p1, const char* p2)
 
278
{
 
279
    int fd[2];
 
280
    int ret;
 
281
 
 
282
    ret = socketpair (AF_UNIX, SOCK_STREAM, 0, fd);
 
283
    if (ret < 0)
 
284
      {
 
285
          perror ("socketpair");
 
286
          exit (1);
 
287
      }
 
288
 
 
289
    child = fork ();
 
290
    if (child < 0)
 
291
      {
 
292
          perror ("fork");
 
293
          fail ("fork");
 
294
          exit (1);
 
295
      }
 
296
 
 
297
    if (child)
 
298
      {
 
299
          int status;
 
300
          /* parent */
 
301
 
 
302
          server (fd[0], p1, p2);
 
303
          wait (&status);
 
304
          if (WEXITSTATUS (status) != 0)
 
305
              fail ("Child died with status %d\n", WEXITSTATUS (status));
 
306
      }
 
307
    else
 
308
      {
 
309
          close (fd[0]);
 
310
          client (fd[1], p2, p1);
 
311
          exit (0);
 
312
      }
 
313
}
 
314
 
 
315
void
 
316
doit (void)
 
317
{
 
318
    start ("spdy/2", "spdy/3");
 
319
    start ("spdy/3", "spdy/2");
 
320
}
 
321
 
 
322
#endif /* _WIN32 */