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 <openssl/pkcs12.h> /* For the PKCS8 stuff o.O */
22 #include <openssl/rsa.h> /* For d2i_RSAPrivateKey */
23 #include <openssl/safestack.h>
24 #include <openssl/store.h>
25 #include <openssl/ui.h>
26 #include <openssl/x509.h> /* For the PKCS8 stuff o.O */
27 #include "crypto/asn1.h"
28 #include "crypto/ctype.h"
29 #include "internal/o_dir.h"
30 #include "internal/cryptlib.h"
31 #include "crypto/store.h"
32 #include "crypto/evp.h"
33 #include "store_local.h"
42 # define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
50 static char *file_get_pass(const UI_METHOD
*ui_method
, char *pass
,
51 size_t maxsize
, const char *prompt_info
, void *data
)
57 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS
, ERR_R_MALLOC_FAILURE
);
61 if (ui_method
!= NULL
)
62 UI_set_method(ui
, ui_method
);
63 UI_add_user_data(ui
, data
);
65 if ((prompt
= UI_construct_prompt(ui
, "pass phrase",
66 prompt_info
)) == NULL
) {
67 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS
, ERR_R_MALLOC_FAILURE
);
69 } else if (!UI_add_input_string(ui
, prompt
, UI_INPUT_FLAG_DEFAULT_PWD
,
70 pass
, 0, maxsize
- 1)) {
71 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS
, ERR_R_UI_LIB
);
74 switch (UI_process(ui
)) {
76 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS
,
77 OSSL_STORE_R_UI_PROCESS_INTERRUPTED_OR_CANCELLED
);
81 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS
, ERR_R_UI_LIB
);
94 struct pem_pass_data
{
95 const UI_METHOD
*ui_method
;
97 const char *prompt_info
;
100 static int file_fill_pem_pass_data(struct pem_pass_data
*pass_data
,
101 const char *prompt_info
,
102 const UI_METHOD
*ui_method
, void *ui_data
)
104 if (pass_data
== NULL
)
106 pass_data
->ui_method
= ui_method
;
107 pass_data
->data
= ui_data
;
108 pass_data
->prompt_info
= prompt_info
;
112 /* This is used anywhere a pem_password_cb is needed */
113 static int file_get_pem_pass(char *buf
, int num
, int w
, void *data
)
115 struct pem_pass_data
*pass_data
= data
;
116 char *pass
= file_get_pass(pass_data
->ui_method
, buf
, num
,
117 pass_data
->prompt_info
, pass_data
->data
);
119 return pass
== NULL
? 0 : strlen(pass
);
123 * The file scheme decoders
124 * ------------------------
126 * Each possible data type has its own decoder, which either operates
127 * through a given PEM name, or attempts to decode to see if the blob
128 * it's given is decodable for its data type. The assumption is that
129 * only the correct data type will match the content.
133 * The try_decode function is called to check if the blob of data can
134 * be used by this handler, and if it can, decodes it into a supported
135 * OpenSSL type and returns a OSSL_STORE_INFO with the decoded data.
137 * pem_name: If this blob comes from a PEM file, this holds
138 * the PEM name. If it comes from another type of
139 * file, this is NULL.
140 * pem_header: If this blob comes from a PEM file, this holds
141 * the PEM headers. If it comes from another type of
142 * file, this is NULL.
143 * blob: The blob of data to match with what this handler
145 * len: The length of the blob.
146 * handler_ctx: For a handler marked repeatable, this pointer can
147 * be used to create a context for the handler. IT IS
148 * THE HANDLER'S RESPONSIBILITY TO CREATE AND DESTROY
149 * THIS CONTEXT APPROPRIATELY, i.e. create on first call
150 * and destroy when about to return NULL.
151 * matchcount: A pointer to an int to count matches for this data.
152 * Usually becomes 0 (no match) or 1 (match!), but may
153 * be higher in the (unlikely) event that the data matches
154 * more than one possibility. The int will always be
155 * zero when the function is called.
156 * ui_method: Application UI method for getting a password, pin
157 * or any other interactive data.
158 * ui_data: Application data to be passed to ui_method when
160 * libctx: The library context to be used if applicable
161 * propq: The property query string for any algorithm fetches
165 typedef OSSL_STORE_INFO
*(*file_try_decode_fn
)(const char *pem_name
,
166 const char *pem_header
,
167 const unsigned char *blob
,
168 size_t len
, void **handler_ctx
,
170 const UI_METHOD
*ui_method
,
175 * The eof function should return 1 if there's no more data to be found
176 * with the handler_ctx, otherwise 0. This is only used when the handler is
179 typedef int (*file_eof_fn
)(void *handler_ctx
);
181 * The destroy_ctx function is used to destroy the handler_ctx that was
182 * initiated by a repeatable try_decode function. This is only used when
183 * the handler is marked repeatable.
185 typedef void (*file_destroy_ctx_fn
)(void **handler_ctx
);
187 typedef struct file_handler_st
{
189 file_try_decode_fn try_decode
;
191 file_destroy_ctx_fn destroy_ctx
;
198 * PKCS#12 decoder. It operates by decoding all of the blob content,
199 * extracting all the interesting data from it and storing them internally,
200 * then serving them one piece at a time.
202 static OSSL_STORE_INFO
*try_decode_PKCS12(const char *pem_name
,
203 const char *pem_header
,
204 const unsigned char *blob
,
205 size_t len
, void **pctx
,
207 const UI_METHOD
*ui_method
,
208 void *ui_data
, OPENSSL_CTX
*libctx
,
211 OSSL_STORE_INFO
*store_info
= NULL
;
212 STACK_OF(OSSL_STORE_INFO
) *ctx
= *pctx
;
215 /* Initial parsing */
219 if (pem_name
!= NULL
)
220 /* No match, there is no PEM PKCS12 tag */
223 if ((p12
= d2i_PKCS12(NULL
, &blob
, len
)) != NULL
) {
225 char tpass
[PEM_BUFSIZE
];
226 EVP_PKEY
*pkey
= NULL
;
228 STACK_OF(X509
) *chain
= NULL
;
232 if (PKCS12_verify_mac(p12
, "", 0)
233 || PKCS12_verify_mac(p12
, NULL
, 0)) {
236 if ((pass
= file_get_pass(ui_method
, tpass
, PEM_BUFSIZE
,
237 "PKCS12 import password",
239 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS12
,
240 OSSL_STORE_R_PASSPHRASE_CALLBACK_ERROR
);
243 if (!PKCS12_verify_mac(p12
, pass
, strlen(pass
))) {
244 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS12
,
245 OSSL_STORE_R_ERROR_VERIFYING_PKCS12_MAC
);
250 if (PKCS12_parse(p12
, pass
, &pkey
, &cert
, &chain
)) {
251 OSSL_STORE_INFO
*osi_pkey
= NULL
;
252 OSSL_STORE_INFO
*osi_cert
= NULL
;
253 OSSL_STORE_INFO
*osi_ca
= NULL
;
255 if ((ctx
= sk_OSSL_STORE_INFO_new_null()) != NULL
256 && (osi_pkey
= OSSL_STORE_INFO_new_PKEY(pkey
)) != NULL
257 && sk_OSSL_STORE_INFO_push(ctx
, osi_pkey
) != 0
258 && (osi_cert
= OSSL_STORE_INFO_new_CERT(cert
)) != NULL
259 && sk_OSSL_STORE_INFO_push(ctx
, osi_cert
) != 0) {
264 while(sk_X509_num(chain
) > 0) {
265 X509
*ca
= sk_X509_value(chain
, 0);
267 if ((osi_ca
= OSSL_STORE_INFO_new_CERT(ca
)) == NULL
268 || sk_OSSL_STORE_INFO_push(ctx
, osi_ca
) == 0) {
273 (void)sk_X509_shift(chain
);
277 OSSL_STORE_INFO_free(osi_ca
);
278 OSSL_STORE_INFO_free(osi_cert
);
279 OSSL_STORE_INFO_free(osi_pkey
);
280 sk_OSSL_STORE_INFO_pop_free(ctx
, OSSL_STORE_INFO_free
);
283 sk_X509_pop_free(chain
, X509_free
);
297 store_info
= sk_OSSL_STORE_INFO_shift(ctx
);
303 static int eof_PKCS12(void *ctx_
)
305 STACK_OF(OSSL_STORE_INFO
) *ctx
= ctx_
;
307 return ctx
== NULL
|| sk_OSSL_STORE_INFO_num(ctx
) == 0;
310 static void destroy_ctx_PKCS12(void **pctx
)
312 STACK_OF(OSSL_STORE_INFO
) *ctx
= *pctx
;
314 sk_OSSL_STORE_INFO_pop_free(ctx
, OSSL_STORE_INFO_free
);
318 static FILE_HANDLER PKCS12_handler
= {
327 * Encrypted PKCS#8 decoder. It operates by just decrypting the given blob
328 * into a new blob, which is returned as an EMBEDDED STORE_INFO. The whole
329 * decoding process will then start over with the new blob.
331 static OSSL_STORE_INFO
*try_decode_PKCS8Encrypted(const char *pem_name
,
332 const char *pem_header
,
333 const unsigned char *blob
,
334 size_t len
, void **pctx
,
336 const UI_METHOD
*ui_method
,
342 char kbuf
[PEM_BUFSIZE
];
344 const X509_ALGOR
*dalg
= NULL
;
345 const ASN1_OCTET_STRING
*doct
= NULL
;
346 OSSL_STORE_INFO
*store_info
= NULL
;
348 unsigned char *new_data
= NULL
;
351 if (pem_name
!= NULL
) {
352 if (strcmp(pem_name
, PEM_STRING_PKCS8
) != 0)
357 if ((p8
= d2i_X509_SIG(NULL
, &blob
, len
)) == NULL
)
362 if ((mem
= BUF_MEM_new()) == NULL
) {
363 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS8ENCRYPTED
,
364 ERR_R_MALLOC_FAILURE
);
368 if ((pass
= file_get_pass(ui_method
, kbuf
, PEM_BUFSIZE
,
369 "PKCS8 decrypt password", ui_data
)) == NULL
) {
370 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS8ENCRYPTED
,
371 OSSL_STORE_R_BAD_PASSWORD_READ
);
375 X509_SIG_get0(p8
, &dalg
, &doct
);
376 if (!PKCS12_pbe_crypt(dalg
, pass
, strlen(pass
), doct
->data
, doct
->length
,
377 &new_data
, &new_data_len
, 0))
380 mem
->data
= (char *)new_data
;
381 mem
->max
= mem
->length
= (size_t)new_data_len
;
384 store_info
= ossl_store_info_new_EMBEDDED(PEM_STRING_PKCS8INF
, mem
);
385 if (store_info
== NULL
) {
386 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS8ENCRYPTED
,
387 ERR_R_MALLOC_FAILURE
);
398 static FILE_HANDLER PKCS8Encrypted_handler
= {
400 try_decode_PKCS8Encrypted
404 * Private key decoder. Decodes all sorts of private keys, both PKCS#8
405 * encoded ones and old style PEM ones (with the key type is encoded into
408 int pem_check_suffix(const char *pem_str
, const char *suffix
);
409 static OSSL_STORE_INFO
*try_decode_PrivateKey(const char *pem_name
,
410 const char *pem_header
,
411 const unsigned char *blob
,
412 size_t len
, void **pctx
,
414 const UI_METHOD
*ui_method
,
415 void *ui_data
, OPENSSL_CTX
*libctx
,
418 OSSL_STORE_INFO
*store_info
= NULL
;
419 EVP_PKEY
*pkey
= NULL
;
420 const EVP_PKEY_ASN1_METHOD
*ameth
= NULL
;
422 if (pem_name
!= NULL
) {
423 if (strcmp(pem_name
, PEM_STRING_PKCS8INF
) == 0) {
424 PKCS8_PRIV_KEY_INFO
*p8inf
=
425 d2i_PKCS8_PRIV_KEY_INFO(NULL
, &blob
, len
);
429 pkey
= evp_pkcs82pkey_int(p8inf
, libctx
, propq
);
430 PKCS8_PRIV_KEY_INFO_free(p8inf
);
434 if ((slen
= pem_check_suffix(pem_name
, "PRIVATE KEY")) > 0
435 && (ameth
= EVP_PKEY_asn1_find_str(NULL
, pem_name
,
438 pkey
= d2i_PrivateKey_ex(ameth
->pkey_id
, NULL
, &blob
, len
,
445 for (i
= 0; i
< EVP_PKEY_asn1_get_count(); i
++) {
446 EVP_PKEY
*tmp_pkey
= NULL
;
447 const unsigned char *tmp_blob
= blob
;
449 ameth
= EVP_PKEY_asn1_get0(i
);
450 if (ameth
->pkey_flags
& ASN1_PKEY_ALIAS
)
453 tmp_pkey
= d2i_PrivateKey_ex(ameth
->pkey_id
, NULL
, &tmp_blob
, len
,
455 if (tmp_pkey
!= NULL
) {
457 EVP_PKEY_free(tmp_pkey
);
464 if (*matchcount
> 1) {
473 store_info
= OSSL_STORE_INFO_new_PKEY(pkey
);
474 if (store_info
== NULL
)
480 static FILE_HANDLER PrivateKey_handler
= {
482 try_decode_PrivateKey
486 * Public key decoder. Only supports SubjectPublicKeyInfo formatted keys.
488 static OSSL_STORE_INFO
*try_decode_PUBKEY(const char *pem_name
,
489 const char *pem_header
,
490 const unsigned char *blob
,
491 size_t len
, void **pctx
,
493 const UI_METHOD
*ui_method
,
494 void *ui_data
, OPENSSL_CTX
*libctx
,
497 OSSL_STORE_INFO
*store_info
= NULL
;
498 EVP_PKEY
*pkey
= NULL
;
500 if (pem_name
!= NULL
) {
501 if (strcmp(pem_name
, PEM_STRING_PUBLIC
) != 0)
507 if ((pkey
= d2i_PUBKEY(NULL
, &blob
, len
)) != NULL
) {
509 store_info
= OSSL_STORE_INFO_new_PKEY(pkey
);
515 static FILE_HANDLER PUBKEY_handler
= {
521 * Key parameter decoder.
523 static OSSL_STORE_INFO
*try_decode_params(const char *pem_name
,
524 const char *pem_header
,
525 const unsigned char *blob
,
526 size_t len
, void **pctx
,
528 const UI_METHOD
*ui_method
,
529 void *ui_data
, OPENSSL_CTX
*libctx
,
532 OSSL_STORE_INFO
*store_info
= NULL
;
534 EVP_PKEY
*pkey
= NULL
;
535 const EVP_PKEY_ASN1_METHOD
*ameth
= NULL
;
538 if (pem_name
!= NULL
) {
539 if ((slen
= pem_check_suffix(pem_name
, "PARAMETERS")) == 0)
545 if ((pkey
= EVP_PKEY_new()) == NULL
) {
546 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PARAMS
, ERR_R_EVP_LIB
);
551 if (EVP_PKEY_set_type_str(pkey
, pem_name
, slen
)
552 && (ameth
= EVP_PKEY_get0_asn1(pkey
)) != NULL
553 && ameth
->param_decode
!= NULL
554 && ameth
->param_decode(pkey
, &blob
, len
))
558 EVP_PKEY
*tmp_pkey
= NULL
;
560 for (i
= 0; i
< EVP_PKEY_asn1_get_count(); i
++) {
561 const unsigned char *tmp_blob
= blob
;
563 if (tmp_pkey
== NULL
&& (tmp_pkey
= EVP_PKEY_new()) == NULL
) {
564 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PARAMS
, ERR_R_EVP_LIB
);
568 ameth
= EVP_PKEY_asn1_get0(i
);
569 if (ameth
->pkey_flags
& ASN1_PKEY_ALIAS
)
572 if (EVP_PKEY_set_type(tmp_pkey
, ameth
->pkey_id
)
573 && (ameth
= EVP_PKEY_get0_asn1(tmp_pkey
)) != NULL
574 && ameth
->param_decode
!= NULL
575 && ameth
->param_decode(tmp_pkey
, &tmp_blob
, len
)) {
577 EVP_PKEY_free(tmp_pkey
);
585 EVP_PKEY_free(tmp_pkey
);
586 if (*matchcount
== 1) {
592 store_info
= OSSL_STORE_INFO_new_PARAMS(pkey
);
593 if (store_info
== NULL
)
599 static FILE_HANDLER params_handler
= {
605 * X.509 certificate decoder.
607 static OSSL_STORE_INFO
*try_decode_X509Certificate(const char *pem_name
,
608 const char *pem_header
,
609 const unsigned char *blob
,
610 size_t len
, void **pctx
,
612 const UI_METHOD
*ui_method
,
617 OSSL_STORE_INFO
*store_info
= NULL
;
621 * In most cases, we can try to interpret the serialized data as a trusted
622 * cert (X509 + X509_AUX) and fall back to reading it as a normal cert
623 * (just X509), but if the PEM name specifically declares it as a trusted
624 * cert, then no fallback should be engaged. |ignore_trusted| tells if
625 * the fallback can be used (1) or not (0).
627 int ignore_trusted
= 1;
629 if (pem_name
!= NULL
) {
630 if (strcmp(pem_name
, PEM_STRING_X509_TRUSTED
) == 0)
632 else if (strcmp(pem_name
, PEM_STRING_X509_OLD
) != 0
633 && strcmp(pem_name
, PEM_STRING_X509
) != 0)
639 if ((cert
= d2i_X509_AUX(NULL
, &blob
, len
)) != NULL
640 || (ignore_trusted
&& (cert
= d2i_X509(NULL
, &blob
, len
)) != NULL
)) {
642 store_info
= OSSL_STORE_INFO_new_CERT(cert
);
645 if (store_info
== NULL
)
651 static FILE_HANDLER X509Certificate_handler
= {
653 try_decode_X509Certificate
659 static OSSL_STORE_INFO
*try_decode_X509CRL(const char *pem_name
,
660 const char *pem_header
,
661 const unsigned char *blob
,
662 size_t len
, void **pctx
,
664 const UI_METHOD
*ui_method
,
665 void *ui_data
, OPENSSL_CTX
*libctx
,
668 OSSL_STORE_INFO
*store_info
= NULL
;
669 X509_CRL
*crl
= NULL
;
671 if (pem_name
!= NULL
) {
672 if (strcmp(pem_name
, PEM_STRING_X509_CRL
) != 0)
678 if ((crl
= d2i_X509_CRL(NULL
, &blob
, len
)) != NULL
) {
680 store_info
= OSSL_STORE_INFO_new_CRL(crl
);
683 if (store_info
== NULL
)
689 static FILE_HANDLER X509CRL_handler
= {
695 * To finish it all off, we collect all the handlers.
697 static const FILE_HANDLER
*file_handlers
[] = {
699 &PKCS8Encrypted_handler
,
700 &X509Certificate_handler
,
713 struct ossl_store_loader_ctx_st
{
720 #define FILE_FLAG_SECMEM (1<<0)
721 #define FILE_FLAG_ATTACHED (1<<1)
724 struct { /* Used with is_raw and is_pem */
728 * The following are used when the handler is marked as
731 const FILE_HANDLER
*last_handler
;
732 void *last_handler_ctx
;
734 struct { /* Used with is_dir */
735 OPENSSL_DIR_CTX
*ctx
;
740 * When a search expression is given, these are filled in.
741 * |search_name| contains the file basename to look for.
742 * The string is exactly 8 characters long.
747 * The directory reading utility we have combines opening with
748 * reading the first name. To make sure we can detect the end
749 * at the right time, we read early and cache the name.
751 const char *last_entry
;
756 /* Expected object type. May be unspecified */
763 static void OSSL_STORE_LOADER_CTX_free(OSSL_STORE_LOADER_CTX
*ctx
)
765 if (ctx
->type
== is_dir
) {
766 OPENSSL_free(ctx
->_
.dir
.uri
);
768 if (ctx
->_
.file
.last_handler
!= NULL
) {
769 ctx
->_
.file
.last_handler
->destroy_ctx(&ctx
->_
.file
.last_handler_ctx
);
770 ctx
->_
.file
.last_handler_ctx
= NULL
;
771 ctx
->_
.file
.last_handler
= NULL
;
774 OPENSSL_free(ctx
->propq
);
778 static int file_find_type(OSSL_STORE_LOADER_CTX
*ctx
)
781 char peekbuf
[4096] = { 0, };
783 if ((buff
= BIO_new(BIO_f_buffer())) == NULL
)
786 ctx
->_
.file
.file
= BIO_push(buff
, ctx
->_
.file
.file
);
787 if (BIO_buffer_peek(ctx
->_
.file
.file
, peekbuf
, sizeof(peekbuf
) - 1) > 0) {
788 peekbuf
[sizeof(peekbuf
) - 1] = '\0';
789 if (strstr(peekbuf
, "-----BEGIN ") != NULL
)
795 static OSSL_STORE_LOADER_CTX
*file_open(const OSSL_STORE_LOADER
*loader
,
797 const UI_METHOD
*ui_method
,
800 OSSL_STORE_LOADER_CTX
*ctx
= NULL
;
804 unsigned int check_absolute
:1;
806 size_t path_data_n
= 0, i
;
810 * First step, just take the URI as is.
812 path_data
[path_data_n
].check_absolute
= 0;
813 path_data
[path_data_n
++].path
= uri
;
816 * Second step, if the URI appears to start with the 'file' scheme,
817 * extract the path and make that the second path to check.
818 * There's a special case if the URI also contains an authority, then
819 * the full URI shouldn't be used as a path anywhere.
821 if (strncasecmp(uri
, "file:", 5) == 0) {
822 const char *p
= &uri
[5];
824 if (strncmp(&uri
[5], "//", 2) == 0) {
825 path_data_n
--; /* Invalidate using the full URI */
826 if (strncasecmp(&uri
[7], "localhost/", 10) == 0) {
828 } else if (uri
[7] == '/') {
831 OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN
,
832 OSSL_STORE_R_URI_AUTHORITY_UNSUPPORTED
);
837 path_data
[path_data_n
].check_absolute
= 1;
839 /* Windows file: URIs with a drive letter start with a / */
840 if (p
[0] == '/' && p
[2] == ':' && p
[3] == '/') {
841 char c
= ossl_tolower(p
[1]);
843 if (c
>= 'a' && c
<= 'z') {
845 /* We know it's absolute, so no need to check */
846 path_data
[path_data_n
].check_absolute
= 0;
850 path_data
[path_data_n
++].path
= p
;
854 for (i
= 0, path
= NULL
; path
== NULL
&& i
< path_data_n
; i
++) {
856 * If the scheme "file" was an explicit part of the URI, the path must
857 * be absolute. So says RFC 8089
859 if (path_data
[i
].check_absolute
&& path_data
[i
].path
[0] != '/') {
860 OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN
,
861 OSSL_STORE_R_PATH_MUST_BE_ABSOLUTE
);
862 ERR_add_error_data(1, path_data
[i
].path
);
866 if (stat(path_data
[i
].path
, &st
) < 0) {
867 ERR_raise_data(ERR_LIB_SYS
, errno
,
871 path
= path_data
[i
].path
;
878 /* Successfully found a working path, clear possible collected errors */
881 ctx
= OPENSSL_zalloc(sizeof(*ctx
));
883 OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN
, ERR_R_MALLOC_FAILURE
);
887 if (S_ISDIR(st
.st_mode
)) {
889 * Try to copy everything, even if we know that some of them must be
890 * NULL for the moment. This prevents errors in the future, when more
891 * components may be used.
893 ctx
->_
.dir
.uri
= OPENSSL_strdup(uri
);
896 if (ctx
->_
.dir
.uri
== NULL
)
899 ctx
->_
.dir
.last_entry
= OPENSSL_DIR_read(&ctx
->_
.dir
.ctx
, path
);
900 ctx
->_
.dir
.last_errno
= errno
;
901 if (ctx
->_
.dir
.last_entry
== NULL
) {
902 if (ctx
->_
.dir
.last_errno
!= 0) {
904 OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN
, ERR_R_SYS_LIB
);
905 errno
= ctx
->_
.dir
.last_errno
;
906 if (openssl_strerror_r(errno
, errbuf
, sizeof(errbuf
)))
907 ERR_add_error_data(1, errbuf
);
910 ctx
->_
.dir
.end_reached
= 1;
912 } else if ((ctx
->_
.file
.file
= BIO_new_file(path
, "rb")) == NULL
913 || !file_find_type(ctx
)) {
914 BIO_free_all(ctx
->_
.file
.file
);
920 OSSL_STORE_LOADER_CTX_free(ctx
);
924 static OSSL_STORE_LOADER_CTX
*file_attach(const OSSL_STORE_LOADER
*loader
,
925 BIO
*bp
, OPENSSL_CTX
*libctx
,
927 const UI_METHOD
*ui_method
,
930 OSSL_STORE_LOADER_CTX
*ctx
;
932 if ((ctx
= OPENSSL_zalloc(sizeof(*ctx
))) == NULL
933 || (propq
!= NULL
&& (ctx
->propq
= OPENSSL_strdup(propq
)) == NULL
)) {
934 OSSL_STOREerr(OSSL_STORE_F_FILE_ATTACH
, ERR_R_MALLOC_FAILURE
);
935 OSSL_STORE_LOADER_CTX_free(ctx
);
939 ctx
->libctx
= libctx
;
940 ctx
->flags
|= FILE_FLAG_ATTACHED
;
941 ctx
->_
.file
.file
= bp
;
942 if (!file_find_type(ctx
)) {
944 ctx
->_
.file
.file
= NULL
;
945 OSSL_STORE_LOADER_CTX_free(ctx
);
952 static int file_ctrl(OSSL_STORE_LOADER_CTX
*ctx
, int cmd
, va_list args
)
957 case OSSL_STORE_C_USE_SECMEM
:
959 int on
= *(va_arg(args
, int *));
963 ctx
->flags
&= ~FILE_FLAG_SECMEM
;
966 ctx
->flags
|= FILE_FLAG_SECMEM
;
969 OSSL_STOREerr(OSSL_STORE_F_FILE_CTRL
,
970 ERR_R_PASSED_INVALID_ARGUMENT
);
983 static int file_expect(OSSL_STORE_LOADER_CTX
*ctx
, int expected
)
985 ctx
->expected_type
= expected
;
989 static int file_find(OSSL_STORE_LOADER_CTX
*ctx
,
990 const OSSL_STORE_SEARCH
*search
)
993 * If ctx == NULL, the library is looking to know if this loader supports
994 * the given search type.
997 if (OSSL_STORE_SEARCH_get_type(search
) == OSSL_STORE_SEARCH_BY_NAME
) {
998 unsigned long hash
= 0;
1003 if (ctx
->type
!= is_dir
) {
1004 OSSL_STOREerr(OSSL_STORE_F_FILE_FIND
,
1005 OSSL_STORE_R_SEARCH_ONLY_SUPPORTED_FOR_DIRECTORIES
);
1009 hash
= X509_NAME_hash(OSSL_STORE_SEARCH_get0_name(search
));
1010 BIO_snprintf(ctx
->_
.dir
.search_name
, sizeof(ctx
->_
.dir
.search_name
),
1016 OSSL_STOREerr(OSSL_STORE_F_FILE_FIND
,
1017 OSSL_STORE_R_UNSUPPORTED_SEARCH_TYPE
);
1021 static OSSL_STORE_INFO
*file_load_try_decode(OSSL_STORE_LOADER_CTX
*ctx
,
1022 const char *pem_name
,
1023 const char *pem_header
,
1024 unsigned char *data
, size_t len
,
1025 const UI_METHOD
*ui_method
,
1026 void *ui_data
, int *matchcount
)
1028 OSSL_STORE_INFO
*result
= NULL
;
1029 BUF_MEM
*new_mem
= NULL
;
1030 char *new_pem_name
= NULL
;
1036 void *handler_ctx
= NULL
;
1037 const FILE_HANDLER
**matching_handlers
=
1038 OPENSSL_zalloc(sizeof(*matching_handlers
)
1039 * OSSL_NELEM(file_handlers
));
1041 if (matching_handlers
== NULL
) {
1042 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD_TRY_DECODE
,
1043 ERR_R_MALLOC_FAILURE
);
1048 for (i
= 0; i
< OSSL_NELEM(file_handlers
); i
++) {
1049 const FILE_HANDLER
*handler
= file_handlers
[i
];
1050 int try_matchcount
= 0;
1051 void *tmp_handler_ctx
= NULL
;
1052 OSSL_STORE_INFO
*tmp_result
=
1053 handler
->try_decode(pem_name
, pem_header
, data
, len
,
1054 &tmp_handler_ctx
, &try_matchcount
,
1055 ui_method
, ui_data
, ctx
->libctx
, ctx
->propq
);
1057 if (try_matchcount
> 0) {
1059 matching_handlers
[*matchcount
] = handler
;
1062 handler
->destroy_ctx(&handler_ctx
);
1063 handler_ctx
= tmp_handler_ctx
;
1065 if ((*matchcount
+= try_matchcount
) > 1) {
1066 /* more than one match => ambiguous, kill any result */
1067 OSSL_STORE_INFO_free(result
);
1068 OSSL_STORE_INFO_free(tmp_result
);
1069 if (handler
->destroy_ctx
!= NULL
)
1070 handler
->destroy_ctx(&handler_ctx
);
1076 result
= tmp_result
;
1080 if (*matchcount
== 1 && matching_handlers
[0]->repeatable
) {
1081 ctx
->_
.file
.last_handler
= matching_handlers
[0];
1082 ctx
->_
.file
.last_handler_ctx
= handler_ctx
;
1085 OPENSSL_free(matching_handlers
);
1089 OPENSSL_free(new_pem_name
);
1090 BUF_MEM_free(new_mem
);
1093 && (t
= OSSL_STORE_INFO_get_type(result
)) == OSSL_STORE_INFO_EMBEDDED
) {
1094 pem_name
= new_pem_name
=
1095 ossl_store_info_get0_EMBEDDED_pem_name(result
);
1096 new_mem
= ossl_store_info_get0_EMBEDDED_buffer(result
);
1097 data
= (unsigned char *)new_mem
->data
;
1098 len
= new_mem
->length
;
1099 OPENSSL_free(result
);
1110 static OSSL_STORE_INFO
*file_load_try_repeat(OSSL_STORE_LOADER_CTX
*ctx
,
1111 const UI_METHOD
*ui_method
,
1114 OSSL_STORE_INFO
*result
= NULL
;
1115 int try_matchcount
= 0;
1117 if (ctx
->_
.file
.last_handler
!= NULL
) {
1119 ctx
->_
.file
.last_handler
->try_decode(NULL
, NULL
, NULL
, 0,
1120 &ctx
->_
.file
.last_handler_ctx
,
1123 ctx
->libctx
, ctx
->propq
);
1125 if (result
== NULL
) {
1126 ctx
->_
.file
.last_handler
->destroy_ctx(&ctx
->_
.file
.last_handler_ctx
);
1127 ctx
->_
.file
.last_handler_ctx
= NULL
;
1128 ctx
->_
.file
.last_handler
= NULL
;
1134 static void pem_free_flag(void *pem_data
, int secure
, size_t num
)
1137 OPENSSL_secure_clear_free(pem_data
, num
);
1139 OPENSSL_free(pem_data
);
1141 static int file_read_pem(BIO
*bp
, char **pem_name
, char **pem_header
,
1142 unsigned char **data
, long *len
,
1143 const UI_METHOD
*ui_method
,
1144 void *ui_data
, int secure
)
1147 ? PEM_read_bio_ex(bp
, pem_name
, pem_header
, data
, len
,
1148 PEM_FLAG_SECURE
| PEM_FLAG_EAY_COMPATIBLE
)
1149 : PEM_read_bio(bp
, pem_name
, pem_header
, data
, len
);
1155 * 10 is the number of characters in "Proc-Type:", which
1156 * PEM_get_EVP_CIPHER_INFO() requires to be present.
1157 * If the PEM header has less characters than that, it's
1158 * not worth spending cycles on it.
1160 if (strlen(*pem_header
) > 10) {
1161 EVP_CIPHER_INFO cipher
;
1162 struct pem_pass_data pass_data
;
1164 if (!PEM_get_EVP_CIPHER_INFO(*pem_header
, &cipher
)
1165 || !file_fill_pem_pass_data(&pass_data
, "PEM", ui_method
, ui_data
)
1166 || !PEM_do_header(&cipher
, *data
, len
, file_get_pem_pass
,
1174 static int file_read_asn1(BIO
*bp
, unsigned char **data
, long *len
)
1176 BUF_MEM
*mem
= NULL
;
1178 if (asn1_d2i_read_bio(bp
, &mem
) < 0)
1181 *data
= (unsigned char *)mem
->data
;
1182 *len
= (long)mem
->length
;
1188 static int ends_with_dirsep(const char *uri
)
1191 uri
+= strlen(uri
) - 1;
1193 if (*uri
== ']' || *uri
== '>' || *uri
== ':')
1195 #elif defined _WIN32
1202 static int file_name_to_uri(OSSL_STORE_LOADER_CTX
*ctx
, const char *name
,
1205 assert(name
!= NULL
);
1206 assert(data
!= NULL
);
1208 const char *pathsep
= ends_with_dirsep(ctx
->_
.dir
.uri
) ? "" : "/";
1209 long calculated_length
= strlen(ctx
->_
.dir
.uri
) + strlen(pathsep
)
1210 + strlen(name
) + 1 /* \0 */;
1212 *data
= OPENSSL_zalloc(calculated_length
);
1213 if (*data
== NULL
) {
1214 OSSL_STOREerr(OSSL_STORE_F_FILE_NAME_TO_URI
, ERR_R_MALLOC_FAILURE
);
1218 OPENSSL_strlcat(*data
, ctx
->_
.dir
.uri
, calculated_length
);
1219 OPENSSL_strlcat(*data
, pathsep
, calculated_length
);
1220 OPENSSL_strlcat(*data
, name
, calculated_length
);
1225 static int file_name_check(OSSL_STORE_LOADER_CTX
*ctx
, const char *name
)
1227 const char *p
= NULL
;
1229 /* If there are no search criteria, all names are accepted */
1230 if (ctx
->_
.dir
.search_name
[0] == '\0')
1233 /* If the expected type isn't supported, no name is accepted */
1234 if (ctx
->expected_type
!= 0
1235 && ctx
->expected_type
!= OSSL_STORE_INFO_CERT
1236 && ctx
->expected_type
!= OSSL_STORE_INFO_CRL
)
1240 * First, check the basename
1242 if (strncasecmp(name
, ctx
->_
.dir
.search_name
,
1243 sizeof(ctx
->_
.dir
.search_name
) - 1) != 0
1244 || name
[sizeof(ctx
->_
.dir
.search_name
) - 1] != '.')
1246 p
= &name
[sizeof(ctx
->_
.dir
.search_name
)];
1249 * Then, if the expected type is a CRL, check that the extension starts
1254 if (ctx
->expected_type
!= 0
1255 && ctx
->expected_type
!= OSSL_STORE_INFO_CRL
)
1257 } else if (ctx
->expected_type
== OSSL_STORE_INFO_CRL
) {
1262 * Last, check that the rest of the extension is a decimal number, at
1263 * least one digit long.
1265 if (!ossl_isdigit(*p
))
1267 while (ossl_isdigit(*p
))
1272 * One extra step here, check for a possible generation number.
1275 for (p
++; *p
!= '\0'; p
++)
1276 if (!ossl_isdigit(*p
))
1281 * If we've reached the end of the string at this point, we've successfully
1282 * found a fitting file name.
1287 static int file_eof(OSSL_STORE_LOADER_CTX
*ctx
);
1288 static int file_error(OSSL_STORE_LOADER_CTX
*ctx
);
1289 static OSSL_STORE_INFO
*file_load(OSSL_STORE_LOADER_CTX
*ctx
,
1290 const UI_METHOD
*ui_method
, void *ui_data
)
1292 OSSL_STORE_INFO
*result
= NULL
;
1297 if (ctx
->type
== is_dir
) {
1299 char *newname
= NULL
;
1301 if (ctx
->_
.dir
.last_entry
== NULL
) {
1302 if (!ctx
->_
.dir
.end_reached
) {
1304 assert(ctx
->_
.dir
.last_errno
!= 0);
1305 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD
, ERR_R_SYS_LIB
);
1306 errno
= ctx
->_
.dir
.last_errno
;
1308 if (openssl_strerror_r(errno
, errbuf
, sizeof(errbuf
)))
1309 ERR_add_error_data(1, errbuf
);
1314 if (ctx
->_
.dir
.last_entry
[0] != '.'
1315 && file_name_check(ctx
, ctx
->_
.dir
.last_entry
)
1316 && !file_name_to_uri(ctx
, ctx
->_
.dir
.last_entry
, &newname
))
1320 * On the first call (with a NULL context), OPENSSL_DIR_read()
1321 * cares about the second argument. On the following calls, it
1322 * only cares that it isn't NULL. Therefore, we can safely give
1325 ctx
->_
.dir
.last_entry
= OPENSSL_DIR_read(&ctx
->_
.dir
.ctx
,
1327 ctx
->_
.dir
.last_errno
= errno
;
1328 if (ctx
->_
.dir
.last_entry
== NULL
&& ctx
->_
.dir
.last_errno
== 0)
1329 ctx
->_
.dir
.end_reached
= 1;
1332 && (result
= OSSL_STORE_INFO_new_NAME(newname
)) == NULL
) {
1333 OPENSSL_free(newname
);
1334 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD
, ERR_R_OSSL_STORE_LIB
);
1337 } while (result
== NULL
&& !file_eof(ctx
));
1339 int matchcount
= -1;
1342 result
= file_load_try_repeat(ctx
, ui_method
, ui_data
);
1350 char *pem_name
= NULL
; /* PEM record name */
1351 char *pem_header
= NULL
; /* PEM record header */
1352 unsigned char *data
= NULL
; /* DER encoded data */
1353 long len
= 0; /* DER encoded data length */
1356 if (ctx
->type
== is_pem
) {
1357 if (!file_read_pem(ctx
->_
.file
.file
, &pem_name
, &pem_header
,
1358 &data
, &len
, ui_method
, ui_data
,
1359 (ctx
->flags
& FILE_FLAG_SECMEM
) != 0)) {
1364 if (!file_read_asn1(ctx
->_
.file
.file
, &data
, &len
)) {
1370 result
= file_load_try_decode(ctx
, pem_name
, pem_header
, data
, len
,
1371 ui_method
, ui_data
, &matchcount
);
1377 * If a PEM name matches more than one handler, the handlers are
1380 if (!ossl_assert(pem_name
== NULL
|| matchcount
<= 1)) {
1385 if (matchcount
> 1) {
1386 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD
,
1387 OSSL_STORE_R_AMBIGUOUS_CONTENT_TYPE
);
1388 } else if (matchcount
== 1) {
1390 * If there are other errors on the stack, they already show
1391 * what the problem is.
1393 if (ERR_peek_error() == 0) {
1394 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD
,
1395 OSSL_STORE_R_UNSUPPORTED_CONTENT_TYPE
);
1396 if (pem_name
!= NULL
)
1397 ERR_add_error_data(3, "PEM type is '", pem_name
, "'");
1404 pem_free_flag(pem_name
, (ctx
->flags
& FILE_FLAG_SECMEM
) != 0, 0);
1405 pem_free_flag(pem_header
, (ctx
->flags
& FILE_FLAG_SECMEM
) != 0, 0);
1406 pem_free_flag(data
, (ctx
->flags
& FILE_FLAG_SECMEM
) != 0, len
);
1407 } while (matchcount
== 0 && !file_eof(ctx
) && !file_error(ctx
));
1409 /* We bail out on ambiguity */
1414 && ctx
->expected_type
!= 0
1415 && ctx
->expected_type
!= OSSL_STORE_INFO_get_type(result
)) {
1416 OSSL_STORE_INFO_free(result
);
1424 static int file_error(OSSL_STORE_LOADER_CTX
*ctx
)
1426 return ctx
->errcnt
> 0;
1429 static int file_eof(OSSL_STORE_LOADER_CTX
*ctx
)
1431 if (ctx
->type
== is_dir
)
1432 return ctx
->_
.dir
.end_reached
;
1434 if (ctx
->_
.file
.last_handler
!= NULL
1435 && !ctx
->_
.file
.last_handler
->eof(ctx
->_
.file
.last_handler_ctx
))
1437 return BIO_eof(ctx
->_
.file
.file
);
1440 static int file_close(OSSL_STORE_LOADER_CTX
*ctx
)
1442 if ((ctx
->flags
& FILE_FLAG_ATTACHED
) == 0) {
1443 if (ctx
->type
== is_dir
)
1444 OPENSSL_DIR_end(&ctx
->_
.dir
.ctx
);
1446 BIO_free_all(ctx
->_
.file
.file
);
1449 * Because file_attach() called file_find_type(), we know that a
1450 * BIO_f_buffer() has been pushed on top of the regular BIO.
1452 BIO
*buff
= ctx
->_
.file
.file
;
1455 (void)BIO_pop(ctx
->_
.file
.file
);
1456 /* Safety measure */
1457 ctx
->_
.file
.file
= NULL
;
1461 OSSL_STORE_LOADER_CTX_free(ctx
);
1465 static OSSL_STORE_LOADER file_loader
=
1480 static void store_file_loader_deinit(void)
1482 ossl_store_unregister_loader_int(file_loader
.scheme
);
1485 int ossl_store_file_loader_init(void)
1487 int ret
= ossl_store_register_loader_int(&file_loader
);
1489 OPENSSL_atexit(store_file_loader_deinit
);