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/ctype.h"
27 #include "internal/o_dir.h"
28 #include "internal/cryptlib.h"
29 #include "internal/store_int.h"
30 #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 * The directory reading utility we have combines opening with
718 * reading the first name. To make sure we can detect the end
719 * at the right time, we read early and cache the name.
721 const char *last_entry;
727 static void OSSL_STORE_LOADER_CTX_free(OSSL_STORE_LOADER_CTX *ctx)
729 if (ctx->type == is_dir) {
730 OPENSSL_free(ctx->_.dir.uri);
732 if (ctx->_.file.last_handler != NULL) {
733 ctx->_.file.last_handler->destroy_ctx(&ctx->_.file.last_handler_ctx);
734 ctx->_.file.last_handler_ctx = NULL;
735 ctx->_.file.last_handler = NULL;
741 static OSSL_STORE_LOADER_CTX *file_open(const OSSL_STORE_LOADER *loader,
743 const UI_METHOD *ui_method,
746 OSSL_STORE_LOADER_CTX *ctx = NULL;
750 unsigned int check_absolute:1;
752 size_t path_data_n = 0, i;
756 * First step, just take the URI as is.
758 path_data[path_data_n].check_absolute = 0;
759 path_data[path_data_n++].path = uri;
762 * Second step, if the URI appears to start with the 'file' scheme,
763 * extract the path and make that the second path to check.
764 * There's a special case if the URI also contains an authority, then
765 * the full URI shouldn't be used as a path anywhere.
767 if (strncasecmp(uri, "file:", 5) == 0) {
768 const char *p = &uri[5];
770 if (strncmp(&uri[5], "//", 2) == 0) {
771 path_data_n--; /* Invalidate using the full URI */
772 if (strncasecmp(&uri[7], "localhost/", 10) == 0) {
774 } else if (uri[7] == '/') {
777 OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN,
778 OSSL_STORE_R_URI_AUTHORITY_UNSUPPORTED);
783 path_data[path_data_n].check_absolute = 1;
785 /* Windows file: URIs with a drive letter start with a / */
786 if (p[0] == '/' && p[2] == ':' && p[3] == '/') {
787 char c = ossl_tolower(p[1]);
789 if (c >= 'a' && c <= 'z') {
791 /* We know it's absolute, so no need to check */
792 path_data[path_data_n].check_absolute = 0;
796 path_data[path_data_n++].path = p;
800 for (i = 0, path = NULL; path == NULL && i < path_data_n; i++) {
802 * If the scheme "file" was an explicit part of the URI, the path must
803 * be absolute. So says RFC 8089
805 if (path_data[i].check_absolute && path_data[i].path[0] != '/') {
806 OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN,
807 OSSL_STORE_R_PATH_MUST_BE_ABSOLUTE);
808 ERR_add_error_data(1, path_data[i].path);
812 if (stat(path_data[i].path, &st) < 0) {
813 SYSerr(SYS_F_STAT, errno);
814 ERR_add_error_data(1, path_data[i].path);
816 path = path_data[i].path;
823 /* Successfully found a working path, clear possible collected errors */
826 ctx = OPENSSL_zalloc(sizeof(*ctx));
828 OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN, ERR_R_MALLOC_FAILURE);
832 if ((st.st_mode & S_IFDIR) == S_IFDIR) {
834 * Try to copy everything, even if we know that some of them must be
835 * NULL for the moment. This prevents errors in the future, when more
836 * components may be used.
838 ctx->_.dir.uri = OPENSSL_strdup(uri);
841 if (ctx->_.dir.uri == NULL)
844 ctx->_.dir.last_entry = OPENSSL_DIR_read(&ctx->_.dir.ctx, path);
845 ctx->_.dir.last_errno = errno;
846 if (ctx->_.dir.last_entry == NULL) {
847 if (ctx->_.dir.last_errno != 0) {
849 errno = ctx->_.dir.last_errno;
850 openssl_strerror_r(errno, errbuf, sizeof(errbuf));
851 OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN, ERR_R_SYS_LIB);
852 ERR_add_error_data(1, errbuf);
855 ctx->_.dir.end_reached = 1;
861 if ((buff = BIO_new(BIO_f_buffer())) == NULL
862 || (ctx->_.file.file = BIO_new_file(path, "rb")) == NULL) {
867 ctx->_.file.file = BIO_push(buff, ctx->_.file.file);
868 if (BIO_buffer_peek(ctx->_.file.file, peekbuf, sizeof(peekbuf) - 1) > 0) {
869 peekbuf[sizeof(peekbuf) - 1] = '\0';
870 if (strstr(peekbuf, "-----BEGIN ") != NULL)
877 OSSL_STORE_LOADER_CTX_free(ctx);
881 static int file_ctrl(OSSL_STORE_LOADER_CTX *ctx, int cmd, va_list args)
886 case OSSL_STORE_C_USE_SECMEM:
888 int on = *(va_arg(args, int *));
892 ctx->flags &= ~FILE_FLAG_SECMEM;
895 ctx->flags |= FILE_FLAG_SECMEM;
898 OSSL_STOREerr(OSSL_STORE_F_FILE_CTRL,
899 ERR_R_PASSED_INVALID_ARGUMENT);
912 /* Internal function to decode an already opened PEM file */
913 OSSL_STORE_LOADER_CTX *ossl_store_file_attach_pem_bio_int(BIO *bp)
915 OSSL_STORE_LOADER_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
918 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_FILE_ATTACH_PEM_BIO_INT,
919 ERR_R_MALLOC_FAILURE);
923 ctx->_.file.file = bp;
929 static OSSL_STORE_INFO *file_load_try_decode(OSSL_STORE_LOADER_CTX *ctx,
930 const char *pem_name,
931 const char *pem_header,
932 unsigned char *data, size_t len,
933 const UI_METHOD *ui_method,
934 void *ui_data, int *matchcount)
936 OSSL_STORE_INFO *result = NULL;
937 BUF_MEM *new_mem = NULL;
938 char *new_pem_name = NULL;
944 void *handler_ctx = NULL;
945 const FILE_HANDLER **matching_handlers =
946 OPENSSL_zalloc(sizeof(*matching_handlers)
947 * OSSL_NELEM(file_handlers));
949 if (matching_handlers == NULL) {
950 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD_TRY_DECODE,
951 ERR_R_MALLOC_FAILURE);
956 for (i = 0; i < OSSL_NELEM(file_handlers); i++) {
957 const FILE_HANDLER *handler = file_handlers[i];
958 int try_matchcount = 0;
959 void *tmp_handler_ctx = NULL;
960 OSSL_STORE_INFO *tmp_result =
961 handler->try_decode(pem_name, pem_header, data, len,
962 &tmp_handler_ctx, &try_matchcount,
965 if (try_matchcount > 0) {
966 if (matching_handlers)
967 matching_handlers[*matchcount] = handler;
970 handler->destroy_ctx(&handler_ctx);
971 handler_ctx = tmp_handler_ctx;
973 if ((*matchcount += try_matchcount) > 1) {
974 /* more than one match => ambiguous, kill any result */
975 OSSL_STORE_INFO_free(result);
976 OSSL_STORE_INFO_free(tmp_result);
977 if (handler->destroy_ctx != NULL)
978 handler->destroy_ctx(&handler_ctx);
988 if (*matchcount == 1 && matching_handlers[0]->repeatable) {
989 ctx->_.file.last_handler = matching_handlers[0];
990 ctx->_.file.last_handler_ctx = handler_ctx;
993 OPENSSL_free(matching_handlers);
997 OPENSSL_free(new_pem_name);
998 BUF_MEM_free(new_mem);
1001 && (t = OSSL_STORE_INFO_get_type(result)) == OSSL_STORE_INFO_EMBEDDED) {
1002 pem_name = new_pem_name =
1003 ossl_store_info_get0_EMBEDDED_pem_name(result);
1004 new_mem = ossl_store_info_get0_EMBEDDED_buffer(result);
1005 data = (unsigned char *)new_mem->data;
1006 len = new_mem->length;
1007 OPENSSL_free(result);
1018 static OSSL_STORE_INFO *file_load_try_repeat(OSSL_STORE_LOADER_CTX *ctx,
1019 const UI_METHOD *ui_method,
1022 OSSL_STORE_INFO *result = NULL;
1023 int try_matchcount = 0;
1025 if (ctx->_.file.last_handler != NULL) {
1027 ctx->_.file.last_handler->try_decode(NULL, NULL, NULL, 0,
1028 &ctx->_.file.last_handler_ctx,
1030 ui_method, ui_data);
1032 if (result == NULL) {
1033 ctx->_.file.last_handler->destroy_ctx(&ctx->_.file.last_handler_ctx);
1034 ctx->_.file.last_handler_ctx = NULL;
1035 ctx->_.file.last_handler = NULL;
1041 static void pem_free_flag(void *pem_data, int secure, size_t num)
1044 OPENSSL_secure_clear_free(pem_data, num);
1046 OPENSSL_free(pem_data);
1048 static int file_read_pem(BIO *bp, char **pem_name, char **pem_header,
1049 unsigned char **data, long *len,
1050 const UI_METHOD *ui_method,
1051 void *ui_data, int secure)
1054 ? PEM_read_bio_ex(bp, pem_name, pem_header, data, len,
1055 PEM_FLAG_SECURE | PEM_FLAG_EAY_COMPATIBLE)
1056 : PEM_read_bio(bp, pem_name, pem_header, data, len);
1062 * 10 is the number of characters in "Proc-Type:", which
1063 * PEM_get_EVP_CIPHER_INFO() requires to be present.
1064 * If the PEM header has less characters than that, it's
1065 * not worth spending cycles on it.
1067 if (strlen(*pem_header) > 10) {
1068 EVP_CIPHER_INFO cipher;
1069 struct pem_pass_data pass_data;
1071 if (!PEM_get_EVP_CIPHER_INFO(*pem_header, &cipher)
1072 || !file_fill_pem_pass_data(&pass_data, "PEM", ui_method, ui_data)
1073 || !PEM_do_header(&cipher, *data, len, file_get_pem_pass,
1081 static int file_read_asn1(BIO *bp, unsigned char **data, long *len)
1083 BUF_MEM *mem = NULL;
1085 if (asn1_d2i_read_bio(bp, &mem) < 0)
1088 *data = (unsigned char *)mem->data;
1089 *len = (long)mem->length;
1095 static int ends_with_dirsep(const char *uri)
1098 uri += strlen(uri) - 1;
1100 if (*uri == ']' || *uri == '>' || *uri == ':')
1102 #elif defined _WIN32
1109 static int file_name_to_uri(OSSL_STORE_LOADER_CTX *ctx, const char *name,
1112 assert(name != NULL);
1113 assert(data != NULL);
1115 const char *pathsep = ends_with_dirsep(ctx->_.dir.uri) ? "" : "/";
1116 long calculated_length = strlen(ctx->_.dir.uri) + strlen(pathsep)
1117 + strlen(name) + 1 /* \0 */;
1119 *data = OPENSSL_zalloc(calculated_length);
1120 if (*data == NULL) {
1121 OSSL_STOREerr(OSSL_STORE_F_FILE_NAME_TO_URI, ERR_R_MALLOC_FAILURE);
1125 OPENSSL_strlcat(*data, ctx->_.dir.uri, calculated_length);
1126 OPENSSL_strlcat(*data, pathsep, calculated_length);
1127 OPENSSL_strlcat(*data, name, calculated_length);
1132 static int file_eof(OSSL_STORE_LOADER_CTX *ctx);
1133 static int file_error(OSSL_STORE_LOADER_CTX *ctx);
1134 static OSSL_STORE_INFO *file_load(OSSL_STORE_LOADER_CTX *ctx,
1135 const UI_METHOD *ui_method, void *ui_data)
1137 OSSL_STORE_INFO *result = NULL;
1142 if (ctx->type == is_dir) {
1144 char *newname = NULL;
1146 if (ctx->_.dir.last_entry == NULL) {
1147 if (!ctx->_.dir.end_reached) {
1149 assert(ctx->_.dir.last_errno != 0);
1150 errno = ctx->_.dir.last_errno;
1152 openssl_strerror_r(errno, errbuf, sizeof(errbuf));
1153 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD, ERR_R_SYS_LIB);
1154 ERR_add_error_data(1, errbuf);
1159 if (ctx->_.dir.last_entry[0] != '.'
1160 && !file_name_to_uri(ctx, ctx->_.dir.last_entry, &newname))
1164 * On the first call (with a NULL context), OPENSSL_DIR_read()
1165 * cares about the second argument. On the following calls, it
1166 * only cares that it isn't NULL. Therefore, we can safely give
1169 ctx->_.dir.last_entry = OPENSSL_DIR_read(&ctx->_.dir.ctx,
1171 ctx->_.dir.last_errno = errno;
1172 if (ctx->_.dir.last_entry == NULL && ctx->_.dir.last_errno == 0)
1173 ctx->_.dir.end_reached = 1;
1176 && (result = OSSL_STORE_INFO_new_NAME(newname)) == NULL) {
1177 OPENSSL_free(newname);
1178 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD, ERR_R_OSSL_STORE_LIB);
1181 } while (result == NULL && !file_eof(ctx));
1183 int matchcount = -1;
1185 result = file_load_try_repeat(ctx, ui_method, ui_data);
1193 char *pem_name = NULL; /* PEM record name */
1194 char *pem_header = NULL; /* PEM record header */
1195 unsigned char *data = NULL; /* DER encoded data */
1196 long len = 0; /* DER encoded data length */
1199 if (ctx->type == is_pem) {
1200 if (!file_read_pem(ctx->_.file.file, &pem_name, &pem_header,
1201 &data, &len, ui_method, ui_data,
1202 (ctx->flags & FILE_FLAG_SECMEM) != 0)) {
1207 if (!file_read_asn1(ctx->_.file.file, &data, &len)) {
1213 result = file_load_try_decode(ctx, pem_name, pem_header, data, len,
1214 ui_method, ui_data, &matchcount);
1220 * If a PEM name matches more than one handler, the handlers are
1223 if (!ossl_assert(pem_name == NULL || matchcount <= 1)) {
1228 if (matchcount > 1) {
1229 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD,
1230 OSSL_STORE_R_AMBIGUOUS_CONTENT_TYPE);
1231 } else if (matchcount == 1) {
1233 * If there are other errors on the stack, they already show
1234 * what the problem is.
1236 if (ERR_peek_error() == 0) {
1237 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD,
1238 OSSL_STORE_R_UNSUPPORTED_CONTENT_TYPE);
1239 if (pem_name != NULL)
1240 ERR_add_error_data(3, "PEM type is '", pem_name, "'");
1247 pem_free_flag(pem_name, (ctx->flags & FILE_FLAG_SECMEM) != 0, 0);
1248 pem_free_flag(pem_header, (ctx->flags & FILE_FLAG_SECMEM) != 0, 0);
1249 pem_free_flag(data, (ctx->flags & FILE_FLAG_SECMEM) != 0, len);
1250 } while (matchcount == 0 && !file_eof(ctx) && !file_error(ctx));
1252 /* We bail out on ambiguity */
1260 static int file_error(OSSL_STORE_LOADER_CTX *ctx)
1262 return ctx->errcnt > 0;
1265 static int file_eof(OSSL_STORE_LOADER_CTX *ctx)
1267 if (ctx->type == is_dir)
1268 return ctx->_.dir.end_reached;
1270 if (ctx->_.file.last_handler != NULL
1271 && !ctx->_.file.last_handler->eof(ctx->_.file.last_handler_ctx))
1273 return BIO_eof(ctx->_.file.file);
1276 static int file_close(OSSL_STORE_LOADER_CTX *ctx)
1278 if (ctx->type == is_dir) {
1279 OPENSSL_DIR_end(&ctx->_.dir.ctx);
1281 BIO_free_all(ctx->_.file.file);
1283 OSSL_STORE_LOADER_CTX_free(ctx);
1287 int ossl_store_file_detach_pem_bio_int(OSSL_STORE_LOADER_CTX *ctx)
1289 OSSL_STORE_LOADER_CTX_free(ctx);
1293 static OSSL_STORE_LOADER file_loader =
1305 static void store_file_loader_deinit(void)
1307 ossl_store_unregister_loader_int(file_loader.scheme);
1310 int ossl_store_file_loader_init(void)
1312 int ret = ossl_store_register_loader_int(&file_loader);
1314 OPENSSL_atexit(store_file_loader_deinit);