]>
Commit | Line | Data |
---|---|---|
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 | 34 | struct lookup_dir_hashes_st { |
0f113f3e MC |
35 | unsigned long hash; |
36 | int suffix; | |
4a1f3f27 | 37 | }; |
5d20c4fb | 38 | |
4a1f3f27 | 39 | struct 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 |
45 | typedef 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 | 51 | static int dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl, |
6725682d SL |
52 | char **retp); |
53 | ||
d02b48c6 RE |
54 | static int new_dir(X509_LOOKUP *lu); |
55 | static void free_dir(X509_LOOKUP *lu); | |
0f113f3e | 56 | static int add_cert_dir(BY_DIR *ctx, const char *dir, int type); |
0946a198 | 57 | static int get_cert_by_subject(X509_LOOKUP *xl, X509_LOOKUP_TYPE type, |
8cc86b81 | 58 | const X509_NAME *name, X509_OBJECT *ret); |
d8652be0 MC |
59 | static 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 | 62 | static 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 | 77 | X509_LOOKUP_METHOD *X509_LOOKUP_hash_dir(void) |
0f113f3e | 78 | { |
26a7d938 | 79 | return &x509_dir_lookup; |
0f113f3e | 80 | } |
d02b48c6 | 81 | |
303c0028 | 82 | static 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) { | |
dfdbc113 | 91 | const char *dir = ossl_safe_getenv(X509_get_default_cert_dir_env()); |
7fcdbd83 | 92 | |
dfdbc113 HL |
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); | |
0f113f3e | 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 | 108 | static int new_dir(X509_LOOKUP *lu) |
0f113f3e | 109 | { |
7fcdbd83 | 110 | BY_DIR *a = OPENSSL_malloc(sizeof(*a)); |
0f113f3e | 111 | |
e077455e | 112 | if (a == NULL) |
4fc4faa7 | 113 | return 0; |
7fcdbd83 | 114 | |
0f113f3e | 115 | if ((a->buffer = BUF_MEM_new()) == NULL) { |
e077455e | 116 | ERR_raise(ERR_LIB_X509, ERR_R_BN_LIB); |
7fcdbd83 | 117 | goto err; |
0f113f3e MC |
118 | } |
119 | a->dirs = NULL; | |
4fc4faa7 MC |
120 | a->lock = CRYPTO_THREAD_lock_new(); |
121 | if (a->lock == NULL) { | |
122 | BUF_MEM_free(a->buffer); | |
e077455e | 123 | ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB); |
7fcdbd83 | 124 | goto err; |
4fc4faa7 | 125 | } |
0124f32a | 126 | lu->method_data = a; |
4fc4faa7 | 127 | return 1; |
7fcdbd83 F |
128 | |
129 | err: | |
130 | OPENSSL_free(a); | |
131 | return 0; | |
0f113f3e | 132 | } |
d02b48c6 | 133 | |
5d20c4fb | 134 | static void by_dir_hash_free(BY_DIR_HASH *hash) |
0f113f3e MC |
135 | { |
136 | OPENSSL_free(hash); | |
137 | } | |
138 | ||
139 | static int by_dir_hash_cmp(const BY_DIR_HASH *const *a, | |
140 | const BY_DIR_HASH *const *b) | |
141 | { | |
142 | if ((*a)->hash > (*b)->hash) | |
143 | return 1; | |
144 | if ((*a)->hash < (*b)->hash) | |
145 | return -1; | |
146 | return 0; | |
147 | } | |
5d20c4fb DSH |
148 | |
149 | static void by_dir_entry_free(BY_DIR_ENTRY *ent) | |
0f113f3e | 150 | { |
b548a1f1 | 151 | OPENSSL_free(ent->dir); |
25aaa98a | 152 | sk_BY_DIR_HASH_pop_free(ent->hashes, by_dir_hash_free); |
0f113f3e MC |
153 | OPENSSL_free(ent); |
154 | } | |
5d20c4fb | 155 | |
6b691a5c | 156 | static void free_dir(X509_LOOKUP *lu) |
0f113f3e | 157 | { |
7fcdbd83 | 158 | BY_DIR *a = (BY_DIR *)lu->method_data; |
d02b48c6 | 159 | |
25aaa98a RS |
160 | sk_BY_DIR_ENTRY_pop_free(a->dirs, by_dir_entry_free); |
161 | BUF_MEM_free(a->buffer); | |
4fc4faa7 | 162 | CRYPTO_THREAD_lock_free(a->lock); |
0f113f3e MC |
163 | OPENSSL_free(a); |
164 | } | |
d02b48c6 | 165 | |
6b691a5c | 166 | static int add_cert_dir(BY_DIR *ctx, const char *dir, int type) |
0f113f3e | 167 | { |
53a73768 P |
168 | int j; |
169 | size_t len; | |
582e2ed2 | 170 | const char *s, *ss, *p; |
0f113f3e | 171 | |
12a765a5 | 172 | if (dir == NULL || *dir == '\0') { |
9311d0c4 | 173 | ERR_raise(ERR_LIB_X509, X509_R_INVALID_DIRECTORY); |
0f113f3e MC |
174 | return 0; |
175 | } | |
176 | ||
177 | s = dir; | |
178 | p = s; | |
179 | do { | |
180 | if ((*p == LIST_SEPARATOR_CHAR) || (*p == '\0')) { | |
181 | BY_DIR_ENTRY *ent; | |
7fcdbd83 | 182 | |
582e2ed2 | 183 | ss = s; |
0f113f3e | 184 | s = p + 1; |
53a73768 | 185 | len = p - ss; |
0f113f3e MC |
186 | if (len == 0) |
187 | continue; | |
188 | for (j = 0; j < sk_BY_DIR_ENTRY_num(ctx->dirs); j++) { | |
189 | ent = sk_BY_DIR_ENTRY_value(ctx->dirs, j); | |
6ffaf15d | 190 | if (strlen(ent->dir) == len && strncmp(ent->dir, ss, len) == 0) |
0f113f3e MC |
191 | break; |
192 | } | |
193 | if (j < sk_BY_DIR_ENTRY_num(ctx->dirs)) | |
194 | continue; | |
195 | if (ctx->dirs == NULL) { | |
196 | ctx->dirs = sk_BY_DIR_ENTRY_new_null(); | |
197 | if (!ctx->dirs) { | |
e077455e | 198 | ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB); |
0f113f3e MC |
199 | return 0; |
200 | } | |
201 | } | |
b4faea50 | 202 | ent = OPENSSL_malloc(sizeof(*ent)); |
e077455e | 203 | if (ent == NULL) |
0f113f3e MC |
204 | return 0; |
205 | ent->dir_type = type; | |
206 | ent->hashes = sk_BY_DIR_HASH_new(by_dir_hash_cmp); | |
6ffaf15d | 207 | ent->dir = OPENSSL_strndup(ss, len); |
90945fa3 | 208 | if (ent->dir == NULL || ent->hashes == NULL) { |
0f113f3e MC |
209 | by_dir_entry_free(ent); |
210 | return 0; | |
211 | } | |
0f113f3e MC |
212 | if (!sk_BY_DIR_ENTRY_push(ctx->dirs, ent)) { |
213 | by_dir_entry_free(ent); | |
e077455e | 214 | ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB); |
0f113f3e MC |
215 | return 0; |
216 | } | |
217 | } | |
218 | } while (*p++ != '\0'); | |
219 | return 1; | |
220 | } | |
d02b48c6 | 221 | |
d8652be0 MC |
222 | static int get_cert_by_subject_ex(X509_LOOKUP *xl, X509_LOOKUP_TYPE type, |
223 | const X509_NAME *name, X509_OBJECT *ret, | |
b4250010 | 224 | OSSL_LIB_CTX *libctx, const char *propq) |
0f113f3e MC |
225 | { |
226 | BY_DIR *ctx; | |
227 | union { | |
5cf6abd8 | 228 | X509 st_x509; |
7aef39a7 | 229 | X509_CRL crl; |
0f113f3e MC |
230 | } data; |
231 | int ok = 0; | |
232 | int i, j, k; | |
233 | unsigned long h; | |
234 | BUF_MEM *b = NULL; | |
235 | X509_OBJECT stmp, *tmp; | |
236 | const char *postfix = ""; | |
237 | ||
238 | if (name == NULL) | |
26a7d938 | 239 | return 0; |
0f113f3e MC |
240 | |
241 | stmp.type = type; | |
242 | if (type == X509_LU_X509) { | |
8cc86b81 | 243 | data.st_x509.cert_info.subject = (X509_NAME *)name; /* won't modify it */ |
5cf6abd8 | 244 | stmp.data.x509 = &data.st_x509; |
0f113f3e | 245 | } else if (type == X509_LU_CRL) { |
8cc86b81 | 246 | data.crl.crl.issuer = (X509_NAME *)name; /* won't modify it */ |
7aef39a7 | 247 | stmp.data.crl = &data.crl; |
0f113f3e MC |
248 | postfix = "r"; |
249 | } else { | |
9311d0c4 | 250 | ERR_raise(ERR_LIB_X509, X509_R_WRONG_LOOKUP_TYPE); |
0f113f3e MC |
251 | goto finish; |
252 | } | |
253 | ||
254 | if ((b = BUF_MEM_new()) == NULL) { | |
9311d0c4 | 255 | ERR_raise(ERR_LIB_X509, ERR_R_BUF_LIB); |
0f113f3e MC |
256 | goto finish; |
257 | } | |
258 | ||
259 | ctx = (BY_DIR *)xl->method_data; | |
bf973d06 DDO |
260 | h = X509_NAME_hash_ex(name, libctx, propq, &i); |
261 | if (i == 0) | |
262 | goto finish; | |
0f113f3e MC |
263 | for (i = 0; i < sk_BY_DIR_ENTRY_num(ctx->dirs); i++) { |
264 | BY_DIR_ENTRY *ent; | |
265 | int idx; | |
266 | BY_DIR_HASH htmp, *hent; | |
7fcdbd83 | 267 | |
0f113f3e MC |
268 | ent = sk_BY_DIR_ENTRY_value(ctx->dirs, i); |
269 | j = strlen(ent->dir) + 1 + 8 + 6 + 1 + 1; | |
270 | if (!BUF_MEM_grow(b, j)) { | |
e077455e | 271 | ERR_raise(ERR_LIB_X509, ERR_R_BUF_LIB); |
0f113f3e MC |
272 | goto finish; |
273 | } | |
274 | if (type == X509_LU_CRL && ent->hashes) { | |
275 | htmp.hash = h; | |
cd3f8c1b RS |
276 | if (!CRYPTO_THREAD_read_lock(ctx->lock)) |
277 | goto finish; | |
0f113f3e MC |
278 | idx = sk_BY_DIR_HASH_find(ent->hashes, &htmp); |
279 | if (idx >= 0) { | |
280 | hent = sk_BY_DIR_HASH_value(ent->hashes, idx); | |
281 | k = hent->suffix; | |
282 | } else { | |
283 | hent = NULL; | |
284 | k = 0; | |
285 | } | |
4fc4faa7 | 286 | CRYPTO_THREAD_unlock(ctx->lock); |
0f113f3e MC |
287 | } else { |
288 | k = 0; | |
289 | hent = NULL; | |
290 | } | |
291 | for (;;) { | |
292 | char c = '/'; | |
19431e5e | 293 | |
af6dab9b | 294 | #ifdef OPENSSL_SYS_VMS |
0f113f3e MC |
295 | c = ent->dir[strlen(ent->dir) - 1]; |
296 | if (c != ':' && c != '>' && c != ']') { | |
297 | /* | |
298 | * If no separator is present, we assume the directory | |
299 | * specifier is a logical name, and add a colon. We really | |
300 | * should use better VMS routines for merging things like | |
301 | * this, but this will do for now... -- Richard Levitte | |
302 | */ | |
303 | c = ':'; | |
304 | } else { | |
305 | c = '\0'; | |
306 | } | |
19431e5e | 307 | |
0f113f3e MC |
308 | if (c == '\0') { |
309 | /* | |
310 | * This is special. When c == '\0', no directory separator | |
311 | * should be added. | |
312 | */ | |
313 | BIO_snprintf(b->data, b->max, | |
314 | "%s%08lx.%s%d", ent->dir, h, postfix, k); | |
19431e5e P |
315 | } else |
316 | #endif | |
317 | { | |
a2371fa9 P |
318 | BIO_snprintf(b->data, b->max, |
319 | "%s%c%08lx.%s%d", ent->dir, c, h, postfix, k); | |
0f113f3e | 320 | } |
49e3c9d8 | 321 | #ifndef OPENSSL_NO_POSIX_IO |
0f113f3e MC |
322 | # ifdef _WIN32 |
323 | # define stat _stat | |
324 | # endif | |
325 | { | |
326 | struct stat st; | |
327 | if (stat(b->data, &st) < 0) | |
328 | break; | |
329 | } | |
49e3c9d8 | 330 | #endif |
0f113f3e MC |
331 | /* found one. */ |
332 | if (type == X509_LU_X509) { | |
d8652be0 MC |
333 | if ((X509_load_cert_file_ex(xl, b->data, ent->dir_type, libctx, |
334 | propq)) == 0) | |
0f113f3e MC |
335 | break; |
336 | } else if (type == X509_LU_CRL) { | |
337 | if ((X509_load_crl_file(xl, b->data, ent->dir_type)) == 0) | |
338 | break; | |
339 | } | |
340 | /* else case will caught higher up */ | |
341 | k++; | |
342 | } | |
343 | ||
344 | /* | |
345 | * we have added it to the cache so now pull it out again | |
efe0222f P |
346 | * |
347 | * Note: quadratic time find here since the objects won't generally be | |
348 | * sorted and sorting the would result in O(n^2 log n) complexity. | |
0f113f3e | 349 | */ |
50001e0e MC |
350 | if (k > 0) { |
351 | X509_STORE_lock(xl->store_ctx); | |
352 | j = sk_X509_OBJECT_find(xl->store_ctx->objs, &stmp); | |
353 | tmp = sk_X509_OBJECT_value(xl->store_ctx->objs, j); | |
354 | X509_STORE_unlock(xl->store_ctx); | |
355 | } else { | |
50001e0e MC |
356 | tmp = NULL; |
357 | } | |
3147785e HL |
358 | /* |
359 | * If a CRL, update the last file suffix added for this. | |
360 | * We don't need to add an entry if k is 0 as this is the initial value. | |
361 | * This avoids the need for a write lock and sort operation in the | |
362 | * simple case where no CRL is present for a hash. | |
363 | */ | |
364 | if (type == X509_LU_CRL && k > 0) { | |
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); |
0f113f3e MC |
380 | ok = 0; |
381 | goto finish; | |
382 | } | |
383 | hent->hash = h; | |
384 | hent->suffix = k; | |
385 | if (!sk_BY_DIR_HASH_push(ent->hashes, hent)) { | |
4fc4faa7 | 386 | CRYPTO_THREAD_unlock(ctx->lock); |
0f113f3e | 387 | OPENSSL_free(hent); |
e077455e | 388 | ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB); |
0f113f3e MC |
389 | ok = 0; |
390 | goto finish; | |
391 | } | |
3147785e HL |
392 | |
393 | /* | |
394 | * Ensure stack is sorted so that subsequent sk_BY_DIR_HASH_find | |
395 | * will not mutate the stack and therefore require a write lock. | |
396 | */ | |
397 | sk_BY_DIR_HASH_sort(ent->hashes); | |
4fc4faa7 | 398 | } else if (hent->suffix < k) { |
0f113f3e | 399 | hent->suffix = k; |
4fc4faa7 | 400 | } |
0f113f3e | 401 | |
4fc4faa7 | 402 | CRYPTO_THREAD_unlock(ctx->lock); |
0f113f3e MC |
403 | |
404 | } | |
405 | ||
406 | if (tmp != NULL) { | |
407 | ok = 1; | |
408 | ret->type = tmp->type; | |
409 | memcpy(&ret->data, &tmp->data, sizeof(ret->data)); | |
c0452248 RS |
410 | |
411 | /* | |
412 | * Clear any errors that might have been raised processing empty | |
413 | * or malformed files. | |
414 | */ | |
415 | ERR_clear_error(); | |
416 | ||
0f113f3e MC |
417 | goto finish; |
418 | } | |
419 | } | |
420 | finish: | |
efe0222f P |
421 | /* If we changed anything, resort the objects for faster lookup */ |
422 | if (!sk_X509_OBJECT_is_sorted(xl->store_ctx->objs)) { | |
423 | X509_STORE_lock(xl->store_ctx); | |
424 | sk_X509_OBJECT_sort(xl->store_ctx->objs); | |
425 | X509_STORE_unlock(xl->store_ctx); | |
426 | } | |
427 | ||
25aaa98a | 428 | BUF_MEM_free(b); |
26a7d938 | 429 | return ok; |
0f113f3e | 430 | } |
6725682d SL |
431 | |
432 | static int get_cert_by_subject(X509_LOOKUP *xl, X509_LOOKUP_TYPE type, | |
433 | const X509_NAME *name, X509_OBJECT *ret) | |
434 | { | |
d8652be0 | 435 | return get_cert_by_subject_ex(xl, type, name, ret, NULL, NULL); |
6725682d | 436 | } |