Fix compile error.
[gnutls.git] / tests / pskself.c
blobd3e42f473f043d7e1e13ce19a4b535657b837cd0
1 /*
2 * Copyright (C) 2004, 2005, 2008 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 #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 <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 "tcp.c"
42 #include "utils.h"
44 /* A very basic TLS client, with PSK authentication.
47 #define MAX_BUF 1024
48 #define MSG "Hello TLS"
50 static void
51 client (void)
53 int ret, sd, ii;
54 gnutls_session_t session;
55 char buffer[MAX_BUF + 1];
56 gnutls_psk_client_credentials_t pskcred;
57 /* Need to enable anonymous KX specifically. */
58 const int kx_prio[] = { GNUTLS_KX_PSK, 0 };
59 const gnutls_datum_t key = { (char*) "DEADBEEF", 8 };
61 gnutls_global_init ();
63 gnutls_psk_allocate_client_credentials (&pskcred);
64 gnutls_psk_set_client_credentials (pskcred, "test", &key,
65 GNUTLS_PSK_KEY_HEX);
67 /* Initialize TLS session
69 gnutls_init (&session, GNUTLS_CLIENT);
71 /* Use default priorities */
72 gnutls_set_default_priority (session);
73 gnutls_kx_set_priority (session, kx_prio);
75 /* put the anonymous credentials to the current session
77 gnutls_credentials_set (session, GNUTLS_CRD_PSK, pskcred);
79 /* connect to the peer
81 sd = tcp_connect ();
83 gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
85 /* Perform the TLS handshake
87 ret = gnutls_handshake (session);
89 if (ret < 0)
91 fail ("client: Handshake failed\n");
92 gnutls_perror (ret);
93 goto end;
95 else
97 success ("client: Handshake was completed\n");
100 gnutls_record_send (session, MSG, strlen (MSG));
102 ret = gnutls_record_recv (session, buffer, MAX_BUF);
103 if (ret == 0)
105 success ("client: Peer has closed the TLS connection\n");
106 goto end;
108 else if (ret < 0)
110 fail ("client: Error: %s\n", gnutls_strerror (ret));
111 goto end;
114 if (debug)
116 printf ("- Received %d bytes: ", ret);
117 for (ii = 0; ii < ret; ii++)
119 fputc (buffer[ii], stdout);
121 fputs ("\n", stdout);
124 gnutls_bye (session, GNUTLS_SHUT_RDWR);
126 end:
128 tcp_close (sd);
130 gnutls_deinit (session);
132 gnutls_psk_free_client_credentials (pskcred);
134 gnutls_global_deinit ();
137 /* This is a sample TLS 1.0 echo server, for PSK authentication.
140 #define SA struct sockaddr
141 #define MAX_BUF 1024
142 #define PORT 5556 /* listen to 5556 port */
144 /* These are global */
145 gnutls_psk_server_credentials_t server_pskcred;
147 static gnutls_session_t
148 initialize_tls_session (void)
150 gnutls_session_t session;
151 const int kx_prio[] = { GNUTLS_KX_PSK, 0 };
153 gnutls_init (&session, GNUTLS_SERVER);
155 /* avoid calling all the priority functions, since the defaults
156 * are adequate.
158 gnutls_set_default_priority (session);
159 gnutls_kx_set_priority (session, kx_prio);
161 gnutls_credentials_set (session, GNUTLS_CRD_PSK, server_pskcred);
163 return session;
166 static int
167 pskfunc (gnutls_session_t session, const char *username, gnutls_datum_t * key)
169 printf ("psk: username %s\n", username);
170 key->data = gnutls_malloc (4);
171 key->data[0] = 0xDE;
172 key->data[1] = 0xAD;
173 key->data[2] = 0xBE;
174 key->data[3] = 0xEF;
175 key->size = 4;
176 return 0;
179 int err, listen_sd, i;
180 int sd, ret;
181 struct sockaddr_in sa_serv;
182 struct sockaddr_in sa_cli;
183 int client_len;
184 char topbuf[512];
185 gnutls_session_t session;
186 char buffer[MAX_BUF + 1];
187 int optval = 1;
189 static void
190 server_start (void)
192 success ("Launched...\n");
194 /* Socket operations
196 listen_sd = socket (AF_INET, SOCK_STREAM, 0);
197 if (err == -1)
199 perror ("socket");
200 fail ("server: socket failed\n");
201 return;
204 memset (&sa_serv, '\0', sizeof (sa_serv));
205 sa_serv.sin_family = AF_INET;
206 sa_serv.sin_addr.s_addr = INADDR_ANY;
207 sa_serv.sin_port = htons (PORT); /* Server Port number */
209 setsockopt (listen_sd, SOL_SOCKET, SO_REUSEADDR, (void *) &optval, sizeof (int));
211 err = bind (listen_sd, (SA *) & sa_serv, sizeof (sa_serv));
212 if (err == -1)
214 perror ("bind");
215 fail ("server: bind failed\n");
216 return;
219 err = listen (listen_sd, 1024);
220 if (err == -1)
222 perror ("listen");
223 fail ("server: listen failed\n");
224 return;
227 success ("server: ready. Listening to port '%d'.\n", PORT);
230 static void
231 server (void)
233 /* this must be called once in the program
235 gnutls_global_init ();
237 gnutls_psk_allocate_server_credentials (&server_pskcred);
238 gnutls_psk_set_server_credentials_function (server_pskcred, pskfunc);
240 client_len = sizeof (sa_cli);
242 session = initialize_tls_session ();
244 sd = accept (listen_sd, (SA *) & sa_cli, &client_len);
246 success ("server: connection from %s, port %d\n",
247 inet_ntop (AF_INET, &sa_cli.sin_addr, topbuf,
248 sizeof (topbuf)), ntohs (sa_cli.sin_port));
250 gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
251 ret = gnutls_handshake (session);
252 if (ret < 0)
254 close (sd);
255 gnutls_deinit (session);
256 fail ("server: Handshake has failed (%s)\n\n", gnutls_strerror (ret));
257 return;
259 success ("server: Handshake was completed\n");
261 /* see the Getting peer's information example */
262 /* print_info(session); */
264 i = 0;
265 for (;;)
267 memset (buffer, 0, MAX_BUF + 1);
268 ret = gnutls_record_recv (session, buffer, MAX_BUF);
270 if (ret == 0)
272 success ("server: Peer has closed the GNUTLS connection\n");
273 break;
275 else if (ret < 0)
277 fail ("server: Received corrupted data(%d). Closing...\n", ret);
278 break;
280 else if (ret > 0)
282 /* echo data back to the client
284 gnutls_record_send (session, buffer, strlen (buffer));
287 /* do not wait for the peer to close the connection.
289 gnutls_bye (session, GNUTLS_SHUT_WR);
291 close (sd);
292 gnutls_deinit (session);
294 close (listen_sd);
296 gnutls_psk_free_server_credentials (server_pskcred);
298 gnutls_global_deinit ();
300 success ("server: finished\n");
303 void
304 doit (void)
306 pid_t child;
308 server_start ();
309 if (error_count)
310 return;
312 child = fork ();
313 if (child < 0)
315 perror ("fork");
316 fail ("fork");
317 return;
320 if (child)
322 int status;
323 /* parent */
324 server ();
325 wait (&status);
327 else
328 client ();