2 * Copyright (C) 2004, 2005, 2006, 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 <netinet/in.h>
34 #include <sys/socket.h>
36 #include <arpa/inet.h>
38 #include <gnutls/gnutls.h>
43 tls_log_func (int level
, const char *str
)
45 fprintf (stderr
, "|<%d>| %s", level
, str
);
48 /* A very basic TLS client, with anonymous authentication.
52 #define MSG "Hello TLS"
54 /* Connects to the peer and returns a socket
60 const char *PORT
= "5556";
61 const char *SERVER
= "127.0.0.1";
63 struct sockaddr_in sa
;
67 sd
= socket (AF_INET
, SOCK_STREAM
, 0);
69 memset (&sa
, '\0', sizeof (sa
));
70 sa
.sin_family
= AF_INET
;
71 sa
.sin_port
= htons (atoi (PORT
));
72 inet_pton (AF_INET
, SERVER
, &sa
.sin_addr
);
74 err
= connect (sd
, (struct sockaddr
*) &sa
, sizeof (sa
));
77 fprintf (stderr
, "Connect error\n");
84 /* closes the given socket descriptor.
89 shutdown (sd
, SHUT_RDWR
); /* no more receptions */
97 gnutls_session_t session
;
98 char buffer
[MAX_BUF
+ 1];
99 gnutls_anon_client_credentials_t anoncred
;
100 /* Need to enable anonymous KX specifically. */
101 const int kx_prio
[] = { GNUTLS_KX_ANON_DH
, 0 };
103 gnutls_global_init ();
105 gnutls_global_set_log_function (tls_log_func
);
106 gnutls_global_set_log_level (4711);
108 gnutls_anon_allocate_client_credentials (&anoncred
);
110 /* Initialize TLS session
112 gnutls_init (&session
, GNUTLS_CLIENT
);
114 /* Use default priorities */
115 gnutls_set_default_priority (session
);
116 gnutls_kx_set_priority (session
, kx_prio
);
118 /* put the anonymous credentials to the current session
120 gnutls_credentials_set (session
, GNUTLS_CRD_ANON
, anoncred
);
122 /* connect to the peer
126 gnutls_transport_set_ptr (session
, (gnutls_transport_ptr_t
) sd
);
128 /* Perform the TLS handshake
130 ret
= gnutls_handshake (session
);
134 fail ("client: Handshake failed\n");
140 success ("client: Handshake was completed\n");
143 success ("client: TLS version is: %s\n",
144 gnutls_protocol_get_name (gnutls_protocol_get_version (session
)));
146 gnutls_record_send (session
, MSG
, strlen (MSG
));
148 ret
= gnutls_record_recv (session
, buffer
, MAX_BUF
);
151 success ("client: Peer has closed the TLS connection\n");
156 fail ("client: Error: %s\n", gnutls_strerror (ret
));
160 printf ("- Received %d bytes: ", ret
);
161 for (ii
= 0; ii
< ret
; ii
++)
163 fputc (buffer
[ii
], stdout
);
165 fputs ("\n", stdout
);
167 gnutls_bye (session
, GNUTLS_SHUT_RDWR
);
173 gnutls_deinit (session
);
175 gnutls_anon_free_client_credentials (anoncred
);
177 gnutls_global_deinit ();
180 /* This is a sample TLS 1.0 echo server, for anonymous authentication only.
183 #define SA struct sockaddr
185 #define PORT 5556 /* listen to 5556 port */
188 /* These are global */
189 gnutls_anon_server_credentials_t anoncred
;
192 initialize_tls_session (void)
194 gnutls_session_t session
;
195 const int kx_prio
[] = { GNUTLS_KX_ANON_DH
, 0 };
197 gnutls_init (&session
, GNUTLS_SERVER
);
199 /* avoid calling all the priority functions, since the defaults
202 gnutls_set_default_priority (session
);
203 gnutls_kx_set_priority (session
, kx_prio
);
205 gnutls_credentials_set (session
, GNUTLS_CRD_ANON
, anoncred
);
207 gnutls_dh_set_prime_bits (session
, DH_BITS
);
212 static gnutls_dh_params_t dh_params
;
215 generate_dh_params (void)
217 const gnutls_datum_t p3
= { pkcs3
, strlen (pkcs3
) };
218 /* Generate Diffie Hellman parameters - for use with DHE
219 * kx algorithms. These should be discarded and regenerated
220 * once a day, once a week or once a month. Depending on the
221 * security requirements.
223 gnutls_dh_params_init (&dh_params
);
224 return gnutls_dh_params_import_pkcs3 (dh_params
, &p3
, GNUTLS_X509_FMT_PEM
);
227 int err
, listen_sd
, i
;
229 struct sockaddr_in sa_serv
;
230 struct sockaddr_in sa_cli
;
233 gnutls_session_t session
;
234 char buffer
[MAX_BUF
+ 1];
240 /* this must be called once in the program
242 gnutls_global_init ();
244 gnutls_global_set_log_function (tls_log_func
);
245 gnutls_global_set_log_level (4711);
247 gnutls_anon_allocate_server_credentials (&anoncred
);
249 success ("Launched, generating DH parameters...\n");
251 generate_dh_params ();
253 gnutls_anon_set_server_dh_params (anoncred
, dh_params
);
257 listen_sd
= socket (AF_INET
, SOCK_STREAM
, 0);
261 fail ("server: socket failed\n");
265 memset (&sa_serv
, '\0', sizeof (sa_serv
));
266 sa_serv
.sin_family
= AF_INET
;
267 sa_serv
.sin_addr
.s_addr
= INADDR_ANY
;
268 sa_serv
.sin_port
= htons (PORT
); /* Server Port number */
270 setsockopt (listen_sd
, SOL_SOCKET
, SO_REUSEADDR
, &optval
, sizeof (int));
272 err
= bind (listen_sd
, (SA
*) & sa_serv
, sizeof (sa_serv
));
276 fail ("server: bind failed\n");
280 err
= listen (listen_sd
, 1024);
284 fail ("server: listen failed\n");
288 success ("server: ready. Listening to port '%d'.\n", PORT
);
294 client_len
= sizeof (sa_cli
);
296 session
= initialize_tls_session ();
298 sd
= accept (listen_sd
, (SA
*) & sa_cli
, &client_len
);
300 success ("server: connection from %s, port %d\n",
301 inet_ntop (AF_INET
, &sa_cli
.sin_addr
, topbuf
,
302 sizeof (topbuf
)), ntohs (sa_cli
.sin_port
));
304 gnutls_transport_set_ptr (session
, (gnutls_transport_ptr_t
) sd
);
305 ret
= gnutls_handshake (session
);
309 gnutls_deinit (session
);
310 fail ("server: Handshake has failed (%s)\n\n", gnutls_strerror (ret
));
313 success ("server: Handshake was completed\n");
315 success ("server: TLS version is: %s\n",
316 gnutls_protocol_get_name (gnutls_protocol_get_version (session
)));
318 /* see the Getting peer's information example */
319 /* print_info(session); */
324 bzero (buffer
, MAX_BUF
+ 1);
325 ret
= gnutls_record_recv (session
, buffer
, MAX_BUF
);
329 success ("server: Peer has closed the GNUTLS connection\n");
334 fail ("server: Received corrupted data(%d). Closing...\n", ret
);
339 /* echo data back to the client
341 gnutls_record_send (session
, buffer
, strlen (buffer
));
344 /* do not wait for the peer to close the connection.
346 gnutls_bye (session
, GNUTLS_SHUT_WR
);
349 gnutls_deinit (session
);
353 gnutls_anon_free_server_credentials (anoncred
);
355 gnutls_global_deinit ();
357 success ("server: finished\n");