|
Packit |
aea12f |
/*
|
|
Packit |
aea12f |
* Copyright (C) 2004-2012 Free Software Foundation, Inc.
|
|
Packit |
aea12f |
*
|
|
Packit |
aea12f |
* Author: Thierry Quemerais
|
|
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 |
/* This tests the supplemental data extension under TLS1.2 */
|
|
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 |
/* socketpair isn't supported on Win32. */
|
|
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 <sys/socket.h>
|
|
Packit |
aea12f |
#include <assert.h>
|
|
Packit |
aea12f |
#if !defined(_WIN32)
|
|
Packit |
aea12f |
#include <sys/wait.h>
|
|
Packit |
aea12f |
#endif
|
|
Packit |
aea12f |
#include <unistd.h>
|
|
Packit |
aea12f |
#include <gnutls/gnutls.h>
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#include "cert-common.h"
|
|
Packit |
aea12f |
#include "utils.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 TLS_SUPPLEMENTALDATATYPE_SAMPLE 0xBABE
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static int TLS_SUPPLEMENTALDATA_client_sent = 0;
|
|
Packit |
aea12f |
static int TLS_SUPPLEMENTALDATA_client_received = 0;
|
|
Packit |
aea12f |
static int TLS_SUPPLEMENTALDATA_server_sent = 0;
|
|
Packit |
aea12f |
static int TLS_SUPPLEMENTALDATA_server_received = 0;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static const unsigned char supp_data[] =
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
0xFE,
|
|
Packit |
aea12f |
0xED
|
|
Packit |
aea12f |
};
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static
|
|
Packit |
aea12f |
int supp_client_recv_func(gnutls_session_t session, const unsigned char *buf, size_t buflen)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
TLS_SUPPLEMENTALDATA_client_received = 1;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (buflen != sizeof(supp_data))
|
|
Packit |
aea12f |
fail("supp_client_recv_func: Invalid input buffer len\n");
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (memcmp(buf, supp_data, sizeof(supp_data)) != 0)
|
|
Packit |
aea12f |
fail("supp_client_recv_func: Invalid input buffer data\n");
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
return GNUTLS_E_SUCCESS;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static
|
|
Packit |
aea12f |
int supp_client_send_func(gnutls_session_t session, gnutls_buffer_t buf)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
TLS_SUPPLEMENTALDATA_client_sent = 1;
|
|
Packit |
aea12f |
gnutls_buffer_append_data(buf, supp_data, sizeof(supp_data));
|
|
Packit |
aea12f |
return GNUTLS_E_SUCCESS;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static
|
|
Packit |
aea12f |
int supp_server_recv_func(gnutls_session_t session, const unsigned char *buf, size_t buflen)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
TLS_SUPPLEMENTALDATA_server_received = 1;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (buflen != sizeof(supp_data))
|
|
Packit |
aea12f |
fail("supp_server_recv_func: Invalid input buffer len\n");
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (memcmp(buf, supp_data, sizeof(supp_data)) != 0)
|
|
Packit |
aea12f |
fail("supp_server_recv_func: Invalid input buffer data\n");
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
return GNUTLS_E_SUCCESS;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static
|
|
Packit |
aea12f |
int supp_server_send_func(gnutls_session_t session, gnutls_buffer_t buf)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
TLS_SUPPLEMENTALDATA_server_sent = 1;
|
|
Packit |
aea12f |
gnutls_buffer_append_data(buf, supp_data, sizeof(supp_data));
|
|
Packit |
aea12f |
return GNUTLS_E_SUCCESS;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void client(int sd, const char *prio, unsigned server_only)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
int ret;
|
|
Packit |
aea12f |
gnutls_session_t session;
|
|
Packit |
aea12f |
gnutls_certificate_credentials_t clientx509cred;
|
|
Packit |
aea12f |
|
|
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(4711);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
side = "client";
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_certificate_allocate_credentials(&clientx509cred);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* Initialize TLS session
|
|
Packit |
aea12f |
*/
|
|
Packit |
aea12f |
gnutls_init(&session, GNUTLS_CLIENT);
|
|
Packit |
aea12f |
gnutls_handshake_set_timeout(session, 20 * 1000);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
assert(gnutls_priority_set_direct(session, prio, NULL) >= 0);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* put the anonymous credentials to the current session
|
|
Packit |
aea12f |
*/
|
|
Packit |
aea12f |
gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE,
|
|
Packit |
aea12f |
clientx509cred);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_transport_set_int(session, sd);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (!server_only) {
|
|
Packit |
aea12f |
gnutls_supplemental_recv(session, 1);
|
|
Packit |
aea12f |
gnutls_supplemental_send(session, 1);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_session_supplemental_register(session, "supplemental_client", TLS_SUPPLEMENTALDATATYPE_SAMPLE, supp_client_recv_func, supp_client_send_func, 0);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* Perform the TLS handshake
|
|
Packit |
aea12f |
*/
|
|
Packit |
aea12f |
ret = gnutls_handshake(session);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (ret < 0) {
|
|
Packit |
aea12f |
fail("client: Handshake failed\n");
|
|
Packit |
aea12f |
gnutls_perror(ret);
|
|
Packit |
aea12f |
goto end;
|
|
Packit |
aea12f |
} else {
|
|
Packit |
aea12f |
if (debug)
|
|
Packit |
aea12f |
success("client: Handshake was completed\n");
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (!server_only) {
|
|
Packit |
aea12f |
if (TLS_SUPPLEMENTALDATA_client_sent != 1 || TLS_SUPPLEMENTALDATA_client_received != 1)
|
|
Packit |
aea12f |
fail("client: extension not properly sent/received\n");
|
|
Packit |
aea12f |
} else {
|
|
Packit |
aea12f |
/* we expect TLS1.2 handshake as TLS1.3 is not (yet) defined
|
|
Packit |
aea12f |
* with supplemental data */
|
|
Packit |
aea12f |
assert(gnutls_protocol_get_version(session) == GNUTLS_TLS1_2);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_bye(session, GNUTLS_SHUT_RDWR);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
end:
|
|
Packit |
aea12f |
close(sd);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_deinit(session);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_certificate_free_credentials(clientx509cred);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_global_deinit();
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void server(int sd, const char *prio, unsigned server_only)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
int ret;
|
|
Packit |
aea12f |
gnutls_session_t session;
|
|
Packit |
aea12f |
gnutls_certificate_credentials_t serverx509cred;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* this must be called once in the program
|
|
Packit |
aea12f |
*/
|
|
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(4711);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
side = "server";
|
|
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 |
|
|
Packit |
aea12f |
gnutls_init(&session, GNUTLS_SERVER);
|
|
Packit |
aea12f |
gnutls_handshake_set_timeout(session, 20 * 1000);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
assert(gnutls_priority_set_direct(session, prio, NULL) >= 0);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE,
|
|
Packit |
aea12f |
serverx509cred);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (!server_only) {
|
|
Packit |
aea12f |
gnutls_supplemental_recv(session, 1);
|
|
Packit |
aea12f |
gnutls_supplemental_send(session, 1);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_session_supplemental_register(session, "supplemental_server", TLS_SUPPLEMENTALDATATYPE_SAMPLE, supp_server_recv_func, supp_server_send_func, 0);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_transport_set_int(session, sd);
|
|
Packit |
aea12f |
ret = gnutls_handshake(session);
|
|
Packit |
aea12f |
if (ret < 0) {
|
|
Packit |
aea12f |
close(sd);
|
|
Packit |
aea12f |
gnutls_deinit(session);
|
|
Packit |
aea12f |
fail("server: Handshake has failed (%s)\n\n",
|
|
Packit |
aea12f |
gnutls_strerror(ret));
|
|
Packit |
aea12f |
return;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
if (debug)
|
|
Packit |
aea12f |
success("server: Handshake was completed\n");
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (!server_only) {
|
|
Packit |
aea12f |
if (TLS_SUPPLEMENTALDATA_server_sent != 1 || TLS_SUPPLEMENTALDATA_server_received != 1)
|
|
Packit |
aea12f |
fail("server: extension not properly sent/received\n");
|
|
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(sd);
|
|
Packit |
aea12f |
gnutls_deinit(session);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_certificate_free_credentials(serverx509cred);
|
|
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 |
static
|
|
Packit |
aea12f |
void start(const char *prio, unsigned server_only)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
pid_t child;
|
|
Packit |
aea12f |
int sockets[2], err;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
signal(SIGPIPE, SIG_IGN);
|
|
Packit |
aea12f |
TLS_SUPPLEMENTALDATA_client_sent = 0;
|
|
Packit |
aea12f |
TLS_SUPPLEMENTALDATA_client_received = 0;
|
|
Packit |
aea12f |
TLS_SUPPLEMENTALDATA_server_sent = 0;
|
|
Packit |
aea12f |
TLS_SUPPLEMENTALDATA_server_received = 0;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
success("trying: %s\n", prio);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
err = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
|
|
Packit |
aea12f |
if (err == -1) {
|
|
Packit |
aea12f |
perror("socketpair");
|
|
Packit |
aea12f |
fail("socketpair failed\n");
|
|
Packit |
aea12f |
return;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
child = fork();
|
|
Packit |
aea12f |
if (child < 0) {
|
|
Packit |
aea12f |
perror("fork");
|
|
Packit |
aea12f |
fail("fork");
|
|
Packit |
aea12f |
return;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (child) {
|
|
Packit |
aea12f |
int status;
|
|
Packit |
aea12f |
/* parent */
|
|
Packit |
aea12f |
server(sockets[0], prio, server_only);
|
|
Packit |
aea12f |
wait(&status);
|
|
Packit |
aea12f |
check_wait_status(status);
|
|
Packit |
aea12f |
} else {
|
|
Packit |
aea12f |
client(sockets[1], prio, server_only);
|
|
Packit |
aea12f |
exit(0);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
void doit(void)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
start("NORMAL:-VERS-ALL:+VERS-TLS1.3:+VERS-TLS1.2", 0);
|
|
Packit |
aea12f |
start("NORMAL:-VERS-ALL:+VERS-TLS1.2", 0);
|
|
Packit |
aea12f |
start("NORMAL", 0);
|
|
Packit |
aea12f |
/* try setting supplemental only in server side, it should
|
|
Packit |
aea12f |
* lead to normal authentication */
|
|
Packit |
aea12f |
start("NORMAL:-VERS-ALL:+VERS-TLS1.3:+VERS-TLS1.2", 1);
|
|
Packit |
aea12f |
start("NORMAL", 1);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
#endif /* _WIN32 */
|