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
11 #include <openssl/ossl_typ.h>
12 #include <openssl/evp.h>
13 #include <openssl/core.h>
14 #include "internal/cryptlib.h"
15 #include "internal/thread_once.h"
16 #include "internal/asn1_int.h"
17 #include "internal/property.h"
18 #include "internal/core.h"
19 #include "internal/evp_int.h" /* evp_locl.h needs it */
22 static void default_method_store_free(void *vstore
)
24 ossl_method_store_free(vstore
);
27 static void *default_method_store_new(OPENSSL_CTX
*ctx
)
29 return ossl_method_store_new(ctx
);
33 static const OPENSSL_CTX_METHOD default_method_store_method
= {
34 default_method_store_new
,
35 default_method_store_free
,
38 /* Data to be passed through ossl_method_construct() */
39 struct method_data_st
{
42 OSSL_METHOD_CONSTRUCT_METHOD
*mcm
;
43 void *(*method_from_dispatch
)(int nid
, const OSSL_DISPATCH
*,
45 int (*refcnt_up_method
)(void *method
);
46 void (*destruct_method
)(void *method
);
47 int (*nid_method
)(void *method
);
51 * Generic routines to fetch / create EVP methods with ossl_method_construct()
53 static void *alloc_tmp_method_store(OPENSSL_CTX
*ctx
)
55 return ossl_method_store_new(ctx
);
58 static void dealloc_tmp_method_store(void *store
)
61 ossl_method_store_free(store
);
64 static OSSL_METHOD_STORE
*get_default_method_store(OPENSSL_CTX
*libctx
)
66 return openssl_ctx_get_data(libctx
, OPENSSL_CTX_DEFAULT_METHOD_STORE_INDEX
,
67 &default_method_store_method
);
70 static void *get_method_from_store(OPENSSL_CTX
*libctx
, void *store
,
71 const char *propquery
, void *data
)
73 struct method_data_st
*methdata
= data
;
77 && (store
= get_default_method_store(libctx
)) == NULL
)
80 (void)ossl_method_store_fetch(store
, methdata
->nid
, propquery
, &method
);
83 && !methdata
->refcnt_up_method(method
)) {
89 static int put_method_in_store(OPENSSL_CTX
*libctx
, void *store
,
91 void *method
, void *data
)
93 struct method_data_st
*methdata
= data
;
94 int nid
= methdata
->nid_method(method
);
100 && (store
= get_default_method_store(libctx
)) == NULL
)
103 if (methdata
->refcnt_up_method(method
)
104 && ossl_method_store_add(store
, nid
, propdef
, method
,
105 methdata
->destruct_method
))
110 static void *construct_method(const char *algorithm_name
,
111 const OSSL_DISPATCH
*fns
, OSSL_PROVIDER
*prov
,
114 struct method_data_st
*methdata
= data
;
116 int nid
= OBJ_sn2nid(algorithm_name
);
118 if (nid
== NID_undef
) {
119 /* Create a new NID for that name on the fly */
122 /* This is the same as OBJ_create() but without requiring a OID */
123 tmpobj
.nid
= OBJ_new_nid(1);
124 tmpobj
.sn
= tmpobj
.ln
= methdata
->name
;
125 tmpobj
.flags
= ASN1_OBJECT_FLAG_DYNAMIC
;
129 nid
= OBJ_add_object(&tmpobj
);
132 if (nid
== NID_undef
)
135 method
= methdata
->method_from_dispatch(nid
, fns
, prov
);
141 static void destruct_method(void *method
, void *data
)
143 struct method_data_st
*methdata
= data
;
145 methdata
->destruct_method(method
);
148 void *evp_generic_fetch(OPENSSL_CTX
*libctx
, int operation_id
,
149 const char *algorithm
, const char *properties
,
150 void *(*new_method
)(int nid
, const OSSL_DISPATCH
*fns
,
151 OSSL_PROVIDER
*prov
),
152 int (*upref_method
)(void *),
153 void (*free_method
)(void *),
154 int (*nid_method
)(void *))
156 OSSL_METHOD_STORE
*store
= get_default_method_store(libctx
);
157 int nid
= OBJ_sn2nid(algorithm
);
164 || !ossl_method_store_cache_get(store
, nid
, properties
, &method
)) {
165 OSSL_METHOD_CONSTRUCT_METHOD mcm
= {
166 alloc_tmp_method_store
,
167 dealloc_tmp_method_store
,
168 get_method_from_store
,
173 struct method_data_st mcmdata
;
177 mcmdata
.method_from_dispatch
= new_method
;
178 mcmdata
.destruct_method
= free_method
;
179 mcmdata
.refcnt_up_method
= upref_method
;
180 mcmdata
.destruct_method
= free_method
;
181 mcmdata
.nid_method
= nid_method
;
182 method
= ossl_method_construct(libctx
, operation_id
, algorithm
,
183 properties
, 0 /* !force_cache */,
185 ossl_method_store_cache_set(store
, nid
, properties
, method
);
187 upref_method(method
);
193 int EVP_set_default_properties(OPENSSL_CTX
*libctx
, const char *propq
)
195 OSSL_METHOD_STORE
*store
= get_default_method_store(libctx
);
198 return ossl_method_store_set_global_properties(store
, propq
);
199 EVPerr(EVP_F_EVP_SET_DEFAULT_PROPERTIES
, ERR_R_INTERNAL_ERROR
);