STORE 'file' scheme loader: Add directory listing capability
[openssl.git] / crypto / store / loader_file.c
index 558980a591394091145c4b0a155eed33ebbb3619..27af75249764e8b7af01669bb98cfba0599d0288 100644 (file)
@@ -8,6 +8,8 @@
  */
 
 #include <string.h>
+#include <sys/stat.h>
+#include <assert.h>
 
 #include <openssl/bio.h>
 #include <openssl/dsa.h>         /* For d2i_DSAPrivateKey */
@@ -21,6 +23,8 @@
 #include <openssl/ui.h>
 #include <openssl/x509.h>        /* For the PKCS8 stuff o.O */
 #include "internal/asn1_int.h"
+#include "internal/o_dir.h"
+#include "internal/cryptlib.h"
 #include "store_locl.h"
 
 #include "e_os.h"
@@ -272,6 +276,69 @@ static FILE_HANDLER PKCS12_handler = {
     1                            /* repeatable */
 };
 
+static OSSL_STORE_INFO *try_decode_PKCS8Encrypted(const char *pem_name,
+                                                  const char *pem_header,
+                                                  const unsigned char *blob,
+                                                  size_t len, void **pctx,
+                                                  const UI_METHOD *ui_method,
+                                                  void *ui_data)
+{
+    X509_SIG *p8 = NULL;
+    char kbuf[PEM_BUFSIZE];
+    char *pass = NULL;
+    const X509_ALGOR *dalg = NULL;
+    const ASN1_OCTET_STRING *doct = NULL;
+    OSSL_STORE_INFO *store_info = NULL;
+    BUF_MEM *mem = NULL;
+    unsigned char *new_data = NULL;
+    int new_data_len;
+
+    if (pem_name != NULL && strcmp(pem_name, PEM_STRING_PKCS8) != 0)
+        return NULL;
+
+    if ((p8 = d2i_X509_SIG(NULL, &blob, len)) == NULL)
+        return NULL;
+
+    if ((mem = BUF_MEM_new()) == NULL) {
+        OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS8ENCRYPTED,
+                      ERR_R_MALLOC_FAILURE);
+        goto nop8;
+    }
+
+    if ((pass = file_get_pass(ui_method, kbuf, PEM_BUFSIZE,
+                              "PKCS8 decrypt password", ui_data)) == NULL) {
+        OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS8ENCRYPTED,
+                      OSSL_STORE_R_BAD_PASSWORD_READ);
+        goto nop8;
+    }
+
+    X509_SIG_get0(p8, &dalg, &doct);
+    if (!PKCS12_pbe_crypt(dalg, pass, strlen(pass), doct->data, doct->length,
+                          &new_data, &new_data_len, 0))
+        goto nop8;
+
+    mem->data = (char *)new_data;
+    mem->max = mem->length = (size_t)new_data_len;
+    X509_SIG_free(p8);
+
+    store_info = ossl_store_info_new_EMBEDDED(PEM_STRING_PKCS8INF, mem);
+    if (store_info == NULL) {
+        OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS8ENCRYPTED,
+                      ERR_R_MALLOC_FAILURE);
+        goto nop8;
+    }
+
+    return store_info;
+ nop8:
+    X509_SIG_free(p8);
+    BUF_MEM_free(mem);
+    return NULL;
+}
+static FILE_HANDLER PKCS8Encrypted_handler = {
+    "PKCS8Encrypted",
+    try_decode_PKCS8Encrypted
+};
+
 int pem_check_suffix(const char *pem_str, const char *suffix);
 static OSSL_STORE_INFO *try_decode_PrivateKey(const char *pem_name,
                                               const char *pem_header,
@@ -285,11 +352,21 @@ static OSSL_STORE_INFO *try_decode_PrivateKey(const char *pem_name,
     const EVP_PKEY_ASN1_METHOD *ameth = NULL;
 
     if (pem_name != NULL) {
-        int slen;
+        if (strcmp(pem_name, PEM_STRING_PKCS8INF) == 0) {
+            PKCS8_PRIV_KEY_INFO *p8inf =
+                d2i_PKCS8_PRIV_KEY_INFO(NULL, &blob, len);
 
-        if ((slen = pem_check_suffix(pem_name, "PRIVATE KEY")) > 0
-            && (ameth = EVP_PKEY_asn1_find_str(NULL, pem_name, slen)) != NULL)
-            pkey = d2i_PrivateKey(ameth->pkey_id, NULL, &blob, len);
+            if (p8inf != NULL)
+                pkey = EVP_PKCS82PKEY(p8inf);
+            PKCS8_PRIV_KEY_INFO_free(p8inf);
+        } else {
+            int slen;
+
+            if ((slen = pem_check_suffix(pem_name, "PRIVATE KEY")) > 0
+                && (ameth = EVP_PKEY_asn1_find_str(NULL, pem_name,
+                                                   slen)) != NULL)
+                pkey = d2i_PrivateKey(ameth->pkey_id, NULL, &blob, len);
+        }
     } else {
         int i;
 
@@ -468,6 +545,7 @@ static FILE_HANDLER X509CRL_handler = {
 
 static const FILE_HANDLER *file_handlers[] = {
     &PKCS12_handler,
+    &PKCS8Encrypted_handler,
     &X509Certificate_handler,
     &X509CRL_handler,
     &params_handler,
@@ -481,23 +559,60 @@ static const FILE_HANDLER *file_handlers[] = {
  */
 
 struct ossl_store_loader_ctx_st {
-    BIO *file;
-    int is_pem;
+    enum {
+        is_raw = 0,
+        is_pem,
+        is_dir
+    } type;
     int errcnt;
+    union {
+        struct {                 /* Used with is_raw and is_pem */
+            BIO *file;
+
+            /*
+             * The following are used when the handler is marked as
+             * repeatable
+             */
+            const FILE_HANDLER *last_handler;
+            void *last_handler_ctx;
+        } file;
+        struct {                 /* Used with is_dir */
+            OPENSSL_DIR_CTX *ctx;
+            int end_reached;
+            char *uri;
 
-    /* The following are used when the handler is marked as repeatable */
-    const FILE_HANDLER *last_handler;
-    void *last_handler_ctx;
+            /*
+             * The directory reading utility we have combines opening with
+             * reading the first name.  To make sure we can detect the end
+             * at the right time, we read early and cache the name.
+             */
+            const char *last_entry;
+            int last_errno;
+        } dir;
+    } _;
 };
 
+static void OSSL_STORE_LOADER_CTX_free(OSSL_STORE_LOADER_CTX *ctx)
+{
+    if (ctx->type == is_dir) {
+        OPENSSL_free(ctx->_.dir.uri);
+    } else {
+        if (ctx->_.file.last_handler != NULL) {
+            ctx->_.file.last_handler->destroy_ctx(&ctx->_.file.last_handler_ctx);
+            ctx->_.file.last_handler_ctx = NULL;
+            ctx->_.file.last_handler = NULL;
+        }
+    }
+    OPENSSL_free(ctx);
+}
+
 static OSSL_STORE_LOADER_CTX *file_open(const OSSL_STORE_LOADER *loader,
                                         const char *uri,
                                         const UI_METHOD *ui_method,
                                         void *ui_data)
 {
-    BIO *buff = NULL;
-    char peekbuf[4096];
     OSSL_STORE_LOADER_CTX *ctx = NULL;
+    struct stat st;
     const char *path = NULL;
 
     if (strncasecmp(uri, "file:", 5) == 0) {
@@ -533,184 +648,366 @@ static OSSL_STORE_LOADER_CTX *file_open(const OSSL_STORE_LOADER *loader,
     }
 
 
+    if (stat(path, &st) < 0) {
+        SYSerr(SYS_F_STAT, errno);
+        ERR_add_error_data(1, path);
+        return NULL;
+    }
+
     ctx = OPENSSL_zalloc(sizeof(*ctx));
     if (ctx == NULL) {
         OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN, ERR_R_MALLOC_FAILURE);
         return NULL;
     }
 
-    if ((buff = BIO_new(BIO_f_buffer())) == NULL)
-        goto err;
-    if ((ctx->file = BIO_new_file(path, "rb")) == NULL) {
-        goto err;
-    }
-    ctx->file = BIO_push(buff, ctx->file);
-    if (BIO_buffer_peek(ctx->file, peekbuf, sizeof(peekbuf)-1) > 0) {
-        peekbuf[sizeof(peekbuf)-1] = '\0';
-        if (strstr(peekbuf, "-----BEGIN ") != NULL)
-            ctx->is_pem = 1;
+    if ((st.st_mode & S_IFDIR) == S_IFDIR) {
+        /*
+         * Try to copy everything, even if we know that some of them must be
+         * NULL for the moment.  This prevents errors in the future, when more
+         * components may be used.
+         */
+        ctx->_.dir.uri = OPENSSL_strdup(uri);
+        ctx->type = is_dir;
+
+        if (ctx->_.dir.uri == NULL)
+            goto err;
+
+        ctx->_.dir.last_entry = OPENSSL_DIR_read(&ctx->_.dir.ctx, path);
+        ctx->_.dir.last_errno = errno;
+        if (ctx->_.dir.last_entry == NULL) {
+            if (ctx->_.dir.last_errno != 0) {
+                char errbuf[256];
+                errno = ctx->_.dir.last_errno;
+                openssl_strerror_r(errno, errbuf, sizeof(errbuf));
+                OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN, ERR_R_SYS_LIB);
+                ERR_add_error_data(1, errbuf);
+                goto err;
+            }
+            ctx->_.dir.end_reached = 1;
+        }
+    } else {
+        BIO *buff = NULL;
+        char peekbuf[4096];
+
+        if ((buff = BIO_new(BIO_f_buffer())) == NULL
+            || (ctx->_.file.file = BIO_new_file(path, "rb")) == NULL) {
+            BIO_free_all(buff);
+            goto err;
+        }
+
+        ctx->_.file.file = BIO_push(buff, ctx->_.file.file);
+        if (BIO_buffer_peek(ctx->_.file.file, peekbuf, sizeof(peekbuf)-1) > 0) {
+            peekbuf[sizeof(peekbuf)-1] = '\0';
+            if (strstr(peekbuf, "-----BEGIN ") != NULL)
+                ctx->type = is_pem;
+        }
     }
 
     return ctx;
  err:
-    if (buff != NULL)
-        BIO_free(buff);
-    OPENSSL_free(ctx);
+    OSSL_STORE_LOADER_CTX_free(ctx);
     return NULL;
 }
 
-static int file_eof(OSSL_STORE_LOADER_CTX *ctx);
-static int file_error(OSSL_STORE_LOADER_CTX *ctx);
-static OSSL_STORE_INFO *file_load(OSSL_STORE_LOADER_CTX *ctx,
-                                  const UI_METHOD *ui_method, void *ui_data)
+static OSSL_STORE_INFO *file_load_try_decode(OSSL_STORE_LOADER_CTX *ctx,
+                                             const char *pem_name,
+                                             const char *pem_header,
+                                             unsigned char *data, size_t len,
+                                             const UI_METHOD *ui_method,
+                                             void *ui_data, int *matchcount)
 {
     OSSL_STORE_INFO *result = NULL;
-    int matchcount = -1;
-
-    if (ctx->last_handler != NULL) {
-        result = ctx->last_handler->try_decode(NULL, NULL, NULL, 0,
-                                               &ctx->last_handler_ctx,
-                                               ui_method, ui_data);
+    BUF_MEM *new_mem = NULL;
+    char *new_pem_name = NULL;
+    int t = 0;
 
-        if (result != NULL)
-            return result;
-
-        ctx->last_handler->destroy_ctx(&ctx->last_handler_ctx);
-        ctx->last_handler_ctx = NULL;
-        ctx->last_handler = NULL;
-    }
+ again:
+    {
+        size_t i = 0;
+        void *handler_ctx = NULL;
+        const FILE_HANDLER **matching_handlers =
+            OPENSSL_zalloc(sizeof(*matching_handlers)
+                           * OSSL_NELEM(file_handlers));
+
+        if (matching_handlers == NULL) {
+            OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD_TRY_DECODE,
+                          ERR_R_MALLOC_FAILURE);
+            goto err;
+        }
 
-    if (file_error(ctx))
-        return NULL;
+        *matchcount = 0;
+        for (i = 0; i < OSSL_NELEM(file_handlers); i++) {
+            const FILE_HANDLER *handler = file_handlers[i];
+            void *tmp_handler_ctx = NULL;
+            OSSL_STORE_INFO *tmp_result =
+                handler->try_decode(pem_name, pem_header, data, len,
+                                    &tmp_handler_ctx, ui_method, ui_data);
+
+            if (tmp_result == NULL) {
+                OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD_TRY_DECODE,
+                              OSSL_STORE_R_IS_NOT_A);
+                ERR_add_error_data(1, handler->name);
+            } else {
+                if (matching_handlers)
+                    matching_handlers[*matchcount] = handler;
 
-    do {
-        char *pem_name = NULL;      /* PEM record name */
-        char *pem_header = NULL;    /* PEM record header */
-        unsigned char *data = NULL; /* DER encoded data */
-        BUF_MEM *mem = NULL;
-        long len = 0;               /* DER encoded data length */
-        int r = 0;
-
-        matchcount = -1;
-        if (ctx->is_pem) {
-            r = PEM_read_bio(ctx->file, &pem_name, &pem_header, &data, &len);
-            if (r <= 0) {
-                if (!file_eof(ctx))
-                    ctx->errcnt++;
-                goto end;
-            }
+                if (handler_ctx)
+                    handler->destroy_ctx(&handler_ctx);
+                handler_ctx = tmp_handler_ctx;
 
-            /*
-             * 10 is the number of characters in "Proc-Type:", which
-             * PEM_get_EVP_CIPHER_INFO() requires to be present.
-             * If the PEM header has less characters than that, it's
-             * not worth spending cycles on it.
-             */
-            if (strlen(pem_header) > 10) {
-                EVP_CIPHER_INFO cipher;
-                struct pem_pass_data pass_data;
-
-                if (!PEM_get_EVP_CIPHER_INFO(pem_header, &cipher)
-                    || !file_fill_pem_pass_data(&pass_data, "PEM", ui_method,
-                                                ui_data)
-                    || !PEM_do_header(&cipher, data, &len, file_get_pem_pass,
-                                      &pass_data)) {
-                    ctx->errcnt++;
-                    goto err;
+                if (++*matchcount == 1) {
+                    result = tmp_result;
+                    tmp_result = NULL;
+                } else {
+                    /* more than one match => ambiguous, kill any result */
+                    OSSL_STORE_INFO_free(result);
+                    OSSL_STORE_INFO_free(tmp_result);
+                    if (handler->destroy_ctx != NULL)
+                        handler->destroy_ctx(&handler_ctx);
+                    handler_ctx = NULL;
+                    result = NULL;
                 }
             }
-        } else {
-            if ((len = asn1_d2i_read_bio(ctx->file, &mem)) < 0) {
-                if (!file_eof(ctx))
-                    ctx->errcnt++;
-                goto err;
-            }
+        }
 
-            data = (unsigned char *)mem->data;
-            len = (long)mem->length;
+        if (*matchcount > 1)
+            OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD_TRY_DECODE,
+                          OSSL_STORE_R_AMBIGUOUS_CONTENT_TYPE);
+        if (*matchcount == 0)
+            OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD_TRY_DECODE,
+                          OSSL_STORE_R_UNSUPPORTED_CONTENT_TYPE);
+        else if (matching_handlers[0]->repeatable) {
+            if (ctx == NULL) {
+                OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD_TRY_DECODE,
+                              OSSL_STORE_R_UNSUPPORTED_CONTENT_TYPE);
+                OSSL_STORE_INFO_free(result);
+                result = NULL;
+            } else {
+                ctx->_.file.last_handler = matching_handlers[0];
+                ctx->_.file.last_handler_ctx = handler_ctx;
+            }
         }
 
+        OPENSSL_free(matching_handlers);
+    }
+
+ err:
+    OPENSSL_free(new_pem_name);
+    BUF_MEM_free(new_mem);
+
+    if (result != NULL
+        && (t = OSSL_STORE_INFO_get_type(result)) == OSSL_STORE_INFO_EMBEDDED) {
+        pem_name = new_pem_name =
+            ossl_store_info_get0_EMBEDDED_pem_name(result);
+        new_mem = ossl_store_info_get0_EMBEDDED_buffer(result);
+        data = (unsigned char *)new_mem->data;
+        len = new_mem->length;
+        OPENSSL_free(result);
         result = NULL;
+        goto again;
+    }
 
-        {
-            size_t i = 0;
-            void *handler_ctx = NULL;
-            const FILE_HANDLER **matching_handlers =
-                OPENSSL_zalloc(sizeof(*matching_handlers)
-                               * OSSL_NELEM(file_handlers));
+    if (result != NULL)
+        ERR_clear_error();
 
-            if (matching_handlers == NULL) {
-                OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD, ERR_R_MALLOC_FAILURE);
-                goto err;
-            }
+    return result;
+}
 
-            matchcount = 0;
-            for (i = 0; i < OSSL_NELEM(file_handlers); i++) {
-                const FILE_HANDLER *handler = file_handlers[i];
-                void *tmp_handler_ctx = NULL;
-                OSSL_STORE_INFO *tmp_result =
-                    handler->try_decode(pem_name, pem_header, data, len,
-                                        &tmp_handler_ctx, ui_method, ui_data);
-
-                if (tmp_result == NULL) {
-                    OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD, OSSL_STORE_R_IS_NOT_A);
-                    ERR_add_error_data(1, handler->name);
-                } else {
-                    if (matching_handlers)
-                        matching_handlers[matchcount] = handler;
+static OSSL_STORE_INFO *file_load_try_repeat(OSSL_STORE_LOADER_CTX *ctx,
+                                             const UI_METHOD *ui_method,
+                                             void *ui_data)
+{
+    OSSL_STORE_INFO *result = NULL;
 
-                    if (handler_ctx)
-                        handler->destroy_ctx(&handler_ctx);
-                    handler_ctx = tmp_handler_ctx;
-
-                    if (++matchcount == 1) {
-                        result = tmp_result;
-                        tmp_result = NULL;
-                    } else {
-                        /* more than one match => ambiguous, kill any result */
-                        OSSL_STORE_INFO_free(result);
-                        OSSL_STORE_INFO_free(tmp_result);
-                        if (handler->destroy_ctx != NULL)
-                            handler->destroy_ctx(&handler_ctx);
-                        handler_ctx = NULL;
-                        result = NULL;
-                    }
+    if (ctx->_.file.last_handler != NULL) {
+        result =
+            ctx->_.file.last_handler->try_decode(NULL, NULL, NULL, 0,
+                                                 &ctx->_.file.last_handler_ctx,
+                                                 ui_method, ui_data);
+
+        if (result == NULL) {
+            ctx->_.file.last_handler->destroy_ctx(&ctx->_.file.last_handler_ctx);
+            ctx->_.file.last_handler_ctx = NULL;
+            ctx->_.file.last_handler = NULL;
+        }
+    }
+    return result;
+}
+
+static int file_read_pem(BIO *bp, char **pem_name, char **pem_header,
+                         unsigned char **data, long *len,
+                         const UI_METHOD *ui_method,
+                         void *ui_data)
+{
+    int i = PEM_read_bio(bp, pem_name, pem_header, data, len);
+
+    if (i <= 0)
+        return 0;
+
+    /*
+     * 10 is the number of characters in "Proc-Type:", which
+     * PEM_get_EVP_CIPHER_INFO() requires to be present.
+     * If the PEM header has less characters than that, it's
+     * not worth spending cycles on it.
+     */
+    if (strlen(*pem_header) > 10) {
+        EVP_CIPHER_INFO cipher;
+        struct pem_pass_data pass_data;
+
+        if (!PEM_get_EVP_CIPHER_INFO(*pem_header, &cipher)
+            || !file_fill_pem_pass_data(&pass_data, "PEM", ui_method, ui_data)
+            || !PEM_do_header(&cipher, *data, len, file_get_pem_pass,
+                              &pass_data)) {
+            return 0;
+        }
+    }
+    return 1;
+}
+
+static int file_read_asn1(BIO *bp, unsigned char **data, long *len)
+{
+    BUF_MEM *mem = NULL;
+
+    if (asn1_d2i_read_bio(bp, &mem) < 0)
+        return 0;
+
+    *data = (unsigned char *)mem->data;
+    *len = (long)mem->length;
+    OPENSSL_free(mem);
+
+    return 1;
+}
+
+static int ends_with_dirsep(const char *uri)
+{
+    if (*uri != '\0')
+        uri += strlen(uri) - 1;
+#if defined __VMS
+    if (*uri == ']' || *uri == '>' || *uri == ':')
+        return 1;
+#elif defined _WIN32
+    if (*uri == '\\')
+        return 1;
+#endif
+    return *uri == '/';
+}
+
+static int file_name_to_uri(OSSL_STORE_LOADER_CTX *ctx, const char *name,
+                            char **data)
+{
+    assert(name != NULL);
+    assert(data != NULL);
+    {
+        const char *pathsep = ends_with_dirsep(ctx->_.dir.uri) ? "" : "/";
+        long calculated_length = strlen(ctx->_.dir.uri) + strlen(pathsep)
+            + strlen(name) + 1 /* \0 */;
+
+        *data = OPENSSL_zalloc(calculated_length);
+        if (*data == NULL) {
+            OSSL_STOREerr(OSSL_STORE_F_FILE_NAME_TO_URI, ERR_R_MALLOC_FAILURE);
+            return 0;
+        }
+
+        OPENSSL_strlcat(*data, ctx->_.dir.uri, calculated_length);
+        OPENSSL_strlcat(*data, pathsep, calculated_length);
+        OPENSSL_strlcat(*data, name, calculated_length);
+    }
+    return 1;
+}
+
+static int file_eof(OSSL_STORE_LOADER_CTX *ctx);
+static int file_error(OSSL_STORE_LOADER_CTX *ctx);
+static OSSL_STORE_INFO *file_load(OSSL_STORE_LOADER_CTX *ctx,
+                                  const UI_METHOD *ui_method, void *ui_data)
+{
+    OSSL_STORE_INFO *result = NULL;
+
+    if (ctx->type == is_dir) {
+        do {
+            char *newname = NULL;
+
+            if (ctx->_.dir.last_entry == NULL) {
+                if (!ctx->_.dir.end_reached) {
+                    char errbuf[256];
+                    assert(ctx->_.dir.last_errno != 0);
+                    errno = ctx->_.dir.last_errno;
+                    ctx->errcnt++;
+                    openssl_strerror_r(errno, errbuf, sizeof(errbuf));
+                    OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD, ERR_R_SYS_LIB);
+                    ERR_add_error_data(1, errbuf);
                 }
+                return NULL;
             }
 
-            if (matchcount > 1)
-                OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD,
-                              OSSL_STORE_R_AMBIGUOUS_CONTENT_TYPE);
-            if (matchcount == 0)
-                OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD,
-                              OSSL_STORE_R_UNSUPPORTED_CONTENT_TYPE);
-            else if (matching_handlers[0]->repeatable) {
-                ctx->last_handler = matching_handlers[0];
-                ctx->last_handler_ctx = handler_ctx;
-                mem = NULL;
-                data = NULL;
+            if (ctx->_.dir.last_entry[0] != '.'
+                && !file_name_to_uri(ctx, ctx->_.dir.last_entry, &newname))
+                return NULL;
+
+            /*
+             * On the first call (with a NULL context), OPENSSL_DIR_read()
+             * cares about the second argument.  On the following calls, it
+             * only cares that it isn't NULL.  Therefore, we can safely give
+             * it our URI here.
+             */
+            ctx->_.dir.last_entry = OPENSSL_DIR_read(&ctx->_.dir.ctx,
+                                                     ctx->_.dir.uri);
+            ctx->_.dir.last_errno = errno;
+            if (ctx->_.dir.last_entry == NULL && ctx->_.dir.last_errno == 0)
+                ctx->_.dir.end_reached = 1;
+
+            if (newname != NULL
+                && (result = OSSL_STORE_INFO_new_NAME(newname)) == NULL) {
+                OPENSSL_free(newname);
+                OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD, ERR_R_OSSL_STORE_LIB);
+                return NULL;
             }
+        } while (result == NULL && !file_eof(ctx));
+    } else {
+        int matchcount = -1;
 
-            OPENSSL_free(matching_handlers);
-        }
+        result = file_load_try_repeat(ctx, ui_method, ui_data);
+        if (result != NULL)
+            return result;
+
+        if (file_error(ctx))
+            return NULL;
+
+        do {
+            char *pem_name = NULL;      /* PEM record name */
+            char *pem_header = NULL;    /* PEM record header */
+            unsigned char *data = NULL; /* DER encoded data */
+            long len = 0;               /* DER encoded data length */
+
+            matchcount = -1;
+            if (ctx->type == is_pem) {
+                if (!file_read_pem(ctx->_.file.file, &pem_name, &pem_header,
+                                   &data, &len, ui_method, ui_data)) {
+                    if (!file_eof(ctx))
+                        ctx->errcnt++;
+                    goto err;
+                }
+            } else {
+                if (!file_read_asn1(ctx->_.file.file, &data, &len)) {
+                    if (!file_eof(ctx))
+                        ctx->errcnt++;
+                    goto err;
+                }
+            }
 
-        if (result)
-            ERR_clear_error();
+            result = file_load_try_decode(ctx, pem_name, pem_header, data, len,
+                                          ui_method, ui_data, &matchcount);
 
-     err:
-        OPENSSL_free(pem_name);
-        OPENSSL_free(pem_header);
-        if (mem == NULL)
+         err:
+            OPENSSL_free(pem_name);
+            OPENSSL_free(pem_header);
             OPENSSL_free(data);
-        else
-            BUF_MEM_free(mem);
-    } while (matchcount == 0 && !file_eof(ctx) && !file_error(ctx));
+        } while (matchcount == 0 && !file_eof(ctx) && !file_error(ctx));
 
-    /* We bail out on ambiguity */
-    if (matchcount > 1)
-        return NULL;
+        /* We bail out on ambiguity */
+        if (matchcount > 1)
+            return NULL;
+    }
 
- end:
     return result;
 }
 
@@ -721,21 +1018,23 @@ static int file_error(OSSL_STORE_LOADER_CTX *ctx)
 
 static int file_eof(OSSL_STORE_LOADER_CTX *ctx)
 {
-    if (ctx->last_handler != NULL
-        && !ctx->last_handler->eof(ctx->last_handler_ctx))
+    if (ctx->type == is_dir)
+        return ctx->_.dir.end_reached;
+
+    if (ctx->_.file.last_handler != NULL
+        && !ctx->_.file.last_handler->eof(ctx->_.file.last_handler_ctx))
         return 0;
-    return BIO_eof(ctx->file);
+    return BIO_eof(ctx->_.file.file);
 }
 
 static int file_close(OSSL_STORE_LOADER_CTX *ctx)
 {
-    if (ctx->last_handler != NULL) {
-        ctx->last_handler->destroy_ctx(&ctx->last_handler_ctx);
-        ctx->last_handler_ctx = NULL;
-        ctx->last_handler = NULL;
+    if (ctx->type == is_dir) {
+        OPENSSL_DIR_end(&ctx->_.dir.ctx);
+    } else {
+        BIO_free_all(ctx->_.file.file);
     }
-    BIO_free_all(ctx->file);
-    OPENSSL_free(ctx);
+    OSSL_STORE_LOADER_CTX_free(ctx);
     return 1;
 }