|
Packit |
aea12f |
/*
|
|
Packit |
aea12f |
* Copyright (C) 2015-2019 Red Hat, Inc.
|
|
Packit |
aea12f |
*
|
|
Packit |
aea12f |
* This file is part of GnuTLS.
|
|
Packit |
aea12f |
*
|
|
Packit |
aea12f |
* GnuTLS is free software; you can redistribute it and/or modify it
|
|
Packit |
aea12f |
* under the terms of the GNU General Public License as published by
|
|
Packit |
aea12f |
* the Free Software Foundation; either version 3 of the License, or
|
|
Packit |
aea12f |
* (at your option) any later version.
|
|
Packit |
aea12f |
*
|
|
Packit |
aea12f |
* GnuTLS is distributed in the hope that it will be useful, but
|
|
Packit |
aea12f |
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
aea12f |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
aea12f |
* General Public License for more details.
|
|
Packit |
aea12f |
*
|
|
Packit |
aea12f |
* You should have received a copy of the GNU Lesser General Public License
|
|
Packit |
aea12f |
* along with this program. If not, see <https://www.gnu.org/licenses/>
|
|
Packit |
aea12f |
*/
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#ifdef HAVE_CONFIG_H
|
|
Packit |
aea12f |
#include <config.h>
|
|
Packit |
aea12f |
#endif
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#include <assert.h>
|
|
Packit |
aea12f |
#include <stdio.h>
|
|
Packit |
aea12f |
#include <stdlib.h>
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#if !defined(__linux__) || !defined(__GNUC__)
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
int main(int argc, char **argv)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
exit(77);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#else
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#include <string.h>
|
|
Packit |
aea12f |
#include <sys/types.h>
|
|
Packit |
aea12f |
#include <netinet/in.h>
|
|
Packit |
aea12f |
#include <sys/socket.h>
|
|
Packit |
aea12f |
#include <sys/wait.h>
|
|
Packit |
aea12f |
#include <arpa/inet.h>
|
|
Packit |
aea12f |
#include <unistd.h>
|
|
Packit |
aea12f |
#include <gnutls/gnutls.h>
|
|
Packit |
aea12f |
#include <gnutls/crypto.h>
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#include "cert-common.h"
|
|
Packit |
aea12f |
#include "utils.h"
|
|
Packit |
aea12f |
#include "virt-time.h"
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void terminate(void);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#define SESSIONS 2
|
|
Packit |
aea12f |
#define MAX_BUF 5*1024
|
|
Packit |
aea12f |
#define MSG "Hello TLS"
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
extern unsigned int _gnutls_global_version;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* This program tests whether the gnutls_prf() works as
|
|
Packit |
aea12f |
* expected.
|
|
Packit |
aea12f |
*/
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void server_log_func(int level, const char *str)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
fprintf(stderr, "server|<%d>| %s", level, str);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void client_log_func(int level, const char *str)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
fprintf(stderr, "client|<%d>| %s", level, str);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* These are global */
|
|
Packit |
aea12f |
static pid_t child;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static const
|
|
Packit |
aea12f |
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 |
aea12f |
static const
|
|
Packit |
aea12f |
gnutls_datum_t hsrnd = {(void*)"\x00\x05\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 |
aea12f |
|
|
Packit |
aea12f |
static int gnutls_rnd_works;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
int __attribute__ ((visibility ("protected")))
|
|
Packit |
aea12f |
gnutls_rnd(gnutls_rnd_level_t level, void *data, size_t len)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
gnutls_rnd_works = 1;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
memset(data, 0xff, len);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* Flip the first byte to avoid infinite loop in the RSA
|
|
Packit |
aea12f |
* blinding code of Nettle */
|
|
Packit |
aea12f |
if (len > 0)
|
|
Packit |
aea12f |
memset(data, 0x0, 1);
|
|
Packit |
aea12f |
return 0;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static gnutls_datum_t session_ticket_key = { NULL, 0 };
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void dump(const char *name, const uint8_t *data, unsigned data_size)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
unsigned i;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
fprintf(stderr, "%s", name);
|
|
Packit |
aea12f |
for (i=0;i
|
|
Packit |
aea12f |
fprintf(stderr, "\\x%.2x", (unsigned)data[i]);
|
|
Packit |
aea12f |
fprintf(stderr, "\n");
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#define TRY(label_size, label, extra_size, extra, size, exp) \
|
|
Packit |
aea12f |
{ \
|
|
Packit |
aea12f |
ret = gnutls_prf_early(session, label_size, label, extra_size, extra, size, \
|
|
Packit |
aea12f |
(void*)key_material); \
|
|
Packit |
aea12f |
if (ret < 0) { \
|
|
Packit |
aea12f |
fprintf(stderr, "gnutls_prf_early: error in %d\n", __LINE__); \
|
|
Packit |
aea12f |
gnutls_perror(ret); \
|
|
Packit |
aea12f |
exit(1); \
|
|
Packit |
aea12f |
} \
|
|
Packit |
aea12f |
if (memcmp(key_material, exp, size) != 0) { \
|
|
Packit |
aea12f |
fprintf(stderr, "gnutls_prf_early: output doesn't match for '%s'\n", label); \
|
|
Packit |
aea12f |
dump("got ", key_material, size); \
|
|
Packit |
aea12f |
dump("expected ", exp, size); \
|
|
Packit |
aea12f |
exit(1); \
|
|
Packit |
aea12f |
} \
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit Service |
991b93 |
#define KEY_EXP_VALUE "\xc1\x6b\x6c\xb9\x88\x33\xd5\x28\x80\xec\x27\x87\xa2\x6f\x4b\xd0\x01\x5e\x7f\xca\xd7\xd4\x8a\x3f\xe2\x48\x92\xef\x02\x14\xfb\x81\x90\x04"
|
|
Packit Service |
991b93 |
#define HELLO_VALUE "\x2a\x73\xd9\x74\x04\x4e\x0a\x5f\x41\x8a\x09\xcb\x45\x33\x1a\xec\xd3\xfc\xdc\x1b\x2c\x67\x26\xe4\x9c\xfe\x1f\xa5\x74\xf1\x4f"
|
|
Packit Service |
991b93 |
#define CONTEXT_VALUE "\x87\xf6\x88\xe3\xd7\xf2\x05\xbc\xa4\x10\xa3\x48\x9f\xf5\xcf\x97\x06\x22\x4e\xfd\x18\x32\x52\x1d\xbd\x26\xf5\x5b\x21\x20\xec"
|
|
Packit Service |
991b93 |
#define NULL_CONTEXT_VALUE "\xf9\xca\xfe\x45\x44\x96\xdb\xc5\x41\x8f\x7e\x8e\xd7\xb0\x7d\x19\x45\xaf\x09\xbc\x1e\x82\x94\xac\x55\xe5\xb9\xb4\x3b\xe8\xc0"
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static int handshake_callback_called;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static int handshake_callback(gnutls_session_t session, unsigned int htype,
|
|
Packit |
aea12f |
unsigned post, unsigned int incoming, const gnutls_datum_t *msg)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
unsigned char key_material[512];
|
|
Packit |
aea12f |
int ret;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
assert(post == GNUTLS_HOOK_POST);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
handshake_callback_called++;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
TRY(13, "key expansion", 0, NULL, 34, (uint8_t*)KEY_EXP_VALUE);
|
|
Packit |
aea12f |
TRY(6, "hello", 0, NULL, 31, (uint8_t*)HELLO_VALUE);
|
|
Packit |
aea12f |
TRY(7, "context", 5, "abcd\xfa", 31, (uint8_t*)CONTEXT_VALUE);
|
|
Packit |
aea12f |
TRY(12, "null-context", 0, "", 31, (uint8_t*)NULL_CONTEXT_VALUE);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
return 0;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void client(int sds[])
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
gnutls_session_t session;
|
|
Packit |
aea12f |
int ret, ii;
|
|
Packit |
aea12f |
gnutls_certificate_credentials_t clientx509cred;
|
|
Packit |
aea12f |
const char *err;
|
|
Packit |
aea12f |
int t;
|
|
Packit |
aea12f |
gnutls_datum_t session_data = {NULL, 0};
|
|
Packit |
aea12f |
char buffer[MAX_BUF + 1];
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
global_init();
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
virt_time_init();
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (debug) {
|
|
Packit |
aea12f |
gnutls_global_set_log_function(client_log_func);
|
|
Packit |
aea12f |
gnutls_global_set_log_level(4711);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_certificate_allocate_credentials(&clientx509cred);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
for (t = 0; t < SESSIONS; t++) {
|
|
Packit |
aea12f |
/* Initialize TLS session
|
|
Packit |
aea12f |
*/
|
|
Packit |
aea12f |
gnutls_init(&session, GNUTLS_CLIENT);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* Use default priorities */
|
|
Packit |
aea12f |
ret = gnutls_priority_set_direct(session,
|
|
Packit |
aea12f |
"NONE:+VERS-TLS1.3:+AES-256-GCM:+AEAD:+SIGN-RSA-PSS-RSAE-SHA384:+GROUP-SECP256R1",
|
|
Packit |
aea12f |
&err;;
|
|
Packit |
aea12f |
if (ret < 0) {
|
|
Packit |
aea12f |
fail("client: priority set failed (%s): %s\n",
|
|
Packit |
aea12f |
gnutls_strerror(ret), err);
|
|
Packit |
aea12f |
exit(1);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ret = gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE,
|
|
Packit |
aea12f |
clientx509cred);
|
|
Packit |
aea12f |
if (ret < 0)
|
|
Packit |
aea12f |
exit(1);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_handshake_set_random(session, &hrnd);
|
|
Packit |
aea12f |
gnutls_transport_set_int(session, sds[t]);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (t > 0) {
|
|
Packit |
aea12f |
gnutls_session_set_data(session, session_data.data,
|
|
Packit |
aea12f |
session_data.size);
|
|
Packit |
aea12f |
gnutls_handshake_set_hook_function(session, GNUTLS_HANDSHAKE_CLIENT_HELLO,
|
|
Packit |
aea12f |
GNUTLS_HOOK_POST,
|
|
Packit |
aea12f |
handshake_callback);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* Perform the TLS handshake
|
|
Packit |
aea12f |
*/
|
|
Packit |
aea12f |
do {
|
|
Packit |
aea12f |
ret = gnutls_handshake(session);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (ret < 0) {
|
|
Packit |
aea12f |
fail("client: Handshake failed: %s\n", strerror(ret));
|
|
Packit |
aea12f |
exit(1);
|
|
Packit |
aea12f |
} else {
|
|
Packit |
aea12f |
if (debug)
|
|
Packit |
aea12f |
success("client: Handshake was completed\n");
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (debug)
|
|
Packit |
aea12f |
success("client: TLS version is: %s\n",
|
|
Packit |
aea12f |
gnutls_protocol_get_name
|
|
Packit |
aea12f |
(gnutls_protocol_get_version(session)));
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ret = gnutls_cipher_get(session);
|
|
Packit |
aea12f |
if (ret != GNUTLS_CIPHER_AES_256_GCM) {
|
|
Packit |
aea12f |
fprintf(stderr, "negotiated unexpected cipher: %s\n", gnutls_cipher_get_name(ret));
|
|
Packit |
aea12f |
exit(1);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ret = gnutls_mac_get(session);
|
|
Packit |
aea12f |
if (ret != GNUTLS_MAC_AEAD) {
|
|
Packit |
aea12f |
fprintf(stderr, "negotiated unexpected mac: %s\n", gnutls_mac_get_name(ret));
|
|
Packit |
aea12f |
exit(1);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (t == 0) {
|
|
Packit |
aea12f |
/* get the session data size */
|
|
Packit |
aea12f |
ret =
|
|
Packit |
aea12f |
gnutls_session_get_data2(session,
|
|
Packit |
aea12f |
&session_data);
|
|
Packit |
aea12f |
if (ret < 0)
|
|
Packit |
aea12f |
fail("Getting resume data failed\n");
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (handshake_callback_called != 0)
|
|
Packit |
aea12f |
fail("client: handshake callback is called\n");
|
|
Packit |
aea12f |
} else {
|
|
Packit |
aea12f |
if (handshake_callback_called != t)
|
|
Packit |
aea12f |
fail("client: handshake callback is not called\n");
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_record_send(session, MSG, strlen(MSG));
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
do {
|
|
Packit |
aea12f |
ret = gnutls_record_recv(session, buffer, MAX_BUF);
|
|
Packit |
aea12f |
} while (ret == GNUTLS_E_AGAIN);
|
|
Packit |
aea12f |
if (ret == 0) {
|
|
Packit |
aea12f |
if (debug)
|
|
Packit |
aea12f |
success
|
|
Packit |
aea12f |
("client: Peer has closed the TLS connection\n");
|
|
Packit |
aea12f |
} else if (ret < 0) {
|
|
Packit |
aea12f |
fail("client: Error: %s\n", gnutls_strerror(ret));
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (debug) {
|
|
Packit |
aea12f |
printf("- Received %d bytes: ", ret);
|
|
Packit |
aea12f |
for (ii = 0; ii < ret; ii++) {
|
|
Packit |
aea12f |
fputc(buffer[ii], stdout);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
fputs("\n", stdout);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_bye(session, GNUTLS_SHUT_WR);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
close(sds[t]);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_deinit(session);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_free(session_data.data);
|
|
Packit |
aea12f |
gnutls_certificate_free_credentials(clientx509cred);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_global_deinit();
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void terminate(void)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
int status = 0;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
kill(child, SIGTERM);
|
|
Packit |
aea12f |
wait(&status);
|
|
Packit |
aea12f |
exit(1);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void server(int sds[])
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
int ret;
|
|
Packit |
aea12f |
gnutls_session_t session;
|
|
Packit |
aea12f |
gnutls_certificate_credentials_t serverx509cred;
|
|
Packit |
aea12f |
int t;
|
|
Packit |
aea12f |
char buffer[MAX_BUF + 1];
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* this must be called once in the program
|
|
Packit |
aea12f |
*/
|
|
Packit |
aea12f |
global_init();
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
virt_time_init();
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (debug) {
|
|
Packit |
aea12f |
gnutls_global_set_log_function(server_log_func);
|
|
Packit |
aea12f |
gnutls_global_set_log_level(4711);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_certificate_allocate_credentials(&serverx509cred);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_session_ticket_key_generate(&session_ticket_key);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
for (t = 0; t < SESSIONS; t++) {
|
|
Packit |
aea12f |
gnutls_init(&session, GNUTLS_SERVER);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_session_ticket_enable_server(session,
|
|
Packit |
aea12f |
&session_ticket_key);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* avoid calling all the priority functions, since the defaults
|
|
Packit |
aea12f |
* are adequate.
|
|
Packit |
aea12f |
*/
|
|
Packit |
aea12f |
ret = gnutls_priority_set_direct(session,
|
|
Packit |
aea12f |
"NORMAL:-VERS-ALL:+VERS-TLS1.3:-KX-ALL:-SIGN-ALL:+SIGN-RSA-PSS-RSAE-SHA384:-GROUP-ALL:+GROUP-SECP256R1", NULL);
|
|
Packit |
aea12f |
if (ret < 0) {
|
|
Packit |
aea12f |
fail("server: priority set failed (%s)\n\n",
|
|
Packit |
aea12f |
gnutls_strerror(ret));
|
|
Packit |
aea12f |
terminate();
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_certificate_set_x509_key_mem(serverx509cred,
|
|
Packit |
aea12f |
&server_cert, &server_key,
|
|
Packit |
aea12f |
GNUTLS_X509_FMT_PEM);
|
|
Packit |
aea12f |
gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE,
|
|
Packit |
aea12f |
serverx509cred);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_handshake_set_random(session, &hsrnd);
|
|
Packit |
aea12f |
gnutls_transport_set_int(session, sds[t]);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (t > 0) {
|
|
Packit |
aea12f |
if (!gnutls_rnd_works) {
|
|
Packit |
aea12f |
fprintf(stderr, "gnutls_rnd() could not be overridden, skipping prf checks see #584\n");
|
|
Packit |
aea12f |
exit(77);
|
|
Packit |
aea12f |
} else {
|
|
Packit |
aea12f |
gnutls_handshake_set_hook_function(session, GNUTLS_HANDSHAKE_CLIENT_HELLO,
|
|
Packit |
aea12f |
GNUTLS_HOOK_POST,
|
|
Packit |
aea12f |
handshake_callback);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
do {
|
|
Packit |
aea12f |
ret = gnutls_handshake(session);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
|
|
Packit |
aea12f |
if (ret < 0) {
|
|
Packit |
aea12f |
close(sds[t]);
|
|
Packit |
aea12f |
gnutls_deinit(session);
|
|
Packit |
aea12f |
fail("server: Handshake has failed (%s)\n\n",
|
|
Packit |
aea12f |
gnutls_strerror(ret));
|
|
Packit |
aea12f |
terminate();
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
if (debug)
|
|
Packit |
aea12f |
success("server: Handshake was completed\n");
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (debug)
|
|
Packit |
aea12f |
success("server: TLS version is: %s\n",
|
|
Packit |
aea12f |
gnutls_protocol_get_name
|
|
Packit |
aea12f |
(gnutls_protocol_get_version(session)));
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (t == 0) {
|
|
Packit |
aea12f |
if (handshake_callback_called != 0)
|
|
Packit |
aea12f |
fail("server: handshake callback is called\n");
|
|
Packit |
aea12f |
} else {
|
|
Packit |
aea12f |
if (handshake_callback_called != t)
|
|
Packit |
aea12f |
fail("server: handshake callback is not called\n");
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
for (;;) {
|
|
Packit |
aea12f |
memset(buffer, 0, MAX_BUF + 1);
|
|
Packit |
aea12f |
ret = gnutls_record_recv(session, buffer, MAX_BUF);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (ret == 0) {
|
|
Packit |
aea12f |
if (debug)
|
|
Packit |
aea12f |
success
|
|
Packit |
aea12f |
("server: Peer has closed the GnuTLS connection\n");
|
|
Packit |
aea12f |
break;
|
|
Packit |
aea12f |
} else if (ret < 0) {
|
|
Packit |
aea12f |
kill(child, SIGTERM);
|
|
Packit |
aea12f |
fail("server: Received corrupted data(%d). Closing...\n", ret);
|
|
Packit |
aea12f |
break;
|
|
Packit |
aea12f |
} else if (ret > 0) {
|
|
Packit |
aea12f |
/* echo data back to the client
|
|
Packit |
aea12f |
*/
|
|
Packit |
aea12f |
gnutls_record_send(session, buffer,
|
|
Packit |
aea12f |
strlen(buffer));
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* do not wait for the peer to close the connection.
|
|
Packit |
aea12f |
*/
|
|
Packit |
aea12f |
gnutls_bye(session, GNUTLS_SHUT_WR);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
close(sds[t]);
|
|
Packit |
aea12f |
gnutls_deinit(session);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_certificate_free_credentials(serverx509cred);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_free(session_ticket_key.data);
|
|
Packit |
aea12f |
session_ticket_key.data = NULL;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_global_deinit();
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (debug)
|
|
Packit |
aea12f |
success("server: finished\n");
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
void doit(void)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
int client_sds[SESSIONS], server_sds[SESSIONS];
|
|
Packit |
aea12f |
int i;
|
|
Packit |
aea12f |
int ret;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
_gnutls_global_version = 0x030607;
|
|
Packit |
aea12f |
signal(SIGPIPE, SIG_IGN);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
for (i = 0; i < SESSIONS; i++) {
|
|
Packit |
aea12f |
int sockets[2];
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ret = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
|
|
Packit |
aea12f |
if (ret == -1) {
|
|
Packit |
aea12f |
perror("socketpair");
|
|
Packit |
aea12f |
fail("socketpair failed\n");
|
|
Packit |
aea12f |
return;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
server_sds[i] = sockets[0];
|
|
Packit |
aea12f |
client_sds[i] = sockets[1];
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
child = fork();
|
|
Packit |
aea12f |
if (child < 0) {
|
|
Packit |
aea12f |
perror("fork");
|
|
Packit |
aea12f |
fail("fork");
|
|
Packit |
aea12f |
exit(1);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (child) {
|
|
Packit |
aea12f |
int status = 0;
|
|
Packit |
aea12f |
/* parent */
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
for (i = 0; i < SESSIONS; i++)
|
|
Packit |
aea12f |
close(client_sds[i]);
|
|
Packit |
aea12f |
server(server_sds);
|
|
Packit |
aea12f |
wait(&status);
|
|
Packit |
aea12f |
check_wait_status(status);
|
|
Packit |
aea12f |
} else {
|
|
Packit |
aea12f |
for (i = 0; i < SESSIONS; i++)
|
|
Packit |
aea12f |
close(server_sds[i]);
|
|
Packit |
aea12f |
client(client_sds);
|
|
Packit |
aea12f |
exit(0);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#endif /* _WIN32 */
|