@@ -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