|
Packit |
aea12f |
/*
|
|
Packit |
aea12f |
* Copyright (C) 2016-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 |
|
|
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 <string.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 <time.h>
|
|
Packit |
aea12f |
#include <gnutls/gnutls.h>
|
|
Packit |
aea12f |
#include <gnutls/dtls.h>
|
|
Packit |
aea12f |
#include <signal.h>
|
|
Packit |
aea12f |
#include <assert.h>
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#include "cert-common.h"
|
|
Packit |
aea12f |
#include "utils.h"
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void terminate(void);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* This program tests that handshakes do not include a session ticket
|
|
Packit |
aea12f |
* if the flag GNUTLS_NO_TICKETS is specified under TLS 1.2.
|
|
Packit |
aea12f |
*
|
|
Packit |
aea12f |
* Under TLS 1.3 it verifies that not enabling session tickets doesn't
|
|
Packit |
aea12f |
* result in a ticket being sent.
|
|
Packit |
aea12f |
*/
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static time_t mytime(time_t * t)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
time_t then = 1464610242;
|
|
Packit |
aea12f |
if (t)
|
|
Packit |
aea12f |
*t = then;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
return then;
|
|
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 sent = 0;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static int handshake_callback(gnutls_session_t session, unsigned int htype,
|
|
Packit |
aea12f |
unsigned post, unsigned int incoming, const gnutls_datum_t *msg)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
if (htype != GNUTLS_HANDSHAKE_NEW_SESSION_TICKET)
|
|
Packit |
aea12f |
return 0;
|
|
Packit |
aea12f |
fail("sent session ticket\n");
|
|
Packit |
aea12f |
sent = 1;
|
|
Packit |
aea12f |
return 0;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#define MAX_BUF 1024
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void client(int fd, const char *prio, unsigned int flags)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
int ret;
|
|
Packit |
aea12f |
gnutls_certificate_credentials_t x509_cred;
|
|
Packit |
aea12f |
gnutls_session_t session;
|
|
Packit |
aea12f |
/* Need to enable anonymous KX specifically. */
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
flags |= GNUTLS_CLIENT;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_global_set_time_function(mytime);
|
|
Packit |
aea12f |
global_init();
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (debug) {
|
|
Packit |
aea12f |
gnutls_global_set_log_function(client_log_func);
|
|
Packit |
aea12f |
gnutls_global_set_log_level(7);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_certificate_allocate_credentials(&x509_cred);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* Initialize TLS session
|
|
Packit |
aea12f |
*/
|
|
Packit |
aea12f |
gnutls_init(&session, flags);
|
|
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, x509_cred);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_transport_set_int(session, fd);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* Perform the TLS handshake
|
|
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 == GNUTLS_E_UNSUPPORTED_SIGNATURE_ALGORITHM) {
|
|
Packit |
aea12f |
/* success */
|
|
Packit |
aea12f |
goto end;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (ret < 0) {
|
|
Packit |
aea12f |
fail("client: Handshake failed: %s\n", gnutls_strerror(ret));
|
|
Packit |
aea12f |
terminate();
|
|
Packit |
aea12f |
} else {
|
|
Packit |
aea12f |
if (debug)
|
|
Packit |
aea12f |
success("client: Handshake was completed\n");
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (debug)
|
|
Packit |
aea12f |
success("client: TLS version is: %s\n",
|
|
Packit |
aea12f |
gnutls_protocol_get_name
|
|
Packit |
aea12f |
(gnutls_protocol_get_version(session)));
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_bye(session, GNUTLS_SHUT_WR);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
end:
|
|
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 |
|
|
Packit |
aea12f |
/* These are global */
|
|
Packit |
aea12f |
pid_t child;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void terminate(void)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
kill(child, SIGTERM);
|
|
Packit |
aea12f |
exit(1);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void server(int fd, const char *prio, unsigned int flags)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
int ret;
|
|
Packit |
aea12f |
char buffer[MAX_BUF + 1];
|
|
Packit |
aea12f |
gnutls_session_t session;
|
|
Packit |
aea12f |
gnutls_certificate_credentials_t x509_cred;
|
|
Packit |
aea12f |
gnutls_datum_t skey = {NULL, 0};
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
flags |= GNUTLS_SERVER;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* this must be called once in the program
|
|
Packit |
aea12f |
*/
|
|
Packit |
aea12f |
global_init();
|
|
Packit |
aea12f |
memset(buffer, 0, sizeof(buffer));
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (debug) {
|
|
Packit |
aea12f |
gnutls_global_set_log_function(server_log_func);
|
|
Packit |
aea12f |
gnutls_global_set_log_level(4711);
|
|
Packit |
aea12f |
}
|
|
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, flags)>=0);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (!(flags & GNUTLS_NO_TICKETS)) {
|
|
Packit |
aea12f |
assert(gnutls_session_ticket_key_generate(&skey)>=0);
|
|
Packit |
aea12f |
assert(gnutls_session_ticket_enable_server(session, &skey) >= 0);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_handshake_set_hook_function(session, GNUTLS_HANDSHAKE_NEW_SESSION_TICKET,
|
|
Packit |
aea12f |
GNUTLS_HOOK_POST,
|
|
Packit |
aea12f |
handshake_callback);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* avoid calling all the priority functions, since the defaults
|
|
Packit |
aea12f |
* are adequate.
|
|
Packit |
aea12f |
*/
|
|
Packit |
aea12f |
assert(gnutls_priority_set_direct(session, prio, 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 |
if (ret < 0) {
|
|
Packit |
aea12f |
/* failure is expected here */
|
|
Packit |
aea12f |
goto end;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (debug) {
|
|
Packit |
aea12f |
success("server: Handshake was completed\n");
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (debug)
|
|
Packit |
aea12f |
success("server: TLS version is: %s\n",
|
|
Packit |
aea12f |
gnutls_protocol_get_name
|
|
Packit |
aea12f |
(gnutls_protocol_get_version(session)));
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (sent != 0) {
|
|
Packit |
aea12f |
fail("new session ticket was sent\n");
|
|
Packit |
aea12f |
exit(1);
|
|
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 |
end:
|
|
Packit |
aea12f |
close(fd);
|
|
Packit |
aea12f |
gnutls_deinit(session);
|
|
Packit |
aea12f |
gnutls_free(skey.data);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_certificate_free_credentials(x509_cred);
|
|
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 void ch_handler(int sig)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
return;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static
|
|
Packit |
aea12f |
void start2(const char *prio, const char *sprio, unsigned int flags, unsigned int sflags)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
int fd[2];
|
|
Packit |
aea12f |
int ret, status = 0;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
success("trying %s\n", prio);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
sent = 0;
|
|
Packit |
aea12f |
signal(SIGCHLD, ch_handler);
|
|
Packit |
aea12f |
signal(SIGPIPE, SIG_IGN);
|
|
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], sprio, sflags);
|
|
Packit |
aea12f |
waitpid(child, &status, 0);
|
|
Packit |
aea12f |
check_wait_status(status);
|
|
Packit |
aea12f |
} else {
|
|
Packit |
aea12f |
close(fd[0]);
|
|
Packit |
aea12f |
client(fd[1], prio, flags);
|
|
Packit |
aea12f |
exit(0);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
return;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static
|
|
Packit |
aea12f |
void start(const char *prio, unsigned int flags)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
start2(prio, prio, GNUTLS_NO_TICKETS, flags);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
void doit(void)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
start("NORMAL:-VERS-ALL:+VERS-TLS1.2", 0);
|
|
Packit |
aea12f |
/* Under TLS 1.3 session tickets are not negotiated; they are
|
|
Packit |
aea12f |
* always sent unless server sets GNUTLS_NO_TICKETS... */
|
|
Packit |
aea12f |
start("NORMAL:-VERS-ALL:+VERS-TLS1.3", GNUTLS_NO_TICKETS);
|
|
Packit |
aea12f |
/* ...or there is no overlap between PSK key exchange modes */
|
|
Packit |
aea12f |
start2("NORMAL:-VERS-ALL:+VERS-TLS1.3:+PSK:-DHE-PSK", "NORMAL:-VERS-ALL:+VERS-TLS1.3", 0, 0);
|
|
Packit |
aea12f |
start("NORMAL", GNUTLS_NO_TICKETS);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#endif /* _WIN32 */
|