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], *scheme
= NULL
;
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
++) {
115 OSSL_TRACE1(STORE
, "Looking up scheme %s\n", scheme
);
116 #ifndef OPENSSL_NO_DEPRECATED_3_0
117 if ((loader
= ossl_store_get0_loader_int(scheme
)) != NULL
) {
119 if (loader
->open_ex
!= NULL
)
120 loader_ctx
= loader
->open_ex(loader
, uri
, libctx
, propq
,
123 loader_ctx
= loader
->open(loader
, uri
, ui_method
, ui_data
);
128 OSSL_STORE_LOADER_fetch(libctx
, scheme
, propq
)) != NULL
) {
129 const OSSL_PROVIDER
*provider
=
130 OSSL_STORE_LOADER_get0_provider(fetched_loader
);
131 void *provctx
= OSSL_PROVIDER_get0_provider_ctx(provider
);
134 loader_ctx
= fetched_loader
->p_open(provctx
, uri
);
135 if (loader_ctx
== NULL
) {
136 OSSL_STORE_LOADER_free(fetched_loader
);
137 fetched_loader
= NULL
;
138 } else if(!loader_set_params(fetched_loader
, loader_ctx
,
140 (void)fetched_loader
->p_close(loader_ctx
);
141 OSSL_STORE_LOADER_free(fetched_loader
);
142 fetched_loader
= NULL
;
144 loader
= fetched_loader
;
150 * It's assumed that ossl_store_get0_loader_int() and
151 * OSSL_STORE_LOADER_fetch() report their own errors
155 OSSL_TRACE1(STORE
, "Found loader for scheme %s\n", scheme
);
157 if (loader_ctx
== NULL
)
159 * It's assumed that the loader's open() method reports its own
164 OSSL_TRACE2(STORE
, "Opened %s => %p\n", uri
, (void *)loader_ctx
);
166 if ((propq
!= NULL
&& (propq_copy
= OPENSSL_strdup(propq
)) == NULL
)
167 || (ctx
= OPENSSL_zalloc(sizeof(*ctx
))) == NULL
) {
168 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
172 if (ui_method
!= NULL
173 && (!ossl_pw_set_ui_method(&ctx
->pwdata
, ui_method
, ui_data
)
174 || !ossl_pw_enable_passphrase_caching(&ctx
->pwdata
))) {
175 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_CRYPTO_LIB
);
178 ctx
->properties
= propq_copy
;
179 ctx
->fetched_loader
= fetched_loader
;
180 ctx
->loader
= loader
;
181 ctx
->loader_ctx
= loader_ctx
;
182 ctx
->post_process
= post_process
;
183 ctx
->post_process_data
= post_process_data
;
186 * If the attempt to open with the 'file' scheme loader failed and the
187 * other scheme loader succeeded, the failure to open with the 'file'
188 * scheme loader leaves an error on the error stack. Let's remove it.
195 ERR_clear_last_mark();
196 if (loader_ctx
!= NULL
) {
198 * Temporary structure so OSSL_STORE_close() can work even when
199 * |ctx| couldn't be allocated properly
201 OSSL_STORE_CTX tmpctx
= { NULL
, };
203 tmpctx
.fetched_loader
= fetched_loader
;
204 tmpctx
.loader
= loader
;
205 tmpctx
.loader_ctx
= loader_ctx
;
208 * We ignore a returned error because we will return NULL anyway in
209 * this case, so if something goes wrong when closing, that'll simply
210 * just add another entry on the error stack.
212 (void)ossl_store_close_it(&tmpctx
);
214 OSSL_STORE_LOADER_free(fetched_loader
);
215 OPENSSL_free(propq_copy
);
220 OSSL_STORE_CTX
*OSSL_STORE_open(const char *uri
,
221 const UI_METHOD
*ui_method
, void *ui_data
,
222 OSSL_STORE_post_process_info_fn post_process
,
223 void *post_process_data
)
225 return OSSL_STORE_open_ex(uri
, NULL
, NULL
, ui_method
, ui_data
, NULL
,
226 post_process
, post_process_data
);
229 #ifndef OPENSSL_NO_DEPRECATED_3_0
230 int OSSL_STORE_ctrl(OSSL_STORE_CTX
*ctx
, int cmd
, ...)
236 ret
= OSSL_STORE_vctrl(ctx
, cmd
, args
);
242 int OSSL_STORE_vctrl(OSSL_STORE_CTX
*ctx
, int cmd
, va_list args
)
244 if (ctx
->fetched_loader
!= NULL
) {
245 if (ctx
->fetched_loader
->p_set_ctx_params
!= NULL
) {
246 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
249 case OSSL_STORE_C_USE_SECMEM
:
251 int on
= *(va_arg(args
, int *));
253 params
[0] = OSSL_PARAM_construct_int("use_secmem", &on
);
260 return ctx
->fetched_loader
->p_set_ctx_params(ctx
->loader_ctx
,
263 } else if (ctx
->loader
->ctrl
!= NULL
) {
264 return ctx
->loader
->ctrl(ctx
->loader_ctx
, cmd
, args
);
268 * If the fetched loader doesn't have a set_ctx_params or a ctrl, it's as
269 * if there was one that ignored our params, which usually returns 1.
275 int OSSL_STORE_expect(OSSL_STORE_CTX
*ctx
, int expected_type
)
280 || expected_type
< 0 || expected_type
> OSSL_STORE_INFO_CRL
) {
281 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_PASSED_INVALID_ARGUMENT
);
285 ERR_raise(ERR_LIB_OSSL_STORE
, OSSL_STORE_R_LOADING_STARTED
);
289 ctx
->expected_type
= expected_type
;
290 if (ctx
->fetched_loader
!= NULL
291 && ctx
->fetched_loader
->p_set_ctx_params
!= NULL
) {
292 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
295 OSSL_PARAM_construct_int(OSSL_STORE_PARAM_EXPECT
, &expected_type
);
296 ret
= ctx
->fetched_loader
->p_set_ctx_params(ctx
->loader_ctx
, params
);
298 #ifndef OPENSSL_NO_DEPRECATED_3_0
299 if (ctx
->fetched_loader
== NULL
300 && ctx
->loader
->expect
!= NULL
) {
301 ret
= ctx
->loader
->expect(ctx
->loader_ctx
, expected_type
);
307 int OSSL_STORE_find(OSSL_STORE_CTX
*ctx
, const OSSL_STORE_SEARCH
*search
)
312 ERR_raise(ERR_LIB_OSSL_STORE
, OSSL_STORE_R_LOADING_STARTED
);
315 if (search
== NULL
) {
316 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_PASSED_NULL_PARAMETER
);
320 if (ctx
->fetched_loader
!= NULL
) {
323 /* OSSL_STORE_SEARCH_BY_NAME, OSSL_STORE_SEARCH_BY_ISSUER_SERIAL*/
324 void *name_der
= NULL
;
326 /* OSSL_STORE_SEARCH_BY_ISSUER_SERIAL */
327 BIGNUM
*number
= NULL
;
329 if (ctx
->fetched_loader
->p_set_ctx_params
== NULL
) {
330 ERR_raise(ERR_LIB_OSSL_STORE
, OSSL_STORE_R_UNSUPPORTED_OPERATION
);
334 if ((bld
= OSSL_PARAM_BLD_new()) == NULL
) {
335 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
339 ret
= 0; /* Assume the worst */
341 switch (search
->search_type
) {
342 case OSSL_STORE_SEARCH_BY_NAME
:
343 if ((name_der_sz
= i2d_X509_NAME(search
->name
,
344 (unsigned char **)&name_der
)) > 0
345 && OSSL_PARAM_BLD_push_octet_string(bld
,
346 OSSL_STORE_PARAM_SUBJECT
,
347 name_der
, name_der_sz
))
350 case OSSL_STORE_SEARCH_BY_ISSUER_SERIAL
:
351 if ((name_der_sz
= i2d_X509_NAME(search
->name
,
352 (unsigned char **)&name_der
)) > 0
353 && (number
= ASN1_INTEGER_to_BN(search
->serial
, NULL
)) != NULL
354 && OSSL_PARAM_BLD_push_octet_string(bld
,
355 OSSL_STORE_PARAM_ISSUER
,
356 name_der
, name_der_sz
)
357 && OSSL_PARAM_BLD_push_BN(bld
, OSSL_STORE_PARAM_SERIAL
,
361 case OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT
:
362 if (OSSL_PARAM_BLD_push_utf8_string(bld
, OSSL_STORE_PARAM_DIGEST
,
363 EVP_MD_get0_name(search
->digest
),
365 && OSSL_PARAM_BLD_push_octet_string(bld
,
366 OSSL_STORE_PARAM_FINGERPRINT
,
368 search
->stringlength
))
371 case OSSL_STORE_SEARCH_BY_ALIAS
:
372 if (OSSL_PARAM_BLD_push_utf8_string(bld
, OSSL_STORE_PARAM_ALIAS
,
373 (char *)search
->string
,
374 search
->stringlength
))
379 params
= OSSL_PARAM_BLD_to_param(bld
);
380 ret
= ctx
->fetched_loader
->p_set_ctx_params(ctx
->loader_ctx
,
382 OSSL_PARAM_free(params
);
384 OSSL_PARAM_BLD_free(bld
);
385 OPENSSL_free(name_der
);
388 #ifndef OPENSSL_NO_DEPRECATED_3_0
389 /* legacy loader section */
390 if (ctx
->loader
->find
== NULL
) {
391 ERR_raise(ERR_LIB_OSSL_STORE
, OSSL_STORE_R_UNSUPPORTED_OPERATION
);
394 ret
= ctx
->loader
->find(ctx
->loader_ctx
, search
);
401 OSSL_STORE_INFO
*OSSL_STORE_load(OSSL_STORE_CTX
*ctx
)
403 OSSL_STORE_INFO
*v
= NULL
;
407 if (OSSL_STORE_eof(ctx
))
410 if (ctx
->loader
!= NULL
)
411 OSSL_TRACE(STORE
, "Loading next object\n");
413 if (ctx
->cached_info
!= NULL
414 && sk_OSSL_STORE_INFO_num(ctx
->cached_info
) == 0) {
415 sk_OSSL_STORE_INFO_free(ctx
->cached_info
);
416 ctx
->cached_info
= NULL
;
419 if (ctx
->cached_info
!= NULL
) {
420 v
= sk_OSSL_STORE_INFO_shift(ctx
->cached_info
);
422 if (ctx
->fetched_loader
!= NULL
) {
423 struct ossl_load_result_data_st load_data
;
428 if (!ctx
->fetched_loader
->p_load(ctx
->loader_ctx
,
429 ossl_store_handle_load_result
,
431 ossl_pw_passphrase_callback_dec
,
433 if (!OSSL_STORE_eof(ctx
))
439 #ifndef OPENSSL_NO_DEPRECATED_3_0
440 if (ctx
->fetched_loader
== NULL
)
441 v
= ctx
->loader
->load(ctx
->loader_ctx
,
442 ctx
->pwdata
._
.ui_method
.ui_method
,
443 ctx
->pwdata
._
.ui_method
.ui_method_data
);
447 if (ctx
->post_process
!= NULL
&& v
!= NULL
) {
448 v
= ctx
->post_process(v
, ctx
->post_process_data
);
451 * By returning NULL, the callback decides that this object should
458 /* Clear any internally cached passphrase */
459 (void)ossl_pw_clear_passphrase_cache(&ctx
->pwdata
);
461 if (v
!= NULL
&& ctx
->expected_type
!= 0) {
462 int returned_type
= OSSL_STORE_INFO_get_type(v
);
464 if (returned_type
!= OSSL_STORE_INFO_NAME
&& returned_type
!= 0) {
465 if (ctx
->expected_type
!= returned_type
) {
466 OSSL_STORE_INFO_free(v
);
473 OSSL_TRACE1(STORE
, "Got a %s\n",
474 OSSL_STORE_INFO_type_string(OSSL_STORE_INFO_get_type(v
)));
479 int OSSL_STORE_error(OSSL_STORE_CTX
*ctx
)
483 if (ctx
->fetched_loader
!= NULL
)
484 ret
= ctx
->error_flag
;
485 #ifndef OPENSSL_NO_DEPRECATED_3_0
486 if (ctx
->fetched_loader
== NULL
)
487 ret
= ctx
->loader
->error(ctx
->loader_ctx
);
492 int OSSL_STORE_eof(OSSL_STORE_CTX
*ctx
)
496 if (ctx
->fetched_loader
!= NULL
)
497 ret
= ctx
->loader
->p_eof(ctx
->loader_ctx
);
498 #ifndef OPENSSL_NO_DEPRECATED_3_0
499 if (ctx
->fetched_loader
== NULL
)
500 ret
= ctx
->loader
->eof(ctx
->loader_ctx
);
505 static int ossl_store_close_it(OSSL_STORE_CTX
*ctx
)
511 OSSL_TRACE1(STORE
, "Closing %p\n", (void *)ctx
->loader_ctx
);
513 if (ctx
->fetched_loader
!= NULL
)
514 ret
= ctx
->loader
->p_close(ctx
->loader_ctx
);
515 #ifndef OPENSSL_NO_DEPRECATED_3_0
516 if (ctx
->fetched_loader
== NULL
)
517 ret
= ctx
->loader
->close(ctx
->loader_ctx
);
520 sk_OSSL_STORE_INFO_pop_free(ctx
->cached_info
, OSSL_STORE_INFO_free
);
521 OSSL_STORE_LOADER_free(ctx
->fetched_loader
);
522 OPENSSL_free(ctx
->properties
);
523 ossl_pw_clear_passphrase_data(&ctx
->pwdata
);
527 int OSSL_STORE_close(OSSL_STORE_CTX
*ctx
)
529 int ret
= ossl_store_close_it(ctx
);
536 * Functions to generate OSSL_STORE_INFOs, one function for each type we
537 * support having in them as well as a generic constructor.
539 * In all cases, ownership of the object is transferred to the OSSL_STORE_INFO
540 * and will therefore be freed when the OSSL_STORE_INFO is freed.
542 OSSL_STORE_INFO
*OSSL_STORE_INFO_new(int type
, void *data
)
544 OSSL_STORE_INFO
*info
= OPENSSL_zalloc(sizeof(*info
));
554 OSSL_STORE_INFO
*OSSL_STORE_INFO_new_NAME(char *name
)
556 OSSL_STORE_INFO
*info
= OSSL_STORE_INFO_new(OSSL_STORE_INFO_NAME
, NULL
);
559 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
563 info
->_
.name
.name
= name
;
564 info
->_
.name
.desc
= NULL
;
569 int OSSL_STORE_INFO_set0_NAME_description(OSSL_STORE_INFO
*info
, char *desc
)
571 if (info
->type
!= OSSL_STORE_INFO_NAME
) {
572 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_PASSED_INVALID_ARGUMENT
);
576 info
->_
.name
.desc
= desc
;
580 OSSL_STORE_INFO
*OSSL_STORE_INFO_new_PARAMS(EVP_PKEY
*params
)
582 OSSL_STORE_INFO
*info
= OSSL_STORE_INFO_new(OSSL_STORE_INFO_PARAMS
, params
);
585 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
589 OSSL_STORE_INFO
*OSSL_STORE_INFO_new_PUBKEY(EVP_PKEY
*pkey
)
591 OSSL_STORE_INFO
*info
= OSSL_STORE_INFO_new(OSSL_STORE_INFO_PUBKEY
, pkey
);
594 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
598 OSSL_STORE_INFO
*OSSL_STORE_INFO_new_PKEY(EVP_PKEY
*pkey
)
600 OSSL_STORE_INFO
*info
= OSSL_STORE_INFO_new(OSSL_STORE_INFO_PKEY
, pkey
);
603 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
607 OSSL_STORE_INFO
*OSSL_STORE_INFO_new_CERT(X509
*x509
)
609 OSSL_STORE_INFO
*info
= OSSL_STORE_INFO_new(OSSL_STORE_INFO_CERT
, x509
);
612 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
616 OSSL_STORE_INFO
*OSSL_STORE_INFO_new_CRL(X509_CRL
*crl
)
618 OSSL_STORE_INFO
*info
= OSSL_STORE_INFO_new(OSSL_STORE_INFO_CRL
, crl
);
621 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
626 * Functions to try to extract data from a OSSL_STORE_INFO.
628 int OSSL_STORE_INFO_get_type(const OSSL_STORE_INFO
*info
)
633 void *OSSL_STORE_INFO_get0_data(int type
, const OSSL_STORE_INFO
*info
)
635 if (info
->type
== type
)
640 const char *OSSL_STORE_INFO_get0_NAME(const OSSL_STORE_INFO
*info
)
642 if (info
->type
== OSSL_STORE_INFO_NAME
)
643 return info
->_
.name
.name
;
647 char *OSSL_STORE_INFO_get1_NAME(const OSSL_STORE_INFO
*info
)
649 if (info
->type
== OSSL_STORE_INFO_NAME
) {
650 char *ret
= OPENSSL_strdup(info
->_
.name
.name
);
653 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
656 ERR_raise(ERR_LIB_OSSL_STORE
, OSSL_STORE_R_NOT_A_NAME
);
660 const char *OSSL_STORE_INFO_get0_NAME_description(const OSSL_STORE_INFO
*info
)
662 if (info
->type
== OSSL_STORE_INFO_NAME
)
663 return info
->_
.name
.desc
;
667 char *OSSL_STORE_INFO_get1_NAME_description(const OSSL_STORE_INFO
*info
)
669 if (info
->type
== OSSL_STORE_INFO_NAME
) {
670 char *ret
= OPENSSL_strdup(info
->_
.name
.desc
671 ? info
->_
.name
.desc
: "");
674 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
677 ERR_raise(ERR_LIB_OSSL_STORE
, OSSL_STORE_R_NOT_A_NAME
);
681 EVP_PKEY
*OSSL_STORE_INFO_get0_PARAMS(const OSSL_STORE_INFO
*info
)
683 if (info
->type
== OSSL_STORE_INFO_PARAMS
)
684 return info
->_
.params
;
688 EVP_PKEY
*OSSL_STORE_INFO_get1_PARAMS(const OSSL_STORE_INFO
*info
)
690 if (info
->type
== OSSL_STORE_INFO_PARAMS
) {
691 EVP_PKEY_up_ref(info
->_
.params
);
692 return info
->_
.params
;
694 ERR_raise(ERR_LIB_OSSL_STORE
, OSSL_STORE_R_NOT_PARAMETERS
);
698 EVP_PKEY
*OSSL_STORE_INFO_get0_PUBKEY(const OSSL_STORE_INFO
*info
)
700 if (info
->type
== OSSL_STORE_INFO_PUBKEY
)
701 return info
->_
.pubkey
;
705 EVP_PKEY
*OSSL_STORE_INFO_get1_PUBKEY(const OSSL_STORE_INFO
*info
)
707 if (info
->type
== OSSL_STORE_INFO_PUBKEY
) {
708 EVP_PKEY_up_ref(info
->_
.pubkey
);
709 return info
->_
.pubkey
;
711 ERR_raise(ERR_LIB_OSSL_STORE
, OSSL_STORE_R_NOT_A_PUBLIC_KEY
);
715 EVP_PKEY
*OSSL_STORE_INFO_get0_PKEY(const OSSL_STORE_INFO
*info
)
717 if (info
->type
== OSSL_STORE_INFO_PKEY
)
722 EVP_PKEY
*OSSL_STORE_INFO_get1_PKEY(const OSSL_STORE_INFO
*info
)
724 if (info
->type
== OSSL_STORE_INFO_PKEY
) {
725 EVP_PKEY_up_ref(info
->_
.pkey
);
728 ERR_raise(ERR_LIB_OSSL_STORE
, OSSL_STORE_R_NOT_A_PRIVATE_KEY
);
732 X509
*OSSL_STORE_INFO_get0_CERT(const OSSL_STORE_INFO
*info
)
734 if (info
->type
== OSSL_STORE_INFO_CERT
)
739 X509
*OSSL_STORE_INFO_get1_CERT(const OSSL_STORE_INFO
*info
)
741 if (info
->type
== OSSL_STORE_INFO_CERT
) {
742 X509_up_ref(info
->_
.x509
);
745 ERR_raise(ERR_LIB_OSSL_STORE
, OSSL_STORE_R_NOT_A_CERTIFICATE
);
749 X509_CRL
*OSSL_STORE_INFO_get0_CRL(const OSSL_STORE_INFO
*info
)
751 if (info
->type
== OSSL_STORE_INFO_CRL
)
756 X509_CRL
*OSSL_STORE_INFO_get1_CRL(const OSSL_STORE_INFO
*info
)
758 if (info
->type
== OSSL_STORE_INFO_CRL
) {
759 X509_CRL_up_ref(info
->_
.crl
);
762 ERR_raise(ERR_LIB_OSSL_STORE
, OSSL_STORE_R_NOT_A_CRL
);
767 * Free the OSSL_STORE_INFO
769 void OSSL_STORE_INFO_free(OSSL_STORE_INFO
*info
)
772 switch (info
->type
) {
773 case OSSL_STORE_INFO_NAME
:
774 OPENSSL_free(info
->_
.name
.name
);
775 OPENSSL_free(info
->_
.name
.desc
);
777 case OSSL_STORE_INFO_PARAMS
:
778 EVP_PKEY_free(info
->_
.params
);
780 case OSSL_STORE_INFO_PUBKEY
:
781 EVP_PKEY_free(info
->_
.pubkey
);
783 case OSSL_STORE_INFO_PKEY
:
784 EVP_PKEY_free(info
->_
.pkey
);
786 case OSSL_STORE_INFO_CERT
:
787 X509_free(info
->_
.x509
);
789 case OSSL_STORE_INFO_CRL
:
790 X509_CRL_free(info
->_
.crl
);
797 int OSSL_STORE_supports_search(OSSL_STORE_CTX
*ctx
, int search_type
)
801 if (ctx
->fetched_loader
!= NULL
) {
803 ossl_provider_ctx(OSSL_STORE_LOADER_get0_provider(ctx
->fetched_loader
));
804 const OSSL_PARAM
*params
;
805 const OSSL_PARAM
*p_subject
= NULL
;
806 const OSSL_PARAM
*p_issuer
= NULL
;
807 const OSSL_PARAM
*p_serial
= NULL
;
808 const OSSL_PARAM
*p_fingerprint
= NULL
;
809 const OSSL_PARAM
*p_alias
= NULL
;
811 if (ctx
->fetched_loader
->p_settable_ctx_params
== NULL
)
814 params
= ctx
->fetched_loader
->p_settable_ctx_params(provctx
);
815 p_subject
= OSSL_PARAM_locate_const(params
, OSSL_STORE_PARAM_SUBJECT
);
816 p_issuer
= OSSL_PARAM_locate_const(params
, OSSL_STORE_PARAM_ISSUER
);
817 p_serial
= OSSL_PARAM_locate_const(params
, OSSL_STORE_PARAM_SERIAL
);
819 OSSL_PARAM_locate_const(params
, OSSL_STORE_PARAM_FINGERPRINT
);
820 p_alias
= OSSL_PARAM_locate_const(params
, OSSL_STORE_PARAM_ALIAS
);
822 switch (search_type
) {
823 case OSSL_STORE_SEARCH_BY_NAME
:
824 ret
= (p_subject
!= NULL
);
826 case OSSL_STORE_SEARCH_BY_ISSUER_SERIAL
:
827 ret
= (p_issuer
!= NULL
&& p_serial
!= NULL
);
829 case OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT
:
830 ret
= (p_fingerprint
!= NULL
);
832 case OSSL_STORE_SEARCH_BY_ALIAS
:
833 ret
= (p_alias
!= NULL
);
837 #ifndef OPENSSL_NO_DEPRECATED_3_0
838 if (ctx
->fetched_loader
== NULL
) {
839 OSSL_STORE_SEARCH tmp_search
;
841 if (ctx
->loader
->find
== NULL
)
843 tmp_search
.search_type
= search_type
;
844 ret
= ctx
->loader
->find(NULL
, &tmp_search
);
850 /* Search term constructors */
851 OSSL_STORE_SEARCH
*OSSL_STORE_SEARCH_by_name(X509_NAME
*name
)
853 OSSL_STORE_SEARCH
*search
= OPENSSL_zalloc(sizeof(*search
));
855 if (search
== NULL
) {
856 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
860 search
->search_type
= OSSL_STORE_SEARCH_BY_NAME
;
865 OSSL_STORE_SEARCH
*OSSL_STORE_SEARCH_by_issuer_serial(X509_NAME
*name
,
866 const ASN1_INTEGER
*serial
)
868 OSSL_STORE_SEARCH
*search
= OPENSSL_zalloc(sizeof(*search
));
870 if (search
== NULL
) {
871 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
875 search
->search_type
= OSSL_STORE_SEARCH_BY_ISSUER_SERIAL
;
877 search
->serial
= serial
;
881 OSSL_STORE_SEARCH
*OSSL_STORE_SEARCH_by_key_fingerprint(const EVP_MD
*digest
,
885 OSSL_STORE_SEARCH
*search
= OPENSSL_zalloc(sizeof(*search
));
887 if (search
== NULL
) {
888 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
892 if (digest
!= NULL
&& len
!= (size_t)EVP_MD_get_size(digest
)) {
893 ERR_raise_data(ERR_LIB_OSSL_STORE
,
894 OSSL_STORE_R_FINGERPRINT_SIZE_DOES_NOT_MATCH_DIGEST
,
895 "%s size is %d, fingerprint size is %zu",
896 EVP_MD_get0_name(digest
), EVP_MD_get_size(digest
), len
);
897 OPENSSL_free(search
);
901 search
->search_type
= OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT
;
902 search
->digest
= digest
;
903 search
->string
= bytes
;
904 search
->stringlength
= len
;
908 OSSL_STORE_SEARCH
*OSSL_STORE_SEARCH_by_alias(const char *alias
)
910 OSSL_STORE_SEARCH
*search
= OPENSSL_zalloc(sizeof(*search
));
912 if (search
== NULL
) {
913 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
917 search
->search_type
= OSSL_STORE_SEARCH_BY_ALIAS
;
918 search
->string
= (const unsigned char *)alias
;
919 search
->stringlength
= strlen(alias
);
923 /* Search term destructor */
924 void OSSL_STORE_SEARCH_free(OSSL_STORE_SEARCH
*search
)
926 OPENSSL_free(search
);
929 /* Search term accessors */
930 int OSSL_STORE_SEARCH_get_type(const OSSL_STORE_SEARCH
*criterion
)
932 return criterion
->search_type
;
935 X509_NAME
*OSSL_STORE_SEARCH_get0_name(const OSSL_STORE_SEARCH
*criterion
)
937 return criterion
->name
;
940 const ASN1_INTEGER
*OSSL_STORE_SEARCH_get0_serial(const OSSL_STORE_SEARCH
943 return criterion
->serial
;
946 const unsigned char *OSSL_STORE_SEARCH_get0_bytes(const OSSL_STORE_SEARCH
947 *criterion
, size_t *length
)
949 *length
= criterion
->stringlength
;
950 return criterion
->string
;
953 const char *OSSL_STORE_SEARCH_get0_string(const OSSL_STORE_SEARCH
*criterion
)
955 return (const char *)criterion
->string
;
958 const EVP_MD
*OSSL_STORE_SEARCH_get0_digest(const OSSL_STORE_SEARCH
*criterion
)
960 return criterion
->digest
;
963 OSSL_STORE_CTX
*OSSL_STORE_attach(BIO
*bp
, const char *scheme
,
964 OSSL_LIB_CTX
*libctx
, const char *propq
,
965 const UI_METHOD
*ui_method
, void *ui_data
,
966 const OSSL_PARAM params
[],
967 OSSL_STORE_post_process_info_fn post_process
,
968 void *post_process_data
)
970 const OSSL_STORE_LOADER
*loader
= NULL
;
971 OSSL_STORE_LOADER
*fetched_loader
= NULL
;
972 OSSL_STORE_LOADER_CTX
*loader_ctx
= NULL
;
973 OSSL_STORE_CTX
*ctx
= NULL
;
978 OSSL_TRACE1(STORE
, "Looking up scheme %s\n", scheme
);
980 #ifndef OPENSSL_NO_DEPRECATED_3_0
981 if ((loader
= ossl_store_get0_loader_int(scheme
)) != NULL
)
982 loader_ctx
= loader
->attach(loader
, bp
, libctx
, propq
,
987 OSSL_STORE_LOADER_fetch(libctx
, scheme
, propq
)) != NULL
) {
988 const OSSL_PROVIDER
*provider
=
989 OSSL_STORE_LOADER_get0_provider(fetched_loader
);
990 void *provctx
= OSSL_PROVIDER_get0_provider_ctx(provider
);
991 OSSL_CORE_BIO
*cbio
= ossl_core_bio_new_from_bio(bp
);
994 || (loader_ctx
= fetched_loader
->p_attach(provctx
, cbio
)) == NULL
) {
995 OSSL_STORE_LOADER_free(fetched_loader
);
996 fetched_loader
= NULL
;
997 } else if (!loader_set_params(fetched_loader
, loader_ctx
,
999 (void)fetched_loader
->p_close(loader_ctx
);
1000 OSSL_STORE_LOADER_free(fetched_loader
);
1001 fetched_loader
= NULL
;
1003 loader
= fetched_loader
;
1004 ossl_core_bio_free(cbio
);
1007 if (loader_ctx
== NULL
) {
1008 ERR_clear_last_mark();
1012 if ((ctx
= OPENSSL_zalloc(sizeof(*ctx
))) == NULL
) {
1013 ERR_clear_last_mark();
1014 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
1018 if (ui_method
!= NULL
1019 && !ossl_pw_set_ui_method(&ctx
->pwdata
, ui_method
, ui_data
)) {
1020 ERR_clear_last_mark();
1025 ctx
->fetched_loader
= fetched_loader
;
1026 ctx
->loader
= loader
;
1027 ctx
->loader_ctx
= loader_ctx
;
1028 ctx
->post_process
= post_process
;
1029 ctx
->post_process_data
= post_process_data
;
1032 * ossl_store_get0_loader_int will raise an error if the loader for the
1033 * the scheme cannot be retrieved. But if a loader was successfully
1034 * fetched then we remove this error from the error stack.