2 * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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 "internal/asn1_int.h"
28 #include "internal/ctype.h"
29 #include "internal/o_dir.h"
30 #include "internal/cryptlib.h"
31 #include "internal/store_int.h"
32 #include "store_locl.h"
43 static char *file_get_pass(const UI_METHOD *ui_method, char *pass,
44 size_t maxsize, const char *prompt_info, void *data)
50 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS, ERR_R_MALLOC_FAILURE);
54 if (ui_method != NULL)
55 UI_set_method(ui, ui_method);
56 UI_add_user_data(ui, data);
58 if ((prompt = UI_construct_prompt(ui, "pass phrase",
59 prompt_info)) == NULL) {
60 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS, ERR_R_MALLOC_FAILURE);
62 } else if (!UI_add_input_string(ui, prompt, UI_INPUT_FLAG_DEFAULT_PWD,
63 pass, 0, maxsize - 1)) {
64 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS, ERR_R_UI_LIB);
67 switch (UI_process(ui)) {
69 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS,
70 OSSL_STORE_R_UI_PROCESS_INTERRUPTED_OR_CANCELLED);
74 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS, ERR_R_UI_LIB);
87 struct pem_pass_data {
88 const UI_METHOD *ui_method;
90 const char *prompt_info;
93 static int file_fill_pem_pass_data(struct pem_pass_data *pass_data,
94 const char *prompt_info,
95 const UI_METHOD *ui_method, void *ui_data)
97 if (pass_data == NULL)
99 pass_data->ui_method = ui_method;
100 pass_data->data = ui_data;
101 pass_data->prompt_info = prompt_info;
105 /* This is used anywhere a pem_password_cb is needed */
106 static int file_get_pem_pass(char *buf, int num, int w, void *data)
108 struct pem_pass_data *pass_data = data;
109 char *pass = file_get_pass(pass_data->ui_method, buf, num,
110 pass_data->prompt_info, pass_data->data);
112 return pass == NULL ? 0 : strlen(pass);
116 * The file scheme decoders
117 * ------------------------
119 * Each possible data type has its own decoder, which either operates
120 * through a given PEM name, or attempts to decode to see if the blob
121 * it's given is decodable for its data type. The assumption is that
122 * only the correct data type will match the content.
126 * The try_decode function is called to check if the blob of data can
127 * be used by this handler, and if it can, decodes it into a supported
128 * OpenSSL type and returns a OSSL_STORE_INFO with the decoded data.
130 * pem_name: If this blob comes from a PEM file, this holds
131 * the PEM name. If it comes from another type of
132 * file, this is NULL.
133 * pem_header: If this blob comes from a PEM file, this holds
134 * the PEM headers. If it comes from another type of
135 * file, this is NULL.
136 * blob: The blob of data to match with what this handler
138 * len: The length of the blob.
139 * handler_ctx: For a handler marked repeatable, this pointer can
140 * be used to create a context for the handler. IT IS
141 * THE HANDLER'S RESPONSIBILITY TO CREATE AND DESTROY
142 * THIS CONTEXT APPROPRIATELY, i.e. create on first call
143 * and destroy when about to return NULL.
144 * matchcount: A pointer to an int to count matches for this data.
145 * Usually becomes 0 (no match) or 1 (match!), but may
146 * be higher in the (unlikely) event that the data matches
147 * more than one possibility. The int will always be
148 * zero when the function is called.
149 * ui_method: Application UI method for getting a password, pin
150 * or any other interactive data.
151 * ui_data: Application data to be passed to ui_method when
156 typedef OSSL_STORE_INFO *(*file_try_decode_fn)(const char *pem_name,
157 const char *pem_header,
158 const unsigned char *blob,
159 size_t len, void **handler_ctx,
161 const UI_METHOD *ui_method,
164 * The eof function should return 1 if there's no more data to be found
165 * with the handler_ctx, otherwise 0. This is only used when the handler is
168 typedef int (*file_eof_fn)(void *handler_ctx);
170 * The destroy_ctx function is used to destroy the handler_ctx that was
171 * intiated by a repeatable try_decode fuction. This is only used when
172 * the handler is marked repeatable.
174 typedef void (*file_destroy_ctx_fn)(void **handler_ctx);
176 typedef struct file_handler_st {
178 file_try_decode_fn try_decode;
180 file_destroy_ctx_fn destroy_ctx;
187 * PKCS#12 decoder. It operates by decoding all of the blob content,
188 * extracting all the interesting data from it and storing them internally,
189 * then serving them one piece at a time.
191 static OSSL_STORE_INFO *try_decode_PKCS12(const char *pem_name,
192 const char *pem_header,
193 const unsigned char *blob,
194 size_t len, void **pctx,
196 const UI_METHOD *ui_method,
199 OSSL_STORE_INFO *store_info = NULL;
200 STACK_OF(OSSL_STORE_INFO) *ctx = *pctx;
203 /* Initial parsing */
207 if (pem_name != NULL)
208 /* No match, there is no PEM PKCS12 tag */
211 if ((p12 = d2i_PKCS12(NULL, &blob, len)) != NULL) {
213 char tpass[PEM_BUFSIZE];
214 EVP_PKEY *pkey = NULL;
216 STACK_OF(X509) *chain = NULL;
220 if (PKCS12_verify_mac(p12, "", 0)
221 || PKCS12_verify_mac(p12, NULL, 0)) {
224 if ((pass = file_get_pass(ui_method, tpass, PEM_BUFSIZE,
225 "PKCS12 import password",
227 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS12,
228 OSSL_STORE_R_PASSPHRASE_CALLBACK_ERROR);
231 if (!PKCS12_verify_mac(p12, pass, strlen(pass))) {
232 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS12,
233 OSSL_STORE_R_ERROR_VERIFYING_PKCS12_MAC);
238 if (PKCS12_parse(p12, pass, &pkey, &cert, &chain)) {
239 OSSL_STORE_INFO *si_pkey = NULL;
240 OSSL_STORE_INFO *si_cert = NULL;
241 OSSL_STORE_INFO *si_ca = NULL;
243 if ((ctx = sk_OSSL_STORE_INFO_new_null()) != NULL
244 && (si_pkey = OSSL_STORE_INFO_new_PKEY(pkey)) != NULL
245 && sk_OSSL_STORE_INFO_push(ctx, si_pkey) != 0
246 && (si_cert = OSSL_STORE_INFO_new_CERT(cert)) != NULL
247 && sk_OSSL_STORE_INFO_push(ctx, si_cert) != 0) {
252 while(sk_X509_num(chain) > 0) {
253 X509 *ca = sk_X509_value(chain, 0);
255 if ((si_ca = OSSL_STORE_INFO_new_CERT(ca)) == NULL
256 || sk_OSSL_STORE_INFO_push(ctx, si_ca) == 0) {
261 (void)sk_X509_shift(chain);
265 OSSL_STORE_INFO_free(si_ca);
266 OSSL_STORE_INFO_free(si_cert);
267 OSSL_STORE_INFO_free(si_pkey);
268 sk_OSSL_STORE_INFO_pop_free(ctx, OSSL_STORE_INFO_free);
271 sk_X509_pop_free(chain, X509_free);
285 store_info = sk_OSSL_STORE_INFO_shift(ctx);
291 static int eof_PKCS12(void *ctx_)
293 STACK_OF(OSSL_STORE_INFO) *ctx = ctx_;
295 return ctx == NULL || sk_OSSL_STORE_INFO_num(ctx) == 0;
298 static void destroy_ctx_PKCS12(void **pctx)
300 STACK_OF(OSSL_STORE_INFO) *ctx = *pctx;
302 sk_OSSL_STORE_INFO_pop_free(ctx, OSSL_STORE_INFO_free);
306 static FILE_HANDLER PKCS12_handler = {
315 * Encrypted PKCS#8 decoder. It operates by just decrypting the given blob
316 * into a new blob, which is returned as an EMBEDDED STORE_INFO. The whole
317 * decoding process will then start over with the new blob.
319 static OSSL_STORE_INFO *try_decode_PKCS8Encrypted(const char *pem_name,
320 const char *pem_header,
321 const unsigned char *blob,
322 size_t len, void **pctx,
324 const UI_METHOD *ui_method,
328 char kbuf[PEM_BUFSIZE];
330 const X509_ALGOR *dalg = NULL;
331 const ASN1_OCTET_STRING *doct = NULL;
332 OSSL_STORE_INFO *store_info = NULL;
334 unsigned char *new_data = NULL;
337 if (pem_name != NULL) {
338 if (strcmp(pem_name, PEM_STRING_PKCS8) != 0)
343 if ((p8 = d2i_X509_SIG(NULL, &blob, len)) == NULL)
348 if ((mem = BUF_MEM_new()) == NULL) {
349 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS8ENCRYPTED,
350 ERR_R_MALLOC_FAILURE);
354 if ((pass = file_get_pass(ui_method, kbuf, PEM_BUFSIZE,
355 "PKCS8 decrypt password", ui_data)) == NULL) {
356 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS8ENCRYPTED,
357 OSSL_STORE_R_BAD_PASSWORD_READ);
361 X509_SIG_get0(p8, &dalg, &doct);
362 if (!PKCS12_pbe_crypt(dalg, pass, strlen(pass), doct->data, doct->length,
363 &new_data, &new_data_len, 0))
366 mem->data = (char *)new_data;
367 mem->max = mem->length = (size_t)new_data_len;
370 store_info = ossl_store_info_new_EMBEDDED(PEM_STRING_PKCS8INF, mem);
371 if (store_info == NULL) {
372 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS8ENCRYPTED,
373 ERR_R_MALLOC_FAILURE);
384 static FILE_HANDLER PKCS8Encrypted_handler = {
386 try_decode_PKCS8Encrypted
390 * Private key decoder. Decodes all sorts of private keys, both PKCS#8
391 * encoded ones and old style PEM ones (with the key type is encoded into
394 int pem_check_suffix(const char *pem_str, const char *suffix);
395 static OSSL_STORE_INFO *try_decode_PrivateKey(const char *pem_name,
396 const char *pem_header,
397 const unsigned char *blob,
398 size_t len, void **pctx,
400 const UI_METHOD *ui_method,
403 OSSL_STORE_INFO *store_info = NULL;
404 EVP_PKEY *pkey = NULL;
405 const EVP_PKEY_ASN1_METHOD *ameth = NULL;
407 if (pem_name != NULL) {
408 if (strcmp(pem_name, PEM_STRING_PKCS8INF) == 0) {
409 PKCS8_PRIV_KEY_INFO *p8inf =
410 d2i_PKCS8_PRIV_KEY_INFO(NULL, &blob, len);
414 pkey = EVP_PKCS82PKEY(p8inf);
415 PKCS8_PRIV_KEY_INFO_free(p8inf);
419 if ((slen = pem_check_suffix(pem_name, "PRIVATE KEY")) > 0
420 && (ameth = EVP_PKEY_asn1_find_str(NULL, pem_name,
423 pkey = d2i_PrivateKey(ameth->pkey_id, NULL, &blob, len);
429 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
430 EVP_PKEY *tmp_pkey = NULL;
431 const unsigned char *tmp_blob = blob;
433 ameth = EVP_PKEY_asn1_get0(i);
434 if (ameth->pkey_flags & ASN1_PKEY_ALIAS)
437 tmp_pkey = d2i_PrivateKey(ameth->pkey_id, NULL, &tmp_blob, len);
438 if (tmp_pkey != NULL) {
440 EVP_PKEY_free(tmp_pkey);
447 if (*matchcount > 1) {
456 store_info = OSSL_STORE_INFO_new_PKEY(pkey);
457 if (store_info == NULL)
463 static FILE_HANDLER PrivateKey_handler = {
465 try_decode_PrivateKey
469 * Public key decoder. Only supports SubjectPublicKeyInfo formated keys.
471 static OSSL_STORE_INFO *try_decode_PUBKEY(const char *pem_name,
472 const char *pem_header,
473 const unsigned char *blob,
474 size_t len, void **pctx,
476 const UI_METHOD *ui_method,
479 OSSL_STORE_INFO *store_info = NULL;
480 EVP_PKEY *pkey = NULL;
482 if (pem_name != NULL) {
483 if (strcmp(pem_name, PEM_STRING_PUBLIC) != 0)
489 if ((pkey = d2i_PUBKEY(NULL, &blob, len)) != NULL) {
491 store_info = OSSL_STORE_INFO_new_PKEY(pkey);
497 static FILE_HANDLER PUBKEY_handler = {
503 * Key parameter decoder.
505 static OSSL_STORE_INFO *try_decode_params(const char *pem_name,
506 const char *pem_header,
507 const unsigned char *blob,
508 size_t len, void **pctx,
510 const UI_METHOD *ui_method,
513 OSSL_STORE_INFO *store_info = NULL;
515 EVP_PKEY *pkey = NULL;
516 const EVP_PKEY_ASN1_METHOD *ameth = NULL;
519 if (pem_name != NULL) {
520 if ((slen = pem_check_suffix(pem_name, "PARAMETERS")) == 0)
526 if ((pkey = EVP_PKEY_new()) == NULL) {
527 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PARAMS, ERR_R_EVP_LIB);
532 if (EVP_PKEY_set_type_str(pkey, pem_name, slen)
533 && (ameth = EVP_PKEY_get0_asn1(pkey)) != NULL
534 && ameth->param_decode != NULL
535 && ameth->param_decode(pkey, &blob, len))
539 EVP_PKEY *tmp_pkey = NULL;
541 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
542 const unsigned char *tmp_blob = blob;
544 if (tmp_pkey == NULL && (tmp_pkey = EVP_PKEY_new()) == NULL) {
545 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PARAMS, ERR_R_EVP_LIB);
549 ameth = EVP_PKEY_asn1_get0(i);
550 if (ameth->pkey_flags & ASN1_PKEY_ALIAS)
553 if (EVP_PKEY_set_type(tmp_pkey, ameth->pkey_id)
554 && (ameth = EVP_PKEY_get0_asn1(tmp_pkey)) != NULL
555 && ameth->param_decode != NULL
556 && ameth->param_decode(tmp_pkey, &tmp_blob, len)) {
558 EVP_PKEY_free(tmp_pkey);
566 EVP_PKEY_free(tmp_pkey);
567 if (*matchcount == 1) {
573 store_info = OSSL_STORE_INFO_new_PARAMS(pkey);
574 if (store_info == NULL)
580 static FILE_HANDLER params_handler = {
586 * X.509 certificate decoder.
588 static OSSL_STORE_INFO *try_decode_X509Certificate(const char *pem_name,
589 const char *pem_header,
590 const unsigned char *blob,
591 size_t len, void **pctx,
593 const UI_METHOD *ui_method,
596 OSSL_STORE_INFO *store_info = NULL;
600 * In most cases, we can try to interpret the serialized data as a trusted
601 * cert (X509 + X509_AUX) and fall back to reading it as a normal cert
602 * (just X509), but if the PEM name specifically declares it as a trusted
603 * cert, then no fallback should be engaged. |ignore_trusted| tells if
604 * the fallback can be used (1) or not (0).
606 int ignore_trusted = 1;
608 if (pem_name != NULL) {
609 if (strcmp(pem_name, PEM_STRING_X509_TRUSTED) == 0)
611 else if (strcmp(pem_name, PEM_STRING_X509_OLD) != 0
612 && strcmp(pem_name, PEM_STRING_X509) != 0)
618 if ((cert = d2i_X509_AUX(NULL, &blob, len)) != NULL
619 || (ignore_trusted && (cert = d2i_X509(NULL, &blob, len)) != NULL)) {
621 store_info = OSSL_STORE_INFO_new_CERT(cert);
624 if (store_info == NULL)
630 static FILE_HANDLER X509Certificate_handler = {
632 try_decode_X509Certificate
638 static OSSL_STORE_INFO *try_decode_X509CRL(const char *pem_name,
639 const char *pem_header,
640 const unsigned char *blob,
641 size_t len, void **pctx,
643 const UI_METHOD *ui_method,
646 OSSL_STORE_INFO *store_info = NULL;
647 X509_CRL *crl = NULL;
649 if (pem_name != NULL) {
650 if (strcmp(pem_name, PEM_STRING_X509_CRL) != 0)
656 if ((crl = d2i_X509_CRL(NULL, &blob, len)) != NULL) {
658 store_info = OSSL_STORE_INFO_new_CRL(crl);
661 if (store_info == NULL)
667 static FILE_HANDLER X509CRL_handler = {
673 * To finish it all off, we collect all the handlers.
675 static const FILE_HANDLER *file_handlers[] = {
677 &PKCS8Encrypted_handler,
678 &X509Certificate_handler,
691 struct ossl_store_loader_ctx_st {
698 #define FILE_FLAG_SECMEM (1<<0)
701 struct { /* Used with is_raw and is_pem */
705 * The following are used when the handler is marked as
708 const FILE_HANDLER *last_handler;
709 void *last_handler_ctx;
711 struct { /* Used with is_dir */
712 OPENSSL_DIR_CTX *ctx;
717 * When a search expression is given, these are filled in.
718 * |search_name| contains the file basename to look for.
719 * The string is exactly 8 characters long.
724 * The directory reading utility we have combines opening with
725 * reading the first name. To make sure we can detect the end
726 * at the right time, we read early and cache the name.
728 const char *last_entry;
733 /* Expected object type. May be unspecified */
737 static void OSSL_STORE_LOADER_CTX_free(OSSL_STORE_LOADER_CTX *ctx)
739 if (ctx->type == is_dir) {
740 OPENSSL_free(ctx->_.dir.uri);
742 if (ctx->_.file.last_handler != NULL) {
743 ctx->_.file.last_handler->destroy_ctx(&ctx->_.file.last_handler_ctx);
744 ctx->_.file.last_handler_ctx = NULL;
745 ctx->_.file.last_handler = NULL;
751 static OSSL_STORE_LOADER_CTX *file_open(const OSSL_STORE_LOADER *loader,
753 const UI_METHOD *ui_method,
756 OSSL_STORE_LOADER_CTX *ctx = NULL;
760 unsigned int check_absolute:1;
762 size_t path_data_n = 0, i;
766 * First step, just take the URI as is.
768 path_data[path_data_n].check_absolute = 0;
769 path_data[path_data_n++].path = uri;
772 * Second step, if the URI appears to start with the 'file' scheme,
773 * extract the path and make that the second path to check.
774 * There's a special case if the URI also contains an authority, then
775 * the full URI shouldn't be used as a path anywhere.
777 if (strncasecmp(uri, "file:", 5) == 0) {
778 const char *p = &uri[5];
780 if (strncmp(&uri[5], "//", 2) == 0) {
781 path_data_n--; /* Invalidate using the full URI */
782 if (strncasecmp(&uri[7], "localhost/", 10) == 0) {
784 } else if (uri[7] == '/') {
787 OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN,
788 OSSL_STORE_R_URI_AUTHORITY_UNSUPPORTED);
793 path_data[path_data_n].check_absolute = 1;
795 /* Windows file: URIs with a drive letter start with a / */
796 if (p[0] == '/' && p[2] == ':' && p[3] == '/') {
797 char c = ossl_tolower(p[1]);
799 if (c >= 'a' && c <= 'z') {
801 /* We know it's absolute, so no need to check */
802 path_data[path_data_n].check_absolute = 0;
806 path_data[path_data_n++].path = p;
810 for (i = 0, path = NULL; path == NULL && i < path_data_n; i++) {
812 * If the scheme "file" was an explicit part of the URI, the path must
813 * be absolute. So says RFC 8089
815 if (path_data[i].check_absolute && path_data[i].path[0] != '/') {
816 OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN,
817 OSSL_STORE_R_PATH_MUST_BE_ABSOLUTE);
818 ERR_add_error_data(1, path_data[i].path);
822 if (stat(path_data[i].path, &st) < 0) {
823 SYSerr(SYS_F_STAT, errno);
824 ERR_add_error_data(1, path_data[i].path);
826 path = path_data[i].path;
833 /* Successfully found a working path, clear possible collected errors */
836 ctx = OPENSSL_zalloc(sizeof(*ctx));
838 OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN, ERR_R_MALLOC_FAILURE);
842 if ((st.st_mode & S_IFDIR) == S_IFDIR) {
844 * Try to copy everything, even if we know that some of them must be
845 * NULL for the moment. This prevents errors in the future, when more
846 * components may be used.
848 ctx->_.dir.uri = OPENSSL_strdup(uri);
851 if (ctx->_.dir.uri == NULL)
854 ctx->_.dir.last_entry = OPENSSL_DIR_read(&ctx->_.dir.ctx, path);
855 ctx->_.dir.last_errno = errno;
856 if (ctx->_.dir.last_entry == NULL) {
857 if (ctx->_.dir.last_errno != 0) {
859 errno = ctx->_.dir.last_errno;
860 openssl_strerror_r(errno, errbuf, sizeof(errbuf));
861 OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN, ERR_R_SYS_LIB);
862 ERR_add_error_data(1, errbuf);
865 ctx->_.dir.end_reached = 1;
869 char peekbuf[4096] = { 0, };
871 if ((buff = BIO_new(BIO_f_buffer())) == NULL
872 || (ctx->_.file.file = BIO_new_file(path, "rb")) == NULL) {
877 ctx->_.file.file = BIO_push(buff, ctx->_.file.file);
878 if (BIO_buffer_peek(ctx->_.file.file, peekbuf, sizeof(peekbuf) - 1) > 0) {
879 peekbuf[sizeof(peekbuf) - 1] = '\0';
880 if (strstr(peekbuf, "-----BEGIN ") != NULL)
887 OSSL_STORE_LOADER_CTX_free(ctx);
891 static int file_ctrl(OSSL_STORE_LOADER_CTX *ctx, int cmd, va_list args)
896 case OSSL_STORE_C_USE_SECMEM:
898 int on = *(va_arg(args, int *));
902 ctx->flags &= ~FILE_FLAG_SECMEM;
905 ctx->flags |= FILE_FLAG_SECMEM;
908 OSSL_STOREerr(OSSL_STORE_F_FILE_CTRL,
909 ERR_R_PASSED_INVALID_ARGUMENT);
922 static int file_expect(OSSL_STORE_LOADER_CTX *ctx, int expected)
924 ctx->expected_type = expected;
928 static int file_find(OSSL_STORE_LOADER_CTX *ctx, OSSL_STORE_SEARCH *search)
931 * If ctx == NULL, the library is looking to know if this loader supports
932 * the given search type.
935 if (OSSL_STORE_SEARCH_get_type(search) == OSSL_STORE_SEARCH_BY_NAME) {
936 unsigned long hash = 0;
941 if (ctx->type != is_dir) {
942 OSSL_STOREerr(OSSL_STORE_F_FILE_FIND,
943 OSSL_STORE_R_SEARCH_ONLY_SUPPORTED_FOR_DIRECTORIES);
947 hash = X509_NAME_hash(OSSL_STORE_SEARCH_get0_name(search));
948 BIO_snprintf(ctx->_.dir.search_name, sizeof(ctx->_.dir.search_name),
954 OSSL_STOREerr(OSSL_STORE_F_FILE_FIND,
955 OSSL_STORE_R_UNSUPPORTED_SEARCH_TYPE);
959 /* Internal function to decode an already opened PEM file */
960 OSSL_STORE_LOADER_CTX *ossl_store_file_attach_pem_bio_int(BIO *bp)
962 OSSL_STORE_LOADER_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
965 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_FILE_ATTACH_PEM_BIO_INT,
966 ERR_R_MALLOC_FAILURE);
970 ctx->_.file.file = bp;
976 static OSSL_STORE_INFO *file_load_try_decode(OSSL_STORE_LOADER_CTX *ctx,
977 const char *pem_name,
978 const char *pem_header,
979 unsigned char *data, size_t len,
980 const UI_METHOD *ui_method,
981 void *ui_data, int *matchcount)
983 OSSL_STORE_INFO *result = NULL;
984 BUF_MEM *new_mem = NULL;
985 char *new_pem_name = NULL;
991 void *handler_ctx = NULL;
992 const FILE_HANDLER **matching_handlers =
993 OPENSSL_zalloc(sizeof(*matching_handlers)
994 * OSSL_NELEM(file_handlers));
996 if (matching_handlers == NULL) {
997 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD_TRY_DECODE,
998 ERR_R_MALLOC_FAILURE);
1003 for (i = 0; i < OSSL_NELEM(file_handlers); i++) {
1004 const FILE_HANDLER *handler = file_handlers[i];
1005 int try_matchcount = 0;
1006 void *tmp_handler_ctx = NULL;
1007 OSSL_STORE_INFO *tmp_result =
1008 handler->try_decode(pem_name, pem_header, data, len,
1009 &tmp_handler_ctx, &try_matchcount,
1010 ui_method, ui_data);
1012 if (try_matchcount > 0) {
1014 matching_handlers[*matchcount] = handler;
1017 handler->destroy_ctx(&handler_ctx);
1018 handler_ctx = tmp_handler_ctx;
1020 if ((*matchcount += try_matchcount) > 1) {
1021 /* more than one match => ambiguous, kill any result */
1022 OSSL_STORE_INFO_free(result);
1023 OSSL_STORE_INFO_free(tmp_result);
1024 if (handler->destroy_ctx != NULL)
1025 handler->destroy_ctx(&handler_ctx);
1031 result = tmp_result;
1035 if (*matchcount == 1 && matching_handlers[0]->repeatable) {
1036 ctx->_.file.last_handler = matching_handlers[0];
1037 ctx->_.file.last_handler_ctx = handler_ctx;
1040 OPENSSL_free(matching_handlers);
1044 OPENSSL_free(new_pem_name);
1045 BUF_MEM_free(new_mem);
1048 && (t = OSSL_STORE_INFO_get_type(result)) == OSSL_STORE_INFO_EMBEDDED) {
1049 pem_name = new_pem_name =
1050 ossl_store_info_get0_EMBEDDED_pem_name(result);
1051 new_mem = ossl_store_info_get0_EMBEDDED_buffer(result);
1052 data = (unsigned char *)new_mem->data;
1053 len = new_mem->length;
1054 OPENSSL_free(result);
1065 static OSSL_STORE_INFO *file_load_try_repeat(OSSL_STORE_LOADER_CTX *ctx,
1066 const UI_METHOD *ui_method,
1069 OSSL_STORE_INFO *result = NULL;
1070 int try_matchcount = 0;
1072 if (ctx->_.file.last_handler != NULL) {
1074 ctx->_.file.last_handler->try_decode(NULL, NULL, NULL, 0,
1075 &ctx->_.file.last_handler_ctx,
1077 ui_method, ui_data);
1079 if (result == NULL) {
1080 ctx->_.file.last_handler->destroy_ctx(&ctx->_.file.last_handler_ctx);
1081 ctx->_.file.last_handler_ctx = NULL;
1082 ctx->_.file.last_handler = NULL;
1088 static void pem_free_flag(void *pem_data, int secure, size_t num)
1091 OPENSSL_secure_clear_free(pem_data, num);
1093 OPENSSL_free(pem_data);
1095 static int file_read_pem(BIO *bp, char **pem_name, char **pem_header,
1096 unsigned char **data, long *len,
1097 const UI_METHOD *ui_method,
1098 void *ui_data, int secure)
1101 ? PEM_read_bio_ex(bp, pem_name, pem_header, data, len,
1102 PEM_FLAG_SECURE | PEM_FLAG_EAY_COMPATIBLE)
1103 : PEM_read_bio(bp, pem_name, pem_header, data, len);
1109 * 10 is the number of characters in "Proc-Type:", which
1110 * PEM_get_EVP_CIPHER_INFO() requires to be present.
1111 * If the PEM header has less characters than that, it's
1112 * not worth spending cycles on it.
1114 if (strlen(*pem_header) > 10) {
1115 EVP_CIPHER_INFO cipher;
1116 struct pem_pass_data pass_data;
1118 if (!PEM_get_EVP_CIPHER_INFO(*pem_header, &cipher)
1119 || !file_fill_pem_pass_data(&pass_data, "PEM", ui_method, ui_data)
1120 || !PEM_do_header(&cipher, *data, len, file_get_pem_pass,
1128 static int file_read_asn1(BIO *bp, unsigned char **data, long *len)
1130 BUF_MEM *mem = NULL;
1132 if (asn1_d2i_read_bio(bp, &mem) < 0)
1135 *data = (unsigned char *)mem->data;
1136 *len = (long)mem->length;
1142 static int ends_with_dirsep(const char *uri)
1145 uri += strlen(uri) - 1;
1147 if (*uri == ']' || *uri == '>' || *uri == ':')
1149 #elif defined _WIN32
1156 static int file_name_to_uri(OSSL_STORE_LOADER_CTX *ctx, const char *name,
1159 assert(name != NULL);
1160 assert(data != NULL);
1162 const char *pathsep = ends_with_dirsep(ctx->_.dir.uri) ? "" : "/";
1163 long calculated_length = strlen(ctx->_.dir.uri) + strlen(pathsep)
1164 + strlen(name) + 1 /* \0 */;
1166 *data = OPENSSL_zalloc(calculated_length);
1167 if (*data == NULL) {
1168 OSSL_STOREerr(OSSL_STORE_F_FILE_NAME_TO_URI, ERR_R_MALLOC_FAILURE);
1172 OPENSSL_strlcat(*data, ctx->_.dir.uri, calculated_length);
1173 OPENSSL_strlcat(*data, pathsep, calculated_length);
1174 OPENSSL_strlcat(*data, name, calculated_length);
1179 static int file_name_check(OSSL_STORE_LOADER_CTX *ctx, const char *name)
1181 const char *p = NULL;
1183 /* If there are no search criteria, all names are accepted */
1184 if (ctx->_.dir.search_name[0] == '\0')
1187 /* If the expected type isn't supported, no name is accepted */
1188 if (ctx->expected_type != 0
1189 && ctx->expected_type != OSSL_STORE_INFO_CERT
1190 && ctx->expected_type != OSSL_STORE_INFO_CRL)
1194 * First, check the basename
1196 if (strncasecmp(name, ctx->_.dir.search_name,
1197 sizeof(ctx->_.dir.search_name) - 1) != 0
1198 || name[sizeof(ctx->_.dir.search_name) - 1] != '.')
1200 p = &name[sizeof(ctx->_.dir.search_name)];
1203 * Then, if the expected type is a CRL, check that the extension starts
1208 if (ctx->expected_type != 0
1209 && ctx->expected_type != OSSL_STORE_INFO_CRL)
1211 } else if (ctx->expected_type == OSSL_STORE_INFO_CRL) {
1216 * Last, check that the rest of the extension is a decimal number, at
1217 * least one digit long.
1226 * One extra step here, check for a possible generation number.
1229 for (p++; *p != '\0'; p++)
1235 * If we've reached the end of the string at this point, we've successfully
1236 * found a fitting file name.
1241 static int file_eof(OSSL_STORE_LOADER_CTX *ctx);
1242 static int file_error(OSSL_STORE_LOADER_CTX *ctx);
1243 static OSSL_STORE_INFO *file_load(OSSL_STORE_LOADER_CTX *ctx,
1244 const UI_METHOD *ui_method, void *ui_data)
1246 OSSL_STORE_INFO *result = NULL;
1251 if (ctx->type == is_dir) {
1253 char *newname = NULL;
1255 if (ctx->_.dir.last_entry == NULL) {
1256 if (!ctx->_.dir.end_reached) {
1258 assert(ctx->_.dir.last_errno != 0);
1259 errno = ctx->_.dir.last_errno;
1261 openssl_strerror_r(errno, errbuf, sizeof(errbuf));
1262 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD, ERR_R_SYS_LIB);
1263 ERR_add_error_data(1, errbuf);
1268 if (ctx->_.dir.last_entry[0] != '.'
1269 && file_name_check(ctx, ctx->_.dir.last_entry)
1270 && !file_name_to_uri(ctx, ctx->_.dir.last_entry, &newname))
1274 * On the first call (with a NULL context), OPENSSL_DIR_read()
1275 * cares about the second argument. On the following calls, it
1276 * only cares that it isn't NULL. Therefore, we can safely give
1279 ctx->_.dir.last_entry = OPENSSL_DIR_read(&ctx->_.dir.ctx,
1281 ctx->_.dir.last_errno = errno;
1282 if (ctx->_.dir.last_entry == NULL && ctx->_.dir.last_errno == 0)
1283 ctx->_.dir.end_reached = 1;
1286 && (result = OSSL_STORE_INFO_new_NAME(newname)) == NULL) {
1287 OPENSSL_free(newname);
1288 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD, ERR_R_OSSL_STORE_LIB);
1291 } while (result == NULL && !file_eof(ctx));
1293 int matchcount = -1;
1296 result = file_load_try_repeat(ctx, ui_method, ui_data);
1304 char *pem_name = NULL; /* PEM record name */
1305 char *pem_header = NULL; /* PEM record header */
1306 unsigned char *data = NULL; /* DER encoded data */
1307 long len = 0; /* DER encoded data length */
1310 if (ctx->type == is_pem) {
1311 if (!file_read_pem(ctx->_.file.file, &pem_name, &pem_header,
1312 &data, &len, ui_method, ui_data,
1313 (ctx->flags & FILE_FLAG_SECMEM) != 0)) {
1318 if (!file_read_asn1(ctx->_.file.file, &data, &len)) {
1324 result = file_load_try_decode(ctx, pem_name, pem_header, data, len,
1325 ui_method, ui_data, &matchcount);
1331 * If a PEM name matches more than one handler, the handlers are
1334 if (!ossl_assert(pem_name == NULL || matchcount <= 1)) {
1339 if (matchcount > 1) {
1340 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD,
1341 OSSL_STORE_R_AMBIGUOUS_CONTENT_TYPE);
1342 } else if (matchcount == 1) {
1344 * If there are other errors on the stack, they already show
1345 * what the problem is.
1347 if (ERR_peek_error() == 0) {
1348 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD,
1349 OSSL_STORE_R_UNSUPPORTED_CONTENT_TYPE);
1350 if (pem_name != NULL)
1351 ERR_add_error_data(3, "PEM type is '", pem_name, "'");
1358 pem_free_flag(pem_name, (ctx->flags & FILE_FLAG_SECMEM) != 0, 0);
1359 pem_free_flag(pem_header, (ctx->flags & FILE_FLAG_SECMEM) != 0, 0);
1360 pem_free_flag(data, (ctx->flags & FILE_FLAG_SECMEM) != 0, len);
1361 } while (matchcount == 0 && !file_eof(ctx) && !file_error(ctx));
1363 /* We bail out on ambiguity */
1368 && ctx->expected_type != 0
1369 && ctx->expected_type != OSSL_STORE_INFO_get_type(result)) {
1370 OSSL_STORE_INFO_free(result);
1378 static int file_error(OSSL_STORE_LOADER_CTX *ctx)
1380 return ctx->errcnt > 0;
1383 static int file_eof(OSSL_STORE_LOADER_CTX *ctx)
1385 if (ctx->type == is_dir)
1386 return ctx->_.dir.end_reached;
1388 if (ctx->_.file.last_handler != NULL
1389 && !ctx->_.file.last_handler->eof(ctx->_.file.last_handler_ctx))
1391 return BIO_eof(ctx->_.file.file);
1394 static int file_close(OSSL_STORE_LOADER_CTX *ctx)
1396 if (ctx->type == is_dir) {
1397 OPENSSL_DIR_end(&ctx->_.dir.ctx);
1399 BIO_free_all(ctx->_.file.file);
1401 OSSL_STORE_LOADER_CTX_free(ctx);
1405 int ossl_store_file_detach_pem_bio_int(OSSL_STORE_LOADER_CTX *ctx)
1407 OSSL_STORE_LOADER_CTX_free(ctx);
1411 static OSSL_STORE_LOADER file_loader =
1425 static void store_file_loader_deinit(void)
1427 ossl_store_unregister_loader_int(file_loader.scheme);
1430 int ossl_store_file_loader_init(void)
1432 int ret = ossl_store_register_loader_int(&file_loader);
1434 OPENSSL_atexit(store_file_loader_deinit);