2 * Copyright 2016-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
14 #include <openssl/bio.h>
15 #include <openssl/dsa.h> /* For d2i_DSAPrivateKey */
16 #include <openssl/err.h>
17 #include <openssl/evp.h>
18 #include <openssl/pem.h>
19 #include <openssl/pkcs12.h> /* For the PKCS8 stuff o.O */
20 #include <openssl/rsa.h> /* For d2i_RSAPrivateKey */
21 #include <openssl/safestack.h>
22 #include <openssl/store.h>
23 #include <openssl/ui.h>
24 #include <openssl/x509.h> /* For the PKCS8 stuff o.O */
25 #include "internal/asn1_int.h"
26 #include "internal/o_dir.h"
27 #include "internal/cryptlib.h"
28 #include "internal/store_int.h"
29 #include "store_locl.h"
42 static char *file_get_pass(const UI_METHOD
*ui_method
, char *pass
,
43 size_t maxsize
, const char *prompt_info
, void *data
)
49 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS
, ERR_R_MALLOC_FAILURE
);
53 if (ui_method
!= NULL
)
54 UI_set_method(ui
, ui_method
);
55 UI_add_user_data(ui
, data
);
57 if ((prompt
= UI_construct_prompt(ui
, "pass phrase",
58 prompt_info
)) == NULL
) {
59 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS
, ERR_R_MALLOC_FAILURE
);
61 } else if (!UI_add_input_string(ui
, prompt
, UI_INPUT_FLAG_DEFAULT_PWD
,
62 pass
, 0, maxsize
- 1)) {
63 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS
, ERR_R_UI_LIB
);
66 switch (UI_process(ui
)) {
68 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS
,
69 OSSL_STORE_R_UI_PROCESS_INTERRUPTED_OR_CANCELLED
);
73 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS
, ERR_R_UI_LIB
);
86 struct pem_pass_data
{
87 const UI_METHOD
*ui_method
;
89 const char *prompt_info
;
92 static int file_fill_pem_pass_data(struct pem_pass_data
*pass_data
,
93 const char *prompt_info
,
94 const UI_METHOD
*ui_method
, void *ui_data
)
96 if (pass_data
== NULL
)
98 pass_data
->ui_method
= ui_method
;
99 pass_data
->data
= ui_data
;
100 pass_data
->prompt_info
= prompt_info
;
104 /* This is used anywhere a pem_password_cb is needed */
105 static int file_get_pem_pass(char *buf
, int num
, int w
, void *data
)
107 struct pem_pass_data
*pass_data
= data
;
108 char *pass
= file_get_pass(pass_data
->ui_method
, buf
, num
,
109 pass_data
->prompt_info
, pass_data
->data
);
111 return pass
== NULL
? 0 : strlen(pass
);
115 * The file scheme decoders
116 * ------------------------
118 * Each possible data type has its own decoder, which either operates
119 * through a given PEM name, or attempts to decode to see if the blob
120 * it's given is decodable for its data type. The assumption is that
121 * only the correct data type will match the content.
125 * The try_decode function is called to check if the blob of data can
126 * be used by this handler, and if it can, decodes it into a supported
127 * OpenSSL type and returns a OSSL_STORE_INFO with the decoded data.
129 * pem_name: If this blob comes from a PEM file, this holds
130 * the PEM name. If it comes from another type of
131 * file, this is NULL.
132 * pem_header: If this blob comes from a PEM file, this holds
133 * the PEM headers. If it comes from another type of
134 * file, this is NULL.
135 * blob: The blob of data to match with what this handler
137 * len: The length of the blob.
138 * handler_ctx: For a handler marked repeatable, this pointer can
139 * be used to create a context for the handler. IT IS
140 * THE HANDLER'S RESPONSIBILITY TO CREATE AND DESTROY
141 * THIS CONTEXT APPROPRIATELY, i.e. create on first call
142 * and destroy when about to return NULL.
143 * matchcount: A pointer to an int to count matches for this data.
144 * Usually becomes 0 (no match) or 1 (match!), but may
145 * be higher in the (unlikely) event that the data matches
146 * more than one possibility. The int will always be
147 * zero when the function is called.
148 * ui_method: Application UI method for getting a password, pin
149 * or any other interactive data.
150 * ui_data: Application data to be passed to ui_method when
155 typedef OSSL_STORE_INFO
*(*file_try_decode_fn
)(const char *pem_name
,
156 const char *pem_header
,
157 const unsigned char *blob
,
158 size_t len
, void **handler_ctx
,
160 const UI_METHOD
*ui_method
,
163 * The eof function should return 1 if there's no more data to be found
164 * with the handler_ctx, otherwise 0. This is only used when the handler is
167 typedef int (*file_eof_fn
)(void *handler_ctx
);
169 * The destroy_ctx function is used to destroy the handler_ctx that was
170 * intiated by a repeatable try_decode fuction. This is only used when
171 * the handler is marked repeatable.
173 typedef void (*file_destroy_ctx_fn
)(void **handler_ctx
);
175 typedef struct file_handler_st
{
177 file_try_decode_fn try_decode
;
179 file_destroy_ctx_fn destroy_ctx
;
186 * PKCS#12 decoder. It operates by decoding all of the blob content,
187 * extracting all the interesting data from it and storing them internally,
188 * then serving them one piece at a time.
190 static OSSL_STORE_INFO
*try_decode_PKCS12(const char *pem_name
,
191 const char *pem_header
,
192 const unsigned char *blob
,
193 size_t len
, void **pctx
,
195 const UI_METHOD
*ui_method
,
198 OSSL_STORE_INFO
*store_info
= NULL
;
199 STACK_OF(OSSL_STORE_INFO
) *ctx
= *pctx
;
202 /* Initial parsing */
206 if (pem_name
!= NULL
)
207 /* No match, there is no PEM PKCS12 tag */
210 if ((p12
= d2i_PKCS12(NULL
, &blob
, len
)) != NULL
) {
212 char tpass
[PEM_BUFSIZE
];
213 EVP_PKEY
*pkey
= NULL
;
215 STACK_OF(X509
) *chain
= NULL
;
219 if (PKCS12_verify_mac(p12
, "", 0)
220 || PKCS12_verify_mac(p12
, NULL
, 0)) {
223 if ((pass
= file_get_pass(ui_method
, tpass
, PEM_BUFSIZE
,
224 "PKCS12 import password",
226 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS12
,
227 OSSL_STORE_R_PASSPHRASE_CALLBACK_ERROR
);
230 if (!PKCS12_verify_mac(p12
, pass
, strlen(pass
))) {
231 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS12
,
232 OSSL_STORE_R_ERROR_VERIFYING_PKCS12_MAC
);
237 if (PKCS12_parse(p12
, pass
, &pkey
, &cert
, &chain
)) {
238 OSSL_STORE_INFO
*si_pkey
= NULL
;
239 OSSL_STORE_INFO
*si_cert
= NULL
;
240 OSSL_STORE_INFO
*si_ca
= NULL
;
242 if ((ctx
= sk_OSSL_STORE_INFO_new_null()) != NULL
243 && (si_pkey
= OSSL_STORE_INFO_new_PKEY(pkey
)) != NULL
244 && sk_OSSL_STORE_INFO_push(ctx
, si_pkey
) != 0
245 && (si_cert
= OSSL_STORE_INFO_new_CERT(cert
)) != NULL
246 && sk_OSSL_STORE_INFO_push(ctx
, si_cert
) != 0) {
251 while(sk_X509_num(chain
) > 0) {
252 X509
*ca
= sk_X509_value(chain
, 0);
254 if ((si_ca
= OSSL_STORE_INFO_new_CERT(ca
)) == NULL
255 || sk_OSSL_STORE_INFO_push(ctx
, si_ca
) == 0) {
260 (void)sk_X509_shift(chain
);
264 OSSL_STORE_INFO_free(si_ca
);
265 OSSL_STORE_INFO_free(si_cert
);
266 OSSL_STORE_INFO_free(si_pkey
);
267 sk_OSSL_STORE_INFO_pop_free(ctx
, OSSL_STORE_INFO_free
);
270 sk_X509_pop_free(chain
, X509_free
);
284 store_info
= sk_OSSL_STORE_INFO_shift(ctx
);
290 static int eof_PKCS12(void *ctx_
)
292 STACK_OF(OSSL_STORE_INFO
) *ctx
= ctx_
;
294 return ctx
== NULL
|| sk_OSSL_STORE_INFO_num(ctx
) == 0;
297 static void destroy_ctx_PKCS12(void **pctx
)
299 STACK_OF(OSSL_STORE_INFO
) *ctx
= *pctx
;
301 sk_OSSL_STORE_INFO_pop_free(ctx
, OSSL_STORE_INFO_free
);
305 static FILE_HANDLER PKCS12_handler
= {
314 * Encrypted PKCS#8 decoder. It operates by just decrypting the given blob
315 * into a new blob, which is returned as an EMBEDDED STORE_INFO. The whole
316 * decoding process will then start over with the new blob.
318 static OSSL_STORE_INFO
*try_decode_PKCS8Encrypted(const char *pem_name
,
319 const char *pem_header
,
320 const unsigned char *blob
,
321 size_t len
, void **pctx
,
323 const UI_METHOD
*ui_method
,
327 char kbuf
[PEM_BUFSIZE
];
329 const X509_ALGOR
*dalg
= NULL
;
330 const ASN1_OCTET_STRING
*doct
= NULL
;
331 OSSL_STORE_INFO
*store_info
= NULL
;
333 unsigned char *new_data
= NULL
;
336 if (pem_name
!= NULL
) {
337 if (strcmp(pem_name
, PEM_STRING_PKCS8
) != 0)
342 if ((p8
= d2i_X509_SIG(NULL
, &blob
, len
)) == NULL
)
347 if ((mem
= BUF_MEM_new()) == NULL
) {
348 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS8ENCRYPTED
,
349 ERR_R_MALLOC_FAILURE
);
353 if ((pass
= file_get_pass(ui_method
, kbuf
, PEM_BUFSIZE
,
354 "PKCS8 decrypt password", ui_data
)) == NULL
) {
355 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS8ENCRYPTED
,
356 OSSL_STORE_R_BAD_PASSWORD_READ
);
360 X509_SIG_get0(p8
, &dalg
, &doct
);
361 if (!PKCS12_pbe_crypt(dalg
, pass
, strlen(pass
), doct
->data
, doct
->length
,
362 &new_data
, &new_data_len
, 0))
365 mem
->data
= (char *)new_data
;
366 mem
->max
= mem
->length
= (size_t)new_data_len
;
369 store_info
= ossl_store_info_new_EMBEDDED(PEM_STRING_PKCS8INF
, mem
);
370 if (store_info
== NULL
) {
371 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS8ENCRYPTED
,
372 ERR_R_MALLOC_FAILURE
);
383 static FILE_HANDLER PKCS8Encrypted_handler
= {
385 try_decode_PKCS8Encrypted
389 * Private key decoder. Decodes all sorts of private keys, both PKCS#8
390 * encoded ones and old style PEM ones (with the key type is encoded into
393 int pem_check_suffix(const char *pem_str
, const char *suffix
);
394 static OSSL_STORE_INFO
*try_decode_PrivateKey(const char *pem_name
,
395 const char *pem_header
,
396 const unsigned char *blob
,
397 size_t len
, void **pctx
,
399 const UI_METHOD
*ui_method
,
402 OSSL_STORE_INFO
*store_info
= NULL
;
403 EVP_PKEY
*pkey
= NULL
;
404 const EVP_PKEY_ASN1_METHOD
*ameth
= NULL
;
406 if (pem_name
!= NULL
) {
407 if (strcmp(pem_name
, PEM_STRING_PKCS8INF
) == 0) {
408 PKCS8_PRIV_KEY_INFO
*p8inf
=
409 d2i_PKCS8_PRIV_KEY_INFO(NULL
, &blob
, len
);
413 pkey
= EVP_PKCS82PKEY(p8inf
);
414 PKCS8_PRIV_KEY_INFO_free(p8inf
);
418 if ((slen
= pem_check_suffix(pem_name
, "PRIVATE KEY")) > 0
419 && (ameth
= EVP_PKEY_asn1_find_str(NULL
, pem_name
,
422 pkey
= d2i_PrivateKey(ameth
->pkey_id
, NULL
, &blob
, len
);
428 for (i
= 0; i
< EVP_PKEY_asn1_get_count(); i
++) {
429 EVP_PKEY
*tmp_pkey
= NULL
;
430 const unsigned char *tmp_blob
= blob
;
432 ameth
= EVP_PKEY_asn1_get0(i
);
433 if (ameth
->pkey_flags
& ASN1_PKEY_ALIAS
)
436 tmp_pkey
= d2i_PrivateKey(ameth
->pkey_id
, NULL
, &tmp_blob
, len
);
437 if (tmp_pkey
!= NULL
) {
439 EVP_PKEY_free(tmp_pkey
);
446 if (*matchcount
> 1) {
455 store_info
= OSSL_STORE_INFO_new_PKEY(pkey
);
456 if (store_info
== NULL
)
462 static FILE_HANDLER PrivateKey_handler
= {
464 try_decode_PrivateKey
468 * Public key decoder. Only supports SubjectPublicKeyInfo formated keys.
470 static OSSL_STORE_INFO
*try_decode_PUBKEY(const char *pem_name
,
471 const char *pem_header
,
472 const unsigned char *blob
,
473 size_t len
, void **pctx
,
475 const UI_METHOD
*ui_method
,
478 OSSL_STORE_INFO
*store_info
= NULL
;
479 EVP_PKEY
*pkey
= NULL
;
481 if (pem_name
!= NULL
) {
482 if (strcmp(pem_name
, PEM_STRING_PUBLIC
) != 0)
488 if ((pkey
= d2i_PUBKEY(NULL
, &blob
, len
)) != NULL
) {
490 store_info
= OSSL_STORE_INFO_new_PKEY(pkey
);
496 static FILE_HANDLER PUBKEY_handler
= {
502 * Key parameter decoder.
504 static OSSL_STORE_INFO
*try_decode_params(const char *pem_name
,
505 const char *pem_header
,
506 const unsigned char *blob
,
507 size_t len
, void **pctx
,
509 const UI_METHOD
*ui_method
,
512 OSSL_STORE_INFO
*store_info
= NULL
;
514 EVP_PKEY
*pkey
= NULL
;
515 const EVP_PKEY_ASN1_METHOD
*ameth
= NULL
;
518 if (pem_name
!= NULL
) {
519 if ((slen
= pem_check_suffix(pem_name
, "PARAMETERS")) == 0)
525 if ((pkey
= EVP_PKEY_new()) == NULL
) {
526 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PARAMS
, ERR_R_EVP_LIB
);
531 if (EVP_PKEY_set_type_str(pkey
, pem_name
, slen
)
532 && (ameth
= EVP_PKEY_get0_asn1(pkey
)) != NULL
533 && ameth
->param_decode
!= NULL
534 && ameth
->param_decode(pkey
, &blob
, len
))
538 EVP_PKEY
*tmp_pkey
= NULL
;
540 for (i
= 0; i
< EVP_PKEY_asn1_get_count(); i
++) {
541 const unsigned char *tmp_blob
= blob
;
543 if (tmp_pkey
== NULL
&& (tmp_pkey
= EVP_PKEY_new()) == NULL
) {
544 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PARAMS
, ERR_R_EVP_LIB
);
548 ameth
= EVP_PKEY_asn1_get0(i
);
549 if (ameth
->pkey_flags
& ASN1_PKEY_ALIAS
)
552 if (EVP_PKEY_set_type(tmp_pkey
, ameth
->pkey_id
)
553 && (ameth
= EVP_PKEY_get0_asn1(tmp_pkey
)) != NULL
554 && ameth
->param_decode
!= NULL
555 && ameth
->param_decode(tmp_pkey
, &tmp_blob
, len
)) {
557 EVP_PKEY_free(tmp_pkey
);
565 EVP_PKEY_free(tmp_pkey
);
566 if (*matchcount
== 1) {
572 store_info
= OSSL_STORE_INFO_new_PARAMS(pkey
);
573 if (store_info
== NULL
)
579 static FILE_HANDLER params_handler
= {
585 * X.509 certificate decoder.
587 static OSSL_STORE_INFO
*try_decode_X509Certificate(const char *pem_name
,
588 const char *pem_header
,
589 const unsigned char *blob
,
590 size_t len
, void **pctx
,
592 const UI_METHOD
*ui_method
,
595 OSSL_STORE_INFO
*store_info
= NULL
;
599 * In most cases, we can try to interpret the serialized data as a trusted
600 * cert (X509 + X509_AUX) and fall back to reading it as a normal cert
601 * (just X509), but if the PEM name specifically declares it as a trusted
602 * cert, then no fallback should be engaged. |ignore_trusted| tells if
603 * the fallback can be used (1) or not (0).
605 int ignore_trusted
= 1;
607 if (pem_name
!= NULL
) {
608 if (strcmp(pem_name
, PEM_STRING_X509_TRUSTED
) == 0)
610 else if (strcmp(pem_name
, PEM_STRING_X509_OLD
) != 0
611 && strcmp(pem_name
, PEM_STRING_X509
) != 0)
617 if ((cert
= d2i_X509_AUX(NULL
, &blob
, len
)) != NULL
618 || (ignore_trusted
&& (cert
= d2i_X509(NULL
, &blob
, len
)) != NULL
)) {
620 store_info
= OSSL_STORE_INFO_new_CERT(cert
);
623 if (store_info
== NULL
)
629 static FILE_HANDLER X509Certificate_handler
= {
631 try_decode_X509Certificate
637 static OSSL_STORE_INFO
*try_decode_X509CRL(const char *pem_name
,
638 const char *pem_header
,
639 const unsigned char *blob
,
640 size_t len
, void **pctx
,
642 const UI_METHOD
*ui_method
,
645 OSSL_STORE_INFO
*store_info
= NULL
;
646 X509_CRL
*crl
= NULL
;
648 if (pem_name
!= NULL
) {
649 if (strcmp(pem_name
, PEM_STRING_X509_CRL
) != 0)
655 if ((crl
= d2i_X509_CRL(NULL
, &blob
, len
)) != NULL
) {
657 store_info
= OSSL_STORE_INFO_new_CRL(crl
);
660 if (store_info
== NULL
)
666 static FILE_HANDLER X509CRL_handler
= {
672 * To finish it all off, we collect all the handlers.
674 static const FILE_HANDLER
*file_handlers
[] = {
676 &PKCS8Encrypted_handler
,
677 &X509Certificate_handler
,
690 struct ossl_store_loader_ctx_st
{
697 #define FILE_FLAG_SECMEM (1<<0)
700 struct { /* Used with is_raw and is_pem */
704 * The following are used when the handler is marked as
707 const FILE_HANDLER
*last_handler
;
708 void *last_handler_ctx
;
710 struct { /* Used with is_dir */
711 OPENSSL_DIR_CTX
*ctx
;
716 * The directory reading utility we have combines opening with
717 * reading the first name. To make sure we can detect the end
718 * at the right time, we read early and cache the name.
720 const char *last_entry
;
726 static void OSSL_STORE_LOADER_CTX_free(OSSL_STORE_LOADER_CTX
*ctx
)
728 if (ctx
->type
== is_dir
) {
729 OPENSSL_free(ctx
->_
.dir
.uri
);
731 if (ctx
->_
.file
.last_handler
!= NULL
) {
732 ctx
->_
.file
.last_handler
->destroy_ctx(&ctx
->_
.file
.last_handler_ctx
);
733 ctx
->_
.file
.last_handler_ctx
= NULL
;
734 ctx
->_
.file
.last_handler
= NULL
;
740 static OSSL_STORE_LOADER_CTX
*file_open(const OSSL_STORE_LOADER
*loader
,
742 const UI_METHOD
*ui_method
,
745 OSSL_STORE_LOADER_CTX
*ctx
= NULL
;
747 const char *paths
[2], *path
;
748 size_t paths_n
= 0, i
;
751 * First step, just take the URI as is.
753 paths
[paths_n
++] = uri
;
756 * Second step, if the URI appears to start with the 'file' scheme,
757 * extract the path and make that the second path to check.
758 * There's a special case if the URI also contains an authority, then
759 * the full URI shouldn't be used as a path anywhere.
761 if (strncasecmp(uri
, "file:", 5) == 0) {
762 const char *p
= &uri
[5];
764 if (strncmp(&uri
[5], "//", 2) == 0) {
765 paths_n
--; /* Invalidate using the full URI */
766 if (strncasecmp(&uri
[7], "localhost/", 10) == 0) {
768 } else if (uri
[7] == '/') {
771 OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN
,
772 OSSL_STORE_R_URI_AUTHORITY_UNSUPPORTED
);
778 * If the scheme "file" was an explicit part of the URI, the path must
779 * be absolute. So says RFC 8089
782 OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN
,
783 OSSL_STORE_R_PATH_MUST_BE_ABSOLUTE
);
788 /* Windows file: URIs with a drive letter start with a / */
789 if (p
[0] == '/' && p
[2] == ':' && p
[3] == '/')
792 paths
[paths_n
++] = p
;
796 for (i
= 0, path
= NULL
; path
== NULL
&& i
< paths_n
; i
++) {
797 if (stat(paths
[i
], &st
) < 0) {
798 SYSerr(SYS_F_STAT
, errno
);
799 ERR_add_error_data(1, paths
[i
]);
808 /* Successfully found a working path, clear possible collected errors */
811 ctx
= OPENSSL_zalloc(sizeof(*ctx
));
813 OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN
, ERR_R_MALLOC_FAILURE
);
817 if ((st
.st_mode
& S_IFDIR
) == S_IFDIR
) {
819 * Try to copy everything, even if we know that some of them must be
820 * NULL for the moment. This prevents errors in the future, when more
821 * components may be used.
823 ctx
->_
.dir
.uri
= OPENSSL_strdup(uri
);
826 if (ctx
->_
.dir
.uri
== NULL
)
829 ctx
->_
.dir
.last_entry
= OPENSSL_DIR_read(&ctx
->_
.dir
.ctx
, path
);
830 ctx
->_
.dir
.last_errno
= errno
;
831 if (ctx
->_
.dir
.last_entry
== NULL
) {
832 if (ctx
->_
.dir
.last_errno
!= 0) {
834 errno
= ctx
->_
.dir
.last_errno
;
835 openssl_strerror_r(errno
, errbuf
, sizeof(errbuf
));
836 OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN
, ERR_R_SYS_LIB
);
837 ERR_add_error_data(1, errbuf
);
840 ctx
->_
.dir
.end_reached
= 1;
846 if ((buff
= BIO_new(BIO_f_buffer())) == NULL
847 || (ctx
->_
.file
.file
= BIO_new_file(path
, "rb")) == NULL
) {
852 ctx
->_
.file
.file
= BIO_push(buff
, ctx
->_
.file
.file
);
853 if (BIO_buffer_peek(ctx
->_
.file
.file
, peekbuf
, sizeof(peekbuf
)-1) > 0) {
854 peekbuf
[sizeof(peekbuf
)-1] = '\0';
855 if (strstr(peekbuf
, "-----BEGIN ") != NULL
)
862 OSSL_STORE_LOADER_CTX_free(ctx
);
866 static int file_ctrl(OSSL_STORE_LOADER_CTX
*ctx
, int cmd
, va_list args
)
871 case OSSL_STORE_C_USE_SECMEM
:
873 int on
= *(va_arg(args
, int *));
877 ctx
->flags
&= ~FILE_FLAG_SECMEM
;
880 ctx
->flags
|= FILE_FLAG_SECMEM
;
883 OSSL_STOREerr(OSSL_STORE_F_FILE_CTRL
,
884 ERR_R_PASSED_INVALID_ARGUMENT
);
897 /* Internal function to decode an already opened PEM file */
898 OSSL_STORE_LOADER_CTX
*ossl_store_file_attach_pem_bio_int(BIO
*bp
)
900 OSSL_STORE_LOADER_CTX
*ctx
= OPENSSL_zalloc(sizeof(*ctx
));
903 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_FILE_ATTACH_PEM_BIO_INT
,
904 ERR_R_MALLOC_FAILURE
);
908 ctx
->_
.file
.file
= bp
;
914 static OSSL_STORE_INFO
*file_load_try_decode(OSSL_STORE_LOADER_CTX
*ctx
,
915 const char *pem_name
,
916 const char *pem_header
,
917 unsigned char *data
, size_t len
,
918 const UI_METHOD
*ui_method
,
919 void *ui_data
, int *matchcount
)
921 OSSL_STORE_INFO
*result
= NULL
;
922 BUF_MEM
*new_mem
= NULL
;
923 char *new_pem_name
= NULL
;
929 void *handler_ctx
= NULL
;
930 const FILE_HANDLER
**matching_handlers
=
931 OPENSSL_zalloc(sizeof(*matching_handlers
)
932 * OSSL_NELEM(file_handlers
));
934 if (matching_handlers
== NULL
) {
935 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD_TRY_DECODE
,
936 ERR_R_MALLOC_FAILURE
);
941 for (i
= 0; i
< OSSL_NELEM(file_handlers
); i
++) {
942 const FILE_HANDLER
*handler
= file_handlers
[i
];
943 int try_matchcount
= 0;
944 void *tmp_handler_ctx
= NULL
;
945 OSSL_STORE_INFO
*tmp_result
=
946 handler
->try_decode(pem_name
, pem_header
, data
, len
,
947 &tmp_handler_ctx
, &try_matchcount
,
950 if (try_matchcount
> 0) {
951 if (matching_handlers
)
952 matching_handlers
[*matchcount
] = handler
;
955 handler
->destroy_ctx(&handler_ctx
);
956 handler_ctx
= tmp_handler_ctx
;
958 if ((*matchcount
+= try_matchcount
) > 1) {
959 /* more than one match => ambiguous, kill any result */
960 OSSL_STORE_INFO_free(result
);
961 OSSL_STORE_INFO_free(tmp_result
);
962 if (handler
->destroy_ctx
!= NULL
)
963 handler
->destroy_ctx(&handler_ctx
);
973 if (*matchcount
== 1 && matching_handlers
[0]->repeatable
) {
974 ctx
->_
.file
.last_handler
= matching_handlers
[0];
975 ctx
->_
.file
.last_handler_ctx
= handler_ctx
;
978 OPENSSL_free(matching_handlers
);
982 OPENSSL_free(new_pem_name
);
983 BUF_MEM_free(new_mem
);
986 && (t
= OSSL_STORE_INFO_get_type(result
)) == OSSL_STORE_INFO_EMBEDDED
) {
987 pem_name
= new_pem_name
=
988 ossl_store_info_get0_EMBEDDED_pem_name(result
);
989 new_mem
= ossl_store_info_get0_EMBEDDED_buffer(result
);
990 data
= (unsigned char *)new_mem
->data
;
991 len
= new_mem
->length
;
992 OPENSSL_free(result
);
1003 static OSSL_STORE_INFO
*file_load_try_repeat(OSSL_STORE_LOADER_CTX
*ctx
,
1004 const UI_METHOD
*ui_method
,
1007 OSSL_STORE_INFO
*result
= NULL
;
1008 int try_matchcount
= 0;
1010 if (ctx
->_
.file
.last_handler
!= NULL
) {
1012 ctx
->_
.file
.last_handler
->try_decode(NULL
, NULL
, NULL
, 0,
1013 &ctx
->_
.file
.last_handler_ctx
,
1015 ui_method
, ui_data
);
1017 if (result
== NULL
) {
1018 ctx
->_
.file
.last_handler
->destroy_ctx(&ctx
->_
.file
.last_handler_ctx
);
1019 ctx
->_
.file
.last_handler_ctx
= NULL
;
1020 ctx
->_
.file
.last_handler
= NULL
;
1026 static void pem_free_flag(void *pem_data
, int secure
)
1029 OPENSSL_secure_free(pem_data
);
1031 OPENSSL_free(pem_data
);
1033 static int file_read_pem(BIO
*bp
, char **pem_name
, char **pem_header
,
1034 unsigned char **data
, long *len
,
1035 const UI_METHOD
*ui_method
,
1036 void *ui_data
, int secure
)
1039 ? PEM_read_bio_ex(bp
, pem_name
, pem_header
, data
, len
,
1040 PEM_FLAG_SECURE
| PEM_FLAG_EAY_COMPATIBLE
)
1041 : PEM_read_bio(bp
, pem_name
, pem_header
, data
, len
);
1047 * 10 is the number of characters in "Proc-Type:", which
1048 * PEM_get_EVP_CIPHER_INFO() requires to be present.
1049 * If the PEM header has less characters than that, it's
1050 * not worth spending cycles on it.
1052 if (strlen(*pem_header
) > 10) {
1053 EVP_CIPHER_INFO cipher
;
1054 struct pem_pass_data pass_data
;
1056 if (!PEM_get_EVP_CIPHER_INFO(*pem_header
, &cipher
)
1057 || !file_fill_pem_pass_data(&pass_data
, "PEM", ui_method
, ui_data
)
1058 || !PEM_do_header(&cipher
, *data
, len
, file_get_pem_pass
,
1066 static int file_read_asn1(BIO
*bp
, unsigned char **data
, long *len
)
1068 BUF_MEM
*mem
= NULL
;
1070 if (asn1_d2i_read_bio(bp
, &mem
) < 0)
1073 *data
= (unsigned char *)mem
->data
;
1074 *len
= (long)mem
->length
;
1080 static int ends_with_dirsep(const char *uri
)
1083 uri
+= strlen(uri
) - 1;
1085 if (*uri
== ']' || *uri
== '>' || *uri
== ':')
1087 #elif defined _WIN32
1094 static int file_name_to_uri(OSSL_STORE_LOADER_CTX
*ctx
, const char *name
,
1097 assert(name
!= NULL
);
1098 assert(data
!= NULL
);
1100 const char *pathsep
= ends_with_dirsep(ctx
->_
.dir
.uri
) ? "" : "/";
1101 long calculated_length
= strlen(ctx
->_
.dir
.uri
) + strlen(pathsep
)
1102 + strlen(name
) + 1 /* \0 */;
1104 *data
= OPENSSL_zalloc(calculated_length
);
1105 if (*data
== NULL
) {
1106 OSSL_STOREerr(OSSL_STORE_F_FILE_NAME_TO_URI
, ERR_R_MALLOC_FAILURE
);
1110 OPENSSL_strlcat(*data
, ctx
->_
.dir
.uri
, calculated_length
);
1111 OPENSSL_strlcat(*data
, pathsep
, calculated_length
);
1112 OPENSSL_strlcat(*data
, name
, calculated_length
);
1117 static int file_eof(OSSL_STORE_LOADER_CTX
*ctx
);
1118 static int file_error(OSSL_STORE_LOADER_CTX
*ctx
);
1119 static OSSL_STORE_INFO
*file_load(OSSL_STORE_LOADER_CTX
*ctx
,
1120 const UI_METHOD
*ui_method
, void *ui_data
)
1122 OSSL_STORE_INFO
*result
= NULL
;
1127 if (ctx
->type
== is_dir
) {
1129 char *newname
= NULL
;
1131 if (ctx
->_
.dir
.last_entry
== NULL
) {
1132 if (!ctx
->_
.dir
.end_reached
) {
1134 assert(ctx
->_
.dir
.last_errno
!= 0);
1135 errno
= ctx
->_
.dir
.last_errno
;
1137 openssl_strerror_r(errno
, errbuf
, sizeof(errbuf
));
1138 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD
, ERR_R_SYS_LIB
);
1139 ERR_add_error_data(1, errbuf
);
1144 if (ctx
->_
.dir
.last_entry
[0] != '.'
1145 && !file_name_to_uri(ctx
, ctx
->_
.dir
.last_entry
, &newname
))
1149 * On the first call (with a NULL context), OPENSSL_DIR_read()
1150 * cares about the second argument. On the following calls, it
1151 * only cares that it isn't NULL. Therefore, we can safely give
1154 ctx
->_
.dir
.last_entry
= OPENSSL_DIR_read(&ctx
->_
.dir
.ctx
,
1156 ctx
->_
.dir
.last_errno
= errno
;
1157 if (ctx
->_
.dir
.last_entry
== NULL
&& ctx
->_
.dir
.last_errno
== 0)
1158 ctx
->_
.dir
.end_reached
= 1;
1161 && (result
= OSSL_STORE_INFO_new_NAME(newname
)) == NULL
) {
1162 OPENSSL_free(newname
);
1163 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD
, ERR_R_OSSL_STORE_LIB
);
1166 } while (result
== NULL
&& !file_eof(ctx
));
1168 int matchcount
= -1;
1170 result
= file_load_try_repeat(ctx
, ui_method
, ui_data
);
1178 char *pem_name
= NULL
; /* PEM record name */
1179 char *pem_header
= NULL
; /* PEM record header */
1180 unsigned char *data
= NULL
; /* DER encoded data */
1181 long len
= 0; /* DER encoded data length */
1184 if (ctx
->type
== is_pem
) {
1185 if (!file_read_pem(ctx
->_
.file
.file
, &pem_name
, &pem_header
,
1186 &data
, &len
, ui_method
, ui_data
,
1187 (ctx
->flags
& FILE_FLAG_SECMEM
) != 0)) {
1192 if (!file_read_asn1(ctx
->_
.file
.file
, &data
, &len
)) {
1198 result
= file_load_try_decode(ctx
, pem_name
, pem_header
, data
, len
,
1199 ui_method
, ui_data
, &matchcount
);
1205 * If a PEM name matches more than one handler, the handlers are
1208 if (!ossl_assert(pem_name
== NULL
|| matchcount
<= 1)) {
1213 if (matchcount
> 1) {
1214 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD
,
1215 OSSL_STORE_R_AMBIGUOUS_CONTENT_TYPE
);
1216 } else if (matchcount
== 1) {
1218 * If there are other errors on the stack, they already show
1219 * what the problem is.
1221 if (ERR_peek_error() == 0) {
1222 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD
,
1223 OSSL_STORE_R_UNSUPPORTED_CONTENT_TYPE
);
1224 if (pem_name
!= NULL
)
1225 ERR_add_error_data(3, "PEM type is '", pem_name
, "'");
1232 pem_free_flag(pem_name
, (ctx
->flags
& FILE_FLAG_SECMEM
) != 0);
1233 pem_free_flag(pem_header
, (ctx
->flags
& FILE_FLAG_SECMEM
) != 0);
1234 pem_free_flag(data
, (ctx
->flags
& FILE_FLAG_SECMEM
) != 0);
1235 } while (matchcount
== 0 && !file_eof(ctx
) && !file_error(ctx
));
1237 /* We bail out on ambiguity */
1245 static int file_error(OSSL_STORE_LOADER_CTX
*ctx
)
1247 return ctx
->errcnt
> 0;
1250 static int file_eof(OSSL_STORE_LOADER_CTX
*ctx
)
1252 if (ctx
->type
== is_dir
)
1253 return ctx
->_
.dir
.end_reached
;
1255 if (ctx
->_
.file
.last_handler
!= NULL
1256 && !ctx
->_
.file
.last_handler
->eof(ctx
->_
.file
.last_handler_ctx
))
1258 return BIO_eof(ctx
->_
.file
.file
);
1261 static int file_close(OSSL_STORE_LOADER_CTX
*ctx
)
1263 if (ctx
->type
== is_dir
) {
1264 OPENSSL_DIR_end(&ctx
->_
.dir
.ctx
);
1266 BIO_free_all(ctx
->_
.file
.file
);
1268 OSSL_STORE_LOADER_CTX_free(ctx
);
1272 int ossl_store_file_detach_pem_bio_int(OSSL_STORE_LOADER_CTX
*ctx
)
1274 OSSL_STORE_LOADER_CTX_free(ctx
);
1278 static OSSL_STORE_LOADER file_loader
=
1290 static void store_file_loader_deinit(void)
1292 ossl_store_unregister_loader_int(file_loader
.scheme
);
1295 int ossl_store_file_loader_init(void)
1297 int ret
= ossl_store_register_loader_int(&file_loader
);
1299 OPENSSL_atexit(store_file_loader_deinit
);