|
Packit Service |
4684c1 |
/*
|
|
Packit Service |
4684c1 |
* Copyright (C) 2012-2018 Free Software Foundation, Inc.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Author: Nikos Mavrogiannopoulos, Daiki Ueno
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This file is part of GnuTLS.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* GnuTLS is free software; you can redistribute it and/or modify it
|
|
Packit Service |
4684c1 |
* under the terms of the GNU General Public License as published by
|
|
Packit Service |
4684c1 |
* the Free Software Foundation; either version 3 of the License, or
|
|
Packit Service |
4684c1 |
* (at your option) any later version.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* GnuTLS is distributed in the hope that it will be useful, but
|
|
Packit Service |
4684c1 |
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
4684c1 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit Service |
4684c1 |
* General Public License for more details.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* You should have received a copy of the GNU Lesser General Public License
|
|
Packit Service |
4684c1 |
* along with this program. If not, see <https://www.gnu.org/licenses/>
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#ifdef HAVE_CONFIG_H
|
|
Packit Service |
4684c1 |
#include <config.h>
|
|
Packit Service |
4684c1 |
#endif
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#include <stdio.h>
|
|
Packit Service |
4684c1 |
#include <stdlib.h>
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#if defined(_WIN32)
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
int main(void)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
exit(77);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#else
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#include <string.h>
|
|
Packit Service |
4684c1 |
#include <sys/types.h>
|
|
Packit Service |
4684c1 |
#include <netinet/in.h>
|
|
Packit Service |
4684c1 |
#include <sys/socket.h>
|
|
Packit Service |
4684c1 |
#include <sys/wait.h>
|
|
Packit Service |
4684c1 |
#include <arpa/inet.h>
|
|
Packit Service |
4684c1 |
#include <unistd.h>
|
|
Packit Service |
4684c1 |
#include <gnutls/gnutls.h>
|
|
Packit Service |
4684c1 |
#include <gnutls/crypto.h>
|
|
Packit Service |
4684c1 |
#include <gnutls/dtls.h>
|
|
Packit Service |
4684c1 |
#include <signal.h>
|
|
Packit Service |
4684c1 |
#include <sys/wait.h>
|
|
Packit Service |
4684c1 |
#include <assert.h>
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#include "cert-common.h"
|
|
Packit Service |
4684c1 |
#include "utils.h"
|
|
Packit Service |
4684c1 |
#include "virt-time.h"
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* This program tests the robustness of record sending with padding.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static void server_log_func(int level, const char *str)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
fprintf(stderr, "server|<%d>| %s", level, str);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static void client_log_func(int level, const char *str)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
fprintf(stderr, "client|<%d>| %s", level, str);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#define SESSIONS 3
|
|
Packit Service |
4684c1 |
#define MAX_BUF 1024
|
|
Packit Service |
4684c1 |
#define MSG "Hello TLS"
|
|
Packit Service |
4684c1 |
#define EARLY_MSG "Hello TLS, it's early"
|
|
Packit Service |
4684c1 |
#define PRIORITY "NORMAL:-VERS-ALL:+VERS-TLS1.3"
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static const
|
|
Packit Service |
4684c1 |
gnutls_datum_t hrnd = {(void*)"\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 32};
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static int gnutls_rnd_works;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
int __attribute__ ((visibility ("protected")))
|
|
Packit Service |
4684c1 |
gnutls_rnd(gnutls_rnd_level_t level, void *data, size_t len)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
gnutls_rnd_works = 1;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
memset(data, 0xff, len);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* Flip the first byte to avoid infinite loop in the RSA
|
|
Packit Service |
4684c1 |
* blinding code of Nettle */
|
|
Packit Service |
4684c1 |
if (len > 0)
|
|
Packit Service |
4684c1 |
memset(data, 0x0, 1);
|
|
Packit Service |
4684c1 |
return 0;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_datum_t client_hello_msg = {NULL, 0};
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static int handshake_callback(gnutls_session_t session, unsigned int htype,
|
|
Packit Service |
4684c1 |
unsigned post, unsigned int incoming, const gnutls_datum_t *msg)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
assert(client_hello_msg.data == NULL);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
client_hello_msg.data = gnutls_malloc(msg->size+9);
|
|
Packit Service |
4684c1 |
assert(client_hello_msg.data != NULL);
|
|
Packit Service |
4684c1 |
client_hello_msg.size = msg->size+9;
|
|
Packit Service |
4684c1 |
memcpy(client_hello_msg.data+9, msg->data, msg->size);
|
|
Packit Service |
4684c1 |
/* reconstruct record header */
|
|
Packit Service |
4684c1 |
client_hello_msg.data[0] = 22;
|
|
Packit Service |
4684c1 |
client_hello_msg.data[1] = 3;
|
|
Packit Service |
4684c1 |
client_hello_msg.data[2] = 3;
|
|
Packit Service |
4684c1 |
client_hello_msg.data[3] = (msg->size+4) >> 8;
|
|
Packit Service |
4684c1 |
client_hello_msg.data[4] = (msg->size+4);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
client_hello_msg.data[5] = GNUTLS_HANDSHAKE_CLIENT_HELLO;
|
|
Packit Service |
4684c1 |
client_hello_msg.data[6] = 0;
|
|
Packit Service |
4684c1 |
client_hello_msg.data[7] = msg->size >> 8;
|
|
Packit Service |
4684c1 |
client_hello_msg.data[8] = msg->size;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return 0;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static void client(int sds[])
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
int ret;
|
|
Packit Service |
4684c1 |
char buffer[MAX_BUF + 1];
|
|
Packit Service |
4684c1 |
gnutls_certificate_credentials_t x509_cred;
|
|
Packit Service |
4684c1 |
gnutls_session_t session;
|
|
Packit Service |
4684c1 |
int t;
|
|
Packit Service |
4684c1 |
gnutls_datum_t session_data = {NULL, 0};
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (debug) {
|
|
Packit Service |
4684c1 |
gnutls_global_set_log_function(client_log_func);
|
|
Packit Service |
4684c1 |
gnutls_global_set_log_level(7);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* Generate the same ob_ticket_age value, which affects the
|
|
Packit Service |
4684c1 |
* binder calculation.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
virt_time_init();
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_certificate_allocate_credentials(&x509_cred);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
for (t = 0; t < SESSIONS-1; t++) {
|
|
Packit Service |
4684c1 |
int sd = sds[t];
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
assert(gnutls_init(&session, GNUTLS_CLIENT)>=0);
|
|
Packit Service |
4684c1 |
assert(gnutls_priority_set_direct(session, PRIORITY, NULL)>=0);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, x509_cred);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_transport_set_int(session, sd);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (t > 0) {
|
|
Packit Service |
4684c1 |
assert(gnutls_session_set_data(session, session_data.data, session_data.size) >= 0);
|
|
Packit Service |
4684c1 |
assert(gnutls_record_send_early_data(session, EARLY_MSG, sizeof(EARLY_MSG)) >= 0);
|
|
Packit Service |
4684c1 |
assert(gnutls_handshake_set_random(session, &hrnd) >= 0);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_handshake_set_hook_function(session, GNUTLS_HANDSHAKE_CLIENT_HELLO,
|
|
Packit Service |
4684c1 |
GNUTLS_HOOK_POST,
|
|
Packit Service |
4684c1 |
handshake_callback);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* Perform the TLS handshake
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
gnutls_handshake_set_timeout(session, 20 * 1000);
|
|
Packit Service |
4684c1 |
do {
|
|
Packit Service |
4684c1 |
ret = gnutls_handshake(session);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (ret < 0) {
|
|
Packit Service |
4684c1 |
fail("client: Handshake failed\n");
|
|
Packit Service |
4684c1 |
gnutls_perror(ret);
|
|
Packit Service |
4684c1 |
exit(1);
|
|
Packit Service |
4684c1 |
} else {
|
|
Packit Service |
4684c1 |
if (debug)
|
|
Packit Service |
4684c1 |
success("client: Handshake was completed\n");
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (t == 0) {
|
|
Packit Service |
4684c1 |
/* get the session data size */
|
|
Packit Service |
4684c1 |
ret =
|
|
Packit Service |
4684c1 |
gnutls_session_get_data2(session,
|
|
Packit Service |
4684c1 |
&session_data);
|
|
Packit Service |
4684c1 |
if (ret < 0)
|
|
Packit Service |
4684c1 |
fail("client: Getting resume data failed\n");
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (t > 0) {
|
|
Packit Service |
4684c1 |
if (!gnutls_session_is_resumed(session)) {
|
|
Packit Service |
4684c1 |
fail("client: session_is_resumed error (%d)\n", t);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_record_send(session, MSG, strlen(MSG));
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
do {
|
|
Packit Service |
4684c1 |
ret = gnutls_record_recv(session, buffer, sizeof(buffer));
|
|
Packit Service |
4684c1 |
} while (ret == GNUTLS_E_AGAIN);
|
|
Packit Service |
4684c1 |
if (ret == 0) {
|
|
Packit Service |
4684c1 |
if (debug)
|
|
Packit Service |
4684c1 |
success
|
|
Packit Service |
4684c1 |
("client: Peer has closed the TLS connection\n");
|
|
Packit Service |
4684c1 |
goto end;
|
|
Packit Service |
4684c1 |
} else if (ret < 0) {
|
|
Packit Service |
4684c1 |
fail("client: Error: %s\n", gnutls_strerror(ret));
|
|
Packit Service |
4684c1 |
goto end;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_bye(session, GNUTLS_SHUT_WR);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
close(sd);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_deinit(session);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
assert(client_hello_msg.data != NULL);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = send(sds[SESSIONS-1], client_hello_msg.data, client_hello_msg.size, 0);
|
|
Packit Service |
4684c1 |
assert(ret == (int)client_hello_msg.size);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
end:
|
|
Packit Service |
4684c1 |
gnutls_free(client_hello_msg.data);
|
|
Packit Service |
4684c1 |
gnutls_free(session_data.data);
|
|
Packit Service |
4684c1 |
gnutls_certificate_free_credentials(x509_cred);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static pid_t child;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#define MAX_CLIENT_HELLO_RECORDED 10
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
struct storage_st {
|
|
Packit Service |
4684c1 |
gnutls_datum_t entries[MAX_CLIENT_HELLO_RECORDED];
|
|
Packit Service |
4684c1 |
size_t num_entries;
|
|
Packit Service |
4684c1 |
};
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static int
|
|
Packit Service |
4684c1 |
storage_add(void *ptr, time_t expires, const gnutls_datum_t *key, const gnutls_datum_t *value)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
struct storage_st *storage = ptr;
|
|
Packit Service |
4684c1 |
gnutls_datum_t *datum;
|
|
Packit Service |
4684c1 |
size_t i;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
for (i = 0; i < storage->num_entries; i++) {
|
|
Packit Service |
4684c1 |
if (key->size == storage->entries[i].size &&
|
|
Packit Service |
4684c1 |
memcmp(storage->entries[i].data, key->data, key->size) == 0) {
|
|
Packit Service |
4684c1 |
return GNUTLS_E_DB_ENTRY_EXISTS;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* If the maximum number of ClientHello exceeded, reject early
|
|
Packit Service |
4684c1 |
* data until next time.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
if (storage->num_entries == MAX_CLIENT_HELLO_RECORDED)
|
|
Packit Service |
4684c1 |
return GNUTLS_E_DB_ERROR;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
datum = &storage->entries[storage->num_entries];
|
|
Packit Service |
4684c1 |
datum->data = gnutls_malloc(key->size);
|
|
Packit Service |
4684c1 |
if (!datum->data)
|
|
Packit Service |
4684c1 |
return GNUTLS_E_MEMORY_ERROR;
|
|
Packit Service |
4684c1 |
memcpy(datum->data, key->data, key->size);
|
|
Packit Service |
4684c1 |
datum->size = key->size;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
storage->num_entries++;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return 0;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static void
|
|
Packit Service |
4684c1 |
storage_clear(struct storage_st *storage)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
size_t i;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
for (i = 0; i < storage->num_entries; i++)
|
|
Packit Service |
4684c1 |
gnutls_free(storage->entries[i].data);
|
|
Packit Service |
4684c1 |
storage->num_entries = 0;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static void server(int sds[])
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
int ret;
|
|
Packit Service |
4684c1 |
char buffer[MAX_BUF + 1];
|
|
Packit Service |
4684c1 |
gnutls_session_t session;
|
|
Packit Service |
4684c1 |
gnutls_certificate_credentials_t x509_cred;
|
|
Packit Service |
4684c1 |
gnutls_datum_t session_ticket_key = { NULL, 0 };
|
|
Packit Service |
4684c1 |
struct storage_st storage;
|
|
Packit Service |
4684c1 |
gnutls_anti_replay_t anti_replay;
|
|
Packit Service |
4684c1 |
int t;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* this must be called once in the program
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
global_init();
|
|
Packit Service |
4684c1 |
memset(buffer, 0, sizeof(buffer));
|
|
Packit Service |
4684c1 |
memset(&storage, 0, sizeof(storage));
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (debug) {
|
|
Packit Service |
4684c1 |
gnutls_global_set_log_function(server_log_func);
|
|
Packit Service |
4684c1 |
gnutls_global_set_log_level(4711);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_certificate_allocate_credentials(&x509_cred);
|
|
Packit Service |
4684c1 |
gnutls_certificate_set_x509_key_mem(x509_cred, &server_cert,
|
|
Packit Service |
4684c1 |
&server_key,
|
|
Packit Service |
4684c1 |
GNUTLS_X509_FMT_PEM);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_session_ticket_key_generate(&session_ticket_key);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = gnutls_anti_replay_init(&anti_replay);
|
|
Packit Service |
4684c1 |
if (ret < 0)
|
|
Packit Service |
4684c1 |
fail("server: failed to initialize anti-replay\n");
|
|
Packit Service |
4684c1 |
gnutls_anti_replay_set_add_function(anti_replay, storage_add);
|
|
Packit Service |
4684c1 |
gnutls_anti_replay_set_ptr(anti_replay, &storage);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
for (t = 0; t < SESSIONS; t++) {
|
|
Packit Service |
4684c1 |
int sd = sds[t];
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
success("=== session %d ===\n", t);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
assert(gnutls_init(&session, GNUTLS_SERVER|GNUTLS_ENABLE_EARLY_DATA)>=0);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
assert(gnutls_priority_set_direct(session, PRIORITY, NULL)>=0);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, x509_cred);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_session_ticket_enable_server(session,
|
|
Packit Service |
4684c1 |
&session_ticket_key);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_anti_replay_enable(session, anti_replay);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_transport_set_int(session, sd);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
do {
|
|
Packit Service |
4684c1 |
ret = gnutls_handshake(session);
|
|
Packit Service |
4684c1 |
} while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (t == SESSIONS-1) {
|
|
Packit Service |
4684c1 |
/* duplicate data expected */
|
|
Packit Service |
4684c1 |
if (ret < 0 && !(gnutls_session_get_flags(session) & GNUTLS_SFLAGS_EARLY_DATA)) {
|
|
Packit Service |
4684c1 |
success("we detected the duplicate data!\n");
|
|
Packit Service |
4684c1 |
close(sd);
|
|
Packit Service |
4684c1 |
gnutls_deinit(session);
|
|
Packit Service |
4684c1 |
goto cleanup;
|
|
Packit Service |
4684c1 |
} else {
|
|
Packit Service |
4684c1 |
fail("server: duplicate early data was not detected (%d)\n", t);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (ret < 0) {
|
|
Packit Service |
4684c1 |
close(sd);
|
|
Packit Service |
4684c1 |
gnutls_deinit(session);
|
|
Packit Service |
4684c1 |
fail("server[%d]: Handshake has failed (%s)\n\n",
|
|
Packit Service |
4684c1 |
t, gnutls_strerror(ret));
|
|
Packit Service |
4684c1 |
return;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
if (debug)
|
|
Packit Service |
4684c1 |
success("server: Handshake was completed\n");
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (t > 0) {
|
|
Packit Service |
4684c1 |
if (!gnutls_session_is_resumed(session)) {
|
|
Packit Service |
4684c1 |
fail("server: session_is_resumed error (%d)\n", t);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* as we reuse the same ticket twice, expect
|
|
Packit Service |
4684c1 |
* early data only on the first resumption */
|
|
Packit Service |
4684c1 |
if (t == 1) {
|
|
Packit Service |
4684c1 |
if (gnutls_rnd_works) {
|
|
Packit Service |
4684c1 |
if (!(gnutls_session_get_flags(session) & GNUTLS_SFLAGS_EARLY_DATA)) {
|
|
Packit Service |
4684c1 |
fail("server: early data is not received (%d)\n", t);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
} else {
|
|
Packit Service |
4684c1 |
success("server: gnutls_rnd() could not be overridden, skip checking replay (%d)\n", t);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = gnutls_record_recv_early_data(session, buffer, sizeof(buffer));
|
|
Packit Service |
4684c1 |
if (ret < 0) {
|
|
Packit Service |
4684c1 |
fail("server: failed to retrieve early data: %s\n",
|
|
Packit Service |
4684c1 |
gnutls_strerror(ret));
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (ret != sizeof(EARLY_MSG) || memcmp(buffer, EARLY_MSG, ret))
|
|
Packit Service |
4684c1 |
fail("server: early data mismatch\n");
|
|
Packit Service |
4684c1 |
} else {
|
|
Packit Service |
4684c1 |
if (gnutls_rnd_works) {
|
|
Packit Service |
4684c1 |
if (gnutls_session_get_flags(session) & GNUTLS_SFLAGS_EARLY_DATA) {
|
|
Packit Service |
4684c1 |
fail("server: early data is not rejected (%d)\n", t);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
} else {
|
|
Packit Service |
4684c1 |
success("server: gnutls_rnd() could not be overridden, skip checking replay (%d)\n", t);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
for (;;) {
|
|
Packit Service |
4684c1 |
memset(buffer, 0, MAX_BUF + 1);
|
|
Packit Service |
4684c1 |
ret = gnutls_record_recv(session, buffer, MAX_BUF);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (ret == 0) {
|
|
Packit Service |
4684c1 |
if (debug)
|
|
Packit Service |
4684c1 |
success
|
|
Packit Service |
4684c1 |
("server: Peer has closed the GnuTLS connection\n");
|
|
Packit Service |
4684c1 |
break;
|
|
Packit Service |
4684c1 |
} else if (ret < 0) {
|
|
Packit Service |
4684c1 |
kill(child, SIGTERM);
|
|
Packit Service |
4684c1 |
fail("server: Received corrupted data(%d). Closing...\n", ret);
|
|
Packit Service |
4684c1 |
break;
|
|
Packit Service |
4684c1 |
} else if (ret > 0) {
|
|
Packit Service |
4684c1 |
/* echo data back to the client
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
gnutls_record_send(session, buffer,
|
|
Packit Service |
4684c1 |
strlen(buffer));
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
/* do not wait for the peer to close the connection.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
gnutls_bye(session, GNUTLS_SHUT_WR);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
close(sd);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_deinit(session);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
cleanup:
|
|
Packit Service |
4684c1 |
gnutls_anti_replay_deinit(anti_replay);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
storage_clear(&storage);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_free(session_ticket_key.data);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_certificate_free_credentials(x509_cred);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (debug)
|
|
Packit Service |
4684c1 |
success("server: finished\n");
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
void doit(void)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
int client_sds[SESSIONS], server_sds[SESSIONS];
|
|
Packit Service |
4684c1 |
int i, status = 0;
|
|
Packit Service |
4684c1 |
int ret;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
signal(SIGCHLD, SIG_IGN);
|
|
Packit Service |
4684c1 |
signal(SIGPIPE, SIG_IGN);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
for (i = 0; i < SESSIONS; i++) {
|
|
Packit Service |
4684c1 |
int sockets[2];
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
|
|
Packit Service |
4684c1 |
if (ret < 0) {
|
|
Packit Service |
4684c1 |
perror("socketpair");
|
|
Packit Service |
4684c1 |
exit(1);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
server_sds[i] = sockets[0];
|
|
Packit Service |
4684c1 |
client_sds[i] = sockets[1];
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
child = fork();
|
|
Packit Service |
4684c1 |
if (child < 0) {
|
|
Packit Service |
4684c1 |
perror("fork");
|
|
Packit Service |
4684c1 |
fail("fork");
|
|
Packit Service |
4684c1 |
exit(1);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (child) {
|
|
Packit Service |
4684c1 |
/* parent */
|
|
Packit Service |
4684c1 |
for (i = 0; i < SESSIONS; i++)
|
|
Packit Service |
4684c1 |
close(client_sds[i]);
|
|
Packit Service |
4684c1 |
server(server_sds);
|
|
Packit Service |
4684c1 |
wait(&status);
|
|
Packit Service |
4684c1 |
check_wait_status(status);
|
|
Packit Service |
4684c1 |
} else {
|
|
Packit Service |
4684c1 |
for (i = 0; i < SESSIONS; i++)
|
|
Packit Service |
4684c1 |
close(server_sds[i]);
|
|
Packit Service |
4684c1 |
client(client_sds);
|
|
Packit Service |
4684c1 |
exit(0);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#endif /* _WIN32 */
|