2 * Copyright (C) 2004-2016 Free Software Foundation, Inc.
3 * Copyright (C) 2013 Adam Sampson <ats@offog.org>
4 * Copyright (C) 2016 Red Hat, Inc.
6 * Author: Simon Josefsson, Nikos Mavrogiannopoulos
8 * This file is part of GnuTLS.
10 * GnuTLS is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
15 * GnuTLS is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with GnuTLS; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 /* Parts copied from GnuTLS example programs. */
36 /* socketpair isn't supported on Win32. */
37 int main(int argc, char **argv)
45 #include <sys/types.h>
46 #include <sys/socket.h>
51 #include <gnutls/gnutls.h>
55 static void wrap_db_init(void);
56 static void wrap_db_deinit(void);
57 static int wrap_db_store(void *dbf, gnutls_datum_t key,
59 static gnutls_datum_t wrap_db_fetch(void *dbf, gnutls_datum_t key);
60 static int wrap_db_delete(void *dbf, gnutls_datum_t key);
62 #define TLS_SESSION_CACHE 50
67 int enable_session_ticket_server;
68 int enable_session_ticket_client;
75 struct params_res resume_tests[] = {
76 {.desc = "try to resume from db",
78 .enable_session_ticket_server = 0,
79 .enable_session_ticket_client = 0,
81 {.desc = "try to resume from db and check ALPN",
83 .enable_session_ticket_server = 0,
84 .enable_session_ticket_client = 0,
87 {.desc = "try to resume from session ticket",
89 .enable_session_ticket_server = 1,
90 .enable_session_ticket_client = 1,
92 {.desc = "try to resume from session ticket (server only)",
94 .enable_session_ticket_server = 1,
95 .enable_session_ticket_client = 0,
97 {.desc = "try to resume from session ticket (client only)",
99 .enable_session_ticket_server = 0,
100 .enable_session_ticket_client = 1,
102 {.desc = "try to resume from db and ticket",
104 .enable_session_ticket_server = 1,
105 .enable_session_ticket_client = 1,
110 /* A very basic TLS client, with anonymous authentication.
114 #define MAX_BUF 5*1024
115 #define MSG "Hello TLS"
117 static void tls_log_func(int level, const char *str)
119 fprintf(stderr, "%s |<%d>| %s", child ? "server" : "client", level,
123 static void append_alpn(gnutls_session_t session, struct params_res *params, unsigned alpn_counter)
125 gnutls_datum_t protocol;
129 if (!params->try_alpn)
132 snprintf(str, sizeof(str), "myproto-%d", alpn_counter);
134 protocol.data = (void*)str;
135 protocol.size = strlen(str);
137 ret = gnutls_alpn_set_protocols(session, &protocol, 1, 0);
144 static void verify_alpn(gnutls_session_t session, struct params_res *params, unsigned alpn_counter)
147 gnutls_datum_t selected;
150 if (!params->try_alpn)
153 snprintf(str, sizeof(str), "myproto-%d", alpn_counter);
155 ret = gnutls_alpn_get_selected_protocol(session, &selected);
161 if (strlen(str) != selected.size || memcmp(str, selected.data, selected.size) != 0) {
162 fail("expected protocol %s, got %.*s\n", str, selected.size, selected.data);
167 success("ALPN got: %s\n", str);
170 static void client(int sds[], struct params_res *params)
173 gnutls_session_t session;
174 char buffer[MAX_BUF + 1];
175 gnutls_anon_client_credentials_t anoncred;
176 /* Need to enable anonymous KX specifically. */
178 /* variables used in session resuming
181 gnutls_datum_t session_data;
184 gnutls_global_set_log_function(tls_log_func);
185 gnutls_global_set_log_level(2);
189 gnutls_anon_allocate_client_credentials(&anoncred);
191 for (t = 0; t < SESSIONS; t++) {
194 /* Initialize TLS session
196 gnutls_init(&session,
197 GNUTLS_CLIENT | GNUTLS_NO_EXTENSIONS);
199 /* Use default priorities */
200 gnutls_priority_set_direct(session,
201 "NONE:+VERS-TLS-ALL:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-DH",
204 append_alpn(session, params, t);
206 /* put the anonymous credentials to the current session
208 gnutls_credentials_set(session, GNUTLS_CRD_ANON, anoncred);
210 if (params->enable_session_ticket_client)
211 gnutls_session_ticket_enable_client(session);
214 /* if this is not the first time we connect */
215 gnutls_session_set_data(session, session_data.data,
219 gnutls_transport_set_int(session, sd);
221 /* Perform the TLS handshake
223 gnutls_handshake_set_timeout(session, 20 * 1000);
225 ret = gnutls_handshake(session);
226 } while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
229 fail("client: Handshake failed\n");
235 ("client: Handshake was completed\n");
238 if (t == 0) { /* the first time we connect */
239 /* get the session data size */
241 gnutls_session_get_data2(session,
244 fail("Getting resume data failed\n");
246 } else { /* the second time we connect */
248 /* check if we actually resumed the previous session */
249 if (gnutls_session_is_resumed(session) != 0) {
250 if (params->expect_resume) {
253 ("- Previous session was resumed\n");
255 fail("- Previous session was resumed\n");
257 if (params->expect_resume) {
258 fail("*** Previous session was NOT resumed\n");
262 ("*** Previous session was NOT resumed (expected)\n");
267 verify_alpn(session, params, t);
269 gnutls_record_send(session, MSG, strlen(MSG));
271 ret = gnutls_record_recv(session, buffer, MAX_BUF);
275 ("client: Peer has closed the TLS connection\n");
277 } else if (ret < 0) {
278 fail("client: Error: %s\n", gnutls_strerror(ret));
283 printf("- Received %d bytes: ", ret);
284 for (ii = 0; ii < ret; ii++) {
285 fputc(buffer[ii], stdout);
290 gnutls_bye(session, GNUTLS_SHUT_RDWR);
294 gnutls_deinit(session);
296 gnutls_free(session_data.data);
299 gnutls_anon_free_client_credentials(anoncred);
302 /* This is a sample TLS 1.0 echo server, for anonymous authentication only.
307 /* These are global */
308 static gnutls_datum_t session_ticket_key = { NULL, 0 };
310 static gnutls_session_t initialize_tls_session(struct params_res *params)
312 gnutls_session_t session;
314 gnutls_init(&session, GNUTLS_SERVER);
316 /* avoid calling all the priority functions, since the defaults
319 gnutls_priority_set_direct(session,
320 "NONE:+VERS-TLS-ALL:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-DH",
324 gnutls_dh_set_prime_bits(session, DH_BITS);
326 if (params->enable_db) {
327 gnutls_db_set_retrieve_function(session, wrap_db_fetch);
328 gnutls_db_set_remove_function(session, wrap_db_delete);
329 gnutls_db_set_store_function(session, wrap_db_store);
330 gnutls_db_set_ptr(session, NULL);
333 if (params->enable_session_ticket_server)
334 gnutls_session_ticket_enable_server(session,
335 &session_ticket_key);
340 static gnutls_dh_params_t dh_params;
341 gnutls_anon_server_credentials_t anoncred;
343 static int generate_dh_params(void)
345 const gnutls_datum_t p3 = { (void *) pkcs3, strlen(pkcs3) };
346 /* Generate Diffie-Hellman parameters - for use with DHE
347 * kx algorithms. These should be discarded and regenerated
348 * once a day, once a week or once a month. Depending on the
349 * security requirements.
351 gnutls_dh_params_init(&dh_params);
352 return gnutls_dh_params_import_pkcs3(dh_params, &p3,
353 GNUTLS_X509_FMT_PEM);
357 static void global_stop(void)
360 success("global stop\n");
362 gnutls_anon_free_server_credentials(anoncred);
364 gnutls_dh_params_deinit(dh_params);
366 gnutls_global_deinit();
369 static void server(int sds[], struct params_res *params)
373 gnutls_session_t session;
374 char buffer[MAX_BUF + 1];
376 /* this must be called once in the program, it is mostly for the server.
379 gnutls_global_set_log_function(tls_log_func);
380 gnutls_global_set_log_level(2);
384 gnutls_anon_allocate_server_credentials(&anoncred);
387 success("Launched, generating DH parameters...\n");
389 generate_dh_params();
391 gnutls_anon_set_server_dh_params(anoncred, dh_params);
393 if (params->enable_db) {
397 if (params->enable_session_ticket_server)
398 gnutls_session_ticket_key_generate(&session_ticket_key);
400 for (t = 0; t < SESSIONS; t++) {
403 session = initialize_tls_session(params);
405 append_alpn(session, params, t);
407 gnutls_credentials_set(session, GNUTLS_CRD_ANON, anoncred);
408 gnutls_transport_set_int(session, sd);
409 gnutls_handshake_set_timeout(session, 20 * 1000);
411 ret = gnutls_handshake(session);
412 } while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
415 gnutls_deinit(session);
416 kill(child, SIGTERM);
417 fail("server: Handshake has failed (%s)\n\n",
418 gnutls_strerror(ret));
422 success("server: Handshake was completed\n");
424 verify_alpn(session, params, t);
426 /* see the Getting peer's information example */
427 /* print_info(session); */
430 memset(buffer, 0, MAX_BUF + 1);
431 ret = gnutls_record_recv(session, buffer, MAX_BUF);
436 ("server: Peer has closed the GnuTLS connection\n");
438 } else if (ret < 0) {
439 kill(child, SIGTERM);
440 fail("server: Received corrupted data(%d). Closing...\n", ret);
442 } else if (ret > 0) {
443 /* echo data back to the client
445 gnutls_record_send(session, buffer,
449 /* do not wait for the peer to close the connection.
451 gnutls_bye(session, GNUTLS_SHUT_WR);
455 gnutls_deinit(session);
458 if (params->enable_db) {
462 gnutls_free(session_ticket_key.data);
463 session_ticket_key.data = NULL;
466 success("server: finished\n");
473 for (i = 0; resume_tests[i].desc; i++) {
474 int client_sds[SESSIONS], server_sds[SESSIONS];
477 printf("%s\n", resume_tests[i].desc);
479 for (j = 0; j < SESSIONS; j++) {
482 err = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
484 perror("socketpair");
485 fail("socketpair failed\n");
489 server_sds[j] = sockets[0];
490 client_sds[j] = sockets[1];
503 server(server_sds, &resume_tests[i]);
505 if (WEXITSTATUS(status) > 0)
509 client(client_sds, &resume_tests[i]);
510 gnutls_global_deinit();
518 /* Functions and other stuff needed for session resuming.
519 * This is done using a very simple list which holds session ids
523 #define MAX_SESSION_ID_SIZE 32
524 #define MAX_SESSION_DATA_SIZE 1024
527 unsigned char session_id[MAX_SESSION_ID_SIZE];
528 unsigned int session_id_size;
530 char session_data[MAX_SESSION_DATA_SIZE];
531 int session_data_size;
534 static CACHE *cache_db;
535 static int cache_db_ptr = 0;
537 static void wrap_db_init(void)
540 /* allocate cache_db */
541 cache_db = calloc(1, TLS_SESSION_CACHE * sizeof(CACHE));
544 static void wrap_db_deinit(void)
552 wrap_db_store(void *dbf, gnutls_datum_t key, gnutls_datum_t data)
554 time_t t, now = time(0);
558 fprintf(stderr, "resume db storing (%d-%d): ", key.size,
560 for (i = 0; i < key.size; i++) {
561 fprintf(stderr, "%02x", key.data[i] & 0xFF);
563 fprintf(stderr, "\n");
564 fprintf(stderr, "data: ");
565 for (i = 0; i < data.size; i++) {
566 fprintf(stderr, "%02x", data.data[i] & 0xFF);
568 fprintf(stderr, "\n");
571 /* check the correctness of gnutls_db_check_entry_time() */
572 t = gnutls_db_check_entry_time(&data);
573 if (t < now - 10 || t > now + 10) {
574 fail("Time returned by gnutls_db_check_entry_time is bogus\n");
578 if (cache_db == NULL)
581 if (key.size > MAX_SESSION_ID_SIZE) {
582 fail("Key size is too large\n");
586 if (data.size > MAX_SESSION_DATA_SIZE) {
587 fail("Data size is too large\n");
591 memcpy(cache_db[cache_db_ptr].session_id, key.data, key.size);
592 cache_db[cache_db_ptr].session_id_size = key.size;
594 memcpy(cache_db[cache_db_ptr].session_data, data.data, data.size);
595 cache_db[cache_db_ptr].session_data_size = data.size;
598 cache_db_ptr %= TLS_SESSION_CACHE;
603 static gnutls_datum_t wrap_db_fetch(void *dbf, gnutls_datum_t key)
605 gnutls_datum_t res = { NULL, 0 };
611 fprintf(stderr, "resume db looking for (%d): ", key.size);
612 for (i = 0; i < key.size; i++) {
613 fprintf(stderr, "%02x", key.data[i] & 0xFF);
615 fprintf(stderr, "\n");
618 if (cache_db == NULL)
621 for (i = 0; i < TLS_SESSION_CACHE; i++) {
622 if (key.size == cache_db[i].session_id_size &&
623 memcmp(key.data, cache_db[i].session_id,
627 ("resume db fetch... return info\n");
629 res.size = cache_db[i].session_data_size;
631 res.data = gnutls_malloc(res.size);
632 if (res.data == NULL)
635 memcpy(res.data, cache_db[i].session_data,
641 for (i = 0; i < res.size; i++) {
644 if ((i + 1) % 16 == 0)
655 success("resume db fetch... NOT FOUND\n");
659 static int wrap_db_delete(void *dbf, gnutls_datum_t key)
663 if (cache_db == NULL)
666 for (i = 0; i < TLS_SESSION_CACHE; i++) {
667 if (key.size == cache_db[i].session_id_size &&
668 memcmp(key.data, cache_db[i].session_id,
671 cache_db[i].session_id_size = 0;
672 cache_db[i].session_data_size = 0;