14969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
24969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * All rights reserved.
34969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *
44969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * This package is an SSL implementation written
54969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * by Eric Young (eay@cryptsoft.com).
64969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * The implementation was written so as to conform with Netscapes SSL.
74969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *
84969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * This library is free for commercial and non-commercial use as long as
94969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * the following conditions are aheared to.  The following conditions
104969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * apply to all code found in this distribution, be it the RC4, RSA,
114969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
124969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * included with this distribution is covered by the same copyright terms
134969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * except that the holder is Tim Hudson (tjh@cryptsoft.com).
144969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *
154969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * Copyright remains Eric Young's, and as such any Copyright notices in
164969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * the code are not to be removed.
174969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * If this package is used in a product, Eric Young should be given attribution
184969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * as the author of the parts of the library used.
194969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * This can be in the form of a textual message at program startup or
204969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * in documentation (online or textual) provided with the package.
214969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *
224969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * Redistribution and use in source and binary forms, with or without
234969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * modification, are permitted provided that the following conditions
244969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * are met:
254969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * 1. Redistributions of source code must retain the copyright
264969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *    notice, this list of conditions and the following disclaimer.
274969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * 2. Redistributions in binary form must reproduce the above copyright
284969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *    notice, this list of conditions and the following disclaimer in the
294969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *    documentation and/or other materials provided with the distribution.
304969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * 3. All advertising materials mentioning features or use of this software
314969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *    must display the following acknowledgement:
324969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *    "This product includes cryptographic software written by
334969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *     Eric Young (eay@cryptsoft.com)"
344969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *    The word 'cryptographic' can be left out if the rouines from the library
354969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *    being used are not cryptographic related :-).
364969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * 4. If you include any Windows specific code (or a derivative thereof) from
374969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *    the apps directory (application code) you must include an acknowledgement:
384969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
394969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *
404969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
414969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
424969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
434969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
444969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
454969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
464969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
474969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
484969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
494969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
504969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * SUCH DAMAGE.
514969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *
524969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * The licence and distribution terms for any publically available version or
534969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * derivative of this code cannot be changed.  i.e. this code cannot simply be
544969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * copied and put under another distribution licence
554969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * [including the GNU Public Licence.]
564969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin */
574969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin/* ====================================================================
584969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * Copyright (c) 1998-2007 The OpenSSL Project.  All rights reserved.
594969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *
604969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * Redistribution and use in source and binary forms, with or without
614969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * modification, are permitted provided that the following conditions
624969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * are met:
634969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *
644969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * 1. Redistributions of source code must retain the above copyright
654969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *    notice, this list of conditions and the following disclaimer.
664969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *
674969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * 2. Redistributions in binary form must reproduce the above copyright
684969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *    notice, this list of conditions and the following disclaimer in
694969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *    the documentation and/or other materials provided with the
704969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *    distribution.
714969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *
724969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * 3. All advertising materials mentioning features or use of this
734969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *    software must display the following acknowledgment:
744969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *    "This product includes software developed by the OpenSSL Project
754969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
764969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *
774969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
784969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *    endorse or promote products derived from this software without
794969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *    prior written permission. For written permission, please contact
804969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *    openssl-core@openssl.org.
814969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *
824969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * 5. Products derived from this software may not be called "OpenSSL"
834969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *    nor may "OpenSSL" appear in their names without prior written
844969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *    permission of the OpenSSL Project.
854969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *
864969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * 6. Redistributions of any form whatsoever must retain the following
874969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *    acknowledgment:
884969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *    "This product includes software developed by the OpenSSL Project
894969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
904969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *
914969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
924969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
934969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
944969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
954969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
964969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
974969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
984969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
994969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1004969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1014969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1024969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * OF THE POSSIBILITY OF SUCH DAMAGE.
1034969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * ====================================================================
1044969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *
1054969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * This product includes cryptographic software written by Eric Young
1064969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * (eay@cryptsoft.com).  This product includes software written by Tim
1074969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin * Hudson (tjh@cryptsoft.com).
1084969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin *
1094969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin */
1104969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin
1114969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin#include <openssl/ssl.h>
1124969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin
1134969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin#if !defined(OPENSSL_WINDOWS) && !defined(OPENSSL_PNACL)
1144969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin
1154969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin#include <dirent.h>
1164969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin#include <errno.h>
1174969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin#include <string.h>
1184969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin
1194969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin#include <openssl/err.h>
1204969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin#include <openssl/mem.h>
1214969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin
1224969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin
123bb1ceac29bc7a18b94e3da78057dc41aa7071784Steven Valdezint SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
124bb1ceac29bc7a18b94e3da78057dc41aa7071784Steven Valdez                                       const char *path) {
125bb1ceac29bc7a18b94e3da78057dc41aa7071784Steven Valdez  DIR *dir = opendir(path);
126bb1ceac29bc7a18b94e3da78057dc41aa7071784Steven Valdez  if (dir == NULL) {
127bb1ceac29bc7a18b94e3da78057dc41aa7071784Steven Valdez    OPENSSL_PUT_ERROR(SSL, ERR_R_SYS_LIB);
128bb1ceac29bc7a18b94e3da78057dc41aa7071784Steven Valdez    ERR_add_error_data(3, "opendir('", dir, "')");
1294969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin    return 0;
1304969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin  }
1314969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin
1324969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin  int ret = 0;
133bb1ceac29bc7a18b94e3da78057dc41aa7071784Steven Valdez  for (;;) {
134bb1ceac29bc7a18b94e3da78057dc41aa7071784Steven Valdez    /* |readdir| may fail with or without setting |errno|. */
135bb1ceac29bc7a18b94e3da78057dc41aa7071784Steven Valdez    errno = 0;
136bb1ceac29bc7a18b94e3da78057dc41aa7071784Steven Valdez    struct dirent *dirent = readdir(dir);
137bb1ceac29bc7a18b94e3da78057dc41aa7071784Steven Valdez    if (dirent == NULL) {
138bb1ceac29bc7a18b94e3da78057dc41aa7071784Steven Valdez      if (errno) {
139bb1ceac29bc7a18b94e3da78057dc41aa7071784Steven Valdez        OPENSSL_PUT_ERROR(SSL, ERR_R_SYS_LIB);
140bb1ceac29bc7a18b94e3da78057dc41aa7071784Steven Valdez        ERR_add_error_data(3, "readdir('", path, "')");
141bb1ceac29bc7a18b94e3da78057dc41aa7071784Steven Valdez      } else {
142bb1ceac29bc7a18b94e3da78057dc41aa7071784Steven Valdez        ret = 1;
143bb1ceac29bc7a18b94e3da78057dc41aa7071784Steven Valdez      }
144bb1ceac29bc7a18b94e3da78057dc41aa7071784Steven Valdez      break;
145bb1ceac29bc7a18b94e3da78057dc41aa7071784Steven Valdez    }
1464969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin
1474969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin    char buf[1024];
148bb1ceac29bc7a18b94e3da78057dc41aa7071784Steven Valdez    if (strlen(path) + strlen(dirent->d_name) + 2 > sizeof(buf)) {
1494969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin      OPENSSL_PUT_ERROR(SSL, SSL_R_PATH_TOO_LONG);
150bb1ceac29bc7a18b94e3da78057dc41aa7071784Steven Valdez      break;
1514969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin    }
1524969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin
153bb1ceac29bc7a18b94e3da78057dc41aa7071784Steven Valdez    int r = BIO_snprintf(buf, sizeof(buf), "%s/%s", path, dirent->d_name);
154bb1ceac29bc7a18b94e3da78057dc41aa7071784Steven Valdez    if (r <= 0 ||
155bb1ceac29bc7a18b94e3da78057dc41aa7071784Steven Valdez        r >= (int)sizeof(buf) ||
1564969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin        !SSL_add_file_cert_subjects_to_stack(stack, buf)) {
157bb1ceac29bc7a18b94e3da78057dc41aa7071784Steven Valdez      break;
1584969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin    }
1594969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin  }
1604969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin
161bb1ceac29bc7a18b94e3da78057dc41aa7071784Steven Valdez  closedir(dir);
1624969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin  return ret;
1634969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin}
1644969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin
1654969cc9b0ab2905ec478277f50ed3849b37a6c6bDavid Benjamin#endif /* !WINDOWS && !PNACL */
166