@@ -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. */
76837687static 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();
0 commit comments