|
Packit |
aea12f |
/*
|
|
Packit |
aea12f |
* Copyright (C) 2016 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 |
/* Parts copied from GnuTLS example programs. */
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#ifdef HAVE_CONFIG_H
|
|
Packit |
aea12f |
#include <config.h>
|
|
Packit |
aea12f |
#endif
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#include <stdbool.h>
|
|
Packit |
aea12f |
#include <stdio.h>
|
|
Packit |
aea12f |
#include <stdlib.h>
|
|
Packit |
aea12f |
#include <string.h>
|
|
Packit |
aea12f |
#include <sys/types.h>
|
|
Packit |
aea12f |
#if !defined(_WIN32)
|
|
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 |
#endif
|
|
Packit |
aea12f |
#include <fcntl.h>
|
|
Packit |
aea12f |
#include <unistd.h>
|
|
Packit |
aea12f |
#include <assert.h>
|
|
Packit |
aea12f |
#include <gnutls/gnutls.h>
|
|
Packit |
aea12f |
#include <gnutls/x509.h>
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#include "utils.h"
|
|
Packit |
aea12f |
#include "cert-common.h"
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* Test for SSLKEYLOGFILE being functional.
|
|
Packit |
aea12f |
*
|
|
Packit |
aea12f |
*/
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void tls_log_func(int level, const char *str)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
fprintf(stderr, "<%d>| %s", level, str);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* In TLS 1.2, we only expect CLIENT_RANDOM. */
|
|
Packit |
aea12f |
static const char *tls12_included_labels[] = { "CLIENT_RANDOM", NULL };
|
|
Packit |
aea12f |
static const char *tls12_excluded_labels[] = { NULL };
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* In TLS 1.3, we expect secrets derived in handshake phases, but not
|
|
Packit |
aea12f |
* CLIENT_RANDOM. */
|
|
Packit |
aea12f |
static const char *tls13_included_labels[] = {
|
|
Packit |
aea12f |
"CLIENT_HANDSHAKE_TRAFFIC_SECRET",
|
|
Packit |
aea12f |
"SERVER_HANDSHAKE_TRAFFIC_SECRET",
|
|
Packit |
aea12f |
"CLIENT_TRAFFIC_SECRET_0",
|
|
Packit |
aea12f |
"SERVER_TRAFFIC_SECRET_0",
|
|
Packit |
aea12f |
"EXPORTER_SECRET",
|
|
Packit |
aea12f |
NULL
|
|
Packit |
aea12f |
};
|
|
Packit |
aea12f |
static const char *tls13_excluded_labels[] = { "CLIENT_RANDOM", NULL };
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void search_for_str(const char *filename, const char *label, bool excluded)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
char line[512];
|
|
Packit |
aea12f |
FILE *fp = fopen(filename, "r");
|
|
Packit |
aea12f |
char *p;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
while( (p = fgets(line, sizeof(line), fp)) != NULL) {
|
|
Packit |
aea12f |
success("%s", line);
|
|
Packit |
aea12f |
if (strncmp(line, label, strlen(label)) == 0 &&
|
|
Packit |
aea12f |
line[strlen(label)] == ' ') {
|
|
Packit |
aea12f |
fclose(fp);
|
|
Packit |
aea12f |
if (excluded)
|
|
Packit |
aea12f |
fail("file should not contain %s\n", label);
|
|
Packit |
aea12f |
return;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
fclose(fp);
|
|
Packit |
aea12f |
if (!excluded)
|
|
Packit |
aea12f |
fail("file should contain %s\n", label);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void run(const char *filename, const char *prio,
|
|
Packit |
aea12f |
const char **included, const char **excluded)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
gnutls_certificate_credentials_t x509_cred;
|
|
Packit |
aea12f |
gnutls_certificate_credentials_t clicred;
|
|
Packit |
aea12f |
const char **p;
|
|
Packit |
aea12f |
int ret;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#ifdef _WIN32
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
char buf[512];
|
|
Packit |
aea12f |
snprintf(buf, sizeof(buf), "SSLKEYLOGFILE=%s", filename);
|
|
Packit |
aea12f |
_putenv(buf);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
#else
|
|
Packit |
aea12f |
setenv("SSLKEYLOGFILE", filename, 1);
|
|
Packit |
aea12f |
#endif
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (debug) {
|
|
Packit |
aea12f |
gnutls_global_set_log_level(6);
|
|
Packit |
aea12f |
gnutls_global_set_log_function(tls_log_func);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* test gnutls_certificate_flags() */
|
|
Packit |
aea12f |
assert(gnutls_certificate_allocate_credentials(&x509_cred)>=0);
|
|
Packit |
aea12f |
assert(gnutls_certificate_allocate_credentials(&clicred) >= 0);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ret = gnutls_certificate_set_x509_key_mem(x509_cred, &server_ca3_localhost_cert_chain,
|
|
Packit |
aea12f |
&server_ca3_key,
|
|
Packit |
aea12f |
GNUTLS_X509_FMT_PEM);
|
|
Packit |
aea12f |
if (ret < 0) {
|
|
Packit |
aea12f |
fail("error in error code\n");
|
|
Packit |
aea12f |
exit(1);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ret = gnutls_certificate_set_x509_trust_mem(clicred, &ca3_cert, GNUTLS_X509_FMT_PEM);
|
|
Packit |
aea12f |
if (ret < 0)
|
|
Packit |
aea12f |
fail("set_x509_trust_file failed: %s\n", gnutls_strerror(ret));
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
test_cli_serv(x509_cred, clicred, prio, "localhost", NULL, NULL, NULL);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (access(filename, R_OK) != 0) {
|
|
Packit |
aea12f |
fail("keylog file was not created\n");
|
|
Packit |
aea12f |
exit(1);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
for (p = included; *p; p++)
|
|
Packit |
aea12f |
search_for_str(filename, *p, false);
|
|
Packit |
aea12f |
for (p = excluded; *p; p++)
|
|
Packit |
aea12f |
search_for_str(filename, *p, true);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_certificate_free_credentials(x509_cred);
|
|
Packit |
aea12f |
gnutls_certificate_free_credentials(clicred);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (debug)
|
|
Packit |
aea12f |
success("success");
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
void doit(void)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
char filename[TMPNAME_SIZE];
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
assert(get_tmpname(filename)!=NULL);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
remove(filename);
|
|
Packit |
aea12f |
global_init();
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
run(filename,
|
|
Packit |
aea12f |
"NONE:+VERS-TLS1.2:+AES-128-CBC:+SHA1:+SIGN-ALL:+COMP-NULL:+RSA",
|
|
Packit |
aea12f |
tls12_included_labels, tls12_excluded_labels);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* This is needed because the SSLKEYLOGFILE envvar is checked
|
|
Packit |
aea12f |
* only once and the file is never closed until the library is
|
|
Packit |
aea12f |
* unloaded. Truncate the file to zero length, so we can
|
|
Packit |
aea12f |
* reuse the same file for multiple tests. */
|
|
Packit |
aea12f |
truncate(filename, 0);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
run(filename,
|
|
Packit |
aea12f |
"NONE:+VERS-TLS1.3:+AES-256-GCM:+AEAD:+SIGN-ALL:+GROUP-ALL",
|
|
Packit |
aea12f |
tls13_included_labels, tls13_excluded_labels);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_global_deinit();
|
|
Packit |
aea12f |
remove(filename);
|
|
Packit |
aea12f |
}
|