2 * Copyright (C) 2004-2012 Free Software Foundation, Inc.
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>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
40 #include <gnutls/gnutls.h>
46 /* A very basic TLS client, with PSK authentication.
50 #define MSG "Hello TLS"
53 tls_log_func (int level
, const char *str
)
55 fprintf (stderr
, "|<%d>| %s", level
, str
);
62 gnutls_session_t session
;
63 char buffer
[MAX_BUF
+ 1];
64 gnutls_psk_client_credentials_t pskcred
;
65 const gnutls_datum_t key
= { (void *) "DEADBEEF", 8 };
67 gnutls_global_init ();
69 gnutls_global_set_log_function (tls_log_func
);
71 gnutls_global_set_log_level (5);
73 gnutls_psk_allocate_client_credentials (&pskcred
);
74 gnutls_psk_set_client_credentials (pskcred
, "test", &key
,
77 /* Initialize TLS session
79 gnutls_init (&session
, GNUTLS_CLIENT
);
81 /* Use default priorities */
82 gnutls_priority_set_direct (session
, "NORMAL:+DHE-PSK", NULL
);
84 /* put the anonymous credentials to the current session
86 gnutls_credentials_set (session
, GNUTLS_CRD_PSK
, pskcred
);
88 /* connect to the peer
92 gnutls_transport_set_ptr (session
, (gnutls_transport_ptr_t
) sd
);
94 /* Perform the TLS handshake
96 ret
= gnutls_handshake (session
);
100 fail ("client: Handshake failed\n");
107 success ("client: Handshake was completed\n");
110 gnutls_record_send (session
, MSG
, strlen (MSG
));
112 ret
= gnutls_record_recv (session
, buffer
, MAX_BUF
);
116 success ("client: Peer has closed the TLS connection\n");
121 fail ("client: Error: %s\n", gnutls_strerror (ret
));
127 printf ("- Received %d bytes: ", ret
);
128 for (ii
= 0; ii
< ret
; ii
++)
129 fputc (buffer
[ii
], stdout
);
130 fputs ("\n", stdout
);
133 gnutls_bye (session
, GNUTLS_SHUT_RDWR
);
139 gnutls_deinit (session
);
141 gnutls_psk_free_client_credentials (pskcred
);
143 gnutls_global_deinit ();
146 /* This is a sample TLS 1.0 echo server, for PSK authentication.
149 #define SA struct sockaddr
151 #define PORT 5556 /* listen to 5556 port */
153 /* These are global */
154 gnutls_psk_server_credentials_t server_pskcred
;
156 static gnutls_session_t
157 initialize_tls_session (void)
159 gnutls_session_t session
;
161 gnutls_init (&session
, GNUTLS_SERVER
);
163 /* avoid calling all the priority functions, since the defaults
166 gnutls_priority_set_direct(session
, "NORMAL:+DHE-PSK", NULL
);
168 gnutls_credentials_set (session
, GNUTLS_CRD_PSK
, server_pskcred
);
173 static gnutls_dh_params_t dh_params
;
176 generate_dh_params (void)
178 const gnutls_datum_t p3
= { (void *) pkcs3
, strlen (pkcs3
) };
179 /* Generate Diffie-Hellman parameters - for use with DHE
180 * kx algorithms. These should be discarded and regenerated
181 * once a day, once a week or once a month. Depending on the
182 * security requirements.
184 gnutls_dh_params_init (&dh_params
);
185 return gnutls_dh_params_import_pkcs3 (dh_params
, &p3
, GNUTLS_X509_FMT_PEM
);
189 pskfunc (gnutls_session_t session
, const char *username
, gnutls_datum_t
* key
)
192 printf ("psk callback to get %s's password\n", username
);
193 key
->data
= gnutls_malloc (4);
202 int err
, listen_sd
, i
;
204 struct sockaddr_in sa_serv
;
205 struct sockaddr_in sa_cli
;
206 socklen_t client_len
;
208 gnutls_session_t session
;
209 char buffer
[MAX_BUF
+ 1];
216 success ("Launched, generating DH parameters...\n");
220 listen_sd
= socket (AF_INET
, SOCK_STREAM
, 0);
224 fail ("server: socket failed\n");
228 memset (&sa_serv
, '\0', sizeof (sa_serv
));
229 sa_serv
.sin_family
= AF_INET
;
230 sa_serv
.sin_addr
.s_addr
= INADDR_ANY
;
231 sa_serv
.sin_port
= htons (PORT
); /* Server Port number */
233 setsockopt (listen_sd
, SOL_SOCKET
, SO_REUSEADDR
, (void *) &optval
,
236 err
= bind (listen_sd
, (SA
*) & sa_serv
, sizeof (sa_serv
));
240 fail ("server: bind failed\n");
244 err
= listen (listen_sd
, 1024);
248 fail ("server: listen failed\n");
253 success ("server: ready. Listening to port '%d'.\n", PORT
);
259 /* this must be called once in the program
261 gnutls_global_init ();
263 gnutls_global_set_log_function (tls_log_func
);
265 gnutls_global_set_log_level (4711);
267 generate_dh_params ();
269 gnutls_psk_allocate_server_credentials (&server_pskcred
);
270 gnutls_psk_set_server_credentials_function (server_pskcred
, pskfunc
);
271 gnutls_psk_set_server_dh_params (server_pskcred
, dh_params
);
273 client_len
= sizeof (sa_cli
);
275 session
= initialize_tls_session ();
277 sd
= accept (listen_sd
, (SA
*) & sa_cli
, &client_len
);
280 success ("server: connection from %s, port %d\n",
281 inet_ntop (AF_INET
, &sa_cli
.sin_addr
, topbuf
,
282 sizeof (topbuf
)), ntohs (sa_cli
.sin_port
));
284 gnutls_transport_set_ptr (session
, (gnutls_transport_ptr_t
) sd
);
285 ret
= gnutls_handshake (session
);
289 gnutls_deinit (session
);
290 fail ("server: Handshake has failed (%s)\n\n", gnutls_strerror (ret
));
294 success ("server: Handshake was completed\n");
296 /* see the Getting peer's information example */
297 /* print_info(session); */
302 memset (buffer
, 0, MAX_BUF
+ 1);
303 ret
= gnutls_record_recv (session
, buffer
, MAX_BUF
);
308 success ("server: Peer has closed the GnuTLS connection\n");
313 fail ("server: Received corrupted data(%d). Closing...\n", ret
);
318 /* echo data back to the client
320 gnutls_record_send (session
, buffer
, strlen (buffer
));
323 /* do not wait for the peer to close the connection.
325 gnutls_bye (session
, GNUTLS_SHUT_WR
);
328 gnutls_deinit (session
);
332 gnutls_psk_free_server_credentials (server_pskcred
);
334 gnutls_dh_params_deinit (dh_params
);
336 gnutls_global_deinit ();
339 success ("server: finished\n");