Path: blob/main/crypto/openssl/providers/implementations/skeymgmt/generic.c
48292 views
/*1* Copyright 2025 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89#include <openssl/core_dispatch.h>10#include <openssl/core_names.h>11#include "crypto/types.h"12#include "internal/skey.h"13#include "prov/provider_ctx.h"14#include "prov/providercommon.h"15#include "prov/implementations.h"16#include "skeymgmt_lcl.h"1718void generic_free(void *keydata)19{20PROV_SKEY *generic = keydata;2122if (generic == NULL)23return;2425OPENSSL_free(generic->data);26OPENSSL_free(generic);27}2829void *generic_import(void *provctx, int selection, const OSSL_PARAM params[])30{31OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);32const OSSL_PARAM *raw_bytes;33PROV_SKEY *generic = NULL;34int ok = 0;3536if (!ossl_prov_is_running())37return NULL;3839if ((selection & OSSL_SKEYMGMT_SELECT_SECRET_KEY) == 0)40return NULL;4142raw_bytes = OSSL_PARAM_locate_const(params, OSSL_SKEY_PARAM_RAW_BYTES);43if (raw_bytes == NULL)44return NULL;4546generic = OPENSSL_zalloc(sizeof(PROV_SKEY));47if (generic == NULL)48return NULL;4950generic->libctx = libctx;5152generic->type = SKEY_TYPE_GENERIC;5354if ((generic->data = OPENSSL_memdup(raw_bytes->data, raw_bytes->data_size)) == NULL)55goto end;56generic->length = raw_bytes->data_size;57ok = 1;5859end:60if (ok == 0) {61generic_free(generic);62generic = NULL;63}64return generic;65}6667int generic_export(void *keydata, int selection,68OSSL_CALLBACK *param_callback, void *cbarg)69{70PROV_SKEY *gen = keydata;71OSSL_PARAM params[2];7273if (!ossl_prov_is_running() || gen == NULL)74return 0;7576/* If we use generic SKEYMGMT as a "base class", we shouldn't check the type */77if ((selection & OSSL_SKEYMGMT_SELECT_SECRET_KEY) == 0)78return 0;7980params[0] = OSSL_PARAM_construct_octet_string(OSSL_SKEY_PARAM_RAW_BYTES,81gen->data, gen->length);82params[1] = OSSL_PARAM_construct_end();8384return param_callback(params, cbarg);85}8687const OSSL_DISPATCH ossl_generic_skeymgmt_functions[] = {88{ OSSL_FUNC_SKEYMGMT_FREE, (void (*)(void))generic_free },89{ OSSL_FUNC_SKEYMGMT_IMPORT, (void (*)(void))generic_import },90{ OSSL_FUNC_SKEYMGMT_EXPORT, (void (*)(void))generic_export },91OSSL_DISPATCH_END92};939495