2 * Copyright 2019 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
12 #include <openssl/core.h>
13 #include "internal/cryptlib.h"
14 #include "internal/core.h"
15 #include "internal/property.h"
16 #include "internal/provider.h"
18 struct construct_data_st
{
20 OSSL_METHOD_STORE
*store
;
23 OSSL_METHOD_CONSTRUCT_METHOD
*mcm
;
27 static int ossl_method_construct_this(OSSL_PROVIDER
*provider
, void *cbdata
)
29 struct construct_data_st
*data
= cbdata
;
30 int no_store
= 0; /* Assume caching is ok */
31 const OSSL_ALGORITHM
*map
=
32 ossl_provider_query_operation(provider
, data
->operation_id
, &no_store
);
34 while (map
->algorithm_name
!= NULL
) {
35 const OSSL_ALGORITHM
*thismap
= map
++;
38 if ((method
= data
->mcm
->construct(thismap
->algorithm_name
,
39 thismap
->implementation
, provider
,
40 data
->mcm_data
)) == NULL
)
44 * Note regarding putting the method in stores:
46 * we don't need to care if it actually got in or not here.
47 * If it didn't get in, it will simply not be available when
48 * ossl_method_construct() tries to get it from the store.
50 * It is *expected* that the put function increments the refcnt
51 * of the passed method.
54 if (data
->force_store
|| !no_store
) {
56 * If we haven't been told not to store,
57 * add to the global store
59 data
->mcm
->put(data
->libctx
, NULL
,
60 thismap
->property_definition
,
61 method
, data
->mcm_data
);
64 data
->mcm
->put(data
->libctx
, data
->store
,
65 thismap
->property_definition
,
66 method
, data
->mcm_data
);
68 /* refcnt-- because we're dropping the reference */
69 data
->mcm
->destruct(method
, data
->mcm_data
);
75 void *ossl_method_construct(OPENSSL_CTX
*libctx
, int operation_id
,
76 const char *name
, const char *propquery
,
78 OSSL_METHOD_CONSTRUCT_METHOD
*mcm
, void *mcm_data
)
82 if ((method
= mcm
->get(libctx
, NULL
, propquery
, mcm_data
)) == NULL
) {
83 struct construct_data_st cbdata
;
86 * We have a temporary store to be able to easily search among new
87 * items, or items that should find themselves in the global store.
89 if ((cbdata
.store
= mcm
->alloc_tmp_store()) == NULL
)
92 cbdata
.libctx
= libctx
;
93 cbdata
.operation_id
= operation_id
;
94 cbdata
.force_store
= force_store
;
96 cbdata
.mcm_data
= mcm_data
;
97 ossl_provider_forall_loaded(libctx
, ossl_method_construct_this
,
100 method
= mcm
->get(libctx
, cbdata
.store
, propquery
, mcm_data
);
101 mcm
->dealloc_tmp_store(cbdata
.store
);