]> git.ipfire.org Git - thirdparty/openssl.git/blob - providers/implementations/storemgmt/file_store.c
461b7e468d67e5a4488a88063dc7d44be0832259
[thirdparty/openssl.git] / providers / implementations / storemgmt / file_store.c
1 /*
2 * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10 /* This file has quite some overlap with engines/e_loader_attic.c */
11
12 #include "internal/e_os.h" /* To get strncasecmp() on Windows */
13
14 #include <string.h>
15 #include <sys/stat.h>
16 #include <ctype.h> /* isdigit */
17 #include <assert.h>
18
19 #include <openssl/core_dispatch.h>
20 #include <openssl/core_names.h>
21 #include <openssl/core_object.h>
22 #include <openssl/bio.h>
23 #include <openssl/err.h>
24 #include <openssl/params.h>
25 #include <openssl/decoder.h>
26 #include <openssl/proverr.h>
27 #include <openssl/store.h> /* The OSSL_STORE_INFO type numbers */
28 #include "internal/cryptlib.h"
29 #include "internal/o_dir.h"
30 #include "crypto/decoder.h"
31 #include "crypto/ctype.h" /* ossl_isdigit() */
32 #include "prov/implementations.h"
33 #include "prov/bio.h"
34 #include "file_store_local.h"
35
36 DEFINE_STACK_OF(OSSL_STORE_INFO)
37
38 #ifdef _WIN32
39 # define stat _stat
40 #endif
41
42 #ifndef S_ISDIR
43 # define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
44 #endif
45
46 static OSSL_FUNC_store_open_fn file_open;
47 static OSSL_FUNC_store_attach_fn file_attach;
48 static OSSL_FUNC_store_settable_ctx_params_fn file_settable_ctx_params;
49 static OSSL_FUNC_store_set_ctx_params_fn file_set_ctx_params;
50 static OSSL_FUNC_store_load_fn file_load;
51 static OSSL_FUNC_store_eof_fn file_eof;
52 static OSSL_FUNC_store_close_fn file_close;
53
54 /*
55 * This implementation makes full use of OSSL_DECODER, and then some.
56 * It uses its own internal decoder implementation that reads DER and
57 * passes that on to the data callback; this decoder is created with
58 * internal OpenSSL functions, thereby bypassing the need for a surrounding
59 * provider. This is ok, since this is a local decoder, not meant for
60 * public consumption. It also uses the libcrypto internal decoder
61 * setup function ossl_decoder_ctx_setup_for_pkey(), to allow the
62 * last resort decoder to be added first (and thereby be executed last).
63 * Finally, it sets up its own construct and cleanup functions.
64 *
65 * Essentially, that makes this implementation a kind of glorified decoder.
66 */
67
68 struct file_ctx_st {
69 void *provctx;
70 char *uri; /* The URI we currently try to load */
71 enum {
72 IS_FILE = 0, /* Read file and pass results */
73 IS_DIR /* Pass directory entry names */
74 } type;
75
76 union {
77 /* Used with |IS_FILE| */
78 struct {
79 BIO *file;
80
81 OSSL_DECODER_CTX *decoderctx;
82 char *input_type;
83 char *propq; /* The properties we got as a parameter */
84 } file;
85
86 /* Used with |IS_DIR| */
87 struct {
88 OPENSSL_DIR_CTX *ctx;
89 int end_reached;
90
91 /*
92 * When a search expression is given, these are filled in.
93 * |search_name| contains the file basename to look for.
94 * The string is exactly 8 characters long.
95 */
96 char search_name[9];
97
98 /*
99 * The directory reading utility we have combines opening with
100 * reading the first name. To make sure we can detect the end
101 * at the right time, we read early and cache the name.
102 */
103 const char *last_entry;
104 int last_errno;
105 } dir;
106 } _;
107
108 /* Expected object type. May be unspecified */
109 int expected_type;
110 };
111
112 static void free_file_ctx(struct file_ctx_st *ctx)
113 {
114 if (ctx == NULL)
115 return;
116
117 OPENSSL_free(ctx->uri);
118 if (ctx->type != IS_DIR) {
119 OSSL_DECODER_CTX_free(ctx->_.file.decoderctx);
120 OPENSSL_free(ctx->_.file.propq);
121 OPENSSL_free(ctx->_.file.input_type);
122 }
123 OPENSSL_free(ctx);
124 }
125
126 static struct file_ctx_st *new_file_ctx(int type, const char *uri,
127 void *provctx)
128 {
129 struct file_ctx_st *ctx = NULL;
130
131 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) != NULL
132 && (uri == NULL || (ctx->uri = OPENSSL_strdup(uri)) != NULL)) {
133 ctx->type = type;
134 ctx->provctx = provctx;
135 return ctx;
136 }
137 free_file_ctx(ctx);
138 return NULL;
139 }
140
141 static OSSL_DECODER_CONSTRUCT file_load_construct;
142 static OSSL_DECODER_CLEANUP file_load_cleanup;
143
144 /*-
145 * Opening / attaching streams and directories
146 * -------------------------------------------
147 */
148
149 /*
150 * Function to service both file_open() and file_attach()
151 *
152 *
153 */
154 static struct file_ctx_st *file_open_stream(BIO *source, const char *uri,
155 void *provctx)
156 {
157 struct file_ctx_st *ctx;
158
159 if ((ctx = new_file_ctx(IS_FILE, uri, provctx)) == NULL) {
160 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
161 goto err;
162 }
163
164 ctx->_.file.file = source;
165
166 return ctx;
167 err:
168 free_file_ctx(ctx);
169 return NULL;
170 }
171
172 static void *file_open_dir(const char *path, const char *uri, void *provctx)
173 {
174 struct file_ctx_st *ctx;
175
176 if ((ctx = new_file_ctx(IS_DIR, uri, provctx)) == NULL) {
177 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
178 return NULL;
179 }
180
181 ctx->_.dir.last_entry = OPENSSL_DIR_read(&ctx->_.dir.ctx, path);
182 ctx->_.dir.last_errno = errno;
183 if (ctx->_.dir.last_entry == NULL) {
184 if (ctx->_.dir.last_errno != 0) {
185 ERR_raise_data(ERR_LIB_SYS, ctx->_.dir.last_errno,
186 "Calling OPENSSL_DIR_read(\"%s\")", path);
187 goto err;
188 }
189 ctx->_.dir.end_reached = 1;
190 }
191 return ctx;
192 err:
193 file_close(ctx);
194 return NULL;
195 }
196
197 static void *file_open(void *provctx, const char *uri)
198 {
199 struct file_ctx_st *ctx = NULL;
200 struct stat st;
201 struct {
202 const char *path;
203 unsigned int check_absolute:1;
204 } path_data[2];
205 size_t path_data_n = 0, i;
206 const char *path, *p = uri, *q;
207 BIO *bio;
208
209 ERR_set_mark();
210
211 /*
212 * First step, just take the URI as is.
213 */
214 path_data[path_data_n].check_absolute = 0;
215 path_data[path_data_n++].path = uri;
216
217 /*
218 * Second step, if the URI appears to start with the "file" scheme,
219 * extract the path and make that the second path to check.
220 * There's a special case if the URI also contains an authority, then
221 * the full URI shouldn't be used as a path anywhere.
222 */
223 if (CHECK_AND_SKIP_CASE_PREFIX(p, "file:")) {
224 q = p;
225 if (CHECK_AND_SKIP_CASE_PREFIX(q, "//")) {
226 path_data_n--; /* Invalidate using the full URI */
227 if (CHECK_AND_SKIP_CASE_PREFIX(q, "localhost/")
228 || CHECK_AND_SKIP_CASE_PREFIX(q, "/")) {
229 p = q - 1;
230 } else {
231 ERR_clear_last_mark();
232 ERR_raise(ERR_LIB_PROV, PROV_R_URI_AUTHORITY_UNSUPPORTED);
233 return NULL;
234 }
235 }
236
237 path_data[path_data_n].check_absolute = 1;
238 #ifdef _WIN32
239 /* Windows "file:" URIs with a drive letter start with a '/' */
240 if (p[0] == '/' && p[2] == ':' && p[3] == '/') {
241 char c = tolower(p[1]);
242
243 if (c >= 'a' && c <= 'z') {
244 p++;
245 /* We know it's absolute, so no need to check */
246 path_data[path_data_n].check_absolute = 0;
247 }
248 }
249 #endif
250 path_data[path_data_n++].path = p;
251 }
252
253
254 for (i = 0, path = NULL; path == NULL && i < path_data_n; i++) {
255 /*
256 * If the scheme "file" was an explicit part of the URI, the path must
257 * be absolute. So says RFC 8089
258 */
259 if (path_data[i].check_absolute && path_data[i].path[0] != '/') {
260 ERR_clear_last_mark();
261 ERR_raise_data(ERR_LIB_PROV, PROV_R_PATH_MUST_BE_ABSOLUTE,
262 "Given path=%s", path_data[i].path);
263 return NULL;
264 }
265
266 if (stat(path_data[i].path, &st) < 0) {
267 ERR_raise_data(ERR_LIB_SYS, errno,
268 "calling stat(%s)",
269 path_data[i].path);
270 } else {
271 path = path_data[i].path;
272 }
273 }
274 if (path == NULL) {
275 ERR_clear_last_mark();
276 return NULL;
277 }
278
279 /* Successfully found a working path, clear possible collected errors */
280 ERR_pop_to_mark();
281
282 if (S_ISDIR(st.st_mode))
283 ctx = file_open_dir(path, uri, provctx);
284 else if ((bio = BIO_new_file(path, "rb")) == NULL
285 || (ctx = file_open_stream(bio, uri, provctx)) == NULL)
286 BIO_free_all(bio);
287
288 return ctx;
289 }
290
291 void *file_attach(void *provctx, OSSL_CORE_BIO *cin)
292 {
293 struct file_ctx_st *ctx;
294 BIO *new_bio = ossl_bio_new_from_core_bio(provctx, cin);
295
296 if (new_bio == NULL)
297 return NULL;
298
299 ctx = file_open_stream(new_bio, NULL, provctx);
300 if (ctx == NULL)
301 BIO_free(new_bio);
302 return ctx;
303 }
304
305 /*-
306 * Setting parameters
307 * ------------------
308 */
309
310 static const OSSL_PARAM *file_settable_ctx_params(void *provctx)
311 {
312 static const OSSL_PARAM known_settable_ctx_params[] = {
313 OSSL_PARAM_utf8_string(OSSL_STORE_PARAM_PROPERTIES, NULL, 0),
314 OSSL_PARAM_int(OSSL_STORE_PARAM_EXPECT, NULL),
315 OSSL_PARAM_octet_string(OSSL_STORE_PARAM_SUBJECT, NULL, 0),
316 OSSL_PARAM_utf8_string(OSSL_STORE_PARAM_INPUT_TYPE, NULL, 0),
317 OSSL_PARAM_END
318 };
319 return known_settable_ctx_params;
320 }
321
322 static int file_set_ctx_params(void *loaderctx, const OSSL_PARAM params[])
323 {
324 struct file_ctx_st *ctx = loaderctx;
325 const OSSL_PARAM *p;
326
327 if (params == NULL)
328 return 1;
329
330 if (ctx->type != IS_DIR) {
331 /* these parameters are ignored for directories */
332 p = OSSL_PARAM_locate_const(params, OSSL_STORE_PARAM_PROPERTIES);
333 if (p != NULL) {
334 OPENSSL_free(ctx->_.file.propq);
335 ctx->_.file.propq = NULL;
336 if (!OSSL_PARAM_get_utf8_string(p, &ctx->_.file.propq, 0))
337 return 0;
338 }
339 p = OSSL_PARAM_locate_const(params, OSSL_STORE_PARAM_INPUT_TYPE);
340 if (p != NULL) {
341 OPENSSL_free(ctx->_.file.input_type);
342 ctx->_.file.input_type = NULL;
343 if (!OSSL_PARAM_get_utf8_string(p, &ctx->_.file.input_type, 0))
344 return 0;
345 }
346 }
347 p = OSSL_PARAM_locate_const(params, OSSL_STORE_PARAM_EXPECT);
348 if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->expected_type))
349 return 0;
350 p = OSSL_PARAM_locate_const(params, OSSL_STORE_PARAM_SUBJECT);
351 if (p != NULL) {
352 const unsigned char *der = NULL;
353 size_t der_len = 0;
354 X509_NAME *x509_name;
355 unsigned long hash;
356 int ok;
357
358 if (ctx->type != IS_DIR) {
359 ERR_raise(ERR_LIB_PROV,
360 PROV_R_SEARCH_ONLY_SUPPORTED_FOR_DIRECTORIES);
361 return 0;
362 }
363
364 if (!OSSL_PARAM_get_octet_string_ptr(p, (const void **)&der, &der_len)
365 || (x509_name = d2i_X509_NAME(NULL, &der, der_len)) == NULL)
366 return 0;
367 hash = X509_NAME_hash_ex(x509_name,
368 ossl_prov_ctx_get0_libctx(ctx->provctx), NULL,
369 &ok);
370 BIO_snprintf(ctx->_.dir.search_name, sizeof(ctx->_.dir.search_name),
371 "%08lx", hash);
372 X509_NAME_free(x509_name);
373 if (ok == 0)
374 return 0;
375 }
376 return 1;
377 }
378
379 /*-
380 * Loading an object from a stream
381 * -------------------------------
382 */
383
384 struct file_load_data_st {
385 OSSL_CALLBACK *object_cb;
386 void *object_cbarg;
387 };
388
389 static int file_load_construct(OSSL_DECODER_INSTANCE *decoder_inst,
390 const OSSL_PARAM *params, void *construct_data)
391 {
392 struct file_load_data_st *data = construct_data;
393
394 /*
395 * At some point, we may find it justifiable to recognise PKCS#12 and
396 * handle it specially here, making |file_load()| return pass its
397 * contents one piece at ta time, like |e_loader_attic.c| does.
398 *
399 * However, that currently means parsing them out, which converts the
400 * DER encoded PKCS#12 into a bunch of EVP_PKEYs and X509s, just to
401 * have to re-encode them into DER to create an object abstraction for
402 * each of them.
403 * It's much simpler (less churn) to pass on the object abstraction we
404 * get to the load_result callback and leave it to that one to do the
405 * work. If that's libcrypto code, we know that it has much better
406 * possibilities to handle the EVP_PKEYs and X509s without the extra
407 * churn.
408 */
409
410 return data->object_cb(params, data->object_cbarg);
411 }
412
413 void file_load_cleanup(void *construct_data)
414 {
415 /* Nothing to do */
416 }
417
418 static int file_setup_decoders(struct file_ctx_st *ctx)
419 {
420 OSSL_LIB_CTX *libctx = ossl_prov_ctx_get0_libctx(ctx->provctx);
421 const OSSL_ALGORITHM *to_algo = NULL;
422 int ok = 0;
423
424 /* Setup for this session, so only if not already done */
425 if (ctx->_.file.decoderctx == NULL) {
426 if ((ctx->_.file.decoderctx = OSSL_DECODER_CTX_new()) == NULL) {
427 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
428 goto err;
429 }
430
431 /* Make sure the input type is set */
432 if (!OSSL_DECODER_CTX_set_input_type(ctx->_.file.decoderctx,
433 ctx->_.file.input_type)) {
434 ERR_raise(ERR_LIB_PROV, ERR_R_OSSL_DECODER_LIB);
435 goto err;
436 }
437
438 /*
439 * Where applicable, set the outermost structure name.
440 * The goal is to avoid the STORE object types that are
441 * potentially password protected but aren't interesting
442 * for this load.
443 */
444 switch (ctx->expected_type) {
445 case OSSL_STORE_INFO_CERT:
446 if (!OSSL_DECODER_CTX_set_input_structure(ctx->_.file.decoderctx,
447 "Certificate")) {
448 ERR_raise(ERR_LIB_PROV, ERR_R_OSSL_DECODER_LIB);
449 goto err;
450 }
451 break;
452 case OSSL_STORE_INFO_CRL:
453 if (!OSSL_DECODER_CTX_set_input_structure(ctx->_.file.decoderctx,
454 "CertificateList")) {
455 ERR_raise(ERR_LIB_PROV, ERR_R_OSSL_DECODER_LIB);
456 goto err;
457 }
458 break;
459 default:
460 break;
461 }
462
463 for (to_algo = ossl_any_to_obj_algorithm;
464 to_algo->algorithm_names != NULL;
465 to_algo++) {
466 OSSL_DECODER *to_obj = NULL;
467 OSSL_DECODER_INSTANCE *to_obj_inst = NULL;
468
469 /*
470 * Create the internal last resort decoder implementation
471 * together with a "decoder instance".
472 * The decoder doesn't need any identification or to be
473 * attached to any provider, since it's only used locally.
474 */
475 to_obj = ossl_decoder_from_algorithm(0, to_algo, NULL);
476 if (to_obj != NULL)
477 to_obj_inst = ossl_decoder_instance_new(to_obj, ctx->provctx);
478 OSSL_DECODER_free(to_obj);
479 if (to_obj_inst == NULL)
480 goto err;
481
482 if (!ossl_decoder_ctx_add_decoder_inst(ctx->_.file.decoderctx,
483 to_obj_inst)) {
484 ossl_decoder_instance_free(to_obj_inst);
485 ERR_raise(ERR_LIB_PROV, ERR_R_OSSL_DECODER_LIB);
486 goto err;
487 }
488 }
489 /* Add on the usual extra decoders */
490 if (!OSSL_DECODER_CTX_add_extra(ctx->_.file.decoderctx,
491 libctx, ctx->_.file.propq)) {
492 ERR_raise(ERR_LIB_PROV, ERR_R_OSSL_DECODER_LIB);
493 goto err;
494 }
495
496 /*
497 * Then install our constructor hooks, which just passes decoded
498 * data to the load callback
499 */
500 if (!OSSL_DECODER_CTX_set_construct(ctx->_.file.decoderctx,
501 file_load_construct)
502 || !OSSL_DECODER_CTX_set_cleanup(ctx->_.file.decoderctx,
503 file_load_cleanup)) {
504 ERR_raise(ERR_LIB_PROV, ERR_R_OSSL_DECODER_LIB);
505 goto err;
506 }
507 }
508
509 ok = 1;
510 err:
511 return ok;
512 }
513
514 static int file_load_file(struct file_ctx_st *ctx,
515 OSSL_CALLBACK *object_cb, void *object_cbarg,
516 OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
517 {
518 struct file_load_data_st data;
519 int ret, err;
520
521 /* Setup the decoders (one time shot per session */
522
523 if (!file_setup_decoders(ctx))
524 return 0;
525
526 /* Setup for this object */
527
528 data.object_cb = object_cb;
529 data.object_cbarg = object_cbarg;
530 OSSL_DECODER_CTX_set_construct_data(ctx->_.file.decoderctx, &data);
531 OSSL_DECODER_CTX_set_passphrase_cb(ctx->_.file.decoderctx, pw_cb, pw_cbarg);
532
533 /* Launch */
534
535 ERR_set_mark();
536 ret = OSSL_DECODER_from_bio(ctx->_.file.decoderctx, ctx->_.file.file);
537 if (BIO_eof(ctx->_.file.file)
538 && ((err = ERR_peek_last_error()) != 0)
539 && ERR_GET_LIB(err) == ERR_LIB_OSSL_DECODER
540 && ERR_GET_REASON(err) == ERR_R_UNSUPPORTED)
541 ERR_pop_to_mark();
542 else
543 ERR_clear_last_mark();
544 return ret;
545 }
546
547 /*-
548 * Loading a name object from a directory
549 * --------------------------------------
550 */
551
552 static char *file_name_to_uri(struct file_ctx_st *ctx, const char *name)
553 {
554 char *data = NULL;
555
556 assert(name != NULL);
557 {
558 const char *pathsep = ossl_ends_with_dirsep(ctx->uri) ? "" : "/";
559 long calculated_length = strlen(ctx->uri) + strlen(pathsep)
560 + strlen(name) + 1 /* \0 */;
561
562 data = OPENSSL_zalloc(calculated_length);
563 if (data == NULL) {
564 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
565 return NULL;
566 }
567
568 OPENSSL_strlcat(data, ctx->uri, calculated_length);
569 OPENSSL_strlcat(data, pathsep, calculated_length);
570 OPENSSL_strlcat(data, name, calculated_length);
571 }
572 return data;
573 }
574
575 static int file_name_check(struct file_ctx_st *ctx, const char *name)
576 {
577 const char *p = NULL;
578 size_t len = strlen(ctx->_.dir.search_name);
579
580 /* If there are no search criteria, all names are accepted */
581 if (ctx->_.dir.search_name[0] == '\0')
582 return 1;
583
584 /* If the expected type isn't supported, no name is accepted */
585 if (ctx->expected_type != 0
586 && ctx->expected_type != OSSL_STORE_INFO_CERT
587 && ctx->expected_type != OSSL_STORE_INFO_CRL)
588 return 0;
589
590 /*
591 * First, check the basename
592 */
593 if (strncasecmp(name, ctx->_.dir.search_name, len) != 0 || name[len] != '.')
594 return 0;
595 p = &name[len + 1];
596
597 /*
598 * Then, if the expected type is a CRL, check that the extension starts
599 * with 'r'
600 */
601 if (*p == 'r') {
602 p++;
603 if (ctx->expected_type != 0
604 && ctx->expected_type != OSSL_STORE_INFO_CRL)
605 return 0;
606 } else if (ctx->expected_type == OSSL_STORE_INFO_CRL) {
607 return 0;
608 }
609
610 /*
611 * Last, check that the rest of the extension is a decimal number, at
612 * least one digit long.
613 */
614 if (!isdigit(*p))
615 return 0;
616 while (isdigit(*p))
617 p++;
618
619 #ifdef __VMS
620 /*
621 * One extra step here, check for a possible generation number.
622 */
623 if (*p == ';')
624 for (p++; *p != '\0'; p++)
625 if (!ossl_isdigit(*p))
626 break;
627 #endif
628
629 /*
630 * If we've reached the end of the string at this point, we've successfully
631 * found a fitting file name.
632 */
633 return *p == '\0';
634 }
635
636 static int file_load_dir_entry(struct file_ctx_st *ctx,
637 OSSL_CALLBACK *object_cb, void *object_cbarg,
638 OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
639 {
640 /* Prepare as much as possible in advance */
641 static const int object_type = OSSL_OBJECT_NAME;
642 OSSL_PARAM object[] = {
643 OSSL_PARAM_int(OSSL_OBJECT_PARAM_TYPE, (int *)&object_type),
644 OSSL_PARAM_utf8_string(OSSL_OBJECT_PARAM_DATA, NULL, 0),
645 OSSL_PARAM_END
646 };
647 char *newname = NULL;
648 int ok;
649
650 /* Loop until we get an error or until we have a suitable name */
651 do {
652 if (ctx->_.dir.last_entry == NULL) {
653 if (!ctx->_.dir.end_reached) {
654 assert(ctx->_.dir.last_errno != 0);
655 ERR_raise(ERR_LIB_SYS, ctx->_.dir.last_errno);
656 }
657 /* file_eof() will tell if EOF was reached */
658 return 0;
659 }
660
661 /* flag acceptable names */
662 if (ctx->_.dir.last_entry[0] != '.'
663 && file_name_check(ctx, ctx->_.dir.last_entry)) {
664
665 /* If we can't allocate the new name, we fail */
666 if ((newname =
667 file_name_to_uri(ctx, ctx->_.dir.last_entry)) == NULL)
668 return 0;
669 }
670
671 /*
672 * On the first call (with a NULL context), OPENSSL_DIR_read()
673 * cares about the second argument. On the following calls, it
674 * only cares that it isn't NULL. Therefore, we can safely give
675 * it our URI here.
676 */
677 ctx->_.dir.last_entry = OPENSSL_DIR_read(&ctx->_.dir.ctx, ctx->uri);
678 ctx->_.dir.last_errno = errno;
679 if (ctx->_.dir.last_entry == NULL && ctx->_.dir.last_errno == 0)
680 ctx->_.dir.end_reached = 1;
681 } while (newname == NULL);
682
683 object[1].data = newname;
684 object[1].data_size = strlen(newname);
685 ok = object_cb(object, object_cbarg);
686 OPENSSL_free(newname);
687 return ok;
688 }
689
690 /*-
691 * Loading, local dispatcher
692 * -------------------------
693 */
694
695 static int file_load(void *loaderctx,
696 OSSL_CALLBACK *object_cb, void *object_cbarg,
697 OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
698 {
699 struct file_ctx_st *ctx = loaderctx;
700
701 switch (ctx->type) {
702 case IS_FILE:
703 return file_load_file(ctx, object_cb, object_cbarg, pw_cb, pw_cbarg);
704 case IS_DIR:
705 return
706 file_load_dir_entry(ctx, object_cb, object_cbarg, pw_cb, pw_cbarg);
707 default:
708 break;
709 }
710
711 /* ctx->type has an unexpected value */
712 assert(0);
713 return 0;
714 }
715
716 /*-
717 * Eof detection and closing
718 * -------------------------
719 */
720
721 static int file_eof(void *loaderctx)
722 {
723 struct file_ctx_st *ctx = loaderctx;
724
725 switch (ctx->type) {
726 case IS_DIR:
727 return ctx->_.dir.end_reached;
728 case IS_FILE:
729 /*
730 * BIO_pending() checks any filter BIO.
731 * BIO_eof() checks the source BIO.
732 */
733 return !BIO_pending(ctx->_.file.file)
734 && BIO_eof(ctx->_.file.file);
735 }
736
737 /* ctx->type has an unexpected value */
738 assert(0);
739 return 1;
740 }
741
742 static int file_close_dir(struct file_ctx_st *ctx)
743 {
744 if (ctx->_.dir.ctx != NULL)
745 OPENSSL_DIR_end(&ctx->_.dir.ctx);
746 free_file_ctx(ctx);
747 return 1;
748 }
749
750 static int file_close_stream(struct file_ctx_st *ctx)
751 {
752 /*
753 * This frees either the provider BIO filter (for file_attach()) OR
754 * the allocated file BIO (for file_open()).
755 */
756 BIO_free(ctx->_.file.file);
757 ctx->_.file.file = NULL;
758
759 free_file_ctx(ctx);
760 return 1;
761 }
762
763 static int file_close(void *loaderctx)
764 {
765 struct file_ctx_st *ctx = loaderctx;
766
767 switch (ctx->type) {
768 case IS_DIR:
769 return file_close_dir(ctx);
770 case IS_FILE:
771 return file_close_stream(ctx);
772 }
773
774 /* ctx->type has an unexpected value */
775 assert(0);
776 return 1;
777 }
778
779 const OSSL_DISPATCH ossl_file_store_functions[] = {
780 { OSSL_FUNC_STORE_OPEN, (void (*)(void))file_open },
781 { OSSL_FUNC_STORE_ATTACH, (void (*)(void))file_attach },
782 { OSSL_FUNC_STORE_SETTABLE_CTX_PARAMS,
783 (void (*)(void))file_settable_ctx_params },
784 { OSSL_FUNC_STORE_SET_CTX_PARAMS, (void (*)(void))file_set_ctx_params },
785 { OSSL_FUNC_STORE_LOAD, (void (*)(void))file_load },
786 { OSSL_FUNC_STORE_EOF, (void (*)(void))file_eof },
787 { OSSL_FUNC_STORE_CLOSE, (void (*)(void))file_close },
788 { 0, NULL },
789 };