2 * Copyright 2016-2017 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
14 #include <openssl/bio.h>
15 #include <openssl/dsa.h> /* For d2i_DSAPrivateKey */
16 #include <openssl/err.h>
17 #include <openssl/evp.h>
18 #include <openssl/pem.h>
19 #include <openssl/pkcs12.h> /* For the PKCS8 stuff o.O */
20 #include <openssl/rsa.h> /* For d2i_RSAPrivateKey */
21 #include <openssl/safestack.h>
22 #include <openssl/store.h>
23 #include <openssl/ui.h>
24 #include <openssl/x509.h> /* For the PKCS8 stuff o.O */
25 #include "internal/asn1_int.h"
26 #include "internal/o_dir.h"
27 #include "internal/cryptlib.h"
28 #include "internal/store_int.h"
29 #include "store_locl.h"
37 static char *file_get_pass(const UI_METHOD *ui_method, char *pass,
38 size_t maxsize, const char *prompt_info, void *data)
44 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS, ERR_R_MALLOC_FAILURE);
48 if (ui_method != NULL)
49 UI_set_method(ui, ui_method);
50 UI_add_user_data(ui, data);
52 if ((prompt = UI_construct_prompt(ui, "pass phrase",
53 prompt_info)) == NULL) {
54 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS, ERR_R_MALLOC_FAILURE);
56 } else if (!UI_add_input_string(ui, prompt, UI_INPUT_FLAG_DEFAULT_PWD,
57 pass, 0, maxsize - 1)) {
58 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS, ERR_R_UI_LIB);
61 switch (UI_process(ui)) {
63 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS,
64 OSSL_STORE_R_UI_PROCESS_INTERRUPTED_OR_CANCELLED);
68 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS, ERR_R_UI_LIB);
81 struct pem_pass_data {
82 const UI_METHOD *ui_method;
84 const char *prompt_info;
86 static int file_fill_pem_pass_data(struct pem_pass_data *pass_data,
87 const char *prompt_info,
88 const UI_METHOD *ui_method, void *ui_data)
90 if (pass_data == NULL)
92 pass_data->ui_method = ui_method;
93 pass_data->data = ui_data;
94 pass_data->prompt_info = prompt_info;
97 static int file_get_pem_pass(char *buf, int num, int w, void *data)
99 struct pem_pass_data *pass_data = data;
100 char *pass = file_get_pass(pass_data->ui_method, buf, num,
101 pass_data->prompt_info, pass_data->data);
103 return pass == NULL ? 0 : strlen(pass);
107 * The file scheme handlers
111 * The try_decode function is called to check if the blob of data can
112 * be used by this handler, and if it can, decodes it into a supported
113 * OpenSSL type and returns a OSSL_STORE_INFO with the decoded data.
115 * pem_name: If this blob comes from a PEM file, this holds
116 * the PEM name. If it comes from another type of
117 * file, this is NULL.
118 * pem_header: If this blob comes from a PEM file, this holds
119 * the PEM headers. If it comes from another type of
120 * file, this is NULL.
121 * blob: The blob of data to match with what this handler
123 * len: The length of the blob.
124 * handler_ctx: For a handler marked repeatable, this pointer can
125 * be used to create a context for the handler. IT IS
126 * THE HANDLER'S RESPONSIBILITY TO CREATE AND DESTROY
127 * THIS CONTEXT APPROPRIATELY, i.e. create on first call
128 * and destroy when about to return NULL.
129 * matchcount: A pointer to an int to count matches for this data.
130 * Usually becomes 0 (no match) or 1 (match!), but may
131 * be higher in the (unlikely) event that the data matches
132 * more than one possibility. The int will always be
133 * zero when the function is called.
134 * ui_method: Application UI method for getting a password, pin
135 * or any other interactive data.
136 * ui_data: Application data to be passed to ui_method when
141 typedef OSSL_STORE_INFO *(*file_try_decode_fn)(const char *pem_name,
142 const char *pem_header,
143 const unsigned char *blob,
144 size_t len, void **handler_ctx,
146 const UI_METHOD *ui_method,
149 * The eof function should return 1 if there's no more data to be found
150 * with the handler_ctx, otherwise 0. This is only used when the handler is
153 typedef int (*file_eof_fn)(void *handler_ctx);
155 * The destroy_ctx function is used to destroy the handler_ctx that was
156 * intiated by a repeatable try_decode fuction. This is only used when
157 * the handler is marked repeatable.
159 typedef void (*file_destroy_ctx_fn)(void **handler_ctx);
161 typedef struct file_handler_st {
163 file_try_decode_fn try_decode;
165 file_destroy_ctx_fn destroy_ctx;
171 static OSSL_STORE_INFO *try_decode_PKCS12(const char *pem_name,
172 const char *pem_header,
173 const unsigned char *blob,
174 size_t len, void **pctx,
176 const UI_METHOD *ui_method,
179 OSSL_STORE_INFO *store_info = NULL;
180 STACK_OF(OSSL_STORE_INFO) *ctx = *pctx;
183 /* Initial parsing */
187 if (pem_name != NULL)
188 /* No match, there is no PEM PKCS12 tag */
191 if ((p12 = d2i_PKCS12(NULL, &blob, len)) != NULL) {
193 char tpass[PEM_BUFSIZE];
194 EVP_PKEY *pkey = NULL;
196 STACK_OF(X509) *chain = NULL;
200 if (PKCS12_verify_mac(p12, "", 0)
201 || PKCS12_verify_mac(p12, NULL, 0)) {
204 if ((pass = file_get_pass(ui_method, tpass, PEM_BUFSIZE,
205 "PKCS12 import password",
207 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS12,
208 OSSL_STORE_R_PASSPHRASE_CALLBACK_ERROR);
211 if (!PKCS12_verify_mac(p12, pass, strlen(pass))) {
212 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS12,
213 OSSL_STORE_R_ERROR_VERIFYING_PKCS12_MAC);
218 if (PKCS12_parse(p12, pass, &pkey, &cert, &chain)) {
219 OSSL_STORE_INFO *si_pkey = NULL;
220 OSSL_STORE_INFO *si_cert = NULL;
221 OSSL_STORE_INFO *si_ca = NULL;
223 if ((ctx = sk_OSSL_STORE_INFO_new_null()) != NULL
224 && (si_pkey = OSSL_STORE_INFO_new_PKEY(pkey)) != NULL
225 && sk_OSSL_STORE_INFO_push(ctx, si_pkey) != 0
226 && (si_cert = OSSL_STORE_INFO_new_CERT(cert)) != NULL
227 && sk_OSSL_STORE_INFO_push(ctx, si_cert) != 0) {
232 while(sk_X509_num(chain) > 0) {
233 X509 *ca = sk_X509_value(chain, 0);
235 if ((si_ca = OSSL_STORE_INFO_new_CERT(ca)) == NULL
236 || sk_OSSL_STORE_INFO_push(ctx, si_ca) == 0) {
241 (void)sk_X509_shift(chain);
245 OSSL_STORE_INFO_free(si_ca);
246 OSSL_STORE_INFO_free(si_cert);
247 OSSL_STORE_INFO_free(si_pkey);
248 sk_OSSL_STORE_INFO_pop_free(ctx, OSSL_STORE_INFO_free);
251 sk_X509_pop_free(chain, X509_free);
265 store_info = sk_OSSL_STORE_INFO_shift(ctx);
270 static int eof_PKCS12(void *ctx_)
272 STACK_OF(OSSL_STORE_INFO) *ctx = ctx_;
274 return ctx == NULL || sk_OSSL_STORE_INFO_num(ctx) == 0;
276 static void destroy_ctx_PKCS12(void **pctx)
278 STACK_OF(OSSL_STORE_INFO) *ctx = *pctx;
280 sk_OSSL_STORE_INFO_pop_free(ctx, OSSL_STORE_INFO_free);
283 static FILE_HANDLER PKCS12_handler = {
291 static OSSL_STORE_INFO *try_decode_PKCS8Encrypted(const char *pem_name,
292 const char *pem_header,
293 const unsigned char *blob,
294 size_t len, void **pctx,
296 const UI_METHOD *ui_method,
300 char kbuf[PEM_BUFSIZE];
302 const X509_ALGOR *dalg = NULL;
303 const ASN1_OCTET_STRING *doct = NULL;
304 OSSL_STORE_INFO *store_info = NULL;
306 unsigned char *new_data = NULL;
309 if (pem_name != NULL) {
310 if (strcmp(pem_name, PEM_STRING_PKCS8) != 0)
315 if ((p8 = d2i_X509_SIG(NULL, &blob, len)) == NULL)
320 if ((mem = BUF_MEM_new()) == NULL) {
321 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS8ENCRYPTED,
322 ERR_R_MALLOC_FAILURE);
326 if ((pass = file_get_pass(ui_method, kbuf, PEM_BUFSIZE,
327 "PKCS8 decrypt password", ui_data)) == NULL) {
328 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS8ENCRYPTED,
329 OSSL_STORE_R_BAD_PASSWORD_READ);
333 X509_SIG_get0(p8, &dalg, &doct);
334 if (!PKCS12_pbe_crypt(dalg, pass, strlen(pass), doct->data, doct->length,
335 &new_data, &new_data_len, 0))
338 mem->data = (char *)new_data;
339 mem->max = mem->length = (size_t)new_data_len;
342 store_info = ossl_store_info_new_EMBEDDED(PEM_STRING_PKCS8INF, mem);
343 if (store_info == NULL) {
344 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS8ENCRYPTED,
345 ERR_R_MALLOC_FAILURE);
355 static FILE_HANDLER PKCS8Encrypted_handler = {
357 try_decode_PKCS8Encrypted
360 int pem_check_suffix(const char *pem_str, const char *suffix);
361 static OSSL_STORE_INFO *try_decode_PrivateKey(const char *pem_name,
362 const char *pem_header,
363 const unsigned char *blob,
364 size_t len, void **pctx,
366 const UI_METHOD *ui_method,
369 OSSL_STORE_INFO *store_info = NULL;
370 EVP_PKEY *pkey = NULL;
371 const EVP_PKEY_ASN1_METHOD *ameth = NULL;
373 if (pem_name != NULL) {
374 if (strcmp(pem_name, PEM_STRING_PKCS8INF) == 0) {
375 PKCS8_PRIV_KEY_INFO *p8inf =
376 d2i_PKCS8_PRIV_KEY_INFO(NULL, &blob, len);
380 pkey = EVP_PKCS82PKEY(p8inf);
381 PKCS8_PRIV_KEY_INFO_free(p8inf);
385 if ((slen = pem_check_suffix(pem_name, "PRIVATE KEY")) > 0
386 && (ameth = EVP_PKEY_asn1_find_str(NULL, pem_name,
389 pkey = d2i_PrivateKey(ameth->pkey_id, NULL, &blob, len);
395 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
396 EVP_PKEY *tmp_pkey = NULL;
397 const unsigned char *tmp_blob = blob;
399 ameth = EVP_PKEY_asn1_get0(i);
400 if (ameth->pkey_flags & ASN1_PKEY_ALIAS)
403 tmp_pkey = d2i_PrivateKey(ameth->pkey_id, NULL, &tmp_blob, len);
404 if (tmp_pkey != NULL) {
406 EVP_PKEY_free(tmp_pkey);
413 if (*matchcount > 1) {
422 store_info = OSSL_STORE_INFO_new_PKEY(pkey);
423 if (store_info == NULL)
428 static FILE_HANDLER PrivateKey_handler = {
430 try_decode_PrivateKey
433 static OSSL_STORE_INFO *try_decode_PUBKEY(const char *pem_name,
434 const char *pem_header,
435 const unsigned char *blob,
436 size_t len, void **pctx,
438 const UI_METHOD *ui_method,
441 OSSL_STORE_INFO *store_info = NULL;
442 EVP_PKEY *pkey = NULL;
444 if (pem_name != NULL) {
445 if (strcmp(pem_name, PEM_STRING_PUBLIC) != 0)
451 if ((pkey = d2i_PUBKEY(NULL, &blob, len)) != NULL) {
453 store_info = OSSL_STORE_INFO_new_PKEY(pkey);
458 static FILE_HANDLER PUBKEY_handler = {
463 static OSSL_STORE_INFO *try_decode_params(const char *pem_name,
464 const char *pem_header,
465 const unsigned char *blob,
466 size_t len, void **pctx,
468 const UI_METHOD *ui_method,
471 OSSL_STORE_INFO *store_info = NULL;
473 EVP_PKEY *pkey = NULL;
474 const EVP_PKEY_ASN1_METHOD *ameth = NULL;
477 if (pem_name != NULL) {
478 if ((slen = pem_check_suffix(pem_name, "PARAMETERS")) == 0)
484 if ((pkey = EVP_PKEY_new()) == NULL) {
485 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PARAMS, ERR_R_EVP_LIB);
490 if (EVP_PKEY_set_type_str(pkey, pem_name, slen)
491 && (ameth = EVP_PKEY_get0_asn1(pkey)) != NULL
492 && ameth->param_decode != NULL
493 && ameth->param_decode(pkey, &blob, len))
497 EVP_PKEY *tmp_pkey = NULL;
499 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
500 const unsigned char *tmp_blob = blob;
502 if (tmp_pkey == NULL && (tmp_pkey = EVP_PKEY_new()) == NULL) {
503 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PARAMS, ERR_R_EVP_LIB);
507 ameth = EVP_PKEY_asn1_get0(i);
508 if (ameth->pkey_flags & ASN1_PKEY_ALIAS)
511 if (EVP_PKEY_set_type(tmp_pkey, ameth->pkey_id)
512 && (ameth = EVP_PKEY_get0_asn1(tmp_pkey)) != NULL
513 && ameth->param_decode != NULL
514 && ameth->param_decode(tmp_pkey, &tmp_blob, len)) {
516 EVP_PKEY_free(tmp_pkey);
524 EVP_PKEY_free(tmp_pkey);
525 if (*matchcount == 1) {
531 store_info = OSSL_STORE_INFO_new_PARAMS(pkey);
532 if (store_info == NULL)
537 static FILE_HANDLER params_handler = {
542 static OSSL_STORE_INFO *try_decode_X509Certificate(const char *pem_name,
543 const char *pem_header,
544 const unsigned char *blob,
545 size_t len, void **pctx,
547 const UI_METHOD *ui_method,
550 OSSL_STORE_INFO *store_info = NULL;
554 * In most cases, we can try to interpret the serialized data as a trusted
555 * cert (X509 + X509_AUX) and fall back to reading it as a normal cert
556 * (just X509), but if the PEM name specifically declares it as a trusted
557 * cert, then no fallback should be engaged. |ignore_trusted| tells if
558 * the fallback can be used (1) or not (0).
560 int ignore_trusted = 1;
562 if (pem_name != NULL) {
563 if (strcmp(pem_name, PEM_STRING_X509_TRUSTED) == 0)
565 else if (strcmp(pem_name, PEM_STRING_X509_OLD) != 0
566 && strcmp(pem_name, PEM_STRING_X509) != 0)
572 if ((cert = d2i_X509_AUX(NULL, &blob, len)) != NULL
573 || (ignore_trusted && (cert = d2i_X509(NULL, &blob, len)) != NULL)) {
575 store_info = OSSL_STORE_INFO_new_CERT(cert);
578 if (store_info == NULL)
583 static FILE_HANDLER X509Certificate_handler = {
585 try_decode_X509Certificate
588 static OSSL_STORE_INFO *try_decode_X509CRL(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;
597 X509_CRL *crl = NULL;
599 if (pem_name != NULL) {
600 if (strcmp(pem_name, PEM_STRING_X509_CRL) != 0)
606 if ((crl = d2i_X509_CRL(NULL, &blob, len)) != NULL) {
608 store_info = OSSL_STORE_INFO_new_CRL(crl);
611 if (store_info == NULL)
616 static FILE_HANDLER X509CRL_handler = {
621 static const FILE_HANDLER *file_handlers[] = {
623 &PKCS8Encrypted_handler,
624 &X509Certificate_handler,
636 struct ossl_store_loader_ctx_st {
643 #define FILE_FLAG_SECMEM (1<<0)
646 struct { /* Used with is_raw and is_pem */
650 * The following are used when the handler is marked as
653 const FILE_HANDLER *last_handler;
654 void *last_handler_ctx;
656 struct { /* Used with is_dir */
657 OPENSSL_DIR_CTX *ctx;
662 * The directory reading utility we have combines opening with
663 * reading the first name. To make sure we can detect the end
664 * at the right time, we read early and cache the name.
666 const char *last_entry;
672 static void OSSL_STORE_LOADER_CTX_free(OSSL_STORE_LOADER_CTX *ctx)
674 if (ctx->type == is_dir) {
675 OPENSSL_free(ctx->_.dir.uri);
677 if (ctx->_.file.last_handler != NULL) {
678 ctx->_.file.last_handler->destroy_ctx(&ctx->_.file.last_handler_ctx);
679 ctx->_.file.last_handler_ctx = NULL;
680 ctx->_.file.last_handler = NULL;
686 static OSSL_STORE_LOADER_CTX *file_open(const OSSL_STORE_LOADER *loader,
688 const UI_METHOD *ui_method,
691 OSSL_STORE_LOADER_CTX *ctx = NULL;
693 const char *path = NULL;
695 if (strncasecmp(uri, "file:", 5) == 0) {
696 if (strncasecmp(&uri[5], "//localhost/", 12) == 0) {
698 } else if (strncmp(&uri[5], "///", 3) == 0) {
700 } else if (strncmp(&uri[5], "//", 2) != 0) {
703 OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN,
704 OSSL_STORE_R_URI_AUTHORITY_UNSUPPORED);
709 * If the scheme "file" was an explicit part of the URI, the path must
710 * be absolute. So says RFC 8089
712 if (path[0] != '/') {
713 OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN,
714 OSSL_STORE_R_PATH_MUST_BE_ABSOLUTE);
719 /* Windows file: URIs with a drive letter start with a / */
720 if (path[0] == '/' && path[2] == ':' && path[3] == '/')
728 if (stat(path, &st) < 0) {
729 SYSerr(SYS_F_STAT, errno);
730 ERR_add_error_data(1, path);
734 ctx = OPENSSL_zalloc(sizeof(*ctx));
736 OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN, ERR_R_MALLOC_FAILURE);
740 if ((st.st_mode & S_IFDIR) == S_IFDIR) {
742 * Try to copy everything, even if we know that some of them must be
743 * NULL for the moment. This prevents errors in the future, when more
744 * components may be used.
746 ctx->_.dir.uri = OPENSSL_strdup(uri);
749 if (ctx->_.dir.uri == NULL)
752 ctx->_.dir.last_entry = OPENSSL_DIR_read(&ctx->_.dir.ctx, path);
753 ctx->_.dir.last_errno = errno;
754 if (ctx->_.dir.last_entry == NULL) {
755 if (ctx->_.dir.last_errno != 0) {
757 errno = ctx->_.dir.last_errno;
758 openssl_strerror_r(errno, errbuf, sizeof(errbuf));
759 OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN, ERR_R_SYS_LIB);
760 ERR_add_error_data(1, errbuf);
763 ctx->_.dir.end_reached = 1;
769 if ((buff = BIO_new(BIO_f_buffer())) == NULL
770 || (ctx->_.file.file = BIO_new_file(path, "rb")) == NULL) {
775 ctx->_.file.file = BIO_push(buff, ctx->_.file.file);
776 if (BIO_buffer_peek(ctx->_.file.file, peekbuf, sizeof(peekbuf)-1) > 0) {
777 peekbuf[sizeof(peekbuf)-1] = '\0';
778 if (strstr(peekbuf, "-----BEGIN ") != NULL)
785 OSSL_STORE_LOADER_CTX_free(ctx);
789 static int file_ctrl(OSSL_STORE_LOADER_CTX *ctx, int cmd, va_list args)
794 case OSSL_STORE_C_USE_SECMEM:
796 int on = *(va_arg(args, int *));
800 ctx->flags &= ~FILE_FLAG_SECMEM;
803 ctx->flags |= FILE_FLAG_SECMEM;
806 OSSL_STOREerr(OSSL_STORE_F_FILE_CTRL,
807 ERR_R_PASSED_INVALID_ARGUMENT);
820 /* Internal function to decode an already opened PEM file */
821 OSSL_STORE_LOADER_CTX *ossl_store_file_attach_pem_bio_int(BIO *bp)
823 OSSL_STORE_LOADER_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
826 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_FILE_ATTACH_PEM_BIO_INT,
827 ERR_R_MALLOC_FAILURE);
831 ctx->_.file.file = bp;
837 static OSSL_STORE_INFO *file_load_try_decode(OSSL_STORE_LOADER_CTX *ctx,
838 const char *pem_name,
839 const char *pem_header,
840 unsigned char *data, size_t len,
841 const UI_METHOD *ui_method,
842 void *ui_data, int *matchcount)
844 OSSL_STORE_INFO *result = NULL;
845 BUF_MEM *new_mem = NULL;
846 char *new_pem_name = NULL;
852 void *handler_ctx = NULL;
853 const FILE_HANDLER **matching_handlers =
854 OPENSSL_zalloc(sizeof(*matching_handlers)
855 * OSSL_NELEM(file_handlers));
857 if (matching_handlers == NULL) {
858 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD_TRY_DECODE,
859 ERR_R_MALLOC_FAILURE);
864 for (i = 0; i < OSSL_NELEM(file_handlers); i++) {
865 const FILE_HANDLER *handler = file_handlers[i];
866 int try_matchcount = 0;
867 void *tmp_handler_ctx = NULL;
868 OSSL_STORE_INFO *tmp_result =
869 handler->try_decode(pem_name, pem_header, data, len,
870 &tmp_handler_ctx, &try_matchcount,
873 if (try_matchcount > 0) {
874 if (matching_handlers)
875 matching_handlers[*matchcount] = handler;
878 handler->destroy_ctx(&handler_ctx);
879 handler_ctx = tmp_handler_ctx;
881 if ((*matchcount += try_matchcount) > 1) {
882 /* more than one match => ambiguous, kill any result */
883 OSSL_STORE_INFO_free(result);
884 OSSL_STORE_INFO_free(tmp_result);
885 if (handler->destroy_ctx != NULL)
886 handler->destroy_ctx(&handler_ctx);
896 if (*matchcount == 1 && matching_handlers[0]->repeatable) {
897 ctx->_.file.last_handler = matching_handlers[0];
898 ctx->_.file.last_handler_ctx = handler_ctx;
901 OPENSSL_free(matching_handlers);
905 OPENSSL_free(new_pem_name);
906 BUF_MEM_free(new_mem);
909 && (t = OSSL_STORE_INFO_get_type(result)) == OSSL_STORE_INFO_EMBEDDED) {
910 pem_name = new_pem_name =
911 ossl_store_info_get0_EMBEDDED_pem_name(result);
912 new_mem = ossl_store_info_get0_EMBEDDED_buffer(result);
913 data = (unsigned char *)new_mem->data;
914 len = new_mem->length;
915 OPENSSL_free(result);
926 static OSSL_STORE_INFO *file_load_try_repeat(OSSL_STORE_LOADER_CTX *ctx,
927 const UI_METHOD *ui_method,
930 OSSL_STORE_INFO *result = NULL;
931 int try_matchcount = 0;
933 if (ctx->_.file.last_handler != NULL) {
935 ctx->_.file.last_handler->try_decode(NULL, NULL, NULL, 0,
936 &ctx->_.file.last_handler_ctx,
940 if (result == NULL) {
941 ctx->_.file.last_handler->destroy_ctx(&ctx->_.file.last_handler_ctx);
942 ctx->_.file.last_handler_ctx = NULL;
943 ctx->_.file.last_handler = NULL;
949 static void pem_free_flag(void *pem_data, int secure)
952 OPENSSL_secure_free(pem_data);
954 OPENSSL_free(pem_data);
956 static int file_read_pem(BIO *bp, char **pem_name, char **pem_header,
957 unsigned char **data, long *len,
958 const UI_METHOD *ui_method,
959 void *ui_data, int secure)
962 ? PEM_read_bio_ex(bp, pem_name, pem_header, data, len,
963 PEM_FLAG_SECURE | PEM_FLAG_EAY_COMPATIBLE)
964 : PEM_read_bio(bp, pem_name, pem_header, data, len);
970 * 10 is the number of characters in "Proc-Type:", which
971 * PEM_get_EVP_CIPHER_INFO() requires to be present.
972 * If the PEM header has less characters than that, it's
973 * not worth spending cycles on it.
975 if (strlen(*pem_header) > 10) {
976 EVP_CIPHER_INFO cipher;
977 struct pem_pass_data pass_data;
979 if (!PEM_get_EVP_CIPHER_INFO(*pem_header, &cipher)
980 || !file_fill_pem_pass_data(&pass_data, "PEM", ui_method, ui_data)
981 || !PEM_do_header(&cipher, *data, len, file_get_pem_pass,
989 static int file_read_asn1(BIO *bp, unsigned char **data, long *len)
993 if (asn1_d2i_read_bio(bp, &mem) < 0)
996 *data = (unsigned char *)mem->data;
997 *len = (long)mem->length;
1003 static int ends_with_dirsep(const char *uri)
1006 uri += strlen(uri) - 1;
1008 if (*uri == ']' || *uri == '>' || *uri == ':')
1010 #elif defined _WIN32
1017 static int file_name_to_uri(OSSL_STORE_LOADER_CTX *ctx, const char *name,
1020 assert(name != NULL);
1021 assert(data != NULL);
1023 const char *pathsep = ends_with_dirsep(ctx->_.dir.uri) ? "" : "/";
1024 long calculated_length = strlen(ctx->_.dir.uri) + strlen(pathsep)
1025 + strlen(name) + 1 /* \0 */;
1027 *data = OPENSSL_zalloc(calculated_length);
1028 if (*data == NULL) {
1029 OSSL_STOREerr(OSSL_STORE_F_FILE_NAME_TO_URI, ERR_R_MALLOC_FAILURE);
1033 OPENSSL_strlcat(*data, ctx->_.dir.uri, calculated_length);
1034 OPENSSL_strlcat(*data, pathsep, calculated_length);
1035 OPENSSL_strlcat(*data, name, calculated_length);
1040 static int file_eof(OSSL_STORE_LOADER_CTX *ctx);
1041 static int file_error(OSSL_STORE_LOADER_CTX *ctx);
1042 static OSSL_STORE_INFO *file_load(OSSL_STORE_LOADER_CTX *ctx,
1043 const UI_METHOD *ui_method, void *ui_data)
1045 OSSL_STORE_INFO *result = NULL;
1050 if (ctx->type == is_dir) {
1052 char *newname = NULL;
1054 if (ctx->_.dir.last_entry == NULL) {
1055 if (!ctx->_.dir.end_reached) {
1057 assert(ctx->_.dir.last_errno != 0);
1058 errno = ctx->_.dir.last_errno;
1060 openssl_strerror_r(errno, errbuf, sizeof(errbuf));
1061 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD, ERR_R_SYS_LIB);
1062 ERR_add_error_data(1, errbuf);
1067 if (ctx->_.dir.last_entry[0] != '.'
1068 && !file_name_to_uri(ctx, ctx->_.dir.last_entry, &newname))
1072 * On the first call (with a NULL context), OPENSSL_DIR_read()
1073 * cares about the second argument. On the following calls, it
1074 * only cares that it isn't NULL. Therefore, we can safely give
1077 ctx->_.dir.last_entry = OPENSSL_DIR_read(&ctx->_.dir.ctx,
1079 ctx->_.dir.last_errno = errno;
1080 if (ctx->_.dir.last_entry == NULL && ctx->_.dir.last_errno == 0)
1081 ctx->_.dir.end_reached = 1;
1084 && (result = OSSL_STORE_INFO_new_NAME(newname)) == NULL) {
1085 OPENSSL_free(newname);
1086 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD, ERR_R_OSSL_STORE_LIB);
1089 } while (result == NULL && !file_eof(ctx));
1091 int matchcount = -1;
1093 result = file_load_try_repeat(ctx, ui_method, ui_data);
1101 char *pem_name = NULL; /* PEM record name */
1102 char *pem_header = NULL; /* PEM record header */
1103 unsigned char *data = NULL; /* DER encoded data */
1104 long len = 0; /* DER encoded data length */
1107 if (ctx->type == is_pem) {
1108 if (!file_read_pem(ctx->_.file.file, &pem_name, &pem_header,
1109 &data, &len, ui_method, ui_data,
1110 (ctx->flags & FILE_FLAG_SECMEM) != 0)) {
1115 if (!file_read_asn1(ctx->_.file.file, &data, &len)) {
1121 result = file_load_try_decode(ctx, pem_name, pem_header, data, len,
1122 ui_method, ui_data, &matchcount);
1128 * If a PEM name matches more than one handler, the handlers are
1131 if (!ossl_assert(pem_name == NULL || matchcount <= 1)) {
1136 if (matchcount > 1) {
1137 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD,
1138 OSSL_STORE_R_AMBIGUOUS_CONTENT_TYPE);
1139 } else if (matchcount == 1) {
1141 * If there are other errors on the stack, they already show
1142 * what the problem is.
1144 if (ERR_peek_error() == 0) {
1145 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD,
1146 OSSL_STORE_R_UNSUPPORTED_CONTENT_TYPE);
1147 if (pem_name != NULL)
1148 ERR_add_error_data(3, "PEM type is '", pem_name, "'");
1155 pem_free_flag(pem_name, (ctx->flags & FILE_FLAG_SECMEM) != 0);
1156 pem_free_flag(pem_header, (ctx->flags & FILE_FLAG_SECMEM) != 0);
1157 pem_free_flag(data, (ctx->flags & FILE_FLAG_SECMEM) != 0);
1158 } while (matchcount == 0 && !file_eof(ctx) && !file_error(ctx));
1160 /* We bail out on ambiguity */
1168 static int file_error(OSSL_STORE_LOADER_CTX *ctx)
1170 return ctx->errcnt > 0;
1173 static int file_eof(OSSL_STORE_LOADER_CTX *ctx)
1175 if (ctx->type == is_dir)
1176 return ctx->_.dir.end_reached;
1178 if (ctx->_.file.last_handler != NULL
1179 && !ctx->_.file.last_handler->eof(ctx->_.file.last_handler_ctx))
1181 return BIO_eof(ctx->_.file.file);
1184 static int file_close(OSSL_STORE_LOADER_CTX *ctx)
1186 if (ctx->type == is_dir) {
1187 OPENSSL_DIR_end(&ctx->_.dir.ctx);
1189 BIO_free_all(ctx->_.file.file);
1191 OSSL_STORE_LOADER_CTX_free(ctx);
1195 int ossl_store_file_detach_pem_bio_int(OSSL_STORE_LOADER_CTX *ctx)
1197 OSSL_STORE_LOADER_CTX_free(ctx);
1201 static OSSL_STORE_LOADER file_loader =
1213 static void store_file_loader_deinit(void)
1215 ossl_store_unregister_loader_int(file_loader.scheme);
1218 int ossl_store_file_loader_init(void)
1220 int ret = ossl_store_register_loader_int(&file_loader);
1222 OPENSSL_atexit(store_file_loader_deinit);