| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) |
| 2 |
// Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
| 3 |
// SPDX-License-Identifier: Apache-2.0 |
| 4 |
|
| 5 |
#include <openssl/ssl.h> |
| 6 |
|
| 7 |
#if !defined(OPENSSL_WINDOWS) && !defined(OPENSSL_PNACL) && \ |
| 8 |
!defined(OPENSSL_NO_FILESYSTEM) |
| 9 |
|
| 10 |
#include <dirent.h> |
| 11 |
#include <errno.h> |
| 12 |
#include <string.h> |
| 13 |
|
| 14 |
#include <openssl/err.h> |
| 15 |
#include <openssl/mem.h> |
| 16 |
|
| 17 |
|
| 18 |
int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack, |
| 19 |
const char *path) { |
| 20 |
DIR *dir = opendir(path); |
| 21 |
if (dir == NULL) { |
| 22 |
OPENSSL_PUT_ERROR(SSL, ERR_R_SYS_LIB); |
| 23 |
ERR_add_error_data(3, "opendir('", dir, "')"); |
| 24 |
return 0; |
| 25 |
} |
| 26 |
|
| 27 |
int ret = 0; |
| 28 |
for (;;) { |
| 29 |
// |readdir| may fail with or without setting |errno|. |
| 30 |
errno = 0; |
| 31 |
struct dirent *dirent = readdir(dir); |
| 32 |
if (dirent == NULL) { |
| 33 |
if (errno) { |
| 34 |
OPENSSL_PUT_ERROR(SSL, ERR_R_SYS_LIB); |
| 35 |
ERR_add_error_data(3, "readdir('", path, "')"); |
| 36 |
} else { |
| 37 |
ret = 1; |
| 38 |
} |
| 39 |
break; |
| 40 |
} |
| 41 |
|
| 42 |
char buf[1024]; |
| 43 |
if (strlen(path) + strlen(dirent->d_name) + 2 > sizeof(buf)) { |
| 44 |
OPENSSL_PUT_ERROR(SSL, SSL_R_PATH_TOO_LONG); |
| 45 |
break; |
| 46 |
} |
| 47 |
|
| 48 |
int r = snprintf(buf, sizeof(buf), "%s/%s", path, dirent->d_name); |
| 49 |
if (r <= 0 || |
| 50 |
r >= (int)sizeof(buf) || |
| 51 |
!SSL_add_file_cert_subjects_to_stack(stack, buf)) { |
| 52 |
break; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
closedir(dir); |
| 57 |
return ret; |
| 58 |
} |
| 59 |
|
| 60 |
#endif // !WINDOWS && !PNACL && !OPENSSL_NO_FILESYSTEM |