|
Packit Service |
4684c1 |
/*
|
|
Packit Service |
4684c1 |
* Copyright (C) 2016-2017 Red Hat, Inc.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Author: Nikos Mavrogiannopoulos
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This file is part of GnuTLS.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* GnuTLS is free software; you can redistribute it and/or modify it
|
|
Packit Service |
4684c1 |
* under the terms of the GNU General Public License as published by
|
|
Packit Service |
4684c1 |
* the Free Software Foundation; either version 3 of the License, or
|
|
Packit Service |
4684c1 |
* (at your option) any later version.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* GnuTLS is distributed in the hope that it will be useful, but
|
|
Packit Service |
4684c1 |
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
4684c1 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit Service |
4684c1 |
* General Public License for more details.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* You should have received a copy of the GNU Lesser General Public License
|
|
Packit Service |
4684c1 |
* along with this program. If not, see <https://www.gnu.org/licenses/>
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#ifdef HAVE_CONFIG_H
|
|
Packit Service |
4684c1 |
#include <config.h>
|
|
Packit Service |
4684c1 |
#endif
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#include <stdio.h>
|
|
Packit Service |
4684c1 |
#include <stdlib.h>
|
|
Packit Service |
4684c1 |
#include <assert.h>
|
|
Packit Service |
4684c1 |
#include <gnutls/gnutls.h>
|
|
Packit Service |
4684c1 |
#include <gnutls/x509.h>
|
|
Packit Service |
4684c1 |
#include <assert.h>
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#ifdef ENABLE_OCSP
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#include "ocsp-common.h"
|
|
Packit Service |
4684c1 |
#include "cert-common.h"
|
|
Packit Service |
4684c1 |
#include "utils.h"
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* Tests whether we can send and receive multiple OCSP responses
|
|
Packit Service |
4684c1 |
* one for each certificate in a chain under TLS 1.3.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static time_t mytime(time_t * t)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
time_t then = 1469186559;
|
|
Packit Service |
4684c1 |
if (t)
|
|
Packit Service |
4684c1 |
*t = then;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return then;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static const gnutls_datum_t ocsp_resp_localhost[] = {
|
|
Packit Service |
4684c1 |
{ (void*)_ocsp_ca3_localhost_unknown, sizeof(_ocsp_ca3_localhost_unknown) },
|
|
Packit Service |
4684c1 |
{ NULL, 0}};
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static const gnutls_datum_t ocsp_resp_localhost6[] = {
|
|
Packit Service |
4684c1 |
{ (void*)_ocsp_ca3_localhost6_unknown, sizeof(_ocsp_ca3_localhost6_unknown) },
|
|
Packit Service |
4684c1 |
{ (void*)_ocsp_subca3_unknown, sizeof(_ocsp_subca3_unknown) }};
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
typedef struct ctx_st {
|
|
Packit Service |
4684c1 |
const char *name;
|
|
Packit Service |
4684c1 |
const gnutls_datum_t *ocsp;
|
|
Packit Service |
4684c1 |
unsigned nocsp;
|
|
Packit Service |
4684c1 |
} ctx_st;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static ctx_st test_localhost = {"single response", ocsp_resp_localhost, 1};
|
|
Packit Service |
4684c1 |
static ctx_st test_localhost6 = {"two responses", ocsp_resp_localhost6, 2};
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#define myfail(fmt, ...) \
|
|
Packit Service |
4684c1 |
fail("%s: "fmt, test->name, ##__VA_ARGS__)
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static void check_response(gnutls_session_t session, void *priv)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
int ret;
|
|
Packit Service |
4684c1 |
gnutls_datum_t resp;
|
|
Packit Service |
4684c1 |
ctx_st *test = priv;
|
|
Packit Service |
4684c1 |
unsigned i;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
assert(test != NULL);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
for (i=0;;i++) {
|
|
Packit Service |
4684c1 |
ret = gnutls_ocsp_status_request_get2(session, i, &resp);
|
|
Packit Service |
4684c1 |
if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
|
|
Packit Service |
4684c1 |
break;
|
|
Packit Service |
4684c1 |
if (ret < 0) {
|
|
Packit Service |
4684c1 |
if (test->ocsp[i].size == 0)
|
|
Packit Service |
4684c1 |
return;
|
|
Packit Service |
4684c1 |
myfail("no response was received\n");
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (test->ocsp[i].size == 0) {
|
|
Packit Service |
4684c1 |
myfail("not expected response, but received one\n");
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (resp.size != test->ocsp[i].size) {
|
|
Packit Service |
4684c1 |
myfail("did not receive the expected response size for %d\n", i);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (memcmp(resp.data, test->ocsp[i].data, resp.size) != 0) {
|
|
Packit Service |
4684c1 |
myfail("did not receive the expected response for %d\n", i);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (i != test->nocsp) {
|
|
Packit Service |
4684c1 |
myfail("The number of OCSP responses received (%d) does not match the expected (%d)\n", i, test->nocsp);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static void tls_log_func(int level, const char *str)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
fprintf(stderr, "|<%d>| %s", level, str);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
void doit(void)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
int ret;
|
|
Packit Service |
4684c1 |
gnutls_certificate_credentials_t xcred;
|
|
Packit Service |
4684c1 |
gnutls_certificate_credentials_t clicred;
|
|
Packit Service |
4684c1 |
const char *certfile1;
|
|
Packit Service |
4684c1 |
const char *certfile2;
|
|
Packit Service |
4684c1 |
char certname1[TMPNAME_SIZE];
|
|
Packit Service |
4684c1 |
char certname2[TMPNAME_SIZE];
|
|
Packit Service |
4684c1 |
FILE *fp;
|
|
Packit Service |
4684c1 |
unsigned index1, index2; /* indexes of certs */
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
global_init();
|
|
Packit Service |
4684c1 |
gnutls_global_set_time_function(mytime);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_global_set_log_function(tls_log_func);
|
|
Packit Service |
4684c1 |
if (debug)
|
|
Packit Service |
4684c1 |
gnutls_global_set_log_level(4711);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
assert(gnutls_certificate_allocate_credentials(&xcred) >= 0);
|
|
Packit Service |
4684c1 |
assert(gnutls_certificate_allocate_credentials(&clicred) >= 0);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_certificate_set_flags(xcred, GNUTLS_CERTIFICATE_API_V2);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* set cert with localhost name */
|
|
Packit Service |
4684c1 |
certfile1 = get_tmpname(certname1);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
fp = fopen(certfile1, "wb");
|
|
Packit Service |
4684c1 |
if (fp == NULL)
|
|
Packit Service |
4684c1 |
fail("error in fopen\n");
|
|
Packit Service |
4684c1 |
assert(fwrite(server_localhost_ca3_cert_chain_pem, 1, strlen(server_localhost_ca3_cert_chain_pem), fp)>0);
|
|
Packit Service |
4684c1 |
assert(fwrite(server_ca3_key_pem, 1, strlen((char*)server_ca3_key_pem), fp)>0);
|
|
Packit Service |
4684c1 |
fclose(fp);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = gnutls_certificate_set_x509_key_file2(xcred, certfile1, certfile1,
|
|
Packit Service |
4684c1 |
GNUTLS_X509_FMT_PEM, NULL, 0);
|
|
Packit Service |
4684c1 |
if (ret < 0)
|
|
Packit Service |
4684c1 |
fail("set_x509_key_file failed: %s\n", gnutls_strerror(ret));
|
|
Packit Service |
4684c1 |
index1 = ret;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* set cert with localhost6 name */
|
|
Packit Service |
4684c1 |
certfile2 = get_tmpname(certname2);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
fp = fopen(certfile2, "wb");
|
|
Packit Service |
4684c1 |
if (fp == NULL)
|
|
Packit Service |
4684c1 |
fail("error in fopen\n");
|
|
Packit Service |
4684c1 |
assert(fwrite(server_localhost6_ca3_cert_chain_pem, 1, strlen(server_localhost6_ca3_cert_chain_pem), fp)>0);
|
|
Packit Service |
4684c1 |
assert(fwrite(server_ca3_key_pem, 1, strlen((char*)server_ca3_key_pem), fp)>0);
|
|
Packit Service |
4684c1 |
fclose(fp);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = gnutls_certificate_set_x509_key_file2(xcred, certfile2, certfile2,
|
|
Packit Service |
4684c1 |
GNUTLS_X509_FMT_PEM, NULL, 0);
|
|
Packit Service |
4684c1 |
if (ret < 0)
|
|
Packit Service |
4684c1 |
fail("set_x509_key_file failed: %s\n", gnutls_strerror(ret));
|
|
Packit Service |
4684c1 |
index2 = ret;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* set OCSP response1 */
|
|
Packit Service |
4684c1 |
ret = gnutls_certificate_set_ocsp_status_request_mem(xcred, &test_localhost.ocsp[0], index1, GNUTLS_X509_FMT_DER);
|
|
Packit Service |
4684c1 |
if (ret < 0)
|
|
Packit Service |
4684c1 |
fail("ocsp file set failed: %s\n", gnutls_strerror(ret));
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* set OCSP response2 */
|
|
Packit Service |
4684c1 |
ret = gnutls_certificate_set_ocsp_status_request_mem(xcred, &test_localhost6.ocsp[0], index2, GNUTLS_X509_FMT_DER);
|
|
Packit Service |
4684c1 |
if (ret < 0)
|
|
Packit Service |
4684c1 |
fail("ocsp file set failed: %s\n", gnutls_strerror(ret));
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = gnutls_certificate_set_ocsp_status_request_mem(xcred, &test_localhost6.ocsp[1], index2, GNUTLS_X509_FMT_DER);
|
|
Packit Service |
4684c1 |
if (ret < 0)
|
|
Packit Service |
4684c1 |
fail("ocsp file set failed: %s\n", gnutls_strerror(ret));
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* make sure that our invalid OCSP responses are not considered in verification
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
gnutls_certificate_set_verify_flags(clicred, GNUTLS_VERIFY_DISABLE_CRL_CHECKS);
|
|
Packit Service |
4684c1 |
if (gnutls_certificate_get_verify_flags(clicred) != GNUTLS_VERIFY_DISABLE_CRL_CHECKS)
|
|
Packit Service |
4684c1 |
fail("error in gnutls_certificate_set_verify_flags\n");
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = gnutls_certificate_set_x509_trust_mem(clicred, &ca3_cert, GNUTLS_X509_FMT_PEM);
|
|
Packit Service |
4684c1 |
if (ret < 0) {
|
|
Packit Service |
4684c1 |
fail("error in setting trust cert: %s\n", gnutls_strerror(ret));
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
test_cli_serv(xcred, clicred, "NORMAL:-VERS-TLS-ALL:+VERS-TLS1.3", "localhost", &test_localhost, check_response, NULL);
|
|
Packit Service |
4684c1 |
test_cli_serv(xcred, clicred, "NORMAL:-VERS-TLS-ALL:+VERS-TLS1.3", "localhost6", &test_localhost6, check_response, NULL);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_certificate_free_credentials(xcred);
|
|
Packit Service |
4684c1 |
gnutls_certificate_free_credentials(clicred);
|
|
Packit Service |
4684c1 |
gnutls_global_deinit();
|
|
Packit Service |
4684c1 |
remove(certfile1);
|
|
Packit Service |
4684c1 |
remove(certfile2);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#else
|
|
Packit Service |
4684c1 |
void doit(void)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
exit(77);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
#endif
|