ssh: cosmetic cleanups

These are the cosmetic cleanups from the bits of code that I
rereviewed.

1) stringLength now takes a int; the length of the string. Too many
   callers were allocating with stringLength([]byte(s)) and
   stringLength only needs to call len().

2) agent.go now has sendAndReceive to remove logic that was
   duplicated.

3) We now reject negative DH values

4) We now reject empty packets rather than crashing.

R=dave, jonathan.mark.pittman
CC=golang-dev
https://golang.org/cl/6061052
This commit is contained in:
Adam Langley
2012-04-20 15:17:42 -04:00
parent 81e0b644eb
commit 63f855d724
13 changed files with 103 additions and 99 deletions
+17 -9
View File
@@ -7,6 +7,7 @@ package ssh
import (
"crypto/dsa"
"crypto/rsa"
"errors"
"math/big"
"strconv"
"sync"
@@ -32,6 +33,13 @@ type dhGroup struct {
g, p *big.Int
}
func (group *dhGroup) diffieHellman(theirPublic, myPrivate *big.Int) (*big.Int, error) {
if theirPublic.Sign() <= 0 || theirPublic.Cmp(group.p) >= 0 {
return nil, errors.New("ssh: DH parameter out of bounds")
}
return new(big.Int).Exp(theirPublic, myPrivate, group.p), nil
}
// dhGroup1 is the group called diffie-hellman-group1-sha1 in RFC 4253 and
// Oakley Group 2 in RFC 2409.
var dhGroup1 *dhGroup
@@ -178,8 +186,8 @@ func serializeSignature(algoname string, sig []byte) []byte {
case hostAlgoDSACertV01:
algoname = "ssh-dss"
}
length := stringLength([]byte(algoname))
length += stringLength(sig)
length := stringLength(len(algoname))
length += stringLength(len(sig))
ret := make([]byte, length)
r := marshalString(ret, []byte(algoname))
@@ -203,7 +211,7 @@ func serializePublickey(key interface{}) []byte {
panic("unexpected key type")
}
length := stringLength([]byte(algoname))
length := stringLength(len(algoname))
length += len(pubKeyBytes)
ret := make([]byte, length)
r := marshalString(ret, []byte(algoname))
@@ -230,14 +238,14 @@ func buildDataSignedForAuth(sessionId []byte, req userAuthRequestMsg, algo, pubK
service := []byte(req.Service)
method := []byte(req.Method)
length := stringLength(sessionId)
length := stringLength(len(sessionId))
length += 1
length += stringLength(user)
length += stringLength(service)
length += stringLength(method)
length += stringLength(len(user))
length += stringLength(len(service))
length += stringLength(len(method))
length += 1
length += stringLength(algo)
length += stringLength(pubKey)
length += stringLength(len(algo))
length += stringLength(len(pubKey))
ret := make([]byte, length)
r := marshalString(ret, sessionId)