|
|
|
|
@ -19,6 +19,11 @@ import (
|
|
|
|
|
// messages are wrong when using ECDH.
|
|
|
|
|
const debugHandshake = false
|
|
|
|
|
|
|
|
|
|
// chanSize sets the amount of buffering SSH connections. This is
|
|
|
|
|
// primarily for testing: setting chanSize=0 uncovers deadlocks more
|
|
|
|
|
// quickly.
|
|
|
|
|
const chanSize = 16
|
|
|
|
|
|
|
|
|
|
// keyingTransport is a packet based transport that supports key
|
|
|
|
|
// changes. It need not be thread-safe. It should pass through
|
|
|
|
|
// msgNewKeys in both directions.
|
|
|
|
|
@ -60,7 +65,8 @@ type handshakeTransport struct {
|
|
|
|
|
pendingPackets [][]byte // Used when a key exchange is in progress.
|
|
|
|
|
|
|
|
|
|
// If the read loop wants to schedule a kex, it pings this
|
|
|
|
|
// channel, and the write loop will send out a kex message.
|
|
|
|
|
// channel, and the write loop will send out a kex
|
|
|
|
|
// message.
|
|
|
|
|
requestKex chan struct{}
|
|
|
|
|
|
|
|
|
|
// If the other side requests or confirms a kex, its kexInit
|
|
|
|
|
@ -72,9 +78,14 @@ type handshakeTransport struct {
|
|
|
|
|
dialAddress string
|
|
|
|
|
remoteAddr net.Addr
|
|
|
|
|
|
|
|
|
|
readSinceKex uint64
|
|
|
|
|
// Algorithms agreed in the last key exchange.
|
|
|
|
|
algorithms *algorithms
|
|
|
|
|
|
|
|
|
|
readPacketsLeft uint32
|
|
|
|
|
readBytesLeft int64
|
|
|
|
|
|
|
|
|
|
writtenSinceKex uint64
|
|
|
|
|
writePacketsLeft uint32
|
|
|
|
|
writeBytesLeft int64
|
|
|
|
|
|
|
|
|
|
// The session ID or nil if first kex did not complete yet.
|
|
|
|
|
sessionID []byte
|
|
|
|
|
@ -90,12 +101,15 @@ func newHandshakeTransport(conn keyingTransport, config *Config, clientVersion,
|
|
|
|
|
conn: conn,
|
|
|
|
|
serverVersion: serverVersion,
|
|
|
|
|
clientVersion: clientVersion,
|
|
|
|
|
incoming: make(chan []byte, 16),
|
|
|
|
|
incoming: make(chan []byte, chanSize),
|
|
|
|
|
requestKex: make(chan struct{}, 1),
|
|
|
|
|
startKex: make(chan *pendingKex, 1),
|
|
|
|
|
|
|
|
|
|
config: config,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We always start with a mandatory key exchange.
|
|
|
|
|
t.requestKex <- struct{}{}
|
|
|
|
|
return t
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -152,6 +166,7 @@ func (t *handshakeTransport) printPacket(p []byte, write bool) {
|
|
|
|
|
if write {
|
|
|
|
|
action = "sent"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if p[0] == msgChannelData || p[0] == msgChannelExtendedData {
|
|
|
|
|
log.Printf("%s %s data (packet %d bytes)", t.id(), action, len(p))
|
|
|
|
|
} else {
|
|
|
|
|
@ -169,12 +184,6 @@ func (t *handshakeTransport) readPacket() ([]byte, error) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (t *handshakeTransport) readLoop() {
|
|
|
|
|
// We always start with the mandatory key exchange. We use
|
|
|
|
|
// the channel for simplicity, and this works if we can rely
|
|
|
|
|
// on the SSH package itself not doing anything else before
|
|
|
|
|
// waitSession has completed.
|
|
|
|
|
t.requestKeyExchange()
|
|
|
|
|
|
|
|
|
|
first := true
|
|
|
|
|
for {
|
|
|
|
|
p, err := t.readOnePacket(first)
|
|
|
|
|
@ -226,10 +235,10 @@ func (t *handshakeTransport) requestKeyExchange() {
|
|
|
|
|
default:
|
|
|
|
|
// something already requested a kex, so do nothing.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (t *handshakeTransport) kexLoop() {
|
|
|
|
|
|
|
|
|
|
write:
|
|
|
|
|
for t.getWriteError() == nil {
|
|
|
|
|
var request *pendingKex
|
|
|
|
|
@ -243,6 +252,7 @@ write:
|
|
|
|
|
break write
|
|
|
|
|
}
|
|
|
|
|
case <-t.requestKex:
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !sent {
|
|
|
|
|
@ -266,7 +276,8 @@ write:
|
|
|
|
|
|
|
|
|
|
// We're not servicing t.startKex, but the remote end
|
|
|
|
|
// has just sent us a kexInitMsg, so it can't send
|
|
|
|
|
// another key change request.
|
|
|
|
|
// another key change request, until we close the done
|
|
|
|
|
// channel on the pendingKex request.
|
|
|
|
|
|
|
|
|
|
err := t.enterKeyExchange(request.otherInit)
|
|
|
|
|
|
|
|
|
|
@ -274,7 +285,29 @@ write:
|
|
|
|
|
t.writeError = err
|
|
|
|
|
t.sentInitPacket = nil
|
|
|
|
|
t.sentInitMsg = nil
|
|
|
|
|
t.writtenSinceKex = 0
|
|
|
|
|
t.writePacketsLeft = packetRekeyThreshold
|
|
|
|
|
if t.config.RekeyThreshold > 0 {
|
|
|
|
|
t.writeBytesLeft = int64(t.config.RekeyThreshold)
|
|
|
|
|
} else if t.algorithms != nil {
|
|
|
|
|
t.writeBytesLeft = t.algorithms.w.rekeyBytes()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// we have completed the key exchange. Since the
|
|
|
|
|
// reader is still blocked, it is safe to clear out
|
|
|
|
|
// the requestKex channel. This avoids the situation
|
|
|
|
|
// where: 1) we consumed our own request for the
|
|
|
|
|
// initial kex, and 2) the kex from the remote side
|
|
|
|
|
// caused another send on the requestKex channel,
|
|
|
|
|
clear:
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-t.requestKex:
|
|
|
|
|
//
|
|
|
|
|
default:
|
|
|
|
|
break clear
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
request.done <- t.writeError
|
|
|
|
|
|
|
|
|
|
// kex finished. Push packets that we received while
|
|
|
|
|
@ -288,7 +321,7 @@ write:
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
t.pendingPackets = t.pendingPackets[0:]
|
|
|
|
|
t.pendingPackets = t.pendingPackets[:0]
|
|
|
|
|
t.mu.Unlock()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -304,17 +337,31 @@ write:
|
|
|
|
|
t.conn.Close()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (t *handshakeTransport) readOnePacket(first bool) ([]byte, error) {
|
|
|
|
|
if t.readSinceKex > t.config.RekeyThreshold {
|
|
|
|
|
t.requestKeyExchange()
|
|
|
|
|
}
|
|
|
|
|
// The protocol uses uint32 for packet counters, so we can't let them
|
|
|
|
|
// reach 1<<32. We will actually read and write more packets than
|
|
|
|
|
// this, though: the other side may send more packets, and after we
|
|
|
|
|
// hit this limit on writing we will send a few more packets for the
|
|
|
|
|
// key exchange itself.
|
|
|
|
|
const packetRekeyThreshold = (1 << 31)
|
|
|
|
|
|
|
|
|
|
func (t *handshakeTransport) readOnePacket(first bool) ([]byte, error) {
|
|
|
|
|
p, err := t.conn.readPacket()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t.readSinceKex += uint64(len(p))
|
|
|
|
|
if t.readPacketsLeft > 0 {
|
|
|
|
|
t.readPacketsLeft--
|
|
|
|
|
} else {
|
|
|
|
|
t.requestKeyExchange()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if t.readBytesLeft > 0 {
|
|
|
|
|
t.readBytesLeft -= int64(len(p))
|
|
|
|
|
} else {
|
|
|
|
|
t.requestKeyExchange()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if debugHandshake {
|
|
|
|
|
t.printPacket(p, false)
|
|
|
|
|
}
|
|
|
|
|
@ -344,7 +391,12 @@ func (t *handshakeTransport) readOnePacket(first bool) ([]byte, error) {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t.readSinceKex = 0
|
|
|
|
|
t.readPacketsLeft = packetRekeyThreshold
|
|
|
|
|
if t.config.RekeyThreshold > 0 {
|
|
|
|
|
t.readBytesLeft = int64(t.config.RekeyThreshold)
|
|
|
|
|
} else {
|
|
|
|
|
t.readBytesLeft = t.algorithms.r.rekeyBytes()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// By default, a key exchange is hidden from higher layers by
|
|
|
|
|
// translating it into msgIgnore.
|
|
|
|
|
@ -427,8 +479,16 @@ func (t *handshakeTransport) writePacket(p []byte) error {
|
|
|
|
|
t.pendingPackets = append(t.pendingPackets, cp)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
t.writtenSinceKex += uint64(len(p))
|
|
|
|
|
if t.writtenSinceKex > t.config.RekeyThreshold {
|
|
|
|
|
|
|
|
|
|
if t.writeBytesLeft > 0 {
|
|
|
|
|
t.writeBytesLeft -= int64(len(p))
|
|
|
|
|
} else {
|
|
|
|
|
t.requestKeyExchange()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if t.writePacketsLeft > 0 {
|
|
|
|
|
t.writePacketsLeft--
|
|
|
|
|
} else {
|
|
|
|
|
t.requestKeyExchange()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -469,7 +529,8 @@ func (t *handshakeTransport) enterKeyExchange(otherInitPacket []byte) error {
|
|
|
|
|
magics.serverKexInit = otherInitPacket
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
algs, err := findAgreedAlgorithms(clientInit, serverInit)
|
|
|
|
|
var err error
|
|
|
|
|
t.algorithms, err = findAgreedAlgorithms(clientInit, serverInit)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
@ -492,16 +553,16 @@ func (t *handshakeTransport) enterKeyExchange(otherInitPacket []byte) error {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
kex, ok := kexAlgoMap[algs.kex]
|
|
|
|
|
kex, ok := kexAlgoMap[t.algorithms.kex]
|
|
|
|
|
if !ok {
|
|
|
|
|
return fmt.Errorf("ssh: unexpected key exchange algorithm %v", algs.kex)
|
|
|
|
|
return fmt.Errorf("ssh: unexpected key exchange algorithm %v", t.algorithms.kex)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result *kexResult
|
|
|
|
|
if len(t.hostKeys) > 0 {
|
|
|
|
|
result, err = t.server(kex, algs, &magics)
|
|
|
|
|
result, err = t.server(kex, t.algorithms, &magics)
|
|
|
|
|
} else {
|
|
|
|
|
result, err = t.client(kex, algs, &magics)
|
|
|
|
|
result, err = t.client(kex, t.algorithms, &magics)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
@ -513,7 +574,7 @@ func (t *handshakeTransport) enterKeyExchange(otherInitPacket []byte) error {
|
|
|
|
|
}
|
|
|
|
|
result.SessionID = t.sessionID
|
|
|
|
|
|
|
|
|
|
t.conn.prepareKeyChange(algs, result)
|
|
|
|
|
t.conn.prepareKeyChange(t.algorithms, result)
|
|
|
|
|
if err = t.conn.writePacket([]byte{msgNewKeys}); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|