|
Packit |
aea12f |
/*
|
|
Packit |
aea12f |
* Copyright (C) 2012 Free Software Foundation, 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 General Public License
|
|
Packit |
aea12f |
* along with GnuTLS; if not, write to the Free Software Foundation,
|
|
Packit |
aea12f |
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
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 <gnutls/gnutls.h>
|
|
Packit |
aea12f |
#include <gnutls/dtls.h>
|
|
Packit |
aea12f |
#include <assert.h>
|
|
Packit |
aea12f |
#include <signal.h>
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#include "utils.h"
|
|
Packit |
aea12f |
#include "cert-common.h"
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* This program tests whether the GNUTLS_CERT_* flags
|
|
Packit |
aea12f |
* work as expected.
|
|
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 |
#define MAX_BUF 1024
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void client(int fd, const char *prio)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
int ret;
|
|
Packit |
aea12f |
const char *p;
|
|
Packit |
aea12f |
gnutls_certificate_credentials_t x509_cred;
|
|
Packit |
aea12f |
gnutls_session_t session;
|
|
Packit |
aea12f |
|
|
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 |
gnutls_init(&session, GNUTLS_CLIENT);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ret =
|
|
Packit |
aea12f |
gnutls_priority_set_direct(session,
|
|
Packit |
aea12f |
prio, &p);
|
|
Packit |
aea12f |
if (ret < 0) {
|
|
Packit |
aea12f |
fail("error in setting priority: %s\n", p);
|
|
Packit |
aea12f |
exit(1);
|
|
Packit |
aea12f |
}
|
|
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 < 0) {
|
|
Packit |
aea12f |
if (debug) {
|
|
Packit |
aea12f |
fail("client: Handshake failed\n");
|
|
Packit |
aea12f |
gnutls_perror(ret);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
exit(1);
|
|
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 |
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 |
static void server(int fd, const char *prio, unsigned status, int expected)
|
|
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 |
|
|
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 |
gnutls_init(&session, GNUTLS_SERVER);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
assert(gnutls_priority_set_direct(session,
|
|
Packit |
aea12f |
prio, NULL)>=0);
|
|
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 |
gnutls_certificate_allocate_credentials(&x509_cred);
|
|
Packit |
aea12f |
gnutls_certificate_set_x509_key_mem(x509_cred, &server_cert,
|
|
Packit |
aea12f |
&server_key,
|
|
Packit |
aea12f |
GNUTLS_X509_FMT_PEM);
|
|
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 |
gnutls_certificate_server_set_request(session, status);
|
|
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 == expected) {
|
|
Packit |
aea12f |
if (debug)
|
|
Packit |
aea12f |
success
|
|
Packit |
aea12f |
("server: Handshake finished as expected (%d)\n", ret);
|
|
Packit |
aea12f |
goto finish;
|
|
Packit |
aea12f |
} else {
|
|
Packit |
aea12f |
fail("expected %d, handshake returned %d\n", expected,
|
|
Packit |
aea12f |
ret);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (debug)
|
|
Packit |
aea12f |
success("server: Handshake was completed\n");
|
|
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 |
finish:
|
|
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 |
if (debug)
|
|
Packit |
aea12f |
success("server: finished\n");
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void start(const char *prio, unsigned status, int expected)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
int fd[2];
|
|
Packit |
aea12f |
int ret;
|
|
Packit |
aea12f |
pid_t child;
|
|
Packit |
aea12f |
int pstatus = 0;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
success("testing: %s (%d,%d)\n", prio, status, expected);
|
|
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], prio, status, expected);
|
|
Packit |
aea12f |
waitpid(-1, &pstatus, 0);
|
|
Packit |
aea12f |
check_wait_status_for_sig(pstatus);
|
|
Packit |
aea12f |
} else {
|
|
Packit |
aea12f |
close(fd[0]);
|
|
Packit |
aea12f |
client(fd[1], prio);
|
|
Packit |
aea12f |
exit(0);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void ch_handler(int sig)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
return;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
void doit(void)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
signal(SIGCHLD, ch_handler);
|
|
Packit |
aea12f |
signal(SIGPIPE, SIG_IGN);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
start("NONE:+VERS-TLS1.0:+AES-128-CBC:+SHA1:+SIGN-ALL:+COMP-NULL:+RSA", GNUTLS_CERT_IGNORE, 0);
|
|
Packit |
aea12f |
start("NONE:+VERS-TLS1.0:+AES-128-CBC:+SHA1:+SIGN-ALL:+COMP-NULL:+RSA", GNUTLS_CERT_REQUEST, 0);
|
|
Packit |
aea12f |
start("NONE:+VERS-TLS1.0:+AES-128-CBC:+SHA1:+SIGN-ALL:+COMP-NULL:+RSA", GNUTLS_CERT_REQUIRE, GNUTLS_E_NO_CERTIFICATE_FOUND);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
start("NORMAL:-VERS-ALL:+VERS-TLS1.2", GNUTLS_CERT_IGNORE, 0);
|
|
Packit |
aea12f |
start("NORMAL:-VERS-ALL:+VERS-TLS1.2", GNUTLS_CERT_REQUEST, 0);
|
|
Packit |
aea12f |
start("NORMAL:-VERS-ALL:+VERS-TLS1.2", GNUTLS_CERT_REQUIRE, GNUTLS_E_NO_CERTIFICATE_FOUND);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
start("NORMAL:-VERS-ALL:+VERS-TLS1.3", GNUTLS_CERT_IGNORE, 0);
|
|
Packit |
aea12f |
start("NORMAL:-VERS-ALL:+VERS-TLS1.3", GNUTLS_CERT_REQUEST, 0);
|
|
Packit |
aea12f |
start("NORMAL:-VERS-ALL:+VERS-TLS1.3", GNUTLS_CERT_REQUIRE, GNUTLS_E_CERTIFICATE_REQUIRED);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
start("NORMAL", GNUTLS_CERT_IGNORE, 0);
|
|
Packit |
aea12f |
start("NORMAL", GNUTLS_CERT_REQUEST, 0);
|
|
Packit |
aea12f |
start("NORMAL", GNUTLS_CERT_REQUIRE, GNUTLS_E_CERTIFICATE_REQUIRED);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#endif /* _WIN32 */
|