|
Packit |
aea12f |
/*
|
|
Packit |
aea12f |
* Copyright (C) 2016-2017 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 |
#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 |
#include <assert.h>
|
|
Packit |
aea12f |
#include <gnutls/gnutls.h>
|
|
Packit |
aea12f |
#include <gnutls/x509.h>
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#ifdef ENABLE_OCSP
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#include "cert-common.h"
|
|
Packit |
aea12f |
#include "ocsp-common.h"
|
|
Packit |
aea12f |
#include "utils.h"
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* Tests whether setting an OCSP response to a server with multiple
|
|
Packit |
aea12f |
* certificate sets, is working as expected */
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static time_t mytime(time_t * t)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
time_t then = OCSP_RESP_DATE;
|
|
Packit |
aea12f |
if (t)
|
|
Packit |
aea12f |
*t = then;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
return then;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static void check_response(gnutls_session_t session, void *priv)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
int ret;
|
|
Packit |
aea12f |
gnutls_datum_t resp;
|
|
Packit |
aea12f |
gnutls_datum_t *exp_resp = priv;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ret = gnutls_ocsp_status_request_get(session, &resp);
|
|
Packit |
aea12f |
if (ret < 0) {
|
|
Packit |
aea12f |
if (priv == NULL)
|
|
Packit |
aea12f |
return;
|
|
Packit |
aea12f |
fail("no response was received\n");
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (priv == NULL) {
|
|
Packit |
aea12f |
fail("not expected response, but received one\n");
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (resp.size != exp_resp->size || memcmp(resp.data, exp_resp->data, resp.size) != 0) {
|
|
Packit |
aea12f |
fail("did not receive the expected response\n");
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* Check intermediate response */
|
|
Packit |
aea12f |
if (gnutls_protocol_get_version(session) == GNUTLS_TLS1_3) {
|
|
Packit |
aea12f |
ret = gnutls_ocsp_status_request_get2(session, 1, &resp);
|
|
Packit |
aea12f |
if (ret < 0) {
|
|
Packit |
aea12f |
fail("no intermediate response was received\n");
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (resp.size != ocsp_subca3_unknown.size || memcmp(resp.data, ocsp_subca3_unknown.data, resp.size) != 0) {
|
|
Packit |
aea12f |
fail("did not receive the expected intermediate response\n");
|
|
Packit |
aea12f |
}
|
|
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 |
void doit(void)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
int ret;
|
|
Packit |
aea12f |
gnutls_certificate_credentials_t xcred;
|
|
Packit |
aea12f |
gnutls_certificate_credentials_t clicred;
|
|
Packit |
aea12f |
const char *certfile1;
|
|
Packit |
aea12f |
const char *certfile2;
|
|
Packit |
aea12f |
const char *ocspfile1;
|
|
Packit |
aea12f |
const char *ocspfile2;
|
|
Packit |
aea12f |
const char *ocspfile3;
|
|
Packit |
aea12f |
char certname1[TMPNAME_SIZE], ocspname1[TMPNAME_SIZE];
|
|
Packit |
aea12f |
char certname2[TMPNAME_SIZE], ocspname2[TMPNAME_SIZE];
|
|
Packit |
aea12f |
char ocspname3[TMPNAME_SIZE];
|
|
Packit |
aea12f |
FILE *fp;
|
|
Packit |
aea12f |
unsigned index1, index2; /* indexes of certs */
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
global_init();
|
|
Packit |
aea12f |
gnutls_global_set_time_function(mytime);
|
|
Packit |
aea12f |
|
|
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 |
assert(gnutls_certificate_allocate_credentials(&xcred) >= 0);
|
|
Packit |
aea12f |
assert(gnutls_certificate_allocate_credentials(&clicred) >= 0);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_certificate_set_flags(xcred, GNUTLS_CERTIFICATE_API_V2);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* set cert with localhost name */
|
|
Packit |
aea12f |
certfile1 = get_tmpname(certname1);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
fp = fopen(certfile1, "wb");
|
|
Packit |
aea12f |
if (fp == NULL)
|
|
Packit |
aea12f |
fail("error in fopen\n");
|
|
Packit |
aea12f |
assert(fwrite(server_localhost_ca3_cert_chain_pem, 1, strlen(server_localhost_ca3_cert_chain_pem), fp)>0);
|
|
Packit |
aea12f |
assert(fwrite(server_ca3_key_pem, 1, strlen((char*)server_ca3_key_pem), fp)>0);
|
|
Packit |
aea12f |
fclose(fp);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ret = gnutls_certificate_set_x509_key_file2(xcred, certfile1, certfile1,
|
|
Packit |
aea12f |
GNUTLS_X509_FMT_PEM, NULL, 0);
|
|
Packit |
aea12f |
if (ret < 0)
|
|
Packit |
aea12f |
fail("set_x509_key_file failed: %s\n", gnutls_strerror(ret));
|
|
Packit |
aea12f |
index1 = ret;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* set cert with localhost6 name */
|
|
Packit |
aea12f |
certfile2 = get_tmpname(certname2);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
fp = fopen(certfile2, "wb");
|
|
Packit |
aea12f |
if (fp == NULL)
|
|
Packit |
aea12f |
fail("error in fopen\n");
|
|
Packit |
aea12f |
assert(fwrite(server_localhost6_ca3_cert_chain_pem, 1, strlen(server_localhost6_ca3_cert_chain_pem), fp)>0);
|
|
Packit |
aea12f |
assert(fwrite(server_ca3_key_pem, 1, strlen((char*)server_ca3_key_pem), fp)>0);
|
|
Packit |
aea12f |
fclose(fp);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ret = gnutls_certificate_set_x509_key_file2(xcred, certfile2, certfile2,
|
|
Packit |
aea12f |
GNUTLS_X509_FMT_PEM, NULL, 0);
|
|
Packit |
aea12f |
if (ret < 0)
|
|
Packit |
aea12f |
fail("set_x509_key_file failed: %s\n", gnutls_strerror(ret));
|
|
Packit |
aea12f |
index2 = ret;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* set OCSP response1 */
|
|
Packit |
aea12f |
ocspfile1 = get_tmpname(ocspname1);
|
|
Packit |
aea12f |
fp = fopen(ocspfile1, "wb");
|
|
Packit |
aea12f |
if (fp == NULL)
|
|
Packit |
aea12f |
fail("error in fopen\n");
|
|
Packit |
aea12f |
assert(fwrite(ocsp_ca3_localhost_unknown.data, 1, ocsp_ca3_localhost_unknown.size, fp)>0);
|
|
Packit |
aea12f |
fclose(fp);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ret = gnutls_certificate_set_ocsp_status_request_file(xcred, ocspfile1, index1);
|
|
Packit |
aea12f |
if (ret < 0)
|
|
Packit |
aea12f |
fail("ocsp file set failed: %s\n", gnutls_strerror(ret));
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* set OCSP response2 */
|
|
Packit |
aea12f |
ocspfile2 = get_tmpname(ocspname2);
|
|
Packit |
aea12f |
fp = fopen(ocspfile2, "wb");
|
|
Packit |
aea12f |
if (fp == NULL)
|
|
Packit |
aea12f |
fail("error in fopen\n");
|
|
Packit |
aea12f |
assert(fwrite(ocsp_ca3_localhost6_unknown.data, 1, ocsp_ca3_localhost6_unknown.size, fp)>0);
|
|
Packit |
aea12f |
fclose(fp);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ret = gnutls_certificate_set_ocsp_status_request_file(xcred, ocspfile2, index2);
|
|
Packit |
aea12f |
if (ret < 0)
|
|
Packit |
aea12f |
fail("ocsp file set failed: %s\n", gnutls_strerror(ret));
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* try to set a duplicate OCSP response */
|
|
Packit |
aea12f |
ocspfile3 = get_tmpname(ocspname3);
|
|
Packit |
aea12f |
fp = fopen(ocspfile3, "wb");
|
|
Packit |
aea12f |
if (fp == NULL)
|
|
Packit |
aea12f |
fail("error in fopen\n");
|
|
Packit |
aea12f |
assert(fwrite(ocsp_ca3_localhost_unknown_sha1.data, 1, ocsp_ca3_localhost_unknown_sha1.size, fp)>0);
|
|
Packit |
aea12f |
fclose(fp);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ret = gnutls_certificate_set_ocsp_status_request_file(xcred, ocspfile3, index1);
|
|
Packit |
aea12f |
if (ret != 0)
|
|
Packit |
aea12f |
fail("setting duplicate didn't succeed as expected: %s\n", gnutls_strerror(ret));
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ret = gnutls_certificate_set_ocsp_status_request_file(xcred, ocspfile3, index2);
|
|
Packit |
aea12f |
if (ret != GNUTLS_E_OCSP_MISMATCH_WITH_CERTS)
|
|
Packit |
aea12f |
fail("setting invalid didn't fail as expected: %s\n", gnutls_strerror(ret));
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* re-set the previous duplicate set for index1 to the expected*/
|
|
Packit |
aea12f |
ret = gnutls_certificate_set_ocsp_status_request_file(xcred, ocspfile1, index1);
|
|
Packit |
aea12f |
if (ret < 0)
|
|
Packit |
aea12f |
fail("ocsp file set failed: %s\n", gnutls_strerror(ret));
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* set an intermediate CA OCSP response */
|
|
Packit |
aea12f |
fp = fopen(ocspfile3, "wb");
|
|
Packit |
aea12f |
if (fp == NULL)
|
|
Packit |
aea12f |
fail("error in fopen\n");
|
|
Packit |
aea12f |
assert(fwrite(ocsp_subca3_unknown.data, 1, ocsp_subca3_unknown.size, fp)>0);
|
|
Packit |
aea12f |
fclose(fp);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ret = gnutls_certificate_set_ocsp_status_request_file(xcred, ocspfile3, index1);
|
|
Packit |
aea12f |
if (ret < 0)
|
|
Packit |
aea12f |
fail("setting subCA failed: %s\n", gnutls_strerror(ret));
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ret = gnutls_certificate_set_ocsp_status_request_file(xcred, ocspfile3, index2);
|
|
Packit |
aea12f |
if (ret < 0)
|
|
Packit |
aea12f |
fail("setting subCA failed: %s\n", gnutls_strerror(ret));
|
|
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("error in setting trust cert: %s\n", gnutls_strerror(ret));
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
test_cli_serv(xcred, clicred, "NORMAL:-ECDHE-ECDSA:-VERS-TLS-ALL:+VERS-TLS1.2", "localhost", &ocsp_ca3_localhost_unknown, check_response, NULL);
|
|
Packit |
aea12f |
test_cli_serv(xcred, clicred, "NORMAL:-ECDHE-ECDSA:-VERS-TLS-ALL:+VERS-TLS1.2", "localhost6", &ocsp_ca3_localhost6_unknown, check_response, NULL);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
test_cli_serv(xcred, clicred, "NORMAL:-ECDHE-ECDSA:-VERS-TLS-ALL:+VERS-TLS1.3", "localhost", &ocsp_ca3_localhost_unknown, check_response, NULL);
|
|
Packit |
aea12f |
test_cli_serv(xcred, clicred, "NORMAL:-ECDHE-ECDSA:-VERS-TLS-ALL:+VERS-TLS1.3", "localhost6", &ocsp_ca3_localhost6_unknown, check_response, NULL);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_certificate_free_credentials(xcred);
|
|
Packit |
aea12f |
gnutls_certificate_free_credentials(clicred);
|
|
Packit |
aea12f |
gnutls_global_deinit();
|
|
Packit |
aea12f |
remove(ocspfile1);
|
|
Packit |
aea12f |
remove(ocspfile2);
|
|
Packit |
aea12f |
remove(ocspfile3);
|
|
Packit |
aea12f |
remove(certfile1);
|
|
Packit |
aea12f |
remove(certfile2);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#else
|
|
Packit |
aea12f |
void doit(void)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
exit(77);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
#endif
|