Skip to content

Commit d9f8480

Browse files
adjustment to client side keyboard auth behavior and auth test case
1 parent 2088e28 commit d9f8480

3 files changed

Lines changed: 31 additions & 15 deletions

File tree

src/internal.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7878,7 +7878,10 @@ static int DoUserAuthFailure(WOLFSSH* ssh,
78787878
break;
78797879
#ifdef WOLFSSH_KEYBOARD_INTERACTIVE
78807880
case ID_USERAUTH_KEYBOARD:
7881-
authType |= WOLFSSH_USERAUTH_KEYBOARD;
7881+
/* try a different auth method if failing */
7882+
if (ssh->kbAuthAttempts < 3) {
7883+
authType |= WOLFSSH_USERAUTH_KEYBOARD;
7884+
}
78827885
break;
78837886
#endif
78847887
#if !defined(WOLFSSH_NO_RSA) || !defined(WOLFSSH_NO_ECDSA)
@@ -13382,6 +13385,11 @@ int SendUserAuthKeyboardRequest(WOLFSSH* ssh, WS_UserAuthData* authData)
1338213385
if (ret == WOLFSSH_USERAUTH_SUCCESS) {
1338313386
ret = WS_SUCCESS;
1338413387
}
13388+
else {
13389+
WLOG(WS_LOG_DEBUG, "Issue with keyboard auth setup, try another "
13390+
"auth type");
13391+
return SendUserAuthFailure(ssh, 0);
13392+
}
1338513393
}
1338613394

1338713395
if (authData->sf.keyboard.promptCount > 0 &&
@@ -13407,10 +13415,12 @@ int SendUserAuthKeyboardRequest(WOLFSSH* ssh, WS_UserAuthData* authData)
1340713415
ret = PreparePacket(ssh, payloadSz);
1340813416
}
1340913417

13410-
output = ssh->outputBuffer.buffer;
13411-
idx = ssh->outputBuffer.length;
13418+
if (ret == WS_SUCCESS) {
13419+
output = ssh->outputBuffer.buffer;
13420+
idx = ssh->outputBuffer.length;
1341213421

13413-
output[idx++] = MSGID_USERAUTH_INFO_REQUEST;
13422+
output[idx++] = MSGID_USERAUTH_INFO_REQUEST;
13423+
}
1341413424

1341513425
if (ret == WS_SUCCESS) {
1341613426
ret = BuildUserAuthRequestKeyboard(ssh, output, &idx, authData);
@@ -15075,6 +15085,7 @@ int SendUserAuthRequest(WOLFSSH* ssh, byte authType, int addSig)
1507515085
/* submethods */
1507615086
c32toa(0, output + idx);
1507715087
idx += LENGTH_SZ;
15088+
ssh->kbAuthAttempts++;
1507815089
}
1507915090
#endif
1508015091
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)