2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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>
21 #include <openssl/x509.h>
22 #include "crypto/x509.h"
23 #include "x509_local.h"
25 DEFINE_STACK_OF(X509_OBJECT)
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,
47 static int new_dir(X509_LOOKUP *lu);
48 static void free_dir(X509_LOOKUP *lu);
49 static int add_cert_dir(BY_DIR *ctx, const char *dir, int type);
50 static int get_cert_by_subject(X509_LOOKUP *xl, X509_LOOKUP_TYPE type,
51 const X509_NAME *name, X509_OBJECT *ret);
52 static int get_cert_by_subject_with_libctx(X509_LOOKUP *xl,
53 X509_LOOKUP_TYPE type,
54 const X509_NAME *name,
58 static X509_LOOKUP_METHOD x509_dir_lookup = {
59 "Load certs from files in a directory",
60 new_dir, /* new_item */
65 get_cert_by_subject, /* get_by_subject */
66 NULL, /* get_by_issuer_serial */
67 NULL, /* get_by_fingerprint */
68 NULL, /* get_by_alias */
69 get_cert_by_subject_with_libctx, /* get_by_subject_with_libctx */
70 NULL, /* ctrl_with_libctx */
73 X509_LOOKUP_METHOD *X509_LOOKUP_hash_dir(void)
75 return &x509_dir_lookup;
78 static int dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl,
82 BY_DIR *ld = (BY_DIR *)ctx->method_data;
86 if (argl == X509_FILETYPE_DEFAULT) {
87 const char *dir = ossl_safe_getenv(X509_get_default_cert_dir_env());
90 ret = add_cert_dir(ld, dir, X509_FILETYPE_PEM);
92 ret = add_cert_dir(ld, X509_get_default_cert_dir(),
95 X509err(X509_F_DIR_CTRL, X509_R_LOADING_CERT_DIR);
98 ret = add_cert_dir(ld, argp, (int)argl);
104 static int new_dir(X509_LOOKUP *lu)
106 BY_DIR *a = OPENSSL_malloc(sizeof(*a));
109 X509err(X509_F_NEW_DIR, ERR_R_MALLOC_FAILURE);
113 if ((a->buffer = BUF_MEM_new()) == NULL) {
114 X509err(X509_F_NEW_DIR, ERR_R_MALLOC_FAILURE);
118 a->lock = CRYPTO_THREAD_lock_new();
119 if (a->lock == NULL) {
120 BUF_MEM_free(a->buffer);
121 X509err(X509_F_NEW_DIR, ERR_R_MALLOC_FAILURE);
132 static void by_dir_hash_free(BY_DIR_HASH *hash)
137 static int by_dir_hash_cmp(const BY_DIR_HASH *const *a,
138 const BY_DIR_HASH *const *b)
140 if ((*a)->hash > (*b)->hash)
142 if ((*a)->hash < (*b)->hash)
147 static void by_dir_entry_free(BY_DIR_ENTRY *ent)
149 OPENSSL_free(ent->dir);
150 sk_BY_DIR_HASH_pop_free(ent->hashes, by_dir_hash_free);
154 static void free_dir(X509_LOOKUP *lu)
156 BY_DIR *a = (BY_DIR *)lu->method_data;
158 sk_BY_DIR_ENTRY_pop_free(a->dirs, by_dir_entry_free);
159 BUF_MEM_free(a->buffer);
160 CRYPTO_THREAD_lock_free(a->lock);
164 static int add_cert_dir(BY_DIR *ctx, const char *dir, int type)
168 const char *s, *ss, *p;
170 if (dir == NULL || *dir == '\0') {
171 X509err(X509_F_ADD_CERT_DIR, X509_R_INVALID_DIRECTORY);
178 if ((*p == LIST_SEPARATOR_CHAR) || (*p == '\0')) {
186 for (j = 0; j < sk_BY_DIR_ENTRY_num(ctx->dirs); j++) {
187 ent = sk_BY_DIR_ENTRY_value(ctx->dirs, j);
188 if (strlen(ent->dir) == len && strncmp(ent->dir, ss, len) == 0)
191 if (j < sk_BY_DIR_ENTRY_num(ctx->dirs))
193 if (ctx->dirs == NULL) {
194 ctx->dirs = sk_BY_DIR_ENTRY_new_null();
196 X509err(X509_F_ADD_CERT_DIR, ERR_R_MALLOC_FAILURE);
200 ent = OPENSSL_malloc(sizeof(*ent));
202 X509err(X509_F_ADD_CERT_DIR, ERR_R_MALLOC_FAILURE);
205 ent->dir_type = type;
206 ent->hashes = sk_BY_DIR_HASH_new(by_dir_hash_cmp);
207 ent->dir = OPENSSL_strndup(ss, len);
208 if (ent->dir == NULL || ent->hashes == NULL) {
209 by_dir_entry_free(ent);
212 if (!sk_BY_DIR_ENTRY_push(ctx->dirs, ent)) {
213 by_dir_entry_free(ent);
214 X509err(X509_F_ADD_CERT_DIR, ERR_R_MALLOC_FAILURE);
218 } while (*p++ != '\0');
222 static int get_cert_by_subject_with_libctx(X509_LOOKUP *xl,
223 X509_LOOKUP_TYPE type,
224 const X509_NAME *name,
238 X509_OBJECT stmp, *tmp;
239 const char *postfix = "";
245 if (type == X509_LU_X509) {
246 data.st_x509.cert_info.subject = (X509_NAME *)name; /* won't modify it */
247 stmp.data.x509 = &data.st_x509;
249 } else if (type == X509_LU_CRL) {
250 data.crl.crl.issuer = (X509_NAME *)name; /* won't modify it */
251 stmp.data.crl = &data.crl;
254 X509err(0, X509_R_WRONG_LOOKUP_TYPE);
258 if ((b = BUF_MEM_new()) == NULL) {
259 X509err(0, ERR_R_BUF_LIB);
263 ctx = (BY_DIR *)xl->method_data;
265 h = X509_NAME_hash(name);
266 for (i = 0; i < sk_BY_DIR_ENTRY_num(ctx->dirs); i++) {
269 BY_DIR_HASH htmp, *hent;
271 ent = sk_BY_DIR_ENTRY_value(ctx->dirs, i);
272 j = strlen(ent->dir) + 1 + 8 + 6 + 1 + 1;
273 if (!BUF_MEM_grow(b, j)) {
274 X509err(0, ERR_R_MALLOC_FAILURE);
277 if (type == X509_LU_CRL && ent->hashes) {
279 CRYPTO_THREAD_read_lock(ctx->lock);
280 idx = sk_BY_DIR_HASH_find(ent->hashes, &htmp);
282 hent = sk_BY_DIR_HASH_value(ent->hashes, idx);
288 CRYPTO_THREAD_unlock(ctx->lock);
295 #ifdef OPENSSL_SYS_VMS
296 c = ent->dir[strlen(ent->dir) - 1];
297 if (c != ':' && c != '>' && c != ']') {
299 * If no separator is present, we assume the directory
300 * specifier is a logical name, and add a colon. We really
301 * should use better VMS routines for merging things like
302 * this, but this will do for now... -- Richard Levitte
311 * This is special. When c == '\0', no directory separator
314 BIO_snprintf(b->data, b->max,
315 "%s%08lx.%s%d", ent->dir, h, postfix, k);
317 BIO_snprintf(b->data, b->max,
318 "%s%c%08lx.%s%d", ent->dir, c, h, postfix, k);
320 #ifndef OPENSSL_NO_POSIX_IO
326 if (stat(b->data, &st) < 0)
331 if (type == X509_LU_X509) {
332 if ((X509_load_cert_file_with_libctx(xl, b->data, ent->dir_type,
333 libctx, propq)) == 0)
335 } else if (type == X509_LU_CRL) {
336 if ((X509_load_crl_file(xl, b->data, ent->dir_type)) == 0)
339 /* else case will caught higher up */
344 * we have added it to the cache so now pull it out again
346 X509_STORE_lock(xl->store_ctx);
347 j = sk_X509_OBJECT_find(xl->store_ctx->objs, &stmp);
348 tmp = sk_X509_OBJECT_value(xl->store_ctx->objs, j);
349 X509_STORE_unlock(xl->store_ctx);
351 /* If a CRL, update the last file suffix added for this */
353 if (type == X509_LU_CRL) {
354 CRYPTO_THREAD_write_lock(ctx->lock);
356 * Look for entry again in case another thread added an entry
361 idx = sk_BY_DIR_HASH_find(ent->hashes, &htmp);
362 hent = sk_BY_DIR_HASH_value(ent->hashes, idx);
365 hent = OPENSSL_malloc(sizeof(*hent));
367 CRYPTO_THREAD_unlock(ctx->lock);
368 X509err(0, ERR_R_MALLOC_FAILURE);
374 if (!sk_BY_DIR_HASH_push(ent->hashes, hent)) {
375 CRYPTO_THREAD_unlock(ctx->lock);
377 X509err(0, ERR_R_MALLOC_FAILURE);
381 } else if (hent->suffix < k) {
385 CRYPTO_THREAD_unlock(ctx->lock);
391 ret->type = tmp->type;
392 memcpy(&ret->data, &tmp->data, sizeof(ret->data));
395 * Clear any errors that might have been raised processing empty
396 * or malformed files.
408 static int get_cert_by_subject(X509_LOOKUP *xl, X509_LOOKUP_TYPE type,
409 const X509_NAME *name, X509_OBJECT *ret)
411 return get_cert_by_subject_with_libctx(xl, type, name, ret, NULL, NULL);