2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include "internal/cryptlib.h"
15 #include <sys/types.h>
17 #ifndef OPENSSL_NO_POSIX_IO
18 # include <sys/stat.h>
22 #include <openssl/lhash.h>
23 #include <openssl/x509.h>
24 #include "internal/x509_int.h"
27 struct lookup_dir_hashes_st {
32 struct lookup_dir_entry_st {
35 STACK_OF(BY_DIR_HASH) *hashes;
38 typedef struct lookup_dir_st {
40 STACK_OF(BY_DIR_ENTRY) *dirs;
44 static int dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl,
46 static int new_dir(X509_LOOKUP *lu);
47 static void free_dir(X509_LOOKUP *lu);
48 static int add_cert_dir(BY_DIR *ctx, const char *dir, int type);
49 static int get_cert_by_subject(X509_LOOKUP *xl, X509_LOOKUP_TYPE type,
50 X509_NAME *name, X509_OBJECT *ret);
51 static X509_LOOKUP_METHOD x509_dir_lookup = {
52 "Load certs from files in a directory",
58 get_cert_by_subject, /* get_by_subject */
59 NULL, /* get_by_issuer_serial */
60 NULL, /* get_by_fingerprint */
61 NULL, /* get_by_alias */
64 X509_LOOKUP_METHOD *X509_LOOKUP_hash_dir(void)
66 return (&x509_dir_lookup);
69 static int dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl,
76 ld = (BY_DIR *)ctx->method_data;
80 if (argl == X509_FILETYPE_DEFAULT) {
81 dir = (char *)getenv(X509_get_default_cert_dir_env());
83 ret = add_cert_dir(ld, dir, X509_FILETYPE_PEM);
85 ret = add_cert_dir(ld, X509_get_default_cert_dir(),
88 X509err(X509_F_DIR_CTRL, X509_R_LOADING_CERT_DIR);
91 ret = add_cert_dir(ld, argp, (int)argl);
97 static int new_dir(X509_LOOKUP *lu)
101 if ((a = OPENSSL_malloc(sizeof(*a))) == NULL)
103 if ((a->buffer = BUF_MEM_new()) == NULL) {
108 a->lock = CRYPTO_THREAD_lock_new();
109 if (a->lock == NULL) {
110 BUF_MEM_free(a->buffer);
114 lu->method_data = (char *)a;
118 static void by_dir_hash_free(BY_DIR_HASH *hash)
123 static int by_dir_hash_cmp(const BY_DIR_HASH *const *a,
124 const BY_DIR_HASH *const *b)
126 if ((*a)->hash > (*b)->hash)
128 if ((*a)->hash < (*b)->hash)
133 static void by_dir_entry_free(BY_DIR_ENTRY *ent)
135 OPENSSL_free(ent->dir);
136 sk_BY_DIR_HASH_pop_free(ent->hashes, by_dir_hash_free);
140 static void free_dir(X509_LOOKUP *lu)
144 a = (BY_DIR *)lu->method_data;
145 sk_BY_DIR_ENTRY_pop_free(a->dirs, by_dir_entry_free);
146 BUF_MEM_free(a->buffer);
147 CRYPTO_THREAD_lock_free(a->lock);
151 static int add_cert_dir(BY_DIR *ctx, const char *dir, int type)
155 const char *s, *ss, *p;
157 if (dir == NULL || !*dir) {
158 X509err(X509_F_ADD_CERT_DIR, X509_R_INVALID_DIRECTORY);
165 if ((*p == LIST_SEPARATOR_CHAR) || (*p == '\0')) {
172 for (j = 0; j < sk_BY_DIR_ENTRY_num(ctx->dirs); j++) {
173 ent = sk_BY_DIR_ENTRY_value(ctx->dirs, j);
174 if (strlen(ent->dir) == len && strncmp(ent->dir, ss, len) == 0)
177 if (j < sk_BY_DIR_ENTRY_num(ctx->dirs))
179 if (ctx->dirs == NULL) {
180 ctx->dirs = sk_BY_DIR_ENTRY_new_null();
182 X509err(X509_F_ADD_CERT_DIR, ERR_R_MALLOC_FAILURE);
186 ent = OPENSSL_malloc(sizeof(*ent));
189 ent->dir_type = type;
190 ent->hashes = sk_BY_DIR_HASH_new(by_dir_hash_cmp);
191 ent->dir = OPENSSL_strndup(ss, len);
192 if (ent->dir == NULL || ent->hashes == NULL) {
193 by_dir_entry_free(ent);
196 if (!sk_BY_DIR_ENTRY_push(ctx->dirs, ent)) {
197 by_dir_entry_free(ent);
201 } while (*p++ != '\0');
205 static int get_cert_by_subject(X509_LOOKUP *xl, X509_LOOKUP_TYPE type,
206 X509_NAME *name, X509_OBJECT *ret)
217 X509_OBJECT stmp, *tmp;
218 const char *postfix = "";
224 if (type == X509_LU_X509) {
225 data.st_x509.cert_info.subject = name;
226 stmp.data.x509 = &data.st_x509;
228 } else if (type == X509_LU_CRL) {
229 data.crl.crl.issuer = name;
230 stmp.data.crl = &data.crl;
233 X509err(X509_F_GET_CERT_BY_SUBJECT, X509_R_WRONG_LOOKUP_TYPE);
237 if ((b = BUF_MEM_new()) == NULL) {
238 X509err(X509_F_GET_CERT_BY_SUBJECT, ERR_R_BUF_LIB);
242 ctx = (BY_DIR *)xl->method_data;
244 h = X509_NAME_hash(name);
245 for (i = 0; i < sk_BY_DIR_ENTRY_num(ctx->dirs); i++) {
248 BY_DIR_HASH htmp, *hent;
249 ent = sk_BY_DIR_ENTRY_value(ctx->dirs, i);
250 j = strlen(ent->dir) + 1 + 8 + 6 + 1 + 1;
251 if (!BUF_MEM_grow(b, j)) {
252 X509err(X509_F_GET_CERT_BY_SUBJECT, ERR_R_MALLOC_FAILURE);
255 if (type == X509_LU_CRL && ent->hashes) {
257 CRYPTO_THREAD_read_lock(ctx->lock);
258 idx = sk_BY_DIR_HASH_find(ent->hashes, &htmp);
260 hent = sk_BY_DIR_HASH_value(ent->hashes, idx);
266 CRYPTO_THREAD_unlock(ctx->lock);
273 #ifdef OPENSSL_SYS_VMS
274 c = ent->dir[strlen(ent->dir) - 1];
275 if (c != ':' && c != '>' && c != ']') {
277 * If no separator is present, we assume the directory
278 * specifier is a logical name, and add a colon. We really
279 * should use better VMS routines for merging things like
280 * this, but this will do for now... -- Richard Levitte
289 * This is special. When c == '\0', no directory separator
292 BIO_snprintf(b->data, b->max,
293 "%s%08lx.%s%d", ent->dir, h, postfix, k);
295 BIO_snprintf(b->data, b->max,
296 "%s%c%08lx.%s%d", ent->dir, c, h, postfix, k);
298 #ifndef OPENSSL_NO_POSIX_IO
304 if (stat(b->data, &st) < 0)
309 if (type == X509_LU_X509) {
310 if ((X509_load_cert_file(xl, b->data, ent->dir_type)) == 0)
312 } else if (type == X509_LU_CRL) {
313 if ((X509_load_crl_file(xl, b->data, ent->dir_type)) == 0)
316 /* else case will caught higher up */
321 * we have added it to the cache so now pull it out again
323 CRYPTO_THREAD_write_lock(ctx->lock);
324 j = sk_X509_OBJECT_find(xl->store_ctx->objs, &stmp);
326 tmp = sk_X509_OBJECT_value(xl->store_ctx->objs, j);
329 CRYPTO_THREAD_unlock(ctx->lock);
331 /* If a CRL, update the last file suffix added for this */
333 if (type == X509_LU_CRL) {
334 CRYPTO_THREAD_write_lock(ctx->lock);
336 * Look for entry again in case another thread added an entry
341 idx = sk_BY_DIR_HASH_find(ent->hashes, &htmp);
343 hent = sk_BY_DIR_HASH_value(ent->hashes, idx);
346 hent = OPENSSL_malloc(sizeof(*hent));
348 CRYPTO_THREAD_unlock(ctx->lock);
349 X509err(X509_F_GET_CERT_BY_SUBJECT, ERR_R_MALLOC_FAILURE);
355 if (!sk_BY_DIR_HASH_push(ent->hashes, hent)) {
356 CRYPTO_THREAD_unlock(ctx->lock);
361 } else if (hent->suffix < k) {
365 CRYPTO_THREAD_unlock(ctx->lock);
371 ret->type = tmp->type;
372 memcpy(&ret->data, &tmp->data, sizeof(ret->data));
375 * Clear any errors that might have been raised processing empty
376 * or malformed files.
381 * If we were going to up the reference count, we would need to
382 * do it on a perl 'type' basis
384 /*- CRYPTO_add(&tmp->data.x509->references,1,