| 1 |
/* |
| 2 |
* Copyright 2025-2026 The OpenSSL Project Authors. All Rights Reserved. |
| 3 |
* |
| 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 |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <string.h> |
| 11 |
#include <openssl/params.h> |
| 12 |
#include <openssl/param_build.h> |
| 13 |
#include <openssl/evp.h> |
| 14 |
#include <openssl/core_names.h> |
| 15 |
|
| 16 |
#include "internal/common.h" |
| 17 |
#include "internal/provider.h" |
| 18 |
#include "crypto/evp.h" |
| 19 |
#include "evp_local.h" |
| 20 |
|
| 21 |
int EVP_SKEY_export(const EVP_SKEY *skey, int selection, |
| 22 |
OSSL_CALLBACK *export_cb, void *export_cbarg) |
| 23 |
{ |
| 24 |
if (skey == NULL) { |
| 25 |
ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
| 26 |
return 0; |
| 27 |
} |
| 28 |
|
| 29 |
return evp_skeymgmt_export(skey->skeymgmt, skey->keydata, selection, export_cb, export_cbarg); |
| 30 |
} |
| 31 |
|
| 32 |
EVP_SKEY *evp_skey_alloc(EVP_SKEYMGMT *skeymgmt) |
| 33 |
{ |
| 34 |
EVP_SKEY *skey; |
| 35 |
|
| 36 |
if (!ossl_assert(skeymgmt != NULL)) |
| 37 |
return NULL; |
| 38 |
|
| 39 |
if ((skey = OPENSSL_zalloc(sizeof(*skey))) == NULL) |
| 40 |
return NULL; |
| 41 |
|
| 42 |
if (!CRYPTO_NEW_REF(&skey->references, 1)) |
| 43 |
goto err; |
| 44 |
|
| 45 |
skey->lock = CRYPTO_THREAD_lock_new(); |
| 46 |
if (skey->lock == NULL) { |
| 47 |
ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB); |
| 48 |
goto err; |
| 49 |
} |
| 50 |
if (EVP_SKEYMGMT_up_ref(skeymgmt)) { |
| 51 |
skey->skeymgmt = skeymgmt; |
| 52 |
return skey; |
| 53 |
} else { |
| 54 |
goto err; |
| 55 |
} |
| 56 |
|
| 57 |
err: |
| 58 |
CRYPTO_FREE_REF(&skey->references); |
| 59 |
CRYPTO_THREAD_lock_free(skey->lock); |
| 60 |
OPENSSL_free(skey); |
| 61 |
return NULL; |
| 62 |
} |
| 63 |
|
| 64 |
static EVP_SKEY *evp_skey_alloc_fetch(OSSL_LIB_CTX *libctx, |
| 65 |
const char *skeymgmtname, |
| 66 |
const char *propquery) |
| 67 |
{ |
| 68 |
EVP_SKEYMGMT *skeymgmt; |
| 69 |
EVP_SKEY *skey; |
| 70 |
|
| 71 |
skeymgmt = EVP_SKEYMGMT_fetch(libctx, skeymgmtname, propquery); |
| 72 |
if (skeymgmt == NULL) { |
| 73 |
/* |
| 74 |
* if the specific key_type is unknown, attempt to use the generic |
| 75 |
* key management |
| 76 |
*/ |
| 77 |
skeymgmt = EVP_SKEYMGMT_fetch(libctx, OSSL_SKEY_TYPE_GENERIC, propquery); |
| 78 |
if (skeymgmt == NULL) { |
| 79 |
ERR_raise(ERR_LIB_EVP, ERR_R_FETCH_FAILED); |
| 80 |
return NULL; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
skey = evp_skey_alloc(skeymgmt); |
| 85 |
EVP_SKEYMGMT_free(skeymgmt); |
| 86 |
|
| 87 |
return skey; |
| 88 |
} |
| 89 |
|
| 90 |
EVP_SKEY *EVP_SKEY_import(OSSL_LIB_CTX *libctx, const char *skeymgmtname, const char *propquery, |
| 91 |
int selection, const OSSL_PARAM *params) |
| 92 |
{ |
| 93 |
EVP_SKEY *skey = evp_skey_alloc_fetch(libctx, skeymgmtname, propquery); |
| 94 |
|
| 95 |
if (skey == NULL) |
| 96 |
return NULL; |
| 97 |
|
| 98 |
skey->keydata = evp_skeymgmt_import(skey->skeymgmt, selection, params); |
| 99 |
if (skey->keydata == NULL) |
| 100 |
goto err; |
| 101 |
|
| 102 |
return skey; |
| 103 |
|
| 104 |
err: |
| 105 |
EVP_SKEY_free(skey); |
| 106 |
return NULL; |
| 107 |
} |
| 108 |
|
| 109 |
EVP_SKEY *EVP_SKEY_import_SKEYMGMT(OSSL_LIB_CTX *libctx, EVP_SKEYMGMT *skeymgmt, |
| 110 |
int selection, const OSSL_PARAM *params) |
| 111 |
{ |
| 112 |
EVP_SKEY *skey = evp_skey_alloc(skeymgmt); |
| 113 |
|
| 114 |
if (skey == NULL) |
| 115 |
return NULL; |
| 116 |
|
| 117 |
skey->keydata = evp_skeymgmt_import(skey->skeymgmt, selection, params); |
| 118 |
if (skey->keydata == NULL) |
| 119 |
goto err; |
| 120 |
|
| 121 |
return skey; |
| 122 |
|
| 123 |
err: |
| 124 |
EVP_SKEY_free(skey); |
| 125 |
return NULL; |
| 126 |
} |
| 127 |
|
| 128 |
EVP_SKEY *EVP_SKEY_generate(OSSL_LIB_CTX *libctx, const char *skeymgmtname, |
| 129 |
const char *propquery, const OSSL_PARAM *params) |
| 130 |
{ |
| 131 |
EVP_SKEY *skey = evp_skey_alloc_fetch(libctx, skeymgmtname, propquery); |
| 132 |
|
| 133 |
if (skey == NULL) |
| 134 |
return NULL; |
| 135 |
|
| 136 |
skey->keydata = evp_skeymgmt_generate(skey->skeymgmt, params); |
| 137 |
if (skey->keydata == NULL) |
| 138 |
goto err; |
| 139 |
|
| 140 |
return skey; |
| 141 |
|
| 142 |
err: |
| 143 |
EVP_SKEY_free(skey); |
| 144 |
return NULL; |
| 145 |
} |
| 146 |
|
| 147 |
struct raw_key_details_st { |
| 148 |
const void **key; |
| 149 |
size_t *len; |
| 150 |
}; |
| 151 |
|
| 152 |
static int get_secret_key(const OSSL_PARAM params[], void *arg) |
| 153 |
{ |
| 154 |
const OSSL_PARAM *p = NULL; |
| 155 |
struct raw_key_details_st *raw_key = arg; |
| 156 |
|
| 157 |
if ((p = OSSL_PARAM_locate_const(params, OSSL_SKEY_PARAM_RAW_BYTES)) != NULL) |
| 158 |
return OSSL_PARAM_get_octet_string_ptr(p, raw_key->key, raw_key->len); |
| 159 |
|
| 160 |
return 0; |
| 161 |
} |
| 162 |
|
| 163 |
int EVP_SKEY_get0_raw_key(const EVP_SKEY *skey, const unsigned char **key, |
| 164 |
size_t *len) |
| 165 |
{ |
| 166 |
struct raw_key_details_st raw_key; |
| 167 |
|
| 168 |
if (skey == NULL || key == NULL || len == NULL) { |
| 169 |
ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
| 170 |
return 0; |
| 171 |
} |
| 172 |
|
| 173 |
raw_key.key = (const void **)key; |
| 174 |
raw_key.len = len; |
| 175 |
|
| 176 |
return evp_skeymgmt_export(skey->skeymgmt, skey->keydata, |
| 177 |
OSSL_SKEYMGMT_SELECT_SECRET_KEY, |
| 178 |
get_secret_key, &raw_key); |
| 179 |
} |
| 180 |
|
| 181 |
EVP_SKEY *EVP_SKEY_import_raw_key(OSSL_LIB_CTX *libctx, const char *skeymgmtname, |
| 182 |
unsigned char *key, size_t keylen, |
| 183 |
const char *propquery) |
| 184 |
{ |
| 185 |
OSSL_PARAM params[2]; |
| 186 |
|
| 187 |
params[0] = OSSL_PARAM_construct_octet_string(OSSL_SKEY_PARAM_RAW_BYTES, |
| 188 |
(void *)key, keylen); |
| 189 |
params[1] = OSSL_PARAM_construct_end(); |
| 190 |
|
| 191 |
return EVP_SKEY_import(libctx, skeymgmtname, propquery, |
| 192 |
OSSL_SKEYMGMT_SELECT_SECRET_KEY, params); |
| 193 |
} |
| 194 |
|
| 195 |
int EVP_SKEY_up_ref(EVP_SKEY *skey) |
| 196 |
{ |
| 197 |
int i; |
| 198 |
|
| 199 |
if (CRYPTO_UP_REF(&skey->references, &i) <= 0) |
| 200 |
return 0; |
| 201 |
|
| 202 |
REF_PRINT_COUNT("EVP_SKEY", i, skey); |
| 203 |
REF_ASSERT_ISNT(i < 2); |
| 204 |
return i > 1 ? 1 : 0; |
| 205 |
} |
| 206 |
|
| 207 |
void EVP_SKEY_free(EVP_SKEY *skey) |
| 208 |
{ |
| 209 |
int i; |
| 210 |
|
| 211 |
if (skey == NULL) |
| 212 |
return; |
| 213 |
|
| 214 |
CRYPTO_DOWN_REF(&skey->references, &i); |
| 215 |
REF_PRINT_COUNT("EVP_SKEY", i, skey); |
| 216 |
if (i > 0) |
| 217 |
return; |
| 218 |
REF_ASSERT_ISNT(i < 0); |
| 219 |
evp_skeymgmt_freedata(skey->skeymgmt, skey->keydata); |
| 220 |
|
| 221 |
EVP_SKEYMGMT_free(skey->skeymgmt); |
| 222 |
|
| 223 |
CRYPTO_THREAD_lock_free(skey->lock); |
| 224 |
CRYPTO_FREE_REF(&skey->references); |
| 225 |
OPENSSL_free(skey); |
| 226 |
} |
| 227 |
|
| 228 |
const char *EVP_SKEY_get0_key_id(const EVP_SKEY *skey) |
| 229 |
{ |
| 230 |
if (skey == NULL) |
| 231 |
return NULL; |
| 232 |
|
| 233 |
if (skey->skeymgmt->get_key_id) |
| 234 |
return skey->skeymgmt->get_key_id(skey->keydata); |
| 235 |
|
| 236 |
return NULL; |
| 237 |
} |
| 238 |
|
| 239 |
const char *EVP_SKEY_get0_skeymgmt_name(const EVP_SKEY *skey) |
| 240 |
{ |
| 241 |
if (skey == NULL) |
| 242 |
return NULL; |
| 243 |
|
| 244 |
return skey->skeymgmt->type_name; |
| 245 |
} |
| 246 |
|
| 247 |
const char *EVP_SKEY_get0_provider_name(const EVP_SKEY *skey) |
| 248 |
{ |
| 249 |
if (skey == NULL) |
| 250 |
return NULL; |
| 251 |
|
| 252 |
return ossl_provider_name(skey->skeymgmt->prov); |
| 253 |
} |
| 254 |
|
| 255 |
int EVP_SKEY_is_a(const EVP_SKEY *skey, const char *name) |
| 256 |
{ |
| 257 |
if (skey == NULL) |
| 258 |
return 0; |
| 259 |
|
| 260 |
return EVP_SKEYMGMT_is_a(skey->skeymgmt, name); |
| 261 |
} |
| 262 |
|
| 263 |
struct transfer_cb_ctx { |
| 264 |
int selection; |
| 265 |
EVP_SKEYMGMT *skeymgmt; |
| 266 |
void *keydata; |
| 267 |
}; |
| 268 |
|
| 269 |
static int transfer_cb(const OSSL_PARAM params[], void *arg) |
| 270 |
{ |
| 271 |
struct transfer_cb_ctx *ctx = arg; |
| 272 |
|
| 273 |
ctx->keydata = evp_skeymgmt_import(ctx->skeymgmt, ctx->selection, params); |
| 274 |
return 1; |
| 275 |
} |
| 276 |
|
| 277 |
EVP_SKEY *EVP_SKEY_to_provider(EVP_SKEY *skey, OSSL_LIB_CTX *libctx, |
| 278 |
OSSL_PROVIDER *prov, const char *propquery) |
| 279 |
{ |
| 280 |
struct transfer_cb_ctx ctx = { 0 }; |
| 281 |
EVP_SKEYMGMT *skeymgmt = NULL; |
| 282 |
EVP_SKEY *ret = NULL; |
| 283 |
|
| 284 |
if (skey == NULL) { |
| 285 |
ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
| 286 |
return NULL; |
| 287 |
} |
| 288 |
|
| 289 |
if (prov != NULL) { |
| 290 |
if (skey->skeymgmt->prov == prov) { |
| 291 |
skeymgmt = skey->skeymgmt; |
| 292 |
/* Balance the short-circuit free below */ |
| 293 |
if (!EVP_SKEYMGMT_up_ref(skeymgmt)) |
| 294 |
goto err; |
| 295 |
} else { |
| 296 |
skeymgmt = evp_skeymgmt_fetch_from_prov(prov, skey->skeymgmt->type_name, |
| 297 |
propquery); |
| 298 |
} |
| 299 |
} else { |
| 300 |
/* If no provider, get the default skeymgmt */ |
| 301 |
skeymgmt = EVP_SKEYMGMT_fetch(libctx, skey->skeymgmt->type_name, |
| 302 |
propquery); |
| 303 |
} |
| 304 |
if (skeymgmt == NULL) { |
| 305 |
ERR_raise(ERR_LIB_EVP, ERR_R_FETCH_FAILED); |
| 306 |
return NULL; |
| 307 |
} |
| 308 |
|
| 309 |
/* Short-circuit if destination provider is the same as origin */ |
| 310 |
if (skey->skeymgmt->name_id == skeymgmt->name_id |
| 311 |
&& skey->skeymgmt->prov == skeymgmt->prov) { |
| 312 |
if (!EVP_SKEY_up_ref(skey)) |
| 313 |
goto err; |
| 314 |
EVP_SKEYMGMT_free(skeymgmt); |
| 315 |
return skey; |
| 316 |
} |
| 317 |
|
| 318 |
ctx.selection = OSSL_SKEYMGMT_SELECT_ALL; |
| 319 |
ctx.skeymgmt = skeymgmt; |
| 320 |
|
| 321 |
if (!EVP_SKEY_export(skey, ctx.selection, transfer_cb, &ctx)) |
| 322 |
goto err; |
| 323 |
|
| 324 |
if (ctx.keydata == NULL) |
| 325 |
goto err; |
| 326 |
|
| 327 |
ret = evp_skey_alloc(skeymgmt); |
| 328 |
if (ret == NULL) |
| 329 |
goto err; |
| 330 |
|
| 331 |
ret->keydata = ctx.keydata; |
| 332 |
|
| 333 |
/* Balance the local reference obtained earlier (fetch or alias up_ref) */ |
| 334 |
EVP_SKEYMGMT_free(skeymgmt); |
| 335 |
|
| 336 |
return ret; |
| 337 |
|
| 338 |
err: |
| 339 |
EVP_SKEYMGMT_free(skeymgmt); |
| 340 |
EVP_SKEY_free(ret); |
| 341 |
return NULL; |
| 342 |
} |