Tizen 2.0 Release
[external/libgnutls26.git] / tests / dhepskself.c
1 /*
2  * Copyright (C) 2004, 2005, 2008, 2009, 2010 Free Software Foundation,
3  * Inc.
4  *
5  * Author: Simon Josefsson
6  *
7  * This file is part of GnuTLS.
8  *
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.
13  *
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.
18  *
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
22  */
23
24 /* Parts copied from GnuTLS example programs. */
25
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #if !defined(_WIN32)
36 #include <sys/wait.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
39 #endif
40 #include <unistd.h>
41 #include <gnutls/gnutls.h>
42
43 #include "tcp.c"
44
45 #include "utils.h"
46
47 /* A very basic TLS client, with PSK authentication.
48  */
49
50 #define MAX_BUF 1024
51 #define MSG "Hello TLS"
52
53 static void
54 tls_log_func (int level, const char *str)
55 {
56   fprintf (stderr, "|<%d>| %s", level, str);
57 }
58
59 static void
60 client (void)
61 {
62   int ret, sd, ii;
63   gnutls_session_t session;
64   char buffer[MAX_BUF + 1];
65   gnutls_psk_client_credentials_t pskcred;
66   const gnutls_datum_t key = { (char *) "DEADBEEF", 8 };
67
68   gnutls_global_init ();
69
70   gnutls_global_set_log_function (tls_log_func);
71   if (debug)
72     gnutls_global_set_log_level (5);
73
74   gnutls_psk_allocate_client_credentials (&pskcred);
75   gnutls_psk_set_client_credentials (pskcred, "test", &key,
76                                      GNUTLS_PSK_KEY_HEX);
77
78   /* Initialize TLS session
79    */
80   gnutls_init (&session, GNUTLS_CLIENT);
81
82   /* Use default priorities */
83   gnutls_priority_set_direct (session, "NORMAL:+DHE-PSK", NULL);
84
85   /* put the anonymous credentials to the current session
86    */
87   gnutls_credentials_set (session, GNUTLS_CRD_PSK, pskcred);
88
89   /* connect to the peer
90    */
91   sd = tcp_connect ();
92
93   gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
94
95   /* Perform the TLS handshake
96    */
97   ret = gnutls_handshake (session);
98
99   if (ret < 0)
100     {
101       fail ("client: Handshake failed\n");
102       gnutls_perror (ret);
103       goto end;
104     }
105   else
106     {
107       if (debug)
108         success ("client: Handshake was completed\n");
109     }
110
111   gnutls_record_send (session, MSG, strlen (MSG));
112
113   ret = gnutls_record_recv (session, buffer, MAX_BUF);
114   if (ret == 0)
115     {
116       if (debug)
117         success ("client: Peer has closed the TLS connection\n");
118       goto end;
119     }
120   else if (ret < 0)
121     {
122       fail ("client: Error: %s\n", gnutls_strerror (ret));
123       goto end;
124     }
125
126   if (debug)
127     {
128       printf ("- Received %d bytes: ", ret);
129       for (ii = 0; ii < ret; ii++)
130         fputc (buffer[ii], stdout);
131       fputs ("\n", stdout);
132     }
133
134   gnutls_bye (session, GNUTLS_SHUT_RDWR);
135
136 end:
137
138   tcp_close (sd);
139
140   gnutls_deinit (session);
141
142   gnutls_psk_free_client_credentials (pskcred);
143
144   gnutls_global_deinit ();
145 }
146
147 /* This is a sample TLS 1.0 echo server, for PSK authentication.
148  */
149
150 #define SA struct sockaddr
151 #define MAX_BUF 1024
152 #define PORT 5556               /* listen to 5556 port */
153
154 /* These are global */
155 gnutls_psk_server_credentials_t server_pskcred;
156
157 static gnutls_session_t
158 initialize_tls_session (void)
159 {
160   gnutls_session_t session;
161
162   gnutls_init (&session, GNUTLS_SERVER);
163
164   /* avoid calling all the priority functions, since the defaults
165    * are adequate.
166    */
167   gnutls_priority_set_direct(session, "NORMAL:+DHE-PSK", NULL);
168
169   gnutls_credentials_set (session, GNUTLS_CRD_PSK, server_pskcred);
170
171   return session;
172 }
173
174 static gnutls_dh_params_t dh_params;
175
176 static int
177 generate_dh_params (void)
178 {
179   const gnutls_datum_t p3 = { (char *) pkcs3, strlen (pkcs3) };
180   /* Generate Diffie-Hellman parameters - for use with DHE
181    * kx algorithms. These should be discarded and regenerated
182    * once a day, once a week or once a month. Depending on the
183    * security requirements.
184    */
185   gnutls_dh_params_init (&dh_params);
186   return gnutls_dh_params_import_pkcs3 (dh_params, &p3, GNUTLS_X509_FMT_PEM);
187 }
188
189 static int
190 pskfunc (gnutls_session_t session, const char *username, gnutls_datum_t * key)
191 {
192   if (debug)
193     printf ("psk callback to get %s's password\n", username);
194   key->data = gnutls_malloc (4);
195   key->data[0] = 0xDE;
196   key->data[1] = 0xAD;
197   key->data[2] = 0xBE;
198   key->data[3] = 0xEF;
199   key->size = 4;
200   return 0;
201 }
202
203 int err, listen_sd, i;
204 int sd, ret;
205 struct sockaddr_in sa_serv;
206 struct sockaddr_in sa_cli;
207 int client_len;
208 char topbuf[512];
209 gnutls_session_t session;
210 char buffer[MAX_BUF + 1];
211 int optval = 1;
212
213 static void
214 server_start (void)
215 {
216   if (debug)
217     success ("Launched, generating DH parameters...\n");
218
219   /* Socket operations
220    */
221   listen_sd = socket (AF_INET, SOCK_STREAM, 0);
222   if (err == -1)
223     {
224       perror ("socket");
225       fail ("server: socket failed\n");
226       return;
227     }
228
229   memset (&sa_serv, '\0', sizeof (sa_serv));
230   sa_serv.sin_family = AF_INET;
231   sa_serv.sin_addr.s_addr = INADDR_ANY;
232   sa_serv.sin_port = htons (PORT);      /* Server Port number */
233
234   setsockopt (listen_sd, SOL_SOCKET, SO_REUSEADDR, (void *) &optval,
235               sizeof (int));
236
237   err = bind (listen_sd, (SA *) & sa_serv, sizeof (sa_serv));
238   if (err == -1)
239     {
240       perror ("bind");
241       fail ("server: bind failed\n");
242       return;
243     }
244
245   err = listen (listen_sd, 1024);
246   if (err == -1)
247     {
248       perror ("listen");
249       fail ("server: listen failed\n");
250       return;
251     }
252
253   if (debug)
254     success ("server: ready. Listening to port '%d'.\n", PORT);
255 }
256
257 static void
258 server (void)
259 {
260   /* this must be called once in the program
261    */
262   gnutls_global_init ();
263
264   gnutls_global_set_log_function (tls_log_func);
265   if (debug)
266     gnutls_global_set_log_level (4711);
267
268   generate_dh_params ();
269
270   gnutls_psk_allocate_server_credentials (&server_pskcred);
271   gnutls_psk_set_server_credentials_function (server_pskcred, pskfunc);
272   gnutls_psk_set_server_dh_params (server_pskcred, dh_params);
273
274   client_len = sizeof (sa_cli);
275
276   session = initialize_tls_session ();
277
278   sd = accept (listen_sd, (SA *) & sa_cli, &client_len);
279
280   if (debug)
281     success ("server: connection from %s, port %d\n",
282              inet_ntop (AF_INET, &sa_cli.sin_addr, topbuf,
283                         sizeof (topbuf)), ntohs (sa_cli.sin_port));
284
285   gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
286   ret = gnutls_handshake (session);
287   if (ret < 0)
288     {
289       close (sd);
290       gnutls_deinit (session);
291       fail ("server: Handshake has failed (%s)\n\n", gnutls_strerror (ret));
292       return;
293     }
294   if (debug)
295     success ("server: Handshake was completed\n");
296
297   /* see the Getting peer's information example */
298   /* print_info(session); */
299
300   i = 0;
301   for (;;)
302     {
303       memset (buffer, 0, MAX_BUF + 1);
304       ret = gnutls_record_recv (session, buffer, MAX_BUF);
305
306       if (ret == 0)
307         {
308           if (debug)
309             success ("server: Peer has closed the GnuTLS connection\n");
310           break;
311         }
312       else if (ret < 0)
313         {
314           fail ("server: Received corrupted data(%d). Closing...\n", ret);
315           break;
316         }
317       else if (ret > 0)
318         {
319           /* echo data back to the client
320            */
321           gnutls_record_send (session, buffer, strlen (buffer));
322         }
323     }
324   /* do not wait for the peer to close the connection.
325    */
326   gnutls_bye (session, GNUTLS_SHUT_WR);
327
328   close (sd);
329   gnutls_deinit (session);
330
331   close (listen_sd);
332
333   gnutls_psk_free_server_credentials (server_pskcred);
334
335   gnutls_dh_params_deinit (dh_params);
336
337   gnutls_global_deinit ();
338
339   if (debug)
340     success ("server: finished\n");
341 }
342
343 void
344 doit (void)
345 {
346   pid_t child;
347
348   server_start ();
349   if (error_count)
350     return;
351
352   child = fork ();
353   if (child < 0)
354     {
355       perror ("fork");
356       fail ("fork");
357       return;
358     }
359
360   if (child)
361     {
362       int status;
363       /* parent */
364       server ();
365       wait (&status);
366     }
367   else
368     client ();
369 }