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;
|
|
|
|
OSSL_METHOD_CONSTRUCT_METHOD *mcm;
|
2019-07-11 04:22:16 +08:00
|
|
|
void *(*method_from_dispatch)(const char *, const OSSL_DISPATCH *,
|
2019-08-23 20:03:28 +08:00
|
|
|
OSSL_PROVIDER *, void *);
|
|
|
|
void *method_data;
|
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
|
|
|
}
|
|
|
|
|
2019-06-07 17:44:08 +08:00
|
|
|
/*
|
|
|
|
* To identity the method in the method store, we mix the name identity
|
|
|
|
* with the operation identity, with the assumption that we don't have
|
|
|
|
* more than 2^24 names or more than 2^8 operation types.
|
|
|
|
*
|
|
|
|
* The resulting identity is a 32-bit integer, composed like this:
|
|
|
|
*
|
|
|
|
* +---------24 bits--------+-8 bits-+
|
|
|
|
* | name identity | op id |
|
|
|
|
* +------------------------+--------+
|
|
|
|
*/
|
|
|
|
static uint32_t method_id(unsigned int operation_id, unsigned int name_id)
|
|
|
|
{
|
|
|
|
if (!ossl_assert(name_id < (1 << 24) || operation_id < (1 << 8))
|
|
|
|
|| !ossl_assert(name_id > 0 && operation_id > 0))
|
|
|
|
return 0;
|
|
|
|
return ((name_id << 8) & 0xFFFFFF00) | (operation_id & 0x000000FF);
|
|
|
|
}
|
|
|
|
|
2019-02-09 00:01:56 +08:00
|
|
|
static void *get_method_from_store(OPENSSL_CTX *libctx, void *store,
|
2019-06-07 17:44:08 +08:00
|
|
|
int operation_id, 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;
|
2019-06-07 17:44:08 +08:00
|
|
|
int nameid;
|
|
|
|
uint32_t methid;
|
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
|
2019-05-23 09:18:04 +08:00
|
|
|
|| (nameid = ossl_namemap_name2num(namemap, name)) == 0
|
2019-06-07 17:44:08 +08:00
|
|
|
|| (methid = method_id(operation_id, nameid)) == 0)
|
2019-05-05 14:42:21 +08:00
|
|
|
return NULL;
|
|
|
|
|
2019-06-07 17:44:08 +08:00
|
|
|
(void)ossl_method_store_fetch(store, methid, 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-08-21 16:08:44 +08:00
|
|
|
void *method, const OSSL_PROVIDER *prov,
|
|
|
|
int operation_id, 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;
|
2019-06-07 17:44:08 +08:00
|
|
|
int nameid;
|
|
|
|
uint32_t methid;
|
2019-03-21 01:51:29 +08:00
|
|
|
|
2019-05-05 14:42:21 +08:00
|
|
|
if ((namemap = ossl_namemap_stored(methdata->libctx)) == NULL
|
2019-05-23 09:18:04 +08:00
|
|
|
|| (nameid = ossl_namemap_add(namemap, 0, name)) == 0
|
2019-06-07 17:44:08 +08:00
|
|
|
|| (methid = method_id(operation_id, nameid)) == 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;
|
|
|
|
|
2019-08-21 16:08:44 +08:00
|
|
|
return ossl_method_store_add(store, prov, methid, propdef, method,
|
2019-08-21 15:58:10 +08:00
|
|
|
methdata->refcnt_up_method,
|
|
|
|
methdata->destruct_method);
|
2019-02-09 00:01:56 +08:00
|
|
|
}
|
|
|
|
|
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-08-23 20:03:28 +08:00
|
|
|
return methdata->method_from_dispatch(name, fns, prov,
|
|
|
|
methdata->method_data);
|
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-07-11 04:22:16 +08:00
|
|
|
void *(*new_method)(const char *name,
|
|
|
|
const OSSL_DISPATCH *fns,
|
2019-08-23 20:03:28 +08:00
|
|
|
OSSL_PROVIDER *prov,
|
|
|
|
void *method_data),
|
|
|
|
void *method_data,
|
2019-07-02 20:57:36 +08:00
|
|
|
int (*up_ref_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-06-07 17:44:08 +08:00
|
|
|
int nameid = 0;
|
|
|
|
uint32_t methid = 0;
|
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-06-07 17:44:08 +08:00
|
|
|
/*
|
|
|
|
* If there's ever an operation_id == 0 passed, we have an internal
|
|
|
|
* programming error.
|
|
|
|
*/
|
|
|
|
if (!ossl_assert(operation_id > 0))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* method_id returns 0 if we have too many operations (more than
|
|
|
|
* about 2^8) or too many names (more than about 2^24). In that
|
|
|
|
* case, we can't create any new method.
|
|
|
|
*/
|
2019-05-23 09:18:04 +08:00
|
|
|
if ((nameid = ossl_namemap_name2num(namemap, name)) != 0
|
2019-06-07 17:44:08 +08:00
|
|
|
&& (methid = method_id(operation_id, nameid)) == 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (nameid == 0
|
|
|
|
|| !ossl_method_store_cache_get(store, methid, 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;
|
2019-07-02 20:57:36 +08:00
|
|
|
mcmdata.refcnt_up_method = up_ref_method;
|
2019-02-09 00:01:56 +08:00
|
|
|
mcmdata.destruct_method = free_method;
|
2019-08-23 20:03:28 +08:00
|
|
|
mcmdata.method_data = method_data;
|
2019-06-07 17:44:08 +08:00
|
|
|
if ((method = ossl_method_construct(libctx, operation_id, name,
|
|
|
|
properties, 0 /* !force_cache */,
|
2019-06-28 21:29:34 +08:00
|
|
|
&mcm, &mcmdata)) != NULL) {
|
2019-06-07 17:44:08 +08:00
|
|
|
/*
|
|
|
|
* If construction did create a method for us, we know that
|
|
|
|
* there is a correct nameid and methodid, since those have
|
|
|
|
* already been calculated in get_method_from_store() and
|
|
|
|
* put_method_in_store() above.
|
|
|
|
*/
|
2019-05-23 09:18:04 +08:00
|
|
|
nameid = ossl_namemap_name2num(namemap, name);
|
2019-06-07 17:44:08 +08:00
|
|
|
methid = method_id(operation_id, nameid);
|
|
|
|
ossl_method_store_cache_set(store, methid, properties, method);
|
|
|
|
}
|
2019-04-18 18:23:21 +08:00
|
|
|
} else {
|
2019-07-02 20:57:36 +08:00
|
|
|
up_ref_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;
|
|
|
|
}
|
2019-07-13 12:53:44 +08:00
|
|
|
|
|
|
|
struct do_all_data_st {
|
|
|
|
void (*user_fn)(void *method, void *arg);
|
|
|
|
void *user_arg;
|
|
|
|
void *(*new_method)(const char *name, const OSSL_DISPATCH *fns,
|
2019-08-23 20:03:28 +08:00
|
|
|
OSSL_PROVIDER *prov, void *method_data);
|
2019-07-13 12:53:44 +08:00
|
|
|
void (*free_method)(void *);
|
|
|
|
};
|
|
|
|
|
|
|
|
static void do_one(OSSL_PROVIDER *provider, const OSSL_ALGORITHM *algo,
|
|
|
|
int no_store, void *vdata)
|
|
|
|
{
|
|
|
|
struct do_all_data_st *data = vdata;
|
|
|
|
void *method = data->new_method(algo->algorithm_name,
|
2019-08-23 20:03:28 +08:00
|
|
|
algo->implementation, provider, NULL);
|
2019-07-13 12:53:44 +08:00
|
|
|
|
|
|
|
if (method != NULL) {
|
|
|
|
data->user_fn(method, data->user_arg);
|
|
|
|
data->free_method(method);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void evp_generic_do_all(OPENSSL_CTX *libctx, int operation_id,
|
|
|
|
void (*user_fn)(void *method, void *arg),
|
|
|
|
void *user_arg,
|
|
|
|
void *(*new_method)(const char *name,
|
|
|
|
const OSSL_DISPATCH *fns,
|
2019-08-23 20:03:28 +08:00
|
|
|
OSSL_PROVIDER *prov,
|
|
|
|
void *method_data),
|
|
|
|
void *method_data,
|
2019-07-13 12:53:44 +08:00
|
|
|
void (*free_method)(void *))
|
|
|
|
{
|
|
|
|
struct do_all_data_st data;
|
|
|
|
|
|
|
|
data.new_method = new_method;
|
|
|
|
data.free_method = free_method;
|
|
|
|
data.user_fn = user_fn;
|
|
|
|
data.user_arg = user_arg;
|
2019-08-23 20:03:28 +08:00
|
|
|
ossl_algorithm_do_all(libctx, operation_id, method_data, do_one, &data);
|
2019-07-13 12:53:44 +08:00
|
|
|
}
|