Skip to content

Commit de8ce4a

Browse files
committed
x509storeissuer: add certificates from the provided directories to the store
Signed-off-by: Eugene Syromiatnikov <[email protected]>
1 parent 4307499 commit de8ce4a

File tree

1 file changed

+83
-4
lines changed

1 file changed

+83
-4
lines changed

source/x509storeissuer.c

Lines changed: 83 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
* https://www.openssl.org/source/license.html
88
*/
99

10+
#include <dirent.h>
11+
#include <errno.h>
1012
#include <stdbool.h>
1113
#include <stdio.h>
1214
#include <stdlib.h>
1315
#include <string.h>
16+
#include <sys/stat.h>
1417
#ifndef _WIN32
1518
# include <libgen.h>
1619
# include <unistd.h>
@@ -254,11 +257,10 @@ main(int argc, char *argv[])
254257
size_t total_count = 0;
255258
size_t total_found = 0;
256259
double avcalltime;
257-
char *cert = NULL;
258260
int ret = EXIT_FAILURE;
259-
BIO *bio = NULL;
260261
int opt;
261262
int dirs_start;
263+
size_t num_certs = 0;
262264
struct nonce_cfg nonce_cfg;
263265

264266
parse_nonce_cfg(NONCE_CFG, &nonce_cfg);
@@ -309,6 +311,85 @@ main(int argc, char *argv[])
309311
if (store == NULL || !X509_STORE_set_default_paths(store))
310312
errx(EXIT_FAILURE, "Failed to create X509_STORE");
311313

314+
for (int i = dirs_start; i < argc - 1; i++) {
315+
char *cert = NULL;
316+
BIO *bio = NULL;
317+
X509 *x509 = NULL;
318+
struct stat st;
319+
struct dirent *e;
320+
DIR *d = opendir(argv[i]);
321+
322+
if (d == NULL)
323+
err(EXIT_FAILURE, "Could not open \"%s\"", argv[i]);
324+
325+
while (1) {
326+
errno = 0;
327+
e = readdir(d);
328+
329+
if (e == NULL) {
330+
if (errno != 0) {
331+
err(EXIT_FAILURE, "An error ocurred while reading directory"
332+
" \"%s\"", argv[i]);
333+
} else {
334+
break;
335+
}
336+
}
337+
338+
cert = perflib_mk_file_path(argv[i], e->d_name);
339+
if (cert == NULL)
340+
errx(EXIT_FAILURE, "Failed to allocate cert name in directory"
341+
" \"%s\" for entry \"%s\"",
342+
argv[i], e->d_name);
343+
344+
if (lstat(cert, &st) < 0) {
345+
warn("Got error on lstat(\"%s\")", cert);
346+
goto next_file;
347+
}
348+
349+
if (st.st_mode & S_IFMT != S_IFREG) {
350+
if (verbosity >= VERBOSITY_DEBUG)
351+
warnx("\"%s\" is not a regular file, skipping", cert);
352+
goto next_file;
353+
}
354+
355+
bio = BIO_new_file(cert, "rb");
356+
if (bio == NULL)
357+
errx(EXIT_FAILURE, "Unable to create BIO for \"%s\"", cert);
358+
359+
x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
360+
if (x509 == NULL) {
361+
if (verbosity >= VERBOSITY_DEBUG)
362+
warnx("Failed to read certificate from \"%s\", skipping",
363+
cert);
364+
goto next_file;
365+
} else {
366+
if (!X509_STORE_add_cert(store, x509)) {
367+
warnx("Failed to add a certificate from \"%s\""
368+
" to the store\n", cert);
369+
goto next_file;
370+
} else {
371+
if (verbosity >= VERBOSITY_DEBUG)
372+
fprintf(stderr, "Successfully added a certificate from"
373+
" \"%s\" to the store\n", cert);
374+
num_certs++;
375+
}
376+
}
377+
378+
next_file:
379+
X509_free(x509);
380+
x509 = NULL;
381+
382+
BIO_free(bio);
383+
bio = NULL;
384+
385+
OPENSSL_free(cert);
386+
cert = NULL;
387+
}
388+
}
389+
390+
if (verbosity >= VERBOSITY_DEBUG_STATS)
391+
fprintf(stderr, "Added %zu certificates to the store\n", num_certs);
392+
312393
counts = OPENSSL_malloc(sizeof(size_t) * threadcount);
313394
if (counts == NULL)
314395
errx(EXIT_FAILURE, "Failed to create counts array");
@@ -356,8 +437,6 @@ main(int argc, char *argv[])
356437
err:
357438
X509_free(x509_nonce);
358439
X509_STORE_free(store);
359-
BIO_free(bio);
360-
OPENSSL_free(cert);
361440
OPENSSL_free(founds);
362441
OPENSSL_free(counts);
363442
return ret;

0 commit comments

Comments
 (0)