|
Packit |
aea12f |
/*
|
|
Packit |
aea12f |
* Copyright (C) 2017-2018 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 |
#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 |
|
|
Packit |
aea12f |
#if defined(_WIN32)
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
int main()
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
exit(77);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#else
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#include <stdint.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/dtls.h>
|
|
Packit |
aea12f |
#include <signal.h>
|
|
Packit |
aea12f |
#include <string.h>
|
|
Packit |
aea12f |
#include <assert.h>
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#include "utils.h"
|
|
Packit |
aea12f |
#include "cert-common.h"
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
enum {
|
|
Packit |
aea12f |
TEST_DEF_HANDHAKE,
|
|
Packit |
aea12f |
TEST_CUSTOM_EXT
|
|
Packit |
aea12f |
};
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* This program tests whether the Post Handshake Auth extension is
|
|
Packit |
aea12f |
* present in the client hello, and whether it is missing from server
|
|
Packit |
aea12f |
* hello. In addition it contains basic functionality test for
|
|
Packit |
aea12f |
* post handshake authentication.
|
|
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 |
static int ext_send(gnutls_session_t session, gnutls_buffer_t extdata)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
gnutls_buffer_append_data(extdata, "\xff", 1);
|
|
Packit |
aea12f |
return 0;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static int ext_recv(gnutls_session_t session, const unsigned char *buf, size_t buflen)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
return 0;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#define TLS_EXT_IMPL_DTLS 0xfeee
|
|
Packit |
aea12f |
#define TLS_EXT_EXPL_TLS 0xfeea
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void client(int fd, int type)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
int ret;
|
|
Packit |
aea12f |
gnutls_certificate_credentials_t x509_cred;
|
|
Packit |
aea12f |
gnutls_session_t session;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
assert(gnutls_global_init() >= 0);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_global_set_log_function(client_log_func);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
assert(gnutls_certificate_allocate_credentials(&x509_cred) >= 0);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
assert(gnutls_init(&session, GNUTLS_CLIENT|GNUTLS_DATAGRAM) >= 0);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (type == TEST_CUSTOM_EXT) {
|
|
Packit |
aea12f |
assert(gnutls_session_ext_register(session, "implicit-dtls", TLS_EXT_IMPL_DTLS, GNUTLS_EXT_TLS, ext_recv, ext_send, NULL, NULL, NULL, GNUTLS_EXT_FLAG_CLIENT_HELLO|GNUTLS_EXT_FLAG_TLS12_SERVER_HELLO|GNUTLS_EXT_FLAG_TLS13_SERVER_HELLO)>=0);
|
|
Packit |
aea12f |
assert(gnutls_session_ext_register(session, "explicit-tls", TLS_EXT_EXPL_TLS, GNUTLS_EXT_TLS, ext_recv, ext_send, NULL, NULL, NULL, GNUTLS_EXT_FLAG_CLIENT_HELLO|GNUTLS_EXT_FLAG_TLS12_SERVER_HELLO|GNUTLS_EXT_FLAG_TLS13_SERVER_HELLO|GNUTLS_EXT_FLAG_TLS)>=0);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_handshake_set_timeout(session, 20 * 1000);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
assert(gnutls_priority_set_direct(session, "NORMAL:-VERS-TLS-ALL:+VERS-TLS1.3:+VERS-TLS1.2:+VERS-TLS1.0", NULL) >= 0);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, x509_cred);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_transport_set_int(session, fd);
|
|
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("handshake: %s\n", gnutls_strerror(ret));
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
close(fd);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_deinit(session);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_certificate_free_credentials(x509_cred);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_global_deinit();
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#define TLS_EXT_KEY_SHARE 51
|
|
Packit |
aea12f |
#define TLS_EXT_POST_HANDSHAKE 49
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
struct ext_ctx_st {
|
|
Packit |
aea12f |
int extno;
|
|
Packit |
aea12f |
int found;
|
|
Packit |
aea12f |
};
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static int parse_ext(void *ctx, unsigned tls_id, const unsigned char *data, unsigned data_size)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
struct ext_ctx_st *s = ctx;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (s->extno == (int)tls_id)
|
|
Packit |
aea12f |
s->found = 1;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
return 0;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static unsigned find_client_extension(const gnutls_datum_t *msg, int extno)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
int ret;
|
|
Packit |
aea12f |
struct ext_ctx_st s;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
memset(&s, 0, sizeof(s));
|
|
Packit |
aea12f |
s.extno = extno;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ret = gnutls_ext_raw_parse(&s, parse_ext, msg, GNUTLS_EXT_RAW_FLAG_DTLS_CLIENT_HELLO);
|
|
Packit |
aea12f |
assert(ret>=0);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (s.found)
|
|
Packit |
aea12f |
return 1;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
return 0;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static int hellos_callback(gnutls_session_t session, unsigned int htype,
|
|
Packit |
aea12f |
unsigned post, unsigned int incoming, const gnutls_datum_t *msg)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
int *type;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (htype != GNUTLS_HANDSHAKE_CLIENT_HELLO || post != GNUTLS_HOOK_PRE)
|
|
Packit |
aea12f |
return 0;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
type = gnutls_session_get_ptr(session);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (find_client_extension(msg, TLS_EXT_KEY_SHARE))
|
|
Packit |
aea12f |
fail("Key share extension seen in client hello!\n");
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (find_client_extension(msg, TLS_EXT_POST_HANDSHAKE))
|
|
Packit |
aea12f |
fail("Key share extension seen in client hello!\n");
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (*type == TEST_CUSTOM_EXT) {
|
|
Packit |
aea12f |
if (!find_client_extension(msg, TLS_EXT_IMPL_DTLS))
|
|
Packit |
aea12f |
fail("Implicit DTLS extension not seen in client hello!\n");
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (find_client_extension(msg, TLS_EXT_EXPL_TLS))
|
|
Packit |
aea12f |
fail("Explicit TLS extension seen in client hello!\n");
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
return 0;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void server(int fd, int type)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
int ret;
|
|
Packit |
aea12f |
gnutls_session_t session;
|
|
Packit |
aea12f |
gnutls_certificate_credentials_t x509_cred;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
assert(gnutls_global_init() >= 0);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_global_set_log_function(server_log_func);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
assert(gnutls_certificate_allocate_credentials(&x509_cred) >= 0);
|
|
Packit |
aea12f |
assert(gnutls_certificate_set_x509_key_mem(x509_cred, &server_cert,
|
|
Packit |
aea12f |
&server_key,
|
|
Packit |
aea12f |
GNUTLS_X509_FMT_PEM) >= 0);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
assert(gnutls_init(&session, GNUTLS_SERVER|GNUTLS_POST_HANDSHAKE_AUTH|GNUTLS_DATAGRAM) >= 0);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_handshake_set_timeout(session, 20 * 1000);
|
|
Packit |
aea12f |
gnutls_handshake_set_hook_function(session, GNUTLS_HANDSHAKE_ANY,
|
|
Packit |
aea12f |
GNUTLS_HOOK_BOTH,
|
|
Packit |
aea12f |
hellos_callback);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_session_set_ptr(session, &type);
|
|
Packit |
aea12f |
assert(gnutls_priority_set_direct(session, "NORMAL:-VERS-TLS-ALL:+VERS-TLS1.3:+VERS-TLS1.2", NULL) >= 0);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, x509_cred);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_transport_set_int(session, fd);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
do {
|
|
Packit |
aea12f |
ret = gnutls_handshake(session);
|
|
Packit |
aea12f |
} while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (ret != 0)
|
|
Packit |
aea12f |
fail("handshake failed: %s\n", gnutls_strerror(ret));
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
close(fd);
|
|
Packit |
aea12f |
gnutls_deinit(session);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_certificate_free_credentials(x509_cred);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_global_deinit();
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void ch_handler(int sig)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
int status;
|
|
Packit |
aea12f |
wait(&status);
|
|
Packit |
aea12f |
check_wait_status(status);
|
|
Packit |
aea12f |
return;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static
|
|
Packit |
aea12f |
void start(const char *name, int type)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
int fd[2];
|
|
Packit |
aea12f |
int ret;
|
|
Packit |
aea12f |
pid_t child;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
signal(SIGCHLD, ch_handler);
|
|
Packit |
aea12f |
success("%s\n", name);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
|
|
Packit |
aea12f |
if (ret < 0) {
|
|
Packit |
aea12f |
perror("socketpair");
|
|
Packit |
aea12f |
exit(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 |
/* parent */
|
|
Packit |
aea12f |
close(fd[1]);
|
|
Packit |
aea12f |
server(fd[0], type);
|
|
Packit |
aea12f |
kill(child, SIGTERM);
|
|
Packit |
aea12f |
} else {
|
|
Packit |
aea12f |
close(fd[0]);
|
|
Packit |
aea12f |
client(fd[1], type);
|
|
Packit |
aea12f |
exit(0);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
void doit(void) {
|
|
Packit |
aea12f |
start("check default extensions", TEST_DEF_HANDHAKE);
|
|
Packit |
aea12f |
start("check registered extensions", TEST_CUSTOM_EXT);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#endif /* _WIN32 */
|