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
diff --git a/ssh/common.go b/ssh/common.go
index 429b488..e94142c 100644
--- a/ssh/common.go
+++ b/ssh/common.go
@@ -7,6 +7,7 @@
import (
"crypto/dsa"
"crypto/rsa"
+ "errors"
"math/big"
"strconv"
"sync"
@@ -32,6 +33,13 @@
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 @@
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 @@
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 @@
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)