Skip to content

Commit

Permalink
ssh: add decode support for banners
Browse files Browse the repository at this point in the history
These banners can be printed when enabling debugHandshake, add decode
support so that they're not printed as unknown messages.

Change-Id: Ic8d56079d8225c35aac843accdbc80a642dd6249
Reviewed-on: https://go-review.googlesource.com/c/crypto/+/650635
Reviewed-by: Junyang Shao <[email protected]>
Reviewed-by: Nicola Murino <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Auto-Submit: Nicola Murino <[email protected]>
Reviewed-by: Dmitri Shuralyov <[email protected]>
  • Loading branch information
imirkin authored and gopherbot committed Mar 3, 2025
1 parent bbc689c commit 24852b6
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ssh/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,8 @@ func decode(packet []byte) (interface{}, error) {
return new(userAuthSuccessMsg), nil
case msgUserAuthFailure:
msg = new(userAuthFailureMsg)
case msgUserAuthBanner:
msg = new(userAuthBannerMsg)
case msgUserAuthPubKeyOk:
msg = new(userAuthPubKeyOkMsg)
case msgGlobalRequest:
Expand Down
56 changes: 56 additions & 0 deletions ssh/messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,62 @@ func TestMarshalMultiTag(t *testing.T) {
}
}

func TestDecode(t *testing.T) {
rnd := rand.New(rand.NewSource(0))
kexInit := new(kexInitMsg).Generate(rnd, 10).Interface()
kexDHInit := new(kexDHInitMsg).Generate(rnd, 10).Interface()
kexDHReply := new(kexDHReplyMsg)
kexDHReply.Y = randomInt(rnd)
// Note: userAuthSuccessMsg can't be tested directly since it
// doesn't have a field for sshtype. So it's tested separately
// at the end.
decodeMessageTypes := []interface{}{
new(disconnectMsg),
new(serviceRequestMsg),
new(serviceAcceptMsg),
new(extInfoMsg),
kexInit,
kexDHInit,
kexDHReply,
new(userAuthRequestMsg),
new(userAuthFailureMsg),
new(userAuthBannerMsg),
new(userAuthPubKeyOkMsg),
new(globalRequestMsg),
new(globalRequestSuccessMsg),
new(globalRequestFailureMsg),
new(channelOpenMsg),
new(channelDataMsg),
new(channelOpenConfirmMsg),
new(channelOpenFailureMsg),
new(windowAdjustMsg),
new(channelEOFMsg),
new(channelCloseMsg),
new(channelRequestMsg),
new(channelRequestSuccessMsg),
new(channelRequestFailureMsg),
new(userAuthGSSAPIToken),
new(userAuthGSSAPIMIC),
new(userAuthGSSAPIErrTok),
new(userAuthGSSAPIError),
}
for _, msg := range decodeMessageTypes {
decoded, err := decode(Marshal(msg))
if err != nil {
t.Errorf("error decoding %T", msg)
} else if reflect.TypeOf(msg) != reflect.TypeOf(decoded) {
t.Errorf("error decoding %T, unexpected %T", msg, decoded)
}
}

userAuthSuccess, err := decode([]byte{msgUserAuthSuccess})
if err != nil {
t.Errorf("error decoding userAuthSuccessMsg")
} else if reflect.TypeOf(userAuthSuccess) != reflect.TypeOf((*userAuthSuccessMsg)(nil)) {
t.Errorf("error decoding userAuthSuccessMsg, unexpected %T", userAuthSuccess)
}
}

func randomBytes(out []byte, rand *rand.Rand) {
for i := 0; i < len(out); i++ {
out[i] = byte(rand.Int31())
Expand Down

0 comments on commit 24852b6

Please sign in to comment.