Skip to content

Commit 5a84d26

Browse files
Replace the matching certificate slot instead of appending a duplicate
1 parent c2d1698 commit 5a84d26

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

src/internal.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2377,7 +2377,11 @@ static int SetHostCertificate(WOLFSSH_CTX* ctx,
23772377
}
23782378
}
23792379

2380+
/* Replace the matching slot if the search found one, else append. */
2381+
destIdx = HINTISSET(certIdx) ? certIdx : ctx->privateKeyCount;
2382+
23802383
if (destIdx >= WOLFSSH_MAX_PVT_KEYS) {
2384+
WFREE(der, ctx->heap, dynamicType);
23812385
ret = WS_CTX_KEY_COUNT_E;
23822386
}
23832387
else {

tests/api.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,11 @@ static void test_wolfSSH_CTX_UseCert_buffer(void)
677677
WOLFSSH_CTX* ctx = NULL;
678678
byte* cert = NULL;
679679
word32 certSz = 0;
680+
#ifndef WOLFSSH_NO_ECDSA_SHA2_NISTP256
681+
byte* lastCert = NULL;
682+
word32 count = 0;
683+
byte lastFmt = ID_NONE;
684+
#endif
680685

681686
ctx = wolfSSH_CTX_new(WOLFSSH_ENDPOINT_SERVER, NULL);
682687
AssertNotNull(ctx);
@@ -695,6 +700,10 @@ static void test_wolfSSH_CTX_UseCert_buffer(void)
695700
#ifndef WOLFSSH_NO_ECDSA_SHA2_NISTP256
696701
AssertIntEQ(WS_SUCCESS,
697702
wolfSSH_CTX_UseCert_buffer(ctx, cert, certSz, WOLFSSH_FORMAT_PEM));
703+
count = ctx->privateKeyCount;
704+
lastCert = ctx->privateKey[0].cert;
705+
AssertIntEQ(1, count);
706+
AssertNotNull(lastCert);
698707
#endif
699708

700709
AssertIntEQ(WS_BAD_FILETYPE_E,
@@ -714,6 +723,22 @@ static void test_wolfSSH_CTX_UseCert_buffer(void)
714723
#ifndef WOLFSSH_NO_ECDSA_SHA2_NISTP256
715724
AssertIntEQ(WS_SUCCESS,
716725
wolfSSH_CTX_UseCert_buffer(ctx, cert, certSz, WOLFSSH_FORMAT_ASN1));
726+
/* Reloading a certificate replaces the slot rather than appending a
727+
* duplicate, so the count and the advertised algorithm list hold. */
728+
AssertIntEQ(count, ctx->privateKeyCount);
729+
AssertPtrNE(lastCert, ctx->privateKey[0].cert);
730+
AssertIntEQ(1, ctx->publicKeyAlgoCount);
731+
732+
/* Every slot taken and none matching: the load is rejected and the
733+
* DER released rather than leaked. */
734+
lastFmt = ctx->privateKey[0].publicKeyFmt;
735+
ctx->privateKey[0].publicKeyFmt = ID_NONE;
736+
ctx->privateKeyCount = WOLFSSH_MAX_PVT_KEYS;
737+
AssertIntEQ(WS_CTX_KEY_COUNT_E,
738+
wolfSSH_CTX_UseCert_buffer(ctx, cert, certSz, WOLFSSH_FORMAT_ASN1));
739+
AssertIntEQ(WOLFSSH_MAX_PVT_KEYS, ctx->privateKeyCount);
740+
ctx->privateKeyCount = count;
741+
ctx->privateKey[0].publicKeyFmt = lastFmt;
717742
#endif
718743

719744
wolfSSH_CTX_free(ctx);

0 commit comments

Comments
 (0)