|
7 | 7 | * https://www.openssl.org/source/license.html |
8 | 8 | */ |
9 | 9 |
|
| 10 | +#include <dirent.h> |
| 11 | +#include <errno.h> |
10 | 12 | #include <stdbool.h> |
11 | 13 | #include <stdio.h> |
12 | 14 | #include <stdlib.h> |
13 | 15 | #include <string.h> |
| 16 | +#include <sys/stat.h> |
14 | 17 | #ifndef _WIN32 |
15 | 18 | # include <libgen.h> |
16 | 19 | # include <unistd.h> |
@@ -254,11 +257,10 @@ main(int argc, char *argv[]) |
254 | 257 | size_t total_count = 0; |
255 | 258 | size_t total_found = 0; |
256 | 259 | double avcalltime; |
257 | | - char *cert = NULL; |
258 | 260 | int ret = EXIT_FAILURE; |
259 | | - BIO *bio = NULL; |
260 | 261 | int opt; |
261 | 262 | int dirs_start; |
| 263 | + size_t num_certs = 0; |
262 | 264 | struct nonce_cfg nonce_cfg; |
263 | 265 |
|
264 | 266 | parse_nonce_cfg(NONCE_CFG, &nonce_cfg); |
@@ -309,6 +311,85 @@ main(int argc, char *argv[]) |
309 | 311 | if (store == NULL || !X509_STORE_set_default_paths(store)) |
310 | 312 | errx(EXIT_FAILURE, "Failed to create X509_STORE"); |
311 | 313 |
|
| 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 | + |
312 | 393 | counts = OPENSSL_malloc(sizeof(size_t) * threadcount); |
313 | 394 | if (counts == NULL) |
314 | 395 | errx(EXIT_FAILURE, "Failed to create counts array"); |
@@ -356,8 +437,6 @@ main(int argc, char *argv[]) |
356 | 437 | err: |
357 | 438 | X509_free(x509_nonce); |
358 | 439 | X509_STORE_free(store); |
359 | | - BIO_free(bio); |
360 | | - OPENSSL_free(cert); |
361 | 440 | OPENSSL_free(founds); |
362 | 441 | OPENSSL_free(counts); |
363 | 442 | return ret; |
|
0 commit comments