2 * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include "e_os.h" /* To get strncasecmp() on Windows */
14 #include <ctype.h> /* isdigit */
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 "crypto/ctype.h" /* ossl_isdigit() */
30 #include "prov/implementations.h"
32 #include "file_store_local.h"
34 DEFINE_STACK_OF(OSSL_STORE_INFO)
41 # define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
44 static OSSL_FUNC_store_open_fn file_open;
45 static OSSL_FUNC_store_attach_fn file_attach;
46 static OSSL_FUNC_store_settable_ctx_params_fn file_settable_ctx_params;
47 static OSSL_FUNC_store_set_ctx_params_fn file_set_ctx_params;
48 static OSSL_FUNC_store_load_fn file_load;
49 static OSSL_FUNC_store_eof_fn file_eof;
50 static OSSL_FUNC_store_close_fn file_close;
53 * This implementation makes full use of OSSL_DECODER, and then some.
54 * It uses its own internal decoder implementation that reads DER and
55 * passes that on to the data callback; this decoder is created with
56 * internal OpenSSL functions, thereby bypassing the need for a surrounding
57 * provider. This is ok, since this is a local decoder, not meant for
58 * public consumption. It also uses the libcrypto internal decoder
59 * setup function ossl_decoder_ctx_setup_for_pkey(), to allow the
60 * last resort decoder to be added first (and thereby be executed last).
61 * Finally, it sets up its own construct and cleanup functions.
63 * Essentially, that makes this implementation a kind of glorified decoder.
68 char *uri; /* The URI we currently try to load */
70 IS_FILE = 0, /* Read file and pass results */
71 IS_DIR /* Pass directory entry names */
75 /* Used with |IS_FILE| */
79 OSSL_DECODER_CTX *decoderctx;
81 char *propq; /* The properties we got as a parameter */
84 /* Used with |IS_DIR| */
90 * When a search expression is given, these are filled in.
91 * |search_name| contains the file basename to look for.
92 * The string is exactly 8 characters long.
97 * The directory reading utility we have combines opening with
98 * reading the first name. To make sure we can detect the end
99 * at the right time, we read early and cache the name.
101 const char *last_entry;
106 /* Expected object type. May be unspecified */
110 static void free_file_ctx(struct file_ctx_st *ctx)
115 OPENSSL_free(ctx->uri);
116 if (ctx->type != IS_DIR) {
117 OSSL_DECODER_CTX_free(ctx->_.file.decoderctx);
118 OPENSSL_free(ctx->_.file.propq);
119 OPENSSL_free(ctx->_.file.input_type);
124 static struct file_ctx_st *new_file_ctx(int type, const char *uri,
127 struct file_ctx_st *ctx = NULL;
129 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) != NULL
130 && (uri == NULL || (ctx->uri = OPENSSL_strdup(uri)) != NULL)) {
132 ctx->provctx = provctx;
139 static OSSL_DECODER_CONSTRUCT file_load_construct;
140 static OSSL_DECODER_CLEANUP file_load_cleanup;
143 * Opening / attaching streams and directories
144 * -------------------------------------------
148 * Function to service both file_open() and file_attach()
152 static struct file_ctx_st *file_open_stream(BIO *source, const char *uri,
155 struct file_ctx_st *ctx;
157 if ((ctx = new_file_ctx(IS_FILE, uri, provctx)) == NULL) {
158 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
162 ctx->_.file.file = source;
170 static void *file_open_dir(const char *path, const char *uri, void *provctx)
172 struct file_ctx_st *ctx;
174 if ((ctx = new_file_ctx(IS_DIR, uri, provctx)) == NULL) {
175 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
179 ctx->_.dir.last_entry = OPENSSL_DIR_read(&ctx->_.dir.ctx, path);
180 ctx->_.dir.last_errno = errno;
181 if (ctx->_.dir.last_entry == NULL) {
182 if (ctx->_.dir.last_errno != 0) {
183 ERR_raise_data(ERR_LIB_SYS, ctx->_.dir.last_errno,
184 "Calling OPENSSL_DIR_read(\"%s\")", path);
187 ctx->_.dir.end_reached = 1;
195 static void *file_open(void *provctx, const char *uri)
197 struct file_ctx_st *ctx = NULL;
201 unsigned int check_absolute:1;
203 size_t path_data_n = 0, i;
210 * First step, just take the URI as is.
212 path_data[path_data_n].check_absolute = 0;
213 path_data[path_data_n++].path = uri;
216 * Second step, if the URI appears to start with the 'file' scheme,
217 * extract the path and make that the second path to check.
218 * There's a special case if the URI also contains an authority, then
219 * the full URI shouldn't be used as a path anywhere.
221 if (strncasecmp(uri, "file:", 5) == 0) {
222 const char *p = &uri[5];
224 if (strncmp(&uri[5], "//", 2) == 0) {
225 path_data_n--; /* Invalidate using the full URI */
226 if (strncasecmp(&uri[7], "localhost/", 10) == 0) {
228 } else if (uri[7] == '/') {
231 ERR_clear_last_mark();
232 ERR_raise(ERR_LIB_PROV, PROV_R_URI_AUTHORITY_UNSUPPORTED);
237 path_data[path_data_n].check_absolute = 1;
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]);
243 if (c >= 'a' && c <= 'z') {
245 /* We know it's absolute, so no need to check */
246 path_data[path_data_n].check_absolute = 0;
250 path_data[path_data_n++].path = p;
254 for (i = 0, path = NULL; path == NULL && i < path_data_n; i++) {
256 * If the scheme "file" was an explicit part of the URI, the path must
257 * be absolute. So says RFC 8089
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);
266 if (stat(path_data[i].path, &st) < 0) {
267 ERR_raise_data(ERR_LIB_SYS, errno,
271 path = path_data[i].path;
275 ERR_clear_last_mark();
279 /* Successfully found a working path, clear possible collected errors */
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)
291 void *file_attach(void *provctx, OSSL_CORE_BIO *cin)
293 struct file_ctx_st *ctx;
294 BIO *new_bio = ossl_bio_new_from_core_bio(provctx, cin);
299 ctx = file_open_stream(new_bio, NULL, provctx);
310 static const OSSL_PARAM *file_settable_ctx_params(void *provctx)
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),
319 return known_settable_ctx_params;
322 static int file_set_ctx_params(void *loaderctx, const OSSL_PARAM params[])
324 struct file_ctx_st *ctx = loaderctx;
330 if (ctx->type != IS_DIR) {
331 /* these parameters are ignored for directories */
332 p = OSSL_PARAM_locate_const(params, OSSL_STORE_PARAM_PROPERTIES);
334 OPENSSL_free(ctx->_.file.propq);
335 ctx->_.file.propq = NULL;
336 if (!OSSL_PARAM_get_utf8_string(p, &ctx->_.file.propq, 0))
339 p = OSSL_PARAM_locate_const(params, OSSL_STORE_PARAM_INPUT_TYPE);
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))
347 p = OSSL_PARAM_locate_const(params, OSSL_STORE_PARAM_EXPECT);
348 if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->expected_type))
350 p = OSSL_PARAM_locate_const(params, OSSL_STORE_PARAM_SUBJECT);
352 const unsigned char *der = NULL;
354 X509_NAME *x509_name;
358 if (ctx->type != IS_DIR) {
359 ERR_raise(ERR_LIB_PROV,
360 PROV_R_SEARCH_ONLY_SUPPORTED_FOR_DIRECTORIES);
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)
367 hash = X509_NAME_hash_ex(x509_name,
368 ossl_prov_ctx_get0_libctx(ctx->provctx), NULL,
370 BIO_snprintf(ctx->_.dir.search_name, sizeof(ctx->_.dir.search_name),
372 X509_NAME_free(x509_name);
380 * Loading an object from a stream
381 * -------------------------------
384 struct file_load_data_st {
385 OSSL_CALLBACK *object_cb;
389 static int file_load_construct(OSSL_DECODER_INSTANCE *decoder_inst,
390 const OSSL_PARAM *params, void *construct_data)
392 struct file_load_data_st *data = construct_data;
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.
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
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
410 return data->object_cb(params, data->object_cbarg);
413 void file_load_cleanup(void *construct_data)
418 static int file_setup_decoders(struct file_ctx_st *ctx)
420 EVP_PKEY *dummy; /* for ossl_decoder_ctx_setup_for_pkey() */
421 OSSL_LIB_CTX *libctx = ossl_prov_ctx_get0_libctx(ctx->provctx);
422 OSSL_DECODER *to_obj = NULL; /* Last resort decoder */
423 OSSL_DECODER_INSTANCE *to_obj_inst = NULL;
424 OSSL_DECODER_CLEANUP *old_cleanup = NULL;
425 void *old_construct_data = NULL;
426 int ok = 0, expect_evp_pkey = 0;
428 /* Setup for this session, so only if not already done */
429 if (ctx->_.file.decoderctx == NULL) {
430 if ((ctx->_.file.decoderctx = OSSL_DECODER_CTX_new()) == NULL) {
431 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
435 expect_evp_pkey = (ctx->expected_type == 0
436 || ctx->expected_type == OSSL_STORE_INFO_PARAMS
437 || ctx->expected_type == OSSL_STORE_INFO_PUBKEY
438 || ctx->expected_type == OSSL_STORE_INFO_PKEY);
440 /* Make sure the input type is set */
441 if (!OSSL_DECODER_CTX_set_input_type(ctx->_.file.decoderctx,
442 ctx->_.file.input_type)) {
443 ERR_raise(ERR_LIB_PROV, ERR_R_OSSL_DECODER_LIB);
448 * Create the internal last resort decoder implementation together
449 * with a "decoder instance".
450 * The decoder doesn't need any identification or to be attached to
451 * any provider, since it's only used locally.
453 to_obj = ossl_decoder_from_algorithm(0, &ossl_der_to_obj_algorithm,
457 to_obj_inst = ossl_decoder_instance_new(to_obj, ctx->provctx);
458 if (to_obj_inst == NULL)
461 if (!ossl_decoder_ctx_add_decoder_inst(ctx->_.file.decoderctx,
463 ERR_raise(ERR_LIB_PROV, ERR_R_OSSL_DECODER_LIB);
468 * OSSL_DECODER_INSTANCE shouldn't be freed from this point on.
469 * That's going to happen whenever the OSSL_DECODER_CTX is freed.
474 * Add on the usual decoder context for keys, with a dummy object.
475 * Since we're setting up our own constructor, we don't need to care
479 && !ossl_decoder_ctx_setup_for_pkey(ctx->_.file.decoderctx,
481 libctx, ctx->_.file.propq))
482 || !OSSL_DECODER_CTX_add_extra(ctx->_.file.decoderctx,
483 libctx, ctx->_.file.propq)) {
484 ERR_raise(ERR_LIB_PROV, ERR_R_OSSL_DECODER_LIB);
489 * Then we throw away the installed finalizer data, and install our
492 old_cleanup = OSSL_DECODER_CTX_get_cleanup(ctx->_.file.decoderctx);
494 OSSL_DECODER_CTX_get_construct_data(ctx->_.file.decoderctx);
495 if (old_cleanup != NULL)
496 old_cleanup(old_construct_data);
501 if (!OSSL_DECODER_CTX_set_construct(ctx->_.file.decoderctx,
503 || !OSSL_DECODER_CTX_set_cleanup(ctx->_.file.decoderctx,
504 file_load_cleanup)) {
505 ERR_raise(ERR_LIB_PROV, ERR_R_OSSL_DECODER_LIB);
512 OSSL_DECODER_free(to_obj);
516 static int file_load_file(struct file_ctx_st *ctx,
517 OSSL_CALLBACK *object_cb, void *object_cbarg,
518 OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
520 struct file_load_data_st data;
522 /* Setup the decoders (one time shot per session */
524 if (!file_setup_decoders(ctx))
527 /* Setup for this object */
529 data.object_cb = object_cb;
530 data.object_cbarg = object_cbarg;
531 OSSL_DECODER_CTX_set_construct_data(ctx->_.file.decoderctx, &data);
532 OSSL_DECODER_CTX_set_passphrase_cb(ctx->_.file.decoderctx, pw_cb, pw_cbarg);
536 return OSSL_DECODER_from_bio(ctx->_.file.decoderctx, ctx->_.file.file);
540 * Loading a name object from a directory
541 * --------------------------------------
544 static char *file_name_to_uri(struct file_ctx_st *ctx, const char *name)
548 assert(name != NULL);
550 const char *pathsep = ossl_ends_with_dirsep(ctx->uri) ? "" : "/";
551 long calculated_length = strlen(ctx->uri) + strlen(pathsep)
552 + strlen(name) + 1 /* \0 */;
554 data = OPENSSL_zalloc(calculated_length);
556 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
560 OPENSSL_strlcat(data, ctx->uri, calculated_length);
561 OPENSSL_strlcat(data, pathsep, calculated_length);
562 OPENSSL_strlcat(data, name, calculated_length);
567 static int file_name_check(struct file_ctx_st *ctx, const char *name)
569 const char *p = NULL;
571 /* If there are no search criteria, all names are accepted */
572 if (ctx->_.dir.search_name[0] == '\0')
575 /* If the expected type isn't supported, no name is accepted */
576 if (ctx->expected_type != 0
577 && ctx->expected_type != OSSL_STORE_INFO_CERT
578 && ctx->expected_type != OSSL_STORE_INFO_CRL)
582 * First, check the basename
584 if (strncasecmp(name, ctx->_.dir.search_name,
585 sizeof(ctx->_.dir.search_name) - 1) != 0
586 || name[sizeof(ctx->_.dir.search_name) - 1] != '.')
588 p = &name[sizeof(ctx->_.dir.search_name)];
591 * Then, if the expected type is a CRL, check that the extension starts
596 if (ctx->expected_type != 0
597 && ctx->expected_type != OSSL_STORE_INFO_CRL)
599 } else if (ctx->expected_type == OSSL_STORE_INFO_CRL) {
604 * Last, check that the rest of the extension is a decimal number, at
605 * least one digit long.
614 * One extra step here, check for a possible generation number.
617 for (p++; *p != '\0'; p++)
618 if (!ossl_isdigit(*p))
623 * If we've reached the end of the string at this point, we've successfully
624 * found a fitting file name.
629 static int file_load_dir_entry(struct file_ctx_st *ctx,
630 OSSL_CALLBACK *object_cb, void *object_cbarg,
631 OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
633 /* Prepare as much as possible in advance */
634 static const int object_type = OSSL_OBJECT_NAME;
635 OSSL_PARAM object[] = {
636 OSSL_PARAM_int(OSSL_OBJECT_PARAM_TYPE, (int *)&object_type),
637 OSSL_PARAM_utf8_string(OSSL_OBJECT_PARAM_DATA, NULL, 0),
640 char *newname = NULL;
643 /* Loop until we get an error or until we have a suitable name */
645 if (ctx->_.dir.last_entry == NULL) {
646 if (!ctx->_.dir.end_reached) {
647 assert(ctx->_.dir.last_errno != 0);
648 ERR_raise(ERR_LIB_SYS, ctx->_.dir.last_errno);
650 /* file_eof() will tell if EOF was reached */
654 /* flag acceptable names */
655 if (ctx->_.dir.last_entry[0] != '.'
656 && file_name_check(ctx, ctx->_.dir.last_entry)) {
658 /* If we can't allocate the new name, we fail */
660 file_name_to_uri(ctx, ctx->_.dir.last_entry)) == NULL)
665 * On the first call (with a NULL context), OPENSSL_DIR_read()
666 * cares about the second argument. On the following calls, it
667 * only cares that it isn't NULL. Therefore, we can safely give
670 ctx->_.dir.last_entry = OPENSSL_DIR_read(&ctx->_.dir.ctx, ctx->uri);
671 ctx->_.dir.last_errno = errno;
672 if (ctx->_.dir.last_entry == NULL && ctx->_.dir.last_errno == 0)
673 ctx->_.dir.end_reached = 1;
674 } while (newname == NULL);
676 object[1].data = newname;
677 object[1].data_size = strlen(newname);
678 ok = object_cb(object, object_cbarg);
679 OPENSSL_free(newname);
684 * Loading, local dispatcher
685 * -------------------------
688 static int file_load(void *loaderctx,
689 OSSL_CALLBACK *object_cb, void *object_cbarg,
690 OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
692 struct file_ctx_st *ctx = loaderctx;
696 return file_load_file(ctx, object_cb, object_cbarg, pw_cb, pw_cbarg);
699 file_load_dir_entry(ctx, object_cb, object_cbarg, pw_cb, pw_cbarg);
704 /* ctx->type has an unexpected value */
710 * Eof detection and closing
711 * -------------------------
714 static int file_eof(void *loaderctx)
716 struct file_ctx_st *ctx = loaderctx;
720 return ctx->_.dir.end_reached;
723 * BIO_pending() checks any filter BIO.
724 * BIO_eof() checks the source BIO.
726 return !BIO_pending(ctx->_.file.file)
727 && BIO_eof(ctx->_.file.file);
730 /* ctx->type has an unexpected value */
735 static int file_close_dir(struct file_ctx_st *ctx)
737 if (ctx->_.dir.ctx != NULL)
738 OPENSSL_DIR_end(&ctx->_.dir.ctx);
743 static int file_close_stream(struct file_ctx_st *ctx)
746 * This frees either the provider BIO filter (for file_attach()) OR
747 * the allocated file BIO (for file_open()).
749 BIO_free(ctx->_.file.file);
750 ctx->_.file.file = NULL;
756 static int file_close(void *loaderctx)
758 struct file_ctx_st *ctx = loaderctx;
762 return file_close_dir(ctx);
764 return file_close_stream(ctx);
767 /* ctx->type has an unexpected value */
772 const OSSL_DISPATCH ossl_file_store_functions[] = {
773 { OSSL_FUNC_STORE_OPEN, (void (*)(void))file_open },
774 { OSSL_FUNC_STORE_ATTACH, (void (*)(void))file_attach },
775 { OSSL_FUNC_STORE_SETTABLE_CTX_PARAMS,
776 (void (*)(void))file_settable_ctx_params },
777 { OSSL_FUNC_STORE_SET_CTX_PARAMS, (void (*)(void))file_set_ctx_params },
778 { OSSL_FUNC_STORE_LOAD, (void (*)(void))file_load },
779 { OSSL_FUNC_STORE_EOF, (void (*)(void))file_eof },
780 { OSSL_FUNC_STORE_CLOSE, (void (*)(void))file_close },