Skip to content

Commit 3024d28

Browse files
authored
Merge pull request #695 from JacobBarthelmeh/541
Server side auth pending support
2 parents 2e0f509 + dc66602 commit 3024d28

5 files changed

Lines changed: 71 additions & 26 deletions

File tree

examples/echoserver/echoserver.c

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,7 +1400,8 @@ static int NonBlockSSH_accept(WOLFSSH* ssh)
14001400

14011401
while ((ret != WS_SUCCESS
14021402
&& ret != WS_SCP_COMPLETE && ret != WS_SFTP_COMPLETE)
1403-
&& (error == WS_WANT_READ || error == WS_WANT_WRITE)) {
1403+
&& (error == WS_WANT_READ || error == WS_WANT_WRITE ||
1404+
error == WS_AUTH_PENDING)) {
14041405

14051406
if (error == WS_WANT_READ)
14061407
printf("... server would read block\n");
@@ -1410,7 +1411,8 @@ static int NonBlockSSH_accept(WOLFSSH* ssh)
14101411
select_ret = tcp_select(sockfd, 1);
14111412
if (select_ret == WS_SELECT_RECV_READY ||
14121413
select_ret == WS_SELECT_ERROR_READY ||
1413-
error == WS_WANT_WRITE)
1414+
error == WS_WANT_WRITE ||
1415+
error == WS_AUTH_PENDING)
14141416
{
14151417
ret = wolfSSH_accept(ssh);
14161418
error = wolfSSH_get_error(ssh);
@@ -1432,11 +1434,16 @@ static THREAD_RETURN WOLFSSH_THREAD server_worker(void* vArgs)
14321434

14331435
passwdRetry = MAX_PASSWD_RETRY;
14341436

1435-
if (!threadCtx->nonBlock)
1437+
if (!threadCtx->nonBlock) {
14361438
ret = wolfSSH_accept(threadCtx->ssh);
1437-
else
1439+
if (wolfSSH_get_error(threadCtx->ssh) == WS_AUTH_PENDING) {
1440+
printf("Auth pending error, use -N for non blocking\n");
1441+
printf("Trying to close down the connection\n");
1442+
}
1443+
}
1444+
else {
14381445
ret = NonBlockSSH_accept(threadCtx->ssh);
1439-
1446+
}
14401447
#ifdef WOLFSSH_SCP
14411448
/* finish off SCP operation */
14421449
if (ret == WS_SCP_INIT) {
@@ -2055,6 +2062,7 @@ static int wsUserAuthResult(byte res,
20552062
}
20562063

20572064

2065+
static int userAuthWouldBlock = 0;
20582066
static int wsUserAuth(byte authType,
20592067
WS_UserAuthData* authData,
20602068
void* ctx)
@@ -2068,6 +2076,12 @@ static int wsUserAuth(byte authType,
20682076
return WOLFSSH_USERAUTH_FAILURE;
20692077
}
20702078

2079+
if (userAuthWouldBlock > 0) {
2080+
printf("User Auth would block ....\n");
2081+
userAuthWouldBlock--;
2082+
return WOLFSSH_USERAUTH_WOULD_BLOCK;
2083+
}
2084+
20712085
if (authType != WOLFSSH_USERAUTH_PASSWORD &&
20722086
#ifdef WOLFSSH_ALLOW_USERAUTH_NONE
20732087
authType != WOLFSSH_USERAUTH_NONE &&
@@ -2284,6 +2298,7 @@ static void ShowUsage(void)
22842298
printf(" -a <file> load in a root CA certificate file\n");
22852299
#endif
22862300
printf(" -k set the list of key algos to use\n");
2301+
printf(" -b <num> test user auth would block\n");
22872302
}
22882303

22892304

@@ -2345,7 +2360,7 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args)
23452360
serverArgs->return_code = EXIT_SUCCESS;
23462361

23472362
if (argc > 0) {
2348-
const char* optlist = "?1a:d:efEp:R:Ni:j:I:J:K:P:k:";
2363+
const char* optlist = "?1a:d:efEp:R:Ni:j:I:J:K:P:k:b:";
23492364
myoptind = 0;
23502365
while ((ch = mygetopt(argc, argv, optlist)) != -1) {
23512366
switch (ch) {
@@ -2429,6 +2444,10 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args)
24292444
passwdList = StrListAdd(passwdList, myoptarg);
24302445
break;
24312446

2447+
case 'b':
2448+
userAuthWouldBlock = atoi(myoptarg);
2449+
break;
2450+
24322451
default:
24332452
ShowUsage();
24342453
serverArgs->return_code = MY_EX_USAGE;

src/internal.c

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,9 @@ const char* GetErrorString(int err)
450450
case WS_ED25519_E:
451451
return "Ed25519 buffer error";
452452

453+
case WS_AUTH_PENDING:
454+
return "userauth is still pending (callback would block)";
455+
453456
default:
454457
return "Unknown error code";
455458
}
@@ -6111,6 +6114,10 @@ static int DoUserAuthRequestNone(WOLFSSH* ssh, WS_UserAuthData* authData,
61116114
ret = WS_USER_AUTH_E;
61126115
#endif
61136116
}
6117+
else if (ret == WOLFSSH_USERAUTH_WOULD_BLOCK) {
6118+
WLOG(WS_LOG_DEBUG, "DUARN: userauth callback would block");
6119+
ret = WS_AUTH_PENDING;
6120+
}
61146121
else {
61156122
WLOG(WS_LOG_DEBUG, "DUARN: none check failed, retry");
61166123
ret = SendUserAuthFailure(ssh, 0);
@@ -6196,6 +6203,10 @@ static int DoUserAuthRequestPassword(WOLFSSH* ssh, WS_UserAuthData* authData,
61966203
#endif
61976204
ret = WS_USER_AUTH_E;
61986205
}
6206+
else if (ret == WOLFSSH_USERAUTH_WOULD_BLOCK) {
6207+
WLOG(WS_LOG_DEBUG, "DUARPW: userauth callback would block");
6208+
ret = WS_AUTH_PENDING;
6209+
}
61996210
else {
62006211
WLOG(WS_LOG_DEBUG, "DUARPW: password check failed, retry");
62016212
authFailure = 1;
@@ -6214,7 +6225,7 @@ static int DoUserAuthRequestPassword(WOLFSSH* ssh, WS_UserAuthData* authData,
62146225
if (authFailure || partialSuccess) {
62156226
ret = SendUserAuthFailure(ssh, partialSuccess);
62166227
}
6217-
else {
6228+
else if (ret == WS_SUCCESS) {
62186229
ssh->clientState = CLIENT_USERAUTH_DONE;
62196230
}
62206231

@@ -7102,6 +7113,7 @@ static int DoUserAuthRequestPublicKey(WOLFSSH* ssh, WS_UserAuthData* authData,
71027113
ret = ssh->ctx->userAuthCb(WOLFSSH_USERAUTH_PUBLICKEY,
71037114
authData, ssh->userAuthCtx);
71047115
WLOG(WS_LOG_DEBUG, "DUARPK: callback result = %d", ret);
7116+
71057117
#ifdef DEBUG_WOLFSSH
71067118
switch (ret) {
71077119
case WOLFSSH_USERAUTH_SUCCESS:
@@ -7131,20 +7143,29 @@ static int DoUserAuthRequestPublicKey(WOLFSSH* ssh, WS_UserAuthData* authData,
71317143
case WOLFSSH_USERAUTH_PARTIAL_SUCCESS:
71327144
WLOG(WS_LOG_DEBUG, "DUARPK: user auth partial success");
71337145
break;
7146+
7147+
case WOLFSSH_USERAUTH_WOULD_BLOCK:
7148+
WLOG(WS_LOG_DEBUG, "DUARPK: userauth callback would block");
7149+
break;
71347150

71357151
default:
71367152
WLOG(WS_LOG_DEBUG,
71377153
"Unexpected return value from Auth callback");
71387154
}
71397155
#endif
71407156

7141-
if (ret == WOLFSSH_USERAUTH_PARTIAL_SUCCESS) {
7142-
partialSuccess = 1;
7157+
if (ret == WOLFSSH_USERAUTH_WOULD_BLOCK) {
7158+
ret = WS_AUTH_PENDING;
71437159
}
7144-
else if (ret != WOLFSSH_USERAUTH_SUCCESS) {
7145-
authFailure = 1;
7160+
else {
7161+
if (ret == WOLFSSH_USERAUTH_PARTIAL_SUCCESS) {
7162+
partialSuccess = 1;
7163+
}
7164+
else if (ret != WOLFSSH_USERAUTH_SUCCESS) {
7165+
authFailure = 1;
7166+
}
7167+
ret = WS_SUCCESS;
71467168
}
7147-
ret = WS_SUCCESS;
71487169
}
71497170
else {
71507171
WLOG(WS_LOG_DEBUG, "DUARPK: no userauth callback set");
@@ -8843,18 +8864,21 @@ static int DoPacket(WOLFSSH* ssh, byte* bufferConsumed)
88438864
ret = SendUnimplemented(ssh);
88448865
}
88458866

8846-
if (payloadSz > 0) {
8847-
idx += payloadIdx;
8848-
if (idx + padSz > len) {
8849-
WLOG(WS_LOG_DEBUG, "Not enough data in buffer for pad.");
8850-
ret = WS_BUFFER_E;
8867+
/* if the auth is still pending, don't discard the packet data */
8868+
if (ret != WS_AUTH_PENDING) {
8869+
if (payloadSz > 0) {
8870+
idx += payloadIdx;
8871+
if (idx + padSz > len) {
8872+
WLOG(WS_LOG_DEBUG, "Not enough data in buffer for pad.");
8873+
ret = WS_BUFFER_E;
8874+
}
88518875
}
8852-
}
88538876

8854-
idx += padSz;
8855-
ssh->inputBuffer.idx = idx;
8856-
ssh->peerSeq++;
8857-
*bufferConsumed = 1;
8877+
idx += padSz;
8878+
ssh->inputBuffer.idx = idx;
8879+
ssh->peerSeq++;
8880+
*bufferConsumed = 1;
8881+
}
88588882

88598883
return ret;
88608884
}

src/ssh.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ int wolfSSH_accept(WOLFSSH* ssh)
414414
return WS_BAD_ARGUMENT;
415415

416416
/* clear want read/writes for retry */
417-
if (ssh->error == WS_WANT_READ || ssh->error == WS_WANT_WRITE)
417+
if (ssh->error == WS_WANT_READ || ssh->error == WS_WANT_WRITE || ssh->error == WS_AUTH_PENDING)
418418
ssh->error = 0;
419419

420420
if (ssh->error != 0) {

wolfssh/error.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,9 @@ enum WS_ErrorCodes {
134134
WS_SFTP_NOT_FILE_E = -1093, /* Not a regular file */
135135
WS_MSGID_NOT_ALLOWED_E = -1094, /* Message not allowed before userauth */
136136
WS_ED25519_E = -1095, /* Ed25519 failure */
137-
138-
WS_LAST_E = -1095 /* Update this to indicate last error */
137+
WS_AUTH_PENDING = -1096, /* User authentication still pending */
138+
139+
WS_LAST_E = -1096 /* Update this to indicate last error */
139140
};
140141

141142

wolfssh/ssh.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,8 @@ enum WS_UserAuthResults
400400
WOLFSSH_USERAUTH_INVALID_PASSWORD,
401401
WOLFSSH_USERAUTH_REJECTED,
402402
WOLFSSH_USERAUTH_INVALID_PUBLICKEY,
403-
WOLFSSH_USERAUTH_PARTIAL_SUCCESS
403+
WOLFSSH_USERAUTH_PARTIAL_SUCCESS,
404+
WOLFSSH_USERAUTH_WOULD_BLOCK
404405
};
405406

406407
enum WS_DisconnectReasonCodes {

0 commit comments

Comments
 (0)