Skip to content

Commit

Permalink
All out of order read of epoch 0. Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ekr committed Mar 3, 2018
1 parent 160b93d commit 781e667
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
51 changes: 51 additions & 0 deletions conn_test.go
Expand Up @@ -1636,3 +1636,54 @@ func TestEarlyIOFail(t *testing.T) {
readWriteExpectFail(t, client)
readWriteExpectFail(t, server)
}

func TestDTLSOutOfEpochHSFail(t *testing.T) {
cConn, sConn := pipe()

cbConn := newBufferedConn(cConn)
sbConn := newBufferedConn(sConn)
cbConn.SetAutoflush()
sbConn.SetAutoflush()

client := Client(cbConn, nbConfigDTLS)
server := Server(sbConn, nbConfigDTLS)

hsUntilBlocked(t, client, cbConn)
hsUntilBlocked(t, server, sbConn)

cbConn.Write([]byte{byte(RecordTypeApplicationData),
byte(dtls12WireVersion >> 8), byte(dtls12WireVersion & 0xff),
0, 0, 0, 0, 0, 0, 0, 0, // Epoch 0, seq 0
0, 5, 1, 2, 3, 4, 5, // Payload
})

// This causes an error because it's an unexpected record type.
err := server.Handshake()
assertEquals(t, err, AlertCloseNotify)
}

func TestDTLSOutOfEpochPostHSDiscard(t *testing.T) {
cConn, sConn := pipe()

cbConn := newBufferedConn(cConn)
sbConn := newBufferedConn(sConn)
cbConn.SetAutoflush()
sbConn.SetAutoflush()

client := Client(cbConn, pskDTLSConfig)
server := Server(sbConn, pskDTLSConfig)

hsRunHandshakeOneThread(t, client, server)

// Now inject something with epoch 0, but as app data.
// It will get discarded.
cbConn.Write([]byte{byte(RecordTypeApplicationData),
byte(dtls12WireVersion >> 8), byte(dtls12WireVersion & 0xff),
0, 0, 0, 0, 0, 0, 0, 0, // Epoch 0, seq 0
0, 5, 1, 2, 3, 4, 5, // Payload
})

tmp := make([]byte, 10)
_, err := server.Read(tmp)
assertEquals(t, err, AlertWouldBlock)
}
6 changes: 4 additions & 2 deletions record-layer.go
Expand Up @@ -118,6 +118,7 @@ func NewRecordLayerDTLS(conn io.ReadWriter, dir direction) *RecordLayer {
r.frame = newFrameReader(recordLayerFrameDetails{true})
r.cipher = newCipherStateNull()
r.readCiphers = make(map[Epoch]*cipherState, 0)
r.readCiphers[0] = r.cipher
r.datagram = true
return &r
}
Expand Down Expand Up @@ -368,14 +369,15 @@ func (r *RecordLayer) nextRecord(allowOldEpoch bool) (*TLSPlaintext, error) {

// Look up the cipher suite from the epoch
if epoch != cipher.epoch {
logf(logTypeIO, "%s Message from non-current epoch: [%v != %v]", r.label, epoch,
cipher.epoch)
logf(logTypeIO, "%s Message from non-current epoch: [%v != %v] out-of-epoch reads=%v", r.label, epoch,
cipher.epoch, allowOldEpoch)
if !allowOldEpoch {
return nil, AlertWouldBlock
}
c, ok := r.readCiphers[epoch]
if !ok {
logf(logTypeIO, "%s Message from unknown epoch: [%v]", r.label, epoch)
fmt.Println("Known epochs = ", r.readCiphers)
return nil, AlertWouldBlock
}
cipher = c
Expand Down

0 comments on commit 781e667

Please sign in to comment.