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
10 /* We need to use some engine deprecated APIs */
11 #define OPENSSL_SUPPRESS_DEPRECATED
19 #include <openssl/bio.h>
20 #include <openssl/dsa.h> /* For d2i_DSAPrivateKey */
21 #include <openssl/err.h>
22 #include <openssl/evp.h>
23 #include <openssl/pem.h>
24 #include "internal/pem_int.h"
25 #include <openssl/pkcs12.h> /* For the PKCS8 stuff o.O */
26 #include <openssl/rsa.h> /* For d2i_RSAPrivateKey */
27 #include <openssl/safestack.h>
28 #include <openssl/store.h>
29 #include <openssl/ui.h>
30 #include <openssl/x509.h> /* For the PKCS8 stuff o.O */
31 #include "crypto/asn1.h"
32 #include "crypto/ctype.h"
33 #include "internal/o_dir.h"
34 #include "internal/cryptlib.h"
35 #include "crypto/store.h"
36 #include "crypto/evp.h"
37 #include "store_local.h"
46 # define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
54 static char *file_get_pass(const UI_METHOD *ui_method, char *pass,
55 size_t maxsize, const char *desc, const char *info,
62 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS, ERR_R_MALLOC_FAILURE);
66 if (ui_method != NULL)
67 UI_set_method(ui, ui_method);
68 UI_add_user_data(ui, data);
70 if ((prompt = UI_construct_prompt(ui, desc, info)) == NULL) {
71 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS, ERR_R_MALLOC_FAILURE);
73 } else if (!UI_add_input_string(ui, prompt, UI_INPUT_FLAG_DEFAULT_PWD,
74 pass, 0, maxsize - 1)) {
75 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS, ERR_R_UI_LIB);
78 switch (UI_process(ui)) {
80 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS,
81 OSSL_STORE_R_UI_PROCESS_INTERRUPTED_OR_CANCELLED);
85 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS, ERR_R_UI_LIB);
98 struct pem_pass_data {
99 const UI_METHOD *ui_method;
101 const char *prompt_desc;
102 const char *prompt_info;
105 static int file_fill_pem_pass_data(struct pem_pass_data *pass_data,
106 const char *desc, const char *info,
107 const UI_METHOD *ui_method, void *ui_data)
109 if (pass_data == NULL)
111 pass_data->ui_method = ui_method;
112 pass_data->data = ui_data;
113 pass_data->prompt_desc = desc;
114 pass_data->prompt_info = info;
118 /* This is used anywhere a pem_password_cb is needed */
119 static int file_get_pem_pass(char *buf, int num, int w, void *data)
121 struct pem_pass_data *pass_data = data;
122 char *pass = file_get_pass(pass_data->ui_method, buf, num,
123 pass_data->prompt_desc, pass_data->prompt_info,
126 return pass == NULL ? 0 : strlen(pass);
130 * The file scheme decoders
131 * ------------------------
133 * Each possible data type has its own decoder, which either operates
134 * through a given PEM name, or attempts to decode to see if the blob
135 * it's given is decodable for its data type. The assumption is that
136 * only the correct data type will match the content.
140 * The try_decode function is called to check if the blob of data can
141 * be used by this handler, and if it can, decodes it into a supported
142 * OpenSSL type and returns a OSSL_STORE_INFO with the decoded data.
144 * pem_name: If this blob comes from a PEM file, this holds
145 * the PEM name. If it comes from another type of
146 * file, this is NULL.
147 * pem_header: If this blob comes from a PEM file, this holds
148 * the PEM headers. If it comes from another type of
149 * file, this is NULL.
150 * blob: The blob of data to match with what this handler
152 * len: The length of the blob.
153 * handler_ctx: For a handler marked repeatable, this pointer can
154 * be used to create a context for the handler. IT IS
155 * THE HANDLER'S RESPONSIBILITY TO CREATE AND DESTROY
156 * THIS CONTEXT APPROPRIATELY, i.e. create on first call
157 * and destroy when about to return NULL.
158 * matchcount: A pointer to an int to count matches for this data.
159 * Usually becomes 0 (no match) or 1 (match!), but may
160 * be higher in the (unlikely) event that the data matches
161 * more than one possibility. The int will always be
162 * zero when the function is called.
163 * ui_method: Application UI method for getting a password, pin
164 * or any other interactive data.
165 * ui_data: Application data to be passed to ui_method when
167 * libctx: The library context to be used if applicable
168 * propq: The property query string for any algorithm fetches
172 typedef OSSL_STORE_INFO *(*file_try_decode_fn)(const char *pem_name,
173 const char *pem_header,
174 const unsigned char *blob,
175 size_t len, void **handler_ctx,
177 const UI_METHOD *ui_method,
178 void *ui_data, const char *uri,
182 * The eof function should return 1 if there's no more data to be found
183 * with the handler_ctx, otherwise 0. This is only used when the handler is
186 typedef int (*file_eof_fn)(void *handler_ctx);
188 * The destroy_ctx function is used to destroy the handler_ctx that was
189 * initiated by a repeatable try_decode function. This is only used when
190 * the handler is marked repeatable.
192 typedef void (*file_destroy_ctx_fn)(void **handler_ctx);
194 typedef struct file_handler_st {
196 file_try_decode_fn try_decode;
198 file_destroy_ctx_fn destroy_ctx;
205 * PKCS#12 decoder. It operates by decoding all of the blob content,
206 * extracting all the interesting data from it and storing them internally,
207 * then serving them one piece at a time.
209 static OSSL_STORE_INFO *try_decode_PKCS12(const char *pem_name,
210 const char *pem_header,
211 const unsigned char *blob,
212 size_t len, void **pctx,
214 const UI_METHOD *ui_method,
215 void *ui_data, const char *uri,
219 OSSL_STORE_INFO *store_info = NULL;
220 STACK_OF(OSSL_STORE_INFO) *ctx = *pctx;
223 /* Initial parsing */
226 if (pem_name != NULL)
227 /* No match, there is no PEM PKCS12 tag */
230 if ((p12 = d2i_PKCS12(NULL, &blob, len)) != NULL) {
232 char tpass[PEM_BUFSIZE];
233 EVP_PKEY *pkey = NULL;
235 STACK_OF(X509) *chain = NULL;
239 if (PKCS12_verify_mac(p12, "", 0)
240 || PKCS12_verify_mac(p12, NULL, 0)) {
243 if ((pass = file_get_pass(ui_method, tpass, PEM_BUFSIZE,
244 "PKCS12 import pass phrase", uri,
246 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS12,
247 OSSL_STORE_R_PASSPHRASE_CALLBACK_ERROR);
250 if (!PKCS12_verify_mac(p12, pass, strlen(pass))) {
251 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS12,
252 OSSL_STORE_R_ERROR_VERIFYING_PKCS12_MAC);
257 if (PKCS12_parse(p12, pass, &pkey, &cert, &chain)) {
258 OSSL_STORE_INFO *osi_pkey = NULL;
259 OSSL_STORE_INFO *osi_cert = NULL;
260 OSSL_STORE_INFO *osi_ca = NULL;
263 if ((ctx = sk_OSSL_STORE_INFO_new_null()) != NULL) {
265 if ((osi_pkey = OSSL_STORE_INFO_new_PKEY(pkey)) != NULL
266 /* clearing pkey here avoids case distinctions */
267 && (pkey = NULL) == NULL
268 && sk_OSSL_STORE_INFO_push(ctx, osi_pkey) != 0)
273 if (ok && cert != NULL) {
274 if ((osi_cert = OSSL_STORE_INFO_new_CERT(cert)) != NULL
275 /* clearing cert here avoids case distinctions */
276 && (cert = NULL) == NULL
277 && sk_OSSL_STORE_INFO_push(ctx, osi_cert) != 0)
282 while (ok && sk_X509_num(chain) > 0) {
283 X509 *ca = sk_X509_value(chain, 0);
285 if ((osi_ca = OSSL_STORE_INFO_new_CERT(ca)) != NULL
286 && sk_X509_shift(chain) != NULL
287 && sk_OSSL_STORE_INFO_push(ctx, osi_ca) != 0)
295 sk_X509_pop_free(chain, X509_free);
296 OSSL_STORE_INFO_free(osi_pkey);
297 OSSL_STORE_INFO_free(osi_cert);
298 OSSL_STORE_INFO_free(osi_ca);
300 sk_OSSL_STORE_INFO_pop_free(ctx, OSSL_STORE_INFO_free);
313 store_info = sk_OSSL_STORE_INFO_shift(ctx);
317 static int eof_PKCS12(void *ctx_)
319 STACK_OF(OSSL_STORE_INFO) *ctx = ctx_;
321 return ctx == NULL || sk_OSSL_STORE_INFO_num(ctx) == 0;
324 static void destroy_ctx_PKCS12(void **pctx)
326 STACK_OF(OSSL_STORE_INFO) *ctx = *pctx;
328 sk_OSSL_STORE_INFO_pop_free(ctx, OSSL_STORE_INFO_free);
332 static FILE_HANDLER PKCS12_handler = {
341 * Encrypted PKCS#8 decoder. It operates by just decrypting the given blob
342 * into a new blob, which is returned as an EMBEDDED STORE_INFO. The whole
343 * decoding process will then start over with the new blob.
345 static OSSL_STORE_INFO *try_decode_PKCS8Encrypted(const char *pem_name,
346 const char *pem_header,
347 const unsigned char *blob,
348 size_t len, void **pctx,
350 const UI_METHOD *ui_method,
357 char kbuf[PEM_BUFSIZE];
359 const X509_ALGOR *dalg = NULL;
360 const ASN1_OCTET_STRING *doct = NULL;
361 OSSL_STORE_INFO *store_info = NULL;
363 unsigned char *new_data = NULL;
366 if (pem_name != NULL) {
367 if (strcmp(pem_name, PEM_STRING_PKCS8) != 0)
372 if ((p8 = d2i_X509_SIG(NULL, &blob, len)) == NULL)
377 if ((mem = BUF_MEM_new()) == NULL) {
378 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS8ENCRYPTED,
379 ERR_R_MALLOC_FAILURE);
383 if ((pass = file_get_pass(ui_method, kbuf, PEM_BUFSIZE,
384 "PKCS8 decrypt pass phrase", uri,
386 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS8ENCRYPTED,
387 OSSL_STORE_R_BAD_PASSWORD_READ);
391 X509_SIG_get0(p8, &dalg, &doct);
392 if (!PKCS12_pbe_crypt(dalg, pass, strlen(pass), doct->data, doct->length,
393 &new_data, &new_data_len, 0))
396 mem->data = (char *)new_data;
397 mem->max = mem->length = (size_t)new_data_len;
400 store_info = ossl_store_info_new_EMBEDDED(PEM_STRING_PKCS8INF, mem);
401 if (store_info == NULL) {
402 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS8ENCRYPTED,
403 ERR_R_MALLOC_FAILURE);
414 static FILE_HANDLER PKCS8Encrypted_handler = {
416 try_decode_PKCS8Encrypted
420 * Private key decoder. Decodes all sorts of private keys, both PKCS#8
421 * encoded ones and old style PEM ones (with the key type is encoded into
424 int pem_check_suffix(const char *pem_str, const char *suffix);
425 static OSSL_STORE_INFO *try_decode_PrivateKey(const char *pem_name,
426 const char *pem_header,
427 const unsigned char *blob,
428 size_t len, void **pctx,
430 const UI_METHOD *ui_method,
431 void *ui_data, const char *uri,
435 OSSL_STORE_INFO *store_info = NULL;
436 EVP_PKEY *pkey = NULL;
437 const EVP_PKEY_ASN1_METHOD *ameth = NULL;
439 if (pem_name != NULL) {
440 if (strcmp(pem_name, PEM_STRING_PKCS8INF) == 0) {
441 PKCS8_PRIV_KEY_INFO *p8inf =
442 d2i_PKCS8_PRIV_KEY_INFO(NULL, &blob, len);
446 pkey = evp_pkcs82pkey_int(p8inf, libctx, propq);
447 PKCS8_PRIV_KEY_INFO_free(p8inf);
451 if ((slen = pem_check_suffix(pem_name, "PRIVATE KEY")) > 0
452 && (ameth = EVP_PKEY_asn1_find_str(NULL, pem_name,
455 pkey = d2i_PrivateKey_ex(ameth->pkey_id, NULL, &blob, len,
461 #ifndef OPENSSL_NO_ENGINE
462 ENGINE *curengine = ENGINE_get_first();
464 while (curengine != NULL) {
465 ENGINE_PKEY_ASN1_METHS_PTR asn1meths =
466 ENGINE_get_pkey_asn1_meths(curengine);
468 if (asn1meths != NULL) {
469 const int *nids = NULL;
470 int nids_n = asn1meths(curengine, NULL, &nids, 0);
472 for (i = 0; i < nids_n; i++) {
473 EVP_PKEY_ASN1_METHOD *ameth2 = NULL;
474 EVP_PKEY *tmp_pkey = NULL;
475 const unsigned char *tmp_blob = blob;
477 if (!asn1meths(curengine, &ameth2, NULL, nids[i]))
480 || ameth2->pkey_flags & ASN1_PKEY_ALIAS)
484 d2i_PrivateKey_ex(ameth2->pkey_id, NULL,
485 &tmp_blob, len, libctx, propq);
486 if (tmp_pkey != NULL) {
488 EVP_PKEY_free(tmp_pkey);
495 curengine = ENGINE_get_next(curengine);
499 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
500 EVP_PKEY *tmp_pkey = NULL;
501 const unsigned char *tmp_blob = blob;
503 ameth = EVP_PKEY_asn1_get0(i);
504 if (ameth->pkey_flags & ASN1_PKEY_ALIAS)
507 tmp_pkey = d2i_PrivateKey_ex(ameth->pkey_id, NULL, &tmp_blob, len,
509 if (tmp_pkey != NULL) {
511 EVP_PKEY_free(tmp_pkey);
518 if (*matchcount > 1) {
527 store_info = OSSL_STORE_INFO_new_PKEY(pkey);
528 if (store_info == NULL)
534 static FILE_HANDLER PrivateKey_handler = {
536 try_decode_PrivateKey
540 * Public key decoder. Only supports SubjectPublicKeyInfo formatted keys.
542 static OSSL_STORE_INFO *try_decode_PUBKEY(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,
548 void *ui_data, const char *uri,
552 OSSL_STORE_INFO *store_info = NULL;
553 EVP_PKEY *pkey = NULL;
555 if (pem_name != NULL) {
556 if (strcmp(pem_name, PEM_STRING_PUBLIC) != 0)
562 if ((pkey = d2i_PUBKEY(NULL, &blob, len)) != NULL) {
564 store_info = OSSL_STORE_INFO_new_PKEY(pkey);
570 static FILE_HANDLER PUBKEY_handler = {
576 * Key parameter decoder.
578 static OSSL_STORE_INFO *try_decode_params(const char *pem_name,
579 const char *pem_header,
580 const unsigned char *blob,
581 size_t len, void **pctx,
583 const UI_METHOD *ui_method,
584 void *ui_data, const char *uri,
588 OSSL_STORE_INFO *store_info = NULL;
590 EVP_PKEY *pkey = NULL;
591 const EVP_PKEY_ASN1_METHOD *ameth = NULL;
594 if (pem_name != NULL) {
595 if ((slen = pem_check_suffix(pem_name, "PARAMETERS")) == 0)
601 if ((pkey = EVP_PKEY_new()) == NULL) {
602 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PARAMS, ERR_R_EVP_LIB);
607 if (EVP_PKEY_set_type_str(pkey, pem_name, slen)
608 && (ameth = EVP_PKEY_get0_asn1(pkey)) != NULL
609 && ameth->param_decode != NULL
610 && ameth->param_decode(pkey, &blob, len))
614 EVP_PKEY *tmp_pkey = NULL;
616 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
617 const unsigned char *tmp_blob = blob;
619 if (tmp_pkey == NULL && (tmp_pkey = EVP_PKEY_new()) == NULL) {
620 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PARAMS, ERR_R_EVP_LIB);
624 ameth = EVP_PKEY_asn1_get0(i);
625 if (ameth->pkey_flags & ASN1_PKEY_ALIAS)
628 if (EVP_PKEY_set_type(tmp_pkey, ameth->pkey_id)
629 && (ameth = EVP_PKEY_get0_asn1(tmp_pkey)) != NULL
630 && ameth->param_decode != NULL
631 && ameth->param_decode(tmp_pkey, &tmp_blob, len)) {
633 EVP_PKEY_free(tmp_pkey);
641 EVP_PKEY_free(tmp_pkey);
642 if (*matchcount == 1) {
648 store_info = OSSL_STORE_INFO_new_PARAMS(pkey);
649 if (store_info == NULL)
655 static FILE_HANDLER params_handler = {
661 * X.509 certificate decoder.
663 static OSSL_STORE_INFO *try_decode_X509Certificate(const char *pem_name,
664 const char *pem_header,
665 const unsigned char *blob,
666 size_t len, void **pctx,
668 const UI_METHOD *ui_method,
674 OSSL_STORE_INFO *store_info = NULL;
678 * In most cases, we can try to interpret the serialized data as a trusted
679 * cert (X509 + X509_AUX) and fall back to reading it as a normal cert
680 * (just X509), but if the PEM name specifically declares it as a trusted
681 * cert, then no fallback should be engaged. |ignore_trusted| tells if
682 * the fallback can be used (1) or not (0).
684 int ignore_trusted = 1;
686 if (pem_name != NULL) {
687 if (strcmp(pem_name, PEM_STRING_X509_TRUSTED) == 0)
689 else if (strcmp(pem_name, PEM_STRING_X509_OLD) != 0
690 && strcmp(pem_name, PEM_STRING_X509) != 0)
696 cert = X509_new_with_libctx(libctx, propq);
700 if ((d2i_X509_AUX(&cert, &blob, len)) != NULL
701 || (ignore_trusted && (d2i_X509(&cert, &blob, len)) != NULL)) {
703 store_info = OSSL_STORE_INFO_new_CERT(cert);
706 if (store_info == NULL)
712 static FILE_HANDLER X509Certificate_handler = {
714 try_decode_X509Certificate
720 static OSSL_STORE_INFO *try_decode_X509CRL(const char *pem_name,
721 const char *pem_header,
722 const unsigned char *blob,
723 size_t len, void **pctx,
725 const UI_METHOD *ui_method,
726 void *ui_data, const char *uri,
730 OSSL_STORE_INFO *store_info = NULL;
731 X509_CRL *crl = NULL;
733 if (pem_name != NULL) {
734 if (strcmp(pem_name, PEM_STRING_X509_CRL) != 0)
740 if ((crl = d2i_X509_CRL(NULL, &blob, len)) != NULL) {
742 store_info = OSSL_STORE_INFO_new_CRL(crl);
745 if (store_info == NULL)
751 static FILE_HANDLER X509CRL_handler = {
757 * To finish it all off, we collect all the handlers.
759 static const FILE_HANDLER *file_handlers[] = {
761 &PKCS8Encrypted_handler,
762 &X509Certificate_handler,
775 struct ossl_store_loader_ctx_st {
776 char *uri; /* The URI we currently try to load */
783 #define FILE_FLAG_SECMEM (1<<0)
784 #define FILE_FLAG_ATTACHED (1<<1)
787 struct { /* Used with is_raw and is_pem */
791 * The following are used when the handler is marked as
794 const FILE_HANDLER *last_handler;
795 void *last_handler_ctx;
797 struct { /* Used with is_dir */
798 OPENSSL_DIR_CTX *ctx;
802 * When a search expression is given, these are filled in.
803 * |search_name| contains the file basename to look for.
804 * The string is exactly 8 characters long.
809 * The directory reading utility we have combines opening with
810 * reading the first name. To make sure we can detect the end
811 * at the right time, we read early and cache the name.
813 const char *last_entry;
818 /* Expected object type. May be unspecified */
824 static void OSSL_STORE_LOADER_CTX_free(OSSL_STORE_LOADER_CTX *ctx)
829 OPENSSL_free(ctx->propq);
830 OPENSSL_free(ctx->uri);
831 if (ctx->type != is_dir) {
832 if (ctx->_.file.last_handler != NULL) {
833 ctx->_.file.last_handler->destroy_ctx(&ctx->_.file.last_handler_ctx);
834 ctx->_.file.last_handler_ctx = NULL;
835 ctx->_.file.last_handler = NULL;
841 static int file_find_type(OSSL_STORE_LOADER_CTX *ctx)
844 char peekbuf[4096] = { 0, };
846 if ((buff = BIO_new(BIO_f_buffer())) == NULL)
849 ctx->_.file.file = BIO_push(buff, ctx->_.file.file);
850 if (BIO_buffer_peek(ctx->_.file.file, peekbuf, sizeof(peekbuf) - 1) > 0) {
851 peekbuf[sizeof(peekbuf) - 1] = '\0';
852 if (strstr(peekbuf, "-----BEGIN ") != NULL)
858 static OSSL_STORE_LOADER_CTX *file_open_with_libctx
859 (const OSSL_STORE_LOADER *loader, const char *uri,
860 OPENSSL_CTX *libctx, const char *propq,
861 const UI_METHOD *ui_method, void *ui_data)
863 OSSL_STORE_LOADER_CTX *ctx = NULL;
867 unsigned int check_absolute:1;
869 size_t path_data_n = 0, i;
873 * First step, just take the URI as is.
875 path_data[path_data_n].check_absolute = 0;
876 path_data[path_data_n++].path = uri;
879 * Second step, if the URI appears to start with the 'file' scheme,
880 * extract the path and make that the second path to check.
881 * There's a special case if the URI also contains an authority, then
882 * the full URI shouldn't be used as a path anywhere.
884 if (strncasecmp(uri, "file:", 5) == 0) {
885 const char *p = &uri[5];
887 if (strncmp(&uri[5], "//", 2) == 0) {
888 path_data_n--; /* Invalidate using the full URI */
889 if (strncasecmp(&uri[7], "localhost/", 10) == 0) {
891 } else if (uri[7] == '/') {
894 OSSL_STOREerr(0, OSSL_STORE_R_URI_AUTHORITY_UNSUPPORTED);
899 path_data[path_data_n].check_absolute = 1;
901 /* Windows file: URIs with a drive letter start with a / */
902 if (p[0] == '/' && p[2] == ':' && p[3] == '/') {
903 char c = ossl_tolower(p[1]);
905 if (c >= 'a' && c <= 'z') {
907 /* We know it's absolute, so no need to check */
908 path_data[path_data_n].check_absolute = 0;
912 path_data[path_data_n++].path = p;
916 for (i = 0, path = NULL; path == NULL && i < path_data_n; i++) {
918 * If the scheme "file" was an explicit part of the URI, the path must
919 * be absolute. So says RFC 8089
921 if (path_data[i].check_absolute && path_data[i].path[0] != '/') {
922 OSSL_STOREerr(0, OSSL_STORE_R_PATH_MUST_BE_ABSOLUTE);
923 ERR_add_error_data(1, path_data[i].path);
927 if (stat(path_data[i].path, &st) < 0) {
928 ERR_raise_data(ERR_LIB_SYS, errno,
932 path = path_data[i].path;
939 /* Successfully found a working path, clear possible collected errors */
942 ctx = OPENSSL_zalloc(sizeof(*ctx));
944 OSSL_STOREerr(0, ERR_R_MALLOC_FAILURE);
947 ctx->uri = OPENSSL_strdup(uri);
948 if (ctx->uri == NULL) {
949 OSSL_STOREerr(0, ERR_R_MALLOC_FAILURE);
953 if (S_ISDIR(st.st_mode)) {
955 ctx->_.dir.last_entry = OPENSSL_DIR_read(&ctx->_.dir.ctx, path);
956 ctx->_.dir.last_errno = errno;
957 if (ctx->_.dir.last_entry == NULL) {
958 if (ctx->_.dir.last_errno != 0) {
960 OSSL_STOREerr(0, ERR_R_SYS_LIB);
961 errno = ctx->_.dir.last_errno;
962 if (openssl_strerror_r(errno, errbuf, sizeof(errbuf)))
963 ERR_add_error_data(1, errbuf);
966 ctx->_.dir.end_reached = 1;
968 } else if ((ctx->_.file.file = BIO_new_file(path, "rb")) == NULL
969 || !file_find_type(ctx)) {
970 BIO_free_all(ctx->_.file.file);
974 ctx->propq = OPENSSL_strdup(propq);
975 if (ctx->propq == NULL) {
976 OSSL_STOREerr(0, ERR_R_MALLOC_FAILURE);
980 ctx->libctx = libctx;
984 OSSL_STORE_LOADER_CTX_free(ctx);
988 static OSSL_STORE_LOADER_CTX *file_open
989 (const OSSL_STORE_LOADER *loader, const char *uri,
990 const UI_METHOD *ui_method, void *ui_data)
992 return file_open_with_libctx(loader, uri, NULL, NULL, ui_method, ui_data);
995 static OSSL_STORE_LOADER_CTX *file_attach
996 (const OSSL_STORE_LOADER *loader, BIO *bp,
997 OPENSSL_CTX *libctx, const char *propq,
998 const UI_METHOD *ui_method, void *ui_data)
1000 OSSL_STORE_LOADER_CTX *ctx = NULL;
1002 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) {
1003 OSSL_STOREerr(0, ERR_R_MALLOC_FAILURE);
1007 if (propq != NULL) {
1008 ctx->propq = OPENSSL_strdup(propq);
1009 if (ctx->propq == NULL) {
1010 OSSL_STOREerr(0, ERR_R_MALLOC_FAILURE);
1014 ctx->libctx = libctx;
1015 ctx->flags |= FILE_FLAG_ATTACHED;
1016 ctx->_.file.file = bp;
1017 if (!file_find_type(ctx)) {
1018 /* Safety measure */
1019 ctx->_.file.file = NULL;
1024 OSSL_STORE_LOADER_CTX_free(ctx);
1028 static int file_ctrl(OSSL_STORE_LOADER_CTX *ctx, int cmd, va_list args)
1033 case OSSL_STORE_C_USE_SECMEM:
1035 int on = *(va_arg(args, int *));
1039 ctx->flags &= ~FILE_FLAG_SECMEM;
1042 ctx->flags |= FILE_FLAG_SECMEM;
1045 OSSL_STOREerr(0, ERR_R_PASSED_INVALID_ARGUMENT);
1058 static int file_expect(OSSL_STORE_LOADER_CTX *ctx, int expected)
1060 ctx->expected_type = expected;
1064 static int file_find(OSSL_STORE_LOADER_CTX *ctx,
1065 const OSSL_STORE_SEARCH *search)
1068 * If ctx == NULL, the library is looking to know if this loader supports
1069 * the given search type.
1072 if (OSSL_STORE_SEARCH_get_type(search) == OSSL_STORE_SEARCH_BY_NAME) {
1073 unsigned long hash = 0;
1078 if (ctx->type != is_dir) {
1079 OSSL_STOREerr(OSSL_STORE_F_FILE_FIND,
1080 OSSL_STORE_R_SEARCH_ONLY_SUPPORTED_FOR_DIRECTORIES);
1084 hash = X509_NAME_hash(OSSL_STORE_SEARCH_get0_name(search));
1085 BIO_snprintf(ctx->_.dir.search_name, sizeof(ctx->_.dir.search_name),
1091 OSSL_STOREerr(OSSL_STORE_F_FILE_FIND,
1092 OSSL_STORE_R_UNSUPPORTED_SEARCH_TYPE);
1096 static OSSL_STORE_INFO *file_load_try_decode(OSSL_STORE_LOADER_CTX *ctx,
1097 const char *pem_name,
1098 const char *pem_header,
1099 unsigned char *data, size_t len,
1100 const UI_METHOD *ui_method,
1101 void *ui_data, int *matchcount)
1103 OSSL_STORE_INFO *result = NULL;
1104 BUF_MEM *new_mem = NULL;
1105 char *new_pem_name = NULL;
1111 void *handler_ctx = NULL;
1112 const FILE_HANDLER **matching_handlers =
1113 OPENSSL_zalloc(sizeof(*matching_handlers)
1114 * OSSL_NELEM(file_handlers));
1116 if (matching_handlers == NULL) {
1117 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD_TRY_DECODE,
1118 ERR_R_MALLOC_FAILURE);
1123 for (i = 0; i < OSSL_NELEM(file_handlers); i++) {
1124 const FILE_HANDLER *handler = file_handlers[i];
1125 int try_matchcount = 0;
1126 void *tmp_handler_ctx = NULL;
1127 OSSL_STORE_INFO *tmp_result =
1128 handler->try_decode(pem_name, pem_header, data, len,
1129 &tmp_handler_ctx, &try_matchcount,
1130 ui_method, ui_data, ctx->uri,
1131 ctx->libctx, ctx->propq);
1133 if (try_matchcount > 0) {
1135 matching_handlers[*matchcount] = handler;
1138 handler->destroy_ctx(&handler_ctx);
1139 handler_ctx = tmp_handler_ctx;
1141 if ((*matchcount += try_matchcount) > 1) {
1142 /* more than one match => ambiguous, kill any result */
1143 OSSL_STORE_INFO_free(result);
1144 OSSL_STORE_INFO_free(tmp_result);
1145 if (handler->destroy_ctx != NULL)
1146 handler->destroy_ctx(&handler_ctx);
1152 result = tmp_result;
1156 if (*matchcount == 1 && matching_handlers[0]->repeatable) {
1157 ctx->_.file.last_handler = matching_handlers[0];
1158 ctx->_.file.last_handler_ctx = handler_ctx;
1161 OPENSSL_free(matching_handlers);
1165 OPENSSL_free(new_pem_name);
1166 BUF_MEM_free(new_mem);
1169 && (t = OSSL_STORE_INFO_get_type(result)) == OSSL_STORE_INFO_EMBEDDED) {
1170 pem_name = new_pem_name =
1171 ossl_store_info_get0_EMBEDDED_pem_name(result);
1172 new_mem = ossl_store_info_get0_EMBEDDED_buffer(result);
1173 data = (unsigned char *)new_mem->data;
1174 len = new_mem->length;
1175 OPENSSL_free(result);
1186 static OSSL_STORE_INFO *file_load_try_repeat(OSSL_STORE_LOADER_CTX *ctx,
1187 const UI_METHOD *ui_method,
1190 OSSL_STORE_INFO *result = NULL;
1191 int try_matchcount = 0;
1193 if (ctx->_.file.last_handler != NULL) {
1195 ctx->_.file.last_handler->try_decode(NULL, NULL, NULL, 0,
1196 &ctx->_.file.last_handler_ctx,
1198 ui_method, ui_data, ctx->uri,
1199 ctx->libctx, ctx->propq);
1201 if (result == NULL) {
1202 ctx->_.file.last_handler->destroy_ctx(&ctx->_.file.last_handler_ctx);
1203 ctx->_.file.last_handler_ctx = NULL;
1204 ctx->_.file.last_handler = NULL;
1210 static void pem_free_flag(void *pem_data, int secure, size_t num)
1213 OPENSSL_secure_clear_free(pem_data, num);
1215 OPENSSL_free(pem_data);
1217 static int file_read_pem(BIO *bp, char **pem_name, char **pem_header,
1218 unsigned char **data, long *len,
1219 const UI_METHOD *ui_method, void *ui_data,
1220 const char *uri, int secure)
1223 ? PEM_read_bio_ex(bp, pem_name, pem_header, data, len,
1224 PEM_FLAG_SECURE | PEM_FLAG_EAY_COMPATIBLE)
1225 : PEM_read_bio(bp, pem_name, pem_header, data, len);
1231 * 10 is the number of characters in "Proc-Type:", which
1232 * PEM_get_EVP_CIPHER_INFO() requires to be present.
1233 * If the PEM header has less characters than that, it's
1234 * not worth spending cycles on it.
1236 if (strlen(*pem_header) > 10) {
1237 EVP_CIPHER_INFO cipher;
1238 struct pem_pass_data pass_data;
1240 if (!PEM_get_EVP_CIPHER_INFO(*pem_header, &cipher)
1241 || !file_fill_pem_pass_data(&pass_data, "PEM pass phrase", uri,
1243 || !PEM_do_header(&cipher, *data, len, file_get_pem_pass,
1251 static OSSL_STORE_INFO *file_try_read_msblob(BIO *bp, int *matchcount)
1253 #ifdef OPENSSL_NO_DSA
1256 OSSL_STORE_INFO *result = NULL;
1260 unsigned int magic = 0, bitlen = 0;
1262 unsigned char peekbuf[16] = { 0, };
1263 const unsigned char *p = peekbuf;
1265 if (BIO_buffer_peek(bp, peekbuf, sizeof(peekbuf)) <= 0)
1267 if (!ossl_do_blob_header(&p, sizeof(peekbuf), &magic, &bitlen,
1275 EVP_PKEY *tmp = ispub
1276 ? b2i_PublicKey_bio(bp)
1277 : b2i_PrivateKey_bio(bp);
1280 || (result = OSSL_STORE_INFO_new_PKEY(tmp)) == NULL) {
1290 static OSSL_STORE_INFO *file_try_read_PVK(BIO *bp, const UI_METHOD *ui_method,
1291 void *ui_data, const char *uri,
1294 #if defined(OPENSSL_NO_DSA) || defined(OPENSSL_NO_RC4)
1297 OSSL_STORE_INFO *result = NULL;
1300 unsigned int saltlen = 0, keylen = 0;
1301 unsigned char peekbuf[24] = { 0, };
1302 const unsigned char *p = peekbuf;
1304 if (BIO_buffer_peek(bp, peekbuf, sizeof(peekbuf)) <= 0)
1306 if (!ossl_do_PVK_header(&p, sizeof(peekbuf), 0, &saltlen, &keylen))
1313 EVP_PKEY *tmp = NULL;
1314 struct pem_pass_data pass_data;
1316 if (!file_fill_pem_pass_data(&pass_data, "PVK pass phrase", uri,
1318 || (tmp = b2i_PVK_bio(bp, file_get_pem_pass, &pass_data)) == NULL
1319 || (result = OSSL_STORE_INFO_new_PKEY(tmp)) == NULL) {
1329 static int file_read_asn1(BIO *bp, unsigned char **data, long *len)
1331 BUF_MEM *mem = NULL;
1333 if (asn1_d2i_read_bio(bp, &mem) < 0)
1336 *data = (unsigned char *)mem->data;
1337 *len = (long)mem->length;
1343 static int ends_with_dirsep(const char *uri)
1346 uri += strlen(uri) - 1;
1348 if (*uri == ']' || *uri == '>' || *uri == ':')
1350 #elif defined(_WIN32)
1357 static int file_name_to_uri(OSSL_STORE_LOADER_CTX *ctx, const char *name,
1360 assert(name != NULL);
1361 assert(data != NULL);
1363 const char *pathsep = ends_with_dirsep(ctx->uri) ? "" : "/";
1364 long calculated_length = strlen(ctx->uri) + strlen(pathsep)
1365 + strlen(name) + 1 /* \0 */;
1367 *data = OPENSSL_zalloc(calculated_length);
1368 if (*data == NULL) {
1369 OSSL_STOREerr(OSSL_STORE_F_FILE_NAME_TO_URI, ERR_R_MALLOC_FAILURE);
1373 OPENSSL_strlcat(*data, ctx->uri, calculated_length);
1374 OPENSSL_strlcat(*data, pathsep, calculated_length);
1375 OPENSSL_strlcat(*data, name, calculated_length);
1380 static int file_name_check(OSSL_STORE_LOADER_CTX *ctx, const char *name)
1382 const char *p = NULL;
1384 /* If there are no search criteria, all names are accepted */
1385 if (ctx->_.dir.search_name[0] == '\0')
1388 /* If the expected type isn't supported, no name is accepted */
1389 if (ctx->expected_type != 0
1390 && ctx->expected_type != OSSL_STORE_INFO_CERT
1391 && ctx->expected_type != OSSL_STORE_INFO_CRL)
1395 * First, check the basename
1397 if (strncasecmp(name, ctx->_.dir.search_name,
1398 sizeof(ctx->_.dir.search_name) - 1) != 0
1399 || name[sizeof(ctx->_.dir.search_name) - 1] != '.')
1401 p = &name[sizeof(ctx->_.dir.search_name)];
1404 * Then, if the expected type is a CRL, check that the extension starts
1409 if (ctx->expected_type != 0
1410 && ctx->expected_type != OSSL_STORE_INFO_CRL)
1412 } else if (ctx->expected_type == OSSL_STORE_INFO_CRL) {
1417 * Last, check that the rest of the extension is a decimal number, at
1418 * least one digit long.
1420 if (!ossl_isdigit(*p))
1422 while (ossl_isdigit(*p))
1427 * One extra step here, check for a possible generation number.
1430 for (p++; *p != '\0'; p++)
1431 if (!ossl_isdigit(*p))
1436 * If we've reached the end of the string at this point, we've successfully
1437 * found a fitting file name.
1442 static int file_eof(OSSL_STORE_LOADER_CTX *ctx);
1443 static int file_error(OSSL_STORE_LOADER_CTX *ctx);
1444 static OSSL_STORE_INFO *file_load(OSSL_STORE_LOADER_CTX *ctx,
1445 const UI_METHOD *ui_method,
1448 OSSL_STORE_INFO *result = NULL;
1453 if (ctx->type == is_dir) {
1455 char *newname = NULL;
1457 if (ctx->_.dir.last_entry == NULL) {
1458 if (!ctx->_.dir.end_reached) {
1460 assert(ctx->_.dir.last_errno != 0);
1461 OSSL_STOREerr(0, ERR_R_SYS_LIB);
1462 errno = ctx->_.dir.last_errno;
1464 if (openssl_strerror_r(errno, errbuf, sizeof(errbuf)))
1465 ERR_add_error_data(1, errbuf);
1470 if (ctx->_.dir.last_entry[0] != '.'
1471 && file_name_check(ctx, ctx->_.dir.last_entry)
1472 && !file_name_to_uri(ctx, ctx->_.dir.last_entry, &newname))
1476 * On the first call (with a NULL context), OPENSSL_DIR_read()
1477 * cares about the second argument. On the following calls, it
1478 * only cares that it isn't NULL. Therefore, we can safely give
1481 ctx->_.dir.last_entry = OPENSSL_DIR_read(&ctx->_.dir.ctx, ctx->uri);
1482 ctx->_.dir.last_errno = errno;
1483 if (ctx->_.dir.last_entry == NULL && ctx->_.dir.last_errno == 0)
1484 ctx->_.dir.end_reached = 1;
1487 && (result = OSSL_STORE_INFO_new_NAME(newname)) == NULL) {
1488 OPENSSL_free(newname);
1489 OSSL_STOREerr(0, ERR_R_OSSL_STORE_LIB);
1492 } while (result == NULL && !file_eof(ctx));
1494 int matchcount = -1;
1497 result = file_load_try_repeat(ctx, ui_method, ui_data);
1505 char *pem_name = NULL; /* PEM record name */
1506 char *pem_header = NULL; /* PEM record header */
1507 unsigned char *data = NULL; /* DER encoded data */
1508 long len = 0; /* DER encoded data length */
1511 if (ctx->type == is_pem) {
1512 if (!file_read_pem(ctx->_.file.file, &pem_name, &pem_header,
1513 &data, &len, ui_method, ui_data, ctx->uri,
1514 (ctx->flags & FILE_FLAG_SECMEM) != 0)) {
1519 if ((result = file_try_read_msblob(ctx->_.file.file,
1520 &matchcount)) != NULL
1521 || (result = file_try_read_PVK(ctx->_.file.file,
1522 ui_method, ui_data, ctx->uri,
1523 &matchcount)) != NULL)
1526 if (!file_read_asn1(ctx->_.file.file, &data, &len)) {
1532 result = file_load_try_decode(ctx, pem_name, pem_header, data, len,
1533 ui_method, ui_data, &matchcount);
1539 * If a PEM name matches more than one handler, the handlers are
1542 if (!ossl_assert(pem_name == NULL || matchcount <= 1)) {
1547 if (matchcount > 1) {
1548 OSSL_STOREerr(0, OSSL_STORE_R_AMBIGUOUS_CONTENT_TYPE);
1549 } else if (matchcount == 1) {
1551 * If there are other errors on the stack, they already show
1552 * what the problem is.
1554 if (ERR_peek_error() == 0) {
1555 OSSL_STOREerr(0, OSSL_STORE_R_UNSUPPORTED_CONTENT_TYPE);
1556 if (pem_name != NULL)
1557 ERR_add_error_data(3, "PEM type is '", pem_name, "'");
1564 pem_free_flag(pem_name, (ctx->flags & FILE_FLAG_SECMEM) != 0, 0);
1565 pem_free_flag(pem_header, (ctx->flags & FILE_FLAG_SECMEM) != 0, 0);
1566 pem_free_flag(data, (ctx->flags & FILE_FLAG_SECMEM) != 0, len);
1567 } while (matchcount == 0 && !file_eof(ctx) && !file_error(ctx));
1569 /* We bail out on ambiguity */
1570 if (matchcount > 1) {
1571 OSSL_STORE_INFO_free(result);
1576 && ctx->expected_type != 0
1577 && ctx->expected_type != OSSL_STORE_INFO_get_type(result)) {
1578 OSSL_STORE_INFO_free(result);
1586 static int file_error(OSSL_STORE_LOADER_CTX *ctx)
1588 return ctx->errcnt > 0;
1591 static int file_eof(OSSL_STORE_LOADER_CTX *ctx)
1593 if (ctx->type == is_dir)
1594 return ctx->_.dir.end_reached;
1596 if (ctx->_.file.last_handler != NULL
1597 && !ctx->_.file.last_handler->eof(ctx->_.file.last_handler_ctx))
1599 return BIO_eof(ctx->_.file.file);
1602 static int file_close(OSSL_STORE_LOADER_CTX *ctx)
1604 if ((ctx->flags & FILE_FLAG_ATTACHED) == 0) {
1605 if (ctx->type == is_dir)
1606 OPENSSL_DIR_end(&ctx->_.dir.ctx);
1608 BIO_free_all(ctx->_.file.file);
1611 * Because file_attach() called file_find_type(), we know that a
1612 * BIO_f_buffer() has been pushed on top of the regular BIO.
1614 BIO *buff = ctx->_.file.file;
1617 (void)BIO_pop(ctx->_.file.file);
1618 /* Safety measure */
1619 ctx->_.file.file = NULL;
1623 OSSL_STORE_LOADER_CTX_free(ctx);
1627 static OSSL_STORE_LOADER file_loader =
1640 file_open_with_libctx,
1643 static void store_file_loader_deinit(void)
1645 ossl_store_unregister_loader_int(file_loader.scheme);
1648 int ossl_store_file_loader_init(void)
1650 int ret = ossl_store_register_loader_int(&file_loader);
1652 OPENSSL_atexit(store_file_loader_deinit);