Skip to content

Commit 1bee0c1

Browse files
adjustment to client side keyboard auth behavior and auth test case
1 parent 88d4eae commit 1bee0c1

3 files changed

Lines changed: 26 additions & 12 deletions

File tree

src/internal.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7881,7 +7881,10 @@ static int DoUserAuthFailure(WOLFSSH* ssh,
78817881
break;
78827882
#ifdef WOLFSSH_KEYBOARD_INTERACTIVE
78837883
case ID_USERAUTH_KEYBOARD:
7884-
authType |= WOLFSSH_USERAUTH_KEYBOARD;
7884+
/* try a different auth method if failing */
7885+
if (ssh->kbAuthAttempts < 3) {
7886+
authType |= WOLFSSH_USERAUTH_KEYBOARD;
7887+
}
78857888
break;
78867889
#endif
78877890
#if !defined(WOLFSSH_NO_RSA) || !defined(WOLFSSH_NO_ECDSA)
@@ -13389,6 +13392,11 @@ int SendUserAuthKeyboardRequest(WOLFSSH* ssh, WS_UserAuthData* authData)
1338913392
if (ret == WOLFSSH_USERAUTH_SUCCESS) {
1339013393
ret = WS_SUCCESS;
1339113394
}
13395+
else {
13396+
WLOG(WS_LOG_DEBUG, "Issue with keyboard auth setup, try another "
13397+
"auth type");
13398+
return SendUserAuthFailure(ssh, 0);
13399+
}
1339213400
}
1339313401

1339413402
if (ret == WS_SUCCESS) {
@@ -15077,6 +15085,7 @@ int SendUserAuthRequest(WOLFSSH* ssh, byte authType, int addSig)
1507715085
/* submethods */
1507815086
c32toa(0, output + idx);
1507915087
idx += LENGTH_SZ;
15088+
ssh->kbAuthAttempts++;
1508015089
}
1508115090
#endif
1508215091
else if (authId == ID_USERAUTH_PUBLICKEY)

tests/auth.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,23 @@ static int load_key(byte isEcc, byte* buf, word32 bufSz)
222222

223223
static int serverUserAuth(byte authType, WS_UserAuthData* authData, void* ctx)
224224
{
225-
(void) ctx;
226-
if (authType != WOLFSSH_USERAUTH_KEYBOARD) {
225+
WS_UserAuthData_Keyboard* prompts = (WS_UserAuthData_Keyboard*)ctx;
226+
227+
if (ctx == NULL) {
227228
return WOLFSSH_USERAUTH_FAILURE;
228229
}
229230

231+
if (authType != WOLFSSH_USERAUTH_KEYBOARD &&
232+
authType != WOLFSSH_USERAUTH_KEYBOARD_SETUP) {
233+
return WOLFSSH_USERAUTH_FAILURE;
234+
}
235+
236+
if (authType == WOLFSSH_USERAUTH_KEYBOARD_SETUP) {
237+
WMEMCPY(&authData->sf.keyboard, prompts,
238+
sizeof(WS_UserAuthData_Keyboard));
239+
return WS_SUCCESS;
240+
}
241+
230242
if (authData->sf.keyboard.responseCount != kbResponseCount) {
231243
return WOLFSSH_USERAUTH_FAILURE;
232244
}
@@ -251,14 +263,6 @@ static int serverUserAuth(byte authType, WS_UserAuthData* authData, void* ctx)
251263
return WOLFSSH_USERAUTH_SUCCESS;
252264
}
253265

254-
static int serverKeyboardCallback(WS_UserAuthData_Keyboard *kbAuth, void *ctx)
255-
{
256-
(void) ctx;
257-
WMEMCPY(kbAuth, &promptData, sizeof(WS_UserAuthData_Keyboard));
258-
259-
return WS_SUCCESS;
260-
}
261-
262266
static INLINE void SignalTcpReady(tcp_ready* ready, word16 port)
263267
{
264268
pthread_mutex_lock(&ready->mutex);
@@ -332,13 +336,13 @@ static THREAD_RETURN WOLFSSH_THREAD server_thread(void* args)
332336
}
333337

334338
wolfSSH_SetUserAuth(ctx, serverUserAuth);
335-
wolfSSH_SetKeyboardAuthPrompts(ctx, serverKeyboardCallback);
336339
ssh = wolfSSH_new(ctx);
337340
if (ssh == NULL) {
338341
ES_ERROR("Couldn't allocate SSH data.\n");
339342
}
340343
keyLoadBuf = buf;
341344
bufSz = EXAMPLE_KEYLOAD_BUFFER_SZ;
345+
wolfSSH_SetUserAuthCtx(ssh, &promptData);
342346

343347
bufSz = load_key(peerEcc, keyLoadBuf, bufSz);
344348
if (bufSz == 0) {

wolfssh/internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,7 @@ struct WOLFSSH {
919919
void* keyingCompletionCtx;
920920
#ifdef WOLFSSH_KEYBOARD_INTERACTIVE
921921
WS_UserAuthData_Keyboard kbAuth;
922+
byte kbAuthAttempts;
922923
#endif
923924
};
924925

0 commit comments

Comments
 (0)