2 * Copyright 2016-2020 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
16 #include <openssl/bio.h>
17 #include <openssl/dsa.h> /* For d2i_DSAPrivateKey */
18 #include <openssl/err.h>
19 #include <openssl/evp.h>
20 #include <openssl/pem.h>
21 #include <openssl/pkcs12.h> /* For the PKCS8 stuff o.O */
22 #include <openssl/rsa.h> /* For d2i_RSAPrivateKey */
23 #include <openssl/safestack.h>
24 #include <openssl/store.h>
25 #include <openssl/ui.h>
26 #include <openssl/x509.h> /* For the PKCS8 stuff o.O */
27 #include "crypto/asn1.h"
28 #include "crypto/ctype.h"
29 #include "internal/o_dir.h"
30 #include "internal/cryptlib.h"
31 #include "crypto/store.h"
32 #include "crypto/evp.h"
33 #include "store_local.h"
42 # define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
50 static char *file_get_pass(const UI_METHOD *ui_method, char *pass,
51 size_t maxsize, const char *prompt_info, void *data)
57 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS, ERR_R_MALLOC_FAILURE);
61 if (ui_method != NULL)
62 UI_set_method(ui, ui_method);
63 UI_add_user_data(ui, data);
65 if ((prompt = UI_construct_prompt(ui, "pass phrase",
66 prompt_info)) == NULL) {
67 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS, ERR_R_MALLOC_FAILURE);
69 } else if (!UI_add_input_string(ui, prompt, UI_INPUT_FLAG_DEFAULT_PWD,
70 pass, 0, maxsize - 1)) {
71 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS, ERR_R_UI_LIB);
74 switch (UI_process(ui)) {
76 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS,
77 OSSL_STORE_R_UI_PROCESS_INTERRUPTED_OR_CANCELLED);
81 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS, ERR_R_UI_LIB);
94 struct pem_pass_data {
95 const UI_METHOD *ui_method;
97 const char *prompt_info;
100 static int file_fill_pem_pass_data(struct pem_pass_data *pass_data,
101 const char *prompt_info,
102 const UI_METHOD *ui_method, void *ui_data)
104 if (pass_data == NULL)
106 pass_data->ui_method = ui_method;
107 pass_data->data = ui_data;
108 pass_data->prompt_info = prompt_info;
112 /* This is used anywhere a pem_password_cb is needed */
113 static int file_get_pem_pass(char *buf, int num, int w, void *data)
115 struct pem_pass_data *pass_data = data;
116 char *pass = file_get_pass(pass_data->ui_method, buf, num,
117 pass_data->prompt_info, pass_data->data);
119 return pass == NULL ? 0 : strlen(pass);
123 * The file scheme decoders
124 * ------------------------
126 * Each possible data type has its own decoder, which either operates
127 * through a given PEM name, or attempts to decode to see if the blob
128 * it's given is decodable for its data type. The assumption is that
129 * only the correct data type will match the content.
133 * The try_decode function is called to check if the blob of data can
134 * be used by this handler, and if it can, decodes it into a supported
135 * OpenSSL type and returns a OSSL_STORE_INFO with the decoded data.
137 * pem_name: If this blob comes from a PEM file, this holds
138 * the PEM name. If it comes from another type of
139 * file, this is NULL.
140 * pem_header: If this blob comes from a PEM file, this holds
141 * the PEM headers. If it comes from another type of
142 * file, this is NULL.
143 * blob: The blob of data to match with what this handler
145 * len: The length of the blob.
146 * handler_ctx: For a handler marked repeatable, this pointer can
147 * be used to create a context for the handler. IT IS
148 * THE HANDLER'S RESPONSIBILITY TO CREATE AND DESTROY
149 * THIS CONTEXT APPROPRIATELY, i.e. create on first call
150 * and destroy when about to return NULL.
151 * matchcount: A pointer to an int to count matches for this data.
152 * Usually becomes 0 (no match) or 1 (match!), but may
153 * be higher in the (unlikely) event that the data matches
154 * more than one possibility. The int will always be
155 * zero when the function is called.
156 * ui_method: Application UI method for getting a password, pin
157 * or any other interactive data.
158 * ui_data: Application data to be passed to ui_method when
160 * libctx: The library context to be used if applicable
161 * propq: The property query string for any algorithm fetches
165 typedef OSSL_STORE_INFO *(*file_try_decode_fn)(const char *pem_name,
166 const char *pem_header,
167 const unsigned char *blob,
168 size_t len, void **handler_ctx,
170 const UI_METHOD *ui_method,
175 * The eof function should return 1 if there's no more data to be found
176 * with the handler_ctx, otherwise 0. This is only used when the handler is
179 typedef int (*file_eof_fn)(void *handler_ctx);
181 * The destroy_ctx function is used to destroy the handler_ctx that was
182 * initiated by a repeatable try_decode function. This is only used when
183 * the handler is marked repeatable.
185 typedef void (*file_destroy_ctx_fn)(void **handler_ctx);
187 typedef struct file_handler_st {
189 file_try_decode_fn try_decode;
191 file_destroy_ctx_fn destroy_ctx;
198 * PKCS#12 decoder. It operates by decoding all of the blob content,
199 * extracting all the interesting data from it and storing them internally,
200 * then serving them one piece at a time.
202 static OSSL_STORE_INFO *try_decode_PKCS12(const char *pem_name,
203 const char *pem_header,
204 const unsigned char *blob,
205 size_t len, void **pctx,
207 const UI_METHOD *ui_method,
208 void *ui_data, OPENSSL_CTX *libctx,
211 OSSL_STORE_INFO *store_info = NULL;
212 STACK_OF(OSSL_STORE_INFO) *ctx = *pctx;
215 /* Initial parsing */
219 if (pem_name != NULL)
220 /* No match, there is no PEM PKCS12 tag */
223 if ((p12 = d2i_PKCS12(NULL, &blob, len)) != NULL) {
225 char tpass[PEM_BUFSIZE];
226 EVP_PKEY *pkey = NULL;
228 STACK_OF(X509) *chain = NULL;
232 if (PKCS12_verify_mac(p12, "", 0)
233 || PKCS12_verify_mac(p12, NULL, 0)) {
236 if ((pass = file_get_pass(ui_method, tpass, PEM_BUFSIZE,
237 "PKCS12 import password",
239 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS12,
240 OSSL_STORE_R_PASSPHRASE_CALLBACK_ERROR);
243 if (!PKCS12_verify_mac(p12, pass, strlen(pass))) {
244 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS12,
245 OSSL_STORE_R_ERROR_VERIFYING_PKCS12_MAC);
250 if (PKCS12_parse(p12, pass, &pkey, &cert, &chain)) {
251 OSSL_STORE_INFO *osi_pkey = NULL;
252 OSSL_STORE_INFO *osi_cert = NULL;
253 OSSL_STORE_INFO *osi_ca = NULL;
255 if ((ctx = sk_OSSL_STORE_INFO_new_null()) != NULL
256 && (osi_pkey = OSSL_STORE_INFO_new_PKEY(pkey)) != NULL
257 && sk_OSSL_STORE_INFO_push(ctx, osi_pkey) != 0
258 && (osi_cert = OSSL_STORE_INFO_new_CERT(cert)) != NULL
259 && sk_OSSL_STORE_INFO_push(ctx, osi_cert) != 0) {
264 while(sk_X509_num(chain) > 0) {
265 X509 *ca = sk_X509_value(chain, 0);
267 if ((osi_ca = OSSL_STORE_INFO_new_CERT(ca)) == NULL
268 || sk_OSSL_STORE_INFO_push(ctx, osi_ca) == 0) {
273 (void)sk_X509_shift(chain);
277 OSSL_STORE_INFO_free(osi_ca);
278 OSSL_STORE_INFO_free(osi_cert);
279 OSSL_STORE_INFO_free(osi_pkey);
280 sk_OSSL_STORE_INFO_pop_free(ctx, OSSL_STORE_INFO_free);
283 sk_X509_pop_free(chain, X509_free);
297 store_info = sk_OSSL_STORE_INFO_shift(ctx);
303 static int eof_PKCS12(void *ctx_)
305 STACK_OF(OSSL_STORE_INFO) *ctx = ctx_;
307 return ctx == NULL || sk_OSSL_STORE_INFO_num(ctx) == 0;
310 static void destroy_ctx_PKCS12(void **pctx)
312 STACK_OF(OSSL_STORE_INFO) *ctx = *pctx;
314 sk_OSSL_STORE_INFO_pop_free(ctx, OSSL_STORE_INFO_free);
318 static FILE_HANDLER PKCS12_handler = {
327 * Encrypted PKCS#8 decoder. It operates by just decrypting the given blob
328 * into a new blob, which is returned as an EMBEDDED STORE_INFO. The whole
329 * decoding process will then start over with the new blob.
331 static OSSL_STORE_INFO *try_decode_PKCS8Encrypted(const char *pem_name,
332 const char *pem_header,
333 const unsigned char *blob,
334 size_t len, void **pctx,
336 const UI_METHOD *ui_method,
342 char kbuf[PEM_BUFSIZE];
344 const X509_ALGOR *dalg = NULL;
345 const ASN1_OCTET_STRING *doct = NULL;
346 OSSL_STORE_INFO *store_info = NULL;
348 unsigned char *new_data = NULL;
351 if (pem_name != NULL) {
352 if (strcmp(pem_name, PEM_STRING_PKCS8) != 0)
357 if ((p8 = d2i_X509_SIG(NULL, &blob, len)) == NULL)
362 if ((mem = BUF_MEM_new()) == NULL) {
363 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS8ENCRYPTED,
364 ERR_R_MALLOC_FAILURE);
368 if ((pass = file_get_pass(ui_method, kbuf, PEM_BUFSIZE,
369 "PKCS8 decrypt password", ui_data)) == NULL) {
370 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS8ENCRYPTED,
371 OSSL_STORE_R_BAD_PASSWORD_READ);
375 X509_SIG_get0(p8, &dalg, &doct);
376 if (!PKCS12_pbe_crypt(dalg, pass, strlen(pass), doct->data, doct->length,
377 &new_data, &new_data_len, 0))
380 mem->data = (char *)new_data;
381 mem->max = mem->length = (size_t)new_data_len;
384 store_info = ossl_store_info_new_EMBEDDED(PEM_STRING_PKCS8INF, mem);
385 if (store_info == NULL) {
386 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS8ENCRYPTED,
387 ERR_R_MALLOC_FAILURE);
398 static FILE_HANDLER PKCS8Encrypted_handler = {
400 try_decode_PKCS8Encrypted
404 * Private key decoder. Decodes all sorts of private keys, both PKCS#8
405 * encoded ones and old style PEM ones (with the key type is encoded into
408 int pem_check_suffix(const char *pem_str, const char *suffix);
409 static OSSL_STORE_INFO *try_decode_PrivateKey(const char *pem_name,
410 const char *pem_header,
411 const unsigned char *blob,
412 size_t len, void **pctx,
414 const UI_METHOD *ui_method,
415 void *ui_data, OPENSSL_CTX *libctx,
418 OSSL_STORE_INFO *store_info = NULL;
419 EVP_PKEY *pkey = NULL;
420 const EVP_PKEY_ASN1_METHOD *ameth = NULL;
422 if (pem_name != NULL) {
423 if (strcmp(pem_name, PEM_STRING_PKCS8INF) == 0) {
424 PKCS8_PRIV_KEY_INFO *p8inf =
425 d2i_PKCS8_PRIV_KEY_INFO(NULL, &blob, len);
429 pkey = evp_pkcs82pkey_int(p8inf, libctx, propq);
430 PKCS8_PRIV_KEY_INFO_free(p8inf);
434 if ((slen = pem_check_suffix(pem_name, "PRIVATE KEY")) > 0
435 && (ameth = EVP_PKEY_asn1_find_str(NULL, pem_name,
438 pkey = d2i_PrivateKey_ex(ameth->pkey_id, NULL, &blob, len,
445 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
446 EVP_PKEY *tmp_pkey = NULL;
447 const unsigned char *tmp_blob = blob;
449 ameth = EVP_PKEY_asn1_get0(i);
450 if (ameth->pkey_flags & ASN1_PKEY_ALIAS)
453 tmp_pkey = d2i_PrivateKey_ex(ameth->pkey_id, NULL, &tmp_blob, len,
455 if (tmp_pkey != NULL) {
457 EVP_PKEY_free(tmp_pkey);
464 if (*matchcount > 1) {
473 store_info = OSSL_STORE_INFO_new_PKEY(pkey);
474 if (store_info == NULL)
480 static FILE_HANDLER PrivateKey_handler = {
482 try_decode_PrivateKey
486 * Public key decoder. Only supports SubjectPublicKeyInfo formatted keys.
488 static OSSL_STORE_INFO *try_decode_PUBKEY(const char *pem_name,
489 const char *pem_header,
490 const unsigned char *blob,
491 size_t len, void **pctx,
493 const UI_METHOD *ui_method,
494 void *ui_data, OPENSSL_CTX *libctx,
497 OSSL_STORE_INFO *store_info = NULL;
498 EVP_PKEY *pkey = NULL;
500 if (pem_name != NULL) {
501 if (strcmp(pem_name, PEM_STRING_PUBLIC) != 0)
507 if ((pkey = d2i_PUBKEY(NULL, &blob, len)) != NULL) {
509 store_info = OSSL_STORE_INFO_new_PKEY(pkey);
515 static FILE_HANDLER PUBKEY_handler = {
521 * Key parameter decoder.
523 static OSSL_STORE_INFO *try_decode_params(const char *pem_name,
524 const char *pem_header,
525 const unsigned char *blob,
526 size_t len, void **pctx,
528 const UI_METHOD *ui_method,
529 void *ui_data, OPENSSL_CTX *libctx,
532 OSSL_STORE_INFO *store_info = NULL;
534 EVP_PKEY *pkey = NULL;
535 const EVP_PKEY_ASN1_METHOD *ameth = NULL;
538 if (pem_name != NULL) {
539 if ((slen = pem_check_suffix(pem_name, "PARAMETERS")) == 0)
545 if ((pkey = EVP_PKEY_new()) == NULL) {
546 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PARAMS, ERR_R_EVP_LIB);
551 if (EVP_PKEY_set_type_str(pkey, pem_name, slen)
552 && (ameth = EVP_PKEY_get0_asn1(pkey)) != NULL
553 && ameth->param_decode != NULL
554 && ameth->param_decode(pkey, &blob, len))
558 EVP_PKEY *tmp_pkey = NULL;
560 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
561 const unsigned char *tmp_blob = blob;
563 if (tmp_pkey == NULL && (tmp_pkey = EVP_PKEY_new()) == NULL) {
564 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PARAMS, ERR_R_EVP_LIB);
568 ameth = EVP_PKEY_asn1_get0(i);
569 if (ameth->pkey_flags & ASN1_PKEY_ALIAS)
572 if (EVP_PKEY_set_type(tmp_pkey, ameth->pkey_id)
573 && (ameth = EVP_PKEY_get0_asn1(tmp_pkey)) != NULL
574 && ameth->param_decode != NULL
575 && ameth->param_decode(tmp_pkey, &tmp_blob, len)) {
577 EVP_PKEY_free(tmp_pkey);
585 EVP_PKEY_free(tmp_pkey);
586 if (*matchcount == 1) {
592 store_info = OSSL_STORE_INFO_new_PARAMS(pkey);
593 if (store_info == NULL)
599 static FILE_HANDLER params_handler = {
605 * X.509 certificate decoder.
607 static OSSL_STORE_INFO *try_decode_X509Certificate(const char *pem_name,
608 const char *pem_header,
609 const unsigned char *blob,
610 size_t len, void **pctx,
612 const UI_METHOD *ui_method,
617 OSSL_STORE_INFO *store_info = NULL;
621 * In most cases, we can try to interpret the serialized data as a trusted
622 * cert (X509 + X509_AUX) and fall back to reading it as a normal cert
623 * (just X509), but if the PEM name specifically declares it as a trusted
624 * cert, then no fallback should be engaged. |ignore_trusted| tells if
625 * the fallback can be used (1) or not (0).
627 int ignore_trusted = 1;
629 if (pem_name != NULL) {
630 if (strcmp(pem_name, PEM_STRING_X509_TRUSTED) == 0)
632 else if (strcmp(pem_name, PEM_STRING_X509_OLD) != 0
633 && strcmp(pem_name, PEM_STRING_X509) != 0)
639 if ((cert = d2i_X509_AUX(NULL, &blob, len)) != NULL
640 || (ignore_trusted && (cert = d2i_X509(NULL, &blob, len)) != NULL)) {
642 store_info = OSSL_STORE_INFO_new_CERT(cert);
645 if (store_info == NULL)
651 static FILE_HANDLER X509Certificate_handler = {
653 try_decode_X509Certificate
659 static OSSL_STORE_INFO *try_decode_X509CRL(const char *pem_name,
660 const char *pem_header,
661 const unsigned char *blob,
662 size_t len, void **pctx,
664 const UI_METHOD *ui_method,
665 void *ui_data, OPENSSL_CTX *libctx,
668 OSSL_STORE_INFO *store_info = NULL;
669 X509_CRL *crl = NULL;
671 if (pem_name != NULL) {
672 if (strcmp(pem_name, PEM_STRING_X509_CRL) != 0)
678 if ((crl = d2i_X509_CRL(NULL, &blob, len)) != NULL) {
680 store_info = OSSL_STORE_INFO_new_CRL(crl);
683 if (store_info == NULL)
689 static FILE_HANDLER X509CRL_handler = {
695 * To finish it all off, we collect all the handlers.
697 static const FILE_HANDLER *file_handlers[] = {
699 &PKCS8Encrypted_handler,
700 &X509Certificate_handler,
713 struct ossl_store_loader_ctx_st {
720 #define FILE_FLAG_SECMEM (1<<0)
723 struct { /* Used with is_raw and is_pem */
727 * The following are used when the handler is marked as
730 const FILE_HANDLER *last_handler;
731 void *last_handler_ctx;
733 struct { /* Used with is_dir */
734 OPENSSL_DIR_CTX *ctx;
739 * When a search expression is given, these are filled in.
740 * |search_name| contains the file basename to look for.
741 * The string is exactly 8 characters long.
746 * The directory reading utility we have combines opening with
747 * reading the first name. To make sure we can detect the end
748 * at the right time, we read early and cache the name.
750 const char *last_entry;
755 /* Expected object type. May be unspecified */
762 static void OSSL_STORE_LOADER_CTX_free(OSSL_STORE_LOADER_CTX *ctx)
764 if (ctx->type == is_dir) {
765 OPENSSL_free(ctx->_.dir.uri);
767 if (ctx->_.file.last_handler != NULL) {
768 ctx->_.file.last_handler->destroy_ctx(&ctx->_.file.last_handler_ctx);
769 ctx->_.file.last_handler_ctx = NULL;
770 ctx->_.file.last_handler = NULL;
773 OPENSSL_free(ctx->propq);
777 static OSSL_STORE_LOADER_CTX *file_open(const OSSL_STORE_LOADER *loader,
779 const UI_METHOD *ui_method,
782 OSSL_STORE_LOADER_CTX *ctx = NULL;
786 unsigned int check_absolute:1;
788 size_t path_data_n = 0, i;
792 * First step, just take the URI as is.
794 path_data[path_data_n].check_absolute = 0;
795 path_data[path_data_n++].path = uri;
798 * Second step, if the URI appears to start with the 'file' scheme,
799 * extract the path and make that the second path to check.
800 * There's a special case if the URI also contains an authority, then
801 * the full URI shouldn't be used as a path anywhere.
803 if (strncasecmp(uri, "file:", 5) == 0) {
804 const char *p = &uri[5];
806 if (strncmp(&uri[5], "//", 2) == 0) {
807 path_data_n--; /* Invalidate using the full URI */
808 if (strncasecmp(&uri[7], "localhost/", 10) == 0) {
810 } else if (uri[7] == '/') {
813 OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN,
814 OSSL_STORE_R_URI_AUTHORITY_UNSUPPORTED);
819 path_data[path_data_n].check_absolute = 1;
821 /* Windows file: URIs with a drive letter start with a / */
822 if (p[0] == '/' && p[2] == ':' && p[3] == '/') {
823 char c = ossl_tolower(p[1]);
825 if (c >= 'a' && c <= 'z') {
827 /* We know it's absolute, so no need to check */
828 path_data[path_data_n].check_absolute = 0;
832 path_data[path_data_n++].path = p;
836 for (i = 0, path = NULL; path == NULL && i < path_data_n; i++) {
838 * If the scheme "file" was an explicit part of the URI, the path must
839 * be absolute. So says RFC 8089
841 if (path_data[i].check_absolute && path_data[i].path[0] != '/') {
842 OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN,
843 OSSL_STORE_R_PATH_MUST_BE_ABSOLUTE);
844 ERR_add_error_data(1, path_data[i].path);
848 if (stat(path_data[i].path, &st) < 0) {
849 ERR_raise_data(ERR_LIB_SYS, errno,
853 path = path_data[i].path;
860 /* Successfully found a working path, clear possible collected errors */
863 ctx = OPENSSL_zalloc(sizeof(*ctx));
865 OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN, ERR_R_MALLOC_FAILURE);
869 if (S_ISDIR(st.st_mode)) {
871 * Try to copy everything, even if we know that some of them must be
872 * NULL for the moment. This prevents errors in the future, when more
873 * components may be used.
875 ctx->_.dir.uri = OPENSSL_strdup(uri);
878 if (ctx->_.dir.uri == NULL)
881 ctx->_.dir.last_entry = OPENSSL_DIR_read(&ctx->_.dir.ctx, path);
882 ctx->_.dir.last_errno = errno;
883 if (ctx->_.dir.last_entry == NULL) {
884 if (ctx->_.dir.last_errno != 0) {
886 OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN, ERR_R_SYS_LIB);
887 errno = ctx->_.dir.last_errno;
888 if (openssl_strerror_r(errno, errbuf, sizeof(errbuf)))
889 ERR_add_error_data(1, errbuf);
892 ctx->_.dir.end_reached = 1;
896 char peekbuf[4096] = { 0, };
898 if ((buff = BIO_new(BIO_f_buffer())) == NULL
899 || (ctx->_.file.file = BIO_new_file(path, "rb")) == NULL) {
904 ctx->_.file.file = BIO_push(buff, ctx->_.file.file);
905 if (BIO_buffer_peek(ctx->_.file.file, peekbuf, sizeof(peekbuf) - 1) > 0) {
906 peekbuf[sizeof(peekbuf) - 1] = '\0';
907 if (strstr(peekbuf, "-----BEGIN ") != NULL)
914 OSSL_STORE_LOADER_CTX_free(ctx);
918 static int file_ctrl(OSSL_STORE_LOADER_CTX *ctx, int cmd, va_list args)
923 case OSSL_STORE_C_USE_SECMEM:
925 int on = *(va_arg(args, int *));
929 ctx->flags &= ~FILE_FLAG_SECMEM;
932 ctx->flags |= FILE_FLAG_SECMEM;
935 OSSL_STOREerr(OSSL_STORE_F_FILE_CTRL,
936 ERR_R_PASSED_INVALID_ARGUMENT);
949 static int file_expect(OSSL_STORE_LOADER_CTX *ctx, int expected)
951 ctx->expected_type = expected;
955 static int file_find(OSSL_STORE_LOADER_CTX *ctx,
956 const OSSL_STORE_SEARCH *search)
959 * If ctx == NULL, the library is looking to know if this loader supports
960 * the given search type.
963 if (OSSL_STORE_SEARCH_get_type(search) == OSSL_STORE_SEARCH_BY_NAME) {
964 unsigned long hash = 0;
969 if (ctx->type != is_dir) {
970 OSSL_STOREerr(OSSL_STORE_F_FILE_FIND,
971 OSSL_STORE_R_SEARCH_ONLY_SUPPORTED_FOR_DIRECTORIES);
975 hash = X509_NAME_hash(OSSL_STORE_SEARCH_get0_name(search));
976 BIO_snprintf(ctx->_.dir.search_name, sizeof(ctx->_.dir.search_name),
982 OSSL_STOREerr(OSSL_STORE_F_FILE_FIND,
983 OSSL_STORE_R_UNSUPPORTED_SEARCH_TYPE);
987 /* Internal function to decode an already opened PEM file */
988 OSSL_STORE_LOADER_CTX *ossl_store_file_attach_pem_bio_int(BIO *bp,
992 OSSL_STORE_LOADER_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
995 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_FILE_ATTACH_PEM_BIO_INT,
996 ERR_R_MALLOC_FAILURE);
1000 ctx->_.file.file = bp;
1003 ctx->libctx = libctx;
1004 if (propq != NULL) {
1005 ctx->propq = OPENSSL_strdup(propq);
1006 if (ctx->propq == NULL) {
1007 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_FILE_ATTACH_PEM_BIO_INT,
1008 ERR_R_MALLOC_FAILURE);
1017 static OSSL_STORE_INFO *file_load_try_decode(OSSL_STORE_LOADER_CTX *ctx,
1018 const char *pem_name,
1019 const char *pem_header,
1020 unsigned char *data, size_t len,
1021 const UI_METHOD *ui_method,
1022 void *ui_data, int *matchcount)
1024 OSSL_STORE_INFO *result = NULL;
1025 BUF_MEM *new_mem = NULL;
1026 char *new_pem_name = NULL;
1032 void *handler_ctx = NULL;
1033 const FILE_HANDLER **matching_handlers =
1034 OPENSSL_zalloc(sizeof(*matching_handlers)
1035 * OSSL_NELEM(file_handlers));
1037 if (matching_handlers == NULL) {
1038 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD_TRY_DECODE,
1039 ERR_R_MALLOC_FAILURE);
1044 for (i = 0; i < OSSL_NELEM(file_handlers); i++) {
1045 const FILE_HANDLER *handler = file_handlers[i];
1046 int try_matchcount = 0;
1047 void *tmp_handler_ctx = NULL;
1048 OSSL_STORE_INFO *tmp_result =
1049 handler->try_decode(pem_name, pem_header, data, len,
1050 &tmp_handler_ctx, &try_matchcount,
1051 ui_method, ui_data, ctx->libctx, ctx->propq);
1053 if (try_matchcount > 0) {
1055 matching_handlers[*matchcount] = handler;
1058 handler->destroy_ctx(&handler_ctx);
1059 handler_ctx = tmp_handler_ctx;
1061 if ((*matchcount += try_matchcount) > 1) {
1062 /* more than one match => ambiguous, kill any result */
1063 OSSL_STORE_INFO_free(result);
1064 OSSL_STORE_INFO_free(tmp_result);
1065 if (handler->destroy_ctx != NULL)
1066 handler->destroy_ctx(&handler_ctx);
1072 result = tmp_result;
1076 if (*matchcount == 1 && matching_handlers[0]->repeatable) {
1077 ctx->_.file.last_handler = matching_handlers[0];
1078 ctx->_.file.last_handler_ctx = handler_ctx;
1081 OPENSSL_free(matching_handlers);
1085 OPENSSL_free(new_pem_name);
1086 BUF_MEM_free(new_mem);
1089 && (t = OSSL_STORE_INFO_get_type(result)) == OSSL_STORE_INFO_EMBEDDED) {
1090 pem_name = new_pem_name =
1091 ossl_store_info_get0_EMBEDDED_pem_name(result);
1092 new_mem = ossl_store_info_get0_EMBEDDED_buffer(result);
1093 data = (unsigned char *)new_mem->data;
1094 len = new_mem->length;
1095 OPENSSL_free(result);
1106 static OSSL_STORE_INFO *file_load_try_repeat(OSSL_STORE_LOADER_CTX *ctx,
1107 const UI_METHOD *ui_method,
1110 OSSL_STORE_INFO *result = NULL;
1111 int try_matchcount = 0;
1113 if (ctx->_.file.last_handler != NULL) {
1115 ctx->_.file.last_handler->try_decode(NULL, NULL, NULL, 0,
1116 &ctx->_.file.last_handler_ctx,
1119 ctx->libctx, ctx->propq);
1121 if (result == NULL) {
1122 ctx->_.file.last_handler->destroy_ctx(&ctx->_.file.last_handler_ctx);
1123 ctx->_.file.last_handler_ctx = NULL;
1124 ctx->_.file.last_handler = NULL;
1130 static void pem_free_flag(void *pem_data, int secure, size_t num)
1133 OPENSSL_secure_clear_free(pem_data, num);
1135 OPENSSL_free(pem_data);
1137 static int file_read_pem(BIO *bp, char **pem_name, char **pem_header,
1138 unsigned char **data, long *len,
1139 const UI_METHOD *ui_method,
1140 void *ui_data, int secure)
1143 ? PEM_read_bio_ex(bp, pem_name, pem_header, data, len,
1144 PEM_FLAG_SECURE | PEM_FLAG_EAY_COMPATIBLE)
1145 : PEM_read_bio(bp, pem_name, pem_header, data, len);
1151 * 10 is the number of characters in "Proc-Type:", which
1152 * PEM_get_EVP_CIPHER_INFO() requires to be present.
1153 * If the PEM header has less characters than that, it's
1154 * not worth spending cycles on it.
1156 if (strlen(*pem_header) > 10) {
1157 EVP_CIPHER_INFO cipher;
1158 struct pem_pass_data pass_data;
1160 if (!PEM_get_EVP_CIPHER_INFO(*pem_header, &cipher)
1161 || !file_fill_pem_pass_data(&pass_data, "PEM", ui_method, ui_data)
1162 || !PEM_do_header(&cipher, *data, len, file_get_pem_pass,
1170 static int file_read_asn1(BIO *bp, unsigned char **data, long *len)
1172 BUF_MEM *mem = NULL;
1174 if (asn1_d2i_read_bio(bp, &mem) < 0)
1177 *data = (unsigned char *)mem->data;
1178 *len = (long)mem->length;
1184 static int ends_with_dirsep(const char *uri)
1187 uri += strlen(uri) - 1;
1189 if (*uri == ']' || *uri == '>' || *uri == ':')
1191 #elif defined _WIN32
1198 static int file_name_to_uri(OSSL_STORE_LOADER_CTX *ctx, const char *name,
1201 assert(name != NULL);
1202 assert(data != NULL);
1204 const char *pathsep = ends_with_dirsep(ctx->_.dir.uri) ? "" : "/";
1205 long calculated_length = strlen(ctx->_.dir.uri) + strlen(pathsep)
1206 + strlen(name) + 1 /* \0 */;
1208 *data = OPENSSL_zalloc(calculated_length);
1209 if (*data == NULL) {
1210 OSSL_STOREerr(OSSL_STORE_F_FILE_NAME_TO_URI, ERR_R_MALLOC_FAILURE);
1214 OPENSSL_strlcat(*data, ctx->_.dir.uri, calculated_length);
1215 OPENSSL_strlcat(*data, pathsep, calculated_length);
1216 OPENSSL_strlcat(*data, name, calculated_length);
1221 static int file_name_check(OSSL_STORE_LOADER_CTX *ctx, const char *name)
1223 const char *p = NULL;
1225 /* If there are no search criteria, all names are accepted */
1226 if (ctx->_.dir.search_name[0] == '\0')
1229 /* If the expected type isn't supported, no name is accepted */
1230 if (ctx->expected_type != 0
1231 && ctx->expected_type != OSSL_STORE_INFO_CERT
1232 && ctx->expected_type != OSSL_STORE_INFO_CRL)
1236 * First, check the basename
1238 if (strncasecmp(name, ctx->_.dir.search_name,
1239 sizeof(ctx->_.dir.search_name) - 1) != 0
1240 || name[sizeof(ctx->_.dir.search_name) - 1] != '.')
1242 p = &name[sizeof(ctx->_.dir.search_name)];
1245 * Then, if the expected type is a CRL, check that the extension starts
1250 if (ctx->expected_type != 0
1251 && ctx->expected_type != OSSL_STORE_INFO_CRL)
1253 } else if (ctx->expected_type == OSSL_STORE_INFO_CRL) {
1258 * Last, check that the rest of the extension is a decimal number, at
1259 * least one digit long.
1261 if (!ossl_isdigit(*p))
1263 while (ossl_isdigit(*p))
1268 * One extra step here, check for a possible generation number.
1271 for (p++; *p != '\0'; p++)
1272 if (!ossl_isdigit(*p))
1277 * If we've reached the end of the string at this point, we've successfully
1278 * found a fitting file name.
1283 static int file_eof(OSSL_STORE_LOADER_CTX *ctx);
1284 static int file_error(OSSL_STORE_LOADER_CTX *ctx);
1285 static OSSL_STORE_INFO *file_load(OSSL_STORE_LOADER_CTX *ctx,
1286 const UI_METHOD *ui_method, void *ui_data)
1288 OSSL_STORE_INFO *result = NULL;
1293 if (ctx->type == is_dir) {
1295 char *newname = NULL;
1297 if (ctx->_.dir.last_entry == NULL) {
1298 if (!ctx->_.dir.end_reached) {
1300 assert(ctx->_.dir.last_errno != 0);
1301 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD, ERR_R_SYS_LIB);
1302 errno = ctx->_.dir.last_errno;
1304 if (openssl_strerror_r(errno, errbuf, sizeof(errbuf)))
1305 ERR_add_error_data(1, errbuf);
1310 if (ctx->_.dir.last_entry[0] != '.'
1311 && file_name_check(ctx, ctx->_.dir.last_entry)
1312 && !file_name_to_uri(ctx, ctx->_.dir.last_entry, &newname))
1316 * On the first call (with a NULL context), OPENSSL_DIR_read()
1317 * cares about the second argument. On the following calls, it
1318 * only cares that it isn't NULL. Therefore, we can safely give
1321 ctx->_.dir.last_entry = OPENSSL_DIR_read(&ctx->_.dir.ctx,
1323 ctx->_.dir.last_errno = errno;
1324 if (ctx->_.dir.last_entry == NULL && ctx->_.dir.last_errno == 0)
1325 ctx->_.dir.end_reached = 1;
1328 && (result = OSSL_STORE_INFO_new_NAME(newname)) == NULL) {
1329 OPENSSL_free(newname);
1330 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD, ERR_R_OSSL_STORE_LIB);
1333 } while (result == NULL && !file_eof(ctx));
1335 int matchcount = -1;
1338 result = file_load_try_repeat(ctx, ui_method, ui_data);
1346 char *pem_name = NULL; /* PEM record name */
1347 char *pem_header = NULL; /* PEM record header */
1348 unsigned char *data = NULL; /* DER encoded data */
1349 long len = 0; /* DER encoded data length */
1352 if (ctx->type == is_pem) {
1353 if (!file_read_pem(ctx->_.file.file, &pem_name, &pem_header,
1354 &data, &len, ui_method, ui_data,
1355 (ctx->flags & FILE_FLAG_SECMEM) != 0)) {
1360 if (!file_read_asn1(ctx->_.file.file, &data, &len)) {
1366 result = file_load_try_decode(ctx, pem_name, pem_header, data, len,
1367 ui_method, ui_data, &matchcount);
1373 * If a PEM name matches more than one handler, the handlers are
1376 if (!ossl_assert(pem_name == NULL || matchcount <= 1)) {
1381 if (matchcount > 1) {
1382 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD,
1383 OSSL_STORE_R_AMBIGUOUS_CONTENT_TYPE);
1384 } else if (matchcount == 1) {
1386 * If there are other errors on the stack, they already show
1387 * what the problem is.
1389 if (ERR_peek_error() == 0) {
1390 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD,
1391 OSSL_STORE_R_UNSUPPORTED_CONTENT_TYPE);
1392 if (pem_name != NULL)
1393 ERR_add_error_data(3, "PEM type is '", pem_name, "'");
1400 pem_free_flag(pem_name, (ctx->flags & FILE_FLAG_SECMEM) != 0, 0);
1401 pem_free_flag(pem_header, (ctx->flags & FILE_FLAG_SECMEM) != 0, 0);
1402 pem_free_flag(data, (ctx->flags & FILE_FLAG_SECMEM) != 0, len);
1403 } while (matchcount == 0 && !file_eof(ctx) && !file_error(ctx));
1405 /* We bail out on ambiguity */
1410 && ctx->expected_type != 0
1411 && ctx->expected_type != OSSL_STORE_INFO_get_type(result)) {
1412 OSSL_STORE_INFO_free(result);
1420 static int file_error(OSSL_STORE_LOADER_CTX *ctx)
1422 return ctx->errcnt > 0;
1425 static int file_eof(OSSL_STORE_LOADER_CTX *ctx)
1427 if (ctx->type == is_dir)
1428 return ctx->_.dir.end_reached;
1430 if (ctx->_.file.last_handler != NULL
1431 && !ctx->_.file.last_handler->eof(ctx->_.file.last_handler_ctx))
1433 return BIO_eof(ctx->_.file.file);
1436 static int file_close(OSSL_STORE_LOADER_CTX *ctx)
1438 if (ctx->type == is_dir) {
1439 OPENSSL_DIR_end(&ctx->_.dir.ctx);
1441 BIO_free_all(ctx->_.file.file);
1443 OSSL_STORE_LOADER_CTX_free(ctx);
1447 int ossl_store_file_detach_pem_bio_int(OSSL_STORE_LOADER_CTX *ctx)
1449 OSSL_STORE_LOADER_CTX_free(ctx);
1453 static OSSL_STORE_LOADER file_loader =
1467 static void store_file_loader_deinit(void)
1469 ossl_store_unregister_loader_int(file_loader.scheme);
1472 int ossl_store_file_loader_init(void)
1474 int ret = ossl_store_register_loader_int(&file_loader);
1476 OPENSSL_atexit(store_file_loader_deinit);