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
);
36 OSSL_STORE_open_ex(const char *uri
, OSSL_LIB_CTX
*libctx
, const char *propq
,
37 const UI_METHOD
*ui_method
, void *ui_data
,
38 OSSL_STORE_post_process_info_fn post_process
,
39 void *post_process_data
)
41 const OSSL_STORE_LOADER
*loader
= NULL
;
42 OSSL_STORE_LOADER
*fetched_loader
= NULL
;
43 OSSL_STORE_LOADER_CTX
*loader_ctx
= NULL
;
44 OSSL_STORE_CTX
*ctx
= NULL
;
45 char *propq_copy
= NULL
;
46 char scheme_copy
[256], *p
, *schemes
[2];
51 * Put the file scheme first. If the uri does represent an existing file,
52 * possible device name and all, then it should be loaded. Only a failed
53 * attempt at loading a local file should have us try something else.
55 schemes
[schemes_n
++] = "file";
58 * Now, check if we have something that looks like a scheme, and add it
59 * as a second scheme. However, also check if there's an authority start
60 * (://), because that will invalidate the previous file scheme. Also,
61 * check that this isn't actually the file scheme, as there's no point
62 * going through that one twice!
64 OPENSSL_strlcpy(scheme_copy
, uri
, sizeof(scheme_copy
));
65 if ((p
= strchr(scheme_copy
, ':')) != NULL
) {
67 if (strcasecmp(scheme_copy
, "file") != 0) {
68 if (strncmp(p
, "//", 2) == 0)
69 schemes_n
--; /* Invalidate the file scheme */
70 schemes
[schemes_n
++] = scheme_copy
;
77 * Try each scheme until we find one that could open the URI.
79 * For each scheme, we look for the engine implementation first, and
80 * failing that, we then try to fetch a provided implementation.
81 * This is consistent with how we handle legacy / engine implementations
84 for (i
= 0; loader_ctx
== NULL
&& i
< schemes_n
; i
++) {
85 OSSL_TRACE1(STORE
, "Looking up scheme %s\n", schemes
[i
]);
86 #ifndef OPENSSL_NO_DEPRECATED_3_0
87 if ((loader
= ossl_store_get0_loader_int(schemes
[i
])) != NULL
) {
88 if (loader
->open_ex
!= NULL
)
89 loader_ctx
= loader
->open_ex(loader
, uri
, libctx
, propq
,
92 loader_ctx
= loader
->open(loader
, uri
, ui_method
, ui_data
);
97 OSSL_STORE_LOADER_fetch(schemes
[i
], libctx
, propq
)) != NULL
) {
98 const OSSL_PROVIDER
*provider
=
99 OSSL_STORE_LOADER_provider(fetched_loader
);
100 void *provctx
= OSSL_PROVIDER_get0_provider_ctx(provider
);
102 loader_ctx
= fetched_loader
->p_open(provctx
, uri
);
103 if (loader_ctx
== NULL
) {
104 OSSL_STORE_LOADER_free(fetched_loader
);
105 fetched_loader
= NULL
;
106 } else if (propq
!= NULL
) {
107 OSSL_PARAM params
[2];
109 params
[0] = OSSL_PARAM_construct_utf8_string(
110 OSSL_STORE_PARAM_PROPERTIES
, (char *)propq
, 0);
111 params
[1] = OSSL_PARAM_construct_end();
113 if (!fetched_loader
->p_set_ctx_params(loader_ctx
, params
)) {
114 (void)fetched_loader
->p_close(loader_ctx
);
115 OSSL_STORE_LOADER_free(fetched_loader
);
116 fetched_loader
= NULL
;
119 loader
= fetched_loader
;
124 OSSL_TRACE1(STORE
, "Found loader for scheme %s\n", schemes
[i
]);
126 if (loader_ctx
== NULL
)
129 OSSL_TRACE2(STORE
, "Opened %s => %p\n", uri
, (void *)loader_ctx
);
131 if ((propq
!= NULL
&& (propq_copy
= OPENSSL_strdup(propq
)) == NULL
)
132 || (ctx
= OPENSSL_zalloc(sizeof(*ctx
))) == NULL
) {
133 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
137 if (ui_method
!= NULL
138 && !ossl_pw_set_ui_method(&ctx
->pwdata
, ui_method
, ui_data
)) {
139 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_CRYPTO_LIB
);
142 ctx
->properties
= propq_copy
;
143 ctx
->fetched_loader
= fetched_loader
;
144 ctx
->loader
= loader
;
145 ctx
->loader_ctx
= loader_ctx
;
146 ctx
->post_process
= post_process
;
147 ctx
->post_process_data
= post_process_data
;
150 * If the attempt to open with the 'file' scheme loader failed and the
151 * other scheme loader succeeded, the failure to open with the 'file'
152 * scheme loader leaves an error on the error stack. Let's remove it.
159 ERR_clear_last_mark();
160 if (loader_ctx
!= NULL
) {
162 * Temporary structure so OSSL_STORE_close() can work even when
163 * |ctx| couldn't be allocated properly
165 OSSL_STORE_CTX tmpctx
= { NULL
, };
167 tmpctx
.fetched_loader
= fetched_loader
;
168 tmpctx
.loader
= loader
;
169 tmpctx
.loader_ctx
= loader_ctx
;
172 * We ignore a returned error because we will return NULL anyway in
173 * this case, so if something goes wrong when closing, that'll simply
174 * just add another entry on the error stack.
176 (void)ossl_store_close_it(&tmpctx
);
178 OSSL_STORE_LOADER_free(fetched_loader
);
179 OPENSSL_free(propq_copy
);
184 OSSL_STORE_CTX
*OSSL_STORE_open(const char *uri
,
185 const UI_METHOD
*ui_method
, void *ui_data
,
186 OSSL_STORE_post_process_info_fn post_process
,
187 void *post_process_data
)
189 return OSSL_STORE_open_ex(uri
, NULL
, NULL
, ui_method
, ui_data
, post_process
,
193 #ifndef OPENSSL_NO_DEPRECATED_3_0
194 int OSSL_STORE_ctrl(OSSL_STORE_CTX
*ctx
, int cmd
, ...)
200 ret
= OSSL_STORE_vctrl(ctx
, cmd
, args
);
206 int OSSL_STORE_vctrl(OSSL_STORE_CTX
*ctx
, int cmd
, va_list args
)
208 if (ctx
->fetched_loader
!= NULL
) {
209 if (ctx
->fetched_loader
->p_set_ctx_params
!= NULL
) {
210 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
213 case OSSL_STORE_C_USE_SECMEM
:
215 int on
= *(va_arg(args
, int *));
217 params
[0] = OSSL_PARAM_construct_int("use_secmem", &on
);
224 return ctx
->fetched_loader
->p_set_ctx_params(ctx
->loader_ctx
,
227 } else if (ctx
->loader
->ctrl
!= NULL
) {
228 return ctx
->loader
->ctrl(ctx
->loader_ctx
, cmd
, args
);
232 * If the fetched loader doesn't have a set_ctx_params or a ctrl, it's as
233 * if there was one that ignored our params, which usually returns 1.
239 int OSSL_STORE_expect(OSSL_STORE_CTX
*ctx
, int expected_type
)
244 ERR_raise(ERR_LIB_OSSL_STORE
, OSSL_STORE_R_LOADING_STARTED
);
248 ctx
->expected_type
= expected_type
;
249 if (ctx
->fetched_loader
!= NULL
250 && ctx
->fetched_loader
->p_set_ctx_params
!= NULL
) {
251 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
254 OSSL_PARAM_construct_int(OSSL_STORE_PARAM_EXPECT
, &expected_type
);
255 ret
= ctx
->fetched_loader
->p_set_ctx_params(ctx
->loader_ctx
, params
);
257 #ifndef OPENSSL_NO_DEPRECATED_3_0
258 if (ctx
->fetched_loader
== NULL
259 && ctx
->loader
->expect
!= NULL
) {
260 ret
= ctx
->loader
->expect(ctx
->loader_ctx
, expected_type
);
266 int OSSL_STORE_find(OSSL_STORE_CTX
*ctx
, const OSSL_STORE_SEARCH
*search
)
271 ERR_raise(ERR_LIB_OSSL_STORE
, OSSL_STORE_R_LOADING_STARTED
);
274 if (search
== NULL
) {
275 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_PASSED_NULL_PARAMETER
);
279 if (ctx
->fetched_loader
!= NULL
) {
282 /* OSSL_STORE_SEARCH_BY_NAME, OSSL_STORE_SEARCH_BY_ISSUER_SERIAL*/
283 void *name_der
= NULL
;
285 /* OSSL_STORE_SEARCH_BY_ISSUER_SERIAL */
286 BIGNUM
*number
= NULL
;
288 if (ctx
->fetched_loader
->p_set_ctx_params
== NULL
) {
289 ERR_raise(ERR_LIB_OSSL_STORE
, OSSL_STORE_R_UNSUPPORTED_OPERATION
);
293 if ((bld
= OSSL_PARAM_BLD_new()) == NULL
) {
294 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
298 ret
= 0; /* Assume the worst */
300 switch (search
->search_type
) {
301 case OSSL_STORE_SEARCH_BY_NAME
:
302 if ((name_der_sz
= i2d_X509_NAME(search
->name
,
303 (unsigned char **)&name_der
)) > 0
304 && OSSL_PARAM_BLD_push_octet_string(bld
,
305 OSSL_STORE_PARAM_SUBJECT
,
306 name_der
, name_der_sz
))
309 case OSSL_STORE_SEARCH_BY_ISSUER_SERIAL
:
310 if ((name_der_sz
= i2d_X509_NAME(search
->name
,
311 (unsigned char **)&name_der
)) > 0
312 && (number
= ASN1_INTEGER_to_BN(search
->serial
, NULL
)) != NULL
313 && OSSL_PARAM_BLD_push_octet_string(bld
,
314 OSSL_STORE_PARAM_ISSUER
,
315 name_der
, name_der_sz
)
316 && OSSL_PARAM_BLD_push_BN(bld
, OSSL_STORE_PARAM_SERIAL
,
320 case OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT
:
321 if (OSSL_PARAM_BLD_push_utf8_string(bld
, OSSL_STORE_PARAM_DIGEST
,
322 EVP_MD_name(search
->digest
), 0)
323 && OSSL_PARAM_BLD_push_octet_string(bld
,
324 OSSL_STORE_PARAM_FINGERPRINT
,
326 search
->stringlength
))
329 case OSSL_STORE_SEARCH_BY_ALIAS
:
330 if (OSSL_PARAM_BLD_push_utf8_string(bld
, OSSL_STORE_PARAM_ALIAS
,
331 (char *)search
->string
,
332 search
->stringlength
))
337 params
= OSSL_PARAM_BLD_to_param(bld
);
338 ret
= ctx
->fetched_loader
->p_set_ctx_params(ctx
->loader_ctx
,
340 OSSL_PARAM_free(params
);
342 OSSL_PARAM_BLD_free(bld
);
343 OPENSSL_free(name_der
);
346 #ifndef OPENSSL_NO_DEPRECATED_3_0
347 /* legacy loader section */
348 if (ctx
->loader
->find
== NULL
) {
349 ERR_raise(ERR_LIB_OSSL_STORE
, OSSL_STORE_R_UNSUPPORTED_OPERATION
);
352 ret
= ctx
->loader
->find(ctx
->loader_ctx
, search
);
359 OSSL_STORE_INFO
*OSSL_STORE_load(OSSL_STORE_CTX
*ctx
)
361 OSSL_STORE_INFO
*v
= NULL
;
365 if (OSSL_STORE_eof(ctx
))
368 if (ctx
->loader
!= NULL
)
369 OSSL_TRACE(STORE
, "Loading next object\n");
371 if (ctx
->cached_info
!= NULL
372 && sk_OSSL_STORE_INFO_num(ctx
->cached_info
) == 0) {
373 sk_OSSL_STORE_INFO_free(ctx
->cached_info
);
374 ctx
->cached_info
= NULL
;
377 if (ctx
->cached_info
!= NULL
) {
378 v
= sk_OSSL_STORE_INFO_shift(ctx
->cached_info
);
380 if (ctx
->fetched_loader
!= NULL
) {
381 struct ossl_load_result_data_st load_data
;
386 if (!ctx
->fetched_loader
->p_load(ctx
->loader_ctx
,
387 ossl_store_handle_load_result
,
389 ossl_pw_passphrase_callback_dec
,
391 if (!OSSL_STORE_eof(ctx
))
397 #ifndef OPENSSL_NO_DEPRECATED_3_0
398 if (ctx
->fetched_loader
== NULL
)
399 v
= ctx
->loader
->load(ctx
->loader_ctx
,
400 ctx
->pwdata
._
.ui_method
.ui_method
,
401 ctx
->pwdata
._
.ui_method
.ui_method_data
);
405 if (ctx
->post_process
!= NULL
&& v
!= NULL
) {
406 v
= ctx
->post_process(v
, ctx
->post_process_data
);
409 * By returning NULL, the callback decides that this object should
416 if (v
!= NULL
&& ctx
->expected_type
!= 0) {
417 int returned_type
= OSSL_STORE_INFO_get_type(v
);
419 if (returned_type
!= OSSL_STORE_INFO_NAME
&& returned_type
!= 0) {
420 if (ctx
->expected_type
!= returned_type
) {
421 OSSL_STORE_INFO_free(v
);
428 OSSL_TRACE1(STORE
, "Got a %s\n",
429 OSSL_STORE_INFO_type_string(OSSL_STORE_INFO_get_type(v
)));
434 int OSSL_STORE_error(OSSL_STORE_CTX
*ctx
)
438 if (ctx
->fetched_loader
!= NULL
)
439 ret
= ctx
->error_flag
;
440 #ifndef OPENSSL_NO_DEPRECATED_3_0
441 if (ctx
->fetched_loader
== NULL
)
442 ret
= ctx
->loader
->error(ctx
->loader_ctx
);
447 int OSSL_STORE_eof(OSSL_STORE_CTX
*ctx
)
451 if (ctx
->fetched_loader
!= NULL
)
452 ret
= ctx
->loader
->p_eof(ctx
->loader_ctx
);
453 #ifndef OPENSSL_NO_DEPRECATED_3_0
454 if (ctx
->fetched_loader
== NULL
)
455 ret
= ctx
->loader
->eof(ctx
->loader_ctx
);
460 static int ossl_store_close_it(OSSL_STORE_CTX
*ctx
)
466 OSSL_TRACE1(STORE
, "Closing %p\n", (void *)ctx
->loader_ctx
);
468 if (ctx
->fetched_loader
!= NULL
)
469 ret
= ctx
->loader
->p_close(ctx
->loader_ctx
);
470 #ifndef OPENSSL_NO_DEPRECATED_3_0
471 if (ctx
->fetched_loader
== NULL
)
472 ret
= ctx
->loader
->close(ctx
->loader_ctx
);
475 sk_OSSL_STORE_INFO_pop_free(ctx
->cached_info
, OSSL_STORE_INFO_free
);
476 OSSL_STORE_LOADER_free(ctx
->fetched_loader
);
477 OPENSSL_free(ctx
->properties
);
478 ossl_pw_clear_passphrase_data(&ctx
->pwdata
);
482 int OSSL_STORE_close(OSSL_STORE_CTX
*ctx
)
484 int ret
= ossl_store_close_it(ctx
);
491 * Functions to generate OSSL_STORE_INFOs, one function for each type we
492 * support having in them as well as a generic constructor.
494 * In all cases, ownership of the object is transferred to the OSSL_STORE_INFO
495 * and will therefore be freed when the OSSL_STORE_INFO is freed.
497 OSSL_STORE_INFO
*OSSL_STORE_INFO_new(int type
, void *data
)
499 OSSL_STORE_INFO
*info
= OPENSSL_zalloc(sizeof(*info
));
509 OSSL_STORE_INFO
*OSSL_STORE_INFO_new_NAME(char *name
)
511 OSSL_STORE_INFO
*info
= OSSL_STORE_INFO_new(OSSL_STORE_INFO_NAME
, NULL
);
514 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
518 info
->_
.name
.name
= name
;
519 info
->_
.name
.desc
= NULL
;
524 int OSSL_STORE_INFO_set0_NAME_description(OSSL_STORE_INFO
*info
, char *desc
)
526 if (info
->type
!= OSSL_STORE_INFO_NAME
) {
527 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_PASSED_INVALID_ARGUMENT
);
531 info
->_
.name
.desc
= desc
;
535 OSSL_STORE_INFO
*OSSL_STORE_INFO_new_PARAMS(EVP_PKEY
*params
)
537 OSSL_STORE_INFO
*info
= OSSL_STORE_INFO_new(OSSL_STORE_INFO_PARAMS
, params
);
540 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
544 OSSL_STORE_INFO
*OSSL_STORE_INFO_new_PUBKEY(EVP_PKEY
*pkey
)
546 OSSL_STORE_INFO
*info
= OSSL_STORE_INFO_new(OSSL_STORE_INFO_PUBKEY
, pkey
);
549 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
553 OSSL_STORE_INFO
*OSSL_STORE_INFO_new_PKEY(EVP_PKEY
*pkey
)
555 OSSL_STORE_INFO
*info
= OSSL_STORE_INFO_new(OSSL_STORE_INFO_PKEY
, pkey
);
558 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
562 OSSL_STORE_INFO
*OSSL_STORE_INFO_new_CERT(X509
*x509
)
564 OSSL_STORE_INFO
*info
= OSSL_STORE_INFO_new(OSSL_STORE_INFO_CERT
, x509
);
567 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
571 OSSL_STORE_INFO
*OSSL_STORE_INFO_new_CRL(X509_CRL
*crl
)
573 OSSL_STORE_INFO
*info
= OSSL_STORE_INFO_new(OSSL_STORE_INFO_CRL
, crl
);
576 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
581 * Functions to try to extract data from a OSSL_STORE_INFO.
583 int OSSL_STORE_INFO_get_type(const OSSL_STORE_INFO
*info
)
588 void *OSSL_STORE_INFO_get0_data(int type
, const OSSL_STORE_INFO
*info
)
590 if (info
->type
== type
)
595 const char *OSSL_STORE_INFO_get0_NAME(const OSSL_STORE_INFO
*info
)
597 if (info
->type
== OSSL_STORE_INFO_NAME
)
598 return info
->_
.name
.name
;
602 char *OSSL_STORE_INFO_get1_NAME(const OSSL_STORE_INFO
*info
)
604 if (info
->type
== OSSL_STORE_INFO_NAME
) {
605 char *ret
= OPENSSL_strdup(info
->_
.name
.name
);
608 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
611 ERR_raise(ERR_LIB_OSSL_STORE
, OSSL_STORE_R_NOT_A_NAME
);
615 const char *OSSL_STORE_INFO_get0_NAME_description(const OSSL_STORE_INFO
*info
)
617 if (info
->type
== OSSL_STORE_INFO_NAME
)
618 return info
->_
.name
.desc
;
622 char *OSSL_STORE_INFO_get1_NAME_description(const OSSL_STORE_INFO
*info
)
624 if (info
->type
== OSSL_STORE_INFO_NAME
) {
625 char *ret
= OPENSSL_strdup(info
->_
.name
.desc
626 ? info
->_
.name
.desc
: "");
629 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
632 ERR_raise(ERR_LIB_OSSL_STORE
, OSSL_STORE_R_NOT_A_NAME
);
636 EVP_PKEY
*OSSL_STORE_INFO_get0_PARAMS(const OSSL_STORE_INFO
*info
)
638 if (info
->type
== OSSL_STORE_INFO_PARAMS
)
639 return info
->_
.params
;
643 EVP_PKEY
*OSSL_STORE_INFO_get1_PARAMS(const OSSL_STORE_INFO
*info
)
645 if (info
->type
== OSSL_STORE_INFO_PARAMS
) {
646 EVP_PKEY_up_ref(info
->_
.params
);
647 return info
->_
.params
;
649 ERR_raise(ERR_LIB_OSSL_STORE
, OSSL_STORE_R_NOT_PARAMETERS
);
653 EVP_PKEY
*OSSL_STORE_INFO_get0_PUBKEY(const OSSL_STORE_INFO
*info
)
655 if (info
->type
== OSSL_STORE_INFO_PUBKEY
)
656 return info
->_
.pubkey
;
660 EVP_PKEY
*OSSL_STORE_INFO_get1_PUBKEY(const OSSL_STORE_INFO
*info
)
662 if (info
->type
== OSSL_STORE_INFO_PUBKEY
) {
663 EVP_PKEY_up_ref(info
->_
.pubkey
);
664 return info
->_
.pubkey
;
666 ERR_raise(ERR_LIB_OSSL_STORE
, OSSL_STORE_R_NOT_A_PUBLIC_KEY
);
670 EVP_PKEY
*OSSL_STORE_INFO_get0_PKEY(const OSSL_STORE_INFO
*info
)
672 if (info
->type
== OSSL_STORE_INFO_PKEY
)
677 EVP_PKEY
*OSSL_STORE_INFO_get1_PKEY(const OSSL_STORE_INFO
*info
)
679 if (info
->type
== OSSL_STORE_INFO_PKEY
) {
680 EVP_PKEY_up_ref(info
->_
.pkey
);
683 ERR_raise(ERR_LIB_OSSL_STORE
, OSSL_STORE_R_NOT_A_PRIVATE_KEY
);
687 X509
*OSSL_STORE_INFO_get0_CERT(const OSSL_STORE_INFO
*info
)
689 if (info
->type
== OSSL_STORE_INFO_CERT
)
694 X509
*OSSL_STORE_INFO_get1_CERT(const OSSL_STORE_INFO
*info
)
696 if (info
->type
== OSSL_STORE_INFO_CERT
) {
697 X509_up_ref(info
->_
.x509
);
700 ERR_raise(ERR_LIB_OSSL_STORE
, OSSL_STORE_R_NOT_A_CERTIFICATE
);
704 X509_CRL
*OSSL_STORE_INFO_get0_CRL(const OSSL_STORE_INFO
*info
)
706 if (info
->type
== OSSL_STORE_INFO_CRL
)
711 X509_CRL
*OSSL_STORE_INFO_get1_CRL(const OSSL_STORE_INFO
*info
)
713 if (info
->type
== OSSL_STORE_INFO_CRL
) {
714 X509_CRL_up_ref(info
->_
.crl
);
717 ERR_raise(ERR_LIB_OSSL_STORE
, OSSL_STORE_R_NOT_A_CRL
);
722 * Free the OSSL_STORE_INFO
724 void OSSL_STORE_INFO_free(OSSL_STORE_INFO
*info
)
727 switch (info
->type
) {
728 case OSSL_STORE_INFO_NAME
:
729 OPENSSL_free(info
->_
.name
.name
);
730 OPENSSL_free(info
->_
.name
.desc
);
732 case OSSL_STORE_INFO_PARAMS
:
733 EVP_PKEY_free(info
->_
.params
);
735 case OSSL_STORE_INFO_PUBKEY
:
736 EVP_PKEY_free(info
->_
.pubkey
);
738 case OSSL_STORE_INFO_PKEY
:
739 EVP_PKEY_free(info
->_
.pkey
);
741 case OSSL_STORE_INFO_CERT
:
742 X509_free(info
->_
.x509
);
744 case OSSL_STORE_INFO_CRL
:
745 X509_CRL_free(info
->_
.crl
);
752 int OSSL_STORE_supports_search(OSSL_STORE_CTX
*ctx
, int search_type
)
756 if (ctx
->fetched_loader
!= NULL
) {
758 ossl_provider_ctx(OSSL_STORE_LOADER_provider(ctx
->fetched_loader
));
759 const OSSL_PARAM
*params
;
760 const OSSL_PARAM
*p_subject
= NULL
;
761 const OSSL_PARAM
*p_issuer
= NULL
;
762 const OSSL_PARAM
*p_serial
= NULL
;
763 const OSSL_PARAM
*p_fingerprint
= NULL
;
764 const OSSL_PARAM
*p_alias
= NULL
;
766 if (ctx
->fetched_loader
->p_settable_ctx_params
== NULL
)
769 params
= ctx
->fetched_loader
->p_settable_ctx_params(provctx
);
770 p_subject
= OSSL_PARAM_locate_const(params
, OSSL_STORE_PARAM_SUBJECT
);
771 p_issuer
= OSSL_PARAM_locate_const(params
, OSSL_STORE_PARAM_ISSUER
);
772 p_serial
= OSSL_PARAM_locate_const(params
, OSSL_STORE_PARAM_SERIAL
);
774 OSSL_PARAM_locate_const(params
, OSSL_STORE_PARAM_FINGERPRINT
);
775 p_alias
= OSSL_PARAM_locate_const(params
, OSSL_STORE_PARAM_ALIAS
);
777 switch (search_type
) {
778 case OSSL_STORE_SEARCH_BY_NAME
:
779 ret
= (p_subject
!= NULL
);
781 case OSSL_STORE_SEARCH_BY_ISSUER_SERIAL
:
782 ret
= (p_issuer
!= NULL
&& p_serial
!= NULL
);
784 case OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT
:
785 ret
= (p_fingerprint
!= NULL
);
787 case OSSL_STORE_SEARCH_BY_ALIAS
:
788 ret
= (p_alias
!= NULL
);
792 #ifndef OPENSSL_NO_DEPRECATED_3_0
793 if (ctx
->fetched_loader
== NULL
) {
794 OSSL_STORE_SEARCH tmp_search
;
796 if (ctx
->loader
->find
== NULL
)
798 tmp_search
.search_type
= search_type
;
799 ret
= ctx
->loader
->find(NULL
, &tmp_search
);
805 /* Search term constructors */
806 OSSL_STORE_SEARCH
*OSSL_STORE_SEARCH_by_name(X509_NAME
*name
)
808 OSSL_STORE_SEARCH
*search
= OPENSSL_zalloc(sizeof(*search
));
810 if (search
== NULL
) {
811 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
815 search
->search_type
= OSSL_STORE_SEARCH_BY_NAME
;
820 OSSL_STORE_SEARCH
*OSSL_STORE_SEARCH_by_issuer_serial(X509_NAME
*name
,
821 const ASN1_INTEGER
*serial
)
823 OSSL_STORE_SEARCH
*search
= OPENSSL_zalloc(sizeof(*search
));
825 if (search
== NULL
) {
826 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
830 search
->search_type
= OSSL_STORE_SEARCH_BY_ISSUER_SERIAL
;
832 search
->serial
= serial
;
836 OSSL_STORE_SEARCH
*OSSL_STORE_SEARCH_by_key_fingerprint(const EVP_MD
*digest
,
840 OSSL_STORE_SEARCH
*search
= OPENSSL_zalloc(sizeof(*search
));
842 if (search
== NULL
) {
843 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
847 if (digest
!= NULL
&& len
!= (size_t)EVP_MD_size(digest
)) {
848 ERR_raise_data(ERR_LIB_OSSL_STORE
,
849 OSSL_STORE_R_FINGERPRINT_SIZE_DOES_NOT_MATCH_DIGEST
,
850 "%s size is %d, fingerprint size is %zu",
851 EVP_MD_name(digest
), EVP_MD_size(digest
), len
);
852 OPENSSL_free(search
);
856 search
->search_type
= OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT
;
857 search
->digest
= digest
;
858 search
->string
= bytes
;
859 search
->stringlength
= len
;
863 OSSL_STORE_SEARCH
*OSSL_STORE_SEARCH_by_alias(const char *alias
)
865 OSSL_STORE_SEARCH
*search
= OPENSSL_zalloc(sizeof(*search
));
867 if (search
== NULL
) {
868 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
872 search
->search_type
= OSSL_STORE_SEARCH_BY_ALIAS
;
873 search
->string
= (const unsigned char *)alias
;
874 search
->stringlength
= strlen(alias
);
878 /* Search term destructor */
879 void OSSL_STORE_SEARCH_free(OSSL_STORE_SEARCH
*search
)
881 OPENSSL_free(search
);
884 /* Search term accessors */
885 int OSSL_STORE_SEARCH_get_type(const OSSL_STORE_SEARCH
*criterion
)
887 return criterion
->search_type
;
890 X509_NAME
*OSSL_STORE_SEARCH_get0_name(const OSSL_STORE_SEARCH
*criterion
)
892 return criterion
->name
;
895 const ASN1_INTEGER
*OSSL_STORE_SEARCH_get0_serial(const OSSL_STORE_SEARCH
898 return criterion
->serial
;
901 const unsigned char *OSSL_STORE_SEARCH_get0_bytes(const OSSL_STORE_SEARCH
902 *criterion
, size_t *length
)
904 *length
= criterion
->stringlength
;
905 return criterion
->string
;
908 const char *OSSL_STORE_SEARCH_get0_string(const OSSL_STORE_SEARCH
*criterion
)
910 return (const char *)criterion
->string
;
913 const EVP_MD
*OSSL_STORE_SEARCH_get0_digest(const OSSL_STORE_SEARCH
*criterion
)
915 return criterion
->digest
;
918 OSSL_STORE_CTX
*OSSL_STORE_attach(BIO
*bp
, const char *scheme
,
919 OSSL_LIB_CTX
*libctx
, const char *propq
,
920 const UI_METHOD
*ui_method
, void *ui_data
,
921 OSSL_STORE_post_process_info_fn post_process
,
922 void *post_process_data
)
924 const OSSL_STORE_LOADER
*loader
= NULL
;
925 OSSL_STORE_LOADER
*fetched_loader
= NULL
;
926 OSSL_STORE_LOADER_CTX
*loader_ctx
= NULL
;
927 OSSL_STORE_CTX
*ctx
= NULL
;
932 OSSL_TRACE1(STORE
, "Looking up scheme %s\n", scheme
);
934 #ifndef OPENSSL_NO_DEPRECATED_3_0
935 if ((loader
= ossl_store_get0_loader_int(scheme
)) != NULL
)
936 loader_ctx
= loader
->attach(loader
, bp
, libctx
, propq
,
941 OSSL_STORE_LOADER_fetch(scheme
, libctx
, propq
)) != NULL
) {
942 const OSSL_PROVIDER
*provider
=
943 OSSL_STORE_LOADER_provider(fetched_loader
);
944 void *provctx
= OSSL_PROVIDER_get0_provider_ctx(provider
);
945 OSSL_CORE_BIO
*cbio
= ossl_core_bio_new_from_bio(bp
);
948 || (loader_ctx
= fetched_loader
->p_attach(provctx
, cbio
)) == NULL
) {
949 OSSL_STORE_LOADER_free(fetched_loader
);
950 fetched_loader
= NULL
;
951 } else if (propq
!= NULL
) {
952 OSSL_PARAM params
[] = {
953 OSSL_PARAM_utf8_string(OSSL_STORE_PARAM_PROPERTIES
,
958 params
[0].data
= (void *)propq
;
959 if (!fetched_loader
->p_set_ctx_params(loader_ctx
, params
)) {
960 (void)fetched_loader
->p_close(loader_ctx
);
961 OSSL_STORE_LOADER_free(fetched_loader
);
962 fetched_loader
= NULL
;
965 loader
= fetched_loader
;
966 ossl_core_bio_free(cbio
);
969 if (loader_ctx
== NULL
) {
970 ERR_clear_last_mark();
974 if ((ctx
= OPENSSL_zalloc(sizeof(*ctx
))) == NULL
) {
975 ERR_clear_last_mark();
976 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_MALLOC_FAILURE
);
980 if (ui_method
!= NULL
981 && !ossl_pw_set_ui_method(&ctx
->pwdata
, ui_method
, ui_data
)) {
982 ERR_clear_last_mark();
987 ctx
->fetched_loader
= fetched_loader
;
988 ctx
->loader
= loader
;
989 ctx
->loader_ctx
= loader_ctx
;
990 ctx
->post_process
= post_process
;
991 ctx
->post_process_data
= post_process_data
;
994 * ossl_store_get0_loader_int will raise an error if the loader for the
995 * the scheme cannot be retrieved. But if a loader was successfully
996 * fetched then we remove this error from the error stack.