@@ -3359,59 +3359,69 @@ static int ReceiveData(WOLFSSH* ssh, byte* buf, word32 sz)
33593359}
33603360
33613361
3362- static int GetInputText(WOLFSSH* ssh, byte** pEol)
3362+ /* Reads one CRLF- or LF-terminated line into ssh->inputBuffer. On success,
3363+ * *pEol points to the start of the terminator ('\r' for CRLF, '\n' for LF
3364+ * only) so the caller can compute line length and terminator size.
3365+ *
3366+ * Per RFC 4253 Section 4.2, the SSH version line is at most 255 octets
3367+ * including the terminator. The same cap is applied to pre-version banner
3368+ * lines: a line longer than that returns WS_VERSION_E. */
3369+ static int GetInputLine(WOLFSSH* ssh, byte** pEol)
33633370{
3364- int gotLine = 0;
3365- int inSz = 255;
3371+ int inSz;
33663372 int in;
3367- char *eol = NULL;
3368-
3369- if (GrowBuffer(&ssh->inputBuffer, inSz) < 0)
3373+ char* lf;
3374+ byte* buffer;
3375+
3376+ /* Compact so any unconsumed data (e.g., the remainder of a prior
3377+ * banner-line read) starts at buffer[0]. GrowBuffer(0) shifts the
3378+ * data when length > 0, and is preferred over ShrinkBuffer here
3379+ * because ShrinkBuffer skips the shift when data exceeds
3380+ * STATIC_BUFFER_LEN. */
3381+ if (GrowBuffer(&ssh->inputBuffer, 0) < 0)
33703382 return WS_MEMORY_E;
33713383
33723384 do {
3373- in = ReceiveData(ssh,
3374- ssh->inputBuffer.buffer + ssh->inputBuffer.length, inSz);
3375-
3376- if (in == -1) {
3377- return WS_SOCKET_ERROR_E;
3378- }
3379-
3380- if (in == WS_WANT_READ) {
3381- return WS_WANT_READ;
3382- }
3383-
3384- if (in > inSz) {
3385- return WS_RECV_OVERFLOW_E;
3386- }
3387-
3388- ssh->inputBuffer.length += in;
3389- inSz -= in;
3390-
3391- eol = WSTRNSTR((const char*)ssh->inputBuffer.buffer, "\r\n",
3392- ssh->inputBuffer.length);
3393-
3394- /* section 4.2 in RFC 4253 states that can be lenient on the CR for
3395- * interop with older or undocumented versions of SSH */
3396- if (!eol) {
3397- WLOG(WS_LOG_DEBUG, "Checking for old version of protocol exchange");
3398- eol = WSTRNSTR((const char*)ssh->inputBuffer.buffer, "\n",
3399- ssh->inputBuffer.length);
3385+ buffer = ssh->inputBuffer.buffer;
3386+ lf = WSTRNSTR((const char*)buffer, "\n", ssh->inputBuffer.length);
3387+ if (lf != NULL) {
3388+ /* eol points to the start of the terminator: back up onto a
3389+ * preceding CR if present, otherwise leave it on the LF. Be
3390+ * lenient on the CR per RFC 4253 Section 4.2. */
3391+ if ((byte*)lf > buffer && lf[-1] == '\r')
3392+ lf--;
3393+ if (pEol)
3394+ *pEol = (byte*)lf;
3395+ return WS_SUCCESS;
34003396 }
34013397
3402- if (eol)
3403- gotLine = 1;
3398+ /* 255-byte per-line cap: includes the terminator. If the buffer is
3399+ * already at the cap with no LF, the line is too long. */
3400+ if (ssh->inputBuffer.length >= 255)
3401+ return WS_VERSION_E;
34043402
3405- } while (!gotLine && inSz);
3403+ if (GrowBuffer(&ssh->inputBuffer,
3404+ 255 - ssh->inputBuffer.length) < 0)
3405+ return WS_MEMORY_E;
34063406
3407- if (pEol)
3408- *pEol = (byte*)eol;
3407+ inSz = 255 - (int)ssh->inputBuffer.length;
3408+ in = ReceiveData(ssh,
3409+ ssh->inputBuffer.buffer + ssh->inputBuffer.length,
3410+ (word32)inSz);
34093411
3410- if (!gotLine) {
3411- return WS_VERSION_E;
3412- }
3412+ if (in == WS_WANT_READ)
3413+ return WS_WANT_READ;
3414+ /* Any other non-positive return is treated as a connection failure:
3415+ * ReceiveData maps WS_CBIO_ERR_CONN_CLOSE to -1, and a clean EOF
3416+ * (in == 0) mid-version-exchange means the peer hung up before
3417+ * sending a complete identification line. */
3418+ if (in <= 0)
3419+ return WS_SOCKET_ERROR_E;
3420+ if (in > inSz)
3421+ return WS_RECV_OVERFLOW_E;
34133422
3414- return WS_SUCCESS;
3423+ ssh->inputBuffer.length += (word32)in;
3424+ } while (1);
34153425}
34163426
34173427
@@ -10722,20 +10732,63 @@ int DoProtoId(WOLFSSH* ssh)
1072210732 int ret;
1072310733 word32 idSz;
1072410734 byte* eol;
10725- byte SSH_PROTO_EOL_SZ = 1;
10735+ byte eolSz;
10736+ int allowBanner;
1072610737
10727- if ( (ret = GetInputText(ssh, &eol)) < 0) {
10728- WLOG(WS_LOG_DEBUG, "get input text failed");
10729- return ret;
10730- }
10738+ /*
10739+ * RFC 4253 Section 4.2: The server MAY send other lines of data before
10740+ * the version string. The client MUST send the version string first
10741+ * with no preceding data. So pre-version banner lines are accepted
10742+ * only when this endpoint is the client reading the server's
10743+ * identification. A wolfSSH server reading a client's identification
10744+ * rejects any non-"SSH-" line. The 255-byte per-line cap is enforced
10745+ * by GetInputLine, which also returns WS_SOCKET_ERROR_E if the peer
10746+ * stops sending before a complete line arrives. The number of banner
10747+ * lines is also bounded by WOLFSSH_MAX_BANNER_LINES so a peer cannot
10748+ * stall the connection by dribbling well-formed banner lines forever
10749+ * even when the embedder has not configured a connect timeout.
10750+ */
10751+ allowBanner = (ssh->ctx->side == WOLFSSH_ENDPOINT_CLIENT);
1073110752
10732- if (eol == NULL) {
10733- WLOG(WS_LOG_DEBUG, "invalid EOL");
10734- return WS_VERSION_E;
10735- }
10753+ do {
10754+ ret = GetInputLine(ssh, &eol);
10755+ if (ret < 0) {
10756+ WLOG(WS_LOG_DEBUG, "get input line failed");
10757+ return ret;
10758+ }
1073610759
10737- if (WSTRNCASECMP((char*)ssh->inputBuffer.buffer,
10738- ssh->ctx->sshProtoIdStr, SSH_PROTO_SZ) == 0) {
10760+ if (ssh->inputBuffer.length >= 4
10761+ && WSTRNCASECMP((char*)ssh->inputBuffer.buffer, "SSH-", 4) == 0)
10762+ break;
10763+
10764+ if (!allowBanner) {
10765+ WLOG(WS_LOG_DEBUG, "non-SSH line from peer");
10766+ return WS_VERSION_E;
10767+ }
10768+ if (++ssh->handshake->bannerLines > WOLFSSH_MAX_BANNER_LINES) {
10769+ WLOG(WS_LOG_DEBUG, "too many banner lines");
10770+ return WS_VERSION_E;
10771+ }
10772+
10773+ /* Banner line: log without the terminator and advance past it.
10774+ * The next GetInputLine compacts so the next line starts at
10775+ * buffer[0]. Logged at debug level since the bytes are
10776+ * peer-controlled and may contain control characters. */
10777+ WLOG(WS_LOG_DEBUG, "peer banner: %.*s",
10778+ (int)(eol - ssh->inputBuffer.buffer),
10779+ ssh->inputBuffer.buffer);
10780+ ssh->inputBuffer.idx +=
10781+ (word32)(eol - ssh->inputBuffer.buffer)
10782+ + ((*eol == '\r') ? 2 : 1);
10783+ } while (1);
10784+
10785+ /* eol points at the start of the version line's terminator: '\r' for
10786+ * CRLF or '\n' for LF only. */
10787+ eolSz = (*eol == '\r') ? 2 : 1;
10788+
10789+ if (ssh->inputBuffer.length >= SSH_PROTO_SZ
10790+ && WSTRNCASECMP((char*)ssh->inputBuffer.buffer,
10791+ ssh->ctx->sshProtoIdStr, SSH_PROTO_SZ) == 0) {
1073910792
1074010793 if (ssh->ctx->side == WOLFSSH_ENDPOINT_SERVER)
1074110794 ssh->clientState = CLIENT_VERSION_DONE;
@@ -10751,9 +10804,6 @@ int DoProtoId(WOLFSSH* ssh)
1075110804 ssh->clientOpenSSH = 1;
1075210805 }
1075310806
10754- if (*eol == '\r') {
10755- SSH_PROTO_EOL_SZ++;
10756- }
1075710807 *eol = 0;
1075810808
1075910809 idSz = (word32)WSTRLEN((char*)ssh->inputBuffer.buffer);
@@ -10769,7 +10819,7 @@ int DoProtoId(WOLFSSH* ssh)
1076910819 ssh->peerProtoIdSz = idSz + LENGTH_SZ;
1077010820 }
1077110821
10772- ssh->inputBuffer.idx += idSz + SSH_PROTO_EOL_SZ ;
10822+ ssh->inputBuffer.idx += idSz + eolSz ;
1077310823
1077410824 ShrinkBuffer(&ssh->inputBuffer, 0);
1077510825
@@ -17869,6 +17919,11 @@ void AddAssign64(word32* addend1, word32 addend2)
1786917919
1787017920#ifdef WOLFSSH_TEST_INTERNAL
1787117921
17922+ int wolfSSH_TestDoProtoId(WOLFSSH* ssh)
17923+ {
17924+ return DoProtoId(ssh);
17925+ }
17926+
1787217927int wolfSSH_TestIsMessageAllowed(WOLFSSH* ssh, byte msg, byte state)
1787317928{
1787417929 return IsMessageAllowed(ssh, msg, state);
0 commit comments