diff --git a/src/internal.c b/src/internal.c index b49708b4c..7de51bb02 100644 --- a/src/internal.c +++ b/src/internal.c @@ -15420,6 +15420,17 @@ int ValidateProtoId(const char* protoIdStr, word32 len) } } + /* softwareversion runs to the first space and must not contain a '-' + * (RFC 4253 section 4.2); the comments may. */ + for (i = SSH_PROTO_SZ; + i < len - SSH_PROTO_EOL_SZ && protoIdStr[i] != ' '; i++) { + if (protoIdStr[i] == '-') { + WLOG(WS_LOG_ERROR, "Proto Id was invalid: softwareversion " + "must not contain '-'"); + return WS_BAD_ARGUMENT; + } + } + return WS_SUCCESS; } diff --git a/tests/unit.c b/tests/unit.c index e94996edf..9bb2c02e4 100644 --- a/tests/unit.c +++ b/tests/unit.c @@ -663,6 +663,9 @@ static int test_DoProtoId(void) * 0x7e must both be accepted. */ { "body w/ SP comments", "SSH-2.0-app comment\r\n", 1 }, { "body w/ tilde", "SSH-2.0-app~1\r\n", 1 }, + /* RFC 4253 section 4.2 bars '-' only from softwareversion. */ + { "comments w/ minus", "SSH-2.0-app my-comment\r\n", 1 }, + { "comments are minus", "SSH-2.0-app -\r\n", 1 }, /* Failing Tests */ { "wrong version prefix", "SSH-2-this_is_my_app\r\n", 0 }, { "bad casing prefix", "sSH-2.0-this_is_my_app\r\n", 0 }, @@ -675,7 +678,10 @@ static int test_DoProtoId(void) { "Body End in CR", "SSH-2.0-s\r\r\n", 0 }, { "Body End in TAB", "SSH-2.0-s\t\r\n", 0 }, { "body starts w/ space", "SSH-2.0-\x20-a-b\r\n", 0 }, - { "body has embedded TAB", "SSH-2.0-\x7e-a\t\r\n", 0 }, + { "body has embedded TAB", "SSH-2.0-\x7e" "a\t\r\n", 0 }, + { "swversion w/ minus", "SSH-2.0-my-app\r\n", 0 }, + { "swversion is minus", "SSH-2.0--\r\n", 0 }, + { "minus before comments", "SSH-2.0-app- comment\r\n", 0 }, { "too long id", tooLongProtoId, 0 }, { "null pointer", NULL, 0 }, }; diff --git a/wolfssh/ssh.h b/wolfssh/ssh.h index a91b43734..54a82c0bd 100644 --- a/wolfssh/ssh.h +++ b/wolfssh/ssh.h @@ -821,6 +821,8 @@ WOLFSSH_API int wolfSSH_CTX_SetBanner(WOLFSSH_CTX* ctx, const char* newBanner); * body as softwareversion [SP comments], so a leading space would * make softwareversion empty. A space later in the body is accepted * and starts the optional comments field. + * MUST NOT carry a '-' in softwareversion, the body up to the first + * space (RFC 4253 section 4.2). The comments may carry one. * If these are not adhered to the function will return WS_BAD_ARGUMENT * and not load the ProtoId into the WOLFSSH_CTX struct. * ProtoIdStr is stored by reference and is not copied, so it must remain