Skip to content

Commit

Permalink
Fix header protection for very small packets
Browse files Browse the repository at this point in the history
  • Loading branch information
mpiraux committed Mar 11, 2022
1 parent be97687 commit 221cf31
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
4 changes: 4 additions & 0 deletions agents/parse_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ func (a *ParsingAgent) Run(conn *Connection) {
}

sample, pnOffset := GetPacketSample(header, ciphertext)
if sample == nil {
a.Logger.Printf("Packet is too short to for header protection, dropping it\n")
break packetSelect
}
mask := cryptoState.HeaderRead.Encrypt(sample, make([]byte, 5, 5))
ciphertext[0] ^= mask[0] & firstByteMask

Expand Down
5 changes: 5 additions & 0 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ func (c *Connection) EncodeAndEncrypt(packet Packet, level EncryptionLevel) []by
firstByteMask = 0x0F
}
sample, pnOffset := GetPacketSample(packet.Header(), packetBytes)
if sample == nil {
paddedSize := pnOffset + 4
packet.(Framer).PadTo(paddedSize)
return c.EncodeAndEncrypt(packet, level)
}
mask := cryptoState.HeaderWrite.Encrypt(sample, make([]byte, 5, 5))
packetBytes[0] ^= mask[0] & firstByteMask

Expand Down
5 changes: 2 additions & 3 deletions crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,8 @@ func GetPacketSample(header Header, packetBytes []byte) ([]byte, int) {
sampleOffset := pnOffset + 4

if sampleOffset+sampleLength > len(packetBytes) {
paddedBytes := make([]byte, sampleOffset+sampleLength)
copy(paddedBytes, packetBytes)
packetBytes = paddedBytes
// Packet is too short for sampling header protection, it must be padded first
return nil, pnOffset
}

return packetBytes[sampleOffset:sampleOffset+sampleLength], pnOffset
Expand Down

0 comments on commit 221cf31

Please sign in to comment.