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 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 /* Parts copied from GnuTLS example programs. */
32 #include <sys/types.h>
33 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
38 #include <gnutls/gnutls.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.
54 #define MSG "Hello TLS"
56 /* Connects to the peer and returns a socket
62 const char *PORT
= "5556";
63 const char *SERVER
= "127.0.0.1";
65 struct sockaddr_in sa
;
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
));
79 fprintf (stderr
, "Connect error\n");
86 /* closes the given socket descriptor.
91 shutdown (sd
, SHUT_RDWR
); /* no more receptions */
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
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
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
);
135 /* if this is not the first time we connect */
136 gnutls_session_set_data (session
, session_data
.data
,
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
);
149 fail ("client: Handshake failed\n");
155 success ("client: Handshake was completed\n");
159 { /* the first time we connect */
160 /* get the session data size */
161 ret
= gnutls_session_get_data2 (session
, &session_data
);
163 fail ("Getting resume data failed\n");
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");
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
);
184 success ("client: Peer has closed the TLS connection\n");
189 fail ("client: Error: %s\n", gnutls_strerror (ret
));
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
);
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
220 #define PORT 5556 /* listen to 5556 port */
223 /* These are global */
224 gnutls_anon_server_credentials_t anoncred
;
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
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
);
255 static gnutls_dh_params_t dh_params
;
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
;
272 struct sockaddr_in sa_serv
;
273 struct sockaddr_in sa_cli
;
276 gnutls_session_t session
;
277 char buffer
[MAX_BUF
+ 1];
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)
302 listen_sd
= socket (AF_INET
, SOCK_STREAM
, 0);
306 fail ("server: socket failed\n");
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
));
321 fail ("server: bind failed\n");
325 err
= listen (listen_sd
, 1024);
329 fail ("server: listen failed\n");
333 success ("server: ready. Listening to port '%d'.\n", PORT
);
339 success ("global stop\n");
341 gnutls_anon_free_server_credentials (anoncred
);
343 gnutls_dh_params_deinit (dh_params
);
345 gnutls_global_deinit ();
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
);
370 gnutls_deinit (session
);
371 fail ("server: Handshake has failed (%s)\n\n",
372 gnutls_strerror (ret
));
375 success ("server: Handshake was completed\n");
377 /* see the Getting peer's information example */
378 /* print_info(session); */
383 bzero (buffer
, MAX_BUF
+ 1);
384 ret
= gnutls_record_recv (session
, buffer
, MAX_BUF
);
388 success ("server: Peer has closed the GNUTLS connection\n");
393 fail ("server: Received corrupted data(%d). Closing...\n", ret
);
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
);
409 gnutls_deinit (session
);
414 success ("server: finished\n");
447 /* Functions and other stuff needed for session resuming.
448 * This is done using a very simple list which holds session ids
452 #define MAX_SESSION_ID_SIZE 32
453 #define MAX_SESSION_DATA_SIZE 512
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
;
464 static CACHE
*cache_db
;
465 static int cache_db_ptr
= 0;
471 /* allocate cache_db */
472 cache_db
= calloc (1, TLS_SESSION_CACHE
* sizeof (CACHE
));
476 wrap_db_deinit (void)
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
);
490 for (i
= 0; i
< key
.size
; i
++)
492 printf ("%02x ", key
.data
[i
] & 0xFF);
493 if ((i
+ 1) % 16 == 0)
498 for (i
= 0; i
< data
.size
; i
++)
500 printf ("%02x ", data
.data
[i
] & 0xFF);
501 if ((i
+ 1) % 16 == 0)
507 if (cache_db
== NULL
)
510 if (key
.size
> MAX_SESSION_ID_SIZE
)
512 if (data
.size
> MAX_SESSION_DATA_SIZE
)
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
;
522 cache_db_ptr
%= TLS_SESSION_CACHE
;
527 static gnutls_datum_t
528 wrap_db_fetch (void *dbf
, gnutls_datum_t key
)
530 gnutls_datum_t res
= { NULL
, 0 };
533 success ("resume db fetch... (%d)\n", key
.size
);
538 for (i
= 0; i
< key
.size
; i
++)
540 printf ("%02x ", key
.data
[i
] & 0xFF);
541 if ((i
+ 1) % 16 == 0)
547 if (cache_db
== NULL
)
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
)
563 memcpy (res
.data
, cache_db
[i
].session_data
, res
.size
);
569 for (i
= 0; i
< res
.size
; i
++)
571 printf ("%02x ", res
.data
[i
] & 0xFF);
572 if ((i
+ 1) % 16 == 0)
585 wrap_db_delete (void *dbf
, gnutls_datum_t key
)
589 if (cache_db
== NULL
)
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;