Skip to content

Commit

Permalink
Asynchronous user authentication.
Browse files Browse the repository at this point in the history
  • Loading branch information
falemagn committed Jul 21, 2023
1 parent 87caf4a commit 0ca42de
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
36 changes: 27 additions & 9 deletions src/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,9 @@ const char* GetErrorString(int err)

case WS_MATCH_UA_KEY_ID_E:
return "unable to match user auth key type";

case WS_AUTH_PENDING:
return "userauth is still pending (callback would block)";

default:
return "Unknown error code";
Expand Down Expand Up @@ -4947,6 +4950,10 @@ static int DoUserAuthRequestNone(WOLFSSH* ssh, WS_UserAuthData* authData,
ret = WS_USER_AUTH_E;
#endif
}
else if (ret == WOLFSSH_USERAUTH_WOULD_BLOCK) {
WLOG(WS_LOG_DEBUG, "DUARN: userauth callback would block");
ret = WS_AUTH_PENDING;
}
else {
WLOG(WS_LOG_DEBUG, "DUARN: none check failed, retry");
ret = SendUserAuthFailure(ssh, 0);
Expand Down Expand Up @@ -5029,6 +5036,10 @@ static int DoUserAuthRequestPassword(WOLFSSH* ssh, WS_UserAuthData* authData,
ret = WS_USER_AUTH_E;
#endif
}
else if (ret == WOLFSSH_USERAUTH_WOULD_BLOCK) {
WLOG(WS_LOG_DEBUG, "DUARPW: userauth callback would block");
ret = WS_AUTH_PENDING;
}
else {
WLOG(WS_LOG_DEBUG, "DUARPW: password check failed, retry");
ret = SendUserAuthFailure(ssh, 0);
Expand Down Expand Up @@ -5842,6 +5853,10 @@ static int DoUserAuthRequestPublicKey(WOLFSSH* ssh, WS_UserAuthData* authData,
authFailure = 1;
ret = WS_SUCCESS;
}
else if (ret == WOLFSSH_USERAUTH_WOULD_BLOCK) {
WLOG(WS_LOG_DEBUG, "DUARPK: userauth callback would block");
ret = WS_AUTH_PENDING;
}
else {
authFailure = 1;
}
Expand Down Expand Up @@ -7178,17 +7193,20 @@ static int DoPacket(WOLFSSH* ssh)
ret = SendUnimplemented(ssh);
}

if (payloadSz > 0) {
idx += payloadIdx;
if (idx + padSz > len) {
WLOG(WS_LOG_DEBUG, "Not enough data in buffer for pad.");
ret = WS_BUFFER_E;
/* if the auth is still pending, don't discard the packet data */
if (ret != WS_AUTH_PENDING) {
if (payloadSz > 0) {
idx += payloadIdx;
if (idx + padSz > len) {
WLOG(WS_LOG_DEBUG, "Not enough data in buffer for pad.");
ret = WS_BUFFER_E;
}
}
}

idx += padSz;
ssh->inputBuffer.idx = idx;
ssh->peerSeq++;
idx += padSz;
ssh->inputBuffer.idx = idx;
ssh->peerSeq++;
}

return ret;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ssh.c
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ int wolfSSH_accept(WOLFSSH* ssh)
return WS_BAD_ARGUMENT;

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

if (ssh->error != 0) {
Expand Down
3 changes: 2 additions & 1 deletion wolfssh/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,9 @@ enum WS_ErrorCodes {
WS_CERT_KEY_SIZE_E = -1087, /* Key size error */
WS_CTX_KEY_COUNT_E = -1088, /* Adding too many private keys */
WS_MATCH_UA_KEY_ID_E = -1089, /* Match user auth key key fail */
WS_AUTH_PENDING = -1090, /* User authentication still pending */

WS_LAST_E = -1089 /* Update this to indicate last error */
WS_LAST_E = -1090 /* Update this to indicate last error */
};


Expand Down
3 changes: 2 additions & 1 deletion wolfssh/ssh.h
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,8 @@ enum WS_UserAuthResults
WOLFSSH_USERAUTH_INVALID_USER,
WOLFSSH_USERAUTH_INVALID_PASSWORD,
WOLFSSH_USERAUTH_REJECTED,
WOLFSSH_USERAUTH_INVALID_PUBLICKEY
WOLFSSH_USERAUTH_INVALID_PUBLICKEY,
WOLFSSH_USERAUTH_WOULD_BLOCK
};

enum WS_DisconnectReasonCodes {
Expand Down

0 comments on commit 0ca42de

Please sign in to comment.