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. */
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>
39 #include <gnutls/extra.h>
45 /* A very basic TLS client, with anonymous authentication.
49 #define MSG "Hello TLS"
51 /* Connects to the peer and returns a socket
57 const char *PORT
= "5556";
58 const char *SERVER
= "127.0.0.1";
60 struct sockaddr_in sa
;
64 sd
= socket (AF_INET
, SOCK_STREAM
, 0);
66 memset (&sa
, '\0', sizeof (sa
));
67 sa
.sin_family
= AF_INET
;
68 sa
.sin_port
= htons (atoi (PORT
));
69 inet_pton (AF_INET
, SERVER
, &sa
.sin_addr
);
71 err
= connect (sd
, (struct sockaddr
*) &sa
, sizeof (sa
));
74 fprintf (stderr
, "Connect error\n");
81 /* closes the given socket descriptor.
86 shutdown (sd
, SHUT_RDWR
); /* no more receptions */
91 client_avp (gnutls_session_t session
, void *ptr
,
92 const char *last
, size_t lastlen
, char **new, size_t * newlen
)
98 printf ("client: received %d bytes AVP: `%.*s'\n",
99 lastlen
, lastlen
, last
);
101 printf ("client: new application phase\n");
106 p
= "client's first AVP, next will be empty";
118 p
= "final client AVP, we'll restart next";
126 p
= readline ("Client TLS/IA AVP: ");
128 *new = gnutls_strdup (p
);
131 *newlen
= strlen (*new);
133 printf ("client: sending %d bytes AVP: `%s'\n", *newlen
, *new);
135 gnutls_ia_permute_inner_secret (session
, 3, "foo");
144 gnutls_session_t session
;
145 char buffer
[MAX_BUF
+ 1];
146 gnutls_anon_client_credentials_t anoncred
;
147 gnutls_ia_client_credentials_t iacred
;
148 /* Need to enable anonymous KX specifically. */
149 const int kx_prio
[] = { GNUTLS_KX_ANON_DH
, 0 };
151 ret
= gnutls_global_init ();
153 fail ("global_init: %d\n", ret
);
154 ret
= gnutls_global_init_extra ();
156 fail ("global_init_extra: %d\n", ret
);
158 gnutls_anon_allocate_client_credentials (&anoncred
);
159 gnutls_ia_allocate_client_credentials (&iacred
);
161 /* Initialize TLS session
163 gnutls_init (&session
, GNUTLS_CLIENT
);
165 /* Use default priorities */
166 gnutls_set_default_priority (session
);
167 gnutls_kx_set_priority (session
, kx_prio
);
169 /* put the anonymous credentials to the current session
171 gnutls_credentials_set (session
, GNUTLS_CRD_ANON
, anoncred
);
172 gnutls_credentials_set (session
, GNUTLS_CRD_IA
, iacred
);
174 /* connect to the peer
178 gnutls_transport_set_ptr (session
, (gnutls_transport_ptr_t
) sd
);
181 gnutls_ia_set_client_avp_function (iacred
, client_avp
);
183 /* Perform the TLS handshake
185 ret
= gnutls_handshake (session
);
189 fail ("client: Handshake failed\n");
195 success ("client: Handshake was completed\n");
199 To test TLS/IA alert's (the server will print that a fatal alert
201 gnutls_alert_send(session, GNUTLS_AL_FATAL,
202 GNUTLS_A_INNER_APPLICATION_FAILURE);
205 if (!gnutls_ia_handshake_p (session
))
206 fail ("client: No TLS/IA negotiation\n");
209 success ("client: TLS/IA handshake\n");
211 ret
= gnutls_ia_handshake (session
);
215 fail ("client: TLS/IA handshake failed\n");
221 success ("client: TLS/IA Handshake was completed\n");
225 gnutls_record_send (session
, MSG
, strlen (MSG
));
227 ret
= gnutls_record_recv (session
, buffer
, MAX_BUF
);
230 success ("client: Peer has closed the TLS connection\n");
235 fail ("client: Error: %s\n", gnutls_strerror (ret
));
241 printf ("- Received %d bytes: ", ret
);
242 for (ii
= 0; ii
< ret
; ii
++)
244 fputc (buffer
[ii
], stdout
);
246 fputs ("\n", stdout
);
249 gnutls_bye (session
, GNUTLS_SHUT_RDWR
);
255 gnutls_deinit (session
);
257 gnutls_ia_free_client_credentials (iacred
);
259 gnutls_anon_free_client_credentials (anoncred
);
261 gnutls_global_deinit ();
264 /* This is a sample TLS 1.0 echo server, for anonymous authentication only.
267 #define SA struct sockaddr
269 #define PORT 5556 /* listen to 5556 port */
272 /* These are global */
273 gnutls_anon_server_credentials_t anoncred
;
274 gnutls_ia_server_credentials_t iacred
;
277 initialize_tls_session (void)
279 gnutls_session_t session
;
280 const int kx_prio
[] = { GNUTLS_KX_ANON_DH
, 0 };
282 gnutls_init (&session
, GNUTLS_SERVER
);
284 /* avoid calling all the priority functions, since the defaults
287 gnutls_set_default_priority (session
);
288 gnutls_kx_set_priority (session
, kx_prio
);
290 gnutls_credentials_set (session
, GNUTLS_CRD_ANON
, anoncred
);
292 gnutls_dh_set_prime_bits (session
, DH_BITS
);
297 static gnutls_dh_params_t dh_params
;
300 generate_dh_params (void)
302 const gnutls_datum_t p3
= { pkcs3
, strlen (pkcs3
) };
303 /* Generate Diffie Hellman parameters - for use with DHE
304 * kx algorithms. These should be discarded and regenerated
305 * once a day, once a week or once a month. Depending on the
306 * security requirements.
308 gnutls_dh_params_init (&dh_params
);
309 return gnutls_dh_params_import_pkcs3 (dh_params
, &p3
, GNUTLS_X509_FMT_PEM
);
312 int err
, listen_sd
, i
;
314 struct sockaddr_in sa_serv
;
315 struct sockaddr_in sa_cli
;
316 socklen_t client_len
;
318 gnutls_session_t session
;
319 char buffer
[MAX_BUF
+ 1];
323 server_avp (gnutls_session_t session
, void *ptr
,
324 const char *last
, size_t lastlen
, char **new, size_t * newlen
)
330 printf ("server: received %d bytes AVP: `%.*s'\n",
331 lastlen
, lastlen
, last
);
333 gnutls_ia_permute_inner_secret (session
, 3, "foo");
338 p
= "first server AVP";
342 p
= "second server AVP, next will be empty, then a intermediate finish";
354 p
= "server avp, after intermediate finish, next another intermediate";
362 p
= "server avp, next will be the finish phase";
373 p
= readline ("Server TLS/IA AVP (type '1' to sync, '2' to finish): ");
378 if (strcmp (p
, "1") == 0)
380 success ("server: Sending IntermediatePhaseFinished...\n");
384 if (strcmp (p
, "2") == 0)
386 success ("server: Sending FinalPhaseFinished...\n");
390 *new = gnutls_strdup (p
);
393 *newlen
= strlen (*new);
395 printf ("server: sending %d bytes AVP: `%s'\n", *newlen
, *new);
403 /* this must be called once in the program
405 gnutls_global_init ();
407 gnutls_anon_allocate_server_credentials (&anoncred
);
408 gnutls_ia_allocate_server_credentials (&iacred
);
410 success ("Launched, generating DH parameters...\n");
412 generate_dh_params ();
414 gnutls_anon_set_server_dh_params (anoncred
, dh_params
);
418 listen_sd
= socket (AF_INET
, SOCK_STREAM
, 0);
422 fail ("server: socket failed\n");
426 memset (&sa_serv
, '\0', sizeof (sa_serv
));
427 sa_serv
.sin_family
= AF_INET
;
428 sa_serv
.sin_addr
.s_addr
= INADDR_ANY
;
429 sa_serv
.sin_port
= htons (PORT
); /* Server Port number */
431 setsockopt (listen_sd
, SOL_SOCKET
, SO_REUSEADDR
, &optval
, sizeof (int));
433 err
= bind (listen_sd
, (SA
*) & sa_serv
, sizeof (sa_serv
));
437 fail ("server: bind failed\n");
441 err
= listen (listen_sd
, 1024);
445 fail ("server: listen failed\n");
449 success ("server: ready. Listening to port '%d'\n", PORT
);
455 client_len
= sizeof (sa_cli
);
457 session
= initialize_tls_session ();
459 sd
= accept (listen_sd
, (SA
*) & sa_cli
, &client_len
);
461 success ("server: connection from %s, port %d\n",
462 inet_ntop (AF_INET
, &sa_cli
.sin_addr
, topbuf
,
463 sizeof (topbuf
)), ntohs (sa_cli
.sin_port
));
465 gnutls_transport_set_ptr (session
, (gnutls_transport_ptr_t
) sd
);
468 gnutls_credentials_set (session
, GNUTLS_CRD_IA
, iacred
);
469 gnutls_ia_set_server_avp_function (iacred
, server_avp
);
471 ret
= gnutls_handshake (session
);
475 gnutls_deinit (session
);
476 fail ("server: Handshake has failed (%s)\n\n", gnutls_strerror (ret
));
479 success ("server: Handshake was completed\n");
481 if (!gnutls_ia_handshake_p (session
))
482 fail ("server: No TLS/IA negotiation\n");
485 success ("server: TLS/IA handshake\n");
487 ret
= gnutls_ia_handshake (session
);
491 fail ("server: TLS/IA handshake failed\n");
497 success ("server: TLS/IA Handshake was completed\n");
501 /* see the Getting peer's information example */
502 /* print_info(session); */
507 bzero (buffer
, MAX_BUF
+ 1);
508 ret
= gnutls_record_recv (session
, buffer
, MAX_BUF
);
512 success ("server: Peer has closed the GNUTLS connection\n");
517 if (ret
== GNUTLS_E_FATAL_ALERT_RECEIVED
)
519 gnutls_alert_description_t alert
;
521 alert
= gnutls_alert_get (session
);
522 err
= gnutls_alert_get_name (alert
);
524 printf ("Fatal alert: %s\n", err
);
527 fail ("server: Received corrupted data(%d). Closing...\n", ret
);
532 /* echo data back to the client
534 gnutls_record_send (session
, buffer
, strlen (buffer
));
537 /* do not wait for the peer to close the connection.
539 gnutls_bye (session
, GNUTLS_SHUT_WR
);
542 gnutls_deinit (session
);
546 gnutls_ia_free_server_credentials (iacred
);
548 gnutls_anon_free_server_credentials (anoncred
);
550 gnutls_global_deinit ();
552 success ("server: finished\n");