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