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. */
32 #include <sys/types.h>
33 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
38 #include <gnutls/gnutls.h>
42 /* A very basic TLS client, with PSK authentication.
46 #define MSG "Hello TLS"
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
60 const char *PORT
= "5556";
61 const char *SERVER
= "127.0.0.1";
63 struct sockaddr_in sa
;
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
));
77 fprintf (stderr
, "Connect error\n");
84 /* closes the given socket descriptor.
89 shutdown (sd
, SHUT_RDWR
); /* no more receptions */
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
,
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
126 gnutls_transport_set_ptr (session
, (gnutls_transport_ptr_t
) sd
);
128 /* Perform the TLS handshake
130 ret
= gnutls_handshake (session
);
134 fail ("client: Handshake failed\n");
140 success ("client: Handshake was completed\n");
143 gnutls_record_send (session
, MSG
, strlen (MSG
));
145 ret
= gnutls_record_recv (session
, buffer
, MAX_BUF
);
148 success ("client: Peer has closed the TLS connection\n");
153 fail ("client: Error: %s\n", gnutls_strerror (ret
));
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
);
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
180 #define PORT 5556 /* listen to 5556 port */
183 /* These are global */
184 gnutls_psk_server_credentials_t server_pskcred
;
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
196 gnutls_set_default_priority (session
);
198 gnutls_credentials_set (session
, GNUTLS_CRD_PSK
, server_pskcred
);
203 static gnutls_dh_params_t dh_params
;
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
);
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);
231 int err
, listen_sd
, i
;
233 struct sockaddr_in sa_serv
;
234 struct sockaddr_in sa_cli
;
237 gnutls_session_t session
;
238 char buffer
[MAX_BUF
+ 1];
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");
261 listen_sd
= socket (AF_INET
, SOCK_STREAM
, 0);
265 fail ("server: socket failed\n");
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
));
280 fail ("server: bind failed\n");
284 err
= listen (listen_sd
, 1024);
288 fail ("server: listen failed\n");
292 success ("server: ready. Listening to port '%d'.\n", PORT
);
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
);
313 gnutls_deinit (session
);
314 fail ("server: Handshake has failed (%s)\n\n", gnutls_strerror (ret
));
317 success ("server: Handshake was completed\n");
319 /* see the Getting peer's information example */
320 /* print_info(session); */
325 bzero (buffer
, MAX_BUF
+ 1);
326 ret
= gnutls_record_recv (session
, buffer
, MAX_BUF
);
330 success ("server: Peer has closed the GNUTLS connection\n");
335 fail ("server: Received corrupted data(%d). Closing...\n", ret
);
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
);
350 gnutls_deinit (session
);
354 gnutls_psk_free_server_credentials (server_pskcred
);
356 gnutls_global_deinit ();
358 success ("server: finished\n");