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>
37 #include <arpa/inet.h>
39 #include <gnutls/gnutls.h>
46 tls_log_func (int level
, const char *str
)
48 fprintf (stderr
, "|<%d>| %s", level
, str
);
51 /* A very basic TLS client, with anonymous authentication.
55 #define MSG "Hello TLS"
61 gnutls_session_t session
;
62 char buffer
[MAX_BUF
+ 1];
63 gnutls_anon_client_credentials_t anoncred
;
64 /* Need to enable anonymous KX specifically. */
66 gnutls_global_init ();
68 gnutls_global_set_log_function (tls_log_func
);
70 gnutls_global_set_log_level (4711);
72 gnutls_anon_allocate_client_credentials (&anoncred
);
74 /* Initialize TLS session
76 gnutls_init (&session
, GNUTLS_CLIENT
);
78 /* Use default priorities */
79 gnutls_priority_set_direct (session
, "NONE:+VERS-TLS-ALL:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-DH", NULL
);
81 /* put the anonymous credentials to the current session
83 gnutls_credentials_set (session
, GNUTLS_CRD_ANON
, anoncred
);
85 /* connect to the peer
89 gnutls_transport_set_ptr (session
, (gnutls_transport_ptr_t
) sd
);
91 /* Perform the TLS handshake
93 ret
= gnutls_handshake (session
);
97 fail ("client: Handshake failed\n");
104 success ("client: Handshake was completed\n");
108 success ("client: TLS version is: %s\n",
109 gnutls_protocol_get_name (gnutls_protocol_get_version
112 gnutls_record_send (session
, MSG
, strlen (MSG
));
114 ret
= gnutls_record_recv (session
, buffer
, MAX_BUF
);
118 success ("client: Peer has closed the TLS connection\n");
123 fail ("client: Error: %s\n", gnutls_strerror (ret
));
129 printf ("- Received %d bytes: ", ret
);
130 for (ii
= 0; ii
< ret
; ii
++)
132 fputc (buffer
[ii
], stdout
);
134 fputs ("\n", stdout
);
137 gnutls_bye (session
, GNUTLS_SHUT_RDWR
);
143 gnutls_deinit (session
);
145 gnutls_anon_free_client_credentials (anoncred
);
147 gnutls_global_deinit ();
150 /* This is a sample TLS 1.0 echo server, for anonymous authentication only.
153 #define SA struct sockaddr
155 #define PORT 5556 /* listen to 5556 port */
158 /* These are global */
159 gnutls_anon_server_credentials_t anoncred
;
161 static gnutls_session_t
162 initialize_tls_session (void)
164 gnutls_session_t session
;
166 gnutls_init (&session
, GNUTLS_SERVER
);
168 /* avoid calling all the priority functions, since the defaults
171 gnutls_priority_set_direct (session
, "NONE:+VERS-TLS-ALL:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-DH", NULL
);
173 gnutls_credentials_set (session
, GNUTLS_CRD_ANON
, anoncred
);
175 gnutls_dh_set_prime_bits (session
, DH_BITS
);
180 static gnutls_dh_params_t dh_params
;
183 generate_dh_params (void)
185 const gnutls_datum_t p3
= { (char *) pkcs3
, strlen (pkcs3
) };
186 /* Generate Diffie-Hellman parameters - for use with DHE
187 * kx algorithms. These should be discarded and regenerated
188 * once a day, once a week or once a month. Depending on the
189 * security requirements.
191 gnutls_dh_params_init (&dh_params
);
192 return gnutls_dh_params_import_pkcs3 (dh_params
, &p3
, GNUTLS_X509_FMT_PEM
);
195 int err
, listen_sd
, i
;
197 struct sockaddr_in sa_serv
;
198 struct sockaddr_in sa_cli
;
201 gnutls_session_t session
;
202 char buffer
[MAX_BUF
+ 1];
210 listen_sd
= socket (AF_INET
, SOCK_STREAM
, 0);
214 fail ("server: socket failed\n");
218 memset (&sa_serv
, '\0', sizeof (sa_serv
));
219 sa_serv
.sin_family
= AF_INET
;
220 sa_serv
.sin_addr
.s_addr
= INADDR_ANY
;
221 sa_serv
.sin_port
= htons (PORT
); /* Server Port number */
223 setsockopt (listen_sd
, SOL_SOCKET
, SO_REUSEADDR
, (void *) &optval
,
226 err
= bind (listen_sd
, (SA
*) & sa_serv
, sizeof (sa_serv
));
230 fail ("server: bind failed\n");
234 err
= listen (listen_sd
, 1024);
238 fail ("server: listen failed\n");
243 success ("server: ready. Listening to port '%d'.\n", PORT
);
249 /* this must be called once in the program
251 gnutls_global_init ();
253 gnutls_global_set_log_function (tls_log_func
);
255 gnutls_global_set_log_level (4711);
257 gnutls_anon_allocate_server_credentials (&anoncred
);
260 success ("Launched, generating DH parameters...\n");
262 generate_dh_params ();
264 gnutls_anon_set_server_dh_params (anoncred
, dh_params
);
266 client_len
= sizeof (sa_cli
);
268 session
= initialize_tls_session ();
270 sd
= accept (listen_sd
, (SA
*) & sa_cli
, &client_len
);
273 success ("server: connection from %s, port %d\n",
274 inet_ntop (AF_INET
, &sa_cli
.sin_addr
, topbuf
,
275 sizeof (topbuf
)), ntohs (sa_cli
.sin_port
));
277 gnutls_transport_set_ptr (session
, (gnutls_transport_ptr_t
) sd
);
278 ret
= gnutls_handshake (session
);
282 gnutls_deinit (session
);
283 fail ("server: Handshake has failed (%s)\n\n", gnutls_strerror (ret
));
287 success ("server: Handshake was completed\n");
290 success ("server: TLS version is: %s\n",
291 gnutls_protocol_get_name (gnutls_protocol_get_version
294 /* see the Getting peer's information example */
295 /* print_info(session); */
300 memset (buffer
, 0, MAX_BUF
+ 1);
301 ret
= gnutls_record_recv (session
, buffer
, MAX_BUF
);
306 success ("server: Peer has closed the GnuTLS connection\n");
311 fail ("server: Received corrupted data(%d). Closing...\n", ret
);
316 /* echo data back to the client
318 gnutls_record_send (session
, buffer
, strlen (buffer
));
321 /* do not wait for the peer to close the connection.
323 gnutls_bye (session
, GNUTLS_SHUT_WR
);
326 gnutls_deinit (session
);
330 gnutls_anon_free_server_credentials (anoncred
);
332 gnutls_dh_params_deinit (dh_params
);
334 gnutls_global_deinit ();
337 success ("server: finished\n");