|
Packit Service |
4684c1 |
/*
|
|
Packit Service |
4684c1 |
* Copyright (C) 2019 Free Software Foundation, Inc.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Author: Aniketh Girish
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This file is part of GnuTLS.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* The GnuTLS is free software; you can redistribute it and/or
|
|
Packit Service |
4684c1 |
* modify it under the terms of the GNU Lesser General Public License
|
|
Packit Service |
4684c1 |
* as published by the Free Software Foundation; either version 2.1 of
|
|
Packit Service |
4684c1 |
* the License, or (at your option) any later version.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This library 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 |
* Lesser 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 |
|
|
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 |
#include <assert.h>
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#if !defined(__linux__) || !defined(__GNUC__)
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
int main(int argc, char **argv)
|
|
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 |
|
|
Packit Service |
4684c1 |
#include "cert-common.h"
|
|
Packit Service |
4684c1 |
#include "utils.h"
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* This program tests whether a keylog function is called.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static void terminate(void);
|
|
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 |
const char *side = "";
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* These are global */
|
|
Packit Service |
4684c1 |
static pid_t child;
|
|
Packit Service |
4684c1 |
#define MAX_BUF 1024
|
|
Packit Service |
4684c1 |
#define MSG "Hello TLS"
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static int
|
|
Packit Service |
4684c1 |
keylog_func(gnutls_session_t session,
|
|
Packit Service |
4684c1 |
const char *label,
|
|
Packit Service |
4684c1 |
const gnutls_datum_t *secret)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
unsigned int *call_count = gnutls_session_get_ptr(session);
|
|
Packit Service |
4684c1 |
static const char *exp_labels[] = {
|
|
Packit Service |
4684c1 |
"CLIENT_HANDSHAKE_TRAFFIC_SECRET",
|
|
Packit Service |
4684c1 |
"SERVER_HANDSHAKE_TRAFFIC_SECRET",
|
|
Packit Service |
4684c1 |
"EXPORTER_SECRET",
|
|
Packit Service |
4684c1 |
"CLIENT_TRAFFIC_SECRET_0",
|
|
Packit Service |
4684c1 |
"SERVER_TRAFFIC_SECRET_0"
|
|
Packit Service |
4684c1 |
};
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (*call_count >= sizeof(exp_labels)/sizeof(exp_labels[0]))
|
|
Packit Service |
4684c1 |
fail("unexpected secret at call count %u\n",
|
|
Packit Service |
4684c1 |
*call_count);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (strcmp(label, exp_labels[*call_count]) != 0)
|
|
Packit Service |
4684c1 |
fail("unexpected %s at call count %u\n",
|
|
Packit Service |
4684c1 |
label, *call_count);
|
|
Packit Service |
4684c1 |
else if (debug)
|
|
Packit Service |
4684c1 |
success("received %s at call count %u\n",
|
|
Packit Service |
4684c1 |
label, *call_count);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
(*call_count)++;
|
|
Packit Service |
4684c1 |
return 0;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static void client(int fd, const char *prio, unsigned int exp_call_count)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
gnutls_session_t session;
|
|
Packit Service |
4684c1 |
char buffer[MAX_BUF + 1];
|
|
Packit Service |
4684c1 |
unsigned int call_count = 0;
|
|
Packit Service |
4684c1 |
int ret, ii;
|
|
Packit Service |
4684c1 |
gnutls_certificate_credentials_t clientx509cred;
|
|
Packit Service |
4684c1 |
const char *err;
|
|
Packit Service |
4684c1 |
/* Need to enable anonymous KX specifically. */
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
global_init();
|
|
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(4711);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_certificate_allocate_credentials(&clientx509cred);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* Initialize TLS session
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
gnutls_init(&session, GNUTLS_CLIENT);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_session_set_ptr(session, &call_count);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* Use default priorities */
|
|
Packit Service |
4684c1 |
ret = gnutls_priority_set_direct(session, prio, &err;;
|
|
Packit Service |
4684c1 |
if (ret < 0) {
|
|
Packit Service |
4684c1 |
fail("client: priority set failed (%s): %s\n",
|
|
Packit Service |
4684c1 |
gnutls_strerror(ret), err);
|
|
Packit Service |
4684c1 |
exit(1);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE,
|
|
Packit Service |
4684c1 |
clientx509cred);
|
|
Packit Service |
4684c1 |
if (ret < 0)
|
|
Packit Service |
4684c1 |
exit(1);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_transport_set_int(session, fd);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_session_set_keylog_function(session, keylog_func);
|
|
Packit Service |
4684c1 |
assert(gnutls_session_get_keylog_function(session) == keylog_func);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* Perform the TLS handshake
|
|
Packit Service |
4684c1 |
*/
|
|
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: %s\n", gnutls_strerror(ret));
|
|
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 (debug)
|
|
Packit Service |
4684c1 |
success("client: TLS version is: %s\n",
|
|
Packit Service |
4684c1 |
gnutls_protocol_get_name
|
|
Packit Service |
4684c1 |
(gnutls_protocol_get_version(session)));
|
|
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, MAX_BUF);
|
|
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 |
} else if (ret < 0) {
|
|
Packit Service |
4684c1 |
fail("client: Error: %s\n", gnutls_strerror(ret));
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (debug) {
|
|
Packit Service |
4684c1 |
printf("- Received %d bytes: ", ret);
|
|
Packit Service |
4684c1 |
for (ii = 0; ii < ret; ii++) {
|
|
Packit Service |
4684c1 |
fputc(buffer[ii], stdout);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
fputs("\n", stdout);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (call_count != exp_call_count)
|
|
Packit Service |
4684c1 |
fail("secret hook is not called %u times (%u)\n",
|
|
Packit Service |
4684c1 |
call_count, exp_call_count);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_bye(session, GNUTLS_SHUT_WR);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
close(fd);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_deinit(session);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_certificate_free_credentials(clientx509cred);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_global_deinit();
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static void server(int fd, const char *prio, unsigned int exp_call_count)
|
|
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 |
unsigned int call_count = 0;
|
|
Packit Service |
4684c1 |
gnutls_certificate_credentials_t serverx509cred;
|
|
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 |
|
|
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(&serverx509cred);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_init(&session, GNUTLS_SERVER);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_session_set_ptr(session, &call_count);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* avoid calling all the priority functions, since the defaults
|
|
Packit Service |
4684c1 |
* are adequate.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
ret = gnutls_priority_set_direct(session,
|
|
Packit Service |
4684c1 |
"NORMAL:-VERS-ALL:+VERS-TLS1.3:-KX-ALL:-SIGN-ALL:+SIGN-RSA-PSS-RSAE-SHA384:-GROUP-ALL:+GROUP-SECP256R1", NULL);
|
|
Packit Service |
4684c1 |
if (ret < 0) {
|
|
Packit Service |
4684c1 |
fail("server: priority set failed (%s)\n\n",
|
|
Packit Service |
4684c1 |
gnutls_strerror(ret));
|
|
Packit Service |
4684c1 |
terminate();
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_certificate_set_x509_key_mem(serverx509cred,
|
|
Packit Service |
4684c1 |
&server_cert, &server_key,
|
|
Packit Service |
4684c1 |
GNUTLS_X509_FMT_PEM);
|
|
Packit Service |
4684c1 |
gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE,
|
|
Packit Service |
4684c1 |
serverx509cred);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_transport_set_int(session, fd);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_session_set_keylog_function(session, keylog_func);
|
|
Packit Service |
4684c1 |
|
|
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 |
if (ret < 0) {
|
|
Packit Service |
4684c1 |
close(fd);
|
|
Packit Service |
4684c1 |
gnutls_deinit(session);
|
|
Packit Service |
4684c1 |
fail("server: Handshake has failed (%s)\n\n",
|
|
Packit Service |
4684c1 |
gnutls_strerror(ret));
|
|
Packit Service |
4684c1 |
terminate();
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
if (debug) {
|
|
Packit Service |
4684c1 |
success("server: Handshake was completed\n");
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
if (debug)
|
|
Packit Service |
4684c1 |
success("server: TLS version is: %s\n",
|
|
Packit Service |
4684c1 |
gnutls_protocol_get_name
|
|
Packit Service |
4684c1 |
(gnutls_protocol_get_version(session)));
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
memset(buffer, 0, MAX_BUF + 1);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
do {
|
|
Packit Service |
4684c1 |
ret = gnutls_record_recv(session, buffer, MAX_BUF);
|
|
Packit Service |
4684c1 |
} while (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (ret == 0) {
|
|
Packit Service |
4684c1 |
if (debug)
|
|
Packit Service |
4684c1 |
success("server: Peer has closed the GnuTLS connection\n");
|
|
Packit Service |
4684c1 |
} else if (ret < 0) {
|
|
Packit Service |
4684c1 |
fail("server: Received corrupted data(%d). Closing...\n", ret);
|
|
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 |
if (call_count != exp_call_count)
|
|
Packit Service |
4684c1 |
fail("secret hook is not called %u times (%u)\n",
|
|
Packit Service |
4684c1 |
call_count, exp_call_count);
|
|
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(fd);
|
|
Packit Service |
4684c1 |
gnutls_deinit(session);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_certificate_free_credentials(serverx509cred);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_global_deinit();
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (debug)
|
|
Packit Service |
4684c1 |
success("server: finished\n");
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static void terminate(void)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
int status = 0;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
kill(child, SIGTERM);
|
|
Packit Service |
4684c1 |
wait(&status);
|
|
Packit Service |
4684c1 |
exit(1);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static void
|
|
Packit Service |
4684c1 |
run(const char *prio, unsigned int exp_call_count)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
int fd[2];
|
|
Packit Service |
4684c1 |
int ret;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
signal(SIGPIPE, SIG_IGN);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
|
|
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 |
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 |
int status = 0;
|
|
Packit Service |
4684c1 |
/* parent */
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
server(fd[0], prio, exp_call_count);
|
|
Packit Service |
4684c1 |
wait(&status);
|
|
Packit Service |
4684c1 |
check_wait_status(status);
|
|
Packit Service |
4684c1 |
} else {
|
|
Packit Service |
4684c1 |
close(fd[0]);
|
|
Packit Service |
4684c1 |
client(fd[1], prio, exp_call_count);
|
|
Packit Service |
4684c1 |
exit(0);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
void doit(void)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
run("NORMAL:-VERS-ALL:+VERS-TLS1.3", 5);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#endif /* _WIN32 */
|