Add `gnutls/dtls.h' to the distribution.
[gnutls.git] / tests / anonself.c
blob134b88babb11281a9ce40121c7ea9652dc3c524d
1 /*
2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software
3 * Foundation, Inc.
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. */
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sys/types.h>
34 #include <netinet/in.h>
35 #include <sys/socket.h>
36 #include <sys/wait.h>
37 #include <arpa/inet.h>
38 #include <unistd.h>
39 #include <gnutls/gnutls.h>
41 #include "tcp.c"
43 #include "utils.h"
45 static void
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.
54 #define MAX_BUF 1024
55 #define MSG "Hello TLS"
57 static void
58 client (void)
60 int ret, sd, ii;
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);
69 if (debug)
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
87 sd = tcp_connect ();
89 gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
91 /* Perform the TLS handshake
93 ret = gnutls_handshake (session);
95 if (ret < 0)
97 fail ("client: Handshake failed\n");
98 gnutls_perror (ret);
99 goto end;
101 else
103 if (debug)
104 success ("client: Handshake was completed\n");
107 if (debug)
108 success ("client: TLS version is: %s\n",
109 gnutls_protocol_get_name (gnutls_protocol_get_version
110 (session)));
112 gnutls_record_send (session, MSG, strlen (MSG));
114 ret = gnutls_record_recv (session, buffer, MAX_BUF);
115 if (ret == 0)
117 if (debug)
118 success ("client: Peer has closed the TLS connection\n");
119 goto end;
121 else if (ret < 0)
123 fail ("client: Error: %s\n", gnutls_strerror (ret));
124 goto end;
127 if (debug)
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);
139 end:
141 tcp_close (sd);
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
154 #define MAX_BUF 1024
155 #define PORT 5556 /* listen to 5556 port */
156 #define DH_BITS 1024
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
169 * are adequate.
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);
177 return session;
180 static gnutls_dh_params_t dh_params;
182 static int
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;
196 int sd, ret;
197 struct sockaddr_in sa_serv;
198 struct sockaddr_in sa_cli;
199 int client_len;
200 char topbuf[512];
201 gnutls_session_t session;
202 char buffer[MAX_BUF + 1];
203 int optval = 1;
205 static void
206 server_start (void)
208 /* Socket operations
210 listen_sd = socket (AF_INET, SOCK_STREAM, 0);
211 if (err == -1)
213 perror ("socket");
214 fail ("server: socket failed\n");
215 return;
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,
224 sizeof (int));
226 err = bind (listen_sd, (SA *) & sa_serv, sizeof (sa_serv));
227 if (err == -1)
229 perror ("bind");
230 fail ("server: bind failed\n");
231 return;
234 err = listen (listen_sd, 1024);
235 if (err == -1)
237 perror ("listen");
238 fail ("server: listen failed\n");
239 return;
242 if (debug)
243 success ("server: ready. Listening to port '%d'.\n", PORT);
246 static void
247 server (void)
249 /* this must be called once in the program
251 gnutls_global_init ();
253 gnutls_global_set_log_function (tls_log_func);
254 if (debug)
255 gnutls_global_set_log_level (4711);
257 gnutls_anon_allocate_server_credentials (&anoncred);
259 if (debug)
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);
272 if (debug)
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);
279 if (ret < 0)
281 close (sd);
282 gnutls_deinit (session);
283 fail ("server: Handshake has failed (%s)\n\n", gnutls_strerror (ret));
284 return;
286 if (debug)
287 success ("server: Handshake was completed\n");
289 if (debug)
290 success ("server: TLS version is: %s\n",
291 gnutls_protocol_get_name (gnutls_protocol_get_version
292 (session)));
294 /* see the Getting peer's information example */
295 /* print_info(session); */
297 i = 0;
298 for (;;)
300 memset (buffer, 0, MAX_BUF + 1);
301 ret = gnutls_record_recv (session, buffer, MAX_BUF);
303 if (ret == 0)
305 if (debug)
306 success ("server: Peer has closed the GnuTLS connection\n");
307 break;
309 else if (ret < 0)
311 fail ("server: Received corrupted data(%d). Closing...\n", ret);
312 break;
314 else if (ret > 0)
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);
325 close (sd);
326 gnutls_deinit (session);
328 close (listen_sd);
330 gnutls_anon_free_server_credentials (anoncred);
332 gnutls_dh_params_deinit (dh_params);
334 gnutls_global_deinit ();
336 if (debug)
337 success ("server: finished\n");
340 void
341 doit (void)
343 pid_t child;
345 server_start ();
346 if (error_count)
347 return;
349 child = fork ();
350 if (child < 0)
352 perror ("fork");
353 fail ("fork");
354 return;
357 if (child)
359 int status;
360 /* parent */
361 server ();
362 wait (&status);
364 else
365 client ();