added missing file
[gnutls.git] / tests / anonself.c
blobe2085ac3a26217a873121e400afeb6fe0cc4f01c
1 /*
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. */
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <netinet/in.h>
34 #include <sys/socket.h>
35 #if !defined(_WIN32)
36 #include <sys/wait.h>
37 #include <arpa/inet.h>
38 #endif
39 #include <unistd.h>
40 #include <gnutls/gnutls.h>
42 #include "tcp.c"
44 #include "utils.h"
46 static void
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.
55 #define MAX_BUF 1024
56 #define MSG "Hello TLS"
58 static void
59 client (void)
61 int ret, sd, ii;
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);
70 if (debug)
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
88 sd = tcp_connect ();
90 gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
92 /* Perform the TLS handshake
94 ret = gnutls_handshake (session);
96 if (ret < 0)
98 fail ("client: Handshake failed\n");
99 gnutls_perror (ret);
100 goto end;
102 else
104 if (debug)
105 success ("client: Handshake was completed\n");
108 if (debug)
109 success ("client: TLS version is: %s\n",
110 gnutls_protocol_get_name (gnutls_protocol_get_version
111 (session)));
113 gnutls_record_send (session, MSG, strlen (MSG));
115 ret = gnutls_record_recv (session, buffer, MAX_BUF);
116 if (ret == 0)
118 if (debug)
119 success ("client: Peer has closed the TLS connection\n");
120 goto end;
122 else if (ret < 0)
124 fail ("client: Error: %s\n", gnutls_strerror (ret));
125 goto end;
128 if (debug)
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);
140 end:
142 tcp_close (sd);
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
155 #define MAX_BUF 1024
156 #define PORT 5556 /* listen to 5556 port */
157 #define DH_BITS 1024
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
170 * are adequate.
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);
178 return session;
181 static gnutls_dh_params_t dh_params;
183 static int
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;
197 int sd, ret;
198 struct sockaddr_in sa_serv;
199 struct sockaddr_in sa_cli;
200 socklen_t client_len;
201 char topbuf[512];
202 gnutls_session_t session;
203 char buffer[MAX_BUF + 1];
204 int optval = 1;
206 static void
207 server_start (void)
209 /* Socket operations
211 listen_sd = socket (AF_INET, SOCK_STREAM, 0);
212 if (err == -1)
214 perror ("socket");
215 fail ("server: socket failed\n");
216 return;
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,
225 sizeof (int));
227 err = bind (listen_sd, (SA *) & sa_serv, sizeof (sa_serv));
228 if (err == -1)
230 perror ("bind");
231 fail ("server: bind failed\n");
232 return;
235 err = listen (listen_sd, 1024);
236 if (err == -1)
238 perror ("listen");
239 fail ("server: listen failed\n");
240 return;
243 if (debug)
244 success ("server: ready. Listening to port '%d'.\n", PORT);
247 static void
248 server (void)
250 /* this must be called once in the program
252 gnutls_global_init ();
254 gnutls_global_set_log_function (tls_log_func);
255 if (debug)
256 gnutls_global_set_log_level (4711);
258 gnutls_anon_allocate_server_credentials (&anoncred);
260 if (debug)
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);
273 if (debug)
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);
280 if (ret < 0)
282 close (sd);
283 gnutls_deinit (session);
284 fail ("server: Handshake has failed (%s)\n\n", gnutls_strerror (ret));
285 return;
287 if (debug)
288 success ("server: Handshake was completed\n");
290 if (debug)
291 success ("server: TLS version is: %s\n",
292 gnutls_protocol_get_name (gnutls_protocol_get_version
293 (session)));
295 /* see the Getting peer's information example */
296 /* print_info(session); */
298 i = 0;
299 for (;;)
301 memset (buffer, 0, MAX_BUF + 1);
302 ret = gnutls_record_recv (session, buffer, MAX_BUF);
304 if (ret == 0)
306 if (debug)
307 success ("server: Peer has closed the GnuTLS connection\n");
308 break;
310 else if (ret < 0)
312 fail ("server: Received corrupted data(%d). Closing...\n", ret);
313 break;
315 else if (ret > 0)
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);
326 close (sd);
327 gnutls_deinit (session);
329 close (listen_sd);
331 gnutls_anon_free_server_credentials (anoncred);
333 gnutls_dh_params_deinit (dh_params);
335 gnutls_global_deinit ();
337 if (debug)
338 success ("server: finished\n");
341 void
342 doit (void)
344 pid_t child;
346 server_start ();
347 if (error_count)
348 return;
350 child = fork ();
351 if (child < 0)
353 perror ("fork");
354 fail ("fork");
355 return;
358 if (child)
360 int status;
361 /* parent */
362 server ();
363 wait (&status);
365 else
366 client ();