2 * Copyright (C) 2004-2012 Free Software Foundation, Inc.
3 * Copyright (C) 2013 Adam Sampson <ats@offog.org>
5 * Author: Simon Josefsson
7 * This file is part of GnuTLS.
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.
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.
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
24 /* Parts copied from GnuTLS example programs. */
35 /* socketpair isn't supported on Win32. */
36 int main(int argc, char **argv)
44 #include <sys/types.h>
45 #include <sys/socket.h>
50 #include <gnutls/gnutls.h>
54 /* A very basic TLS client, with PSK authentication.
58 #define MSG "Hello TLS"
60 static void tls_log_func(int level, const char *str)
62 fprintf(stderr, "|<%d>| %s", level, str);
65 static void client(int sd)
68 gnutls_session_t session;
69 char buffer[MAX_BUF + 1];
70 gnutls_psk_client_credentials_t pskcred;
71 const gnutls_datum_t key = { (void *) "DEADBEEF", 8 };
75 gnutls_global_set_log_function(tls_log_func);
77 gnutls_global_set_log_level(5);
79 gnutls_psk_allocate_client_credentials(&pskcred);
80 gnutls_psk_set_client_credentials(pskcred, "test", &key,
83 /* Initialize TLS session
85 gnutls_init(&session, GNUTLS_CLIENT);
87 /* Use default priorities */
88 gnutls_priority_set_direct(session, "NORMAL:+DHE-PSK", NULL);
90 /* put the anonymous credentials to the current session
92 gnutls_credentials_set(session, GNUTLS_CRD_PSK, pskcred);
94 gnutls_transport_set_int(session, sd);
96 /* Perform the TLS handshake
98 ret = gnutls_handshake(session);
101 fail("client: Handshake failed\n");
106 success("client: Handshake was completed\n");
109 ret = gnutls_dh_get_prime_bits(session);
111 fail("server: too small prime size: %d\n", ret);
114 ret = gnutls_dh_get_secret_bits(session);
116 fail("server: too small secret key size: %d\n", ret);
119 gnutls_record_send(session, MSG, strlen(MSG));
121 ret = gnutls_record_recv(session, buffer, MAX_BUF);
125 ("client: Peer has closed the TLS connection\n");
127 } else if (ret < 0) {
128 fail("client: Error: %s\n", gnutls_strerror(ret));
133 printf("- Received %d bytes: ", ret);
134 for (ii = 0; ii < ret; ii++)
135 fputc(buffer[ii], stdout);
139 gnutls_bye(session, GNUTLS_SHUT_RDWR);
145 gnutls_deinit(session);
147 gnutls_psk_free_client_credentials(pskcred);
149 gnutls_global_deinit();
152 /* This is a sample TLS 1.0 echo server, for PSK authentication.
157 /* These are global */
158 gnutls_psk_server_credentials_t server_pskcred;
160 static gnutls_session_t initialize_tls_session(void)
162 gnutls_session_t session;
164 gnutls_init(&session, GNUTLS_SERVER);
166 /* avoid calling all the priority functions, since the defaults
169 gnutls_priority_set_direct(session, "NORMAL:+DHE-PSK", NULL);
171 gnutls_credentials_set(session, GNUTLS_CRD_PSK, server_pskcred);
176 static gnutls_dh_params_t dh_params;
178 static int generate_dh_params(void)
180 const gnutls_datum_t p3 = { (void *) pkcs3, strlen(pkcs3) };
181 /* Generate Diffie-Hellman parameters - for use with DHE
182 * kx algorithms. These should be discarded and regenerated
183 * once a day, once a week or once a month. Depending on the
184 * security requirements.
186 gnutls_dh_params_init(&dh_params);
187 return gnutls_dh_params_import_pkcs3(dh_params, &p3,
188 GNUTLS_X509_FMT_PEM);
192 pskfunc(gnutls_session_t session, const char *username,
193 gnutls_datum_t * key)
196 printf("psk callback to get %s's password\n", username);
197 key->data = gnutls_malloc(4);
208 gnutls_session_t session;
209 char buffer[MAX_BUF + 1];
212 static void server(int sd)
214 /* this must be called once in the program
218 gnutls_global_set_log_function(tls_log_func);
220 gnutls_global_set_log_level(4711);
222 generate_dh_params();
224 gnutls_psk_allocate_server_credentials(&server_pskcred);
225 gnutls_psk_set_server_credentials_function(server_pskcred,
227 gnutls_psk_set_server_dh_params(server_pskcred, dh_params);
229 session = initialize_tls_session();
231 gnutls_transport_set_int(session, sd);
232 ret = gnutls_handshake(session);
235 gnutls_deinit(session);
236 fail("server: Handshake has failed (%s)\n\n",
237 gnutls_strerror(ret));
241 success("server: Handshake was completed\n");
243 ret = gnutls_dh_get_prime_bits(session);
245 fail("server: too small prime size: %d\n", ret);
248 ret = gnutls_dh_get_secret_bits(session);
250 fail("server: too small secret key size: %d\n", ret);
253 /* see the Getting peer's information example */
254 /* print_info(session); */
257 memset(buffer, 0, MAX_BUF + 1);
258 ret = gnutls_record_recv(session, buffer, MAX_BUF);
263 ("server: Peer has closed the GnuTLS connection\n");
265 } else if (ret < 0) {
266 fail("server: Received corrupted data(%d). Closing...\n", ret);
268 } else if (ret > 0) {
269 /* echo data back to the client
271 gnutls_record_send(session, buffer,
275 /* do not wait for the peer to close the connection.
277 gnutls_bye(session, GNUTLS_SHUT_WR);
280 gnutls_deinit(session);
282 gnutls_psk_free_server_credentials(server_pskcred);
284 gnutls_dh_params_deinit(dh_params);
286 gnutls_global_deinit();
289 success("server: finished\n");
297 err = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
299 perror("socketpair");
300 fail("socketpair failed\n");