|
Packit |
aea12f |
/*
|
|
Packit |
aea12f |
* Copyright (C) 2016 Red Hat, Inc.
|
|
Packit |
aea12f |
*
|
|
Packit |
aea12f |
* Author: Nikos Mavrogiannopoulos
|
|
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 General Public License
|
|
Packit |
aea12f |
* along with GnuTLS; if not, write to the Free Software Foundation,
|
|
Packit |
aea12f |
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
Packit |
aea12f |
*/
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#ifdef HAVE_CONFIG_H
|
|
Packit |
aea12f |
#include <config.h>
|
|
Packit |
aea12f |
#endif
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#include <stdio.h>
|
|
Packit |
aea12f |
#include <stdlib.h>
|
|
Packit |
aea12f |
#include <string.h>
|
|
Packit |
aea12f |
#include <errno.h>
|
|
Packit |
aea12f |
#include <gnutls/gnutls.h>
|
|
Packit |
aea12f |
#include "utils.h"
|
|
Packit |
aea12f |
#include "eagain-common.h"
|
|
Packit |
aea12f |
#include "cert-common.h"
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
const char *side;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void tls_log_func(int level, const char *str)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
fprintf(stderr, "%s|<%d>| %s", side, level, str);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#define TESTDATA "xxxtesttest1234"
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* counts the number of calls of send_testdata() within a handshake */
|
|
Packit |
aea12f |
enum {
|
|
Packit |
aea12f |
TEST_SEND_RECV,
|
|
Packit |
aea12f |
TEST_RECV_SEND,
|
|
Packit |
aea12f |
TEST_HANDSHAKE_CALL,
|
|
Packit |
aea12f |
TEST_BYE,
|
|
Packit |
aea12f |
TESTNO_MAX
|
|
Packit |
aea12f |
};
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#define myfail(fmt, ...) \
|
|
Packit |
aea12f |
fail("%s%s %d: "fmt, dtls?"dtls":"tls", name, testno, ##__VA_ARGS__)
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void try(const char *name, unsigned testno, unsigned fs,
|
|
Packit |
aea12f |
const char *prio, unsigned dhsize,
|
|
Packit |
aea12f |
unsigned dtls)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
int ret;
|
|
Packit |
aea12f |
/* Server stuff. */
|
|
Packit |
aea12f |
gnutls_certificate_credentials_t serverx509cred;
|
|
Packit |
aea12f |
gnutls_anon_client_credentials_t clientanoncred;
|
|
Packit |
aea12f |
gnutls_anon_server_credentials_t serveranoncred;
|
|
Packit |
aea12f |
gnutls_session_t server;
|
|
Packit |
aea12f |
int sret = GNUTLS_E_AGAIN;
|
|
Packit |
aea12f |
char buffer[512];
|
|
Packit |
aea12f |
/* Client stuff. */
|
|
Packit |
aea12f |
gnutls_certificate_credentials_t clientx509cred;
|
|
Packit |
aea12f |
gnutls_session_t client;
|
|
Packit |
aea12f |
int cret = GNUTLS_E_AGAIN;
|
|
Packit |
aea12f |
const gnutls_datum_t p3_2048 =
|
|
Packit |
aea12f |
{ (void *)pkcs3_2048, strlen(pkcs3_2048) };
|
|
Packit |
aea12f |
const gnutls_datum_t p3_3072 =
|
|
Packit |
aea12f |
{ (void *)pkcs3_3072, strlen(pkcs3_3072) };
|
|
Packit |
aea12f |
gnutls_dh_params_t dh_params;
|
|
Packit |
aea12f |
unsigned flags = 0;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (testno == TEST_HANDSHAKE_CALL && fs == 0)
|
|
Packit |
aea12f |
return;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (dtls)
|
|
Packit |
aea12f |
flags |= GNUTLS_DATAGRAM|GNUTLS_NONBLOCK;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* General init. */
|
|
Packit |
aea12f |
gnutls_global_set_log_function(tls_log_func);
|
|
Packit |
aea12f |
if (debug)
|
|
Packit |
aea12f |
gnutls_global_set_log_level(999);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_dh_params_init(&dh_params);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (dhsize < 3072) {
|
|
Packit |
aea12f |
ret = gnutls_dh_params_import_pkcs3(dh_params, &p3_2048,
|
|
Packit |
aea12f |
GNUTLS_X509_FMT_PEM);
|
|
Packit |
aea12f |
} else {
|
|
Packit |
aea12f |
ret = gnutls_dh_params_import_pkcs3(dh_params, &p3_3072,
|
|
Packit |
aea12f |
GNUTLS_X509_FMT_PEM);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* Init server */
|
|
Packit |
aea12f |
gnutls_anon_allocate_server_credentials(&serveranoncred);
|
|
Packit |
aea12f |
gnutls_anon_set_server_dh_params(serveranoncred, dh_params);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_certificate_allocate_credentials(&serverx509cred);
|
|
Packit |
aea12f |
gnutls_certificate_set_x509_key_mem(serverx509cred,
|
|
Packit |
aea12f |
&server_cert, &server_key,
|
|
Packit |
aea12f |
GNUTLS_X509_FMT_PEM);
|
|
Packit |
aea12f |
gnutls_certificate_set_x509_key_mem(serverx509cred,
|
|
Packit |
aea12f |
&server_ecc_cert, &server_ecc_key,
|
|
Packit |
aea12f |
GNUTLS_X509_FMT_PEM);
|
|
Packit |
aea12f |
gnutls_certificate_set_dh_params(serverx509cred, dh_params);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_init(&server, GNUTLS_SERVER|flags);
|
|
Packit |
aea12f |
gnutls_credentials_set(server, GNUTLS_CRD_CERTIFICATE, serverx509cred);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_credentials_set(server, GNUTLS_CRD_ANON, serveranoncred);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_priority_set_direct(server, prio, NULL);
|
|
Packit |
aea12f |
gnutls_transport_set_push_function(server, server_push);
|
|
Packit |
aea12f |
gnutls_transport_set_pull_function(server, server_pull);
|
|
Packit |
aea12f |
gnutls_transport_set_pull_timeout_function(server, server_pull_timeout_func);
|
|
Packit |
aea12f |
gnutls_transport_set_ptr(server, server);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* Init client */
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_anon_allocate_client_credentials(&clientanoncred);
|
|
Packit |
aea12f |
ret = gnutls_certificate_allocate_credentials(&clientx509cred);
|
|
Packit |
aea12f |
if (ret < 0)
|
|
Packit |
aea12f |
exit(1);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ret =
|
|
Packit |
aea12f |
gnutls_certificate_set_x509_trust_mem(clientx509cred, &ca_cert,
|
|
Packit |
aea12f |
GNUTLS_X509_FMT_PEM);
|
|
Packit |
aea12f |
if (ret < 0)
|
|
Packit |
aea12f |
exit(1);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ret = gnutls_init(&client, GNUTLS_CLIENT|GNUTLS_ENABLE_FALSE_START|flags);
|
|
Packit |
aea12f |
if (ret < 0)
|
|
Packit |
aea12f |
exit(1);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ret = gnutls_credentials_set(client, GNUTLS_CRD_CERTIFICATE,
|
|
Packit |
aea12f |
clientx509cred);
|
|
Packit |
aea12f |
if (ret < 0)
|
|
Packit |
aea12f |
exit(1);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ret = gnutls_credentials_set(client, GNUTLS_CRD_ANON, clientanoncred);
|
|
Packit |
aea12f |
if (ret < 0)
|
|
Packit |
aea12f |
exit(1);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ret = gnutls_priority_set_direct(client, prio, NULL);
|
|
Packit |
aea12f |
if (ret < 0)
|
|
Packit |
aea12f |
exit(1);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_transport_set_push_function(client, client_push);
|
|
Packit |
aea12f |
gnutls_transport_set_pull_function(client, client_pull);
|
|
Packit |
aea12f |
gnutls_transport_set_pull_timeout_function(client, client_pull_timeout_func);
|
|
Packit |
aea12f |
gnutls_transport_set_ptr(client, client);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
HANDSHAKE(client, server);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if ((gnutls_session_get_flags(client) & GNUTLS_SFLAGS_FALSE_START)
|
|
Packit |
aea12f |
&& !fs) {
|
|
Packit |
aea12f |
myfail("false start occurred but not expected\n");
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (!(gnutls_session_get_flags(client) & GNUTLS_SFLAGS_FALSE_START)
|
|
Packit |
aea12f |
&& fs) {
|
|
Packit |
aea12f |
myfail("false start expected but not happened\n");
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (testno == TEST_SEND_RECV) {
|
|
Packit |
aea12f |
side = "client";
|
|
Packit |
aea12f |
ret =
|
|
Packit |
aea12f |
gnutls_record_send(client, TESTDATA, sizeof(TESTDATA) - 1);
|
|
Packit |
aea12f |
if (ret < 0) {
|
|
Packit |
aea12f |
myfail("error sending false start data: %s\n",
|
|
Packit |
aea12f |
gnutls_strerror(ret));
|
|
Packit |
aea12f |
exit(1);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
side = "server";
|
|
Packit |
aea12f |
/* verify whether the server received the expected data */
|
|
Packit |
aea12f |
ret = gnutls_record_recv(server, buffer, sizeof(buffer));
|
|
Packit |
aea12f |
if (ret < 0) {
|
|
Packit |
aea12f |
myfail("error receiving data: %s\n",
|
|
Packit |
aea12f |
gnutls_strerror(ret));
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (ret != sizeof(TESTDATA) - 1) {
|
|
Packit |
aea12f |
myfail("error in received data size\n");
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (memcmp(buffer, TESTDATA, ret) != 0) {
|
|
Packit |
aea12f |
myfail("error in received data\n");
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* check handshake completion */
|
|
Packit |
aea12f |
ret =
|
|
Packit |
aea12f |
gnutls_record_send(server, TESTDATA, sizeof(TESTDATA) - 1);
|
|
Packit |
aea12f |
if (ret < 0) {
|
|
Packit |
aea12f |
myfail("error sending false start data: %s\n",
|
|
Packit |
aea12f |
gnutls_strerror(ret));
|
|
Packit |
aea12f |
exit(1);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
side = "client";
|
|
Packit |
aea12f |
do {
|
|
Packit |
aea12f |
ret =
|
|
Packit |
aea12f |
gnutls_record_recv(client, buffer, sizeof(buffer));
|
|
Packit |
aea12f |
} while (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED);
|
|
Packit |
aea12f |
if (ret < 0) {
|
|
Packit |
aea12f |
myfail("error receiving data: %s\n",
|
|
Packit |
aea12f |
gnutls_strerror(ret));
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
} else if (testno == TEST_RECV_SEND) {
|
|
Packit |
aea12f |
side = "server";
|
|
Packit |
aea12f |
ret =
|
|
Packit |
aea12f |
gnutls_record_send(server, TESTDATA, sizeof(TESTDATA) - 1);
|
|
Packit |
aea12f |
if (ret < 0) {
|
|
Packit |
aea12f |
myfail("error sending false start data: %s\n",
|
|
Packit |
aea12f |
gnutls_strerror(ret));
|
|
Packit |
aea12f |
exit(1);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
side = "client";
|
|
Packit |
aea12f |
/* verify whether the server received the expected data */
|
|
Packit |
aea12f |
ret = gnutls_record_recv(client, buffer, sizeof(buffer));
|
|
Packit |
aea12f |
if (ret < 0) {
|
|
Packit |
aea12f |
myfail("error receiving data: %s\n",
|
|
Packit |
aea12f |
gnutls_strerror(ret));
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (ret != sizeof(TESTDATA) - 1) {
|
|
Packit |
aea12f |
myfail("error in received data size\n");
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (memcmp(buffer, TESTDATA, ret) != 0) {
|
|
Packit |
aea12f |
myfail("error in received data\n");
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
} else if (testno == TEST_HANDSHAKE_CALL) {
|
|
Packit |
aea12f |
/* explicit completion by caller */
|
|
Packit |
aea12f |
ret = gnutls_handshake(client);
|
|
Packit |
aea12f |
if (ret != GNUTLS_E_HANDSHAKE_DURING_FALSE_START) {
|
|
Packit |
aea12f |
myfail
|
|
Packit |
aea12f |
("error in explicit handshake after false start: %s\n",
|
|
Packit |
aea12f |
gnutls_strerror(ret));
|
|
Packit |
aea12f |
exit(1);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
goto exit;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
side = "server";
|
|
Packit |
aea12f |
ret = gnutls_bye(server, GNUTLS_SHUT_WR);
|
|
Packit |
aea12f |
if (ret < 0) {
|
|
Packit |
aea12f |
myfail("error in server bye: %s\n",
|
|
Packit |
aea12f |
gnutls_strerror(ret));
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
side = "client";
|
|
Packit |
aea12f |
ret = gnutls_bye(client, GNUTLS_SHUT_RDWR);
|
|
Packit |
aea12f |
if (ret < 0) {
|
|
Packit |
aea12f |
myfail("error in client bye: %s\n",
|
|
Packit |
aea12f |
gnutls_strerror(ret));
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
success("%5s%s \tok\n", dtls?"dtls-":"tls-", name);
|
|
Packit |
aea12f |
exit:
|
|
Packit |
aea12f |
gnutls_deinit(client);
|
|
Packit |
aea12f |
gnutls_deinit(server);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_dh_params_deinit(dh_params);
|
|
Packit |
aea12f |
gnutls_anon_free_server_credentials(serveranoncred);
|
|
Packit |
aea12f |
gnutls_anon_free_client_credentials(clientanoncred);
|
|
Packit |
aea12f |
gnutls_certificate_free_credentials(serverx509cred);
|
|
Packit |
aea12f |
gnutls_certificate_free_credentials(clientx509cred);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
void doit(void)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
unsigned i, j;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
global_init();
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
for (j=0;j<2;j++) {
|
|
Packit |
aea12f |
for (i = 0; i < TESTNO_MAX; i++) {
|
|
Packit |
aea12f |
try("1.2 anon-dh :", i, 0, "NORMAL:-VERS-ALL:+VERS-DTLS1.2:+VERS-TLS1.2:-KX-ALL:+ANON-DH", 3072, j);
|
|
Packit |
aea12f |
reset_buffers();
|
|
Packit |
aea12f |
try("1.2 anon-ecdh:", i, 0, "NORMAL:-VERS-ALL:+VERS-DTLS1.2:+VERS-TLS1.2:-KX-ALL:+ANON-ECDH", 2048, j);
|
|
Packit |
aea12f |
reset_buffers();
|
|
Packit |
aea12f |
try("1.2 ecdhe-rsa:", i, 1, "NORMAL:-VERS-ALL:+VERS-DTLS1.2:+VERS-TLS1.2:-KX-ALL:+ECDHE-RSA", 2048, j);
|
|
Packit |
aea12f |
reset_buffers();
|
|
Packit |
aea12f |
try("1.2 ecdhe-x25519-rsa:", i, 1, "NORMAL:-VERS-ALL:+VERS-DTLS1.2:+VERS-TLS1.2:-KX-ALL:+ECDHE-RSA:-CURVE-ALL:+CURVE-X25519", 2048, j);
|
|
Packit |
aea12f |
reset_buffers();
|
|
Packit |
aea12f |
try("1.2 ecdhe-ecdsa:", i, 1, "NORMAL:-VERS-ALL:+VERS-DTLS1.2:+VERS-TLS1.2:-KX-ALL:+ECDHE-ECDSA", 2048, j);
|
|
Packit |
aea12f |
reset_buffers();
|
|
Packit |
aea12f |
try("1.2 dhe-rsa-2048:", i, 0, "NORMAL:-VERS-ALL:+VERS-DTLS1.2:+VERS-TLS1.2:-KX-ALL:+DHE-RSA", 2048, j);
|
|
Packit |
aea12f |
reset_buffers();
|
|
Packit |
aea12f |
try("1.2 dhe-rsa-3072:", i, 1, "NORMAL:-VERS-ALL:+VERS-DTLS1.2:+VERS-TLS1.2:-KX-ALL:+DHE-RSA", 3072, j);
|
|
Packit |
aea12f |
reset_buffers();
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* it should work, but false start will not be reported */
|
|
Packit |
aea12f |
try("1.3 secp256r1:", i, 0, "NORMAL:-VERS-ALL:+VERS-TLS1.3:-GROUP-ALL:+GROUP-SECP256R1", 2048, 0);
|
|
Packit |
aea12f |
reset_buffers();
|
|
Packit |
aea12f |
try("1.3 ffdhe2048:", i, 0, "NORMAL:-VERS-ALL:+VERS-TLS1.3:+GROUP-FFDHE2048", 2048, 0);
|
|
Packit |
aea12f |
reset_buffers();
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_global_deinit();
|
|
Packit |
aea12f |
}
|