| 1 |
/* |
| 2 |
* Copyright 1999-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 |
/* clang-format off */ |
| 10 |
{- |
| 11 |
use OpenSSL::paramnames qw(produce_param_decoder); |
| 12 |
-} |
| 13 |
/* clang-format on */ |
| 14 |
|
| 15 |
#include <openssl/trace.h> |
| 16 |
#include <stdlib.h> |
| 17 |
#include <stdarg.h> |
| 18 |
#include <string.h> |
| 19 |
#include <openssl/evp.h> |
| 20 |
#include <openssl/kdf.h> |
| 21 |
#include <openssl/core_names.h> |
| 22 |
#include <openssl/proverr.h> |
| 23 |
#include "internal/common.h" |
| 24 |
#include "internal/cryptlib.h" |
| 25 |
#include "internal/numbers.h" |
| 26 |
#include "crypto/evp.h" |
| 27 |
#include "prov/provider_ctx.h" |
| 28 |
#include "prov/providercommon.h" |
| 29 |
#include "prov/implementations.h" |
| 30 |
#include "prov/provider_util.h" |
| 31 |
|
| 32 |
static OSSL_FUNC_kdf_newctx_fn kdf_pkcs12_new; |
| 33 |
static OSSL_FUNC_kdf_dupctx_fn kdf_pkcs12_dup; |
| 34 |
static OSSL_FUNC_kdf_freectx_fn kdf_pkcs12_free; |
| 35 |
static OSSL_FUNC_kdf_reset_fn kdf_pkcs12_reset; |
| 36 |
static OSSL_FUNC_kdf_derive_fn kdf_pkcs12_derive; |
| 37 |
static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_pkcs12_settable_ctx_params; |
| 38 |
static OSSL_FUNC_kdf_set_ctx_params_fn kdf_pkcs12_set_ctx_params; |
| 39 |
static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_pkcs12_gettable_ctx_params; |
| 40 |
static OSSL_FUNC_kdf_get_ctx_params_fn kdf_pkcs12_get_ctx_params; |
| 41 |
|
| 42 |
typedef struct { |
| 43 |
void *provctx; |
| 44 |
PROV_DIGEST digest; |
| 45 |
unsigned char *pass; |
| 46 |
size_t pass_len; |
| 47 |
unsigned char *salt; |
| 48 |
size_t salt_len; |
| 49 |
uint64_t iter; |
| 50 |
int id; |
| 51 |
} KDF_PKCS12; |
| 52 |
|
| 53 |
/* PKCS12 compatible key/IV generation */ |
| 54 |
|
| 55 |
static int pkcs12kdf_derive(const unsigned char *pass, size_t passlen, |
| 56 |
const unsigned char *salt, size_t saltlen, |
| 57 |
int id, uint64_t iter, const EVP_MD *md_type, |
| 58 |
unsigned char *out, size_t n) |
| 59 |
{ |
| 60 |
unsigned char *B = NULL, *D = NULL, *I = NULL, *p = NULL, *Ai = NULL; |
| 61 |
size_t Slen, Plen, Ilen; |
| 62 |
size_t i, j, k, u, v; |
| 63 |
uint64_t iter_cnt; |
| 64 |
int ret = 0, ui, vi; |
| 65 |
EVP_MD_CTX *ctx = NULL; |
| 66 |
|
| 67 |
ctx = EVP_MD_CTX_new(); |
| 68 |
if (ctx == NULL) { |
| 69 |
ERR_raise(ERR_LIB_PROV, ERR_R_EVP_LIB); |
| 70 |
goto end; |
| 71 |
} |
| 72 |
vi = EVP_MD_get_block_size(md_type); |
| 73 |
ui = EVP_MD_get_size(md_type); |
| 74 |
if (ui <= 0 || vi <= 0) { |
| 75 |
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE); |
| 76 |
goto end; |
| 77 |
} |
| 78 |
u = (size_t)ui; |
| 79 |
v = (size_t)vi; |
| 80 |
D = OPENSSL_malloc(v); |
| 81 |
Ai = OPENSSL_malloc(u); |
| 82 |
B = OPENSSL_malloc(v + 1); |
| 83 |
Slen = v * ((saltlen + v - 1) / v); |
| 84 |
if (passlen != 0) |
| 85 |
Plen = v * ((passlen + v - 1) / v); |
| 86 |
else |
| 87 |
Plen = 0; |
| 88 |
Ilen = Slen + Plen; |
| 89 |
I = OPENSSL_malloc(Ilen); |
| 90 |
if (D == NULL || Ai == NULL || B == NULL || I == NULL) |
| 91 |
goto end; |
| 92 |
for (i = 0; i < v; i++) |
| 93 |
D[i] = id; |
| 94 |
p = I; |
| 95 |
for (i = 0; i < Slen; i++) |
| 96 |
*p++ = salt[i % saltlen]; |
| 97 |
for (i = 0; i < Plen; i++) |
| 98 |
*p++ = pass[i % passlen]; |
| 99 |
for (;;) { |
| 100 |
if (!EVP_DigestInit_ex(ctx, md_type, NULL) |
| 101 |
|| !EVP_DigestUpdate(ctx, D, v) |
| 102 |
|| !EVP_DigestUpdate(ctx, I, Ilen) |
| 103 |
|| !EVP_DigestFinal_ex(ctx, Ai, NULL)) |
| 104 |
goto end; |
| 105 |
for (iter_cnt = 1; iter_cnt < iter; iter_cnt++) { |
| 106 |
if (!EVP_DigestInit_ex(ctx, md_type, NULL) |
| 107 |
|| !EVP_DigestUpdate(ctx, Ai, u) |
| 108 |
|| !EVP_DigestFinal_ex(ctx, Ai, NULL)) |
| 109 |
goto end; |
| 110 |
} |
| 111 |
memcpy(out, Ai, n < u ? n : u); |
| 112 |
if (u >= n) { |
| 113 |
ret = 1; |
| 114 |
break; |
| 115 |
} |
| 116 |
n -= u; |
| 117 |
out += u; |
| 118 |
for (j = 0; j < v; j++) |
| 119 |
B[j] = Ai[j % u]; |
| 120 |
for (j = 0; j < Ilen; j += v) { |
| 121 |
unsigned char *Ij = I + j; |
| 122 |
uint16_t c = 1; |
| 123 |
|
| 124 |
/* Work out Ij = Ij + B + 1 */ |
| 125 |
for (k = v; k > 0;) { |
| 126 |
k--; |
| 127 |
c += Ij[k] + B[k]; |
| 128 |
Ij[k] = (unsigned char)c; |
| 129 |
c >>= 8; |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
end: |
| 135 |
OPENSSL_free(Ai); |
| 136 |
OPENSSL_free(B); |
| 137 |
OPENSSL_free(D); |
| 138 |
OPENSSL_free(I); |
| 139 |
EVP_MD_CTX_free(ctx); |
| 140 |
return ret; |
| 141 |
} |
| 142 |
|
| 143 |
static void *kdf_pkcs12_new(void *provctx) |
| 144 |
{ |
| 145 |
KDF_PKCS12 *ctx; |
| 146 |
|
| 147 |
if (!ossl_prov_is_running()) |
| 148 |
return NULL; |
| 149 |
|
| 150 |
ctx = OPENSSL_zalloc(sizeof(*ctx)); |
| 151 |
if (ctx == NULL) |
| 152 |
return NULL; |
| 153 |
ctx->provctx = provctx; |
| 154 |
return ctx; |
| 155 |
} |
| 156 |
|
| 157 |
static void kdf_pkcs12_cleanup(KDF_PKCS12 *ctx) |
| 158 |
{ |
| 159 |
ossl_prov_digest_reset(&ctx->digest); |
| 160 |
OPENSSL_free(ctx->salt); |
| 161 |
OPENSSL_clear_free(ctx->pass, ctx->pass_len); |
| 162 |
memset(ctx, 0, sizeof(*ctx)); |
| 163 |
} |
| 164 |
|
| 165 |
static void kdf_pkcs12_free(void *vctx) |
| 166 |
{ |
| 167 |
KDF_PKCS12 *ctx = (KDF_PKCS12 *)vctx; |
| 168 |
|
| 169 |
if (ctx != NULL) { |
| 170 |
kdf_pkcs12_cleanup(ctx); |
| 171 |
OPENSSL_free(ctx); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
static void kdf_pkcs12_reset(void *vctx) |
| 176 |
{ |
| 177 |
KDF_PKCS12 *ctx = (KDF_PKCS12 *)vctx; |
| 178 |
void *provctx = ctx->provctx; |
| 179 |
|
| 180 |
kdf_pkcs12_cleanup(ctx); |
| 181 |
ctx->provctx = provctx; |
| 182 |
} |
| 183 |
|
| 184 |
static void *kdf_pkcs12_dup(void *vctx) |
| 185 |
{ |
| 186 |
const KDF_PKCS12 *src = (const KDF_PKCS12 *)vctx; |
| 187 |
KDF_PKCS12 *dest; |
| 188 |
|
| 189 |
dest = kdf_pkcs12_new(src->provctx); |
| 190 |
if (dest != NULL) { |
| 191 |
if (!ossl_prov_memdup(src->salt, src->salt_len, |
| 192 |
&dest->salt, &dest->salt_len) |
| 193 |
|| !ossl_prov_memdup(src->pass, src->pass_len, |
| 194 |
&dest->pass, &dest->pass_len) |
| 195 |
|| !ossl_prov_digest_copy(&dest->digest, &src->digest)) |
| 196 |
goto err; |
| 197 |
dest->iter = src->iter; |
| 198 |
dest->id = src->id; |
| 199 |
} |
| 200 |
return dest; |
| 201 |
|
| 202 |
err: |
| 203 |
kdf_pkcs12_free(dest); |
| 204 |
return NULL; |
| 205 |
} |
| 206 |
|
| 207 |
static int pkcs12kdf_set_membuf(unsigned char **buffer, size_t *buflen, |
| 208 |
const OSSL_PARAM *p) |
| 209 |
{ |
| 210 |
OPENSSL_clear_free(*buffer, *buflen); |
| 211 |
*buffer = NULL; |
| 212 |
*buflen = 0; |
| 213 |
|
| 214 |
if (p->data_size == 0) { |
| 215 |
if ((*buffer = OPENSSL_malloc(1)) == NULL) |
| 216 |
return 0; |
| 217 |
} else if (p->data != NULL) { |
| 218 |
if (!OSSL_PARAM_get_octet_string(p, (void **)buffer, 0, buflen)) |
| 219 |
return 0; |
| 220 |
} |
| 221 |
return 1; |
| 222 |
} |
| 223 |
|
| 224 |
static int kdf_pkcs12_derive(void *vctx, unsigned char *key, size_t keylen, |
| 225 |
const OSSL_PARAM params[]) |
| 226 |
{ |
| 227 |
KDF_PKCS12 *ctx = (KDF_PKCS12 *)vctx; |
| 228 |
const EVP_MD *md; |
| 229 |
|
| 230 |
if (!ossl_prov_is_running() || !kdf_pkcs12_set_ctx_params(ctx, params)) |
| 231 |
return 0; |
| 232 |
|
| 233 |
if (ctx->pass == NULL) { |
| 234 |
ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_PASS); |
| 235 |
return 0; |
| 236 |
} |
| 237 |
|
| 238 |
if (ctx->salt == NULL) { |
| 239 |
ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SALT); |
| 240 |
return 0; |
| 241 |
} |
| 242 |
|
| 243 |
md = ossl_prov_digest_md(&ctx->digest); |
| 244 |
return pkcs12kdf_derive(ctx->pass, ctx->pass_len, ctx->salt, ctx->salt_len, |
| 245 |
ctx->id, ctx->iter, md, key, keylen); |
| 246 |
} |
| 247 |
|
| 248 |
/* clang-format off */ |
| 249 |
{- produce_param_decoder('pkcs12_set_ctx_params', |
| 250 |
(['OSSL_KDF_PARAM_PROPERTIES', 'propq', 'utf8_string'], |
| 251 |
['OSSL_ALG_PARAM_ENGINE', 'engine', 'utf8_string', 'hidden'], |
| 252 |
['OSSL_KDF_PARAM_DIGEST', 'digest', 'utf8_string'], |
| 253 |
['OSSL_KDF_PARAM_PASSWORD', 'pw', 'octet_string'], |
| 254 |
['OSSL_KDF_PARAM_SALT', 'salt', 'octet_string'], |
| 255 |
['OSSL_KDF_PARAM_ITER', 'iter', 'uint64'], |
| 256 |
['OSSL_KDF_PARAM_PKCS12_ID', 'p12id', 'int'], |
| 257 |
)); -} |
| 258 |
/* clang-format on */ |
| 259 |
|
| 260 |
static int kdf_pkcs12_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
| 261 |
{ |
| 262 |
struct pkcs12_set_ctx_params_st p; |
| 263 |
KDF_PKCS12 *ctx = vctx; |
| 264 |
OSSL_LIB_CTX *provctx; |
| 265 |
|
| 266 |
if (ctx == NULL || !pkcs12_set_ctx_params_decoder(params, &p)) |
| 267 |
return 0; |
| 268 |
|
| 269 |
provctx = PROV_LIBCTX_OF(ctx->provctx); |
| 270 |
|
| 271 |
if (!ossl_prov_digest_load(&ctx->digest, p.digest, |
| 272 |
p.propq, p.engine, provctx)) |
| 273 |
return 0; |
| 274 |
|
| 275 |
if (p.pw != NULL && !pkcs12kdf_set_membuf(&ctx->pass, &ctx->pass_len, p.pw)) |
| 276 |
return 0; |
| 277 |
|
| 278 |
if (p.salt != NULL |
| 279 |
&& !pkcs12kdf_set_membuf(&ctx->salt, &ctx->salt_len, p.salt)) |
| 280 |
return 0; |
| 281 |
|
| 282 |
if (p.p12id != NULL && !OSSL_PARAM_get_int(p.p12id, &ctx->id)) |
| 283 |
return 0; |
| 284 |
|
| 285 |
if (p.iter != NULL && !OSSL_PARAM_get_uint64(p.iter, &ctx->iter)) |
| 286 |
return 0; |
| 287 |
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
| 288 |
/* |
| 289 |
* If we're running the fuzzer, limit iteration count to |
| 290 |
* 100 so we don't time out running the derivation for |
| 291 |
* a really long time |
| 292 |
*/ |
| 293 |
if (getenv("OPENSSL_RUNNING_UNIT_TESTS") == NULL && p.iter != NULL && ctx->iter > 100) |
| 294 |
ctx->iter = 100; |
| 295 |
#endif |
| 296 |
return 1; |
| 297 |
} |
| 298 |
|
| 299 |
static const OSSL_PARAM *kdf_pkcs12_settable_ctx_params( |
| 300 |
ossl_unused void *ctx, ossl_unused void *provctx) |
| 301 |
{ |
| 302 |
return pkcs12_set_ctx_params_list; |
| 303 |
} |
| 304 |
|
| 305 |
/* clang-format off */ |
| 306 |
{- produce_param_decoder('pkcs12_get_ctx_params', |
| 307 |
(['OSSL_KDF_PARAM_SIZE', 'size', 'size_t'], |
| 308 |
)); -} |
| 309 |
/* clang-format on */ |
| 310 |
|
| 311 |
static int kdf_pkcs12_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
| 312 |
{ |
| 313 |
struct pkcs12_get_ctx_params_st p; |
| 314 |
KDF_PKCS12 *ctx = vctx; |
| 315 |
|
| 316 |
if (ctx == NULL || !pkcs12_get_ctx_params_decoder(params, &p)) |
| 317 |
return 0; |
| 318 |
|
| 319 |
if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, SIZE_MAX)) |
| 320 |
return 0; |
| 321 |
return 1; |
| 322 |
} |
| 323 |
|
| 324 |
static const OSSL_PARAM *kdf_pkcs12_gettable_ctx_params( |
| 325 |
ossl_unused void *ctx, ossl_unused void *provctx) |
| 326 |
{ |
| 327 |
return pkcs12_get_ctx_params_list; |
| 328 |
} |
| 329 |
|
| 330 |
const OSSL_DISPATCH ossl_kdf_pkcs12_functions[] = { |
| 331 |
{ OSSL_FUNC_KDF_NEWCTX, (void (*)(void))kdf_pkcs12_new }, |
| 332 |
{ OSSL_FUNC_KDF_DUPCTX, (void (*)(void))kdf_pkcs12_dup }, |
| 333 |
{ OSSL_FUNC_KDF_FREECTX, (void (*)(void))kdf_pkcs12_free }, |
| 334 |
{ OSSL_FUNC_KDF_RESET, (void (*)(void))kdf_pkcs12_reset }, |
| 335 |
{ OSSL_FUNC_KDF_DERIVE, (void (*)(void))kdf_pkcs12_derive }, |
| 336 |
{ OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, |
| 337 |
(void (*)(void))kdf_pkcs12_settable_ctx_params }, |
| 338 |
{ OSSL_FUNC_KDF_SET_CTX_PARAMS, (void (*)(void))kdf_pkcs12_set_ctx_params }, |
| 339 |
{ OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, |
| 340 |
(void (*)(void))kdf_pkcs12_gettable_ctx_params }, |
| 341 |
{ OSSL_FUNC_KDF_GET_CTX_PARAMS, (void (*)(void))kdf_pkcs12_get_ctx_params }, |
| 342 |
OSSL_DISPATCH_END |
| 343 |
}; |