2019-02-09 00:01:56 +08:00
|
|
|
/*
|
|
|
|
* Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <openssl/ossl_typ.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/core.h>
|
|
|
|
#include "internal/cryptlib.h"
|
|
|
|
#include "internal/thread_once.h"
|
|
|
|
#include "internal/property.h"
|
|
|
|
#include "internal/core.h"
|
2019-05-04 18:56:32 +08:00
|
|
|
#include "internal/namemap.h"
|
2019-02-09 00:01:56 +08:00
|
|
|
#include "internal/evp_int.h" /* evp_locl.h needs it */
|
|
|
|
#include "evp_locl.h"
|
|
|
|
|
|
|
|
static void default_method_store_free(void *vstore)
|
|
|
|
{
|
|
|
|
ossl_method_store_free(vstore);
|
|
|
|
}
|
|
|
|
|
2019-05-01 18:02:43 +08:00
|
|
|
static void *default_method_store_new(OPENSSL_CTX *ctx)
|
2019-02-09 00:01:56 +08:00
|
|
|
{
|
2019-05-01 18:02:43 +08:00
|
|
|
return ossl_method_store_new(ctx);
|
2019-02-09 00:01:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const OPENSSL_CTX_METHOD default_method_store_method = {
|
|
|
|
default_method_store_new,
|
|
|
|
default_method_store_free,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Data to be passed through ossl_method_construct() */
|
|
|
|
struct method_data_st {
|
2019-05-04 18:56:32 +08:00
|
|
|
OPENSSL_CTX *libctx;
|
2019-02-09 00:01:56 +08:00
|
|
|
const char *name;
|
2019-05-08 20:00:31 +08:00
|
|
|
int id;
|
2019-02-09 00:01:56 +08:00
|
|
|
OSSL_METHOD_CONSTRUCT_METHOD *mcm;
|
2019-05-08 20:00:31 +08:00
|
|
|
void *(*method_from_dispatch)(const OSSL_DISPATCH *, OSSL_PROVIDER *);
|
2019-02-09 00:01:56 +08:00
|
|
|
int (*refcnt_up_method)(void *method);
|
|
|
|
void (*destruct_method)(void *method);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generic routines to fetch / create EVP methods with ossl_method_construct()
|
|
|
|
*/
|
2019-05-01 18:02:43 +08:00
|
|
|
static void *alloc_tmp_method_store(OPENSSL_CTX *ctx)
|
2019-02-09 00:01:56 +08:00
|
|
|
{
|
2019-05-01 18:02:43 +08:00
|
|
|
return ossl_method_store_new(ctx);
|
2019-02-09 00:01:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void dealloc_tmp_method_store(void *store)
|
|
|
|
{
|
|
|
|
if (store != NULL)
|
|
|
|
ossl_method_store_free(store);
|
|
|
|
}
|
|
|
|
|
2019-04-05 16:46:18 +08:00
|
|
|
static OSSL_METHOD_STORE *get_default_method_store(OPENSSL_CTX *libctx)
|
2019-02-09 00:01:56 +08:00
|
|
|
{
|
2019-05-01 18:02:43 +08:00
|
|
|
return openssl_ctx_get_data(libctx, OPENSSL_CTX_DEFAULT_METHOD_STORE_INDEX,
|
|
|
|
&default_method_store_method);
|
2019-02-09 00:01:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *get_method_from_store(OPENSSL_CTX *libctx, void *store,
|
2019-05-05 14:42:21 +08:00
|
|
|
const char *name, const char *propquery,
|
|
|
|
void *data)
|
2019-02-09 00:01:56 +08:00
|
|
|
{
|
|
|
|
struct method_data_st *methdata = data;
|
|
|
|
void *method = NULL;
|
2019-05-05 14:42:21 +08:00
|
|
|
OSSL_NAMEMAP *namemap;
|
|
|
|
int id;
|
2019-02-09 00:01:56 +08:00
|
|
|
|
|
|
|
if (store == NULL
|
|
|
|
&& (store = get_default_method_store(libctx)) == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2019-05-05 14:42:21 +08:00
|
|
|
if ((namemap = ossl_namemap_stored(libctx)) == NULL
|
|
|
|
|| (id = ossl_namemap_add(namemap, name)) == 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
(void)ossl_method_store_fetch(store, id, propquery, &method);
|
2019-02-09 00:01:56 +08:00
|
|
|
|
|
|
|
if (method != NULL
|
|
|
|
&& !methdata->refcnt_up_method(method)) {
|
|
|
|
method = NULL;
|
|
|
|
}
|
|
|
|
return method;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int put_method_in_store(OPENSSL_CTX *libctx, void *store,
|
2019-05-05 14:42:21 +08:00
|
|
|
void *method, const char *name,
|
|
|
|
const char *propdef, void *data)
|
2019-02-09 00:01:56 +08:00
|
|
|
{
|
|
|
|
struct method_data_st *methdata = data;
|
2019-05-05 14:42:21 +08:00
|
|
|
OSSL_NAMEMAP *namemap;
|
|
|
|
int id;
|
2019-03-21 01:51:29 +08:00
|
|
|
|
2019-05-05 14:42:21 +08:00
|
|
|
if ((namemap = ossl_namemap_stored(methdata->libctx)) == NULL
|
|
|
|
|| (id = ossl_namemap_add(namemap, name)) == 0)
|
2019-03-21 01:51:29 +08:00
|
|
|
return 0;
|
2019-02-09 00:01:56 +08:00
|
|
|
|
|
|
|
if (store == NULL
|
|
|
|
&& (store = get_default_method_store(libctx)) == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (methdata->refcnt_up_method(method)
|
2019-05-05 14:42:21 +08:00
|
|
|
&& ossl_method_store_add(store, id, propdef, method,
|
2019-02-09 00:01:56 +08:00
|
|
|
methdata->destruct_method))
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-05 14:42:21 +08:00
|
|
|
static void *construct_method(const char *name, const OSSL_DISPATCH *fns,
|
|
|
|
OSSL_PROVIDER *prov, void *data)
|
2019-02-09 00:01:56 +08:00
|
|
|
{
|
|
|
|
struct method_data_st *methdata = data;
|
|
|
|
|
2019-05-08 20:00:31 +08:00
|
|
|
return methdata->method_from_dispatch(fns, prov);
|
2019-02-09 00:01:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void destruct_method(void *method, void *data)
|
|
|
|
{
|
|
|
|
struct method_data_st *methdata = data;
|
|
|
|
|
|
|
|
methdata->destruct_method(method);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *evp_generic_fetch(OPENSSL_CTX *libctx, int operation_id,
|
2019-05-05 14:42:21 +08:00
|
|
|
const char *name, const char *properties,
|
2019-05-08 20:00:31 +08:00
|
|
|
void *(*new_method)(const OSSL_DISPATCH *fns,
|
2019-02-09 00:01:56 +08:00
|
|
|
OSSL_PROVIDER *prov),
|
|
|
|
int (*upref_method)(void *),
|
2019-05-08 20:00:31 +08:00
|
|
|
void (*free_method)(void *))
|
2019-02-09 00:01:56 +08:00
|
|
|
{
|
2019-04-18 18:23:21 +08:00
|
|
|
OSSL_METHOD_STORE *store = get_default_method_store(libctx);
|
2019-05-04 18:56:32 +08:00
|
|
|
OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
|
2019-05-05 14:42:21 +08:00
|
|
|
int id;
|
2019-02-09 00:01:56 +08:00
|
|
|
void *method = NULL;
|
|
|
|
|
2019-05-04 18:56:32 +08:00
|
|
|
if (store == NULL || namemap == NULL)
|
2019-04-18 18:23:21 +08:00
|
|
|
return NULL;
|
|
|
|
|
2019-05-05 14:42:21 +08:00
|
|
|
if ((id = ossl_namemap_number(namemap, name)) == 0
|
|
|
|
|| !ossl_method_store_cache_get(store, id, properties, &method)) {
|
2019-02-09 00:01:56 +08:00
|
|
|
OSSL_METHOD_CONSTRUCT_METHOD mcm = {
|
|
|
|
alloc_tmp_method_store,
|
|
|
|
dealloc_tmp_method_store,
|
|
|
|
get_method_from_store,
|
|
|
|
put_method_in_store,
|
|
|
|
construct_method,
|
|
|
|
destruct_method
|
|
|
|
};
|
|
|
|
struct method_data_st mcmdata;
|
|
|
|
|
|
|
|
mcmdata.mcm = &mcm;
|
2019-05-04 18:56:32 +08:00
|
|
|
mcmdata.libctx = libctx;
|
2019-04-11 18:27:59 +08:00
|
|
|
mcmdata.name = name;
|
2019-02-09 00:01:56 +08:00
|
|
|
mcmdata.method_from_dispatch = new_method;
|
|
|
|
mcmdata.destruct_method = free_method;
|
|
|
|
mcmdata.refcnt_up_method = upref_method;
|
|
|
|
mcmdata.destruct_method = free_method;
|
2019-05-05 14:42:21 +08:00
|
|
|
method = ossl_method_construct(libctx, operation_id, name,
|
2019-02-09 00:01:56 +08:00
|
|
|
properties, 0 /* !force_cache */,
|
|
|
|
&mcm, &mcmdata);
|
2019-05-05 14:42:21 +08:00
|
|
|
ossl_method_store_cache_set(store, id, properties, method);
|
2019-04-18 18:23:21 +08:00
|
|
|
} else {
|
|
|
|
upref_method(method);
|
2019-02-09 00:01:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return method;
|
|
|
|
}
|
2019-04-05 16:46:18 +08:00
|
|
|
|
|
|
|
int EVP_set_default_properties(OPENSSL_CTX *libctx, const char *propq)
|
|
|
|
{
|
|
|
|
OSSL_METHOD_STORE *store = get_default_method_store(libctx);
|
|
|
|
|
|
|
|
if (store != NULL)
|
|
|
|
return ossl_method_store_set_global_properties(store, propq);
|
|
|
|
EVPerr(EVP_F_EVP_SET_DEFAULT_PROPERTIES, ERR_R_INTERNAL_ERROR);
|
|
|
|
return 0;
|
|
|
|
}
|