save the decoded packet number on the unpacked packet

This commit is contained in:
Marten Seemann 2018-11-29 16:16:57 +07:00
parent a8e5309cd1
commit ce6b9f2f1d
4 changed files with 22 additions and 21 deletions

View file

@ -11,6 +11,7 @@ import (
) )
type unpackedPacket struct { type unpackedPacket struct {
packetNumber protocol.PacketNumber
encryptionLevel protocol.EncryptionLevel encryptionLevel protocol.EncryptionLevel
frames []wire.Frame frames []wire.Frame
} }
@ -40,7 +41,7 @@ func newPacketUnpacker(aead quicAEAD, version protocol.VersionNumber) unpacker {
} }
func (u *packetUnpacker) Unpack(headerBinary []byte, hdr *wire.ExtendedHeader, data []byte) (*unpackedPacket, error) { func (u *packetUnpacker) Unpack(headerBinary []byte, hdr *wire.ExtendedHeader, data []byte) (*unpackedPacket, error) {
hdr.PacketNumber = protocol.DecodePacketNumber( pn := protocol.DecodePacketNumber(
hdr.PacketNumberLen, hdr.PacketNumberLen,
u.largestRcvdPacketNumber, u.largestRcvdPacketNumber,
hdr.PacketNumber, hdr.PacketNumber,
@ -55,16 +56,16 @@ func (u *packetUnpacker) Unpack(headerBinary []byte, hdr *wire.ExtendedHeader, d
var err error var err error
switch hdr.Type { switch hdr.Type {
case protocol.PacketTypeInitial: case protocol.PacketTypeInitial:
decrypted, err = u.aead.OpenInitial(buf, data, hdr.PacketNumber, headerBinary) decrypted, err = u.aead.OpenInitial(buf, data, pn, headerBinary)
encryptionLevel = protocol.EncryptionInitial encryptionLevel = protocol.EncryptionInitial
case protocol.PacketTypeHandshake: case protocol.PacketTypeHandshake:
decrypted, err = u.aead.OpenHandshake(buf, data, hdr.PacketNumber, headerBinary) decrypted, err = u.aead.OpenHandshake(buf, data, pn, headerBinary)
encryptionLevel = protocol.EncryptionHandshake encryptionLevel = protocol.EncryptionHandshake
default: default:
if hdr.IsLongHeader { if hdr.IsLongHeader {
return nil, fmt.Errorf("unknown packet type: %s", hdr.Type) return nil, fmt.Errorf("unknown packet type: %s", hdr.Type)
} }
decrypted, err = u.aead.Open1RTT(buf, data, hdr.PacketNumber, headerBinary) decrypted, err = u.aead.Open1RTT(buf, data, pn, headerBinary)
encryptionLevel = protocol.Encryption1RTT encryptionLevel = protocol.Encryption1RTT
} }
if err != nil { if err != nil {
@ -72,7 +73,7 @@ func (u *packetUnpacker) Unpack(headerBinary []byte, hdr *wire.ExtendedHeader, d
} }
// Only do this after decrypting, so we are sure the packet is not attacker-controlled // Only do this after decrypting, so we are sure the packet is not attacker-controlled
u.largestRcvdPacketNumber = utils.MaxPacketNumber(u.largestRcvdPacketNumber, hdr.PacketNumber) u.largestRcvdPacketNumber = utils.MaxPacketNumber(u.largestRcvdPacketNumber, pn)
fs, err := u.parseFrames(decrypted) fs, err := u.parseFrames(decrypted)
if err != nil { if err != nil {
@ -80,6 +81,7 @@ func (u *packetUnpacker) Unpack(headerBinary []byte, hdr *wire.ExtendedHeader, d
} }
return &unpackedPacket{ return &unpackedPacket{
packetNumber: pn,
encryptionLevel: encryptionLevel, encryptionLevel: encryptionLevel,
frames: fs, frames: fs,
}, nil }, nil

View file

@ -69,8 +69,9 @@ var _ = Describe("Packet Unpacker", func() {
PacketNumberLen: 2, PacketNumberLen: 2,
} }
aead.EXPECT().Open1RTT(gomock.Any(), gomock.Any(), firstHdr.PacketNumber, gomock.Any()).Return([]byte{0}, nil) aead.EXPECT().Open1RTT(gomock.Any(), gomock.Any(), firstHdr.PacketNumber, gomock.Any()).Return([]byte{0}, nil)
_, err := unpacker.Unpack(firstHdr.Raw, firstHdr, nil) packet, err := unpacker.Unpack(firstHdr.Raw, firstHdr, nil)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(packet.packetNumber).To(Equal(protocol.PacketNumber(0x1337)))
// the real packet number is 0x1338, but only the last byte is sent // the real packet number is 0x1338, but only the last byte is sent
secondHdr := &wire.ExtendedHeader{ secondHdr := &wire.ExtendedHeader{
PacketNumber: 0x38, PacketNumber: 0x38,
@ -78,8 +79,9 @@ var _ = Describe("Packet Unpacker", func() {
} }
// expect the call with the decoded packet number // expect the call with the decoded packet number
aead.EXPECT().Open1RTT(gomock.Any(), gomock.Any(), protocol.PacketNumber(0x1338), gomock.Any()).Return([]byte{0}, nil) aead.EXPECT().Open1RTT(gomock.Any(), gomock.Any(), protocol.PacketNumber(0x1338), gomock.Any()).Return([]byte{0}, nil)
_, err = unpacker.Unpack(secondHdr.Raw, secondHdr, nil) packet, err = unpacker.Unpack(secondHdr.Raw, secondHdr, nil)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(packet.packetNumber).To(Equal(protocol.PacketNumber(0x1338)))
}) })
It("unpacks the frames", func() { It("unpacks the frames", func() {

View file

@ -504,19 +504,16 @@ func (s *session) handlePacketImpl(p *receivedPacket) error {
} }
packet, err := s.unpacker.Unpack(hdr.Raw, hdr, data) packet, err := s.unpacker.Unpack(hdr.Raw, hdr, data)
if s.logger.Debug() {
if err != nil {
s.logger.Debugf("<- Reading packet 0x%x (%d bytes) for connection %s", hdr.PacketNumber, len(p.data), hdr.DestConnectionID)
} else {
s.logger.Debugf("<- Reading packet 0x%x (%d bytes) for connection %s, %s", hdr.PacketNumber, len(p.data), hdr.DestConnectionID, packet.encryptionLevel)
}
hdr.Log(s.logger)
}
// if the decryption failed, this might be a packet sent by an attacker // if the decryption failed, this might be a packet sent by an attacker
if err != nil { if err != nil {
return err return err
} }
if s.logger.Debug() {
s.logger.Debugf("<- Reading packet %#x (%d bytes) for connection %s, %s", packet.packetNumber, len(p.data), p.hdr.DestConnectionID, packet.encryptionLevel)
hdr.Log(s.logger)
}
// The server can change the source connection ID with the first Handshake packet. // The server can change the source connection ID with the first Handshake packet.
if s.perspective == protocol.PerspectiveClient && !s.receivedFirstPacket && hdr.IsLongHeader && !hdr.SrcConnectionID.Equal(s.destConnID) { if s.perspective == protocol.PerspectiveClient && !s.receivedFirstPacket && hdr.IsLongHeader && !hdr.SrcConnectionID.Equal(s.destConnID) {
s.logger.Debugf("Received first packet. Switching destination connection ID to: %s", hdr.SrcConnectionID) s.logger.Debugf("Received first packet. Switching destination connection ID to: %s", hdr.SrcConnectionID)
@ -541,12 +538,12 @@ func (s *session) handlePacketImpl(p *receivedPacket) error {
// The session will be closed and recreated as soon as the crypto setup processed the HRR. // The session will be closed and recreated as soon as the crypto setup processed the HRR.
if hdr.Type != protocol.PacketTypeRetry { if hdr.Type != protocol.PacketTypeRetry {
isRetransmittable := ackhandler.HasRetransmittableFrames(packet.frames) isRetransmittable := ackhandler.HasRetransmittableFrames(packet.frames)
if err := s.receivedPacketHandler.ReceivedPacket(hdr.PacketNumber, p.rcvTime, isRetransmittable); err != nil { if err := s.receivedPacketHandler.ReceivedPacket(packet.packetNumber, p.rcvTime, isRetransmittable); err != nil {
return err return err
} }
} }
return s.handleFrames(packet.frames, hdr.PacketNumber, packet.encryptionLevel) return s.handleFrames(packet.frames, packet.packetNumber, packet.encryptionLevel)
} }
func (s *session) handleFrames(fs []wire.Frame, pn protocol.PacketNumber, encLevel protocol.EncryptionLevel) error { func (s *session) handleFrames(fs []wire.Frame, pn protocol.PacketNumber, encLevel protocol.EncryptionLevel) error {

View file

@ -464,13 +464,13 @@ var _ = Describe("Session", func() {
It("informs the ReceivedPacketHandler", func() { It("informs the ReceivedPacketHandler", func() {
hdr := &wire.ExtendedHeader{ hdr := &wire.ExtendedHeader{
Raw: []byte("raw header"), Raw: []byte("raw header"),
PacketNumber: 5, PacketNumber: 0x37,
PacketNumberLen: protocol.PacketNumberLen4, PacketNumberLen: protocol.PacketNumberLen1,
} }
rcvTime := time.Now().Add(-10 * time.Second) rcvTime := time.Now().Add(-10 * time.Second)
unpacker.EXPECT().Unpack(gomock.Any(), gomock.Any(), gomock.Any()).Return(&unpackedPacket{}, nil) unpacker.EXPECT().Unpack(gomock.Any(), gomock.Any(), gomock.Any()).Return(&unpackedPacket{packetNumber: 0x1337}, nil)
rph := mockackhandler.NewMockReceivedPacketHandler(mockCtrl) rph := mockackhandler.NewMockReceivedPacketHandler(mockCtrl)
rph.EXPECT().ReceivedPacket(protocol.PacketNumber(5), rcvTime, false) rph.EXPECT().ReceivedPacket(protocol.PacketNumber(0x1337), rcvTime, false)
sess.receivedPacketHandler = rph sess.receivedPacketHandler = rph
Expect(sess.handlePacketImpl(&receivedPacket{ Expect(sess.handlePacketImpl(&receivedPacket{
rcvTime: rcvTime, rcvTime: rcvTime,