2 * Copyright (C) 2004-2012 Free Software Foundation, Inc.
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 <netinet/in.h>
34 #include <sys/socket.h>
37 #include <arpa/inet.h>
40 #include <gnutls/gnutls.h>
47 tls_log_func (int level
, const char *str
)
49 fprintf (stderr
, "|<%d>| %s", level
, str
);
52 /* A very basic TLS client, with anonymous authentication.
56 #define MSG "Hello TLS"
62 gnutls_session_t session
;
63 char buffer
[MAX_BUF
+ 1];
64 gnutls_anon_client_credentials_t anoncred
;
65 /* Need to enable anonymous KX specifically. */
67 gnutls_global_init ();
69 gnutls_global_set_log_function (tls_log_func
);
71 gnutls_global_set_log_level (4711);
73 gnutls_anon_allocate_client_credentials (&anoncred
);
75 /* Initialize TLS session
77 gnutls_init (&session
, GNUTLS_CLIENT
);
79 /* Use default priorities */
80 gnutls_priority_set_direct (session
, "NONE:+VERS-TLS-ALL:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-DH", NULL
);
82 /* put the anonymous credentials to the current session
84 gnutls_credentials_set (session
, GNUTLS_CRD_ANON
, anoncred
);
86 /* connect to the peer
90 gnutls_transport_set_ptr (session
, (gnutls_transport_ptr_t
) sd
);
92 /* Perform the TLS handshake
94 ret
= gnutls_handshake (session
);
98 fail ("client: Handshake failed\n");
105 success ("client: Handshake was completed\n");
109 success ("client: TLS version is: %s\n",
110 gnutls_protocol_get_name (gnutls_protocol_get_version
113 gnutls_record_send (session
, MSG
, strlen (MSG
));
115 ret
= gnutls_record_recv (session
, buffer
, MAX_BUF
);
119 success ("client: Peer has closed the TLS connection\n");
124 fail ("client: Error: %s\n", gnutls_strerror (ret
));
130 printf ("- Received %d bytes: ", ret
);
131 for (ii
= 0; ii
< ret
; ii
++)
133 fputc (buffer
[ii
], stdout
);
135 fputs ("\n", stdout
);
138 gnutls_bye (session
, GNUTLS_SHUT_RDWR
);
144 gnutls_deinit (session
);
146 gnutls_anon_free_client_credentials (anoncred
);
148 gnutls_global_deinit ();
151 /* This is a sample TLS 1.0 echo server, for anonymous authentication only.
154 #define SA struct sockaddr
156 #define PORT 5556 /* listen to 5556 port */
159 /* These are global */
160 gnutls_anon_server_credentials_t anoncred
;
162 static gnutls_session_t
163 initialize_tls_session (void)
165 gnutls_session_t session
;
167 gnutls_init (&session
, GNUTLS_SERVER
);
169 /* avoid calling all the priority functions, since the defaults
172 gnutls_priority_set_direct (session
, "NONE:+VERS-TLS-ALL:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-DH", NULL
);
174 gnutls_credentials_set (session
, GNUTLS_CRD_ANON
, anoncred
);
176 gnutls_dh_set_prime_bits (session
, DH_BITS
);
181 static gnutls_dh_params_t dh_params
;
184 generate_dh_params (void)
186 const gnutls_datum_t p3
= { (void *) pkcs3
, strlen (pkcs3
) };
187 /* Generate Diffie-Hellman parameters - for use with DHE
188 * kx algorithms. These should be discarded and regenerated
189 * once a day, once a week or once a month. Depending on the
190 * security requirements.
192 gnutls_dh_params_init (&dh_params
);
193 return gnutls_dh_params_import_pkcs3 (dh_params
, &p3
, GNUTLS_X509_FMT_PEM
);
196 int err
, listen_sd
, i
;
198 struct sockaddr_in sa_serv
;
199 struct sockaddr_in sa_cli
;
200 socklen_t client_len
;
202 gnutls_session_t session
;
203 char buffer
[MAX_BUF
+ 1];
211 listen_sd
= socket (AF_INET
, SOCK_STREAM
, 0);
215 fail ("server: socket failed\n");
219 memset (&sa_serv
, '\0', sizeof (sa_serv
));
220 sa_serv
.sin_family
= AF_INET
;
221 sa_serv
.sin_addr
.s_addr
= INADDR_ANY
;
222 sa_serv
.sin_port
= htons (PORT
); /* Server Port number */
224 setsockopt (listen_sd
, SOL_SOCKET
, SO_REUSEADDR
, (void *) &optval
,
227 err
= bind (listen_sd
, (SA
*) & sa_serv
, sizeof (sa_serv
));
231 fail ("server: bind failed\n");
235 err
= listen (listen_sd
, 1024);
239 fail ("server: listen failed\n");
244 success ("server: ready. Listening to port '%d'.\n", PORT
);
250 /* this must be called once in the program
252 gnutls_global_init ();
254 gnutls_global_set_log_function (tls_log_func
);
256 gnutls_global_set_log_level (4711);
258 gnutls_anon_allocate_server_credentials (&anoncred
);
261 success ("Launched, generating DH parameters...\n");
263 generate_dh_params ();
265 gnutls_anon_set_server_dh_params (anoncred
, dh_params
);
267 client_len
= sizeof (sa_cli
);
269 session
= initialize_tls_session ();
271 sd
= accept (listen_sd
, (SA
*) & sa_cli
, &client_len
);
274 success ("server: connection from %s, port %d\n",
275 inet_ntop (AF_INET
, &sa_cli
.sin_addr
, topbuf
,
276 sizeof (topbuf
)), ntohs (sa_cli
.sin_port
));
278 gnutls_transport_set_ptr (session
, (gnutls_transport_ptr_t
) sd
);
279 ret
= gnutls_handshake (session
);
283 gnutls_deinit (session
);
284 fail ("server: Handshake has failed (%s)\n\n", gnutls_strerror (ret
));
288 success ("server: Handshake was completed\n");
291 success ("server: TLS version is: %s\n",
292 gnutls_protocol_get_name (gnutls_protocol_get_version
295 /* see the Getting peer's information example */
296 /* print_info(session); */
301 memset (buffer
, 0, MAX_BUF
+ 1);
302 ret
= gnutls_record_recv (session
, buffer
, MAX_BUF
);
307 success ("server: Peer has closed the GnuTLS connection\n");
312 fail ("server: Received corrupted data(%d). Closing...\n", ret
);
317 /* echo data back to the client
319 gnutls_record_send (session
, buffer
, strlen (buffer
));
322 /* do not wait for the peer to close the connection.
324 gnutls_bye (session
, GNUTLS_SHUT_WR
);
327 gnutls_deinit (session
);
331 gnutls_anon_free_server_credentials (anoncred
);
333 gnutls_dh_params_deinit (dh_params
);
335 gnutls_global_deinit ();
338 success ("server: finished\n");