|
Packit Service |
4684c1 |
/*
|
|
Packit Service |
4684c1 |
* Copyright (C) 2014-2016 Nikos Mavrogiannopoulos
|
|
Packit Service |
4684c1 |
* Copyright (C) 2016 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 General Public License
|
|
Packit Service |
4684c1 |
* along with GnuTLS; if not, write to the Free Software Foundation,
|
|
Packit Service |
4684c1 |
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* This test checks the behavior of gnutls_certificate_set_x509_key_file2()
|
|
Packit Service |
4684c1 |
* when the GNUTLS_CERTIFICATE_API_V2 is not set */
|
|
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 |
|
|
Packit Service |
4684c1 |
#include "cert-common.h"
|
|
Packit Service |
4684c1 |
#include "utils.h"
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static time_t mytime(time_t * t)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
time_t then = 1470002400;
|
|
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 void compare(const gnutls_datum_t *der, const void *ipem)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
gnutls_datum_t pem = {(void*)ipem, strlen((char*)ipem)};
|
|
Packit Service |
4684c1 |
gnutls_datum_t new_der;
|
|
Packit Service |
4684c1 |
int ret;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = gnutls_pem_base64_decode2("CERTIFICATE", &pem, &new_der);
|
|
Packit Service |
4684c1 |
if (ret < 0) {
|
|
Packit Service |
4684c1 |
fail("error: %s\n", gnutls_strerror(ret));
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (der->size != new_der.size || memcmp(der->data, new_der.data, der->size) != 0) {
|
|
Packit Service |
4684c1 |
fail("error in %d: %s\n", __LINE__, "cert don't match");
|
|
Packit Service |
4684c1 |
exit(1);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
gnutls_free(new_der.data);
|
|
Packit Service |
4684c1 |
return;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static unsigned set_cert(gnutls_certificate_credentials_t xcred, const gnutls_datum_t *key, const gnutls_datum_t *cert)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
const char *certfile;
|
|
Packit Service |
4684c1 |
FILE *fp;
|
|
Packit Service |
4684c1 |
int ret;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
certfile = get_tmpname(NULL);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
fp = fopen(certfile, "w");
|
|
Packit Service |
4684c1 |
if (fp == NULL)
|
|
Packit Service |
4684c1 |
fail("error in fopen\n");
|
|
Packit Service |
4684c1 |
assert(fwrite(cert->data, 1, cert->size, fp)>0);
|
|
Packit Service |
4684c1 |
assert(fwrite(key->data, 1, key->size, fp)>0);
|
|
Packit Service |
4684c1 |
fclose(fp);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = gnutls_certificate_set_x509_key_file2(xcred, certfile, certfile,
|
|
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 |
|
|
Packit Service |
4684c1 |
/* return index */
|
|
Packit Service |
4684c1 |
return ret;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static void verify_written_cert(gnutls_certificate_credentials_t xcred, unsigned idx, const gnutls_datum_t *cert, unsigned ncerts)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
int ret;
|
|
Packit Service |
4684c1 |
gnutls_datum_t tcert = {NULL, 0};
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* verify whether the stored certificate match the ones we have */
|
|
Packit Service |
4684c1 |
ret = gnutls_certificate_get_crt_raw(xcred, idx, 0, &tcert);
|
|
Packit Service |
4684c1 |
if (ret < 0) {
|
|
Packit Service |
4684c1 |
fail("error in %d: %s\n", __LINE__, gnutls_strerror(ret));
|
|
Packit Service |
4684c1 |
exit(1);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
compare(&tcert, cert->data);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (ncerts > 1) {
|
|
Packit Service |
4684c1 |
ret = gnutls_certificate_get_crt_raw(xcred, idx, 1, &tcert);
|
|
Packit Service |
4684c1 |
if (ret < 0) {
|
|
Packit Service |
4684c1 |
fail("error in %d: %s\n", __LINE__, gnutls_strerror(ret));
|
|
Packit Service |
4684c1 |
exit(1);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* skip headers of first cert */
|
|
Packit Service |
4684c1 |
compare(&tcert, cert->data+2);
|
|
Packit Service |
4684c1 |
}
|
|
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, clicred;
|
|
Packit Service |
4684c1 |
const char *keyfile = "./certs/ecc256.pem";
|
|
Packit Service |
4684c1 |
const char *certfile = "does-not-exist.pem";
|
|
Packit Service |
4684c1 |
unsigned idx, i;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
global_init();
|
|
Packit Service |
4684c1 |
assert(gnutls_certificate_allocate_credentials(&xcred) >= 0);
|
|
Packit Service |
4684c1 |
gnutls_global_set_time_function(mytime);
|
|
Packit Service |
4684c1 |
track_temp_files();
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* this will fail */
|
|
Packit Service |
4684c1 |
ret = gnutls_certificate_set_x509_key_file2(xcred, certfile, keyfile,
|
|
Packit Service |
4684c1 |
GNUTLS_X509_FMT_PEM, NULL, 0);
|
|
Packit Service |
4684c1 |
if (ret != GNUTLS_E_FILE_ERROR)
|
|
Packit Service |
4684c1 |
fail("set_x509_key_file failed: %s\n", gnutls_strerror(ret));
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_certificate_free_credentials(xcred);
|
|
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 |
ret = gnutls_certificate_set_x509_trust_mem(clicred, &subca3_cert, GNUTLS_X509_FMT_PEM);
|
|
Packit Service |
4684c1 |
if (ret < 0)
|
|
Packit Service |
4684c1 |
fail("set_x509_trust_file failed: %s\n", gnutls_strerror(ret));
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
success("Testing store of certificates\n");
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
idx = set_cert(xcred, &server_ca3_key, &server_ca3_localhost6_cert_chain);
|
|
Packit Service |
4684c1 |
verify_written_cert(xcred, idx, &server_ca3_localhost6_cert_chain, 2);
|
|
Packit Service |
4684c1 |
assert(idx == 0);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
success("Tested store of %d\n", idx);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
idx = set_cert(xcred, &server_ca3_key, &server_ca3_localhost_cert);
|
|
Packit Service |
4684c1 |
assert(idx == 0);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
success("Tested store of %d\n", idx);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
test_cli_serv(xcred, clicred, "NORMAL", "localhost", NULL, NULL, NULL); /* the DNS name of the first cert */
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
idx = set_cert(xcred, &server_key, &server_cert);
|
|
Packit Service |
4684c1 |
assert(idx == 0);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
success("Tested store of %d\n", idx);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
for (i=0;i<16;i++) {
|
|
Packit Service |
4684c1 |
idx = set_cert(xcred, &server_ecc_key, &server_ecc_cert);
|
|
Packit Service |
4684c1 |
assert(idx == 0);
|
|
Packit Service |
4684c1 |
success("Tested store of %d\n", idx);
|
|
Packit Service |
4684c1 |
}
|
|
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 |
delete_temp_files();
|
|
Packit Service |
4684c1 |
}
|