@@ -15419,55 +15419,80 @@ static int BuildUserAuthRequestPublicKey(WOLFSSH* ssh,
1541915419int SendUserAuthKeyboardResponse(WOLFSSH* ssh)
1542015420{
1542115421 byte* output;
15422+ int authRet = WOLFSSH_USERAUTH_FAILURE;
1542215423 int ret = WS_SUCCESS;
1542315424 word32 idx;
1542415425 word32 payloadSz = 0;
1542515426 word32 prompt;
1542615427 WS_UserAuthData authData;
1542715428
15428- WLOG(WS_LOG_DEBUG, "Entering SendUserAuthKeyboardResponse()" );
15429+ WMEMSET(&authData, 0, sizeof(authData) );
1542915430
15430- authData.type = WOLFSSH_USERAUTH_KEYBOARD;
15431- authData.username = (const byte*)ssh->userName;
15432- authData.usernameSz = ssh->userNameSz;
15433- authData.sf.keyboard.promptCount = ssh->kbAuth.promptCount;
15434- authData.sf.keyboard.promptName = ssh->kbAuth.promptName;
15435- authData.sf.keyboard.promptNameSz = ssh->kbAuth.promptName ?
15436- (word32)WSTRLEN((char*)ssh->kbAuth.promptName) : 0;
15437- authData.sf.keyboard.promptInstruction = ssh->kbAuth.promptInstruction;
15438- authData.sf.keyboard.promptInstructionSz = ssh->kbAuth.promptInstruction ?
15439- (word32)WSTRLEN((char*)ssh->kbAuth.promptInstruction) : 0;
15440- authData.sf.keyboard.promptLanguage = ssh->kbAuth.promptLanguage;
15441- authData.sf.keyboard.promptLanguageSz = ssh->kbAuth.promptLanguage ?
15442- (word32)WSTRLEN((char*)ssh->kbAuth.promptLanguage) : 0;
15443- authData.sf.keyboard.prompts = ssh->kbAuth.prompts;
15444- authData.sf.keyboard.promptEcho = ssh->kbAuth.promptEcho;
15445- authData.sf.keyboard.responseCount = 0;
15446-
15447- WLOG(WS_LOG_DEBUG, "SUAR: Calling the userauth callback");
15448- ret = ssh->ctx->userAuthCb(WOLFSSH_USERAUTH_KEYBOARD, &authData,
15449- ssh->userAuthCtx);
15431+ WLOG(WS_LOG_DEBUG, "Entering SendUserAuthKeyboardResponse()");
1545015432
15451- WFREE(ssh->kbAuth.promptName, ssh->ctx->heap, 0);
15452- WFREE(ssh->kbAuth.promptInstruction, ssh->ctx->heap, 0);
15453- WFREE(ssh->kbAuth.promptLanguage, ssh->ctx->heap, 0);
15454- WFREE(ssh->kbAuth.promptEcho, ssh->ctx->heap, 0);
15455- for (prompt = 0; prompt < ssh->kbAuth.promptCount; prompt++) {
15456- WFREE((void*)ssh->kbAuth.prompts[prompt], ssh->ctx->heap, 0);
15433+ if (ssh == NULL || ssh->ctx == NULL) {
15434+ ret = WS_BAD_ARGUMENT;
1545715435 }
15458- WFREE(ssh->kbAuth.prompts, ssh->ctx->heap, 0);
15459-
15460- if (ret != WOLFSSH_USERAUTH_SUCCESS) {
15461- WLOG(WS_LOG_DEBUG, "SUAR: Couldn't get keyboard auth");
15462- ret = WS_FATAL_ERROR;
15436+ if (ret == WS_SUCCESS && ssh->ctx->userAuthCb == NULL) {
15437+ ret = WS_INVALID_STATE_E;
1546315438 }
15464- else if (ssh->kbAuth.promptCount != authData.sf.keyboard.responseCount) {
15465- WLOG(WS_LOG_DEBUG,
15466- "SUAR: Keyboard auth response count does not match request count");
15467- ret = WS_USER_AUTH_E;
15439+
15440+ if (ret == WS_SUCCESS) {
15441+ authData.type = WOLFSSH_USERAUTH_KEYBOARD;
15442+ authData.username = (const byte*)ssh->userName;
15443+ authData.usernameSz = ssh->userNameSz;
15444+ authData.sf.keyboard.promptCount = ssh->kbAuth.promptCount;
15445+ authData.sf.keyboard.promptName = ssh->kbAuth.promptName;
15446+ authData.sf.keyboard.promptNameSz = ssh->kbAuth.promptName ?
15447+ (word32)WSTRLEN((char*)ssh->kbAuth.promptName) : 0;
15448+ authData.sf.keyboard.promptInstruction = ssh->kbAuth.promptInstruction;
15449+ authData.sf.keyboard.promptInstructionSz = ssh->kbAuth.promptInstruction ?
15450+ (word32)WSTRLEN((char*)ssh->kbAuth.promptInstruction) : 0;
15451+ authData.sf.keyboard.promptLanguage = ssh->kbAuth.promptLanguage;
15452+ authData.sf.keyboard.promptLanguageSz = ssh->kbAuth.promptLanguage ?
15453+ (word32)WSTRLEN((char*)ssh->kbAuth.promptLanguage) : 0;
15454+ authData.sf.keyboard.prompts = ssh->kbAuth.prompts;
15455+ authData.sf.keyboard.promptEcho = ssh->kbAuth.promptEcho;
15456+ authData.sf.keyboard.responseCount = 0;
15457+
15458+ WLOG(WS_LOG_DEBUG, "SUAR: Calling the userauth callback");
15459+ authRet = ssh->ctx->userAuthCb(WOLFSSH_USERAUTH_KEYBOARD, &authData,
15460+ ssh->userAuthCtx);
15461+ }
15462+
15463+ if (ret == WS_SUCCESS) {
15464+ if (authRet != WOLFSSH_USERAUTH_SUCCESS) {
15465+ WLOG(WS_LOG_DEBUG, "SUAR: Couldn't get keyboard auth");
15466+ ret = WS_FATAL_ERROR;
15467+ }
15468+ else if (ssh->kbAuth.promptCount != authData.sf.keyboard.responseCount) {
15469+ WLOG(WS_LOG_DEBUG,
15470+ "SUAR: Keyboard auth response count does not match request count");
15471+ ret = WS_USER_AUTH_E;
15472+ }
15473+ else {
15474+ WLOG(WS_LOG_DEBUG, "SUAR: Callback successful keyboard");
15475+ }
1546815476 }
15469- else {
15470- WLOG(WS_LOG_DEBUG, "SUAR: Callback successful keyboard");
15477+
15478+ if (ssh != NULL && ssh->ctx != NULL) {
15479+ WFREE(ssh->kbAuth.promptName, ssh->ctx->heap, 0);
15480+ WFREE(ssh->kbAuth.promptInstruction, ssh->ctx->heap, 0);
15481+ WFREE(ssh->kbAuth.promptLanguage, ssh->ctx->heap, 0);
15482+ WFREE(ssh->kbAuth.promptEcho, ssh->ctx->heap, 0);
15483+ if (ssh->kbAuth.prompts != NULL) {
15484+ for (prompt = 0; prompt < ssh->kbAuth.promptCount; prompt++) {
15485+ WFREE((void*)ssh->kbAuth.prompts[prompt], ssh->ctx->heap, 0);
15486+ }
15487+ }
15488+ WFREE(ssh->kbAuth.prompts, ssh->ctx->heap, 0);
15489+
15490+ ssh->kbAuth.promptName = NULL;
15491+ ssh->kbAuth.promptInstruction = NULL;
15492+ ssh->kbAuth.promptLanguage = NULL;
15493+ ssh->kbAuth.promptEcho = NULL;
15494+ ssh->kbAuth.prompts = NULL;
15495+ ssh->kbAuth.promptCount = 0;
1547115496 }
1547215497
1547315498 payloadSz = MSG_ID_SZ;
@@ -15479,13 +15504,13 @@ int SendUserAuthKeyboardResponse(WOLFSSH* ssh)
1547915504 ret = PreparePacket(ssh, payloadSz);
1548015505 }
1548115506
15482- output = ssh->outputBuffer.buffer;
15483- idx = ssh->outputBuffer.length;
15484-
15485- output[idx++] = MSGID_USERAUTH_INFO_RESPONSE;
15507+ if (ret == WS_SUCCESS) {
15508+ output = ssh->outputBuffer.buffer;
15509+ idx = ssh->outputBuffer.length;
1548615510
15487- if (ret == WS_SUCCESS)
15511+ output[idx++] = MSGID_USERAUTH_INFO_RESPONSE;
1548815512 ret = BuildUserAuthResponseKeyboard(ssh, output, &idx, &authData);
15513+ }
1548915514
1549015515 if (ret == WS_SUCCESS) {
1549115516 ssh->outputBuffer.length = idx;
0 commit comments