2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software
5 * Author: Simon Josefsson
7 * This file is part of GnuTLS.
9 * GnuTLS is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * GnuTLS is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with GnuTLS; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 /* Parts copied from GnuTLS example programs. */
33 #include <sys/types.h>
34 #include <netinet/in.h>
35 #include <sys/socket.h>
38 #include <arpa/inet.h>
41 #include <gnutls/gnutls.h>
48 tls_log_func (int level, const char *str)
50 fprintf (stderr, "|<%d>| %s", level, str);
53 /* A very basic TLS client, with anonymous authentication.
57 #define MSG "Hello TLS"
63 gnutls_session_t session;
64 char buffer[MAX_BUF + 1];
65 gnutls_anon_client_credentials_t anoncred;
66 /* Need to enable anonymous KX specifically. */
68 gnutls_global_init ();
70 gnutls_global_set_log_function (tls_log_func);
72 gnutls_global_set_log_level (4711);
74 gnutls_anon_allocate_client_credentials (&anoncred);
76 /* Initialize TLS session
78 gnutls_init (&session, GNUTLS_CLIENT);
80 /* Use default priorities */
81 gnutls_priority_set_direct (session, "NONE:+VERS-TLS-ALL:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-DH", NULL);
83 /* put the anonymous credentials to the current session
85 gnutls_credentials_set (session, GNUTLS_CRD_ANON, anoncred);
87 /* connect to the peer
91 gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
93 /* Perform the TLS handshake
95 ret = gnutls_handshake (session);
99 fail ("client: Handshake failed\n");
106 success ("client: Handshake was completed\n");
110 success ("client: TLS version is: %s\n",
111 gnutls_protocol_get_name (gnutls_protocol_get_version
114 gnutls_record_send (session, MSG, strlen (MSG));
116 ret = gnutls_record_recv (session, buffer, MAX_BUF);
120 success ("client: Peer has closed the TLS connection\n");
125 fail ("client: Error: %s\n", gnutls_strerror (ret));
131 printf ("- Received %d bytes: ", ret);
132 for (ii = 0; ii < ret; ii++)
134 fputc (buffer[ii], stdout);
136 fputs ("\n", stdout);
139 gnutls_bye (session, GNUTLS_SHUT_RDWR);
145 gnutls_deinit (session);
147 gnutls_anon_free_client_credentials (anoncred);
149 gnutls_global_deinit ();
152 /* This is a sample TLS 1.0 echo server, for anonymous authentication only.
155 #define SA struct sockaddr
157 #define PORT 5556 /* listen to 5556 port */
160 /* These are global */
161 gnutls_anon_server_credentials_t anoncred;
163 static gnutls_session_t
164 initialize_tls_session (void)
166 gnutls_session_t session;
168 gnutls_init (&session, GNUTLS_SERVER);
170 /* avoid calling all the priority functions, since the defaults
173 gnutls_priority_set_direct (session, "NONE:+VERS-TLS-ALL:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-DH", NULL);
175 gnutls_credentials_set (session, GNUTLS_CRD_ANON, anoncred);
177 gnutls_dh_set_prime_bits (session, DH_BITS);
182 static gnutls_dh_params_t dh_params;
185 generate_dh_params (void)
187 const gnutls_datum_t p3 = { (char *) pkcs3, strlen (pkcs3) };
188 /* Generate Diffie-Hellman parameters - for use with DHE
189 * kx algorithms. These should be discarded and regenerated
190 * once a day, once a week or once a month. Depending on the
191 * security requirements.
193 gnutls_dh_params_init (&dh_params);
194 return gnutls_dh_params_import_pkcs3 (dh_params, &p3, GNUTLS_X509_FMT_PEM);
197 int err, listen_sd, i;
199 struct sockaddr_in sa_serv;
200 struct sockaddr_in sa_cli;
203 gnutls_session_t session;
204 char buffer[MAX_BUF + 1];
212 listen_sd = socket (AF_INET, SOCK_STREAM, 0);
216 fail ("server: socket failed\n");
220 memset (&sa_serv, '\0', sizeof (sa_serv));
221 sa_serv.sin_family = AF_INET;
222 sa_serv.sin_addr.s_addr = INADDR_ANY;
223 sa_serv.sin_port = htons (PORT); /* Server Port number */
225 setsockopt (listen_sd, SOL_SOCKET, SO_REUSEADDR, (void *) &optval,
228 err = bind (listen_sd, (SA *) & sa_serv, sizeof (sa_serv));
232 fail ("server: bind failed\n");
236 err = listen (listen_sd, 1024);
240 fail ("server: listen failed\n");
245 success ("server: ready. Listening to port '%d'.\n", PORT);
251 /* this must be called once in the program
253 gnutls_global_init ();
255 gnutls_global_set_log_function (tls_log_func);
257 gnutls_global_set_log_level (4711);
259 gnutls_anon_allocate_server_credentials (&anoncred);
262 success ("Launched, generating DH parameters...\n");
264 generate_dh_params ();
266 gnutls_anon_set_server_dh_params (anoncred, dh_params);
268 client_len = sizeof (sa_cli);
270 session = initialize_tls_session ();
272 sd = accept (listen_sd, (SA *) & sa_cli, &client_len);
275 success ("server: connection from %s, port %d\n",
276 inet_ntop (AF_INET, &sa_cli.sin_addr, topbuf,
277 sizeof (topbuf)), ntohs (sa_cli.sin_port));
279 gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
280 ret = gnutls_handshake (session);
284 gnutls_deinit (session);
285 fail ("server: Handshake has failed (%s)\n\n", gnutls_strerror (ret));
289 success ("server: Handshake was completed\n");
292 success ("server: TLS version is: %s\n",
293 gnutls_protocol_get_name (gnutls_protocol_get_version
296 /* see the Getting peer's information example */
297 /* print_info(session); */
302 memset (buffer, 0, MAX_BUF + 1);
303 ret = gnutls_record_recv (session, buffer, MAX_BUF);
308 success ("server: Peer has closed the GnuTLS connection\n");
313 fail ("server: Received corrupted data(%d). Closing...\n", ret);
318 /* echo data back to the client
320 gnutls_record_send (session, buffer, strlen (buffer));
323 /* do not wait for the peer to close the connection.
325 gnutls_bye (session, GNUTLS_SHUT_WR);
328 gnutls_deinit (session);
332 gnutls_anon_free_server_credentials (anoncred);
334 gnutls_dh_params_deinit (dh_params);
336 gnutls_global_deinit ();
339 success ("server: finished\n");