The test now works.
[gnutls.git] / tests / dhepskself.c
blob73c901afe262b9bab2a222e07d8671bff159cc0d
1 /*
2 * Copyright (C) 2004, 2005 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 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 #if 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 <sys/socket.h>
34 #include <sys/wait.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
37 #include <unistd.h>
38 #include <gnutls/gnutls.h>
40 #include "utils.h"
42 /* A very basic TLS client, with PSK authentication.
45 #define MAX_BUF 1024
46 #define MSG "Hello TLS"
48 static void
49 tls_log_func (int level, const char *str)
51 fprintf (stderr, "|<%d>| %s", level, str);
54 /* Connects to the peer and returns a socket
55 * descriptor.
57 int
58 tcp_connect (void)
60 const char *PORT = "5556";
61 const char *SERVER = "127.0.0.1";
62 int err, sd;
63 struct sockaddr_in sa;
65 /* connects to server
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));
75 if (err < 0)
77 fprintf (stderr, "Connect error\n");
78 exit (1);
81 return sd;
84 /* closes the given socket descriptor.
86 void
87 tcp_close (int sd)
89 shutdown (sd, SHUT_RDWR); /* no more receptions */
90 close (sd);
93 void
94 client (void)
96 int ret, sd, ii;
97 gnutls_session_t session;
98 char buffer[MAX_BUF + 1];
99 gnutls_psk_client_credentials_t pskcred;
100 const gnutls_datum_t key = { "DEADBEEF", 8 };
102 gnutls_global_init ();
104 gnutls_global_set_log_function (tls_log_func);
105 // gnutls_global_set_log_level (99);
107 gnutls_psk_allocate_client_credentials (&pskcred);
108 gnutls_psk_set_client_credentials (pskcred, "test", &key,
109 GNUTLS_PSK_KEY_HEX);
111 /* Initialize TLS session
113 gnutls_init (&session, GNUTLS_CLIENT);
115 /* Use default priorities */
116 gnutls_set_default_priority (session);
118 /* put the anonymous credentials to the current session
120 gnutls_credentials_set (session, GNUTLS_CRD_PSK, pskcred);
122 /* connect to the peer
124 sd = tcp_connect ();
126 gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
128 /* Perform the TLS handshake
130 ret = gnutls_handshake (session);
132 if (ret < 0)
134 fail ("client: Handshake failed\n");
135 gnutls_perror (ret);
136 goto end;
138 else
140 success ("client: Handshake was completed\n");
143 gnutls_record_send (session, MSG, strlen (MSG));
145 ret = gnutls_record_recv (session, buffer, MAX_BUF);
146 if (ret == 0)
148 success ("client: Peer has closed the TLS connection\n");
149 goto end;
151 else if (ret < 0)
153 fail ("client: Error: %s\n", gnutls_strerror (ret));
154 goto end;
157 printf ("- Received %d bytes: ", ret);
158 for (ii = 0; ii < ret; ii++)
159 fputc (buffer[ii], stdout);
160 fputs ("\n", stdout);
162 gnutls_bye (session, GNUTLS_SHUT_RDWR);
164 end:
166 tcp_close (sd);
168 gnutls_deinit (session);
170 gnutls_psk_free_client_credentials (pskcred);
172 gnutls_global_deinit ();
175 /* This is a sample TLS 1.0 echo server, for PSK authentication.
178 #define SA struct sockaddr
179 #define MAX_BUF 1024
180 #define PORT 5556 /* listen to 5556 port */
181 #define DH_BITS 1024
183 /* These are global */
184 gnutls_psk_server_credentials_t server_pskcred;
186 gnutls_session_t
187 initialize_tls_session (void)
189 gnutls_session_t session;
191 gnutls_init (&session, GNUTLS_SERVER);
193 /* avoid calling all the priority functions, since the defaults
194 * are adequate.
196 gnutls_set_default_priority (session);
198 gnutls_credentials_set (session, GNUTLS_CRD_PSK, server_pskcred);
200 return session;
203 static gnutls_dh_params_t dh_params;
205 static int
206 generate_dh_params (void)
208 const gnutls_datum_t p3 = { pkcs3, strlen (pkcs3) };
209 /* Generate Diffie Hellman parameters - for use with DHE
210 * kx algorithms. These should be discarded and regenerated
211 * once a day, once a week or once a month. Depending on the
212 * security requirements.
214 gnutls_dh_params_init (&dh_params);
215 return gnutls_dh_params_import_pkcs3 (dh_params, &p3, GNUTLS_X509_FMT_PEM);
218 static int
219 pskfunc (gnutls_session_t session, const char *username, gnutls_datum_t * key)
221 printf ("psk callback to get %s's password\n", username);
222 key->data = gnutls_malloc (4);
223 key->data[0] = 0xDE;
224 key->data[1] = 0xAD;
225 key->data[2] = 0xBE;
226 key->data[3] = 0xEF;
227 key->size = 4;
228 return 0;
231 int err, listen_sd, i;
232 int sd, ret;
233 struct sockaddr_in sa_serv;
234 struct sockaddr_in sa_cli;
235 int client_len;
236 char topbuf[512];
237 gnutls_session_t session;
238 char buffer[MAX_BUF + 1];
239 int optval = 1;
241 void
242 server_start (void)
244 /* this must be called once in the program
246 gnutls_global_init ();
248 gnutls_global_set_log_function (tls_log_func);
249 // gnutls_global_set_log_level (99);
251 generate_dh_params ();
253 gnutls_psk_allocate_server_credentials (&server_pskcred);
254 gnutls_psk_set_server_credentials_function (server_pskcred, pskfunc);
255 gnutls_psk_set_server_dh_params (server_pskcred, dh_params);
257 success ("Launched, generating DH parameters...\n");
259 /* Socket operations
261 listen_sd = socket (AF_INET, SOCK_STREAM, 0);
262 if (err == -1)
264 perror ("socket");
265 fail ("server: socket failed\n");
266 return;
269 memset (&sa_serv, '\0', sizeof (sa_serv));
270 sa_serv.sin_family = AF_INET;
271 sa_serv.sin_addr.s_addr = INADDR_ANY;
272 sa_serv.sin_port = htons (PORT); /* Server Port number */
274 setsockopt (listen_sd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof (int));
276 err = bind (listen_sd, (SA *) & sa_serv, sizeof (sa_serv));
277 if (err == -1)
279 perror ("bind");
280 fail ("server: bind failed\n");
281 return;
284 err = listen (listen_sd, 1024);
285 if (err == -1)
287 perror ("listen");
288 fail ("server: listen failed\n");
289 return;
292 success ("server: ready. Listening to port '%d'.\n", PORT);
295 void
296 server (void)
298 client_len = sizeof (sa_cli);
300 session = initialize_tls_session ();
302 sd = accept (listen_sd, (SA *) & sa_cli, &client_len);
304 success ("server: connection from %s, port %d\n",
305 inet_ntop (AF_INET, &sa_cli.sin_addr, topbuf,
306 sizeof (topbuf)), ntohs (sa_cli.sin_port));
308 gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
309 ret = gnutls_handshake (session);
310 if (ret < 0)
312 close (sd);
313 gnutls_deinit (session);
314 fail ("server: Handshake has failed (%s)\n\n", gnutls_strerror (ret));
315 return;
317 success ("server: Handshake was completed\n");
319 /* see the Getting peer's information example */
320 /* print_info(session); */
322 i = 0;
323 for (;;)
325 bzero (buffer, MAX_BUF + 1);
326 ret = gnutls_record_recv (session, buffer, MAX_BUF);
328 if (ret == 0)
330 success ("server: Peer has closed the GNUTLS connection\n");
331 break;
333 else if (ret < 0)
335 fail ("server: Received corrupted data(%d). Closing...\n", ret);
336 break;
338 else if (ret > 0)
340 /* echo data back to the client
342 gnutls_record_send (session, buffer, strlen (buffer));
345 /* do not wait for the peer to close the connection.
347 gnutls_bye (session, GNUTLS_SHUT_WR);
349 close (sd);
350 gnutls_deinit (session);
352 close (listen_sd);
354 gnutls_psk_free_server_credentials (server_pskcred);
356 gnutls_global_deinit ();
358 success ("server: finished\n");
361 void
362 doit (void)
364 pid_t child;
366 server_start ();
367 if (error_count)
368 return;
370 child = fork ();
371 if (child < 0)
373 perror ("fork");
374 fail ("fork");
375 return;
378 if (child)
380 int status;
381 /* parent */
382 server ();
383 wait (&status);
385 else
386 client ();