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
14 /* We need to use some STORE deprecated APIs */
15 #define OPENSSL_SUPPRESS_DEPRECATED
19 #include <openssl/crypto.h>
20 #include <openssl/err.h>
21 #include <openssl/trace.h>
22 #include <openssl/core_names.h>
23 #include <openssl/provider.h>
24 #include <openssl/param_build.h>
25 #include <openssl/store.h>
26 #include "internal/thread_once.h"
27 #include "internal/cryptlib.h"
28 #include "internal/provider.h"
29 #include "internal/bio.h"
30 #include "crypto/store.h"
31 #include "store_local.h"
33 static int ossl_store_close_it(OSSL_STORE_CTX
*ctx
);
35 static int loader_set_params(OSSL_STORE_LOADER
*loader
,
36 OSSL_STORE_LOADER_CTX
*loader_ctx
,
37 const OSSL_PARAM params
[], const char *propq
)
40 if (!loader
->p_set_ctx_params(loader_ctx
, params
))
47 if (OSSL_PARAM_locate_const(params
,
48 OSSL_STORE_PARAM_PROPERTIES
) != NULL
)
49 /* use the propq from params */
52 propp
[0] = OSSL_PARAM_construct_utf8_string(OSSL_STORE_PARAM_PROPERTIES
,
54 propp
[1] = OSSL_PARAM_construct_end();
56 if (!loader
->p_set_ctx_params(loader_ctx
, propp
))
63 OSSL_STORE_open_ex(const char *uri
, OSSL_LIB_CTX
*libctx
, const char *propq
,
64 const UI_METHOD
*ui_method
, void *ui_data
,
65 const OSSL_PARAM params
[],
66 OSSL_STORE_post_process_info_fn post_process
,
67 void *post_process_data
)
69 const OSSL_STORE_LOADER
*loader
= NULL
;
70 OSSL_STORE_LOADER
*fetched_loader
= NULL
;
71 OSSL_STORE_LOADER_CTX
*loader_ctx
= NULL
;
72 OSSL_STORE_CTX
*ctx
= NULL
;
73 char *propq_copy
= NULL
;
74 int no_loader_found
= 1;
75 char scheme_copy
[256], *p
, *schemes
[2];
80 * Put the file scheme first. If the uri does represent an existing file,
81 * possible device name and all, then it should be loaded. Only a failed
82 * attempt at loading a local file should have us try something else.
84 schemes
[schemes_n
++] = "file";
87 * Now, check if we have something that looks like a scheme, and add it
88 * as a second scheme. However, also check if there's an authority start
89 * (://), because that will invalidate the previous file scheme. Also,
90 * check that this isn't actually the file scheme, as there's no point
91 * going through that one twice!
93 OPENSSL_strlcpy(scheme_copy
, uri
, sizeof(scheme_copy
));
94 if ((p
= strchr(scheme_copy
, ':')) != NULL
) {
96 if (strcasecmp(scheme_copy
, "file") != 0) {
97 if (strncmp(p
, "//", 2) == 0)
98 schemes_n
--; /* Invalidate the file scheme */
99 schemes
[schemes_n
++] = scheme_copy
;
106 * Try each scheme until we find one that could open the URI.
108 * For each scheme, we look for the engine implementation first, and
109 * failing that, we then try to fetch a provided implementation.
110 * This is consistent with how we handle legacy / engine implementations
113 for (i
= 0; loader_ctx
== NULL
&& i
< schemes_n
; i
++) {
114 OSSL_TRACE1(STORE
, "Looking up scheme %s\n", schemes
[i
]);
115 #ifndef OPENSSL_NO_DEPRECATED_3_0
116 if ((loader
= ossl_store_get0_loader_int(schemes
[i
])) != NULL
) {
118 if (loader
->open_ex
!= NULL
)
119 loader_ctx
= loader
->open_ex(loader
, uri
, libctx
, propq
,
122 loader_ctx
= loader
->open(loader
, uri
, ui_method
, ui_data
);
127 OSSL_STORE_LOADER_fetch(libctx
, schemes
[i
], propq
)) != NULL
) {
128 const OSSL_PROVIDER
*provider
=
129 OSSL_STORE_LOADER_get0_provider(fetched_loader
);
130 void *provctx
= OSSL_PROVIDER_get0_provider_ctx(provider
);
133 loader_ctx
= fetched_loader
->p_open(provctx
, uri
);
134 if (loader_ctx
== NULL
) {
135 OSSL_STORE_LOADER_free(fetched_loader
);
136 fetched_loader
= NULL
;
137 } else if(!loader_set_params(fetched_loader
, loader_ctx
,
139 (void)fetched_loader
->p_close(loader_ctx
);
140 OSSL_STORE_LOADER_free(fetched_loader
);
141 fetched_loader
= NULL
;
143 loader
= fetched_loader
;
149 * It's assumed that ossl_store_get0_loader_int() and
150 * OSSL_STORE_LOADER_fetch() report their own errors
154 OSSL_TRACE1(STORE
, "Found loader for scheme %s\n", schemes
[i
]);
156 if (loader_ctx
== NULL
)
158 * It's assumed that the loader's open() method reports its own
163 OSSL_TRACE2(STORE
, "Opened %s => %p\n", uri
, (void *)loader_ctx
);
165 if ((propq
!= NULL
&& (propq_copy
= OPENSSL_strdup(propq
)) == NULL
)
166 || (ctx
= OPENSSL_zalloc(sizeof(*ctx
))) == NULL
) {
167 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
171 if (ui_method
!= NULL
172 && (!ossl_pw_set_ui_method(&ctx
->pwdata
, ui_method
, ui_data
)
173 || !ossl_pw_enable_passphrase_caching(&ctx
->pwdata
))) {
174 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_CRYPTO_LIB
);
177 ctx
->properties
= propq_copy
;
178 ctx
->fetched_loader
= fetched_loader
;
179 ctx
->loader
= loader
;
180 ctx
->loader_ctx
= loader_ctx
;
181 ctx
->post_process
= post_process
;
182 ctx
->post_process_data
= post_process_data
;
185 * If the attempt to open with the 'file' scheme loader failed and the
186 * other scheme loader succeeded, the failure to open with the 'file'
187 * scheme loader leaves an error on the error stack. Let's remove it.
194 ERR_clear_last_mark();
195 if (loader_ctx
!= NULL
) {
197 * Temporary structure so OSSL_STORE_close() can work even when
198 * |ctx| couldn't be allocated properly
200 OSSL_STORE_CTX tmpctx
= { NULL
, };
202 tmpctx
.fetched_loader
= fetched_loader
;
203 tmpctx
.loader
= loader
;
204 tmpctx
.loader_ctx
= loader_ctx
;
207 * We ignore a returned error because we will return NULL anyway in
208 * this case, so if something goes wrong when closing, that'll simply
209 * just add another entry on the error stack.
211 (void)ossl_store_close_it(&tmpctx
);
213 OSSL_STORE_LOADER_free(fetched_loader
);
214 OPENSSL_free(propq_copy
);
219 OSSL_STORE_CTX
*OSSL_STORE_open(const char *uri
,
220 const UI_METHOD
*ui_method
, void *ui_data
,
221 OSSL_STORE_post_process_info_fn post_process
,
222 void *post_process_data
)
224 return OSSL_STORE_open_ex(uri
, NULL
, NULL
, ui_method
, ui_data
, NULL
,
225 post_process
, post_process_data
);
228 #ifndef OPENSSL_NO_DEPRECATED_3_0
229 int OSSL_STORE_ctrl(OSSL_STORE_CTX
*ctx
, int cmd
, ...)
235 ret
= OSSL_STORE_vctrl(ctx
, cmd
, args
);
241 int OSSL_STORE_vctrl(OSSL_STORE_CTX
*ctx
, int cmd
, va_list args
)
243 if (ctx
->fetched_loader
!= NULL
) {
244 if (ctx
->fetched_loader
->p_set_ctx_params
!= NULL
) {
245 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
248 case OSSL_STORE_C_USE_SECMEM
:
250 int on
= *(va_arg(args
, int *));
252 params
[0] = OSSL_PARAM_construct_int("use_secmem", &on
);
259 return ctx
->fetched_loader
->p_set_ctx_params(ctx
->loader_ctx
,
262 } else if (ctx
->loader
->ctrl
!= NULL
) {
263 return ctx
->loader
->ctrl(ctx
->loader_ctx
, cmd
, args
);
267 * If the fetched loader doesn't have a set_ctx_params or a ctrl, it's as
268 * if there was one that ignored our params, which usually returns 1.
274 int OSSL_STORE_expect(OSSL_STORE_CTX
*ctx
, int expected_type
)
279 || expected_type
< 0 || expected_type
> OSSL_STORE_INFO_CRL
) {
280 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_PASSED_INVALID_ARGUMENT
);
284 ERR_raise(ERR_LIB_OSSL_STORE
, OSSL_STORE_R_LOADING_STARTED
);
288 ctx
->expected_type
= expected_type
;
289 if (ctx
->fetched_loader
!= NULL
290 && ctx
->fetched_loader
->p_set_ctx_params
!= NULL
) {
291 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
294 OSSL_PARAM_construct_int(OSSL_STORE_PARAM_EXPECT
, &expected_type
);
295 ret
= ctx
->fetched_loader
->p_set_ctx_params(ctx
->loader_ctx
, params
);
297 #ifndef OPENSSL_NO_DEPRECATED_3_0
298 if (ctx
->fetched_loader
== NULL
299 && ctx
->loader
->expect
!= NULL
) {
300 ret
= ctx
->loader
->expect(ctx
->loader_ctx
, expected_type
);
306 int OSSL_STORE_find(OSSL_STORE_CTX
*ctx
, const OSSL_STORE_SEARCH
*search
)
311 ERR_raise(ERR_LIB_OSSL_STORE
, OSSL_STORE_R_LOADING_STARTED
);
314 if (search
== NULL
) {
315 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_PASSED_NULL_PARAMETER
);
319 if (ctx
->fetched_loader
!= NULL
) {
322 /* OSSL_STORE_SEARCH_BY_NAME, OSSL_STORE_SEARCH_BY_ISSUER_SERIAL*/
323 void *name_der
= NULL
;
325 /* OSSL_STORE_SEARCH_BY_ISSUER_SERIAL */
326 BIGNUM
*number
= NULL
;
328 if (ctx
->fetched_loader
->p_set_ctx_params
== NULL
) {
329 ERR_raise(ERR_LIB_OSSL_STORE
, OSSL_STORE_R_UNSUPPORTED_OPERATION
);
333 if ((bld
= OSSL_PARAM_BLD_new()) == NULL
) {
334 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
338 ret
= 0; /* Assume the worst */
340 switch (search
->search_type
) {
341 case OSSL_STORE_SEARCH_BY_NAME
:
342 if ((name_der_sz
= i2d_X509_NAME(search
->name
,
343 (unsigned char **)&name_der
)) > 0
344 && OSSL_PARAM_BLD_push_octet_string(bld
,
345 OSSL_STORE_PARAM_SUBJECT
,
346 name_der
, name_der_sz
))
349 case OSSL_STORE_SEARCH_BY_ISSUER_SERIAL
:
350 if ((name_der_sz
= i2d_X509_NAME(search
->name
,
351 (unsigned char **)&name_der
)) > 0
352 && (number
= ASN1_INTEGER_to_BN(search
->serial
, NULL
)) != NULL
353 && OSSL_PARAM_BLD_push_octet_string(bld
,
354 OSSL_STORE_PARAM_ISSUER
,
355 name_der
, name_der_sz
)
356 && OSSL_PARAM_BLD_push_BN(bld
, OSSL_STORE_PARAM_SERIAL
,
360 case OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT
:
361 if (OSSL_PARAM_BLD_push_utf8_string(bld
, OSSL_STORE_PARAM_DIGEST
,
362 EVP_MD_get0_name(search
->digest
),
364 && OSSL_PARAM_BLD_push_octet_string(bld
,
365 OSSL_STORE_PARAM_FINGERPRINT
,
367 search
->stringlength
))
370 case OSSL_STORE_SEARCH_BY_ALIAS
:
371 if (OSSL_PARAM_BLD_push_utf8_string(bld
, OSSL_STORE_PARAM_ALIAS
,
372 (char *)search
->string
,
373 search
->stringlength
))
378 params
= OSSL_PARAM_BLD_to_param(bld
);
379 ret
= ctx
->fetched_loader
->p_set_ctx_params(ctx
->loader_ctx
,
381 OSSL_PARAM_free(params
);
383 OSSL_PARAM_BLD_free(bld
);
384 OPENSSL_free(name_der
);
387 #ifndef OPENSSL_NO_DEPRECATED_3_0
388 /* legacy loader section */
389 if (ctx
->loader
->find
== NULL
) {
390 ERR_raise(ERR_LIB_OSSL_STORE
, OSSL_STORE_R_UNSUPPORTED_OPERATION
);
393 ret
= ctx
->loader
->find(ctx
->loader_ctx
, search
);
400 OSSL_STORE_INFO
*OSSL_STORE_load(OSSL_STORE_CTX
*ctx
)
402 OSSL_STORE_INFO
*v
= NULL
;
406 if (OSSL_STORE_eof(ctx
))
409 if (ctx
->loader
!= NULL
)
410 OSSL_TRACE(STORE
, "Loading next object\n");
412 if (ctx
->cached_info
!= NULL
413 && sk_OSSL_STORE_INFO_num(ctx
->cached_info
) == 0) {
414 sk_OSSL_STORE_INFO_free(ctx
->cached_info
);
415 ctx
->cached_info
= NULL
;
418 if (ctx
->cached_info
!= NULL
) {
419 v
= sk_OSSL_STORE_INFO_shift(ctx
->cached_info
);
421 if (ctx
->fetched_loader
!= NULL
) {
422 struct ossl_load_result_data_st load_data
;
427 if (!ctx
->fetched_loader
->p_load(ctx
->loader_ctx
,
428 ossl_store_handle_load_result
,
430 ossl_pw_passphrase_callback_dec
,
432 if (!OSSL_STORE_eof(ctx
))
438 #ifndef OPENSSL_NO_DEPRECATED_3_0
439 if (ctx
->fetched_loader
== NULL
)
440 v
= ctx
->loader
->load(ctx
->loader_ctx
,
441 ctx
->pwdata
._
.ui_method
.ui_method
,
442 ctx
->pwdata
._
.ui_method
.ui_method_data
);
446 if (ctx
->post_process
!= NULL
&& v
!= NULL
) {
447 v
= ctx
->post_process(v
, ctx
->post_process_data
);
450 * By returning NULL, the callback decides that this object should
457 /* Clear any internally cached passphrase */
458 (void)ossl_pw_clear_passphrase_cache(&ctx
->pwdata
);
460 if (v
!= NULL
&& ctx
->expected_type
!= 0) {
461 int returned_type
= OSSL_STORE_INFO_get_type(v
);
463 if (returned_type
!= OSSL_STORE_INFO_NAME
&& returned_type
!= 0) {
464 if (ctx
->expected_type
!= returned_type
) {
465 OSSL_STORE_INFO_free(v
);
472 OSSL_TRACE1(STORE
, "Got a %s\n",
473 OSSL_STORE_INFO_type_string(OSSL_STORE_INFO_get_type(v
)));
478 int OSSL_STORE_error(OSSL_STORE_CTX
*ctx
)
482 if (ctx
->fetched_loader
!= NULL
)
483 ret
= ctx
->error_flag
;
484 #ifndef OPENSSL_NO_DEPRECATED_3_0
485 if (ctx
->fetched_loader
== NULL
)
486 ret
= ctx
->loader
->error(ctx
->loader_ctx
);
491 int OSSL_STORE_eof(OSSL_STORE_CTX
*ctx
)
495 if (ctx
->fetched_loader
!= NULL
)
496 ret
= ctx
->loader
->p_eof(ctx
->loader_ctx
);
497 #ifndef OPENSSL_NO_DEPRECATED_3_0
498 if (ctx
->fetched_loader
== NULL
)
499 ret
= ctx
->loader
->eof(ctx
->loader_ctx
);
504 static int ossl_store_close_it(OSSL_STORE_CTX
*ctx
)
510 OSSL_TRACE1(STORE
, "Closing %p\n", (void *)ctx
->loader_ctx
);
512 if (ctx
->fetched_loader
!= NULL
)
513 ret
= ctx
->loader
->p_close(ctx
->loader_ctx
);
514 #ifndef OPENSSL_NO_DEPRECATED_3_0
515 if (ctx
->fetched_loader
== NULL
)
516 ret
= ctx
->loader
->close(ctx
->loader_ctx
);
519 sk_OSSL_STORE_INFO_pop_free(ctx
->cached_info
, OSSL_STORE_INFO_free
);
520 OSSL_STORE_LOADER_free(ctx
->fetched_loader
);
521 OPENSSL_free(ctx
->properties
);
522 ossl_pw_clear_passphrase_data(&ctx
->pwdata
);
526 int OSSL_STORE_close(OSSL_STORE_CTX
*ctx
)
528 int ret
= ossl_store_close_it(ctx
);
535 * Functions to generate OSSL_STORE_INFOs, one function for each type we
536 * support having in them as well as a generic constructor.
538 * In all cases, ownership of the object is transferred to the OSSL_STORE_INFO
539 * and will therefore be freed when the OSSL_STORE_INFO is freed.
541 OSSL_STORE_INFO
*OSSL_STORE_INFO_new(int type
, void *data
)
543 OSSL_STORE_INFO
*info
= OPENSSL_zalloc(sizeof(*info
));
553 OSSL_STORE_INFO
*OSSL_STORE_INFO_new_NAME(char *name
)
555 OSSL_STORE_INFO
*info
= OSSL_STORE_INFO_new(OSSL_STORE_INFO_NAME
, NULL
);
558 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
562 info
->_
.name
.name
= name
;
563 info
->_
.name
.desc
= NULL
;
568 int OSSL_STORE_INFO_set0_NAME_description(OSSL_STORE_INFO
*info
, char *desc
)
570 if (info
->type
!= OSSL_STORE_INFO_NAME
) {
571 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_PASSED_INVALID_ARGUMENT
);
575 info
->_
.name
.desc
= desc
;
579 OSSL_STORE_INFO
*OSSL_STORE_INFO_new_PARAMS(EVP_PKEY
*params
)
581 OSSL_STORE_INFO
*info
= OSSL_STORE_INFO_new(OSSL_STORE_INFO_PARAMS
, params
);
584 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
588 OSSL_STORE_INFO
*OSSL_STORE_INFO_new_PUBKEY(EVP_PKEY
*pkey
)
590 OSSL_STORE_INFO
*info
= OSSL_STORE_INFO_new(OSSL_STORE_INFO_PUBKEY
, pkey
);
593 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
597 OSSL_STORE_INFO
*OSSL_STORE_INFO_new_PKEY(EVP_PKEY
*pkey
)
599 OSSL_STORE_INFO
*info
= OSSL_STORE_INFO_new(OSSL_STORE_INFO_PKEY
, pkey
);
602 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
606 OSSL_STORE_INFO
*OSSL_STORE_INFO_new_CERT(X509
*x509
)
608 OSSL_STORE_INFO
*info
= OSSL_STORE_INFO_new(OSSL_STORE_INFO_CERT
, x509
);
611 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
615 OSSL_STORE_INFO
*OSSL_STORE_INFO_new_CRL(X509_CRL
*crl
)
617 OSSL_STORE_INFO
*info
= OSSL_STORE_INFO_new(OSSL_STORE_INFO_CRL
, crl
);
620 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
625 * Functions to try to extract data from a OSSL_STORE_INFO.
627 int OSSL_STORE_INFO_get_type(const OSSL_STORE_INFO
*info
)
632 void *OSSL_STORE_INFO_get0_data(int type
, const OSSL_STORE_INFO
*info
)
634 if (info
->type
== type
)
639 const char *OSSL_STORE_INFO_get0_NAME(const OSSL_STORE_INFO
*info
)
641 if (info
->type
== OSSL_STORE_INFO_NAME
)
642 return info
->_
.name
.name
;
646 char *OSSL_STORE_INFO_get1_NAME(const OSSL_STORE_INFO
*info
)
648 if (info
->type
== OSSL_STORE_INFO_NAME
) {
649 char *ret
= OPENSSL_strdup(info
->_
.name
.name
);
652 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
655 ERR_raise(ERR_LIB_OSSL_STORE
, OSSL_STORE_R_NOT_A_NAME
);
659 const char *OSSL_STORE_INFO_get0_NAME_description(const OSSL_STORE_INFO
*info
)
661 if (info
->type
== OSSL_STORE_INFO_NAME
)
662 return info
->_
.name
.desc
;
666 char *OSSL_STORE_INFO_get1_NAME_description(const OSSL_STORE_INFO
*info
)
668 if (info
->type
== OSSL_STORE_INFO_NAME
) {
669 char *ret
= OPENSSL_strdup(info
->_
.name
.desc
670 ? info
->_
.name
.desc
: "");
673 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
676 ERR_raise(ERR_LIB_OSSL_STORE
, OSSL_STORE_R_NOT_A_NAME
);
680 EVP_PKEY
*OSSL_STORE_INFO_get0_PARAMS(const OSSL_STORE_INFO
*info
)
682 if (info
->type
== OSSL_STORE_INFO_PARAMS
)
683 return info
->_
.params
;
687 EVP_PKEY
*OSSL_STORE_INFO_get1_PARAMS(const OSSL_STORE_INFO
*info
)
689 if (info
->type
== OSSL_STORE_INFO_PARAMS
) {
690 EVP_PKEY_up_ref(info
->_
.params
);
691 return info
->_
.params
;
693 ERR_raise(ERR_LIB_OSSL_STORE
, OSSL_STORE_R_NOT_PARAMETERS
);
697 EVP_PKEY
*OSSL_STORE_INFO_get0_PUBKEY(const OSSL_STORE_INFO
*info
)
699 if (info
->type
== OSSL_STORE_INFO_PUBKEY
)
700 return info
->_
.pubkey
;
704 EVP_PKEY
*OSSL_STORE_INFO_get1_PUBKEY(const OSSL_STORE_INFO
*info
)
706 if (info
->type
== OSSL_STORE_INFO_PUBKEY
) {
707 EVP_PKEY_up_ref(info
->_
.pubkey
);
708 return info
->_
.pubkey
;
710 ERR_raise(ERR_LIB_OSSL_STORE
, OSSL_STORE_R_NOT_A_PUBLIC_KEY
);
714 EVP_PKEY
*OSSL_STORE_INFO_get0_PKEY(const OSSL_STORE_INFO
*info
)
716 if (info
->type
== OSSL_STORE_INFO_PKEY
)
721 EVP_PKEY
*OSSL_STORE_INFO_get1_PKEY(const OSSL_STORE_INFO
*info
)
723 if (info
->type
== OSSL_STORE_INFO_PKEY
) {
724 EVP_PKEY_up_ref(info
->_
.pkey
);
727 ERR_raise(ERR_LIB_OSSL_STORE
, OSSL_STORE_R_NOT_A_PRIVATE_KEY
);
731 X509
*OSSL_STORE_INFO_get0_CERT(const OSSL_STORE_INFO
*info
)
733 if (info
->type
== OSSL_STORE_INFO_CERT
)
738 X509
*OSSL_STORE_INFO_get1_CERT(const OSSL_STORE_INFO
*info
)
740 if (info
->type
== OSSL_STORE_INFO_CERT
) {
741 X509_up_ref(info
->_
.x509
);
744 ERR_raise(ERR_LIB_OSSL_STORE
, OSSL_STORE_R_NOT_A_CERTIFICATE
);
748 X509_CRL
*OSSL_STORE_INFO_get0_CRL(const OSSL_STORE_INFO
*info
)
750 if (info
->type
== OSSL_STORE_INFO_CRL
)
755 X509_CRL
*OSSL_STORE_INFO_get1_CRL(const OSSL_STORE_INFO
*info
)
757 if (info
->type
== OSSL_STORE_INFO_CRL
) {
758 X509_CRL_up_ref(info
->_
.crl
);
761 ERR_raise(ERR_LIB_OSSL_STORE
, OSSL_STORE_R_NOT_A_CRL
);
766 * Free the OSSL_STORE_INFO
768 void OSSL_STORE_INFO_free(OSSL_STORE_INFO
*info
)
771 switch (info
->type
) {
772 case OSSL_STORE_INFO_NAME
:
773 OPENSSL_free(info
->_
.name
.name
);
774 OPENSSL_free(info
->_
.name
.desc
);
776 case OSSL_STORE_INFO_PARAMS
:
777 EVP_PKEY_free(info
->_
.params
);
779 case OSSL_STORE_INFO_PUBKEY
:
780 EVP_PKEY_free(info
->_
.pubkey
);
782 case OSSL_STORE_INFO_PKEY
:
783 EVP_PKEY_free(info
->_
.pkey
);
785 case OSSL_STORE_INFO_CERT
:
786 X509_free(info
->_
.x509
);
788 case OSSL_STORE_INFO_CRL
:
789 X509_CRL_free(info
->_
.crl
);
796 int OSSL_STORE_supports_search(OSSL_STORE_CTX
*ctx
, int search_type
)
800 if (ctx
->fetched_loader
!= NULL
) {
802 ossl_provider_ctx(OSSL_STORE_LOADER_get0_provider(ctx
->fetched_loader
));
803 const OSSL_PARAM
*params
;
804 const OSSL_PARAM
*p_subject
= NULL
;
805 const OSSL_PARAM
*p_issuer
= NULL
;
806 const OSSL_PARAM
*p_serial
= NULL
;
807 const OSSL_PARAM
*p_fingerprint
= NULL
;
808 const OSSL_PARAM
*p_alias
= NULL
;
810 if (ctx
->fetched_loader
->p_settable_ctx_params
== NULL
)
813 params
= ctx
->fetched_loader
->p_settable_ctx_params(provctx
);
814 p_subject
= OSSL_PARAM_locate_const(params
, OSSL_STORE_PARAM_SUBJECT
);
815 p_issuer
= OSSL_PARAM_locate_const(params
, OSSL_STORE_PARAM_ISSUER
);
816 p_serial
= OSSL_PARAM_locate_const(params
, OSSL_STORE_PARAM_SERIAL
);
818 OSSL_PARAM_locate_const(params
, OSSL_STORE_PARAM_FINGERPRINT
);
819 p_alias
= OSSL_PARAM_locate_const(params
, OSSL_STORE_PARAM_ALIAS
);
821 switch (search_type
) {
822 case OSSL_STORE_SEARCH_BY_NAME
:
823 ret
= (p_subject
!= NULL
);
825 case OSSL_STORE_SEARCH_BY_ISSUER_SERIAL
:
826 ret
= (p_issuer
!= NULL
&& p_serial
!= NULL
);
828 case OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT
:
829 ret
= (p_fingerprint
!= NULL
);
831 case OSSL_STORE_SEARCH_BY_ALIAS
:
832 ret
= (p_alias
!= NULL
);
836 #ifndef OPENSSL_NO_DEPRECATED_3_0
837 if (ctx
->fetched_loader
== NULL
) {
838 OSSL_STORE_SEARCH tmp_search
;
840 if (ctx
->loader
->find
== NULL
)
842 tmp_search
.search_type
= search_type
;
843 ret
= ctx
->loader
->find(NULL
, &tmp_search
);
849 /* Search term constructors */
850 OSSL_STORE_SEARCH
*OSSL_STORE_SEARCH_by_name(X509_NAME
*name
)
852 OSSL_STORE_SEARCH
*search
= OPENSSL_zalloc(sizeof(*search
));
854 if (search
== NULL
) {
855 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
859 search
->search_type
= OSSL_STORE_SEARCH_BY_NAME
;
864 OSSL_STORE_SEARCH
*OSSL_STORE_SEARCH_by_issuer_serial(X509_NAME
*name
,
865 const ASN1_INTEGER
*serial
)
867 OSSL_STORE_SEARCH
*search
= OPENSSL_zalloc(sizeof(*search
));
869 if (search
== NULL
) {
870 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
874 search
->search_type
= OSSL_STORE_SEARCH_BY_ISSUER_SERIAL
;
876 search
->serial
= serial
;
880 OSSL_STORE_SEARCH
*OSSL_STORE_SEARCH_by_key_fingerprint(const EVP_MD
*digest
,
884 OSSL_STORE_SEARCH
*search
= OPENSSL_zalloc(sizeof(*search
));
886 if (search
== NULL
) {
887 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
891 if (digest
!= NULL
&& len
!= (size_t)EVP_MD_get_size(digest
)) {
892 ERR_raise_data(ERR_LIB_OSSL_STORE
,
893 OSSL_STORE_R_FINGERPRINT_SIZE_DOES_NOT_MATCH_DIGEST
,
894 "%s size is %d, fingerprint size is %zu",
895 EVP_MD_get0_name(digest
), EVP_MD_get_size(digest
), len
);
896 OPENSSL_free(search
);
900 search
->search_type
= OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT
;
901 search
->digest
= digest
;
902 search
->string
= bytes
;
903 search
->stringlength
= len
;
907 OSSL_STORE_SEARCH
*OSSL_STORE_SEARCH_by_alias(const char *alias
)
909 OSSL_STORE_SEARCH
*search
= OPENSSL_zalloc(sizeof(*search
));
911 if (search
== NULL
) {
912 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
916 search
->search_type
= OSSL_STORE_SEARCH_BY_ALIAS
;
917 search
->string
= (const unsigned char *)alias
;
918 search
->stringlength
= strlen(alias
);
922 /* Search term destructor */
923 void OSSL_STORE_SEARCH_free(OSSL_STORE_SEARCH
*search
)
925 OPENSSL_free(search
);
928 /* Search term accessors */
929 int OSSL_STORE_SEARCH_get_type(const OSSL_STORE_SEARCH
*criterion
)
931 return criterion
->search_type
;
934 X509_NAME
*OSSL_STORE_SEARCH_get0_name(const OSSL_STORE_SEARCH
*criterion
)
936 return criterion
->name
;
939 const ASN1_INTEGER
*OSSL_STORE_SEARCH_get0_serial(const OSSL_STORE_SEARCH
942 return criterion
->serial
;
945 const unsigned char *OSSL_STORE_SEARCH_get0_bytes(const OSSL_STORE_SEARCH
946 *criterion
, size_t *length
)
948 *length
= criterion
->stringlength
;
949 return criterion
->string
;
952 const char *OSSL_STORE_SEARCH_get0_string(const OSSL_STORE_SEARCH
*criterion
)
954 return (const char *)criterion
->string
;
957 const EVP_MD
*OSSL_STORE_SEARCH_get0_digest(const OSSL_STORE_SEARCH
*criterion
)
959 return criterion
->digest
;
962 OSSL_STORE_CTX
*OSSL_STORE_attach(BIO
*bp
, const char *scheme
,
963 OSSL_LIB_CTX
*libctx
, const char *propq
,
964 const UI_METHOD
*ui_method
, void *ui_data
,
965 const OSSL_PARAM params
[],
966 OSSL_STORE_post_process_info_fn post_process
,
967 void *post_process_data
)
969 const OSSL_STORE_LOADER
*loader
= NULL
;
970 OSSL_STORE_LOADER
*fetched_loader
= NULL
;
971 OSSL_STORE_LOADER_CTX
*loader_ctx
= NULL
;
972 OSSL_STORE_CTX
*ctx
= NULL
;
977 OSSL_TRACE1(STORE
, "Looking up scheme %s\n", scheme
);
979 #ifndef OPENSSL_NO_DEPRECATED_3_0
980 if ((loader
= ossl_store_get0_loader_int(scheme
)) != NULL
)
981 loader_ctx
= loader
->attach(loader
, bp
, libctx
, propq
,
986 OSSL_STORE_LOADER_fetch(libctx
, scheme
, propq
)) != NULL
) {
987 const OSSL_PROVIDER
*provider
=
988 OSSL_STORE_LOADER_get0_provider(fetched_loader
);
989 void *provctx
= OSSL_PROVIDER_get0_provider_ctx(provider
);
990 OSSL_CORE_BIO
*cbio
= ossl_core_bio_new_from_bio(bp
);
993 || (loader_ctx
= fetched_loader
->p_attach(provctx
, cbio
)) == NULL
) {
994 OSSL_STORE_LOADER_free(fetched_loader
);
995 fetched_loader
= NULL
;
996 } else if (!loader_set_params(fetched_loader
, loader_ctx
,
998 (void)fetched_loader
->p_close(loader_ctx
);
999 OSSL_STORE_LOADER_free(fetched_loader
);
1000 fetched_loader
= NULL
;
1002 loader
= fetched_loader
;
1003 ossl_core_bio_free(cbio
);
1006 if (loader_ctx
== NULL
) {
1007 ERR_clear_last_mark();
1011 if ((ctx
= OPENSSL_zalloc(sizeof(*ctx
))) == NULL
) {
1012 ERR_clear_last_mark();
1013 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
1017 if (ui_method
!= NULL
1018 && !ossl_pw_set_ui_method(&ctx
->pwdata
, ui_method
, ui_data
)) {
1019 ERR_clear_last_mark();
1024 ctx
->fetched_loader
= fetched_loader
;
1025 ctx
->loader
= loader
;
1026 ctx
->loader_ctx
= loader_ctx
;
1027 ctx
->post_process
= post_process
;
1028 ctx
->post_process_data
= post_process_data
;
1031 * ossl_store_get0_loader_int will raise an error if the loader for the
1032 * the scheme cannot be retrieved. But if a loader was successfully
1033 * fetched then we remove this error from the error stack.