We reverted the ABI bump.
[gnutls.git] / tests / pskself.c
blobca82baee83955e9778b8e5b836fe090226c455d5
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 /* Connects to the peer and returns a socket
49 * descriptor.
51 int
52 tcp_connect (void)
54 const char *PORT = "5556";
55 const char *SERVER = "127.0.0.1";
56 int err, sd;
57 struct sockaddr_in sa;
59 /* connects to server
61 sd = socket (AF_INET, SOCK_STREAM, 0);
63 memset (&sa, '\0', sizeof (sa));
64 sa.sin_family = AF_INET;
65 sa.sin_port = htons (atoi (PORT));
66 inet_pton (AF_INET, SERVER, &sa.sin_addr);
68 err = connect (sd, (struct sockaddr *) &sa, sizeof (sa));
69 if (err < 0)
71 fprintf (stderr, "Connect error\n");
72 exit (1);
75 return sd;
78 /* closes the given socket descriptor.
80 void
81 tcp_close (int sd)
83 shutdown (sd, SHUT_RDWR); /* no more receptions */
84 close (sd);
87 void
88 client (void)
90 int ret, sd, ii;
91 gnutls_session_t session;
92 char buffer[MAX_BUF + 1];
93 gnutls_psk_client_credentials_t pskcred;
94 /* Need to enable anonymous KX specifically. */
95 const int kx_prio[] = { GNUTLS_KX_PSK, 0 };
96 const gnutls_datum_t key = { "DEADBEEF", 8 };
98 gnutls_global_init ();
100 gnutls_psk_allocate_client_credentials (&pskcred);
101 gnutls_psk_set_client_credentials (pskcred, "test", &key,
102 GNUTLS_PSK_KEY_HEX);
104 /* Initialize TLS session
106 gnutls_init (&session, GNUTLS_CLIENT);
108 /* Use default priorities */
109 gnutls_set_default_priority (session);
110 gnutls_kx_set_priority (session, kx_prio);
112 /* put the anonymous credentials to the current session
114 gnutls_credentials_set (session, GNUTLS_CRD_PSK, pskcred);
116 /* connect to the peer
118 sd = tcp_connect ();
120 gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
122 /* Perform the TLS handshake
124 ret = gnutls_handshake (session);
126 if (ret < 0)
128 fail ("client: Handshake failed\n");
129 gnutls_perror (ret);
130 goto end;
132 else
134 success ("client: Handshake was completed\n");
137 gnutls_record_send (session, MSG, strlen (MSG));
139 ret = gnutls_record_recv (session, buffer, MAX_BUF);
140 if (ret == 0)
142 success ("client: Peer has closed the TLS connection\n");
143 goto end;
145 else if (ret < 0)
147 fail ("client: Error: %s\n", gnutls_strerror (ret));
148 goto end;
151 if (debug)
153 printf ("- Received %d bytes: ", ret);
154 for (ii = 0; ii < ret; ii++)
156 fputc (buffer[ii], stdout);
158 fputs ("\n", stdout);
161 gnutls_bye (session, GNUTLS_SHUT_RDWR);
163 end:
165 tcp_close (sd);
167 gnutls_deinit (session);
169 gnutls_psk_free_client_credentials (pskcred);
171 gnutls_global_deinit ();
174 /* This is a sample TLS 1.0 echo server, for PSK authentication.
177 #define SA struct sockaddr
178 #define MAX_BUF 1024
179 #define PORT 5556 /* listen to 5556 port */
181 /* These are global */
182 gnutls_psk_server_credentials_t server_pskcred;
184 gnutls_session_t
185 initialize_tls_session (void)
187 gnutls_session_t session;
188 const int kx_prio[] = { GNUTLS_KX_PSK, 0 };
190 gnutls_init (&session, GNUTLS_SERVER);
192 /* avoid calling all the priority functions, since the defaults
193 * are adequate.
195 gnutls_set_default_priority (session);
196 gnutls_kx_set_priority (session, kx_prio);
198 gnutls_credentials_set (session, GNUTLS_CRD_PSK, server_pskcred);
200 return session;
203 static int
204 pskfunc (gnutls_session_t session, const char *username, gnutls_datum_t * key)
206 printf ("psk: username %s\n", username);
207 key->data = gnutls_malloc (4);
208 key->data[0] = 0xDE;
209 key->data[1] = 0xAD;
210 key->data[2] = 0xBE;
211 key->data[3] = 0xEF;
212 key->size = 4;
213 return 0;
216 int err, listen_sd, i;
217 int sd, ret;
218 struct sockaddr_in sa_serv;
219 struct sockaddr_in sa_cli;
220 int client_len;
221 char topbuf[512];
222 gnutls_session_t session;
223 char buffer[MAX_BUF + 1];
224 int optval = 1;
226 void
227 server_start (void)
229 /* this must be called once in the program
231 gnutls_global_init ();
233 gnutls_psk_allocate_server_credentials (&server_pskcred);
234 gnutls_psk_set_server_credentials_function (server_pskcred, pskfunc);
236 success ("Launched...\n");
238 /* Socket operations
240 listen_sd = socket (AF_INET, SOCK_STREAM, 0);
241 if (err == -1)
243 perror ("socket");
244 fail ("server: socket failed\n");
245 return;
248 memset (&sa_serv, '\0', sizeof (sa_serv));
249 sa_serv.sin_family = AF_INET;
250 sa_serv.sin_addr.s_addr = INADDR_ANY;
251 sa_serv.sin_port = htons (PORT); /* Server Port number */
253 setsockopt (listen_sd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof (int));
255 err = bind (listen_sd, (SA *) & sa_serv, sizeof (sa_serv));
256 if (err == -1)
258 perror ("bind");
259 fail ("server: bind failed\n");
260 return;
263 err = listen (listen_sd, 1024);
264 if (err == -1)
266 perror ("listen");
267 fail ("server: listen failed\n");
268 return;
271 success ("server: ready. Listening to port '%d'.\n", PORT);
274 void
275 server (void)
277 client_len = sizeof (sa_cli);
279 session = initialize_tls_session ();
281 sd = accept (listen_sd, (SA *) & sa_cli, &client_len);
283 success ("server: connection from %s, port %d\n",
284 inet_ntop (AF_INET, &sa_cli.sin_addr, topbuf,
285 sizeof (topbuf)), ntohs (sa_cli.sin_port));
287 gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
288 ret = gnutls_handshake (session);
289 if (ret < 0)
291 close (sd);
292 gnutls_deinit (session);
293 fail ("server: Handshake has failed (%s)\n\n", gnutls_strerror (ret));
294 return;
296 success ("server: Handshake was completed\n");
298 /* see the Getting peer's information example */
299 /* print_info(session); */
301 i = 0;
302 for (;;)
304 bzero (buffer, MAX_BUF + 1);
305 ret = gnutls_record_recv (session, buffer, MAX_BUF);
307 if (ret == 0)
309 success ("server: Peer has closed the GNUTLS connection\n");
310 break;
312 else if (ret < 0)
314 fail ("server: Received corrupted data(%d). Closing...\n", ret);
315 break;
317 else if (ret > 0)
319 /* echo data back to the client
321 gnutls_record_send (session, buffer, strlen (buffer));
324 /* do not wait for the peer to close the connection.
326 gnutls_bye (session, GNUTLS_SHUT_WR);
328 close (sd);
329 gnutls_deinit (session);
331 close (listen_sd);
333 gnutls_psk_free_server_credentials (server_pskcred);
335 gnutls_global_deinit ();
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 ();