OSSL_STORE: spell error reason correctly
[openssl.git] / crypto / store / loader_file.c
index 27af75249764e8b7af01669bb98cfba0599d0288..85ab2b4206f28bfbacd32da3508b6f33d4fd4007 100644 (file)
 #include "internal/asn1_int.h"
 #include "internal/o_dir.h"
 #include "internal/cryptlib.h"
+#include "internal/store_int.h"
 #include "store_locl.h"
 
 #include "e_os.h"
 
-/*
+#ifdef _WIN32
+# define stat    _stat
+#endif
+
+/*-
  *  Password prompting
+ *  ------------------
  */
 
 static char *file_get_pass(const UI_METHOD *ui_method, char *pass,
@@ -82,6 +88,7 @@ struct pem_pass_data {
     void *data;
     const char *prompt_info;
 };
+
 static int file_fill_pem_pass_data(struct pem_pass_data *pass_data,
                                    const char *prompt_info,
                                    const UI_METHOD *ui_method, void *ui_data)
@@ -93,6 +100,8 @@ static int file_fill_pem_pass_data(struct pem_pass_data *pass_data,
     pass_data->prompt_info = prompt_info;
     return 1;
 }
+
+/* This is used anywhere a pem_password_cb is needed */
 static int file_get_pem_pass(char *buf, int num, int w, void *data)
 {
     struct pem_pass_data *pass_data = data;
@@ -102,8 +111,14 @@ static int file_get_pem_pass(char *buf, int num, int w, void *data)
     return pass == NULL ? 0 : strlen(pass);
 }
 
-/*
- *  The file scheme handlers
+/*-
+ *  The file scheme decoders
+ *  ------------------------
+ *
+ *  Each possible data type has its own decoder, which either operates
+ *  through a given PEM name, or attempts to decode to see if the blob
+ *  it's given is decodable for its data type.  The assumption is that
+ *  only the correct data type will match the content.
  */
 
 /*-
@@ -125,6 +140,11 @@ static int file_get_pem_pass(char *buf, int num, int w, void *data)
  *                  THE HANDLER'S RESPONSIBILITY TO CREATE AND DESTROY
  *                  THIS CONTEXT APPROPRIATELY, i.e. create on first call
  *                  and destroy when about to return NULL.
+ *    matchcount:   A pointer to an int to count matches for this data.
+ *                  Usually becomes 0 (no match) or 1 (match!), but may
+ *                  be higher in the (unlikely) event that the data matches
+ *                  more than one possibility.  The int will always be
+ *                  zero when the function is called.
  *    ui_method:    Application UI method for getting a password, pin
  *                  or any other interactive data.
  *    ui_data:      Application data to be passed to ui_method when
@@ -136,6 +156,7 @@ typedef OSSL_STORE_INFO *(*file_try_decode_fn)(const char *pem_name,
                                                const char *pem_header,
                                                const unsigned char *blob,
                                                size_t len, void **handler_ctx,
+                                               int *matchcount,
                                                const UI_METHOD *ui_method,
                                                void *ui_data);
 /*
@@ -161,10 +182,16 @@ typedef struct file_handler_st {
     int repeatable;
 } FILE_HANDLER;
 
+/*
+ * PKCS#12 decoder.  It operates by decoding all of the blob content,
+ * extracting all the interesting data from it and storing them internally,
+ * then serving them one piece at a time.
+ */
 static OSSL_STORE_INFO *try_decode_PKCS12(const char *pem_name,
                                           const char *pem_header,
                                           const unsigned char *blob,
                                           size_t len, void **pctx,
+                                          int *matchcount,
                                           const UI_METHOD *ui_method,
                                           void *ui_data)
 {
@@ -187,6 +214,8 @@ static OSSL_STORE_INFO *try_decode_PKCS12(const char *pem_name,
             X509 *cert = NULL;
             STACK_OF(X509) *chain = NULL;
 
+            *matchcount = 1;
+
             if (PKCS12_verify_mac(p12, "", 0)
                 || PKCS12_verify_mac(p12, NULL, 0)) {
                 pass = "";
@@ -250,17 +279,21 @@ static OSSL_STORE_INFO *try_decode_PKCS12(const char *pem_name,
             return NULL;
     }
 
-    if (ctx != NULL)
+    if (ctx != NULL) {
+        *matchcount = 1;
         store_info = sk_OSSL_STORE_INFO_shift(ctx);
+    }
 
     return store_info;
 }
+
 static int eof_PKCS12(void *ctx_)
 {
     STACK_OF(OSSL_STORE_INFO) *ctx = ctx_;
 
     return ctx == NULL || sk_OSSL_STORE_INFO_num(ctx) == 0;
 }
+
 static void destroy_ctx_PKCS12(void **pctx)
 {
     STACK_OF(OSSL_STORE_INFO) *ctx = *pctx;
@@ -268,6 +301,7 @@ static void destroy_ctx_PKCS12(void **pctx)
     sk_OSSL_STORE_INFO_pop_free(ctx, OSSL_STORE_INFO_free);
     *pctx = NULL;
 }
+
 static FILE_HANDLER PKCS12_handler = {
     "PKCS12",
     try_decode_PKCS12,
@@ -276,10 +310,16 @@ static FILE_HANDLER PKCS12_handler = {
     1                            /* repeatable */
 };
 
+/*
+ * Encrypted PKCS#8 decoder.  It operates by just decrypting the given blob
+ * into a new blob, which is returned as an EMBEDDED STORE_INFO.  The whole
+ * decoding process will then start over with the new blob.
+ */
 static OSSL_STORE_INFO *try_decode_PKCS8Encrypted(const char *pem_name,
                                                   const char *pem_header,
                                                   const unsigned char *blob,
                                                   size_t len, void **pctx,
+                                                  int *matchcount,
                                                   const UI_METHOD *ui_method,
                                                   void *ui_data)
 {
@@ -293,12 +333,17 @@ static OSSL_STORE_INFO *try_decode_PKCS8Encrypted(const char *pem_name,
     unsigned char *new_data = NULL;
     int new_data_len;
 
-    if (pem_name != NULL && strcmp(pem_name, PEM_STRING_PKCS8) != 0)
-        return NULL;
+    if (pem_name != NULL) {
+        if (strcmp(pem_name, PEM_STRING_PKCS8) != 0)
+            return NULL;
+        *matchcount = 1;
+    }
 
     if ((p8 = d2i_X509_SIG(NULL, &blob, len)) == NULL)
         return NULL;
 
+    *matchcount = 1;
+
     if ((mem = BUF_MEM_new()) == NULL) {
         OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS8ENCRYPTED,
                       ERR_R_MALLOC_FAILURE);
@@ -334,16 +379,23 @@ static OSSL_STORE_INFO *try_decode_PKCS8Encrypted(const char *pem_name,
     BUF_MEM_free(mem);
     return NULL;
 }
+
 static FILE_HANDLER PKCS8Encrypted_handler = {
     "PKCS8Encrypted",
     try_decode_PKCS8Encrypted
 };
 
+/*
+ * Private key decoder.  Decodes all sorts of private keys, both PKCS#8
+ * encoded ones and old style PEM ones (with the key type is encoded into
+ * the PEM name).
+ */
 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,
                                               const unsigned char *blob,
                                               size_t len, void **pctx,
+                                              int *matchcount,
                                               const UI_METHOD *ui_method,
                                               void *ui_data)
 {
@@ -356,6 +408,7 @@ static OSSL_STORE_INFO *try_decode_PrivateKey(const char *pem_name,
             PKCS8_PRIV_KEY_INFO *p8inf =
                 d2i_PKCS8_PRIV_KEY_INFO(NULL, &blob, len);
 
+            *matchcount = 1;
             if (p8inf != NULL)
                 pkey = EVP_PKCS82PKEY(p8inf);
             PKCS8_PRIV_KEY_INFO_free(p8inf);
@@ -364,19 +417,35 @@ static OSSL_STORE_INFO *try_decode_PrivateKey(const char *pem_name,
 
             if ((slen = pem_check_suffix(pem_name, "PRIVATE KEY")) > 0
                 && (ameth = EVP_PKEY_asn1_find_str(NULL, pem_name,
-                                                   slen)) != NULL)
+                                                   slen)) != NULL) {
+                *matchcount = 1;
                 pkey = d2i_PrivateKey(ameth->pkey_id, NULL, &blob, len);
+            }
         }
     } else {
         int i;
 
         for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
+            EVP_PKEY *tmp_pkey = NULL;
+            const unsigned char *tmp_blob = blob;
+
             ameth = EVP_PKEY_asn1_get0(i);
             if (ameth->pkey_flags & ASN1_PKEY_ALIAS)
                 continue;
-            pkey = d2i_PrivateKey(ameth->pkey_id, NULL, &blob, len);
-            if (pkey != NULL)
-                break;
+
+            tmp_pkey = d2i_PrivateKey(ameth->pkey_id, NULL, &tmp_blob, len);
+            if (tmp_pkey != NULL) {
+                if (pkey != NULL)
+                    EVP_PKEY_free(tmp_pkey);
+                else
+                    pkey = tmp_pkey;
+                (*matchcount)++;
+            }
+        }
+
+        if (*matchcount > 1) {
+            EVP_PKEY_free(pkey);
+            pkey = NULL;
         }
     }
     if (pkey == NULL)
@@ -389,76 +458,114 @@ static OSSL_STORE_INFO *try_decode_PrivateKey(const char *pem_name,
 
     return store_info;
 }
+
 static FILE_HANDLER PrivateKey_handler = {
     "PrivateKey",
     try_decode_PrivateKey
 };
 
+/*
+ * Public key decoder.  Only supports SubjectPublicKeyInfo formated keys.
+ */
 static OSSL_STORE_INFO *try_decode_PUBKEY(const char *pem_name,
                                           const char *pem_header,
                                           const unsigned char *blob,
                                           size_t len, void **pctx,
+                                          int *matchcount,
                                           const UI_METHOD *ui_method,
                                           void *ui_data)
 {
     OSSL_STORE_INFO *store_info = NULL;
     EVP_PKEY *pkey = NULL;
 
-    if (pem_name != NULL && strcmp(pem_name, PEM_STRING_PUBLIC) != 0)
-        /* No match */
-        return NULL;
+    if (pem_name != NULL) {
+        if (strcmp(pem_name, PEM_STRING_PUBLIC) != 0)
+            /* No match */
+            return NULL;
+        *matchcount = 1;
+    }
 
-    if ((pkey = d2i_PUBKEY(NULL, &blob, len)) != NULL)
+    if ((pkey = d2i_PUBKEY(NULL, &blob, len)) != NULL) {
+        *matchcount = 1;
         store_info = OSSL_STORE_INFO_new_PKEY(pkey);
+    }
 
     return store_info;
 }
+
 static FILE_HANDLER PUBKEY_handler = {
     "PUBKEY",
     try_decode_PUBKEY
 };
 
+/*
+ * Key parameter decoder.
+ */
 static OSSL_STORE_INFO *try_decode_params(const char *pem_name,
                                           const char *pem_header,
                                           const unsigned char *blob,
                                           size_t len, void **pctx,
+                                          int *matchcount,
                                           const UI_METHOD *ui_method,
                                           void *ui_data)
 {
     OSSL_STORE_INFO *store_info = NULL;
-    EVP_PKEY *pkey = EVP_PKEY_new();
+    int slen = 0;
+    EVP_PKEY *pkey = NULL;
     const EVP_PKEY_ASN1_METHOD *ameth = NULL;
     int ok = 0;
 
-    if (pkey == NULL) {
-        OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PARAMS, ERR_R_EVP_LIB);
-        return NULL;
+    if (pem_name != NULL) {
+        if ((slen = pem_check_suffix(pem_name, "PARAMETERS")) == 0)
+            return NULL;
+        *matchcount = 1;
     }
 
-    if (pem_name != NULL) {
-        int slen;
+    if (slen > 0) {
+        if ((pkey = EVP_PKEY_new()) == NULL) {
+            OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PARAMS, ERR_R_EVP_LIB);
+            return NULL;
+        }
 
-        if ((slen = pem_check_suffix(pem_name, "PARAMETERS")) > 0
-            && EVP_PKEY_set_type_str(pkey, pem_name, slen)
+
+        if (EVP_PKEY_set_type_str(pkey, pem_name, slen)
             && (ameth = EVP_PKEY_get0_asn1(pkey)) != NULL
             && ameth->param_decode != NULL
             && ameth->param_decode(pkey, &blob, len))
             ok = 1;
     } else {
         int i;
+        EVP_PKEY *tmp_pkey = NULL;
 
         for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
+            const unsigned char *tmp_blob = blob;
+
+            if (tmp_pkey == NULL && (tmp_pkey = EVP_PKEY_new()) == NULL) {
+                OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PARAMS, ERR_R_EVP_LIB);
+                break;
+            }
+
             ameth = EVP_PKEY_asn1_get0(i);
             if (ameth->pkey_flags & ASN1_PKEY_ALIAS)
                 continue;
-            if (EVP_PKEY_set_type(pkey, ameth->pkey_id)
-                && (ameth = EVP_PKEY_get0_asn1(pkey)) != NULL
+
+            if (EVP_PKEY_set_type(tmp_pkey, ameth->pkey_id)
+                && (ameth = EVP_PKEY_get0_asn1(tmp_pkey)) != NULL
                 && ameth->param_decode != NULL
-                && ameth->param_decode(pkey, &blob, len)) {
-                ok = 1;
-                break;
+                && ameth->param_decode(tmp_pkey, &tmp_blob, len)) {
+                if (pkey != NULL)
+                    EVP_PKEY_free(tmp_pkey);
+                else
+                    pkey = tmp_pkey;
+                tmp_pkey = NULL;
+                (*matchcount)++;
             }
         }
+
+        EVP_PKEY_free(tmp_pkey);
+        if (*matchcount == 1) {
+            ok = 1;
+        }
     }
 
     if (ok)
@@ -468,15 +575,20 @@ static OSSL_STORE_INFO *try_decode_params(const char *pem_name,
 
     return store_info;
 }
+
 static FILE_HANDLER params_handler = {
     "params",
     try_decode_params
 };
 
+/*
+ * X.509 certificate decoder.
+ */
 static OSSL_STORE_INFO *try_decode_X509Certificate(const char *pem_name,
                                                    const char *pem_header,
                                                    const unsigned char *blob,
                                                    size_t len, void **pctx,
+                                                   int *matchcount,
                                                    const UI_METHOD *ui_method,
                                                    void *ui_data)
 {
@@ -499,50 +611,66 @@ static OSSL_STORE_INFO *try_decode_X509Certificate(const char *pem_name,
                  && strcmp(pem_name, PEM_STRING_X509) != 0)
             /* No match */
             return NULL;
+        *matchcount = 1;
     }
 
     if ((cert = d2i_X509_AUX(NULL, &blob, len)) != NULL
-        || (ignore_trusted && (cert = d2i_X509(NULL, &blob, len)) != NULL))
+        || (ignore_trusted && (cert = d2i_X509(NULL, &blob, len)) != NULL)) {
+        *matchcount = 1;
         store_info = OSSL_STORE_INFO_new_CERT(cert);
+    }
 
     if (store_info == NULL)
         X509_free(cert);
 
     return store_info;
 }
+
 static FILE_HANDLER X509Certificate_handler = {
     "X509Certificate",
     try_decode_X509Certificate
 };
 
+/*
+ * X.509 CRL decoder.
+ */
 static OSSL_STORE_INFO *try_decode_X509CRL(const char *pem_name,
                                            const char *pem_header,
                                            const unsigned char *blob,
                                            size_t len, void **pctx,
+                                           int *matchcount,
                                            const UI_METHOD *ui_method,
                                            void *ui_data)
 {
     OSSL_STORE_INFO *store_info = NULL;
     X509_CRL *crl = NULL;
 
-    if (pem_name != NULL
-        && strcmp(pem_name, PEM_STRING_X509_CRL) != 0)
-        /* No match */
-        return NULL;
+    if (pem_name != NULL) {
+        if (strcmp(pem_name, PEM_STRING_X509_CRL) != 0)
+            /* No match */
+            return NULL;
+        *matchcount = 1;
+    }
 
-    if ((crl = d2i_X509_CRL(NULL, &blob, len)) != NULL)
+    if ((crl = d2i_X509_CRL(NULL, &blob, len)) != NULL) {
+        *matchcount = 1;
         store_info = OSSL_STORE_INFO_new_CRL(crl);
+    }
 
     if (store_info == NULL)
         X509_CRL_free(crl);
 
     return store_info;
 }
+
 static FILE_HANDLER X509CRL_handler = {
     "X509CRL",
     try_decode_X509CRL
 };
 
+/*
+ * To finish it all off, we collect all the handlers.
+ */
 static const FILE_HANDLER *file_handlers[] = {
     &PKCS12_handler,
     &PKCS8Encrypted_handler,
@@ -554,8 +682,9 @@ static const FILE_HANDLER *file_handlers[] = {
 };
 
 
-/*
+/*-
  *  The loader itself
+ *  -----------------
  */
 
 struct ossl_store_loader_ctx_st {
@@ -565,6 +694,8 @@ struct ossl_store_loader_ctx_st {
         is_dir
     } type;
     int errcnt;
+#define FILE_FLAG_SECMEM         (1<<0)
+    unsigned int flags;
     union {
         struct {                 /* Used with is_raw and is_pem */
             BIO *file;
@@ -616,7 +747,7 @@ static OSSL_STORE_LOADER_CTX *file_open(const OSSL_STORE_LOADER *loader,
     const char *path = NULL;
 
     if (strncasecmp(uri, "file:", 5) == 0) {
-        if (strncmp(&uri[5], "//localhost/", 12) == 0) {
+        if (strncasecmp(&uri[5], "//localhost/", 12) == 0) {
             path = &uri[16];
         } else if (strncmp(&uri[5], "///", 3) == 0) {
             path = &uri[7];
@@ -624,7 +755,7 @@ static OSSL_STORE_LOADER_CTX *file_open(const OSSL_STORE_LOADER *loader,
             path = &uri[5];
         } else {
             OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN,
-                          OSSL_STORE_R_URI_AUTHORITY_UNSUPPORED);
+                          OSSL_STORE_R_URI_AUTHORITY_UNSUPPORTED);
             return NULL;
         }
 
@@ -709,6 +840,54 @@ static OSSL_STORE_LOADER_CTX *file_open(const OSSL_STORE_LOADER *loader,
     return NULL;
 }
 
+static int file_ctrl(OSSL_STORE_LOADER_CTX *ctx, int cmd, va_list args)
+{
+    int ret = 1;
+
+    switch (cmd) {
+    case OSSL_STORE_C_USE_SECMEM:
+        {
+            int on = *(va_arg(args, int *));
+
+            switch (on) {
+            case 0:
+                ctx->flags &= ~FILE_FLAG_SECMEM;
+                break;
+            case 1:
+                ctx->flags |= FILE_FLAG_SECMEM;
+                break;
+            default:
+                OSSL_STOREerr(OSSL_STORE_F_FILE_CTRL,
+                              ERR_R_PASSED_INVALID_ARGUMENT);
+                ret = 0;
+                break;
+            }
+        }
+        break;
+    default:
+        break;
+    }
+
+    return ret;
+}
+
+/* Internal function to decode an already opened PEM file */
+OSSL_STORE_LOADER_CTX *ossl_store_file_attach_pem_bio_int(BIO *bp)
+{
+    OSSL_STORE_LOADER_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
+
+    if (ctx == NULL) {
+        OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_FILE_ATTACH_PEM_BIO_INT,
+                      ERR_R_MALLOC_FAILURE);
+        return NULL;
+    }
+
+    ctx->_.file.file = bp;
+    ctx->type = is_pem;
+
+    return ctx;
+}
+
 static OSSL_STORE_INFO *file_load_try_decode(OSSL_STORE_LOADER_CTX *ctx,
                                              const char *pem_name,
                                              const char *pem_header,
@@ -738,16 +917,14 @@ static OSSL_STORE_INFO *file_load_try_decode(OSSL_STORE_LOADER_CTX *ctx,
         *matchcount = 0;
         for (i = 0; i < OSSL_NELEM(file_handlers); i++) {
             const FILE_HANDLER *handler = file_handlers[i];
+            int try_matchcount = 0;
             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);
+                                    &tmp_handler_ctx, &try_matchcount,
+                                    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 (try_matchcount > 0) {
                 if (matching_handlers)
                     matching_handlers[*matchcount] = handler;
 
@@ -755,37 +932,24 @@ static OSSL_STORE_INFO *file_load_try_decode(OSSL_STORE_LOADER_CTX *ctx,
                     handler->destroy_ctx(&handler_ctx);
                 handler_ctx = tmp_handler_ctx;
 
-                if (++*matchcount == 1) {
-                    result = tmp_result;
-                    tmp_result = NULL;
-                } else {
+                if ((*matchcount += try_matchcount) > 1) {
                     /* 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;
+                    tmp_result = NULL;
                     result = NULL;
                 }
+                if (result == NULL)
+                    result = tmp_result;
             }
         }
 
-        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;
-            }
+        if (*matchcount == 1 && matching_handlers[0]->repeatable) {
+            ctx->_.file.last_handler = matching_handlers[0];
+            ctx->_.file.last_handler_ctx = handler_ctx;
         }
 
         OPENSSL_free(matching_handlers);
@@ -818,11 +982,13 @@ static OSSL_STORE_INFO *file_load_try_repeat(OSSL_STORE_LOADER_CTX *ctx,
                                              void *ui_data)
 {
     OSSL_STORE_INFO *result = NULL;
+    int try_matchcount = 0;
 
     if (ctx->_.file.last_handler != NULL) {
         result =
             ctx->_.file.last_handler->try_decode(NULL, NULL, NULL, 0,
                                                  &ctx->_.file.last_handler_ctx,
+                                                 &try_matchcount,
                                                  ui_method, ui_data);
 
         if (result == NULL) {
@@ -834,12 +1000,22 @@ static OSSL_STORE_INFO *file_load_try_repeat(OSSL_STORE_LOADER_CTX *ctx,
     return result;
 }
 
+static void pem_free_flag(void *pem_data, int secure)
+{
+    if (secure)
+        OPENSSL_secure_free(pem_data);
+    else
+        OPENSSL_free(pem_data);
+}
 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)
+                         void *ui_data, int secure)
 {
-    int i = PEM_read_bio(bp, pem_name, pem_header, data, len);
+    int i = secure
+        ? PEM_read_bio_ex(bp, pem_name, pem_header, data, len,
+                          PEM_FLAG_SECURE | PEM_FLAG_EAY_COMPATIBLE)
+        : PEM_read_bio(bp, pem_name, pem_header, data, len);
 
     if (i <= 0)
         return 0;
@@ -922,6 +1098,9 @@ static OSSL_STORE_INFO *file_load(OSSL_STORE_LOADER_CTX *ctx,
 {
     OSSL_STORE_INFO *result = NULL;
 
+    ctx->errcnt = 0;
+    ERR_clear_error();
+
     if (ctx->type == is_dir) {
         do {
             char *newname = NULL;
@@ -969,7 +1148,7 @@ static OSSL_STORE_INFO *file_load(OSSL_STORE_LOADER_CTX *ctx,
         if (result != NULL)
             return result;
 
-        if (file_error(ctx))
+        if (file_eof(ctx))
             return NULL;
 
         do {
@@ -981,26 +1160,55 @@ static OSSL_STORE_INFO *file_load(OSSL_STORE_LOADER_CTX *ctx,
             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;
+                                   &data, &len, ui_method, ui_data,
+                                   (ctx->flags & FILE_FLAG_SECMEM) != 0)) {
+                    ctx->errcnt++;
+                    goto endloop;
                 }
             } else {
                 if (!file_read_asn1(ctx->_.file.file, &data, &len)) {
-                    if (!file_eof(ctx))
-                        ctx->errcnt++;
-                    goto err;
+                    ctx->errcnt++;
+                    goto endloop;
                 }
             }
 
             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);
-            OPENSSL_free(data);
+            if (result != NULL)
+                goto endloop;
+
+            /*
+             * If a PEM name matches more than one handler, the handlers are
+             * badly coded.
+             */
+            if (!ossl_assert(pem_name == NULL || matchcount <= 1)) {
+                ctx->errcnt++;
+                goto endloop;
+            }
+
+            if (matchcount > 1) {
+                OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD,
+                              OSSL_STORE_R_AMBIGUOUS_CONTENT_TYPE);
+            } else if (matchcount == 1) {
+                /*
+                 * If there are other errors on the stack, they already show
+                 * what the problem is.
+                 */
+                if (ERR_peek_error() == 0) {
+                    OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD,
+                                  OSSL_STORE_R_UNSUPPORTED_CONTENT_TYPE);
+                    if (pem_name != NULL)
+                        ERR_add_error_data(3, "PEM type is '", pem_name, "'");
+                }
+            }
+            if (matchcount > 0)
+                ctx->errcnt++;
+
+         endloop:
+            pem_free_flag(pem_name, (ctx->flags & FILE_FLAG_SECMEM) != 0);
+            pem_free_flag(pem_header, (ctx->flags & FILE_FLAG_SECMEM) != 0);
+            pem_free_flag(data, (ctx->flags & FILE_FLAG_SECMEM) != 0);
         } while (matchcount == 0 && !file_eof(ctx) && !file_error(ctx));
 
         /* We bail out on ambiguity */
@@ -1038,11 +1246,18 @@ static int file_close(OSSL_STORE_LOADER_CTX *ctx)
     return 1;
 }
 
+int ossl_store_file_detach_pem_bio_int(OSSL_STORE_LOADER_CTX *ctx)
+{
+    OSSL_STORE_LOADER_CTX_free(ctx);
+    return 1;
+}
+
 static OSSL_STORE_LOADER file_loader =
     {
         "file",
-        file_open,
         NULL,
+        file_open,
+        file_ctrl,
         file_load,
         file_eof,
         file_error,