Skip to content

Commit 476200c

Browse files
committed
fix: always send USERAUTH_FAILURE on reject
RFC 4252 section 5.1 has the server answer a request it does not accept with USERAUTH_FAILURE. A callback returning WOLFSSH_USERAUTH_REJECTED now always gets that reply, and always ends the session. NO_FAILURE_ON_REJECTED suppressed the reply. The macro was never set by configure, named in a header, or documented, and it guarded all four method handlers alike. - drop the macro and its four guards - end the session on a rejected keyboard-interactive setup, the one rejection that used to leave the peer retrying to the cap - state the guarantee on WOLFSSH_USERAUTH_REJECTED in wolfssh/ssh.h - add test_UserAuthRejectedSendsFailure() over the dispatched methods, asserting on the wire since the handlers return WS_USER_AUTH_E anyway Issue: F-11672
1 parent ad059d7 commit 476200c

3 files changed

Lines changed: 198 additions & 17 deletions

File tree

src/internal.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8524,11 +8524,9 @@ static int DoUserAuthRequestNone(WOLFSSH* ssh, WS_UserAuthData* authData,
85248524
}
85258525
else if (ret == WOLFSSH_USERAUTH_REJECTED) {
85268526
WLOG(WS_LOG_DEBUG, "DUARN: none rejected");
8527-
#ifndef NO_FAILURE_ON_REJECTED
85288527
/* Count before failing: a send that blocks returns
85298528
* WS_WANT_WRITE, which isn't fatal on its own. */
85308529
(void)SendUserAuthFailureCount(ssh, 0, countIt);
8531-
#endif
85328530
ret = WS_USER_AUTH_E;
85338531
}
85348532
else if (ret == WOLFSSH_USERAUTH_WOULD_BLOCK) {
@@ -8657,9 +8655,7 @@ static int DoUserAuthInfoResponse(WOLFSSH* ssh,
86578655
}
86588656
else if (ret == WOLFSSH_USERAUTH_REJECTED) {
86598657
WLOG(WS_LOG_DEBUG, "DUARKB: keyboard rejected");
8660-
#ifndef NO_FAILURE_ON_REJECTED
8661-
authFailure = 1;
8662-
#endif
8658+
authFailure = 1;
86638659
authRejected = 1;
86648660
ret = WS_USER_AUTH_E;
86658661
}
@@ -8772,9 +8768,7 @@ static int DoUserAuthRequestPassword(WOLFSSH* ssh, WS_UserAuthData* authData,
87728768
}
87738769
else if (ret == WOLFSSH_USERAUTH_REJECTED) {
87748770
WLOG(WS_LOG_DEBUG, "DUARPW: password rejected");
8775-
#ifndef NO_FAILURE_ON_REJECTED
8776-
authFailure = 1;
8777-
#endif
8771+
authFailure = 1;
87788772
authRejected = 1;
87798773
ret = WS_USER_AUTH_E;
87808774
}
@@ -10204,9 +10198,7 @@ static int DoUserAuthRequestPublicKey(WOLFSSH* ssh, WS_UserAuthData* authData,
1020410198
ret = WS_AUTH_PENDING;
1020510199
}
1020610200
else if (ret == WOLFSSH_USERAUTH_REJECTED) {
10207-
#ifndef NO_FAILURE_ON_REJECTED
10208-
authFailure = 1;
10209-
#endif
10201+
authFailure = 1;
1021010202
authRejected = 1;
1021110203
ret = WS_USER_AUTH_E;
1021210204
}
@@ -17459,6 +17451,15 @@ int SendUserAuthKeyboardRequest(WOLFSSH* ssh, WS_UserAuthData* authData)
1745917451
ssh->kbSetupPending = 0;
1746017452
return WS_AUTH_PENDING;
1746117453
}
17454+
else if (ret == WOLFSSH_USERAUTH_REJECTED) {
17455+
/* A hard rejection ends the session here as it does in the other
17456+
* methods. Count before failing: a send that blocks returns
17457+
* WS_WANT_WRITE, which isn't fatal on its own. */
17458+
WLOG(WS_LOG_DEBUG, "SUAKR: keyboard setup rejected");
17459+
ssh->kbSetupPending = 0;
17460+
(void)SendUserAuthFailureCount(ssh, 0, 1);
17461+
return WS_USER_AUTH_E;
17462+
}
1746217463
else {
1746317464
WLOG(WS_LOG_DEBUG, "Issue with keyboard auth setup, try another "
1746417465
"auth type");

tests/unit.c

Lines changed: 183 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7673,13 +7673,17 @@ static int UnitAuthKbSetupOk(byte authType, WS_UserAuthData* authData,
76737673
}
76747674
return WOLFSSH_USERAUTH_INVALID_PASSWORD;
76757675
}
7676+
#endif /* WOLFSSH_KEYBOARD_INTERACTIVE */
76767677

7678+
#if defined(WOLFSSH_KEYBOARD_INTERACTIVE) || \
7679+
defined(WOLFSSH_ALLOW_USERAUTH_NONE) || !defined(WOLFSSH_NO_PUBKEY_AUTH)
76777680
/* Build a USERAUTH_REQUEST payload for `service` and auth method `method`
76787681
* with no method-specific fields, as DoUserAuthRequest() sees it with the
76797682
* message id already consumed. The keyboard and none dispatch paths don't
7680-
* parse trailing fields, so three strings (user, service, method) suffice. A
7681-
* NULL method omits that field, which a bad service name never reaches.
7682-
* Returns the payload size, 0 on overflow. */
7683+
* parse trailing fields, so three strings (user, service, method) suffice,
7684+
* and the publickey path appends its own fields to this. A NULL method omits
7685+
* that field, which a bad service name never reaches. Returns the payload
7686+
* size, 0 on overflow. */
76837687
static word32 BuildAuthMethodRequest(byte* buf, word32 bufSz,
76847688
const char* service, const char* method)
76857689
{
@@ -7703,7 +7707,54 @@ static word32 BuildAuthMethodRequest(byte* buf, word32 bufSz,
77037707
}
77047708
return idx;
77057709
}
7706-
#endif /* WOLFSSH_KEYBOARD_INTERACTIVE */
7710+
7711+
#ifndef WOLFSSH_NO_PUBKEY_AUTH
7712+
/* A public key algorithm the server offers, so the request gets past the
7713+
* algorithm match and reaches the userauth callback. */
7714+
#ifndef WOLFSSH_NO_ECDSA_SHA2_NISTP256
7715+
#define UNIT_AUTH_PK_ALGO "ecdsa-sha2-nistp256"
7716+
#elif !defined(WOLFSSH_NO_RSA_SHA2_256)
7717+
#define UNIT_AUTH_PK_ALGO "rsa-sha2-256"
7718+
#elif !defined(WOLFSSH_NO_ED25519)
7719+
#define UNIT_AUTH_PK_ALGO "ssh-ed25519"
7720+
#endif
7721+
7722+
#ifdef UNIT_AUTH_PK_ALGO
7723+
/* Build the no-signature "publickey" USERAUTH_REQUEST, the PK_OK probe. The
7724+
* handler reads the key blob only as far as its format string before calling
7725+
* the userauth callback, so the algorithm name stands in for the key. Returns
7726+
* the payload size, 0 on overflow. */
7727+
static word32 BuildAuthPkRequest(byte* buf, word32 bufSz)
7728+
{
7729+
word32 idx, algoSz = (word32)WSTRLEN(UNIT_AUTH_PK_ALGO);
7730+
7731+
idx = BuildAuthMethodRequest(buf, bufSz, "ssh-connection", "publickey");
7732+
if (idx == 0)
7733+
return 0;
7734+
if (idx + BOOLEAN_SZ + 3 * UINT32_SZ + 2 * algoSz > bufSz)
7735+
return 0;
7736+
7737+
buf[idx++] = 0; /* has signature FALSE */
7738+
7739+
PutU32BE(buf + idx, algoSz);
7740+
idx += UINT32_SZ;
7741+
WMEMCPY(buf + idx, UNIT_AUTH_PK_ALGO, algoSz);
7742+
idx += algoSz;
7743+
7744+
/* Key blob, itself a string holding the key format. */
7745+
PutU32BE(buf + idx, UINT32_SZ + algoSz);
7746+
idx += UINT32_SZ;
7747+
PutU32BE(buf + idx, algoSz);
7748+
idx += UINT32_SZ;
7749+
WMEMCPY(buf + idx, UNIT_AUTH_PK_ALGO, algoSz);
7750+
idx += algoSz;
7751+
7752+
return idx;
7753+
}
7754+
#endif /* UNIT_AUTH_PK_ALGO */
7755+
#endif /* !WOLFSSH_NO_PUBKEY_AUTH */
7756+
#endif /* WOLFSSH_KEYBOARD_INTERACTIVE || WOLFSSH_ALLOW_USERAUTH_NONE
7757+
* || !WOLFSSH_NO_PUBKEY_AUTH */
77077758

77087759
/* Build a "password" USERAUTH_REQUEST payload, as DoUserAuthRequest() sees it
77097760
* with the message id already consumed. Returns the payload size. */
@@ -7969,6 +8020,129 @@ static int CaptureIoSendAuthSvc(WOLFSSH* ssh, void* buf, word32 sz, void* ctx)
79698020
return (int)sz;
79708021
}
79718022

8023+
/* Userauth callback that rejects outright. */
8024+
static int UnitAuthAlwaysReject(byte authType, WS_UserAuthData* authData,
8025+
void* ctx)
8026+
{
8027+
(void)authType;
8028+
(void)authData;
8029+
(void)ctx;
8030+
return WOLFSSH_USERAUTH_REJECTED;
8031+
}
8032+
8033+
8034+
/* Drive one rejected USERAUTH_REQUEST on a fresh session and check that a
8035+
* USERAUTH_FAILURE went out. Returns 0, or 1..3 naming what failed. */
8036+
static int RunRejectedRequest(const byte* request, word32 requestSz,
8037+
const char* method)
8038+
{
8039+
WOLFSSH_CTX* ctx = NULL;
8040+
WOLFSSH* ssh = NULL;
8041+
word32 idx = 0;
8042+
int ret;
8043+
int result = 0;
8044+
8045+
ctx = wolfSSH_CTX_new(WOLFSSH_ENDPOINT_SERVER, NULL);
8046+
if (ctx == NULL)
8047+
return 1;
8048+
wolfSSH_SetUserAuth(ctx, UnitAuthAlwaysReject);
8049+
wolfSSH_SetIOSend(ctx, CaptureIoSendAuthSvc);
8050+
8051+
ssh = wolfSSH_new(ctx);
8052+
if (ssh == NULL) {
8053+
wolfSSH_CTX_free(ctx);
8054+
return 1;
8055+
}
8056+
8057+
s_authSvcCaptureSz = 0;
8058+
s_authSvcSendCount = 0;
8059+
WMEMSET(s_authSvcCapture, 0, sizeof(s_authSvcCapture));
8060+
8061+
ret = wolfSSH_TestDoUserAuthRequest(ssh, (byte*)request, requestSz, &idx);
8062+
8063+
if (ret != WS_USER_AUTH_E) {
8064+
printf("UserAuthRejectedSendsFailure: %s ret=%d expected %d\n",
8065+
method, ret, WS_USER_AUTH_E);
8066+
result = 1;
8067+
}
8068+
else if (s_authSvcSendCount == 0) {
8069+
printf("UserAuthRejectedSendsFailure: %s sent nothing on rejection\n",
8070+
method);
8071+
result = 2;
8072+
}
8073+
else if (CaptureMsgId(s_authSvcCapture, s_authSvcCaptureSz)
8074+
!= MSGID_USERAUTH_FAILURE) {
8075+
printf("UserAuthRejectedSendsFailure: %s msgId=%d expected"
8076+
" USERAUTH_FAILURE\n", method,
8077+
CaptureMsgId(s_authSvcCapture, s_authSvcCaptureSz));
8078+
result = 3;
8079+
}
8080+
8081+
wolfSSH_free(ssh);
8082+
wolfSSH_CTX_free(ctx);
8083+
8084+
return result;
8085+
}
8086+
8087+
8088+
/* RFC 4252 section 5.1: a rejected request is answered with
8089+
* USERAUTH_FAILURE. The handler returns WS_USER_AUTH_E either way, so the
8090+
* assertion is on what went out on the wire. Every method the build dispatches
8091+
* has its own reject branch, so each is driven here.
8092+
*
8093+
* DoUserAuthInfoResponse() is the one reject branch left uncovered; reaching
8094+
* it needs an INFO_RESPONSE shim. The keyboard case here is the setup
8095+
* callback, which answers the request before any prompt goes out. The
8096+
* publickey case needs one of the offered key algorithms compiled in. */
8097+
static int test_UserAuthRejectedSendsFailure(void)
8098+
{
8099+
byte request[128];
8100+
word32 requestSz;
8101+
int ret;
8102+
8103+
requestSz = BuildAuthPwRequest(request, (word32)sizeof(request));
8104+
if (requestSz == 0)
8105+
return -790;
8106+
ret = RunRejectedRequest(request, requestSz, "password");
8107+
if (ret != 0)
8108+
return -790 - ret;
8109+
8110+
#ifdef WOLFSSH_KEYBOARD_INTERACTIVE
8111+
requestSz = BuildAuthMethodRequest(request, (word32)sizeof(request),
8112+
"ssh-connection", "keyboard-interactive");
8113+
if (requestSz == 0)
8114+
return -800;
8115+
ret = RunRejectedRequest(request, requestSz, "keyboard-interactive");
8116+
if (ret != 0)
8117+
return -800 - ret;
8118+
#endif
8119+
8120+
#ifdef WOLFSSH_ALLOW_USERAUTH_NONE
8121+
requestSz = BuildAuthMethodRequest(request, (word32)sizeof(request),
8122+
"ssh-connection", "none");
8123+
if (requestSz == 0)
8124+
return -795;
8125+
/* The opening probe is exempt from the failure count, not from the
8126+
* reply. */
8127+
ret = RunRejectedRequest(request, requestSz, "none");
8128+
if (ret != 0)
8129+
return -795 - ret;
8130+
#endif
8131+
8132+
#ifdef UNIT_AUTH_PK_ALGO
8133+
requestSz = BuildAuthPkRequest(request, (word32)sizeof(request));
8134+
if (requestSz == 0)
8135+
return -805;
8136+
/* No signature, so the callback answers the PK_OK probe. */
8137+
ret = RunRejectedRequest(request, requestSz, "publickey");
8138+
if (ret != 0)
8139+
return -805 - ret;
8140+
#endif
8141+
8142+
return 0;
8143+
}
8144+
8145+
79728146
/* Verify DoUserAuthRequest rejects non-"ssh-connection" service names per
79738147
* RFC 4252 Section 5. For each case we assert:
79748148
* 1. ret == WS_SUCCESS (connection stays open for retry)
@@ -17840,6 +18014,11 @@ int wolfSSH_UnitTest(int argc, char** argv)
1784018014
(unitResult == 0 ? "SUCCESS" : "FAILED"));
1784118015
testResult = testResult || unitResult;
1784218016

18017+
unitResult = test_UserAuthRejectedSendsFailure();
18018+
printf("UserAuthRejectedSendsFailure: %s\n",
18019+
(unitResult == 0 ? "SUCCESS" : "FAILED"));
18020+
testResult = testResult || unitResult;
18021+
1784318022
#if !defined(WOLFSSH_NO_ECDSA_SHA2_NISTP256) && \
1784418023
!defined(WOLFSSH_NO_ECDSA_SHA2_NISTP384)
1784518024
unitResult = test_EccUserAuthCurveMismatch();

wolfssh/ssh.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,8 +466,9 @@ typedef struct WS_UserAuthData {
466466
* a multi-method authentication passed, WOLFSSH_USERAUTH_SUCCESS_ANOTHER
467467
* reports that a keyboard-interactive round passed and asks for the next
468468
* round, WOLFSSH_USERAUTH_WOULD_BLOCK asks for the request to be
469-
* retried, and WOLFSSH_USERAUTH_REJECTED is a hard rejection that ends
470-
* the session; any other value is treated as an ordinary failure.
469+
* retried, and WOLFSSH_USERAUTH_REJECTED is a hard rejection: the server
470+
* answers with USERAUTH_FAILURE, then ends the session. Any other value
471+
* is treated as an ordinary failure.
471472
*
472473
* WARNING: WOLFSSH_USERAUTH_SUCCESS has the value 0, the same as
473474
* WS_SUCCESS and the C "no error" idiom. A bare "return 0;", a forwarded

0 commit comments

Comments
 (0)