2 * Copyright 2016-2021 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 /* THIS ENGINE IS FOR TESTING PURPOSES ONLY. */
12 /* This file has quite some overlap with providers/implementations/storemgmt/file_store.c */
14 /* We need to use some engine deprecated APIs */
15 #define OPENSSL_SUPPRESS_DEPRECATED
17 /* #include "e_os.h" */
23 #include <openssl/bio.h>
24 #include <openssl/dsa.h> /* For d2i_DSAPrivateKey */
25 #include <openssl/err.h>
26 #include <openssl/evp.h>
27 #include <openssl/pem.h>
28 #include <openssl/pkcs12.h> /* For the PKCS8 stuff o.O */
29 #include <openssl/rsa.h> /* For d2i_RSAPrivateKey */
30 #include <openssl/safestack.h>
31 #include <openssl/store.h>
32 #include <openssl/ui.h>
33 #include <openssl/engine.h>
34 #include <openssl/x509.h> /* For the PKCS8 stuff o.O */
35 #include "internal/asn1.h" /* For asn1_d2i_read_bio */
36 #include "internal/o_dir.h"
37 #include "internal/cryptlib.h"
38 #include "crypto/ctype.h" /* For ossl_isdigit */
39 #include "crypto/pem.h" /* For PVK and "blob" PEM headers */
41 #include "e_loader_attic_err.c"
43 DEFINE_STACK_OF(OSSL_STORE_INFO)
47 # define strncasecmp _strnicmp
51 # define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
59 static char *file_get_pass(const UI_METHOD *ui_method, char *pass,
60 size_t maxsize, const char *desc, const char *info,
67 ATTICerr(0, ERR_R_MALLOC_FAILURE);
71 if (ui_method != NULL)
72 UI_set_method(ui, ui_method);
73 UI_add_user_data(ui, data);
75 if ((prompt = UI_construct_prompt(ui, desc, info)) == NULL) {
76 ATTICerr(0, ERR_R_MALLOC_FAILURE);
78 } else if (!UI_add_input_string(ui, prompt, UI_INPUT_FLAG_DEFAULT_PWD,
79 pass, 0, maxsize - 1)) {
80 ATTICerr(0, ERR_R_UI_LIB);
83 switch (UI_process(ui)) {
85 ATTICerr(0, ATTIC_R_UI_PROCESS_INTERRUPTED_OR_CANCELLED);
89 ATTICerr(0, ERR_R_UI_LIB);
102 struct pem_pass_data {
103 const UI_METHOD *ui_method;
105 const char *prompt_desc;
106 const char *prompt_info;
109 static int file_fill_pem_pass_data(struct pem_pass_data *pass_data,
110 const char *desc, const char *info,
111 const UI_METHOD *ui_method, void *ui_data)
113 if (pass_data == NULL)
115 pass_data->ui_method = ui_method;
116 pass_data->data = ui_data;
117 pass_data->prompt_desc = desc;
118 pass_data->prompt_info = info;
122 /* This is used anywhere a pem_password_cb is needed */
123 static int file_get_pem_pass(char *buf, int num, int w, void *data)
125 struct pem_pass_data *pass_data = data;
126 char *pass = file_get_pass(pass_data->ui_method, buf, num,
127 pass_data->prompt_desc, pass_data->prompt_info,
130 return pass == NULL ? 0 : strlen(pass);
134 * Check if |str| ends with |suffix| preceded by a space, and if it does,
135 * return the index of that space. If there is no such suffix in |str|,
137 * For |str| == "FOO BAR" and |suffix| == "BAR", the returned value is 3.
139 static int check_suffix(const char *str, const char *suffix)
141 int str_len = strlen(str);
142 int suffix_len = strlen(suffix) + 1;
143 const char *p = NULL;
145 if (suffix_len >= str_len)
147 p = str + str_len - suffix_len;
149 || strcmp(p + 1, suffix) != 0)
155 * EMBEDDED is a special type of OSSL_STORE_INFO, specially for the file
156 * handlers, so we define it internally. This uses the possibility to
157 * create an OSSL_STORE_INFO with a generic data pointer and arbitrary
160 * This is used by a FILE_HANDLER's try_decode function to signal that it
161 * has decoded the incoming blob into a new blob, and that the attempted
162 * decoding should be immediately restarted with the new blob, using the
165 /* Negative numbers are never used for public OSSL_STORE_INFO types */
166 #define STORE_INFO_EMBEDDED -1
168 /* This is the embedded data */
174 /* Helper functions */
175 static struct embedded_st *get0_EMBEDDED(OSSL_STORE_INFO *info)
177 return OSSL_STORE_INFO_get0_data(STORE_INFO_EMBEDDED, info);
180 static void store_info_free(OSSL_STORE_INFO *info)
182 struct embedded_st *data;
184 if (info != NULL && (data = get0_EMBEDDED(info)) != NULL) {
185 BUF_MEM_free(data->blob);
186 OPENSSL_free(data->pem_name);
189 OSSL_STORE_INFO_free(info);
192 static OSSL_STORE_INFO *new_EMBEDDED(const char *new_pem_name,
195 OSSL_STORE_INFO *info = NULL;
196 struct embedded_st *data = NULL;
198 if ((data = OPENSSL_zalloc(sizeof(*data))) == NULL
199 || (info = OSSL_STORE_INFO_new(STORE_INFO_EMBEDDED, data)) == NULL) {
200 ATTICerr(0, ERR_R_MALLOC_FAILURE);
205 data->blob = embedded;
207 new_pem_name == NULL ? NULL : OPENSSL_strdup(new_pem_name);
209 if (new_pem_name != NULL && data->pem_name == NULL) {
210 ATTICerr(0, ERR_R_MALLOC_FAILURE);
211 store_info_free(info);
219 * The file scheme decoders
220 * ------------------------
222 * Each possible data type has its own decoder, which either operates
223 * through a given PEM name, or attempts to decode to see if the blob
224 * it's given is decodable for its data type. The assumption is that
225 * only the correct data type will match the content.
229 * The try_decode function is called to check if the blob of data can
230 * be used by this handler, and if it can, decodes it into a supported
231 * OpenSSL type and returns a OSSL_STORE_INFO with the decoded data.
233 * pem_name: If this blob comes from a PEM file, this holds
234 * the PEM name. If it comes from another type of
235 * file, this is NULL.
236 * pem_header: If this blob comes from a PEM file, this holds
237 * the PEM headers. If it comes from another type of
238 * file, this is NULL.
239 * blob: The blob of data to match with what this handler
241 * len: The length of the blob.
242 * handler_ctx: For a handler marked repeatable, this pointer can
243 * be used to create a context for the handler. IT IS
244 * THE HANDLER'S RESPONSIBILITY TO CREATE AND DESTROY
245 * THIS CONTEXT APPROPRIATELY, i.e. create on first call
246 * and destroy when about to return NULL.
247 * matchcount: A pointer to an int to count matches for this data.
248 * Usually becomes 0 (no match) or 1 (match!), but may
249 * be higher in the (unlikely) event that the data matches
250 * more than one possibility. The int will always be
251 * zero when the function is called.
252 * ui_method: Application UI method for getting a password, pin
253 * or any other interactive data.
254 * ui_data: Application data to be passed to ui_method when
256 * libctx: The library context to be used if applicable
257 * propq: The property query string for any algorithm fetches
261 typedef OSSL_STORE_INFO *(*file_try_decode_fn)(const char *pem_name,
262 const char *pem_header,
263 const unsigned char *blob,
264 size_t len, void **handler_ctx,
266 const UI_METHOD *ui_method,
267 void *ui_data, const char *uri,
268 OSSL_LIB_CTX *libctx,
271 * The eof function should return 1 if there's no more data to be found
272 * with the handler_ctx, otherwise 0. This is only used when the handler is
275 typedef int (*file_eof_fn)(void *handler_ctx);
277 * The destroy_ctx function is used to destroy the handler_ctx that was
278 * initiated by a repeatable try_decode function. This is only used when
279 * the handler is marked repeatable.
281 typedef void (*file_destroy_ctx_fn)(void **handler_ctx);
283 typedef struct file_handler_st {
285 file_try_decode_fn try_decode;
287 file_destroy_ctx_fn destroy_ctx;
294 * PKCS#12 decoder. It operates by decoding all of the blob content,
295 * extracting all the interesting data from it and storing them internally,
296 * then serving them one piece at a time.
298 static OSSL_STORE_INFO *try_decode_PKCS12(const char *pem_name,
299 const char *pem_header,
300 const unsigned char *blob,
301 size_t len, void **pctx,
303 const UI_METHOD *ui_method,
304 void *ui_data, const char *uri,
305 OSSL_LIB_CTX *libctx,
308 OSSL_STORE_INFO *store_info = NULL;
309 STACK_OF(OSSL_STORE_INFO) *ctx = *pctx;
312 /* Initial parsing */
315 if (pem_name != NULL)
316 /* No match, there is no PEM PKCS12 tag */
319 if ((p12 = d2i_PKCS12(NULL, &blob, len)) != NULL) {
321 char tpass[PEM_BUFSIZE];
322 EVP_PKEY *pkey = NULL;
324 STACK_OF(X509) *chain = NULL;
328 if (!PKCS12_mac_present(p12)
329 || PKCS12_verify_mac(p12, "", 0)
330 || PKCS12_verify_mac(p12, NULL, 0)) {
333 if ((pass = file_get_pass(ui_method, tpass, PEM_BUFSIZE,
334 "PKCS12 import", uri,
336 ATTICerr(0, ATTIC_R_PASSPHRASE_CALLBACK_ERROR);
339 if (!PKCS12_verify_mac(p12, pass, strlen(pass))) {
340 ATTICerr(0, ATTIC_R_ERROR_VERIFYING_PKCS12_MAC);
345 if (PKCS12_parse(p12, pass, &pkey, &cert, &chain)) {
346 OSSL_STORE_INFO *osi_pkey = NULL;
347 OSSL_STORE_INFO *osi_cert = NULL;
348 OSSL_STORE_INFO *osi_ca = NULL;
351 if ((ctx = sk_OSSL_STORE_INFO_new_null()) != NULL) {
353 if ((osi_pkey = OSSL_STORE_INFO_new_PKEY(pkey)) != NULL
354 /* clearing pkey here avoids case distinctions */
355 && (pkey = NULL) == NULL
356 && sk_OSSL_STORE_INFO_push(ctx, osi_pkey) != 0)
361 if (ok && cert != NULL) {
362 if ((osi_cert = OSSL_STORE_INFO_new_CERT(cert)) != NULL
363 /* clearing cert here avoids case distinctions */
364 && (cert = NULL) == NULL
365 && sk_OSSL_STORE_INFO_push(ctx, osi_cert) != 0)
370 while (ok && sk_X509_num(chain) > 0) {
371 X509 *ca = sk_X509_value(chain, 0);
373 if ((osi_ca = OSSL_STORE_INFO_new_CERT(ca)) != NULL
374 && sk_X509_shift(chain) != NULL
375 && sk_OSSL_STORE_INFO_push(ctx, osi_ca) != 0)
383 sk_X509_pop_free(chain, X509_free);
384 store_info_free(osi_pkey);
385 store_info_free(osi_cert);
386 store_info_free(osi_ca);
388 sk_OSSL_STORE_INFO_pop_free(ctx, store_info_free);
401 store_info = sk_OSSL_STORE_INFO_shift(ctx);
405 static int eof_PKCS12(void *ctx_)
407 STACK_OF(OSSL_STORE_INFO) *ctx = ctx_;
409 return ctx == NULL || sk_OSSL_STORE_INFO_num(ctx) == 0;
412 static void destroy_ctx_PKCS12(void **pctx)
414 STACK_OF(OSSL_STORE_INFO) *ctx = *pctx;
416 sk_OSSL_STORE_INFO_pop_free(ctx, store_info_free);
420 static FILE_HANDLER PKCS12_handler = {
429 * Encrypted PKCS#8 decoder. It operates by just decrypting the given blob
430 * into a new blob, which is returned as an EMBEDDED STORE_INFO. The whole
431 * decoding process will then start over with the new blob.
433 static OSSL_STORE_INFO *try_decode_PKCS8Encrypted(const char *pem_name,
434 const char *pem_header,
435 const unsigned char *blob,
436 size_t len, void **pctx,
438 const UI_METHOD *ui_method,
441 OSSL_LIB_CTX *libctx,
445 char kbuf[PEM_BUFSIZE];
447 const X509_ALGOR *dalg = NULL;
448 const ASN1_OCTET_STRING *doct = NULL;
449 OSSL_STORE_INFO *store_info = NULL;
451 unsigned char *new_data = NULL;
454 if (pem_name != NULL) {
455 if (strcmp(pem_name, PEM_STRING_PKCS8) != 0)
460 if ((p8 = d2i_X509_SIG(NULL, &blob, len)) == NULL)
465 if ((mem = BUF_MEM_new()) == NULL) {
466 ATTICerr(0, ERR_R_MALLOC_FAILURE);
470 if ((pass = file_get_pass(ui_method, kbuf, PEM_BUFSIZE,
471 "PKCS8 decrypt pass phrase", uri,
473 ATTICerr(0, ATTIC_R_BAD_PASSWORD_READ);
477 X509_SIG_get0(p8, &dalg, &doct);
478 if (!PKCS12_pbe_crypt(dalg, pass, strlen(pass), doct->data, doct->length,
479 &new_data, &new_data_len, 0))
482 mem->data = (char *)new_data;
483 mem->max = mem->length = (size_t)new_data_len;
487 store_info = new_EMBEDDED(PEM_STRING_PKCS8INF, mem);
488 if (store_info == NULL) {
489 ATTICerr(0, ERR_R_MALLOC_FAILURE);
500 static FILE_HANDLER PKCS8Encrypted_handler = {
502 try_decode_PKCS8Encrypted
506 * Private key decoder. Decodes all sorts of private keys, both PKCS#8
507 * encoded ones and old style PEM ones (with the key type is encoded into
510 static OSSL_STORE_INFO *try_decode_PrivateKey(const char *pem_name,
511 const char *pem_header,
512 const unsigned char *blob,
513 size_t len, void **pctx,
515 const UI_METHOD *ui_method,
516 void *ui_data, const char *uri,
517 OSSL_LIB_CTX *libctx,
520 OSSL_STORE_INFO *store_info = NULL;
521 EVP_PKEY *pkey = NULL;
522 const EVP_PKEY_ASN1_METHOD *ameth = NULL;
524 if (pem_name != NULL) {
525 if (strcmp(pem_name, PEM_STRING_PKCS8INF) == 0) {
526 PKCS8_PRIV_KEY_INFO *p8inf =
527 d2i_PKCS8_PRIV_KEY_INFO(NULL, &blob, len);
531 pkey = EVP_PKCS82PKEY_ex(p8inf, libctx, propq);
532 PKCS8_PRIV_KEY_INFO_free(p8inf);
537 if ((slen = check_suffix(pem_name, "PRIVATE KEY")) > 0
538 && (ameth = EVP_PKEY_asn1_find_str(NULL, pem_name,
540 && EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL,
543 pkey = d2i_PrivateKey_ex(pkey_id, NULL, &blob, len,
549 #ifndef OPENSSL_NO_ENGINE
550 ENGINE *curengine = ENGINE_get_first();
552 while (curengine != NULL) {
553 ENGINE_PKEY_ASN1_METHS_PTR asn1meths =
554 ENGINE_get_pkey_asn1_meths(curengine);
556 if (asn1meths != NULL) {
557 const int *nids = NULL;
558 int nids_n = asn1meths(curengine, NULL, &nids, 0);
560 for (i = 0; i < nids_n; i++) {
561 EVP_PKEY_ASN1_METHOD *ameth2 = NULL;
562 EVP_PKEY *tmp_pkey = NULL;
563 const unsigned char *tmp_blob = blob;
564 int pkey_id, pkey_flags;
566 if (!asn1meths(curengine, &ameth2, NULL, nids[i])
567 || !EVP_PKEY_asn1_get0_info(&pkey_id, NULL,
568 &pkey_flags, NULL, NULL,
570 || (pkey_flags & ASN1_PKEY_ALIAS) != 0)
573 ERR_set_mark(); /* prevent flooding error queue */
574 tmp_pkey = d2i_PrivateKey_ex(pkey_id, NULL,
577 if (tmp_pkey != NULL) {
579 EVP_PKEY_free(tmp_pkey);
587 curengine = ENGINE_get_next(curengine);
591 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
592 EVP_PKEY *tmp_pkey = NULL;
593 const unsigned char *tmp_blob = blob;
594 int pkey_id, pkey_flags;
596 ameth = EVP_PKEY_asn1_get0(i);
597 if (!EVP_PKEY_asn1_get0_info(&pkey_id, NULL, &pkey_flags, NULL,
599 || (pkey_flags & ASN1_PKEY_ALIAS) != 0)
602 ERR_set_mark(); /* prevent flooding error queue */
603 tmp_pkey = d2i_PrivateKey_ex(pkey_id, NULL, &tmp_blob, len,
605 if (tmp_pkey != NULL) {
607 EVP_PKEY_free(tmp_pkey);
615 if (*matchcount > 1) {
624 store_info = OSSL_STORE_INFO_new_PKEY(pkey);
625 if (store_info == NULL)
631 static FILE_HANDLER PrivateKey_handler = {
633 try_decode_PrivateKey
637 * Public key decoder. Only supports SubjectPublicKeyInfo formatted keys.
639 static OSSL_STORE_INFO *try_decode_PUBKEY(const char *pem_name,
640 const char *pem_header,
641 const unsigned char *blob,
642 size_t len, void **pctx,
644 const UI_METHOD *ui_method,
645 void *ui_data, const char *uri,
646 OSSL_LIB_CTX *libctx,
649 OSSL_STORE_INFO *store_info = NULL;
650 EVP_PKEY *pkey = NULL;
652 if (pem_name != NULL) {
653 if (strcmp(pem_name, PEM_STRING_PUBLIC) != 0)
659 if ((pkey = d2i_PUBKEY(NULL, &blob, len)) != NULL) {
661 store_info = OSSL_STORE_INFO_new_PUBKEY(pkey);
667 static FILE_HANDLER PUBKEY_handler = {
673 * Key parameter decoder.
675 static OSSL_STORE_INFO *try_decode_params(const char *pem_name,
676 const char *pem_header,
677 const unsigned char *blob,
678 size_t len, void **pctx,
680 const UI_METHOD *ui_method,
681 void *ui_data, const char *uri,
682 OSSL_LIB_CTX *libctx,
685 OSSL_STORE_INFO *store_info = NULL;
686 EVP_PKEY *pkey = NULL;
687 const EVP_PKEY_ASN1_METHOD *ameth = NULL;
689 if (pem_name != NULL) {
693 if ((slen = check_suffix(pem_name, "PARAMETERS")) > 0
694 && (ameth = EVP_PKEY_asn1_find_str(NULL, pem_name, slen)) != NULL
695 && EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL,
698 pkey = d2i_KeyParams(pkey_id, NULL, &blob, len);
703 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
704 EVP_PKEY *tmp_pkey = NULL;
705 const unsigned char *tmp_blob = blob;
706 int pkey_id, pkey_flags;
708 ameth = EVP_PKEY_asn1_get0(i);
709 if (!EVP_PKEY_asn1_get0_info(&pkey_id, NULL, &pkey_flags, NULL,
711 || (pkey_flags & ASN1_PKEY_ALIAS) != 0)
714 ERR_set_mark(); /* prevent flooding error queue */
716 tmp_pkey = d2i_KeyParams(pkey_id, NULL, &tmp_blob, len);
718 if (tmp_pkey != NULL) {
720 EVP_PKEY_free(tmp_pkey);
728 if (*matchcount > 1) {
737 store_info = OSSL_STORE_INFO_new_PARAMS(pkey);
738 if (store_info == NULL)
744 static FILE_HANDLER params_handler = {
750 * X.509 certificate decoder.
752 static OSSL_STORE_INFO *try_decode_X509Certificate(const char *pem_name,
753 const char *pem_header,
754 const unsigned char *blob,
755 size_t len, void **pctx,
757 const UI_METHOD *ui_method,
760 OSSL_LIB_CTX *libctx,
763 OSSL_STORE_INFO *store_info = NULL;
767 * In most cases, we can try to interpret the serialized data as a trusted
768 * cert (X509 + X509_AUX) and fall back to reading it as a normal cert
769 * (just X509), but if the PEM name specifically declares it as a trusted
770 * cert, then no fallback should be engaged. |ignore_trusted| tells if
771 * the fallback can be used (1) or not (0).
773 int ignore_trusted = 1;
775 if (pem_name != NULL) {
776 if (strcmp(pem_name, PEM_STRING_X509_TRUSTED) == 0)
778 else if (strcmp(pem_name, PEM_STRING_X509_OLD) != 0
779 && strcmp(pem_name, PEM_STRING_X509) != 0)
785 cert = X509_new_ex(libctx, propq);
789 if ((d2i_X509_AUX(&cert, &blob, len)) != NULL
790 || (ignore_trusted && (d2i_X509(&cert, &blob, len)) != NULL)) {
792 store_info = OSSL_STORE_INFO_new_CERT(cert);
795 if (store_info == NULL)
801 static FILE_HANDLER X509Certificate_handler = {
803 try_decode_X509Certificate
809 static OSSL_STORE_INFO *try_decode_X509CRL(const char *pem_name,
810 const char *pem_header,
811 const unsigned char *blob,
812 size_t len, void **pctx,
814 const UI_METHOD *ui_method,
815 void *ui_data, const char *uri,
816 OSSL_LIB_CTX *libctx,
819 OSSL_STORE_INFO *store_info = NULL;
820 X509_CRL *crl = NULL;
822 if (pem_name != NULL) {
823 if (strcmp(pem_name, PEM_STRING_X509_CRL) != 0)
829 if ((crl = d2i_X509_CRL(NULL, &blob, len)) != NULL) {
831 store_info = OSSL_STORE_INFO_new_CRL(crl);
834 if (store_info == NULL)
840 static FILE_HANDLER X509CRL_handler = {
846 * To finish it all off, we collect all the handlers.
848 static const FILE_HANDLER *file_handlers[] = {
850 &PKCS8Encrypted_handler,
851 &X509Certificate_handler,
864 struct ossl_store_loader_ctx_st {
865 char *uri; /* The URI we currently try to load */
872 #define FILE_FLAG_SECMEM (1<<0)
873 #define FILE_FLAG_ATTACHED (1<<1)
876 struct { /* Used with is_raw and is_pem */
880 * The following are used when the handler is marked as
883 const FILE_HANDLER *last_handler;
884 void *last_handler_ctx;
886 struct { /* Used with is_dir */
887 OPENSSL_DIR_CTX *ctx;
891 * When a search expression is given, these are filled in.
892 * |search_name| contains the file basename to look for.
893 * The string is exactly 8 characters long.
898 * The directory reading utility we have combines opening with
899 * reading the first name. To make sure we can detect the end
900 * at the right time, we read early and cache the name.
902 const char *last_entry;
907 /* Expected object type. May be unspecified */
910 OSSL_LIB_CTX *libctx;
914 static void OSSL_STORE_LOADER_CTX_free(OSSL_STORE_LOADER_CTX *ctx)
919 OPENSSL_free(ctx->propq);
920 OPENSSL_free(ctx->uri);
921 if (ctx->type != is_dir) {
922 if (ctx->_.file.last_handler != NULL) {
923 ctx->_.file.last_handler->destroy_ctx(&ctx->_.file.last_handler_ctx);
924 ctx->_.file.last_handler_ctx = NULL;
925 ctx->_.file.last_handler = NULL;
931 static int file_find_type(OSSL_STORE_LOADER_CTX *ctx)
934 char peekbuf[4096] = { 0, };
936 if ((buff = BIO_new(BIO_f_buffer())) == NULL)
939 ctx->_.file.file = BIO_push(buff, ctx->_.file.file);
940 if (BIO_buffer_peek(ctx->_.file.file, peekbuf, sizeof(peekbuf) - 1) > 0) {
941 peekbuf[sizeof(peekbuf) - 1] = '\0';
942 if (strstr(peekbuf, "-----BEGIN ") != NULL)
948 static OSSL_STORE_LOADER_CTX *file_open_ex
949 (const OSSL_STORE_LOADER *loader, const char *uri,
950 OSSL_LIB_CTX *libctx, const char *propq,
951 const UI_METHOD *ui_method, void *ui_data)
953 OSSL_STORE_LOADER_CTX *ctx = NULL;
957 unsigned int check_absolute:1;
959 size_t path_data_n = 0, i;
963 * First step, just take the URI as is.
965 path_data[path_data_n].check_absolute = 0;
966 path_data[path_data_n++].path = uri;
969 * Second step, if the URI appears to start with the 'file' scheme,
970 * extract the path and make that the second path to check.
971 * There's a special case if the URI also contains an authority, then
972 * the full URI shouldn't be used as a path anywhere.
974 if (strncasecmp(uri, "file:", 5) == 0) {
975 const char *p = &uri[5];
977 if (strncmp(&uri[5], "//", 2) == 0) {
978 path_data_n--; /* Invalidate using the full URI */
979 if (strncasecmp(&uri[7], "localhost/", 10) == 0) {
981 } else if (uri[7] == '/') {
984 ATTICerr(0, ATTIC_R_URI_AUTHORITY_UNSUPPORTED);
989 path_data[path_data_n].check_absolute = 1;
991 /* Windows file: URIs with a drive letter start with a / */
992 if (p[0] == '/' && p[2] == ':' && p[3] == '/') {
993 char c = tolower(p[1]);
995 if (c >= 'a' && c <= 'z') {
997 /* We know it's absolute, so no need to check */
998 path_data[path_data_n].check_absolute = 0;
1002 path_data[path_data_n++].path = p;
1006 for (i = 0, path = NULL; path == NULL && i < path_data_n; i++) {
1008 * If the scheme "file" was an explicit part of the URI, the path must
1009 * be absolute. So says RFC 8089
1011 if (path_data[i].check_absolute && path_data[i].path[0] != '/') {
1012 ATTICerr(0, ATTIC_R_PATH_MUST_BE_ABSOLUTE);
1013 ERR_add_error_data(1, path_data[i].path);
1017 if (stat(path_data[i].path, &st) < 0) {
1018 ERR_raise_data(ERR_LIB_SYS, errno,
1022 path = path_data[i].path;
1029 /* Successfully found a working path */
1031 ctx = OPENSSL_zalloc(sizeof(*ctx));
1033 ATTICerr(0, ERR_R_MALLOC_FAILURE);
1036 ctx->uri = OPENSSL_strdup(uri);
1037 if (ctx->uri == NULL) {
1038 ATTICerr(0, ERR_R_MALLOC_FAILURE);
1042 if (S_ISDIR(st.st_mode)) {
1044 ctx->_.dir.last_entry = OPENSSL_DIR_read(&ctx->_.dir.ctx, path);
1045 ctx->_.dir.last_errno = errno;
1046 if (ctx->_.dir.last_entry == NULL) {
1047 if (ctx->_.dir.last_errno != 0) {
1048 ERR_raise(ERR_LIB_SYS, ctx->_.dir.last_errno);
1051 ctx->_.dir.end_reached = 1;
1053 } else if ((ctx->_.file.file = BIO_new_file(path, "rb")) == NULL
1054 || !file_find_type(ctx)) {
1055 BIO_free_all(ctx->_.file.file);
1058 if (propq != NULL) {
1059 ctx->propq = OPENSSL_strdup(propq);
1060 if (ctx->propq == NULL) {
1061 ATTICerr(0, ERR_R_MALLOC_FAILURE);
1065 ctx->libctx = libctx;
1069 OSSL_STORE_LOADER_CTX_free(ctx);
1073 static OSSL_STORE_LOADER_CTX *file_open
1074 (const OSSL_STORE_LOADER *loader, const char *uri,
1075 const UI_METHOD *ui_method, void *ui_data)
1077 return file_open_ex(loader, uri, NULL, NULL, ui_method, ui_data);
1080 static OSSL_STORE_LOADER_CTX *file_attach
1081 (const OSSL_STORE_LOADER *loader, BIO *bp,
1082 OSSL_LIB_CTX *libctx, const char *propq,
1083 const UI_METHOD *ui_method, void *ui_data)
1085 OSSL_STORE_LOADER_CTX *ctx = NULL;
1087 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL
1088 || (propq != NULL && (ctx->propq = OPENSSL_strdup(propq)) == NULL)) {
1089 ATTICerr(0, ERR_R_MALLOC_FAILURE);
1090 OSSL_STORE_LOADER_CTX_free(ctx);
1093 ctx->libctx = libctx;
1094 ctx->flags |= FILE_FLAG_ATTACHED;
1095 ctx->_.file.file = bp;
1096 if (!file_find_type(ctx)) {
1097 /* Safety measure */
1098 ctx->_.file.file = NULL;
1103 OSSL_STORE_LOADER_CTX_free(ctx);
1107 static int file_ctrl(OSSL_STORE_LOADER_CTX *ctx, int cmd, va_list args)
1112 case OSSL_STORE_C_USE_SECMEM:
1114 int on = *(va_arg(args, int *));
1118 ctx->flags &= ~FILE_FLAG_SECMEM;
1121 ctx->flags |= FILE_FLAG_SECMEM;
1124 ATTICerr(0, ERR_R_PASSED_INVALID_ARGUMENT);
1137 static int file_expect(OSSL_STORE_LOADER_CTX *ctx, int expected)
1139 ctx->expected_type = expected;
1143 static int file_find(OSSL_STORE_LOADER_CTX *ctx,
1144 const OSSL_STORE_SEARCH *search)
1147 * If ctx == NULL, the library is looking to know if this loader supports
1148 * the given search type.
1151 if (OSSL_STORE_SEARCH_get_type(search) == OSSL_STORE_SEARCH_BY_NAME) {
1152 unsigned long hash = 0;
1157 if (ctx->type != is_dir) {
1158 ATTICerr(0, ATTIC_R_SEARCH_ONLY_SUPPORTED_FOR_DIRECTORIES);
1162 hash = X509_NAME_hash_ex(OSSL_STORE_SEARCH_get0_name(search),
1164 BIO_snprintf(ctx->_.dir.search_name, sizeof(ctx->_.dir.search_name),
1170 ATTICerr(0, ATTIC_R_UNSUPPORTED_SEARCH_TYPE);
1174 static OSSL_STORE_INFO *file_load_try_decode(OSSL_STORE_LOADER_CTX *ctx,
1175 const char *pem_name,
1176 const char *pem_header,
1177 unsigned char *data, size_t len,
1178 const UI_METHOD *ui_method,
1179 void *ui_data, int *matchcount)
1181 OSSL_STORE_INFO *result = NULL;
1182 BUF_MEM *new_mem = NULL;
1183 char *new_pem_name = NULL;
1189 void *handler_ctx = NULL;
1190 const FILE_HANDLER **matching_handlers =
1191 OPENSSL_zalloc(sizeof(*matching_handlers)
1192 * OSSL_NELEM(file_handlers));
1194 if (matching_handlers == NULL) {
1195 ATTICerr(0, ERR_R_MALLOC_FAILURE);
1200 for (i = 0; i < OSSL_NELEM(file_handlers); i++) {
1201 const FILE_HANDLER *handler = file_handlers[i];
1202 int try_matchcount = 0;
1203 void *tmp_handler_ctx = NULL;
1204 OSSL_STORE_INFO *tmp_result;
1209 handler->try_decode(pem_name, pem_header, data, len,
1210 &tmp_handler_ctx, &try_matchcount,
1211 ui_method, ui_data, ctx->uri,
1212 ctx->libctx, ctx->propq);
1213 /* avoid flooding error queue with low-level ASN.1 parse errors */
1214 err = ERR_peek_last_error();
1215 if (ERR_GET_LIB(err) == ERR_LIB_ASN1
1216 && ERR_GET_REASON(err) == ERR_R_NESTED_ASN1_ERROR)
1219 ERR_clear_last_mark();
1221 if (try_matchcount > 0) {
1223 matching_handlers[*matchcount] = handler;
1226 handler->destroy_ctx(&handler_ctx);
1227 handler_ctx = tmp_handler_ctx;
1229 if ((*matchcount += try_matchcount) > 1) {
1230 /* more than one match => ambiguous, kill any result */
1231 store_info_free(result);
1232 store_info_free(tmp_result);
1233 if (handler->destroy_ctx != NULL)
1234 handler->destroy_ctx(&handler_ctx);
1240 result = tmp_result;
1241 if (result == NULL) /* e.g., PKCS#12 file decryption error */
1247 && *matchcount == 1 && matching_handlers[0]->repeatable) {
1248 ctx->_.file.last_handler = matching_handlers[0];
1249 ctx->_.file.last_handler_ctx = handler_ctx;
1252 OPENSSL_free(matching_handlers);
1256 OPENSSL_free(new_pem_name);
1257 BUF_MEM_free(new_mem);
1260 && (t = OSSL_STORE_INFO_get_type(result)) == STORE_INFO_EMBEDDED) {
1261 struct embedded_st *embedded = get0_EMBEDDED(result);
1263 /* "steal" the embedded data */
1264 pem_name = new_pem_name = embedded->pem_name;
1265 new_mem = embedded->blob;
1266 data = (unsigned char *)new_mem->data;
1267 len = new_mem->length;
1268 embedded->pem_name = NULL;
1269 embedded->blob = NULL;
1271 store_info_free(result);
1279 static OSSL_STORE_INFO *file_load_try_repeat(OSSL_STORE_LOADER_CTX *ctx,
1280 const UI_METHOD *ui_method,
1283 OSSL_STORE_INFO *result = NULL;
1284 int try_matchcount = 0;
1286 if (ctx->_.file.last_handler != NULL) {
1288 ctx->_.file.last_handler->try_decode(NULL, NULL, NULL, 0,
1289 &ctx->_.file.last_handler_ctx,
1291 ui_method, ui_data, ctx->uri,
1292 ctx->libctx, ctx->propq);
1294 if (result == NULL) {
1295 ctx->_.file.last_handler->destroy_ctx(&ctx->_.file.last_handler_ctx);
1296 ctx->_.file.last_handler_ctx = NULL;
1297 ctx->_.file.last_handler = NULL;
1303 static void pem_free_flag(void *pem_data, int secure, size_t num)
1306 OPENSSL_secure_clear_free(pem_data, num);
1308 OPENSSL_free(pem_data);
1310 static int file_read_pem(BIO *bp, char **pem_name, char **pem_header,
1311 unsigned char **data, long *len,
1312 const UI_METHOD *ui_method, void *ui_data,
1313 const char *uri, int secure)
1316 ? PEM_read_bio_ex(bp, pem_name, pem_header, data, len,
1317 PEM_FLAG_SECURE | PEM_FLAG_EAY_COMPATIBLE)
1318 : PEM_read_bio(bp, pem_name, pem_header, data, len);
1324 * 10 is the number of characters in "Proc-Type:", which
1325 * PEM_get_EVP_CIPHER_INFO() requires to be present.
1326 * If the PEM header has less characters than that, it's
1327 * not worth spending cycles on it.
1329 if (strlen(*pem_header) > 10) {
1330 EVP_CIPHER_INFO cipher;
1331 struct pem_pass_data pass_data;
1333 if (!PEM_get_EVP_CIPHER_INFO(*pem_header, &cipher)
1334 || !file_fill_pem_pass_data(&pass_data, "PEM pass phrase", uri,
1336 || !PEM_do_header(&cipher, *data, len, file_get_pem_pass,
1344 static OSSL_STORE_INFO *file_try_read_msblob(BIO *bp, int *matchcount)
1346 OSSL_STORE_INFO *result = NULL;
1350 unsigned int magic = 0, bitlen = 0;
1352 unsigned char peekbuf[16] = { 0, };
1353 const unsigned char *p = peekbuf;
1355 if (BIO_buffer_peek(bp, peekbuf, sizeof(peekbuf)) <= 0)
1357 if (!ossl_do_blob_header(&p, sizeof(peekbuf), &magic, &bitlen,
1365 EVP_PKEY *tmp = ispub
1366 ? b2i_PublicKey_bio(bp)
1367 : b2i_PrivateKey_bio(bp);
1370 || (result = OSSL_STORE_INFO_new_PKEY(tmp)) == NULL) {
1379 static OSSL_STORE_INFO *file_try_read_PVK(BIO *bp, const UI_METHOD *ui_method,
1380 void *ui_data, const char *uri,
1383 OSSL_STORE_INFO *result = NULL;
1386 unsigned int saltlen = 0, keylen = 0;
1387 unsigned char peekbuf[24] = { 0, };
1388 const unsigned char *p = peekbuf;
1390 if (BIO_buffer_peek(bp, peekbuf, sizeof(peekbuf)) <= 0)
1392 if (!ossl_do_PVK_header(&p, sizeof(peekbuf), 0, &saltlen, &keylen))
1399 EVP_PKEY *tmp = NULL;
1400 struct pem_pass_data pass_data;
1402 if (!file_fill_pem_pass_data(&pass_data, "PVK pass phrase", uri,
1404 || (tmp = b2i_PVK_bio(bp, file_get_pem_pass, &pass_data)) == NULL
1405 || (result = OSSL_STORE_INFO_new_PKEY(tmp)) == NULL) {
1414 static int file_read_asn1(BIO *bp, unsigned char **data, long *len)
1416 BUF_MEM *mem = NULL;
1418 if (asn1_d2i_read_bio(bp, &mem) < 0)
1421 *data = (unsigned char *)mem->data;
1422 *len = (long)mem->length;
1428 static int file_name_to_uri(OSSL_STORE_LOADER_CTX *ctx, const char *name,
1431 assert(name != NULL);
1432 assert(data != NULL);
1434 const char *pathsep = ossl_ends_with_dirsep(ctx->uri) ? "" : "/";
1435 long calculated_length = strlen(ctx->uri) + strlen(pathsep)
1436 + strlen(name) + 1 /* \0 */;
1438 *data = OPENSSL_zalloc(calculated_length);
1439 if (*data == NULL) {
1440 ATTICerr(0, ERR_R_MALLOC_FAILURE);
1444 OPENSSL_strlcat(*data, ctx->uri, calculated_length);
1445 OPENSSL_strlcat(*data, pathsep, calculated_length);
1446 OPENSSL_strlcat(*data, name, calculated_length);
1451 static int file_name_check(OSSL_STORE_LOADER_CTX *ctx, const char *name)
1453 const char *p = NULL;
1454 size_t len = strlen(ctx->_.dir.search_name);
1456 /* If there are no search criteria, all names are accepted */
1457 if (ctx->_.dir.search_name[0] == '\0')
1460 /* If the expected type isn't supported, no name is accepted */
1461 if (ctx->expected_type != 0
1462 && ctx->expected_type != OSSL_STORE_INFO_CERT
1463 && ctx->expected_type != OSSL_STORE_INFO_CRL)
1467 * First, check the basename
1469 if (strncasecmp(name, ctx->_.dir.search_name, len) != 0 || name[len] != '.')
1474 * Then, if the expected type is a CRL, check that the extension starts
1479 if (ctx->expected_type != 0
1480 && ctx->expected_type != OSSL_STORE_INFO_CRL)
1482 } else if (ctx->expected_type == OSSL_STORE_INFO_CRL) {
1487 * Last, check that the rest of the extension is a decimal number, at
1488 * least one digit long.
1497 * One extra step here, check for a possible generation number.
1500 for (p++; *p != '\0'; p++)
1501 if (!ossl_isdigit(*p))
1506 * If we've reached the end of the string at this point, we've successfully
1507 * found a fitting file name.
1512 static int file_eof(OSSL_STORE_LOADER_CTX *ctx);
1513 static int file_error(OSSL_STORE_LOADER_CTX *ctx);
1514 static OSSL_STORE_INFO *file_load(OSSL_STORE_LOADER_CTX *ctx,
1515 const UI_METHOD *ui_method,
1518 OSSL_STORE_INFO *result = NULL;
1522 if (ctx->type == is_dir) {
1524 char *newname = NULL;
1526 if (ctx->_.dir.last_entry == NULL) {
1527 if (!ctx->_.dir.end_reached) {
1528 assert(ctx->_.dir.last_errno != 0);
1529 ERR_raise(ERR_LIB_SYS, ctx->_.dir.last_errno);
1535 if (ctx->_.dir.last_entry[0] != '.'
1536 && file_name_check(ctx, ctx->_.dir.last_entry)
1537 && !file_name_to_uri(ctx, ctx->_.dir.last_entry, &newname))
1541 * On the first call (with a NULL context), OPENSSL_DIR_read()
1542 * cares about the second argument. On the following calls, it
1543 * only cares that it isn't NULL. Therefore, we can safely give
1546 ctx->_.dir.last_entry = OPENSSL_DIR_read(&ctx->_.dir.ctx, ctx->uri);
1547 ctx->_.dir.last_errno = errno;
1548 if (ctx->_.dir.last_entry == NULL && ctx->_.dir.last_errno == 0)
1549 ctx->_.dir.end_reached = 1;
1552 && (result = OSSL_STORE_INFO_new_NAME(newname)) == NULL) {
1553 OPENSSL_free(newname);
1554 ATTICerr(0, ERR_R_OSSL_STORE_LIB);
1557 } while (result == NULL && !file_eof(ctx));
1559 int matchcount = -1;
1562 result = file_load_try_repeat(ctx, ui_method, ui_data);
1570 char *pem_name = NULL; /* PEM record name */
1571 char *pem_header = NULL; /* PEM record header */
1572 unsigned char *data = NULL; /* DER encoded data */
1573 long len = 0; /* DER encoded data length */
1576 if (ctx->type == is_pem) {
1577 if (!file_read_pem(ctx->_.file.file, &pem_name, &pem_header,
1578 &data, &len, ui_method, ui_data, ctx->uri,
1579 (ctx->flags & FILE_FLAG_SECMEM) != 0)) {
1584 if ((result = file_try_read_msblob(ctx->_.file.file,
1585 &matchcount)) != NULL
1586 || (result = file_try_read_PVK(ctx->_.file.file,
1587 ui_method, ui_data, ctx->uri,
1588 &matchcount)) != NULL)
1591 if (!file_read_asn1(ctx->_.file.file, &data, &len)) {
1597 result = file_load_try_decode(ctx, pem_name, pem_header, data, len,
1598 ui_method, ui_data, &matchcount);
1604 * If a PEM name matches more than one handler, the handlers are
1607 if (!ossl_assert(pem_name == NULL || matchcount <= 1)) {
1612 if (matchcount > 1) {
1613 ATTICerr(0, ATTIC_R_AMBIGUOUS_CONTENT_TYPE);
1614 } else if (matchcount == 1) {
1616 * If there are other errors on the stack, they already show
1617 * what the problem is.
1619 if (ERR_peek_error() == 0) {
1620 ATTICerr(0, ATTIC_R_UNSUPPORTED_CONTENT_TYPE);
1621 if (pem_name != NULL)
1622 ERR_add_error_data(3, "PEM type is '", pem_name, "'");
1629 pem_free_flag(pem_name, (ctx->flags & FILE_FLAG_SECMEM) != 0, 0);
1630 pem_free_flag(pem_header, (ctx->flags & FILE_FLAG_SECMEM) != 0, 0);
1631 pem_free_flag(data, (ctx->flags & FILE_FLAG_SECMEM) != 0, len);
1632 } while (matchcount == 0 && !file_eof(ctx) && !file_error(ctx));
1634 /* We bail out on ambiguity */
1635 if (matchcount > 1) {
1636 store_info_free(result);
1641 && ctx->expected_type != 0
1642 && ctx->expected_type != OSSL_STORE_INFO_get_type(result)) {
1643 store_info_free(result);
1651 static int file_error(OSSL_STORE_LOADER_CTX *ctx)
1653 return ctx->errcnt > 0;
1656 static int file_eof(OSSL_STORE_LOADER_CTX *ctx)
1658 if (ctx->type == is_dir)
1659 return ctx->_.dir.end_reached;
1661 if (ctx->_.file.last_handler != NULL
1662 && !ctx->_.file.last_handler->eof(ctx->_.file.last_handler_ctx))
1664 return BIO_eof(ctx->_.file.file);
1667 static int file_close(OSSL_STORE_LOADER_CTX *ctx)
1669 if ((ctx->flags & FILE_FLAG_ATTACHED) == 0) {
1670 if (ctx->type == is_dir)
1671 OPENSSL_DIR_end(&ctx->_.dir.ctx);
1673 BIO_free_all(ctx->_.file.file);
1676 * Because file_attach() called file_find_type(), we know that a
1677 * BIO_f_buffer() has been pushed on top of the regular BIO.
1679 BIO *buff = ctx->_.file.file;
1682 (void)BIO_pop(ctx->_.file.file);
1683 /* Safety measure */
1684 ctx->_.file.file = NULL;
1688 OSSL_STORE_LOADER_CTX_free(ctx);
1696 static const char *loader_attic_id = "loader_attic";
1697 static const char *loader_attic_name = "'file:' loader";
1699 static OSSL_STORE_LOADER *loader_attic = NULL;
1701 static int loader_attic_init(ENGINE *e)
1707 static int loader_attic_finish(ENGINE *e)
1713 static int loader_attic_destroy(ENGINE *e)
1715 OSSL_STORE_LOADER *loader = OSSL_STORE_unregister_loader("file");
1720 ERR_unload_ATTIC_strings();
1721 OSSL_STORE_LOADER_free(loader);
1725 static int bind_loader_attic(ENGINE *e)
1728 /* Ensure the ATTIC error handling is set up on best effort basis */
1729 ERR_load_ATTIC_strings();
1731 if (/* Create the OSSL_STORE_LOADER */
1732 (loader_attic = OSSL_STORE_LOADER_new(e, "file")) == NULL
1733 || !OSSL_STORE_LOADER_set_open_ex(loader_attic, file_open_ex)
1734 || !OSSL_STORE_LOADER_set_open(loader_attic, file_open)
1735 || !OSSL_STORE_LOADER_set_attach(loader_attic, file_attach)
1736 || !OSSL_STORE_LOADER_set_ctrl(loader_attic, file_ctrl)
1737 || !OSSL_STORE_LOADER_set_expect(loader_attic, file_expect)
1738 || !OSSL_STORE_LOADER_set_find(loader_attic, file_find)
1739 || !OSSL_STORE_LOADER_set_load(loader_attic, file_load)
1740 || !OSSL_STORE_LOADER_set_eof(loader_attic, file_eof)
1741 || !OSSL_STORE_LOADER_set_error(loader_attic, file_error)
1742 || !OSSL_STORE_LOADER_set_close(loader_attic, file_close)
1743 /* Init the engine itself */
1744 || !ENGINE_set_id(e, loader_attic_id)
1745 || !ENGINE_set_name(e, loader_attic_name)
1746 || !ENGINE_set_destroy_function(e, loader_attic_destroy)
1747 || !ENGINE_set_init_function(e, loader_attic_init)
1748 || !ENGINE_set_finish_function(e, loader_attic_finish)
1749 /* Finally, register the method with libcrypto */
1750 || !OSSL_STORE_register_loader(loader_attic)) {
1751 OSSL_STORE_LOADER_free(loader_attic);
1752 loader_attic = NULL;
1753 ATTICerr(0, ATTIC_R_INIT_FAILED);
1760 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
1761 # error "Only allowed as dynamically shared object"
1764 static int bind_helper(ENGINE *e, const char *id)
1766 if (id && (strcmp(id, loader_attic_id) != 0))
1768 if (!bind_loader_attic(e))
1773 IMPLEMENT_DYNAMIC_CHECK_FN()
1774 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)