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)
483 if ((pkey = EVP_PKEY_new()) == NULL) {
484 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PARAMS, ERR_R_EVP_LIB);
489 if (EVP_PKEY_set_type_str(pkey, pem_name, slen)
490 && (ameth = EVP_PKEY_get0_asn1(pkey)) != NULL
491 && ameth->param_decode != NULL
492 && ameth->param_decode(pkey, &blob, len))
497 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
498 const unsigned char *tmp_blob = blob;
500 ameth = EVP_PKEY_asn1_get0(i);
501 if (ameth->pkey_flags & ASN1_PKEY_ALIAS)
503 if (EVP_PKEY_set_type(pkey, ameth->pkey_id)
504 && (ameth = EVP_PKEY_get0_asn1(pkey)) != NULL
505 && ameth->param_decode != NULL
506 && ameth->param_decode(pkey, &tmp_blob, len)) {
515 store_info = OSSL_STORE_INFO_new_PARAMS(pkey);
516 if (store_info == NULL)
521 static FILE_HANDLER params_handler = {
526 static OSSL_STORE_INFO *try_decode_X509Certificate(const char *pem_name,
527 const char *pem_header,
528 const unsigned char *blob,
529 size_t len, void **pctx,
531 const UI_METHOD *ui_method,
534 OSSL_STORE_INFO *store_info = NULL;
538 * In most cases, we can try to interpret the serialized data as a trusted
539 * cert (X509 + X509_AUX) and fall back to reading it as a normal cert
540 * (just X509), but if the PEM name specifically declares it as a trusted
541 * cert, then no fallback should be engaged. |ignore_trusted| tells if
542 * the fallback can be used (1) or not (0).
544 int ignore_trusted = 1;
546 if (pem_name != NULL) {
547 if (strcmp(pem_name, PEM_STRING_X509_TRUSTED) == 0)
549 else if (strcmp(pem_name, PEM_STRING_X509_OLD) != 0
550 && strcmp(pem_name, PEM_STRING_X509) != 0)
556 if ((cert = d2i_X509_AUX(NULL, &blob, len)) != NULL
557 || (ignore_trusted && (cert = d2i_X509(NULL, &blob, len)) != NULL)) {
559 store_info = OSSL_STORE_INFO_new_CERT(cert);
562 if (store_info == NULL)
567 static FILE_HANDLER X509Certificate_handler = {
569 try_decode_X509Certificate
572 static OSSL_STORE_INFO *try_decode_X509CRL(const char *pem_name,
573 const char *pem_header,
574 const unsigned char *blob,
575 size_t len, void **pctx,
577 const UI_METHOD *ui_method,
580 OSSL_STORE_INFO *store_info = NULL;
581 X509_CRL *crl = NULL;
583 if (pem_name != NULL) {
584 if (strcmp(pem_name, PEM_STRING_X509_CRL) != 0)
590 if ((crl = d2i_X509_CRL(NULL, &blob, len)) != NULL) {
592 store_info = OSSL_STORE_INFO_new_CRL(crl);
595 if (store_info == NULL)
600 static FILE_HANDLER X509CRL_handler = {
605 static const FILE_HANDLER *file_handlers[] = {
607 &PKCS8Encrypted_handler,
608 &X509Certificate_handler,
620 struct ossl_store_loader_ctx_st {
627 #define FILE_FLAG_SECMEM (1<<0)
630 struct { /* Used with is_raw and is_pem */
634 * The following are used when the handler is marked as
637 const FILE_HANDLER *last_handler;
638 void *last_handler_ctx;
640 struct { /* Used with is_dir */
641 OPENSSL_DIR_CTX *ctx;
646 * The directory reading utility we have combines opening with
647 * reading the first name. To make sure we can detect the end
648 * at the right time, we read early and cache the name.
650 const char *last_entry;
656 static void OSSL_STORE_LOADER_CTX_free(OSSL_STORE_LOADER_CTX *ctx)
658 if (ctx->type == is_dir) {
659 OPENSSL_free(ctx->_.dir.uri);
661 if (ctx->_.file.last_handler != NULL) {
662 ctx->_.file.last_handler->destroy_ctx(&ctx->_.file.last_handler_ctx);
663 ctx->_.file.last_handler_ctx = NULL;
664 ctx->_.file.last_handler = NULL;
670 static OSSL_STORE_LOADER_CTX *file_open(const OSSL_STORE_LOADER *loader,
672 const UI_METHOD *ui_method,
675 OSSL_STORE_LOADER_CTX *ctx = NULL;
677 const char *path = NULL;
679 if (strncasecmp(uri, "file:", 5) == 0) {
680 if (strncmp(&uri[5], "//localhost/", 12) == 0) {
682 } else if (strncmp(&uri[5], "///", 3) == 0) {
684 } else if (strncmp(&uri[5], "//", 2) != 0) {
687 OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN,
688 OSSL_STORE_R_URI_AUTHORITY_UNSUPPORED);
693 * If the scheme "file" was an explicit part of the URI, the path must
694 * be absolute. So says RFC 8089
696 if (path[0] != '/') {
697 OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN,
698 OSSL_STORE_R_PATH_MUST_BE_ABSOLUTE);
703 /* Windows file: URIs with a drive letter start with a / */
704 if (path[0] == '/' && path[2] == ':' && path[3] == '/')
712 if (stat(path, &st) < 0) {
713 SYSerr(SYS_F_STAT, errno);
714 ERR_add_error_data(1, path);
718 ctx = OPENSSL_zalloc(sizeof(*ctx));
720 OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN, ERR_R_MALLOC_FAILURE);
724 if ((st.st_mode & S_IFDIR) == S_IFDIR) {
726 * Try to copy everything, even if we know that some of them must be
727 * NULL for the moment. This prevents errors in the future, when more
728 * components may be used.
730 ctx->_.dir.uri = OPENSSL_strdup(uri);
733 if (ctx->_.dir.uri == NULL)
736 ctx->_.dir.last_entry = OPENSSL_DIR_read(&ctx->_.dir.ctx, path);
737 ctx->_.dir.last_errno = errno;
738 if (ctx->_.dir.last_entry == NULL) {
739 if (ctx->_.dir.last_errno != 0) {
741 errno = ctx->_.dir.last_errno;
742 openssl_strerror_r(errno, errbuf, sizeof(errbuf));
743 OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN, ERR_R_SYS_LIB);
744 ERR_add_error_data(1, errbuf);
747 ctx->_.dir.end_reached = 1;
753 if ((buff = BIO_new(BIO_f_buffer())) == NULL
754 || (ctx->_.file.file = BIO_new_file(path, "rb")) == NULL) {
759 ctx->_.file.file = BIO_push(buff, ctx->_.file.file);
760 if (BIO_buffer_peek(ctx->_.file.file, peekbuf, sizeof(peekbuf)-1) > 0) {
761 peekbuf[sizeof(peekbuf)-1] = '\0';
762 if (strstr(peekbuf, "-----BEGIN ") != NULL)
769 OSSL_STORE_LOADER_CTX_free(ctx);
773 static int file_ctrl(OSSL_STORE_LOADER_CTX *ctx, int cmd, va_list args)
778 case OSSL_STORE_C_USE_SECMEM:
780 int on = *(va_arg(args, int *));
784 ctx->flags &= ~FILE_FLAG_SECMEM;
787 ctx->flags |= FILE_FLAG_SECMEM;
790 OSSL_STOREerr(OSSL_STORE_F_FILE_CTRL,
791 ERR_R_PASSED_INVALID_ARGUMENT);
804 /* Internal function to decode an already opened PEM file */
805 OSSL_STORE_LOADER_CTX *ossl_store_file_attach_pem_bio_int(BIO *bp)
807 OSSL_STORE_LOADER_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
810 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_FILE_ATTACH_PEM_BIO_INT,
811 ERR_R_MALLOC_FAILURE);
815 ctx->_.file.file = bp;
821 static OSSL_STORE_INFO *file_load_try_decode(OSSL_STORE_LOADER_CTX *ctx,
822 const char *pem_name,
823 const char *pem_header,
824 unsigned char *data, size_t len,
825 const UI_METHOD *ui_method,
826 void *ui_data, int *matchcount)
828 OSSL_STORE_INFO *result = NULL;
829 BUF_MEM *new_mem = NULL;
830 char *new_pem_name = NULL;
836 void *handler_ctx = NULL;
837 const FILE_HANDLER **matching_handlers =
838 OPENSSL_zalloc(sizeof(*matching_handlers)
839 * OSSL_NELEM(file_handlers));
841 if (matching_handlers == NULL) {
842 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD_TRY_DECODE,
843 ERR_R_MALLOC_FAILURE);
848 for (i = 0; i < OSSL_NELEM(file_handlers); i++) {
849 const FILE_HANDLER *handler = file_handlers[i];
850 int try_matchcount = 0;
851 void *tmp_handler_ctx = NULL;
852 OSSL_STORE_INFO *tmp_result =
853 handler->try_decode(pem_name, pem_header, data, len,
854 &tmp_handler_ctx, &try_matchcount,
857 if (try_matchcount > 0) {
858 if (matching_handlers)
859 matching_handlers[*matchcount] = handler;
862 handler->destroy_ctx(&handler_ctx);
863 handler_ctx = tmp_handler_ctx;
865 if ((*matchcount += try_matchcount) > 1) {
866 /* more than one match => ambiguous, kill any result */
867 OSSL_STORE_INFO_free(result);
868 OSSL_STORE_INFO_free(tmp_result);
869 if (handler->destroy_ctx != NULL)
870 handler->destroy_ctx(&handler_ctx);
880 if (*matchcount == 1 && matching_handlers[0]->repeatable) {
881 ctx->_.file.last_handler = matching_handlers[0];
882 ctx->_.file.last_handler_ctx = handler_ctx;
885 OPENSSL_free(matching_handlers);
889 OPENSSL_free(new_pem_name);
890 BUF_MEM_free(new_mem);
893 && (t = OSSL_STORE_INFO_get_type(result)) == OSSL_STORE_INFO_EMBEDDED) {
894 pem_name = new_pem_name =
895 ossl_store_info_get0_EMBEDDED_pem_name(result);
896 new_mem = ossl_store_info_get0_EMBEDDED_buffer(result);
897 data = (unsigned char *)new_mem->data;
898 len = new_mem->length;
899 OPENSSL_free(result);
910 static OSSL_STORE_INFO *file_load_try_repeat(OSSL_STORE_LOADER_CTX *ctx,
911 const UI_METHOD *ui_method,
914 OSSL_STORE_INFO *result = NULL;
915 int try_matchcount = 0;
917 if (ctx->_.file.last_handler != NULL) {
919 ctx->_.file.last_handler->try_decode(NULL, NULL, NULL, 0,
920 &ctx->_.file.last_handler_ctx,
924 if (result == NULL) {
925 ctx->_.file.last_handler->destroy_ctx(&ctx->_.file.last_handler_ctx);
926 ctx->_.file.last_handler_ctx = NULL;
927 ctx->_.file.last_handler = NULL;
933 static void pem_free_flag(void *pem_data, int secure)
936 OPENSSL_secure_free(pem_data);
938 OPENSSL_free(pem_data);
940 static int file_read_pem(BIO *bp, char **pem_name, char **pem_header,
941 unsigned char **data, long *len,
942 const UI_METHOD *ui_method,
943 void *ui_data, int secure)
946 ? PEM_read_bio_ex(bp, pem_name, pem_header, data, len,
947 PEM_FLAG_SECURE | PEM_FLAG_EAY_COMPATIBLE)
948 : PEM_read_bio(bp, pem_name, pem_header, data, len);
954 * 10 is the number of characters in "Proc-Type:", which
955 * PEM_get_EVP_CIPHER_INFO() requires to be present.
956 * If the PEM header has less characters than that, it's
957 * not worth spending cycles on it.
959 if (strlen(*pem_header) > 10) {
960 EVP_CIPHER_INFO cipher;
961 struct pem_pass_data pass_data;
963 if (!PEM_get_EVP_CIPHER_INFO(*pem_header, &cipher)
964 || !file_fill_pem_pass_data(&pass_data, "PEM", ui_method, ui_data)
965 || !PEM_do_header(&cipher, *data, len, file_get_pem_pass,
973 static int file_read_asn1(BIO *bp, unsigned char **data, long *len)
977 if (asn1_d2i_read_bio(bp, &mem) < 0)
980 *data = (unsigned char *)mem->data;
981 *len = (long)mem->length;
987 static int ends_with_dirsep(const char *uri)
990 uri += strlen(uri) - 1;
992 if (*uri == ']' || *uri == '>' || *uri == ':')
1001 static int file_name_to_uri(OSSL_STORE_LOADER_CTX *ctx, const char *name,
1004 assert(name != NULL);
1005 assert(data != NULL);
1007 const char *pathsep = ends_with_dirsep(ctx->_.dir.uri) ? "" : "/";
1008 long calculated_length = strlen(ctx->_.dir.uri) + strlen(pathsep)
1009 + strlen(name) + 1 /* \0 */;
1011 *data = OPENSSL_zalloc(calculated_length);
1012 if (*data == NULL) {
1013 OSSL_STOREerr(OSSL_STORE_F_FILE_NAME_TO_URI, ERR_R_MALLOC_FAILURE);
1017 OPENSSL_strlcat(*data, ctx->_.dir.uri, calculated_length);
1018 OPENSSL_strlcat(*data, pathsep, calculated_length);
1019 OPENSSL_strlcat(*data, name, calculated_length);
1024 static int file_eof(OSSL_STORE_LOADER_CTX *ctx);
1025 static int file_error(OSSL_STORE_LOADER_CTX *ctx);
1026 static OSSL_STORE_INFO *file_load(OSSL_STORE_LOADER_CTX *ctx,
1027 const UI_METHOD *ui_method, void *ui_data)
1029 OSSL_STORE_INFO *result = NULL;
1034 if (ctx->type == is_dir) {
1036 char *newname = NULL;
1038 if (ctx->_.dir.last_entry == NULL) {
1039 if (!ctx->_.dir.end_reached) {
1041 assert(ctx->_.dir.last_errno != 0);
1042 errno = ctx->_.dir.last_errno;
1044 openssl_strerror_r(errno, errbuf, sizeof(errbuf));
1045 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD, ERR_R_SYS_LIB);
1046 ERR_add_error_data(1, errbuf);
1051 if (ctx->_.dir.last_entry[0] != '.'
1052 && !file_name_to_uri(ctx, ctx->_.dir.last_entry, &newname))
1056 * On the first call (with a NULL context), OPENSSL_DIR_read()
1057 * cares about the second argument. On the following calls, it
1058 * only cares that it isn't NULL. Therefore, we can safely give
1061 ctx->_.dir.last_entry = OPENSSL_DIR_read(&ctx->_.dir.ctx,
1063 ctx->_.dir.last_errno = errno;
1064 if (ctx->_.dir.last_entry == NULL && ctx->_.dir.last_errno == 0)
1065 ctx->_.dir.end_reached = 1;
1068 && (result = OSSL_STORE_INFO_new_NAME(newname)) == NULL) {
1069 OPENSSL_free(newname);
1070 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD, ERR_R_OSSL_STORE_LIB);
1073 } while (result == NULL && !file_eof(ctx));
1075 int matchcount = -1;
1077 result = file_load_try_repeat(ctx, ui_method, ui_data);
1085 char *pem_name = NULL; /* PEM record name */
1086 char *pem_header = NULL; /* PEM record header */
1087 unsigned char *data = NULL; /* DER encoded data */
1088 long len = 0; /* DER encoded data length */
1091 if (ctx->type == is_pem) {
1092 if (!file_read_pem(ctx->_.file.file, &pem_name, &pem_header,
1093 &data, &len, ui_method, ui_data,
1094 (ctx->flags & FILE_FLAG_SECMEM) != 0)) {
1099 if (!file_read_asn1(ctx->_.file.file, &data, &len)) {
1105 result = file_load_try_decode(ctx, pem_name, pem_header, data, len,
1106 ui_method, ui_data, &matchcount);
1112 * If a PEM name matches more than one handler, the handlers are
1115 if (!ossl_assert(pem_name == NULL || matchcount <= 1)) {
1120 if (matchcount > 1) {
1121 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD,
1122 OSSL_STORE_R_AMBIGUOUS_CONTENT_TYPE);
1123 } else if (matchcount == 1) {
1125 * If there are other errors on the stack, they already show
1126 * what the problem is.
1128 if (ERR_peek_error() == 0) {
1129 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD,
1130 OSSL_STORE_R_UNSUPPORTED_CONTENT_TYPE);
1131 if (pem_name != NULL)
1132 ERR_add_error_data(3, "PEM type is '", pem_name, "'");
1139 pem_free_flag(pem_name, (ctx->flags & FILE_FLAG_SECMEM) != 0);
1140 pem_free_flag(pem_header, (ctx->flags & FILE_FLAG_SECMEM) != 0);
1141 pem_free_flag(data, (ctx->flags & FILE_FLAG_SECMEM) != 0);
1142 } while (matchcount == 0 && !file_eof(ctx) && !file_error(ctx));
1144 /* We bail out on ambiguity */
1152 static int file_error(OSSL_STORE_LOADER_CTX *ctx)
1154 return ctx->errcnt > 0;
1157 static int file_eof(OSSL_STORE_LOADER_CTX *ctx)
1159 if (ctx->type == is_dir)
1160 return ctx->_.dir.end_reached;
1162 if (ctx->_.file.last_handler != NULL
1163 && !ctx->_.file.last_handler->eof(ctx->_.file.last_handler_ctx))
1165 return BIO_eof(ctx->_.file.file);
1168 static int file_close(OSSL_STORE_LOADER_CTX *ctx)
1170 if (ctx->type == is_dir) {
1171 OPENSSL_DIR_end(&ctx->_.dir.ctx);
1173 BIO_free_all(ctx->_.file.file);
1175 OSSL_STORE_LOADER_CTX_free(ctx);
1179 int ossl_store_file_detach_pem_bio_int(OSSL_STORE_LOADER_CTX *ctx)
1181 OSSL_STORE_LOADER_CTX_free(ctx);
1185 static OSSL_STORE_LOADER file_loader =
1197 static void store_file_loader_deinit(void)
1199 ossl_store_unregister_loader_int(file_loader.scheme);
1202 int ossl_store_file_loader_init(void)
1204 int ret = ossl_store_register_loader_int(&file_loader);
1206 OPENSSL_atexit(store_file_loader_deinit);