]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/x509/by_dir.c
Corrected missing definitions from NonStop SPT build.
[thirdparty/openssl.git] / crypto / x509 / by_dir.c
CommitLineData
b1322259 1/*
4333b89f 2 * Copyright 1995-2021 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
07016a8a
P
19#include "e_os.h"
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
0f113f3e
MC
93 if (dir)
94 ret = add_cert_dir(ld, dir, X509_FILETYPE_PEM);
95 else
96 ret = add_cert_dir(ld, X509_get_default_cert_dir(),
97 X509_FILETYPE_PEM);
98 if (!ret) {
9311d0c4 99 ERR_raise(ERR_LIB_X509, X509_R_LOADING_CERT_DIR);
0f113f3e
MC
100 }
101 } else
102 ret = add_cert_dir(ld, argp, (int)argl);
103 break;
104 }
26a7d938 105 return ret;
0f113f3e 106}
d02b48c6 107
6b691a5c 108static int new_dir(X509_LOOKUP *lu)
0f113f3e 109{
7fcdbd83 110 BY_DIR *a = OPENSSL_malloc(sizeof(*a));
0f113f3e 111
7fcdbd83 112 if (a == NULL) {
9311d0c4 113 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
4fc4faa7 114 return 0;
7fcdbd83
F
115 }
116
0f113f3e 117 if ((a->buffer = BUF_MEM_new()) == NULL) {
9311d0c4 118 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
7fcdbd83 119 goto err;
0f113f3e
MC
120 }
121 a->dirs = NULL;
4fc4faa7
MC
122 a->lock = CRYPTO_THREAD_lock_new();
123 if (a->lock == NULL) {
124 BUF_MEM_free(a->buffer);
9311d0c4 125 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
7fcdbd83 126 goto err;
4fc4faa7 127 }
0124f32a 128 lu->method_data = a;
4fc4faa7 129 return 1;
7fcdbd83
F
130
131 err:
132 OPENSSL_free(a);
133 return 0;
0f113f3e 134}
d02b48c6 135
5d20c4fb 136static void by_dir_hash_free(BY_DIR_HASH *hash)
0f113f3e
MC
137{
138 OPENSSL_free(hash);
139}
140
141static int by_dir_hash_cmp(const BY_DIR_HASH *const *a,
142 const BY_DIR_HASH *const *b)
143{
144 if ((*a)->hash > (*b)->hash)
145 return 1;
146 if ((*a)->hash < (*b)->hash)
147 return -1;
148 return 0;
149}
5d20c4fb
DSH
150
151static void by_dir_entry_free(BY_DIR_ENTRY *ent)
0f113f3e 152{
b548a1f1 153 OPENSSL_free(ent->dir);
25aaa98a 154 sk_BY_DIR_HASH_pop_free(ent->hashes, by_dir_hash_free);
0f113f3e
MC
155 OPENSSL_free(ent);
156}
5d20c4fb 157
6b691a5c 158static void free_dir(X509_LOOKUP *lu)
0f113f3e 159{
7fcdbd83 160 BY_DIR *a = (BY_DIR *)lu->method_data;
d02b48c6 161
25aaa98a
RS
162 sk_BY_DIR_ENTRY_pop_free(a->dirs, by_dir_entry_free);
163 BUF_MEM_free(a->buffer);
4fc4faa7 164 CRYPTO_THREAD_lock_free(a->lock);
0f113f3e
MC
165 OPENSSL_free(a);
166}
d02b48c6 167
6b691a5c 168static int add_cert_dir(BY_DIR *ctx, const char *dir, int type)
0f113f3e 169{
53a73768
P
170 int j;
171 size_t len;
582e2ed2 172 const char *s, *ss, *p;
0f113f3e 173
12a765a5 174 if (dir == NULL || *dir == '\0') {
9311d0c4 175 ERR_raise(ERR_LIB_X509, X509_R_INVALID_DIRECTORY);
0f113f3e
MC
176 return 0;
177 }
178
179 s = dir;
180 p = s;
181 do {
182 if ((*p == LIST_SEPARATOR_CHAR) || (*p == '\0')) {
183 BY_DIR_ENTRY *ent;
7fcdbd83 184
582e2ed2 185 ss = s;
0f113f3e 186 s = p + 1;
53a73768 187 len = p - ss;
0f113f3e
MC
188 if (len == 0)
189 continue;
190 for (j = 0; j < sk_BY_DIR_ENTRY_num(ctx->dirs); j++) {
191 ent = sk_BY_DIR_ENTRY_value(ctx->dirs, j);
6ffaf15d 192 if (strlen(ent->dir) == len && strncmp(ent->dir, ss, len) == 0)
0f113f3e
MC
193 break;
194 }
195 if (j < sk_BY_DIR_ENTRY_num(ctx->dirs))
196 continue;
197 if (ctx->dirs == NULL) {
198 ctx->dirs = sk_BY_DIR_ENTRY_new_null();
199 if (!ctx->dirs) {
9311d0c4 200 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
201 return 0;
202 }
203 }
b4faea50 204 ent = OPENSSL_malloc(sizeof(*ent));
7fcdbd83 205 if (ent == NULL) {
9311d0c4 206 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
0f113f3e 207 return 0;
7fcdbd83 208 }
0f113f3e
MC
209 ent->dir_type = type;
210 ent->hashes = sk_BY_DIR_HASH_new(by_dir_hash_cmp);
6ffaf15d 211 ent->dir = OPENSSL_strndup(ss, len);
90945fa3 212 if (ent->dir == NULL || ent->hashes == NULL) {
0f113f3e
MC
213 by_dir_entry_free(ent);
214 return 0;
215 }
0f113f3e
MC
216 if (!sk_BY_DIR_ENTRY_push(ctx->dirs, ent)) {
217 by_dir_entry_free(ent);
9311d0c4 218 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
219 return 0;
220 }
221 }
222 } while (*p++ != '\0');
223 return 1;
224}
d02b48c6 225
d8652be0
MC
226static int get_cert_by_subject_ex(X509_LOOKUP *xl, X509_LOOKUP_TYPE type,
227 const X509_NAME *name, X509_OBJECT *ret,
b4250010 228 OSSL_LIB_CTX *libctx, const char *propq)
0f113f3e
MC
229{
230 BY_DIR *ctx;
231 union {
5cf6abd8 232 X509 st_x509;
7aef39a7 233 X509_CRL crl;
0f113f3e
MC
234 } data;
235 int ok = 0;
236 int i, j, k;
237 unsigned long h;
238 BUF_MEM *b = NULL;
239 X509_OBJECT stmp, *tmp;
240 const char *postfix = "";
241
242 if (name == NULL)
26a7d938 243 return 0;
0f113f3e
MC
244
245 stmp.type = type;
246 if (type == X509_LU_X509) {
8cc86b81 247 data.st_x509.cert_info.subject = (X509_NAME *)name; /* won't modify it */
5cf6abd8 248 stmp.data.x509 = &data.st_x509;
0f113f3e 249 } else if (type == X509_LU_CRL) {
8cc86b81 250 data.crl.crl.issuer = (X509_NAME *)name; /* won't modify it */
7aef39a7 251 stmp.data.crl = &data.crl;
0f113f3e
MC
252 postfix = "r";
253 } else {
9311d0c4 254 ERR_raise(ERR_LIB_X509, X509_R_WRONG_LOOKUP_TYPE);
0f113f3e
MC
255 goto finish;
256 }
257
258 if ((b = BUF_MEM_new()) == NULL) {
9311d0c4 259 ERR_raise(ERR_LIB_X509, ERR_R_BUF_LIB);
0f113f3e
MC
260 goto finish;
261 }
262
263 ctx = (BY_DIR *)xl->method_data;
bf973d06
DDO
264 h = X509_NAME_hash_ex(name, libctx, propq, &i);
265 if (i == 0)
266 goto finish;
0f113f3e
MC
267 for (i = 0; i < sk_BY_DIR_ENTRY_num(ctx->dirs); i++) {
268 BY_DIR_ENTRY *ent;
269 int idx;
270 BY_DIR_HASH htmp, *hent;
7fcdbd83 271
0f113f3e
MC
272 ent = sk_BY_DIR_ENTRY_value(ctx->dirs, i);
273 j = strlen(ent->dir) + 1 + 8 + 6 + 1 + 1;
274 if (!BUF_MEM_grow(b, j)) {
9311d0c4 275 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
276 goto finish;
277 }
278 if (type == X509_LU_CRL && ent->hashes) {
279 htmp.hash = h;
cd3f8c1b
RS
280 if (!CRYPTO_THREAD_read_lock(ctx->lock))
281 goto finish;
0f113f3e
MC
282 idx = sk_BY_DIR_HASH_find(ent->hashes, &htmp);
283 if (idx >= 0) {
284 hent = sk_BY_DIR_HASH_value(ent->hashes, idx);
285 k = hent->suffix;
286 } else {
287 hent = NULL;
288 k = 0;
289 }
4fc4faa7 290 CRYPTO_THREAD_unlock(ctx->lock);
0f113f3e
MC
291 } else {
292 k = 0;
293 hent = NULL;
294 }
295 for (;;) {
296 char c = '/';
19431e5e 297
af6dab9b 298#ifdef OPENSSL_SYS_VMS
0f113f3e
MC
299 c = ent->dir[strlen(ent->dir) - 1];
300 if (c != ':' && c != '>' && c != ']') {
301 /*
302 * If no separator is present, we assume the directory
303 * specifier is a logical name, and add a colon. We really
304 * should use better VMS routines for merging things like
305 * this, but this will do for now... -- Richard Levitte
306 */
307 c = ':';
308 } else {
309 c = '\0';
310 }
19431e5e 311
0f113f3e
MC
312 if (c == '\0') {
313 /*
314 * This is special. When c == '\0', no directory separator
315 * should be added.
316 */
317 BIO_snprintf(b->data, b->max,
318 "%s%08lx.%s%d", ent->dir, h, postfix, k);
19431e5e
P
319 } else
320#endif
321 {
a2371fa9
P
322 BIO_snprintf(b->data, b->max,
323 "%s%c%08lx.%s%d", ent->dir, c, h, postfix, k);
0f113f3e 324 }
49e3c9d8 325#ifndef OPENSSL_NO_POSIX_IO
0f113f3e
MC
326# ifdef _WIN32
327# define stat _stat
328# endif
329 {
330 struct stat st;
331 if (stat(b->data, &st) < 0)
332 break;
333 }
49e3c9d8 334#endif
0f113f3e
MC
335 /* found one. */
336 if (type == X509_LU_X509) {
d8652be0
MC
337 if ((X509_load_cert_file_ex(xl, b->data, ent->dir_type, libctx,
338 propq)) == 0)
0f113f3e
MC
339 break;
340 } else if (type == X509_LU_CRL) {
341 if ((X509_load_crl_file(xl, b->data, ent->dir_type)) == 0)
342 break;
343 }
344 /* else case will caught higher up */
345 k++;
346 }
347
348 /*
349 * we have added it to the cache so now pull it out again
350 */
a161738a 351 X509_STORE_lock(xl->store_ctx);
0f113f3e 352 j = sk_X509_OBJECT_find(xl->store_ctx->objs, &stmp);
5b37fef0 353 tmp = sk_X509_OBJECT_value(xl->store_ctx->objs, j);
a161738a 354 X509_STORE_unlock(xl->store_ctx);
0f113f3e
MC
355
356 /* If a CRL, update the last file suffix added for this */
357
358 if (type == X509_LU_CRL) {
cd3f8c1b
RS
359 if (!CRYPTO_THREAD_write_lock(ctx->lock))
360 goto finish;
0f113f3e
MC
361 /*
362 * Look for entry again in case another thread added an entry
363 * first.
364 */
5b37fef0 365 if (hent == NULL) {
0f113f3e
MC
366 htmp.hash = h;
367 idx = sk_BY_DIR_HASH_find(ent->hashes, &htmp);
5b37fef0 368 hent = sk_BY_DIR_HASH_value(ent->hashes, idx);
0f113f3e 369 }
7fcdbd83 370 if (hent == NULL) {
b4faea50 371 hent = OPENSSL_malloc(sizeof(*hent));
0f113f3e 372 if (hent == NULL) {
4fc4faa7 373 CRYPTO_THREAD_unlock(ctx->lock);
9311d0c4 374 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
375 ok = 0;
376 goto finish;
377 }
378 hent->hash = h;
379 hent->suffix = k;
380 if (!sk_BY_DIR_HASH_push(ent->hashes, hent)) {
4fc4faa7 381 CRYPTO_THREAD_unlock(ctx->lock);
0f113f3e 382 OPENSSL_free(hent);
9311d0c4 383 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
384 ok = 0;
385 goto finish;
386 }
4fc4faa7 387 } else if (hent->suffix < k) {
0f113f3e 388 hent->suffix = k;
4fc4faa7 389 }
0f113f3e 390
4fc4faa7 391 CRYPTO_THREAD_unlock(ctx->lock);
0f113f3e
MC
392
393 }
394
395 if (tmp != NULL) {
396 ok = 1;
397 ret->type = tmp->type;
398 memcpy(&ret->data, &tmp->data, sizeof(ret->data));
c0452248
RS
399
400 /*
401 * Clear any errors that might have been raised processing empty
402 * or malformed files.
403 */
404 ERR_clear_error();
405
0f113f3e
MC
406 goto finish;
407 }
408 }
409 finish:
25aaa98a 410 BUF_MEM_free(b);
26a7d938 411 return ok;
0f113f3e 412}
6725682d
SL
413
414static int get_cert_by_subject(X509_LOOKUP *xl, X509_LOOKUP_TYPE type,
415 const X509_NAME *name, X509_OBJECT *ret)
416{
d8652be0 417 return get_cert_by_subject_ex(xl, type, name, ret, NULL, NULL);
6725682d 418}