Fix symbol export rules.
[gnutls.git] / tests / dhepskself.c
blob406668d40d09bf79c6847aa528c98f82c12f6014
1 /*
2 * Copyright (C) 2004, 2005, 2008, 2009 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 tls_log_func (int level, const char *str)
53 fprintf (stderr, "|<%d>| %s", level, str);
56 static void
57 client (void)
59 int ret, sd, ii;
60 gnutls_session_t session;
61 char buffer[MAX_BUF + 1];
62 gnutls_psk_client_credentials_t pskcred;
63 const gnutls_datum_t key = { (char*) "DEADBEEF", 8 };
65 gnutls_global_init ();
67 gnutls_global_set_log_function (tls_log_func);
68 // gnutls_global_set_log_level (99);
70 gnutls_psk_allocate_client_credentials (&pskcred);
71 gnutls_psk_set_client_credentials (pskcred, "test", &key,
72 GNUTLS_PSK_KEY_HEX);
74 /* Initialize TLS session
76 gnutls_init (&session, GNUTLS_CLIENT);
78 /* Use default priorities */
79 gnutls_set_default_priority (session);
81 /* put the anonymous credentials to the current session
83 gnutls_credentials_set (session, GNUTLS_CRD_PSK, pskcred);
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 success ("client: Handshake was completed\n");
106 gnutls_record_send (session, MSG, strlen (MSG));
108 ret = gnutls_record_recv (session, buffer, MAX_BUF);
109 if (ret == 0)
111 success ("client: Peer has closed the TLS connection\n");
112 goto end;
114 else if (ret < 0)
116 fail ("client: Error: %s\n", gnutls_strerror (ret));
117 goto end;
120 printf ("- Received %d bytes: ", ret);
121 for (ii = 0; ii < ret; ii++)
122 fputc (buffer[ii], stdout);
123 fputs ("\n", stdout);
125 gnutls_bye (session, GNUTLS_SHUT_RDWR);
127 end:
129 tcp_close (sd);
131 gnutls_deinit (session);
133 gnutls_psk_free_client_credentials (pskcred);
135 gnutls_global_deinit ();
138 /* This is a sample TLS 1.0 echo server, for PSK authentication.
141 #define SA struct sockaddr
142 #define MAX_BUF 1024
143 #define PORT 5556 /* listen to 5556 port */
145 /* These are global */
146 gnutls_psk_server_credentials_t server_pskcred;
148 static gnutls_session_t
149 initialize_tls_session (void)
151 gnutls_session_t session;
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);
160 gnutls_credentials_set (session, GNUTLS_CRD_PSK, server_pskcred);
162 return session;
165 static gnutls_dh_params_t dh_params;
167 static int
168 generate_dh_params (void)
170 const gnutls_datum_t p3 = { (char*) pkcs3, strlen (pkcs3) };
171 /* Generate Diffie-Hellman parameters - for use with DHE
172 * kx algorithms. These should be discarded and regenerated
173 * once a day, once a week or once a month. Depending on the
174 * security requirements.
176 gnutls_dh_params_init (&dh_params);
177 return gnutls_dh_params_import_pkcs3 (dh_params, &p3, GNUTLS_X509_FMT_PEM);
180 static int
181 pskfunc (gnutls_session_t session, const char *username, gnutls_datum_t * key)
183 printf ("psk callback to get %s's password\n", username);
184 key->data = gnutls_malloc (4);
185 key->data[0] = 0xDE;
186 key->data[1] = 0xAD;
187 key->data[2] = 0xBE;
188 key->data[3] = 0xEF;
189 key->size = 4;
190 return 0;
193 int err, listen_sd, i;
194 int sd, ret;
195 struct sockaddr_in sa_serv;
196 struct sockaddr_in sa_cli;
197 int client_len;
198 char topbuf[512];
199 gnutls_session_t session;
200 char buffer[MAX_BUF + 1];
201 int optval = 1;
203 static void
204 server_start (void)
206 success ("Launched, generating DH parameters...\n");
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, sizeof (int));
225 err = bind (listen_sd, (SA *) & sa_serv, sizeof (sa_serv));
226 if (err == -1)
228 perror ("bind");
229 fail ("server: bind failed\n");
230 return;
233 err = listen (listen_sd, 1024);
234 if (err == -1)
236 perror ("listen");
237 fail ("server: listen failed\n");
238 return;
241 success ("server: ready. Listening to port '%d'.\n", PORT);
244 static void
245 server (void)
247 /* this must be called once in the program
249 gnutls_global_init ();
251 gnutls_global_set_log_function (tls_log_func);
252 if (debug)
253 gnutls_global_set_log_level (4711);
255 generate_dh_params ();
257 gnutls_psk_allocate_server_credentials (&server_pskcred);
258 gnutls_psk_set_server_credentials_function (server_pskcred, pskfunc);
259 gnutls_psk_set_server_dh_params (server_pskcred, dh_params);
261 client_len = sizeof (sa_cli);
263 session = initialize_tls_session ();
265 sd = accept (listen_sd, (SA *) & sa_cli, &client_len);
267 success ("server: connection from %s, port %d\n",
268 inet_ntop (AF_INET, &sa_cli.sin_addr, topbuf,
269 sizeof (topbuf)), ntohs (sa_cli.sin_port));
271 gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
272 ret = gnutls_handshake (session);
273 if (ret < 0)
275 close (sd);
276 gnutls_deinit (session);
277 fail ("server: Handshake has failed (%s)\n\n", gnutls_strerror (ret));
278 return;
280 success ("server: Handshake was completed\n");
282 /* see the Getting peer's information example */
283 /* print_info(session); */
285 i = 0;
286 for (;;)
288 memset (buffer, 0, MAX_BUF + 1);
289 ret = gnutls_record_recv (session, buffer, MAX_BUF);
291 if (ret == 0)
293 success ("server: Peer has closed the GNUTLS connection\n");
294 break;
296 else if (ret < 0)
298 fail ("server: Received corrupted data(%d). Closing...\n", ret);
299 break;
301 else if (ret > 0)
303 /* echo data back to the client
305 gnutls_record_send (session, buffer, strlen (buffer));
308 /* do not wait for the peer to close the connection.
310 gnutls_bye (session, GNUTLS_SHUT_WR);
312 close (sd);
313 gnutls_deinit (session);
315 close (listen_sd);
317 gnutls_psk_free_server_credentials (server_pskcred);
319 gnutls_dh_params_deinit (dh_params);
321 gnutls_global_deinit ();
323 success ("server: finished\n");
326 void
327 doit (void)
329 pid_t child;
331 server_start ();
332 if (error_count)
333 return;
335 child = fork ();
336 if (child < 0)
338 perror ("fork");
339 fail ("fork");
340 return;
343 if (child)
345 int status;
346 /* parent */
347 server ();
348 wait (&status);
350 else
351 client ();