Skip to content

Commit 5fd1f57

Browse files
authored
Merge pull request #367 from padelsbach/wp-finding-166-167
Erase password buffers when exiting routines
2 parents 65fa70f + bec7427 commit 5fd1f57

5 files changed

Lines changed: 29 additions & 7 deletions

File tree

src/wp_dec_epki2pki.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ static int wp_epki2pki_decode(wp_Epki2Pki* ctx, OSSL_CORE_BIO* coreBio,
263263
/* Dispose of the EPKI data buffer. */
264264
OPENSSL_free(data);
265265

266+
OPENSSL_cleanse(password, sizeof(password));
267+
266268
WOLFPROV_LEAVE(WP_LOG_COMP_PK, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
267269
return ok;
268270
}

src/wp_dh_exch.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,10 @@ static int wp_dh_set_param_kdf_digest(wp_DhCtx* ctx, const OSSL_PARAM params[])
512512
}
513513
if (ok && (mdName != NULL)) {
514514
const char* mdProps = NULL;
515+
size_t mdNameLen = OPENSSL_strnlen(mdName, sizeof(ctx->kdfMdName) - 1);
515516

516-
XMEMCPY(ctx->kdfMdName, mdName, XSTRLEN(mdName) + 1);
517+
XMEMCPY(ctx->kdfMdName, mdName, mdNameLen);
518+
ctx->kdfMdName[mdNameLen] = '\0';
517519
if (!wp_params_get_utf8_string_ptr(params,
518520
OSSL_EXCHANGE_PARAM_KDF_DIGEST_PROPS, &mdProps)) {
519521
ok = 0;

src/wp_ecdh_exch.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,8 @@ static int wp_ecdh_derive(wp_EcdhCtx* ctx, unsigned char* secret,
315315
int done = 0;
316316
unsigned char* out;
317317
size_t outLen;
318-
unsigned char tmp[72];
318+
unsigned char* tmp = NULL;
319+
size_t maxLen = (size_t)wp_ecc_get_size(ctx->key);
319320

320321
WOLFPROV_ENTER(WP_LOG_COMP_ECDH, "wp_ecdh_derive");
321322

@@ -326,10 +327,10 @@ static int wp_ecdh_derive(wp_EcdhCtx* ctx, unsigned char* secret,
326327
/* No output buffer, return maximum size only. */
327328
if (ok && (secret == NULL)) {
328329
if (ctx->kdfType == WP_KDF_NONE) {
329-
*secLen = wp_ecc_get_size(ctx->key);
330+
*secLen = maxLen;
330331
}
331332
else {
332-
*secLen = ctx->keyLen;;
333+
*secLen = ctx->keyLen;
333334
}
334335
done = 1;
335336
}
@@ -342,8 +343,15 @@ static int wp_ecdh_derive(wp_EcdhCtx* ctx, unsigned char* secret,
342343
}
343344
else if (ctx->kdfType == WP_KDF_X963) {
344345
/* Output of ECDH key exchange goes into temporary buffer. */
345-
out = tmp;
346-
outLen = sizeof(tmp);
346+
tmp = OPENSSL_malloc(maxLen);
347+
if (tmp == NULL) {
348+
ok = 0;
349+
outLen = 0;
350+
}
351+
else {
352+
out = tmp;
353+
outLen = maxLen;
354+
}
347355
}
348356
else {
349357
ok = 0;
@@ -365,6 +373,8 @@ static int wp_ecdh_derive(wp_EcdhCtx* ctx, unsigned char* secret,
365373
}
366374
}
367375

376+
OPENSSL_clear_free(tmp, maxLen);
377+
368378
WOLFPROV_LEAVE(WP_LOG_COMP_ECDH, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
369379
return ok;
370380
}
@@ -460,8 +470,10 @@ static int wp_ecdh_set_param_kdf_digest(wp_EcdhCtx* ctx,
460470
}
461471
if (ok && (mdName != NULL)) {
462472
const char* mdProps = NULL;
473+
size_t mdNameLen = OPENSSL_strnlen(mdName, sizeof(ctx->kdfMdName) - 1);
463474

464-
XMEMCPY(ctx->kdfMdName, mdName, XSTRLEN(mdName) + 1);
475+
XMEMCPY(ctx->kdfMdName, mdName, mdNameLen);
476+
ctx->kdfMdName[mdNameLen] = '\0';
465477
if (!wp_params_get_utf8_string_ptr(params,
466478
OSSL_EXCHANGE_PARAM_KDF_DIGEST_PROPS, &mdProps)) {
467479
ok = 0;

src/wp_internal.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,8 @@ int wp_encrypt_key(WOLFPROV_CTX* provCtx, const char* cipherName,
997997
*keyLen = len;
998998
}
999999

1000+
OPENSSL_cleanse(password, sizeof(password));
1001+
10001002
WOLFPROV_LEAVE(WP_LOG_COMP_PROVIDER, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
10011003
return ok;
10021004
#else

src/wp_rsa_kmgmt.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2472,6 +2472,8 @@ static int wp_rsa_decode_enc_pki(wp_Rsa* rsa, unsigned char* data, word32 len,
24722472
ok = wp_rsa_decode_pki(rsa, data, len);
24732473
}
24742474

2475+
OPENSSL_cleanse(password, sizeof(password));
2476+
24752477
WOLFPROV_LEAVE_SILENT(WP_LOG_COMP_RSA, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__),
24762478
ok);
24772479
return ok;
@@ -3263,6 +3265,8 @@ static int wp_rsa_encode_enc_pki(const wp_RsaEncDecCtx* ctx, const wp_Rsa* rsa,
32633265

32643266
XFREE(encodedKey, NULL, DYNAMIC_TYPE_RSA_BUFFER);
32653267

3268+
OPENSSL_cleanse(password, sizeof(password));
3269+
32663270
WOLFPROV_LEAVE(WP_LOG_COMP_RSA, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
32673271
return ok;
32683272
}

0 commit comments

Comments
 (0)