x/crypto/ssh: hide msgNewKeys in the transport layer.

This ensures that extraneous key exchanges cannot confuse application
level code.

Change-Id: I1a333e2b7b46f1e484406a79db7a949294e79c6d
Reviewed-on: https://go-review.googlesource.com/22417
Reviewed-by: Han-Wen Nienhuys <hanwen@google.com>
Run-TryBot: Han-Wen Nienhuys <hanwen@google.com>
Reviewed-by: Adam Langley <agl@golang.org>
This commit is contained in:
Han-Wen Nienhuys
2016-05-10 17:36:43 +00:00
committed by Adam Langley
parent 91ab96ae98
commit b76c864ef1
5 changed files with 77 additions and 43 deletions
+36 -15
View File
@@ -173,6 +173,17 @@ func (t *handshakeTransport) readOnePacket() ([]byte, error) {
}
t.mu.Lock()
// By default, a key exchange is hidden from higher layers by
// translating it into msgIgnore.
successPacket := []byte{msgIgnore}
if t.sessionID == nil {
// sendKexInit() for the first kex waits for
// msgNewKeys so the authentication process is
// guaranteed to happen over an encrypted transport.
successPacket = []byte{msgNewKeys}
}
err = t.enterKeyExchangeLocked(p)
if err != nil {
// drop connection
@@ -196,7 +207,7 @@ func (t *handshakeTransport) readOnePacket() ([]byte, error) {
}
t.readSinceKex = 0
return []byte{msgNewKeys}, nil
return successPacket, nil
}
// keyChangeCategory describes whether a key exchange is the first on a
@@ -213,20 +224,37 @@ const (
// blocked until the change is done, and a failed key change will
// close the underlying transport. This function is safe for
// concurrent use by multiple goroutines.
func (t *handshakeTransport) sendKexInit(isFirst keyChangeCategory) (*kexInitMsg, []byte, error) {
func (t *handshakeTransport) sendKexInit(isFirst keyChangeCategory) error {
t.mu.Lock()
defer t.mu.Unlock()
return t.sendKexInitLocked(isFirst)
// If this is the initial key change, but we already have a sessionID,
// then do nothing because the key exchange has already completed
// asynchronously.
if isFirst && t.sessionID != nil {
t.mu.Unlock()
return nil
}
_, _, err := t.sendKexInitLocked(isFirst)
t.mu.Unlock()
if err != nil {
return err
}
if isFirst {
if packet, err := t.readPacket(); err != nil {
return err
} else if packet[0] != msgNewKeys {
return unexpectedMessageError(msgNewKeys, packet[0])
}
}
return nil
}
func (t *handshakeTransport) requestInitialKeyChange() error {
_, _, err := t.sendKexInit(firstKeyExchange)
return err
return t.sendKexInit(firstKeyExchange)
}
func (t *handshakeTransport) requestKeyChange() error {
_, _, err := t.sendKexInit(subsequentKeyExchange)
return err
return t.sendKexInit(subsequentKeyExchange)
}
// sendKexInitLocked sends a key change message. t.mu must be locked
@@ -240,13 +268,6 @@ func (t *handshakeTransport) sendKexInitLocked(isFirst keyChangeCategory) (*kexI
return t.sentInitMsg, t.sentInitPacket, nil
}
// If this is the initial key change, but we already have a sessionID,
// then do nothing because the key exchange has already completed
// asynchronously.
if isFirst && t.sessionID != nil {
return nil, nil, nil
}
msg := &kexInitMsg{
KexAlgos: t.config.KeyExchanges,
CiphersClientServer: t.config.Ciphers,