]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/x509/by_dir.c
Fix URI handling in SSL_CERT_DIR/introduce SSL_CERT_URI env
[thirdparty/openssl.git] / crypto / x509 / by_dir.c
CommitLineData
b1322259 1/*
fecb3aae 2 * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
3e4b43b9 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
b1322259
RS
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
d02b48c6
RE
8 */
9
650c6687
RB
10#if defined (__TANDEM) && defined (_SPT_MODEL_)
11 /*
12 * These definitions have to come first in SPT due to scoping of the
13 * declarations in c99 associated with SPT use of stat.
14 */
15# include <sys/types.h>
16# include <sys/stat.h>
17#endif
18
d5f9166b 19#include "internal/e_os.h"
07016a8a 20#include "internal/cryptlib.h"
d02b48c6
RE
21#include <stdio.h>
22#include <time.h>
23#include <errno.h>
b379fe6c 24#include <sys/types.h>
d02b48c6 25
49e3c9d8 26#ifndef OPENSSL_NO_POSIX_IO
17f389bb
AP
27# include <sys/stat.h>
28#endif
29
ec577822 30#include <openssl/x509.h>
25f2138b 31#include "crypto/x509.h"
706457b7 32#include "x509_local.h"
d02b48c6 33
4a1f3f27 34struct lookup_dir_hashes_st {
0f113f3e
MC
35 unsigned long hash;
36 int suffix;
4a1f3f27 37};
5d20c4fb 38
4a1f3f27 39struct lookup_dir_entry_st {
0f113f3e
MC
40 char *dir;
41 int dir_type;
42 STACK_OF(BY_DIR_HASH) *hashes;
4a1f3f27 43};
5d20c4fb 44
0f113f3e
MC
45typedef struct lookup_dir_st {
46 BUF_MEM *buffer;
47 STACK_OF(BY_DIR_ENTRY) *dirs;
4fc4faa7 48 CRYPTO_RWLOCK *lock;
0f113f3e 49} BY_DIR;
d02b48c6 50
303c0028 51static int dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl,
6725682d
SL
52 char **retp);
53
d02b48c6
RE
54static int new_dir(X509_LOOKUP *lu);
55static void free_dir(X509_LOOKUP *lu);
0f113f3e 56static int add_cert_dir(BY_DIR *ctx, const char *dir, int type);
0946a198 57static int get_cert_by_subject(X509_LOOKUP *xl, X509_LOOKUP_TYPE type,
8cc86b81 58 const X509_NAME *name, X509_OBJECT *ret);
d8652be0
MC
59static int get_cert_by_subject_ex(X509_LOOKUP *xl, X509_LOOKUP_TYPE type,
60 const X509_NAME *name, X509_OBJECT *ret,
b4250010 61 OSSL_LIB_CTX *libctx, const char *propq);
df2ee0e2 62static X509_LOOKUP_METHOD x509_dir_lookup = {
0f113f3e 63 "Load certs from files in a directory",
6725682d
SL
64 new_dir, /* new_item */
65 free_dir, /* free */
66 NULL, /* init */
67 NULL, /* shutdown */
68 dir_ctrl, /* ctrl */
69 get_cert_by_subject, /* get_by_subject */
70 NULL, /* get_by_issuer_serial */
71 NULL, /* get_by_fingerprint */
72 NULL, /* get_by_alias */
746f3674 73 get_cert_by_subject_ex, /* get_by_subject_ex */
d8652be0 74 NULL, /* ctrl_ex */
0f113f3e 75};
d02b48c6 76
6b691a5c 77X509_LOOKUP_METHOD *X509_LOOKUP_hash_dir(void)
0f113f3e 78{
26a7d938 79 return &x509_dir_lookup;
0f113f3e 80}
d02b48c6 81
303c0028 82static int dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl,
0f113f3e
MC
83 char **retp)
84{
85 int ret = 0;
7fcdbd83 86 BY_DIR *ld = (BY_DIR *)ctx->method_data;
0f113f3e
MC
87
88 switch (cmd) {
89 case X509_L_ADD_DIR:
90 if (argl == X509_FILETYPE_DEFAULT) {
5c39a55d 91 const char *dir = ossl_safe_getenv(X509_get_default_cert_dir_env());
7fcdbd83 92
021859bf
HL
93 /*
94 * If SSL_CERT_DIR seems to specify a URI, don't process it as a
95 * directory.
96 */
97 if (dir != NULL && ossl_is_uri(dir))
98 return 0;
99
100 if (dir == NULL)
101 dir = X509_get_default_cert_dir();
102
103 ret = add_cert_dir(ld, dir, X509_FILETYPE_PEM);
0f113f3e 104 if (!ret) {
9311d0c4 105 ERR_raise(ERR_LIB_X509, X509_R_LOADING_CERT_DIR);
0f113f3e
MC
106 }
107 } else
108 ret = add_cert_dir(ld, argp, (int)argl);
109 break;
110 }
26a7d938 111 return ret;
0f113f3e 112}
d02b48c6 113
6b691a5c 114static int new_dir(X509_LOOKUP *lu)
0f113f3e 115{
7fcdbd83 116 BY_DIR *a = OPENSSL_malloc(sizeof(*a));
0f113f3e 117
7fcdbd83 118 if (a == NULL) {
9311d0c4 119 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
4fc4faa7 120 return 0;
7fcdbd83
F
121 }
122
0f113f3e 123 if ((a->buffer = BUF_MEM_new()) == NULL) {
9311d0c4 124 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
7fcdbd83 125 goto err;
0f113f3e
MC
126 }
127 a->dirs = NULL;
4fc4faa7
MC
128 a->lock = CRYPTO_THREAD_lock_new();
129 if (a->lock == NULL) {
130 BUF_MEM_free(a->buffer);
9311d0c4 131 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
7fcdbd83 132 goto err;
4fc4faa7 133 }
0124f32a 134 lu->method_data = a;
4fc4faa7 135 return 1;
7fcdbd83
F
136
137 err:
138 OPENSSL_free(a);
139 return 0;
0f113f3e 140}
d02b48c6 141
5d20c4fb 142static void by_dir_hash_free(BY_DIR_HASH *hash)
0f113f3e
MC
143{
144 OPENSSL_free(hash);
145}
146
147static int by_dir_hash_cmp(const BY_DIR_HASH *const *a,
148 const BY_DIR_HASH *const *b)
149{
150 if ((*a)->hash > (*b)->hash)
151 return 1;
152 if ((*a)->hash < (*b)->hash)
153 return -1;
154 return 0;
155}
5d20c4fb
DSH
156
157static void by_dir_entry_free(BY_DIR_ENTRY *ent)
0f113f3e 158{
b548a1f1 159 OPENSSL_free(ent->dir);
25aaa98a 160 sk_BY_DIR_HASH_pop_free(ent->hashes, by_dir_hash_free);
0f113f3e
MC
161 OPENSSL_free(ent);
162}
5d20c4fb 163
6b691a5c 164static void free_dir(X509_LOOKUP *lu)
0f113f3e 165{
7fcdbd83 166 BY_DIR *a = (BY_DIR *)lu->method_data;
d02b48c6 167
25aaa98a
RS
168 sk_BY_DIR_ENTRY_pop_free(a->dirs, by_dir_entry_free);
169 BUF_MEM_free(a->buffer);
4fc4faa7 170 CRYPTO_THREAD_lock_free(a->lock);
0f113f3e
MC
171 OPENSSL_free(a);
172}
d02b48c6 173
6b691a5c 174static int add_cert_dir(BY_DIR *ctx, const char *dir, int type)
0f113f3e 175{
53a73768
P
176 int j;
177 size_t len;
582e2ed2 178 const char *s, *ss, *p;
0f113f3e 179
12a765a5 180 if (dir == NULL || *dir == '\0') {
9311d0c4 181 ERR_raise(ERR_LIB_X509, X509_R_INVALID_DIRECTORY);
0f113f3e
MC
182 return 0;
183 }
184
185 s = dir;
186 p = s;
187 do {
188 if ((*p == LIST_SEPARATOR_CHAR) || (*p == '\0')) {
189 BY_DIR_ENTRY *ent;
7fcdbd83 190
582e2ed2 191 ss = s;
0f113f3e 192 s = p + 1;
53a73768 193 len = p - ss;
0f113f3e
MC
194 if (len == 0)
195 continue;
196 for (j = 0; j < sk_BY_DIR_ENTRY_num(ctx->dirs); j++) {
197 ent = sk_BY_DIR_ENTRY_value(ctx->dirs, j);
6ffaf15d 198 if (strlen(ent->dir) == len && strncmp(ent->dir, ss, len) == 0)
0f113f3e
MC
199 break;
200 }
201 if (j < sk_BY_DIR_ENTRY_num(ctx->dirs))
202 continue;
203 if (ctx->dirs == NULL) {
204 ctx->dirs = sk_BY_DIR_ENTRY_new_null();
205 if (!ctx->dirs) {
9311d0c4 206 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
207 return 0;
208 }
209 }
b4faea50 210 ent = OPENSSL_malloc(sizeof(*ent));
7fcdbd83 211 if (ent == NULL) {
9311d0c4 212 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
0f113f3e 213 return 0;
7fcdbd83 214 }
0f113f3e
MC
215 ent->dir_type = type;
216 ent->hashes = sk_BY_DIR_HASH_new(by_dir_hash_cmp);
6ffaf15d 217 ent->dir = OPENSSL_strndup(ss, len);
90945fa3 218 if (ent->dir == NULL || ent->hashes == NULL) {
0f113f3e
MC
219 by_dir_entry_free(ent);
220 return 0;
221 }
0f113f3e
MC
222 if (!sk_BY_DIR_ENTRY_push(ctx->dirs, ent)) {
223 by_dir_entry_free(ent);
9311d0c4 224 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
225 return 0;
226 }
227 }
228 } while (*p++ != '\0');
229 return 1;
230}
d02b48c6 231
d8652be0
MC
232static int get_cert_by_subject_ex(X509_LOOKUP *xl, X509_LOOKUP_TYPE type,
233 const X509_NAME *name, X509_OBJECT *ret,
b4250010 234 OSSL_LIB_CTX *libctx, const char *propq)
0f113f3e
MC
235{
236 BY_DIR *ctx;
237 union {
5cf6abd8 238 X509 st_x509;
7aef39a7 239 X509_CRL crl;
0f113f3e
MC
240 } data;
241 int ok = 0;
242 int i, j, k;
243 unsigned long h;
244 BUF_MEM *b = NULL;
245 X509_OBJECT stmp, *tmp;
246 const char *postfix = "";
247
248 if (name == NULL)
26a7d938 249 return 0;
0f113f3e
MC
250
251 stmp.type = type;
252 if (type == X509_LU_X509) {
8cc86b81 253 data.st_x509.cert_info.subject = (X509_NAME *)name; /* won't modify it */
5cf6abd8 254 stmp.data.x509 = &data.st_x509;
0f113f3e 255 } else if (type == X509_LU_CRL) {
8cc86b81 256 data.crl.crl.issuer = (X509_NAME *)name; /* won't modify it */
7aef39a7 257 stmp.data.crl = &data.crl;
0f113f3e
MC
258 postfix = "r";
259 } else {
9311d0c4 260 ERR_raise(ERR_LIB_X509, X509_R_WRONG_LOOKUP_TYPE);
0f113f3e
MC
261 goto finish;
262 }
263
264 if ((b = BUF_MEM_new()) == NULL) {
9311d0c4 265 ERR_raise(ERR_LIB_X509, ERR_R_BUF_LIB);
0f113f3e
MC
266 goto finish;
267 }
268
269 ctx = (BY_DIR *)xl->method_data;
bf973d06
DDO
270 h = X509_NAME_hash_ex(name, libctx, propq, &i);
271 if (i == 0)
272 goto finish;
0f113f3e
MC
273 for (i = 0; i < sk_BY_DIR_ENTRY_num(ctx->dirs); i++) {
274 BY_DIR_ENTRY *ent;
275 int idx;
276 BY_DIR_HASH htmp, *hent;
7fcdbd83 277
0f113f3e
MC
278 ent = sk_BY_DIR_ENTRY_value(ctx->dirs, i);
279 j = strlen(ent->dir) + 1 + 8 + 6 + 1 + 1;
280 if (!BUF_MEM_grow(b, j)) {
9311d0c4 281 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
282 goto finish;
283 }
284 if (type == X509_LU_CRL && ent->hashes) {
285 htmp.hash = h;
cd3f8c1b
RS
286 if (!CRYPTO_THREAD_read_lock(ctx->lock))
287 goto finish;
0f113f3e
MC
288 idx = sk_BY_DIR_HASH_find(ent->hashes, &htmp);
289 if (idx >= 0) {
290 hent = sk_BY_DIR_HASH_value(ent->hashes, idx);
291 k = hent->suffix;
292 } else {
293 hent = NULL;
294 k = 0;
295 }
4fc4faa7 296 CRYPTO_THREAD_unlock(ctx->lock);
0f113f3e
MC
297 } else {
298 k = 0;
299 hent = NULL;
300 }
301 for (;;) {
302 char c = '/';
19431e5e 303
af6dab9b 304#ifdef OPENSSL_SYS_VMS
0f113f3e
MC
305 c = ent->dir[strlen(ent->dir) - 1];
306 if (c != ':' && c != '>' && c != ']') {
307 /*
308 * If no separator is present, we assume the directory
309 * specifier is a logical name, and add a colon. We really
310 * should use better VMS routines for merging things like
311 * this, but this will do for now... -- Richard Levitte
312 */
313 c = ':';
314 } else {
315 c = '\0';
316 }
19431e5e 317
0f113f3e
MC
318 if (c == '\0') {
319 /*
320 * This is special. When c == '\0', no directory separator
321 * should be added.
322 */
323 BIO_snprintf(b->data, b->max,
324 "%s%08lx.%s%d", ent->dir, h, postfix, k);
19431e5e
P
325 } else
326#endif
327 {
a2371fa9
P
328 BIO_snprintf(b->data, b->max,
329 "%s%c%08lx.%s%d", ent->dir, c, h, postfix, k);
0f113f3e 330 }
49e3c9d8 331#ifndef OPENSSL_NO_POSIX_IO
0f113f3e
MC
332# ifdef _WIN32
333# define stat _stat
334# endif
335 {
336 struct stat st;
337 if (stat(b->data, &st) < 0)
338 break;
339 }
49e3c9d8 340#endif
0f113f3e
MC
341 /* found one. */
342 if (type == X509_LU_X509) {
d8652be0
MC
343 if ((X509_load_cert_file_ex(xl, b->data, ent->dir_type, libctx,
344 propq)) == 0)
0f113f3e
MC
345 break;
346 } else if (type == X509_LU_CRL) {
347 if ((X509_load_crl_file(xl, b->data, ent->dir_type)) == 0)
348 break;
349 }
350 /* else case will caught higher up */
351 k++;
352 }
353
354 /*
355 * we have added it to the cache so now pull it out again
356 */
a161738a 357 X509_STORE_lock(xl->store_ctx);
0f113f3e 358 j = sk_X509_OBJECT_find(xl->store_ctx->objs, &stmp);
5b37fef0 359 tmp = sk_X509_OBJECT_value(xl->store_ctx->objs, j);
a161738a 360 X509_STORE_unlock(xl->store_ctx);
0f113f3e
MC
361
362 /* If a CRL, update the last file suffix added for this */
363
364 if (type == X509_LU_CRL) {
cd3f8c1b
RS
365 if (!CRYPTO_THREAD_write_lock(ctx->lock))
366 goto finish;
0f113f3e
MC
367 /*
368 * Look for entry again in case another thread added an entry
369 * first.
370 */
5b37fef0 371 if (hent == NULL) {
0f113f3e
MC
372 htmp.hash = h;
373 idx = sk_BY_DIR_HASH_find(ent->hashes, &htmp);
5b37fef0 374 hent = sk_BY_DIR_HASH_value(ent->hashes, idx);
0f113f3e 375 }
7fcdbd83 376 if (hent == NULL) {
b4faea50 377 hent = OPENSSL_malloc(sizeof(*hent));
0f113f3e 378 if (hent == NULL) {
4fc4faa7 379 CRYPTO_THREAD_unlock(ctx->lock);
9311d0c4 380 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
381 ok = 0;
382 goto finish;
383 }
384 hent->hash = h;
385 hent->suffix = k;
386 if (!sk_BY_DIR_HASH_push(ent->hashes, hent)) {
4fc4faa7 387 CRYPTO_THREAD_unlock(ctx->lock);
0f113f3e 388 OPENSSL_free(hent);
9311d0c4 389 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
390 ok = 0;
391 goto finish;
392 }
4fc4faa7 393 } else if (hent->suffix < k) {
0f113f3e 394 hent->suffix = k;
4fc4faa7 395 }
0f113f3e 396
4fc4faa7 397 CRYPTO_THREAD_unlock(ctx->lock);
0f113f3e
MC
398
399 }
400
401 if (tmp != NULL) {
402 ok = 1;
403 ret->type = tmp->type;
404 memcpy(&ret->data, &tmp->data, sizeof(ret->data));
c0452248
RS
405
406 /*
407 * Clear any errors that might have been raised processing empty
408 * or malformed files.
409 */
410 ERR_clear_error();
411
0f113f3e
MC
412 goto finish;
413 }
414 }
415 finish:
25aaa98a 416 BUF_MEM_free(b);
26a7d938 417 return ok;
0f113f3e 418}
6725682d
SL
419
420static int get_cert_by_subject(X509_LOOKUP *xl, X509_LOOKUP_TYPE type,
421 const X509_NAME *name, X509_OBJECT *ret)
422{
d8652be0 423 return get_cert_by_subject_ex(xl, type, name, ret, NULL, NULL);
6725682d 424}