Skip to content

Commit

Permalink
added a shared method to parse extInfoMsgs
Browse files Browse the repository at this point in the history
both client and server side need to parse this message

Signed-off-by: Nicola Murino <[email protected]>
  • Loading branch information
drakkan committed May 19, 2022
1 parent 5835f04 commit 92ea34e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 32 deletions.
17 changes: 2 additions & 15 deletions ssh/client_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,10 @@ func (c *connection) clientAuthenticate(config *ClientConfig) error {
// RFC 8308, Section 2.4.
extensions := make(map[string][]byte)
if len(packet) > 0 && packet[0] == msgExtInfo {
var extInfo extInfoMsg
if err := Unmarshal(packet, &extInfo); err != nil {
extensions, err = parseExtInfoMsg(packet)
if err != nil {
return err
}
payload := extInfo.Payload
for i := uint32(0); i < extInfo.NumExtensions; i++ {
name, rest, ok := parseString(payload)
if !ok {
return parseError(msgExtInfo)
}
value, rest, ok := parseString(rest)
if !ok {
return parseError(msgExtInfo)
}
extensions[string(name)] = value
payload = rest
}
packet, err = c.transport.readPacket()
if err != nil {
return err
Expand Down
25 changes: 25 additions & 0 deletions ssh/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,31 @@ func parseError(tag uint8) error {
return fmt.Errorf("ssh: parse error in message type %d", tag)
}

// parseExtInfoMsg returns the extensions from an extInfoMsg packet.
// packet must be an already validated extInfoMsg
func parseExtInfoMsg(packet []byte) (map[string][]byte, error) {
extensions := make(map[string][]byte)
var extInfo extInfoMsg

if err := Unmarshal(packet, &extInfo); err != nil {
return nil, err
}
payload := extInfo.Payload
for i := uint32(0); i < extInfo.NumExtensions; i++ {
name, rest, ok := parseString(payload)
if !ok {
return nil, parseError(msgExtInfo)
}
value, rest, ok := parseString(rest)
if !ok {
return nil, parseError(msgExtInfo)
}
extensions[string(name)] = value
payload = rest
}
return extensions, nil
}

func findCommon(what string, client []string, server []string) (common string, err error) {
for _, c := range client {
for _, s := range server {
Expand Down
18 changes: 1 addition & 17 deletions ssh/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,25 +265,9 @@ func (s *connection) serverHandshake(config *ServerConfig) (*Permissions, error)

if len(packet) > 0 && packet[0] == msgExtInfo {
// read SSH_MSG_EXT_INFO
var extInfo extInfoMsg
extensions := make(map[string][]byte)
if err := Unmarshal(packet, &extInfo); err != nil {
if _, err := parseExtInfoMsg(packet); err != nil {
return nil, err
}
payload := extInfo.Payload
for i := uint32(0); i < extInfo.NumExtensions; i++ {
name, rest, ok := parseString(payload)
if !ok {
return nil, parseError(msgExtInfo)
}
value, rest, ok := parseString(rest)
if !ok {
return nil, parseError(msgExtInfo)
}
extensions[string(name)] = value
payload = rest
}

// read the next packet
if packet, err = s.transport.readPacket(); err != nil {
return nil, err
Expand Down

0 comments on commit 92ea34e

Please sign in to comment.