|
Packit |
aea12f |
/*
|
|
Packit |
aea12f |
* Copyright (C) 2016 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 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 |
#include <string.h>
|
|
Packit |
aea12f |
#include <unistd.h>
|
|
Packit |
aea12f |
#include <assert.h>
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#include <gnutls/gnutls.h>
|
|
Packit |
aea12f |
#include <gnutls/x509.h>
|
|
Packit |
aea12f |
#include <gnutls/x509-ext.h>
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#include "utils.h"
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* Tests the certificate extension override in "trusted" PKCS#11 modules
|
|
Packit |
aea12f |
* when used with gnutls_pkcs11_get_raw_issuer().
|
|
Packit |
aea12f |
*/
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#ifdef _WIN32
|
|
Packit |
aea12f |
# define P11LIB "libpkcs11mock1.dll"
|
|
Packit |
aea12f |
#else
|
|
Packit |
aea12f |
# define P11LIB "libpkcs11mock1.so"
|
|
Packit |
aea12f |
#endif
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
static time_t mytime(time_t * t)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
time_t then = 1424466893;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (t)
|
|
Packit |
aea12f |
*t = then;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
return then;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
void doit(void)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
int ret;
|
|
Packit |
aea12f |
gnutls_x509_crt_t crt, ocrt;
|
|
Packit |
aea12f |
unsigned keyusage;
|
|
Packit |
aea12f |
const char *lib;
|
|
Packit |
aea12f |
gnutls_datum_t issuer = {NULL, 0};
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ret = global_init();
|
|
Packit |
aea12f |
if (ret != 0) {
|
|
Packit |
aea12f |
fail("%d: %s\n", ret, gnutls_strerror(ret));
|
|
Packit |
aea12f |
exit(1);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
lib = getenv("P11MOCKLIB1");
|
|
Packit |
aea12f |
if (lib == NULL)
|
|
Packit |
aea12f |
lib = P11LIB;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_global_set_time_function(mytime);
|
|
Packit |
aea12f |
if (debug) {
|
|
Packit |
aea12f |
gnutls_global_set_log_level(4711);
|
|
Packit |
aea12f |
success("loading lib %s\n", lib);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ret = gnutls_pkcs11_init(GNUTLS_PKCS11_FLAG_MANUAL, NULL);
|
|
Packit |
aea12f |
if (ret != 0) {
|
|
Packit |
aea12f |
fail("%d: %s\n", ret, gnutls_strerror(ret));
|
|
Packit |
aea12f |
exit(1);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ret = gnutls_pkcs11_add_provider(lib, "trusted");
|
|
Packit |
aea12f |
if (ret != 0) {
|
|
Packit |
aea12f |
fail("%d: %s\n", ret, gnutls_strerror(ret));
|
|
Packit |
aea12f |
exit(1);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
assert(gnutls_x509_crt_init(&crt)>=0);
|
|
Packit |
aea12f |
assert(gnutls_x509_crt_init(&ocrt)>=0);
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* check high level certificate functions */
|
|
Packit |
aea12f |
ret = gnutls_x509_crt_import_url(crt, "pkcs11:type=cert;object=cert1", 0);
|
|
Packit |
aea12f |
if (ret < 0) {
|
|
Packit |
aea12f |
fail("%d: %s\n", ret, gnutls_strerror(ret));
|
|
Packit |
aea12f |
exit(1);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ret = gnutls_pkcs11_get_raw_issuer("pkcs11:", crt, &issuer, GNUTLS_X509_FMT_DER, GNUTLS_PKCS11_OBJ_FLAG_OVERWRITE_TRUSTMOD_EXT);
|
|
Packit |
aea12f |
if (ret < 0) {
|
|
Packit |
aea12f |
fail("%d: %s\n", ret, gnutls_strerror(ret));
|
|
Packit |
aea12f |
exit(1);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ret = gnutls_x509_crt_equals2(crt, &issuer);
|
|
Packit |
aea12f |
if (ret != 0) {
|
|
Packit |
aea12f |
fail("exported certificates are equal!\n");
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ret = gnutls_x509_crt_import(ocrt, &issuer, GNUTLS_X509_FMT_DER);
|
|
Packit |
aea12f |
if (ret < 0) {
|
|
Packit |
aea12f |
fail("%d: %s\n", ret, gnutls_strerror(ret));
|
|
Packit |
aea12f |
exit(1);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ret = gnutls_x509_crt_get_ca_status(ocrt, NULL);
|
|
Packit |
aea12f |
if (ret < 0) {
|
|
Packit |
aea12f |
fail("%d: %s\n", ret, gnutls_strerror(ret));
|
|
Packit |
aea12f |
exit(1);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (ret == 0) {
|
|
Packit |
aea12f |
fail("overridden cert is not a CA!\n");
|
|
Packit |
aea12f |
exit(1);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
ret = gnutls_x509_crt_get_key_usage(ocrt, &keyusage, NULL);
|
|
Packit |
aea12f |
if (ret < 0) {
|
|
Packit |
aea12f |
fail("%d: %s\n", ret, gnutls_strerror(ret));
|
|
Packit |
aea12f |
exit(1);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
if (keyusage != (GNUTLS_KEY_KEY_ENCIPHERMENT|GNUTLS_KEY_ENCIPHER_ONLY|GNUTLS_KEY_KEY_CERT_SIGN)) {
|
|
Packit |
aea12f |
fail("Extension does not have the expected key usage!\n");
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_x509_crt_deinit(crt);
|
|
Packit |
aea12f |
gnutls_x509_crt_deinit(ocrt);
|
|
Packit |
aea12f |
gnutls_free(issuer.data);
|
|
Packit |
aea12f |
if (debug)
|
|
Packit |
aea12f |
printf("done\n\n\n");
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
gnutls_global_deinit();
|
|
Packit |
aea12f |
}
|