@@ -2501,6 +2501,7 @@ int wolfSSH_CTX_UsePrivateKey_fromStore(WOLFSSH_CTX* ctx,
25012501 PCCERT_CONTEXT pCertContext = NULL ;
25022502 word32 keyIdx = 0 ;
25032503 byte keyId = ID_NONE ;
2504+ byte addedNewSlot = 0 ;
25042505 void * heap = NULL ;
25052506
25062507 WLOG (WS_LOG_DEBUG , "Entering wolfSSH_CTX_UsePrivateKey_fromStore()" );
@@ -2624,6 +2625,7 @@ int wolfSSH_CTX_UsePrivateKey_fromStore(WOLFSSH_CTX* ctx,
26242625 if (keyIdx == WOLFSSH_MAX_PVT_KEYS && ctx -> privateKeyCount < WOLFSSH_MAX_PVT_KEYS ) {
26252626 keyIdx = ctx -> privateKeyCount ;
26262627 ctx -> privateKeyCount ++ ;
2628+ addedNewSlot = 1 ;
26272629 }
26282630 }
26292631
@@ -2634,6 +2636,19 @@ int wolfSSH_CTX_UsePrivateKey_fromStore(WOLFSSH_CTX* ctx,
26342636 return WS_MEMORY_E ;
26352637 }
26362638
2639+ /* Free existing resources if replacing an existing slot */
2640+ if (ctx -> privateKey [keyIdx ].useCertStore ) {
2641+ if (ctx -> privateKey [keyIdx ].certStoreContext != NULL )
2642+ CertFreeCertificateContext (
2643+ (PCCERT_CONTEXT )ctx -> privateKey [keyIdx ].certStoreContext );
2644+ if (ctx -> privateKey [keyIdx ].storeName != NULL )
2645+ WFREE (ctx -> privateKey [keyIdx ].storeName , heap , DYNTYPE_STRING );
2646+ if (ctx -> privateKey [keyIdx ].subjectName != NULL )
2647+ WFREE (ctx -> privateKey [keyIdx ].subjectName , heap , DYNTYPE_STRING );
2648+ if (ctx -> privateKey [keyIdx ].cert != NULL )
2649+ WFREE (ctx -> privateKey [keyIdx ].cert , heap , DYNTYPE_CERT );
2650+ }
2651+
26372652 /* Set up the private key structure */
26382653 ctx -> privateKey [keyIdx ].publicKeyFmt = keyId ;
26392654 ctx -> privateKey [keyIdx ].useCertStore = 1 ;
@@ -2668,8 +2683,8 @@ int wolfSSH_CTX_UsePrivateKey_fromStore(WOLFSSH_CTX* ctx,
26682683 byte * certBuf = (byte * )WMALLOC (certSz , heap , DYNTYPE_CERT );
26692684 if (certBuf == NULL ) {
26702685 /* Cleanup */
2671- WFREE (( void * ) ctx -> privateKey [keyIdx ].storeName , heap , DYNTYPE_STRING );
2672- WFREE (( void * ) ctx -> privateKey [keyIdx ].subjectName , heap , DYNTYPE_STRING );
2686+ WFREE (ctx -> privateKey [keyIdx ].storeName , heap , DYNTYPE_STRING );
2687+ WFREE (ctx -> privateKey [keyIdx ].subjectName , heap , DYNTYPE_STRING );
26732688 CertFreeCertificateContext (pCertContext );
26742689 CertCloseStore (hStore , 0 );
26752690 WLOG (WS_LOG_DEBUG , "wolfSSH_CTX_UsePrivateKey_fromStore: Certificate buffer allocation failed" );
@@ -2697,8 +2712,8 @@ int wolfSSH_CTX_UsePrivateKey_fromStore(WOLFSSH_CTX* ctx,
26972712 "access private key, error: %lu. Check that the current user "
26982713 "or service account has permission to access the key." , dwErr );
26992714 /* Cleanup already stored data */
2700- WFREE (( void * ) ctx -> privateKey [keyIdx ].storeName , heap , DYNTYPE_STRING );
2701- WFREE (( void * ) ctx -> privateKey [keyIdx ].subjectName , heap , DYNTYPE_STRING );
2715+ WFREE (ctx -> privateKey [keyIdx ].storeName , heap , DYNTYPE_STRING );
2716+ WFREE (ctx -> privateKey [keyIdx ].subjectName , heap , DYNTYPE_STRING );
27022717 WFREE (ctx -> privateKey [keyIdx ].cert , heap , DYNTYPE_CERT );
27032718 ctx -> privateKey [keyIdx ].useCertStore = 0 ;
27042719 CertFreeCertificateContext (pCertContext );
@@ -2707,7 +2722,8 @@ int wolfSSH_CTX_UsePrivateKey_fromStore(WOLFSSH_CTX* ctx,
27072722 ctx -> privateKey [keyIdx ].subjectName = NULL ;
27082723 ctx -> privateKey [keyIdx ].cert = NULL ;
27092724 ctx -> privateKey [keyIdx ].certSz = 0 ;
2710- ctx -> privateKeyCount -- ;
2725+ if (addedNewSlot )
2726+ ctx -> privateKeyCount -- ;
27112727 CertCloseStore (hStore , 0 );
27122728 return WS_CRYPTO_FAILED ;
27132729 }
0 commit comments