|
Packit |
aea12f |
/*
|
|
Packit |
aea12f |
* Copyright (C) 2019 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 Lesser General Public License
|
|
Packit |
aea12f |
* along with this program. If not, see <https://www.gnu.org/licenses/>
|
|
Packit |
aea12f |
*
|
|
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 <stdint.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 |
#include "tls13/ext-parse.h"
|
|
Packit |
aea12f |
#include <assert.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 |
static int server_handshake_callback(gnutls_session_t session, unsigned int htype,
|
|
Packit |
aea12f |
unsigned post, unsigned int incoming,
|
|
Packit |
aea12f |
const gnutls_datum_t *msg)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
unsigned pos;
|
|
Packit |
aea12f |
gnutls_datum_t mmsg;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
assert(post && htype == GNUTLS_HANDSHAKE_SERVER_HELLO);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
switch (htype) {
|
|
Packit |
aea12f |
case GNUTLS_HANDSHAKE_SERVER_HELLO:
|
|
Packit |
aea12f |
assert(msg->size >= HANDSHAKE_SESSION_ID_POS);
|
|
Packit |
aea12f |
pos = HANDSHAKE_SESSION_ID_POS;
|
|
Packit |
aea12f |
SKIP8(pos, msg->size);
|
|
Packit |
aea12f |
pos += 3; /* ciphersuite + compression */
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
mmsg.data = &msg->data[pos];
|
|
Packit |
aea12f |
mmsg.size = msg->size - pos;
|
|
Packit |
aea12f |
if (pos != msg->size) {
|
|
Packit |
aea12f |
if (pos < msg->size-1) {
|
|
Packit |
aea12f |
fprintf(stderr, "additional bytes: %.2x%.2x\n", mmsg.data[0], mmsg.data[1]);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
fail("the server hello contains additional bytes\n");
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
break;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
return 0;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static int client_handshake_callback(gnutls_session_t session, unsigned int htype,
|
|
Packit |
aea12f |
unsigned post, unsigned int incoming,
|
|
Packit |
aea12f |
const gnutls_datum_t *msg)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
unsigned pos;
|
|
Packit |
aea12f |
gnutls_datum_t mmsg;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
assert(!post && htype == GNUTLS_HANDSHAKE_CLIENT_HELLO);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
switch (htype) {
|
|
Packit |
aea12f |
case GNUTLS_HANDSHAKE_CLIENT_HELLO:
|
|
Packit |
aea12f |
assert(msg->size >= HANDSHAKE_SESSION_ID_POS);
|
|
Packit |
aea12f |
pos = HANDSHAKE_SESSION_ID_POS;
|
|
Packit |
aea12f |
SKIP8(pos, msg->size);
|
|
Packit |
aea12f |
SKIP16(pos, msg->size);
|
|
Packit |
aea12f |
SKIP8(pos, msg->size);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
mmsg.data = &msg->data[pos];
|
|
Packit |
aea12f |
mmsg.size = msg->size - pos;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (pos != msg->size) {
|
|
Packit |
aea12f |
if (pos < msg->size-1) {
|
|
Packit |
aea12f |
fprintf(stderr, "additional bytes: %.2x%.2x\n", mmsg.data[0], mmsg.data[1]);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
fail("the client hello contains additional bytes\n");
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
break;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
return 0;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static
|
|
Packit |
aea12f |
void start(const char *prio, gnutls_protocol_t exp_version)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
int ret;
|
|
Packit |
aea12f |
/* Server stuff. */
|
|
Packit |
aea12f |
gnutls_certificate_credentials_t serverx509cred;
|
|
Packit |
aea12f |
gnutls_session_t server;
|
|
Packit |
aea12f |
int sret = GNUTLS_E_AGAIN;
|
|
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 |
|
|
Packit |
aea12f |
success("trying %s\n", prio);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* General init. */
|
|
Packit |
aea12f |
global_init();
|
|
Packit |
aea12f |
gnutls_global_set_log_function(tls_log_func);
|
|
Packit |
aea12f |
if (debug)
|
|
Packit |
aea12f |
gnutls_global_set_log_level(6);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* Init server */
|
|
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 |
|
|
Packit |
aea12f |
gnutls_init(&server, GNUTLS_SERVER);
|
|
Packit |
aea12f |
gnutls_credentials_set(server, GNUTLS_CRD_CERTIFICATE,
|
|
Packit |
aea12f |
serverx509cred);
|
|
Packit |
aea12f |
assert(gnutls_priority_set_direct(server, prio, NULL)>=0);
|
|
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_ptr(server, server);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_handshake_set_hook_function(server,
|
|
Packit |
aea12f |
GNUTLS_HANDSHAKE_SERVER_HELLO,
|
|
Packit |
aea12f |
GNUTLS_HOOK_POST,
|
|
Packit |
aea12f |
server_handshake_callback);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* Init client */
|
|
Packit |
aea12f |
ret = gnutls_certificate_allocate_credentials(&clientx509cred);
|
|
Packit |
aea12f |
if (ret < 0)
|
|
Packit |
aea12f |
exit(1);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ret = gnutls_certificate_set_x509_trust_mem(clientx509cred, &ca_cert, GNUTLS_X509_FMT_PEM);
|
|
Packit |
aea12f |
if (ret < 0)
|
|
Packit |
aea12f |
exit(1);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ret = gnutls_init(&client, GNUTLS_CLIENT);
|
|
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 |
gnutls_priority_set_direct(client, prio, NULL);
|
|
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_ptr(client, client);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_handshake_set_hook_function(client,
|
|
Packit |
aea12f |
GNUTLS_HANDSHAKE_CLIENT_HELLO,
|
|
Packit |
aea12f |
GNUTLS_HOOK_PRE,
|
|
Packit |
aea12f |
client_handshake_callback);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
HANDSHAKE(client, server);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* check gnutls_certificate_get_ours() - client side */
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
const gnutls_datum_t *mcert;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
mcert = gnutls_certificate_get_ours(client);
|
|
Packit |
aea12f |
if (mcert != NULL) {
|
|
Packit |
aea12f |
fail("gnutls_certificate_get_ours(): failed\n");
|
|
Packit |
aea12f |
exit(1);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
assert(gnutls_protocol_get_version(server) == exp_version);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
assert(gnutls_certificate_type_get(server)==GNUTLS_CRT_X509);
|
|
Packit |
aea12f |
assert(gnutls_certificate_type_get(client)==GNUTLS_CRT_X509);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_bye(client, GNUTLS_SHUT_RDWR);
|
|
Packit |
aea12f |
gnutls_bye(server, GNUTLS_SHUT_RDWR);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_deinit(client);
|
|
Packit |
aea12f |
gnutls_deinit(server);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_certificate_free_credentials(serverx509cred);
|
|
Packit |
aea12f |
gnutls_certificate_free_credentials(clientx509cred);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_global_deinit();
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
reset_buffers();
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
void doit(void)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
start("NORMAL:-VERS-ALL:+VERS-TLS1.0:%NO_EXTENSIONS", GNUTLS_TLS1_0);
|
|
Packit |
aea12f |
start("NORMAL:-VERS-ALL:+VERS-TLS1.1:%NO_EXTENSIONS", GNUTLS_TLS1_1);
|
|
Packit |
aea12f |
start("NORMAL:-VERS-ALL:+VERS-TLS1.2:%NO_EXTENSIONS", GNUTLS_TLS1_2);
|
|
Packit |
aea12f |
start("NORMAL:-VERS-ALL:+VERS-TLS1.3:+VERS-TLS1.2:%NO_EXTENSIONS", GNUTLS_TLS1_2);
|
|
Packit |
aea12f |
}
|