|
Packit Service |
4684c1 |
/*
|
|
Packit Service |
4684c1 |
* Copyright (C) 2006-2012 Free Software Foundation, Inc.
|
|
Packit Service |
4684c1 |
* Copyright (C) 2016 Red Hat, Inc.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Author: Simon Josefsson, 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 |
#ifdef HAVE_CONFIG_H
|
|
Packit Service |
4684c1 |
#include "config.h"
|
|
Packit Service |
4684c1 |
#endif
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#include <gnutls/gnutls.h>
|
|
Packit Service |
4684c1 |
#include <gnutls/x509.h>
|
|
Packit Service |
4684c1 |
#include <stdlib.h>
|
|
Packit Service |
4684c1 |
#include <limits.h>
|
|
Packit Service |
4684c1 |
#include <dirent.h>
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#include "utils.h"
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* This program will load certificates from CERT_DIR and try to print
|
|
Packit Service |
4684c1 |
* them. If CERT_DIR/certname.err is available, it should contain the
|
|
Packit Service |
4684c1 |
* error code that gnutls_x509_crt_import() should return.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#define CERT_DIR "certs-interesting"
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static int getnextcert(DIR **dirp, gnutls_datum_t *der, int *exp_ret)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
struct dirent *d;
|
|
Packit Service |
4684c1 |
char path[256];
|
|
Packit Service |
4684c1 |
char cert_dir[256];
|
|
Packit Service |
4684c1 |
const char *src;
|
|
Packit Service |
4684c1 |
int ret;
|
|
Packit Service |
4684c1 |
gnutls_datum_t local;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
src = getenv("srcdir");
|
|
Packit Service |
4684c1 |
if (src == NULL)
|
|
Packit Service |
4684c1 |
src = ".";
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
snprintf(cert_dir, sizeof(cert_dir), "%s/%s", src, CERT_DIR);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (*dirp == NULL) {
|
|
Packit Service |
4684c1 |
*dirp = opendir(cert_dir);
|
|
Packit Service |
4684c1 |
if (*dirp == NULL)
|
|
Packit Service |
4684c1 |
return -1;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
do {
|
|
Packit Service |
4684c1 |
d = readdir(*dirp);
|
|
Packit Service |
4684c1 |
if (d != NULL
|
|
Packit Service |
4684c1 |
#ifdef _DIRENT_HAVE_D_TYPE
|
|
Packit Service |
4684c1 |
&& d->d_type == DT_REG
|
|
Packit Service |
4684c1 |
#endif
|
|
Packit Service |
4684c1 |
) {
|
|
Packit Service |
4684c1 |
if (strstr(d->d_name, ".der") == 0)
|
|
Packit Service |
4684c1 |
continue;
|
|
Packit Service |
4684c1 |
if (strstr(d->d_name, ".err") != 0)
|
|
Packit Service |
4684c1 |
continue;
|
|
Packit Service |
4684c1 |
snprintf(path, sizeof(path), "%s/%s", cert_dir, d->d_name);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
success("Loading %s\n", path);
|
|
Packit Service |
4684c1 |
ret = gnutls_load_file(path, der);
|
|
Packit Service |
4684c1 |
if (ret < 0) {
|
|
Packit Service |
4684c1 |
return -1;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
snprintf(path, sizeof(path), "%s/%s.err", cert_dir, d->d_name);
|
|
Packit Service |
4684c1 |
success("Loading errfile %s\n", path);
|
|
Packit Service |
4684c1 |
ret = gnutls_load_file(path, &local);
|
|
Packit Service |
4684c1 |
if (ret < 0) { /* not found assume success */
|
|
Packit Service |
4684c1 |
*exp_ret = 0;
|
|
Packit Service |
4684c1 |
} else {
|
|
Packit Service |
4684c1 |
*exp_ret = atoi((char*)local.data);
|
|
Packit Service |
4684c1 |
success("expecting error code %d\n", *exp_ret);
|
|
Packit Service |
4684c1 |
gnutls_free(local.data);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return 0;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
} while(d != NULL);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
closedir(*dirp);
|
|
Packit Service |
4684c1 |
return -1; /* finished */
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
void doit(void)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
int ret, exp_ret;
|
|
Packit Service |
4684c1 |
gnutls_x509_crt_t cert;
|
|
Packit Service |
4684c1 |
gnutls_datum_t der;
|
|
Packit Service |
4684c1 |
DIR *dirp = NULL;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = global_init();
|
|
Packit Service |
4684c1 |
if (ret < 0)
|
|
Packit Service |
4684c1 |
fail("init %d\n", ret);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
while (getnextcert(&dirp, &der, &exp_ret)==0) {
|
|
Packit Service |
4684c1 |
ret = gnutls_x509_crt_init(&cert);
|
|
Packit Service |
4684c1 |
if (ret < 0)
|
|
Packit Service |
4684c1 |
fail("crt_init %d\n", ret);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_x509_crt_set_flags(cert, GNUTLS_X509_CRT_FLAG_IGNORE_SANITY);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = gnutls_x509_crt_import(cert, &der, GNUTLS_X509_FMT_DER);
|
|
Packit Service |
4684c1 |
if (ret != exp_ret) {
|
|
Packit Service |
4684c1 |
fail("crt_import %s\n", gnutls_strerror(ret));
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (ret == 0) {
|
|
Packit Service |
4684c1 |
/* attempt to fully decode */
|
|
Packit Service |
4684c1 |
gnutls_datum_t out;
|
|
Packit Service |
4684c1 |
ret = gnutls_x509_crt_print(cert, GNUTLS_CRT_PRINT_FULL, &out;;
|
|
Packit Service |
4684c1 |
if (ret < 0) {
|
|
Packit Service |
4684c1 |
fail("print: %s\n", gnutls_strerror(ret));
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
gnutls_free(out.data);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_x509_crt_deinit(cert);
|
|
Packit Service |
4684c1 |
gnutls_free(der.data);
|
|
Packit Service |
4684c1 |
der.size = 0;
|
|
Packit Service |
4684c1 |
exp_ret = -1;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_global_deinit();
|
|
Packit Service |
4684c1 |
}
|