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 struct lookup_dir_hashes_st {
30 struct lookup_dir_entry_st {
33 STACK_OF(BY_DIR_HASH) *hashes;
36 typedef struct lookup_dir_st {
38 STACK_OF(BY_DIR_ENTRY) *dirs;
42 static int dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl,
45 static int new_dir(X509_LOOKUP *lu);
46 static void free_dir(X509_LOOKUP *lu);
47 static int add_cert_dir(BY_DIR *ctx, const char *dir, int type);
48 static int get_cert_by_subject(X509_LOOKUP *xl, X509_LOOKUP_TYPE type,
49 const X509_NAME *name, X509_OBJECT *ret);
50 static int get_cert_by_subject_ex(X509_LOOKUP *xl, X509_LOOKUP_TYPE type,
51 const X509_NAME *name, X509_OBJECT *ret,
52 OSSL_LIB_CTX *libctx, const char *propq);
53 static X509_LOOKUP_METHOD x509_dir_lookup = {
54 "Load certs from files in a directory",
55 new_dir, /* new_item */
60 get_cert_by_subject, /* get_by_subject */
61 NULL, /* get_by_issuer_serial */
62 NULL, /* get_by_fingerprint */
63 NULL, /* get_by_alias */
64 get_cert_by_subject_ex, /* get_by_subject_ex */
68 X509_LOOKUP_METHOD *X509_LOOKUP_hash_dir(void)
70 return &x509_dir_lookup;
73 static int dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl,
77 BY_DIR *ld = (BY_DIR *)ctx->method_data;
81 if (argl == X509_FILETYPE_DEFAULT) {
82 const char *dir = ossl_safe_getenv(X509_get_default_cert_dir_env());
85 ret = add_cert_dir(ld, dir, X509_FILETYPE_PEM);
87 ret = add_cert_dir(ld, X509_get_default_cert_dir(),
90 X509err(X509_F_DIR_CTRL, X509_R_LOADING_CERT_DIR);
93 ret = add_cert_dir(ld, argp, (int)argl);
99 static int new_dir(X509_LOOKUP *lu)
101 BY_DIR *a = OPENSSL_malloc(sizeof(*a));
104 X509err(X509_F_NEW_DIR, ERR_R_MALLOC_FAILURE);
108 if ((a->buffer = BUF_MEM_new()) == NULL) {
109 X509err(X509_F_NEW_DIR, ERR_R_MALLOC_FAILURE);
113 a->lock = CRYPTO_THREAD_lock_new();
114 if (a->lock == NULL) {
115 BUF_MEM_free(a->buffer);
116 X509err(X509_F_NEW_DIR, ERR_R_MALLOC_FAILURE);
127 static void by_dir_hash_free(BY_DIR_HASH *hash)
132 static int by_dir_hash_cmp(const BY_DIR_HASH *const *a,
133 const BY_DIR_HASH *const *b)
135 if ((*a)->hash > (*b)->hash)
137 if ((*a)->hash < (*b)->hash)
142 static void by_dir_entry_free(BY_DIR_ENTRY *ent)
144 OPENSSL_free(ent->dir);
145 sk_BY_DIR_HASH_pop_free(ent->hashes, by_dir_hash_free);
149 static void free_dir(X509_LOOKUP *lu)
151 BY_DIR *a = (BY_DIR *)lu->method_data;
153 sk_BY_DIR_ENTRY_pop_free(a->dirs, by_dir_entry_free);
154 BUF_MEM_free(a->buffer);
155 CRYPTO_THREAD_lock_free(a->lock);
159 static int add_cert_dir(BY_DIR *ctx, const char *dir, int type)
163 const char *s, *ss, *p;
165 if (dir == NULL || *dir == '\0') {
166 X509err(X509_F_ADD_CERT_DIR, X509_R_INVALID_DIRECTORY);
173 if ((*p == LIST_SEPARATOR_CHAR) || (*p == '\0')) {
181 for (j = 0; j < sk_BY_DIR_ENTRY_num(ctx->dirs); j++) {
182 ent = sk_BY_DIR_ENTRY_value(ctx->dirs, j);
183 if (strlen(ent->dir) == len && strncmp(ent->dir, ss, len) == 0)
186 if (j < sk_BY_DIR_ENTRY_num(ctx->dirs))
188 if (ctx->dirs == NULL) {
189 ctx->dirs = sk_BY_DIR_ENTRY_new_null();
191 X509err(X509_F_ADD_CERT_DIR, ERR_R_MALLOC_FAILURE);
195 ent = OPENSSL_malloc(sizeof(*ent));
197 X509err(X509_F_ADD_CERT_DIR, ERR_R_MALLOC_FAILURE);
200 ent->dir_type = type;
201 ent->hashes = sk_BY_DIR_HASH_new(by_dir_hash_cmp);
202 ent->dir = OPENSSL_strndup(ss, len);
203 if (ent->dir == NULL || ent->hashes == NULL) {
204 by_dir_entry_free(ent);
207 if (!sk_BY_DIR_ENTRY_push(ctx->dirs, ent)) {
208 by_dir_entry_free(ent);
209 X509err(X509_F_ADD_CERT_DIR, ERR_R_MALLOC_FAILURE);
213 } while (*p++ != '\0');
217 static int get_cert_by_subject_ex(X509_LOOKUP *xl, X509_LOOKUP_TYPE type,
218 const X509_NAME *name, X509_OBJECT *ret,
219 OSSL_LIB_CTX *libctx, const char *propq)
230 X509_OBJECT stmp, *tmp;
231 const char *postfix = "";
237 if (type == X509_LU_X509) {
238 data.st_x509.cert_info.subject = (X509_NAME *)name; /* won't modify it */
239 stmp.data.x509 = &data.st_x509;
241 } else if (type == X509_LU_CRL) {
242 data.crl.crl.issuer = (X509_NAME *)name; /* won't modify it */
243 stmp.data.crl = &data.crl;
246 X509err(0, X509_R_WRONG_LOOKUP_TYPE);
250 if ((b = BUF_MEM_new()) == NULL) {
251 X509err(0, ERR_R_BUF_LIB);
255 ctx = (BY_DIR *)xl->method_data;
257 h = X509_NAME_hash(name);
258 for (i = 0; i < sk_BY_DIR_ENTRY_num(ctx->dirs); i++) {
261 BY_DIR_HASH htmp, *hent;
263 ent = sk_BY_DIR_ENTRY_value(ctx->dirs, i);
264 j = strlen(ent->dir) + 1 + 8 + 6 + 1 + 1;
265 if (!BUF_MEM_grow(b, j)) {
266 X509err(0, ERR_R_MALLOC_FAILURE);
269 if (type == X509_LU_CRL && ent->hashes) {
271 CRYPTO_THREAD_read_lock(ctx->lock);
272 idx = sk_BY_DIR_HASH_find(ent->hashes, &htmp);
274 hent = sk_BY_DIR_HASH_value(ent->hashes, idx);
280 CRYPTO_THREAD_unlock(ctx->lock);
288 #ifdef OPENSSL_SYS_VMS
289 c = ent->dir[strlen(ent->dir) - 1];
290 if (c != ':' && c != '>' && c != ']') {
292 * If no separator is present, we assume the directory
293 * specifier is a logical name, and add a colon. We really
294 * should use better VMS routines for merging things like
295 * this, but this will do for now... -- Richard Levitte
304 * This is special. When c == '\0', no directory separator
307 BIO_snprintf(b->data, b->max,
308 "%s%08lx.%s%d", ent->dir, h, postfix, k);
312 BIO_snprintf(b->data, b->max,
313 "%s%c%08lx.%s%d", ent->dir, c, h, postfix, k);
315 #ifndef OPENSSL_NO_POSIX_IO
321 if (stat(b->data, &st) < 0)
326 if (type == X509_LU_X509) {
327 if ((X509_load_cert_file_ex(xl, b->data, ent->dir_type, libctx,
330 } else if (type == X509_LU_CRL) {
331 if ((X509_load_crl_file(xl, b->data, ent->dir_type)) == 0)
334 /* else case will caught higher up */
339 * we have added it to the cache so now pull it out again
341 X509_STORE_lock(xl->store_ctx);
342 j = sk_X509_OBJECT_find(xl->store_ctx->objs, &stmp);
343 tmp = sk_X509_OBJECT_value(xl->store_ctx->objs, j);
344 X509_STORE_unlock(xl->store_ctx);
346 /* If a CRL, update the last file suffix added for this */
348 if (type == X509_LU_CRL) {
349 CRYPTO_THREAD_write_lock(ctx->lock);
351 * Look for entry again in case another thread added an entry
356 idx = sk_BY_DIR_HASH_find(ent->hashes, &htmp);
357 hent = sk_BY_DIR_HASH_value(ent->hashes, idx);
360 hent = OPENSSL_malloc(sizeof(*hent));
362 CRYPTO_THREAD_unlock(ctx->lock);
363 X509err(0, ERR_R_MALLOC_FAILURE);
369 if (!sk_BY_DIR_HASH_push(ent->hashes, hent)) {
370 CRYPTO_THREAD_unlock(ctx->lock);
372 X509err(0, ERR_R_MALLOC_FAILURE);
376 } else if (hent->suffix < k) {
380 CRYPTO_THREAD_unlock(ctx->lock);
386 ret->type = tmp->type;
387 memcpy(&ret->data, &tmp->data, sizeof(ret->data));
390 * Clear any errors that might have been raised processing empty
391 * or malformed files.
403 static int get_cert_by_subject(X509_LOOKUP *xl, X509_LOOKUP_TYPE type,
404 const X509_NAME *name, X509_OBJECT *ret)
406 return get_cert_by_subject_ex(xl, type, name, ret, NULL, NULL);