2 * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
16 #include <openssl/bio.h>
17 #include <openssl/dsa.h> /* For d2i_DSAPrivateKey */
18 #include <openssl/err.h>
19 #include <openssl/evp.h>
20 #include <openssl/pem.h>
21 #include "internal/pem_int.h"
22 #include <openssl/pkcs12.h> /* For the PKCS8 stuff o.O */
23 #include <openssl/rsa.h> /* For d2i_RSAPrivateKey */
24 #include <openssl/safestack.h>
25 #include <openssl/store.h>
26 #include <openssl/ui.h>
27 #include <openssl/x509.h> /* For the PKCS8 stuff o.O */
28 #include "crypto/asn1.h"
29 #include "crypto/ctype.h"
30 #include "internal/o_dir.h"
31 #include "internal/cryptlib.h"
32 #include "crypto/store.h"
33 #include "crypto/evp.h"
34 #include "store_local.h"
43 # define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
51 static char *file_get_pass(const UI_METHOD *ui_method, char *pass,
52 size_t maxsize, const char *desc, const char *info,
59 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS, ERR_R_MALLOC_FAILURE);
63 if (ui_method != NULL)
64 UI_set_method(ui, ui_method);
65 UI_add_user_data(ui, data);
67 if ((prompt = UI_construct_prompt(ui, desc, info)) == NULL) {
68 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS, ERR_R_MALLOC_FAILURE);
70 } else if (!UI_add_input_string(ui, prompt, UI_INPUT_FLAG_DEFAULT_PWD,
71 pass, 0, maxsize - 1)) {
72 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS, ERR_R_UI_LIB);
75 switch (UI_process(ui)) {
77 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS,
78 OSSL_STORE_R_UI_PROCESS_INTERRUPTED_OR_CANCELLED);
82 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS, ERR_R_UI_LIB);
95 struct pem_pass_data {
96 const UI_METHOD *ui_method;
98 const char *prompt_desc;
99 const char *prompt_info;
102 static int file_fill_pem_pass_data(struct pem_pass_data *pass_data,
103 const char *desc, const char *info,
104 const UI_METHOD *ui_method, void *ui_data)
106 if (pass_data == NULL)
108 pass_data->ui_method = ui_method;
109 pass_data->data = ui_data;
110 pass_data->prompt_desc = desc;
111 pass_data->prompt_info = info;
115 /* This is used anywhere a pem_password_cb is needed */
116 static int file_get_pem_pass(char *buf, int num, int w, void *data)
118 struct pem_pass_data *pass_data = data;
119 char *pass = file_get_pass(pass_data->ui_method, buf, num,
120 pass_data->prompt_desc, pass_data->prompt_info,
123 return pass == NULL ? 0 : strlen(pass);
127 * The file scheme decoders
128 * ------------------------
130 * Each possible data type has its own decoder, which either operates
131 * through a given PEM name, or attempts to decode to see if the blob
132 * it's given is decodable for its data type. The assumption is that
133 * only the correct data type will match the content.
137 * The try_decode function is called to check if the blob of data can
138 * be used by this handler, and if it can, decodes it into a supported
139 * OpenSSL type and returns a OSSL_STORE_INFO with the decoded data.
141 * pem_name: If this blob comes from a PEM file, this holds
142 * the PEM name. If it comes from another type of
143 * file, this is NULL.
144 * pem_header: If this blob comes from a PEM file, this holds
145 * the PEM headers. If it comes from another type of
146 * file, this is NULL.
147 * blob: The blob of data to match with what this handler
149 * len: The length of the blob.
150 * handler_ctx: For a handler marked repeatable, this pointer can
151 * be used to create a context for the handler. IT IS
152 * THE HANDLER'S RESPONSIBILITY TO CREATE AND DESTROY
153 * THIS CONTEXT APPROPRIATELY, i.e. create on first call
154 * and destroy when about to return NULL.
155 * matchcount: A pointer to an int to count matches for this data.
156 * Usually becomes 0 (no match) or 1 (match!), but may
157 * be higher in the (unlikely) event that the data matches
158 * more than one possibility. The int will always be
159 * zero when the function is called.
160 * ui_method: Application UI method for getting a password, pin
161 * or any other interactive data.
162 * ui_data: Application data to be passed to ui_method when
164 * libctx: The library context to be used if applicable
165 * propq: The property query string for any algorithm fetches
169 typedef OSSL_STORE_INFO *(*file_try_decode_fn)(const char *pem_name,
170 const char *pem_header,
171 const unsigned char *blob,
172 size_t len, void **handler_ctx,
174 const UI_METHOD *ui_method,
175 void *ui_data, const char *uri,
179 * The eof function should return 1 if there's no more data to be found
180 * with the handler_ctx, otherwise 0. This is only used when the handler is
183 typedef int (*file_eof_fn)(void *handler_ctx);
185 * The destroy_ctx function is used to destroy the handler_ctx that was
186 * initiated by a repeatable try_decode function. This is only used when
187 * the handler is marked repeatable.
189 typedef void (*file_destroy_ctx_fn)(void **handler_ctx);
191 typedef struct file_handler_st {
193 file_try_decode_fn try_decode;
195 file_destroy_ctx_fn destroy_ctx;
202 * PKCS#12 decoder. It operates by decoding all of the blob content,
203 * extracting all the interesting data from it and storing them internally,
204 * then serving them one piece at a time.
206 static OSSL_STORE_INFO *try_decode_PKCS12(const char *pem_name,
207 const char *pem_header,
208 const unsigned char *blob,
209 size_t len, void **pctx,
211 const UI_METHOD *ui_method,
212 void *ui_data, const char *uri,
216 OSSL_STORE_INFO *store_info = NULL;
217 STACK_OF(OSSL_STORE_INFO) *ctx = *pctx;
220 /* Initial parsing */
224 if (pem_name != NULL)
225 /* No match, there is no PEM PKCS12 tag */
228 if ((p12 = d2i_PKCS12(NULL, &blob, len)) != NULL) {
230 char tpass[PEM_BUFSIZE];
231 EVP_PKEY *pkey = NULL;
233 STACK_OF(X509) *chain = NULL;
237 if (PKCS12_verify_mac(p12, "", 0)
238 || PKCS12_verify_mac(p12, NULL, 0)) {
241 if ((pass = file_get_pass(ui_method, tpass, PEM_BUFSIZE,
242 "PKCS12 import pass phrase", uri,
244 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS12,
245 OSSL_STORE_R_PASSPHRASE_CALLBACK_ERROR);
248 if (!PKCS12_verify_mac(p12, pass, strlen(pass))) {
249 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS12,
250 OSSL_STORE_R_ERROR_VERIFYING_PKCS12_MAC);
255 if (PKCS12_parse(p12, pass, &pkey, &cert, &chain)) {
256 OSSL_STORE_INFO *osi_pkey = NULL;
257 OSSL_STORE_INFO *osi_cert = NULL;
258 OSSL_STORE_INFO *osi_ca = NULL;
260 if ((ctx = sk_OSSL_STORE_INFO_new_null()) != NULL
261 && (osi_pkey = OSSL_STORE_INFO_new_PKEY(pkey)) != NULL
262 && sk_OSSL_STORE_INFO_push(ctx, osi_pkey) != 0
263 && (osi_cert = OSSL_STORE_INFO_new_CERT(cert)) != NULL
264 && sk_OSSL_STORE_INFO_push(ctx, osi_cert) != 0) {
269 while(sk_X509_num(chain) > 0) {
270 X509 *ca = sk_X509_value(chain, 0);
272 if ((osi_ca = OSSL_STORE_INFO_new_CERT(ca)) == NULL
273 || sk_OSSL_STORE_INFO_push(ctx, osi_ca) == 0) {
278 (void)sk_X509_shift(chain);
282 OSSL_STORE_INFO_free(osi_ca);
283 OSSL_STORE_INFO_free(osi_cert);
284 OSSL_STORE_INFO_free(osi_pkey);
285 sk_OSSL_STORE_INFO_pop_free(ctx, OSSL_STORE_INFO_free);
288 sk_X509_pop_free(chain, X509_free);
302 store_info = sk_OSSL_STORE_INFO_shift(ctx);
308 static int eof_PKCS12(void *ctx_)
310 STACK_OF(OSSL_STORE_INFO) *ctx = ctx_;
312 return ctx == NULL || sk_OSSL_STORE_INFO_num(ctx) == 0;
315 static void destroy_ctx_PKCS12(void **pctx)
317 STACK_OF(OSSL_STORE_INFO) *ctx = *pctx;
319 sk_OSSL_STORE_INFO_pop_free(ctx, OSSL_STORE_INFO_free);
323 static FILE_HANDLER PKCS12_handler = {
332 * Encrypted PKCS#8 decoder. It operates by just decrypting the given blob
333 * into a new blob, which is returned as an EMBEDDED STORE_INFO. The whole
334 * decoding process will then start over with the new blob.
336 static OSSL_STORE_INFO *try_decode_PKCS8Encrypted(const char *pem_name,
337 const char *pem_header,
338 const unsigned char *blob,
339 size_t len, void **pctx,
341 const UI_METHOD *ui_method,
348 char kbuf[PEM_BUFSIZE];
350 const X509_ALGOR *dalg = NULL;
351 const ASN1_OCTET_STRING *doct = NULL;
352 OSSL_STORE_INFO *store_info = NULL;
354 unsigned char *new_data = NULL;
357 if (pem_name != NULL) {
358 if (strcmp(pem_name, PEM_STRING_PKCS8) != 0)
363 if ((p8 = d2i_X509_SIG(NULL, &blob, len)) == NULL)
368 if ((mem = BUF_MEM_new()) == NULL) {
369 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS8ENCRYPTED,
370 ERR_R_MALLOC_FAILURE);
374 if ((pass = file_get_pass(ui_method, kbuf, PEM_BUFSIZE,
375 "PKCS8 decrypt pass phrase", uri,
377 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS8ENCRYPTED,
378 OSSL_STORE_R_BAD_PASSWORD_READ);
382 X509_SIG_get0(p8, &dalg, &doct);
383 if (!PKCS12_pbe_crypt(dalg, pass, strlen(pass), doct->data, doct->length,
384 &new_data, &new_data_len, 0))
387 mem->data = (char *)new_data;
388 mem->max = mem->length = (size_t)new_data_len;
391 store_info = ossl_store_info_new_EMBEDDED(PEM_STRING_PKCS8INF, mem);
392 if (store_info == NULL) {
393 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS8ENCRYPTED,
394 ERR_R_MALLOC_FAILURE);
405 static FILE_HANDLER PKCS8Encrypted_handler = {
407 try_decode_PKCS8Encrypted
411 * Private key decoder. Decodes all sorts of private keys, both PKCS#8
412 * encoded ones and old style PEM ones (with the key type is encoded into
415 int pem_check_suffix(const char *pem_str, const char *suffix);
416 static OSSL_STORE_INFO *try_decode_PrivateKey(const char *pem_name,
417 const char *pem_header,
418 const unsigned char *blob,
419 size_t len, void **pctx,
421 const UI_METHOD *ui_method,
422 void *ui_data, const char *uri,
426 OSSL_STORE_INFO *store_info = NULL;
427 EVP_PKEY *pkey = NULL;
428 const EVP_PKEY_ASN1_METHOD *ameth = NULL;
430 if (pem_name != NULL) {
431 if (strcmp(pem_name, PEM_STRING_PKCS8INF) == 0) {
432 PKCS8_PRIV_KEY_INFO *p8inf =
433 d2i_PKCS8_PRIV_KEY_INFO(NULL, &blob, len);
437 pkey = evp_pkcs82pkey_int(p8inf, libctx, propq);
438 PKCS8_PRIV_KEY_INFO_free(p8inf);
442 if ((slen = pem_check_suffix(pem_name, "PRIVATE KEY")) > 0
443 && (ameth = EVP_PKEY_asn1_find_str(NULL, pem_name,
446 pkey = d2i_PrivateKey_ex(ameth->pkey_id, NULL, &blob, len,
453 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
454 EVP_PKEY *tmp_pkey = NULL;
455 const unsigned char *tmp_blob = blob;
457 ameth = EVP_PKEY_asn1_get0(i);
458 if (ameth->pkey_flags & ASN1_PKEY_ALIAS)
461 tmp_pkey = d2i_PrivateKey_ex(ameth->pkey_id, NULL, &tmp_blob, len,
463 if (tmp_pkey != NULL) {
465 EVP_PKEY_free(tmp_pkey);
472 if (*matchcount > 1) {
481 store_info = OSSL_STORE_INFO_new_PKEY(pkey);
482 if (store_info == NULL)
488 static FILE_HANDLER PrivateKey_handler = {
490 try_decode_PrivateKey
494 * Public key decoder. Only supports SubjectPublicKeyInfo formatted keys.
496 static OSSL_STORE_INFO *try_decode_PUBKEY(const char *pem_name,
497 const char *pem_header,
498 const unsigned char *blob,
499 size_t len, void **pctx,
501 const UI_METHOD *ui_method,
502 void *ui_data, const char *uri,
506 OSSL_STORE_INFO *store_info = NULL;
507 EVP_PKEY *pkey = NULL;
509 if (pem_name != NULL) {
510 if (strcmp(pem_name, PEM_STRING_PUBLIC) != 0)
516 if ((pkey = d2i_PUBKEY(NULL, &blob, len)) != NULL) {
518 store_info = OSSL_STORE_INFO_new_PKEY(pkey);
524 static FILE_HANDLER PUBKEY_handler = {
530 * Key parameter decoder.
532 static OSSL_STORE_INFO *try_decode_params(const char *pem_name,
533 const char *pem_header,
534 const unsigned char *blob,
535 size_t len, void **pctx,
537 const UI_METHOD *ui_method,
538 void *ui_data, const char *uri,
542 OSSL_STORE_INFO *store_info = NULL;
544 EVP_PKEY *pkey = NULL;
545 const EVP_PKEY_ASN1_METHOD *ameth = NULL;
548 if (pem_name != NULL) {
549 if ((slen = pem_check_suffix(pem_name, "PARAMETERS")) == 0)
555 if ((pkey = EVP_PKEY_new()) == NULL) {
556 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PARAMS, ERR_R_EVP_LIB);
561 if (EVP_PKEY_set_type_str(pkey, pem_name, slen)
562 && (ameth = EVP_PKEY_get0_asn1(pkey)) != NULL
563 && ameth->param_decode != NULL
564 && ameth->param_decode(pkey, &blob, len))
568 EVP_PKEY *tmp_pkey = NULL;
570 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
571 const unsigned char *tmp_blob = blob;
573 if (tmp_pkey == NULL && (tmp_pkey = EVP_PKEY_new()) == NULL) {
574 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PARAMS, ERR_R_EVP_LIB);
578 ameth = EVP_PKEY_asn1_get0(i);
579 if (ameth->pkey_flags & ASN1_PKEY_ALIAS)
582 if (EVP_PKEY_set_type(tmp_pkey, ameth->pkey_id)
583 && (ameth = EVP_PKEY_get0_asn1(tmp_pkey)) != NULL
584 && ameth->param_decode != NULL
585 && ameth->param_decode(tmp_pkey, &tmp_blob, len)) {
587 EVP_PKEY_free(tmp_pkey);
595 EVP_PKEY_free(tmp_pkey);
596 if (*matchcount == 1) {
602 store_info = OSSL_STORE_INFO_new_PARAMS(pkey);
603 if (store_info == NULL)
609 static FILE_HANDLER params_handler = {
615 * X.509 certificate decoder.
617 static OSSL_STORE_INFO *try_decode_X509Certificate(const char *pem_name,
618 const char *pem_header,
619 const unsigned char *blob,
620 size_t len, void **pctx,
622 const UI_METHOD *ui_method,
628 OSSL_STORE_INFO *store_info = NULL;
632 * In most cases, we can try to interpret the serialized data as a trusted
633 * cert (X509 + X509_AUX) and fall back to reading it as a normal cert
634 * (just X509), but if the PEM name specifically declares it as a trusted
635 * cert, then no fallback should be engaged. |ignore_trusted| tells if
636 * the fallback can be used (1) or not (0).
638 int ignore_trusted = 1;
640 if (pem_name != NULL) {
641 if (strcmp(pem_name, PEM_STRING_X509_TRUSTED) == 0)
643 else if (strcmp(pem_name, PEM_STRING_X509_OLD) != 0
644 && strcmp(pem_name, PEM_STRING_X509) != 0)
650 if ((cert = d2i_X509_AUX(NULL, &blob, len)) != NULL
651 || (ignore_trusted && (cert = d2i_X509(NULL, &blob, len)) != NULL)) {
653 store_info = OSSL_STORE_INFO_new_CERT(cert);
656 if (store_info == NULL)
662 static FILE_HANDLER X509Certificate_handler = {
664 try_decode_X509Certificate
670 static OSSL_STORE_INFO *try_decode_X509CRL(const char *pem_name,
671 const char *pem_header,
672 const unsigned char *blob,
673 size_t len, void **pctx,
675 const UI_METHOD *ui_method,
676 void *ui_data, const char *uri,
680 OSSL_STORE_INFO *store_info = NULL;
681 X509_CRL *crl = NULL;
683 if (pem_name != NULL) {
684 if (strcmp(pem_name, PEM_STRING_X509_CRL) != 0)
690 if ((crl = d2i_X509_CRL(NULL, &blob, len)) != NULL) {
692 store_info = OSSL_STORE_INFO_new_CRL(crl);
695 if (store_info == NULL)
701 static FILE_HANDLER X509CRL_handler = {
707 * To finish it all off, we collect all the handlers.
709 static const FILE_HANDLER *file_handlers[] = {
711 &PKCS8Encrypted_handler,
712 &X509Certificate_handler,
725 struct ossl_store_loader_ctx_st {
726 char *uri; /* The URI we currently try to load */
733 #define FILE_FLAG_SECMEM (1<<0)
734 #define FILE_FLAG_ATTACHED (1<<1)
737 struct { /* Used with is_raw and is_pem */
741 * The following are used when the handler is marked as
744 const FILE_HANDLER *last_handler;
745 void *last_handler_ctx;
747 struct { /* Used with is_dir */
748 OPENSSL_DIR_CTX *ctx;
752 * When a search expression is given, these are filled in.
753 * |search_name| contains the file basename to look for.
754 * The string is exactly 8 characters long.
759 * The directory reading utility we have combines opening with
760 * reading the first name. To make sure we can detect the end
761 * at the right time, we read early and cache the name.
763 const char *last_entry;
768 /* Expected object type. May be unspecified */
775 static void OSSL_STORE_LOADER_CTX_free(OSSL_STORE_LOADER_CTX *ctx)
780 OPENSSL_free(ctx->uri);
781 if (ctx->type != is_dir) {
782 if (ctx->_.file.last_handler != NULL) {
783 ctx->_.file.last_handler->destroy_ctx(&ctx->_.file.last_handler_ctx);
784 ctx->_.file.last_handler_ctx = NULL;
785 ctx->_.file.last_handler = NULL;
788 OPENSSL_free(ctx->propq);
792 static int file_find_type(OSSL_STORE_LOADER_CTX *ctx)
795 char peekbuf[4096] = { 0, };
797 if ((buff = BIO_new(BIO_f_buffer())) == NULL)
800 ctx->_.file.file = BIO_push(buff, ctx->_.file.file);
801 if (BIO_buffer_peek(ctx->_.file.file, peekbuf, sizeof(peekbuf) - 1) > 0) {
802 peekbuf[sizeof(peekbuf) - 1] = '\0';
803 if (strstr(peekbuf, "-----BEGIN ") != NULL)
809 static OSSL_STORE_LOADER_CTX *file_open(const OSSL_STORE_LOADER *loader,
811 const UI_METHOD *ui_method,
814 OSSL_STORE_LOADER_CTX *ctx = NULL;
818 unsigned int check_absolute:1;
820 size_t path_data_n = 0, i;
824 * First step, just take the URI as is.
826 path_data[path_data_n].check_absolute = 0;
827 path_data[path_data_n++].path = uri;
830 * Second step, if the URI appears to start with the 'file' scheme,
831 * extract the path and make that the second path to check.
832 * There's a special case if the URI also contains an authority, then
833 * the full URI shouldn't be used as a path anywhere.
835 if (strncasecmp(uri, "file:", 5) == 0) {
836 const char *p = &uri[5];
838 if (strncmp(&uri[5], "//", 2) == 0) {
839 path_data_n--; /* Invalidate using the full URI */
840 if (strncasecmp(&uri[7], "localhost/", 10) == 0) {
842 } else if (uri[7] == '/') {
845 OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN,
846 OSSL_STORE_R_URI_AUTHORITY_UNSUPPORTED);
851 path_data[path_data_n].check_absolute = 1;
853 /* Windows file: URIs with a drive letter start with a / */
854 if (p[0] == '/' && p[2] == ':' && p[3] == '/') {
855 char c = ossl_tolower(p[1]);
857 if (c >= 'a' && c <= 'z') {
859 /* We know it's absolute, so no need to check */
860 path_data[path_data_n].check_absolute = 0;
864 path_data[path_data_n++].path = p;
868 for (i = 0, path = NULL; path == NULL && i < path_data_n; i++) {
870 * If the scheme "file" was an explicit part of the URI, the path must
871 * be absolute. So says RFC 8089
873 if (path_data[i].check_absolute && path_data[i].path[0] != '/') {
874 OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN,
875 OSSL_STORE_R_PATH_MUST_BE_ABSOLUTE);
876 ERR_add_error_data(1, path_data[i].path);
880 if (stat(path_data[i].path, &st) < 0) {
881 ERR_raise_data(ERR_LIB_SYS, errno,
885 path = path_data[i].path;
892 /* Successfully found a working path, clear possible collected errors */
895 ctx = OPENSSL_zalloc(sizeof(*ctx));
897 OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN, ERR_R_MALLOC_FAILURE);
900 ctx->uri = OPENSSL_strdup(uri);
901 if (ctx->uri == NULL) {
902 OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN, ERR_R_MALLOC_FAILURE);
906 if (S_ISDIR(st.st_mode)) {
908 ctx->_.dir.last_entry = OPENSSL_DIR_read(&ctx->_.dir.ctx, path);
909 ctx->_.dir.last_errno = errno;
910 if (ctx->_.dir.last_entry == NULL) {
911 if (ctx->_.dir.last_errno != 0) {
913 OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN, ERR_R_SYS_LIB);
914 errno = ctx->_.dir.last_errno;
915 if (openssl_strerror_r(errno, errbuf, sizeof(errbuf)))
916 ERR_add_error_data(1, errbuf);
919 ctx->_.dir.end_reached = 1;
921 } else if ((ctx->_.file.file = BIO_new_file(path, "rb")) == NULL
922 || !file_find_type(ctx)) {
923 BIO_free_all(ctx->_.file.file);
929 OSSL_STORE_LOADER_CTX_free(ctx);
933 static OSSL_STORE_LOADER_CTX *file_attach(const OSSL_STORE_LOADER *loader,
934 BIO *bp, OPENSSL_CTX *libctx,
936 const UI_METHOD *ui_method,
939 OSSL_STORE_LOADER_CTX *ctx;
941 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL
942 || (propq != NULL && (ctx->propq = OPENSSL_strdup(propq)) == NULL)) {
943 OSSL_STOREerr(OSSL_STORE_F_FILE_ATTACH, ERR_R_MALLOC_FAILURE);
944 OSSL_STORE_LOADER_CTX_free(ctx);
948 ctx->libctx = libctx;
949 ctx->flags |= FILE_FLAG_ATTACHED;
950 ctx->_.file.file = bp;
951 if (!file_find_type(ctx)) {
953 ctx->_.file.file = NULL;
954 OSSL_STORE_LOADER_CTX_free(ctx);
961 static int file_ctrl(OSSL_STORE_LOADER_CTX *ctx, int cmd, va_list args)
966 case OSSL_STORE_C_USE_SECMEM:
968 int on = *(va_arg(args, int *));
972 ctx->flags &= ~FILE_FLAG_SECMEM;
975 ctx->flags |= FILE_FLAG_SECMEM;
978 OSSL_STOREerr(OSSL_STORE_F_FILE_CTRL,
979 ERR_R_PASSED_INVALID_ARGUMENT);
992 static int file_expect(OSSL_STORE_LOADER_CTX *ctx, int expected)
994 ctx->expected_type = expected;
998 static int file_find(OSSL_STORE_LOADER_CTX *ctx,
999 const OSSL_STORE_SEARCH *search)
1002 * If ctx == NULL, the library is looking to know if this loader supports
1003 * the given search type.
1006 if (OSSL_STORE_SEARCH_get_type(search) == OSSL_STORE_SEARCH_BY_NAME) {
1007 unsigned long hash = 0;
1012 if (ctx->type != is_dir) {
1013 OSSL_STOREerr(OSSL_STORE_F_FILE_FIND,
1014 OSSL_STORE_R_SEARCH_ONLY_SUPPORTED_FOR_DIRECTORIES);
1018 hash = X509_NAME_hash(OSSL_STORE_SEARCH_get0_name(search));
1019 BIO_snprintf(ctx->_.dir.search_name, sizeof(ctx->_.dir.search_name),
1025 OSSL_STOREerr(OSSL_STORE_F_FILE_FIND,
1026 OSSL_STORE_R_UNSUPPORTED_SEARCH_TYPE);
1030 static OSSL_STORE_INFO *file_load_try_decode(OSSL_STORE_LOADER_CTX *ctx,
1031 const char *pem_name,
1032 const char *pem_header,
1033 unsigned char *data, size_t len,
1034 const UI_METHOD *ui_method,
1035 void *ui_data, int *matchcount)
1037 OSSL_STORE_INFO *result = NULL;
1038 BUF_MEM *new_mem = NULL;
1039 char *new_pem_name = NULL;
1045 void *handler_ctx = NULL;
1046 const FILE_HANDLER **matching_handlers =
1047 OPENSSL_zalloc(sizeof(*matching_handlers)
1048 * OSSL_NELEM(file_handlers));
1050 if (matching_handlers == NULL) {
1051 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD_TRY_DECODE,
1052 ERR_R_MALLOC_FAILURE);
1057 for (i = 0; i < OSSL_NELEM(file_handlers); i++) {
1058 const FILE_HANDLER *handler = file_handlers[i];
1059 int try_matchcount = 0;
1060 void *tmp_handler_ctx = NULL;
1061 OSSL_STORE_INFO *tmp_result =
1062 handler->try_decode(pem_name, pem_header, data, len,
1063 &tmp_handler_ctx, &try_matchcount,
1064 ui_method, ui_data, ctx->uri,
1065 ctx->libctx, ctx->propq);
1067 if (try_matchcount > 0) {
1069 matching_handlers[*matchcount] = handler;
1072 handler->destroy_ctx(&handler_ctx);
1073 handler_ctx = tmp_handler_ctx;
1075 if ((*matchcount += try_matchcount) > 1) {
1076 /* more than one match => ambiguous, kill any result */
1077 OSSL_STORE_INFO_free(result);
1078 OSSL_STORE_INFO_free(tmp_result);
1079 if (handler->destroy_ctx != NULL)
1080 handler->destroy_ctx(&handler_ctx);
1086 result = tmp_result;
1090 if (*matchcount == 1 && matching_handlers[0]->repeatable) {
1091 ctx->_.file.last_handler = matching_handlers[0];
1092 ctx->_.file.last_handler_ctx = handler_ctx;
1095 OPENSSL_free(matching_handlers);
1099 OPENSSL_free(new_pem_name);
1100 BUF_MEM_free(new_mem);
1103 && (t = OSSL_STORE_INFO_get_type(result)) == OSSL_STORE_INFO_EMBEDDED) {
1104 pem_name = new_pem_name =
1105 ossl_store_info_get0_EMBEDDED_pem_name(result);
1106 new_mem = ossl_store_info_get0_EMBEDDED_buffer(result);
1107 data = (unsigned char *)new_mem->data;
1108 len = new_mem->length;
1109 OPENSSL_free(result);
1120 static OSSL_STORE_INFO *file_load_try_repeat(OSSL_STORE_LOADER_CTX *ctx,
1121 const UI_METHOD *ui_method,
1124 OSSL_STORE_INFO *result = NULL;
1125 int try_matchcount = 0;
1127 if (ctx->_.file.last_handler != NULL) {
1129 ctx->_.file.last_handler->try_decode(NULL, NULL, NULL, 0,
1130 &ctx->_.file.last_handler_ctx,
1132 ui_method, ui_data, ctx->uri,
1133 ctx->libctx, ctx->propq);
1135 if (result == NULL) {
1136 ctx->_.file.last_handler->destroy_ctx(&ctx->_.file.last_handler_ctx);
1137 ctx->_.file.last_handler_ctx = NULL;
1138 ctx->_.file.last_handler = NULL;
1144 static void pem_free_flag(void *pem_data, int secure, size_t num)
1147 OPENSSL_secure_clear_free(pem_data, num);
1149 OPENSSL_free(pem_data);
1151 static int file_read_pem(BIO *bp, char **pem_name, char **pem_header,
1152 unsigned char **data, long *len,
1153 const UI_METHOD *ui_method, void *ui_data,
1154 const char *uri, int secure)
1157 ? PEM_read_bio_ex(bp, pem_name, pem_header, data, len,
1158 PEM_FLAG_SECURE | PEM_FLAG_EAY_COMPATIBLE)
1159 : PEM_read_bio(bp, pem_name, pem_header, data, len);
1165 * 10 is the number of characters in "Proc-Type:", which
1166 * PEM_get_EVP_CIPHER_INFO() requires to be present.
1167 * If the PEM header has less characters than that, it's
1168 * not worth spending cycles on it.
1170 if (strlen(*pem_header) > 10) {
1171 EVP_CIPHER_INFO cipher;
1172 struct pem_pass_data pass_data;
1174 if (!PEM_get_EVP_CIPHER_INFO(*pem_header, &cipher)
1175 || !file_fill_pem_pass_data(&pass_data, "PEM pass phrase", uri,
1177 || !PEM_do_header(&cipher, *data, len, file_get_pem_pass,
1185 static OSSL_STORE_INFO *file_try_read_msblob(BIO *bp, int *matchcount)
1187 #ifdef OPENSSL_NO_DSA
1190 OSSL_STORE_INFO *result = NULL;
1194 unsigned int magic = 0, bitlen = 0;
1196 unsigned char peekbuf[16] = { 0, };
1197 const unsigned char *p = peekbuf;
1199 if (BIO_buffer_peek(bp, peekbuf, sizeof(peekbuf)) <= 0)
1201 if (!ossl_do_blob_header(&p, sizeof(peekbuf), &magic, &bitlen,
1209 EVP_PKEY *tmp = ispub
1210 ? b2i_PublicKey_bio(bp)
1211 : b2i_PrivateKey_bio(bp);
1214 || (result = OSSL_STORE_INFO_new_PKEY(tmp)) == NULL) {
1224 static OSSL_STORE_INFO *file_try_read_PVK(BIO *bp, const UI_METHOD *ui_method,
1225 void *ui_data, const char *uri,
1228 #if defined(OPENSSL_NO_DSA) || defined(OPENSSL_NO_RC4)
1231 OSSL_STORE_INFO *result = NULL;
1234 unsigned int saltlen = 0, keylen = 0;
1235 unsigned char peekbuf[24] = { 0, };
1236 const unsigned char *p = peekbuf;
1238 if (BIO_buffer_peek(bp, peekbuf, sizeof(peekbuf)) <= 0)
1240 if (!ossl_do_PVK_header(&p, sizeof(peekbuf), 0, &saltlen, &keylen))
1247 EVP_PKEY *tmp = NULL;
1248 struct pem_pass_data pass_data;
1250 if (!file_fill_pem_pass_data(&pass_data, "PVK pass phrase", uri,
1252 || (tmp = b2i_PVK_bio(bp, file_get_pem_pass, &pass_data)) == NULL
1253 || (result = OSSL_STORE_INFO_new_PKEY(tmp)) == NULL) {
1263 static int file_read_asn1(BIO *bp, unsigned char **data, long *len)
1265 BUF_MEM *mem = NULL;
1267 if (asn1_d2i_read_bio(bp, &mem) < 0)
1270 *data = (unsigned char *)mem->data;
1271 *len = (long)mem->length;
1277 static int ends_with_dirsep(const char *uri)
1280 uri += strlen(uri) - 1;
1282 if (*uri == ']' || *uri == '>' || *uri == ':')
1284 #elif defined _WIN32
1291 static int file_name_to_uri(OSSL_STORE_LOADER_CTX *ctx, const char *name,
1294 assert(name != NULL);
1295 assert(data != NULL);
1297 const char *pathsep = ends_with_dirsep(ctx->uri) ? "" : "/";
1298 long calculated_length = strlen(ctx->uri) + strlen(pathsep)
1299 + strlen(name) + 1 /* \0 */;
1301 *data = OPENSSL_zalloc(calculated_length);
1302 if (*data == NULL) {
1303 OSSL_STOREerr(OSSL_STORE_F_FILE_NAME_TO_URI, ERR_R_MALLOC_FAILURE);
1307 OPENSSL_strlcat(*data, ctx->uri, calculated_length);
1308 OPENSSL_strlcat(*data, pathsep, calculated_length);
1309 OPENSSL_strlcat(*data, name, calculated_length);
1314 static int file_name_check(OSSL_STORE_LOADER_CTX *ctx, const char *name)
1316 const char *p = NULL;
1318 /* If there are no search criteria, all names are accepted */
1319 if (ctx->_.dir.search_name[0] == '\0')
1322 /* If the expected type isn't supported, no name is accepted */
1323 if (ctx->expected_type != 0
1324 && ctx->expected_type != OSSL_STORE_INFO_CERT
1325 && ctx->expected_type != OSSL_STORE_INFO_CRL)
1329 * First, check the basename
1331 if (strncasecmp(name, ctx->_.dir.search_name,
1332 sizeof(ctx->_.dir.search_name) - 1) != 0
1333 || name[sizeof(ctx->_.dir.search_name) - 1] != '.')
1335 p = &name[sizeof(ctx->_.dir.search_name)];
1338 * Then, if the expected type is a CRL, check that the extension starts
1343 if (ctx->expected_type != 0
1344 && ctx->expected_type != OSSL_STORE_INFO_CRL)
1346 } else if (ctx->expected_type == OSSL_STORE_INFO_CRL) {
1351 * Last, check that the rest of the extension is a decimal number, at
1352 * least one digit long.
1354 if (!ossl_isdigit(*p))
1356 while (ossl_isdigit(*p))
1361 * One extra step here, check for a possible generation number.
1364 for (p++; *p != '\0'; p++)
1365 if (!ossl_isdigit(*p))
1370 * If we've reached the end of the string at this point, we've successfully
1371 * found a fitting file name.
1376 static int file_eof(OSSL_STORE_LOADER_CTX *ctx);
1377 static int file_error(OSSL_STORE_LOADER_CTX *ctx);
1378 static OSSL_STORE_INFO *file_load(OSSL_STORE_LOADER_CTX *ctx,
1379 const UI_METHOD *ui_method, void *ui_data)
1381 OSSL_STORE_INFO *result = NULL;
1386 if (ctx->type == is_dir) {
1388 char *newname = NULL;
1390 if (ctx->_.dir.last_entry == NULL) {
1391 if (!ctx->_.dir.end_reached) {
1393 assert(ctx->_.dir.last_errno != 0);
1394 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD, ERR_R_SYS_LIB);
1395 errno = ctx->_.dir.last_errno;
1397 if (openssl_strerror_r(errno, errbuf, sizeof(errbuf)))
1398 ERR_add_error_data(1, errbuf);
1403 if (ctx->_.dir.last_entry[0] != '.'
1404 && file_name_check(ctx, ctx->_.dir.last_entry)
1405 && !file_name_to_uri(ctx, ctx->_.dir.last_entry, &newname))
1409 * On the first call (with a NULL context), OPENSSL_DIR_read()
1410 * cares about the second argument. On the following calls, it
1411 * only cares that it isn't NULL. Therefore, we can safely give
1414 ctx->_.dir.last_entry = OPENSSL_DIR_read(&ctx->_.dir.ctx, ctx->uri);
1415 ctx->_.dir.last_errno = errno;
1416 if (ctx->_.dir.last_entry == NULL && ctx->_.dir.last_errno == 0)
1417 ctx->_.dir.end_reached = 1;
1420 && (result = OSSL_STORE_INFO_new_NAME(newname)) == NULL) {
1421 OPENSSL_free(newname);
1422 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD, ERR_R_OSSL_STORE_LIB);
1425 } while (result == NULL && !file_eof(ctx));
1427 int matchcount = -1;
1430 result = file_load_try_repeat(ctx, ui_method, ui_data);
1438 char *pem_name = NULL; /* PEM record name */
1439 char *pem_header = NULL; /* PEM record header */
1440 unsigned char *data = NULL; /* DER encoded data */
1441 long len = 0; /* DER encoded data length */
1444 if (ctx->type == is_pem) {
1445 if (!file_read_pem(ctx->_.file.file, &pem_name, &pem_header,
1446 &data, &len, ui_method, ui_data, ctx->uri,
1447 (ctx->flags & FILE_FLAG_SECMEM) != 0)) {
1452 if ((result = file_try_read_msblob(ctx->_.file.file,
1453 &matchcount)) != NULL
1454 || (result = file_try_read_PVK(ctx->_.file.file,
1455 ui_method, ui_data, ctx->uri,
1456 &matchcount)) != NULL)
1459 if (!file_read_asn1(ctx->_.file.file, &data, &len)) {
1465 result = file_load_try_decode(ctx, pem_name, pem_header, data, len,
1466 ui_method, ui_data, &matchcount);
1472 * If a PEM name matches more than one handler, the handlers are
1475 if (!ossl_assert(pem_name == NULL || matchcount <= 1)) {
1480 if (matchcount > 1) {
1481 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD,
1482 OSSL_STORE_R_AMBIGUOUS_CONTENT_TYPE);
1483 } else if (matchcount == 1) {
1485 * If there are other errors on the stack, they already show
1486 * what the problem is.
1488 if (ERR_peek_error() == 0) {
1489 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD,
1490 OSSL_STORE_R_UNSUPPORTED_CONTENT_TYPE);
1491 if (pem_name != NULL)
1492 ERR_add_error_data(3, "PEM type is '", pem_name, "'");
1499 pem_free_flag(pem_name, (ctx->flags & FILE_FLAG_SECMEM) != 0, 0);
1500 pem_free_flag(pem_header, (ctx->flags & FILE_FLAG_SECMEM) != 0, 0);
1501 pem_free_flag(data, (ctx->flags & FILE_FLAG_SECMEM) != 0, len);
1502 } while (matchcount == 0 && !file_eof(ctx) && !file_error(ctx));
1504 /* We bail out on ambiguity */
1509 && ctx->expected_type != 0
1510 && ctx->expected_type != OSSL_STORE_INFO_get_type(result)) {
1511 OSSL_STORE_INFO_free(result);
1519 static int file_error(OSSL_STORE_LOADER_CTX *ctx)
1521 return ctx->errcnt > 0;
1524 static int file_eof(OSSL_STORE_LOADER_CTX *ctx)
1526 if (ctx->type == is_dir)
1527 return ctx->_.dir.end_reached;
1529 if (ctx->_.file.last_handler != NULL
1530 && !ctx->_.file.last_handler->eof(ctx->_.file.last_handler_ctx))
1532 return BIO_eof(ctx->_.file.file);
1535 static int file_close(OSSL_STORE_LOADER_CTX *ctx)
1537 if ((ctx->flags & FILE_FLAG_ATTACHED) == 0) {
1538 if (ctx->type == is_dir)
1539 OPENSSL_DIR_end(&ctx->_.dir.ctx);
1541 BIO_free_all(ctx->_.file.file);
1544 * Because file_attach() called file_find_type(), we know that a
1545 * BIO_f_buffer() has been pushed on top of the regular BIO.
1547 BIO *buff = ctx->_.file.file;
1550 (void)BIO_pop(ctx->_.file.file);
1551 /* Safety measure */
1552 ctx->_.file.file = NULL;
1556 OSSL_STORE_LOADER_CTX_free(ctx);
1560 static OSSL_STORE_LOADER file_loader =
1575 static void store_file_loader_deinit(void)
1577 ossl_store_unregister_loader_int(file_loader.scheme);
1580 int ossl_store_file_loader_init(void)
1582 int ret = ossl_store_register_loader_int(&file_loader);
1584 OPENSSL_atexit(store_file_loader_deinit);