Try to handle "function declaration isn't a prototype" warnings.
[gnutls.git] / tests / resume.c
blobe2d08e22828a9d2cafda0faff4ca432378a190f8
1 /*
2 * Copyright (C) 2004, 2005, 2007 Free Software Foundation
4 * Author: Simon Josefsson
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 2 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 /* Parts copied from GnuTLS example programs. */
25 #if HAVE_CONFIG_H
26 # include <config.h>
27 #endif
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <sys/wait.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
37 #include <unistd.h>
38 #include <gnutls/gnutls.h>
40 #include "utils.h"
42 static void wrap_db_init (void);
43 static void wrap_db_deinit (void);
44 static int wrap_db_store (void *dbf, gnutls_datum_t key, gnutls_datum_t data);
45 static gnutls_datum_t wrap_db_fetch (void *dbf, gnutls_datum_t key);
46 static int wrap_db_delete (void *dbf, gnutls_datum_t key);
48 #define TLS_SESSION_CACHE 50
50 /* A very basic TLS client, with anonymous authentication.
53 #define MAX_BUF 1024
54 #define MSG "Hello TLS"
56 /* Connects to the peer and returns a socket
57 * descriptor.
59 int
60 tcp_connect (void)
62 const char *PORT = "5556";
63 const char *SERVER = "127.0.0.1";
64 int err, sd;
65 struct sockaddr_in sa;
67 /* connects to server
69 sd = socket (AF_INET, SOCK_STREAM, 0);
71 memset (&sa, '\0', sizeof (sa));
72 sa.sin_family = AF_INET;
73 sa.sin_port = htons (atoi (PORT));
74 inet_pton (AF_INET, SERVER, &sa.sin_addr);
76 err = connect (sd, (struct sockaddr *) &sa, sizeof (sa));
77 if (err < 0)
79 fprintf (stderr, "Connect error\n");
80 exit (1);
83 return sd;
86 /* closes the given socket descriptor.
88 void
89 tcp_close (int sd)
91 shutdown (sd, SHUT_RDWR); /* no more receptions */
92 close (sd);
95 void
96 client (void)
98 int ret, sd, ii;
99 gnutls_session_t session;
100 char buffer[MAX_BUF + 1];
101 gnutls_anon_client_credentials_t anoncred;
102 /* Need to enable anonymous KX specifically. */
103 const int kx_prio[] = { GNUTLS_KX_ANON_DH, 0 };
105 /* variables used in session resuming
107 int t;
108 gnutls_datum session_data;
110 gnutls_global_init ();
112 gnutls_anon_allocate_client_credentials (&anoncred);
114 for (t = 0; t < 2; t++)
115 { /* connect 2 times to the server */
117 /* connect to the peer
119 sd = tcp_connect ();
121 /* Initialize TLS session
123 gnutls_init (&session, GNUTLS_CLIENT);
125 /* Use default priorities */
126 gnutls_set_default_priority (session);
127 gnutls_kx_set_priority (session, kx_prio);
129 /* put the anonymous credentials to the current session
131 gnutls_credentials_set (session, GNUTLS_CRD_ANON, anoncred);
133 if (t > 0)
135 /* if this is not the first time we connect */
136 gnutls_session_set_data (session, session_data.data,
137 session_data.size);
138 gnutls_free (session_data.data);
141 gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
143 /* Perform the TLS handshake
145 ret = gnutls_handshake (session);
147 if (ret < 0)
149 fail ("client: Handshake failed\n");
150 gnutls_perror (ret);
151 goto end;
153 else
155 success ("client: Handshake was completed\n");
158 if (t == 0)
159 { /* the first time we connect */
160 /* get the session data size */
161 ret = gnutls_session_get_data2 (session, &session_data);
162 if (ret < 0)
163 fail ("Getting resume data failed\n");
165 else
166 { /* the second time we connect */
168 /* check if we actually resumed the previous session */
169 if (gnutls_session_is_resumed (session) != 0)
171 success ("- Previous session was resumed\n");
173 else
175 success ("*** Previous session was NOT resumed\n");
179 gnutls_record_send (session, MSG, strlen (MSG));
181 ret = gnutls_record_recv (session, buffer, MAX_BUF);
182 if (ret == 0)
184 success ("client: Peer has closed the TLS connection\n");
185 goto end;
187 else if (ret < 0)
189 fail ("client: Error: %s\n", gnutls_strerror (ret));
190 goto end;
193 if (debug)
195 printf ("- Received %d bytes: ", ret);
196 for (ii = 0; ii < ret; ii++)
198 fputc (buffer[ii], stdout);
200 fputs ("\n", stdout);
203 gnutls_bye (session, GNUTLS_SHUT_RDWR);
205 end:
207 tcp_close (sd);
209 gnutls_deinit (session);
212 gnutls_anon_free_client_credentials (anoncred);
215 /* This is a sample TLS 1.0 echo server, for anonymous authentication only.
218 #define SA struct sockaddr
219 #define MAX_BUF 1024
220 #define PORT 5556 /* listen to 5556 port */
221 #define DH_BITS 1024
223 /* These are global */
224 gnutls_anon_server_credentials_t anoncred;
226 gnutls_session_t
227 initialize_tls_session (void)
229 gnutls_session_t session;
230 const int kx_prio[] = { GNUTLS_KX_ANON_DH, 0 };
232 gnutls_init (&session, GNUTLS_SERVER);
234 /* avoid calling all the priority functions, since the defaults
235 * are adequate.
237 gnutls_set_default_priority (session);
238 gnutls_kx_set_priority (session, kx_prio);
240 gnutls_credentials_set (session, GNUTLS_CRD_ANON, anoncred);
242 gnutls_dh_set_prime_bits (session, DH_BITS);
244 if (TLS_SESSION_CACHE != 0)
246 gnutls_db_set_retrieve_function (session, wrap_db_fetch);
247 gnutls_db_set_remove_function (session, wrap_db_delete);
248 gnutls_db_set_store_function (session, wrap_db_store);
249 gnutls_db_set_ptr (session, NULL);
252 return session;
255 static gnutls_dh_params_t dh_params;
257 static int
258 generate_dh_params (void)
260 const gnutls_datum_t p3 = { pkcs3, strlen (pkcs3) };
261 /* Generate Diffie Hellman parameters - for use with DHE
262 * kx algorithms. These should be discarded and regenerated
263 * once a day, once a week or once a month. Depending on the
264 * security requirements.
266 gnutls_dh_params_init (&dh_params);
267 return gnutls_dh_params_import_pkcs3 (dh_params, &p3, GNUTLS_X509_FMT_PEM);
270 int err, listen_sd, i;
271 int sd, ret;
272 struct sockaddr_in sa_serv;
273 struct sockaddr_in sa_cli;
274 int client_len;
275 char topbuf[512];
276 gnutls_session_t session;
277 char buffer[MAX_BUF + 1];
278 int optval = 1;
280 void
281 global_start (void)
283 /* this must be called once in the program, it is mostly for the server.
285 gnutls_global_init ();
287 gnutls_anon_allocate_server_credentials (&anoncred);
289 success ("Launched, generating DH parameters...\n");
291 generate_dh_params ();
293 gnutls_anon_set_server_dh_params (anoncred, dh_params);
295 if (TLS_SESSION_CACHE != 0)
297 wrap_db_init ();
300 /* Socket operations
302 listen_sd = socket (AF_INET, SOCK_STREAM, 0);
303 if (err == -1)
305 perror ("socket");
306 fail ("server: socket failed\n");
307 return;
310 memset (&sa_serv, '\0', sizeof (sa_serv));
311 sa_serv.sin_family = AF_INET;
312 sa_serv.sin_addr.s_addr = INADDR_ANY;
313 sa_serv.sin_port = htons (PORT); /* Server Port number */
315 setsockopt (listen_sd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof (int));
317 err = bind (listen_sd, (SA *) & sa_serv, sizeof (sa_serv));
318 if (err == -1)
320 perror ("bind");
321 fail ("server: bind failed\n");
322 return;
325 err = listen (listen_sd, 1024);
326 if (err == -1)
328 perror ("listen");
329 fail ("server: listen failed\n");
330 return;
333 success ("server: ready. Listening to port '%d'.\n", PORT);
336 void
337 global_stop (void)
339 success ("global stop\n");
341 gnutls_anon_free_server_credentials (anoncred);
343 gnutls_dh_params_deinit (dh_params);
345 gnutls_global_deinit ();
348 void
349 server (void)
351 int t;
353 for (t = 0; t < 2; t++)
355 client_len = sizeof (sa_cli);
357 session = initialize_tls_session ();
359 sd = accept (listen_sd, (SA *) & sa_cli, &client_len);
361 success ("server: connection from %s, port %d\n",
362 inet_ntop (AF_INET, &sa_cli.sin_addr, topbuf,
363 sizeof (topbuf)), ntohs (sa_cli.sin_port));
365 gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
366 ret = gnutls_handshake (session);
367 if (ret < 0)
369 close (sd);
370 gnutls_deinit (session);
371 fail ("server: Handshake has failed (%s)\n\n",
372 gnutls_strerror (ret));
373 return;
375 success ("server: Handshake was completed\n");
377 /* see the Getting peer's information example */
378 /* print_info(session); */
380 i = 0;
381 for (;;)
383 bzero (buffer, MAX_BUF + 1);
384 ret = gnutls_record_recv (session, buffer, MAX_BUF);
386 if (ret == 0)
388 success ("server: Peer has closed the GNUTLS connection\n");
389 break;
391 else if (ret < 0)
393 fail ("server: Received corrupted data(%d). Closing...\n", ret);
394 break;
396 else if (ret > 0)
398 /* echo data back to the client
400 gnutls_record_send (session, buffer, strlen (buffer));
403 /* do not wait for the peer to close the connection.
405 gnutls_bye (session, GNUTLS_SHUT_WR);
407 close (sd);
409 gnutls_deinit (session);
412 close (listen_sd);
414 success ("server: finished\n");
417 void
418 doit (void)
420 pid_t child;
422 global_start ();
423 if (error_count)
424 return;
426 child = fork ();
427 if (child < 0)
429 perror ("fork");
430 fail ("fork");
431 return;
434 if (child)
436 int status;
437 /* parent */
438 server ();
439 wait (&status);
441 else
442 client ();
444 global_stop ();
447 /* Functions and other stuff needed for session resuming.
448 * This is done using a very simple list which holds session ids
449 * and session data.
452 #define MAX_SESSION_ID_SIZE 32
453 #define MAX_SESSION_DATA_SIZE 512
455 typedef struct
457 unsigned char session_id[MAX_SESSION_ID_SIZE];
458 unsigned int session_id_size;
460 char session_data[MAX_SESSION_DATA_SIZE];
461 int session_data_size;
462 } CACHE;
464 static CACHE *cache_db;
465 static int cache_db_ptr = 0;
467 static void
468 wrap_db_init (void)
471 /* allocate cache_db */
472 cache_db = calloc (1, TLS_SESSION_CACHE * sizeof (CACHE));
475 static void
476 wrap_db_deinit (void)
478 return;
481 static int
482 wrap_db_store (void *dbf, gnutls_datum_t key, gnutls_datum_t data)
484 success ("resume db storing... (%d-%d)\n", key.size, data.size);
486 if (debug)
488 unsigned int i;
489 printf ("key:\n");
490 for (i = 0; i < key.size; i++)
492 printf ("%02x ", key.data[i] & 0xFF);
493 if ((i + 1) % 16 == 0)
494 printf ("\n");
496 printf ("\n");
497 printf ("data:\n");
498 for (i = 0; i < data.size; i++)
500 printf ("%02x ", data.data[i] & 0xFF);
501 if ((i + 1) % 16 == 0)
502 printf ("\n");
504 printf ("\n");
507 if (cache_db == NULL)
508 return -1;
510 if (key.size > MAX_SESSION_ID_SIZE)
511 return -1;
512 if (data.size > MAX_SESSION_DATA_SIZE)
513 return -1;
515 memcpy (cache_db[cache_db_ptr].session_id, key.data, key.size);
516 cache_db[cache_db_ptr].session_id_size = key.size;
518 memcpy (cache_db[cache_db_ptr].session_data, data.data, data.size);
519 cache_db[cache_db_ptr].session_data_size = data.size;
521 cache_db_ptr++;
522 cache_db_ptr %= TLS_SESSION_CACHE;
524 return 0;
527 static gnutls_datum_t
528 wrap_db_fetch (void *dbf, gnutls_datum_t key)
530 gnutls_datum_t res = { NULL, 0 };
531 int i;
533 success ("resume db fetch... (%d)\n", key.size);
534 if (debug)
536 unsigned int i;
537 printf ("key:\n");
538 for (i = 0; i < key.size; i++)
540 printf ("%02x ", key.data[i] & 0xFF);
541 if ((i + 1) % 16 == 0)
542 printf ("\n");
544 printf ("\n");
547 if (cache_db == NULL)
548 return res;
550 for (i = 0; i < TLS_SESSION_CACHE; i++)
552 if (key.size == cache_db[i].session_id_size &&
553 memcmp (key.data, cache_db[i].session_id, key.size) == 0)
555 success ("resume db fetch... return info\n");
557 res.size = cache_db[i].session_data_size;
559 res.data = gnutls_malloc (res.size);
560 if (res.data == NULL)
561 return res;
563 memcpy (res.data, cache_db[i].session_data, res.size);
565 if (debug)
567 unsigned int i;
568 printf ("data:\n");
569 for (i = 0; i < res.size; i++)
571 printf ("%02x ", res.data[i] & 0xFF);
572 if ((i + 1) % 16 == 0)
573 printf ("\n");
575 printf ("\n");
578 return res;
581 return res;
584 static int
585 wrap_db_delete (void *dbf, gnutls_datum_t key)
587 int i;
589 if (cache_db == NULL)
590 return -1;
592 for (i = 0; i < TLS_SESSION_CACHE; i++)
594 if (key.size == cache_db[i].session_id_size &&
595 memcmp (key.data, cache_db[i].session_id, key.size) == 0)
598 cache_db[i].session_id_size = 0;
599 cache_db[i].session_data_size = 0;
601 return 0;
605 return -1;