Skip to content

Commit f8e8e1e

Browse files
committed
Pad PQ queries sent over TCP
Fixes #3376
1 parent 71c139a commit f8e8e1e

2 files changed

Lines changed: 42 additions & 26 deletions

File tree

‎dnscrypt-proxy/pq.go‎

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const (
2626
PQKdfID = 0x01
2727
PQAeadID = 0x01
2828
PQResumedPaddingFloor = 256
29+
PQQueryOverhead = PQClientMagicLen + PQXWingCiphertextSize + HalfNonceSize + TagSize
2930
)
3031

3132
var (
@@ -111,12 +112,11 @@ func pqResumedSharedKey(resumeSecret [32]byte, clientMagic [8]byte, clientNonce,
111112
return key
112113
}
113114

114-
// pqPad applies ISO/IEC 7816-4 padding to the next multiple of 64, with a
115-
// minimum floor (itself a multiple of 64).
115+
// pqPad applies ISO/IEC 7816-4 padding up to floor bytes, or to the next multiple of 64 if the packet does not fit.
116116
func pqPad(packet []byte, floor int) []byte {
117-
target := (len(packet) + 1 + 63) &^ 63
118-
if target < floor {
119-
target = floor
117+
target := floor
118+
if len(packet) >= floor {
119+
target = (len(packet) + 1 + 63) &^ 63
120120
}
121121
padded := make([]byte, target)
122122
copy(padded, packet)
@@ -221,19 +221,6 @@ func pqUDPPacketLimit(serverInfo *ServerInfo) int {
221221
return MaxDNSUDPPacketSize
222222
}
223223

224-
// pqResumedPaddingFloor returns the padding floor for a resumed query. Over
225-
// UDP, the encrypted response can never be larger than the query, so the
226-
// query is grown to the regular UDP question size target, leaving the
227-
// resolver the same response budget as a classic query.
228-
func (proxy *Proxy) pqResumedPaddingFloor(serverInfo *ServerInfo, ticketLen int, proto string) int {
229-
if proto != "udp" {
230-
return PQResumedPaddingFloor
231-
}
232-
overhead := pqResumedOverhead(ticketLen)
233-
want := proxy.questionSizeEstimator.MinQuestionSize() - overhead
234-
return Max(PQResumedPaddingFloor, Min((want+63)&^63, (pqUDPPacketLimit(serverInfo)-overhead)&^63))
235-
}
236-
237224
// encryptPQ builds a PQ query: a resumed query when a valid ticket is held,
238225
// otherwise a query that carries an X-Wing ciphertext, reusing the cached
239226
// encapsulation when one is available for the current network epoch.
@@ -252,16 +239,22 @@ func (proxy *Proxy) encryptPQ(
252239

253240
if ticket, resumeSecret, ok := serverInfo.pqSession.get(queryEpoch); ok {
254241
key := pqResumedSharedKey(resumeSecret, serverInfo.MagicQuery, clientNonce, ticket)
255-
padded := pqPad(packet, proxy.pqResumedPaddingFloor(serverInfo, len(ticket), proto))
256-
ct := xsecretbox.Seal(nil, nonce, padded, key[:])
257-
out := make([]byte, 0, len(PQResumeMagic)+2+len(ticket)+HalfNonceSize+len(ct))
242+
overhead := pqResumedOverhead(len(ticket))
243+
floor := Max(PQResumedPaddingFloor, minRelayedQuerySize(serverInfo, proto)-overhead)
244+
if proto == "udp" {
245+
// A UDP response can't be larger than its query, so this one gets the same size target as a classic query.
246+
want := proxy.questionSizeEstimator.MinQuestionSize() - overhead
247+
floor = Max(PQResumedPaddingFloor, Min((want+63)&^63, (pqUDPPacketLimit(serverInfo)-overhead)&^63))
248+
}
249+
padded := pqPad(packet, floor)
250+
out := make([]byte, 0, overhead+len(padded))
258251
out = append(out, PQResumeMagic[:]...)
259252
var tl [2]byte
260253
binary.BigEndian.PutUint16(tl[:], uint16(len(ticket)))
261254
out = append(out, tl[:]...)
262255
out = append(out, ticket...)
263256
out = append(out, clientNonce...)
264-
out = append(out, ct...)
257+
out = xsecretbox.Seal(out, nonce, padded, key[:])
265258
return &key, out, clientNonce, queryEpoch, nil
266259
}
267260

@@ -274,13 +267,12 @@ func (proxy *Proxy) encryptPQ(
274267
key = pqDeriveSharedKey(kemSS, serverInfo.MagicQuery, serverInfo.PqCertContext, ctKem)
275268
serverInfo.pqSession.storeEncapsulation(ctKem, key, queryEpoch)
276269
}
277-
padded := pqPad(packet, 64)
278-
ct := xsecretbox.Seal(nil, nonce, padded, key[:])
279-
out := make([]byte, 0, PQClientMagicLen+len(ctKem)+HalfNonceSize+len(ct))
270+
padded := pqPad(packet, Max(64, minRelayedQuerySize(serverInfo, proto)-PQQueryOverhead))
271+
out := make([]byte, 0, PQQueryOverhead+len(padded))
280272
out = append(out, serverInfo.MagicQuery[:]...)
281273
out = append(out, ctKem...)
282274
out = append(out, clientNonce...)
283-
out = append(out, ct...)
275+
out = xsecretbox.Seal(out, nonce, padded, key[:])
284276
return &key, out, clientNonce, queryEpoch, nil
285277
}
286278

‎dnscrypt-proxy/pq_test.go‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,30 @@ func TestPQResumedQueryMeetsUDPSizeTarget(t *testing.T) {
252252
}
253253
}
254254

255+
// A relay drops responses larger than the query, so PQ queries sent to it over TCP must use the maximum size.
256+
func TestPQRelayedTCPQueryLeavesRoomForResponse(t *testing.T) {
257+
proxy := newTestPQProxy(1)
258+
serverInfo := newTestPQServerInfo()
259+
serverInfo.Relay = &Relay{Dnscrypt: &DNSCryptRelay{}}
260+
query := mustHex("12340100000100000000000003777777076578616d706c6503636f6d0000010001")
261+
262+
// Over UDP, a truncated answer triggers a TCP retry, so the query keeps its regular size.
263+
if _, out, _, _, err := proxy.Encrypt(serverInfo, query, "udp"); err != nil || len(out) != PQQueryOverhead+64 {
264+
t.Fatalf("relayed UDP query is %d bytes (%v)", len(out), err)
265+
}
266+
267+
_, out, _, _, err := proxy.Encrypt(serverInfo, query, "tcp")
268+
if err != nil || len(out) != MaxDNSPacketSize {
269+
t.Fatalf("relayed TCP query is %d bytes (%v)", len(out), err)
270+
}
271+
272+
serverInfo.pqSession.store(iotaBytes(130, 0x24), [32]byte{42}, time.Now().Add(time.Minute), 1)
273+
_, out, _, _, err = proxy.Encrypt(serverInfo, query, "tcp")
274+
if err != nil || !bytes.HasPrefix(out, PQResumeMagic[:]) || len(out) != MaxDNSPacketSize {
275+
t.Fatalf("relayed resumed TCP query is %d bytes (%v)", len(out), err)
276+
}
277+
}
278+
255279
func TestPQProcessControlDiscardsTicketAfterNetworkChange(t *testing.T) {
256280
proxy := newTestPQProxy(2)
257281
serverInfo := newTestPQServerInfo()

0 commit comments

Comments
 (0)