forked from golang/net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkey_update_test.go
234 lines (209 loc) · 7.85 KB
/
key_update_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build go1.21
package quic
import (
"testing"
)
func TestKeyUpdatePeerUpdates(t *testing.T) {
tc := newTestConn(t, serverSide)
tc.handshake()
tc.ignoreFrames = nil // ignore nothing
// Peer initiates a key update.
tc.sendKeyNumber = 1
tc.sendKeyPhaseBit = true
tc.writeFrames(packetType1RTT, debugFramePing{})
// We update to the new key.
tc.advanceToTimer()
tc.wantFrameType("conn ACKs last packet",
packetType1RTT, debugFrameAck{})
tc.wantFrame("first packet after a key update is always ack-eliciting",
packetType1RTT, debugFramePing{})
if got, want := tc.lastPacket.keyNumber, 1; got != want {
t.Errorf("after key rotation, conn sent packet with key %v, want %v", got, want)
}
if !tc.lastPacket.keyPhaseBit {
t.Errorf("after key rotation, conn failed to change Key Phase bit")
}
tc.wantIdle("conn has nothing to send")
// Peer's ACK of a packet we sent in the new phase completes the update.
tc.writeAckForAll()
// Peer initiates a second key update.
tc.sendKeyNumber = 2
tc.sendKeyPhaseBit = false
tc.writeFrames(packetType1RTT, debugFramePing{})
// We update to the new key.
tc.advanceToTimer()
tc.wantFrameType("conn ACKs last packet",
packetType1RTT, debugFrameAck{})
tc.wantFrame("first packet after a key update is always ack-eliciting",
packetType1RTT, debugFramePing{})
if got, want := tc.lastPacket.keyNumber, 2; got != want {
t.Errorf("after key rotation, conn sent packet with key %v, want %v", got, want)
}
if tc.lastPacket.keyPhaseBit {
t.Errorf("after second key rotation, conn failed to change Key Phase bit")
}
tc.wantIdle("conn has nothing to send")
}
func TestKeyUpdateAcceptPreviousPhaseKeys(t *testing.T) {
// "An endpoint SHOULD retain old keys for some time after
// unprotecting a packet sent using the new keys."
// https://www.rfc-editor.org/rfc/rfc9001#section-6.1-8
tc := newTestConn(t, serverSide)
tc.handshake()
tc.ignoreFrames = nil // ignore nothing
// Peer initiates a key update, skipping one packet number.
pnum0 := tc.peerNextPacketNum[appDataSpace]
tc.peerNextPacketNum[appDataSpace]++
tc.sendKeyNumber = 1
tc.sendKeyPhaseBit = true
tc.writeFrames(packetType1RTT, debugFramePing{})
// We update to the new key.
// This ACK is not delayed, because we've skipped a packet number.
tc.wantFrame("conn ACKs last packet",
packetType1RTT, debugFrameAck{
ranges: []i64range[packetNumber]{
{0, pnum0},
{pnum0 + 1, pnum0 + 2},
},
})
tc.wantFrame("first packet after a key update is always ack-eliciting",
packetType1RTT, debugFramePing{})
if got, want := tc.lastPacket.keyNumber, 1; got != want {
t.Errorf("after key rotation, conn sent packet with key %v, want %v", got, want)
}
if !tc.lastPacket.keyPhaseBit {
t.Errorf("after key rotation, conn failed to change Key Phase bit")
}
tc.wantIdle("conn has nothing to send")
// We receive the previously-skipped packet in the earlier key phase.
tc.peerNextPacketNum[appDataSpace] = pnum0
tc.sendKeyNumber = 0
tc.sendKeyPhaseBit = false
tc.writeFrames(packetType1RTT, debugFramePing{})
// We ack the reordered packet immediately, still in the new key phase.
tc.wantFrame("conn ACKs reordered packet",
packetType1RTT, debugFrameAck{
ranges: []i64range[packetNumber]{
{0, pnum0 + 2},
},
})
tc.wantIdle("packet is not ack-eliciting")
if got, want := tc.lastPacket.keyNumber, 1; got != want {
t.Errorf("after key rotation, conn sent packet with key %v, want %v", got, want)
}
if !tc.lastPacket.keyPhaseBit {
t.Errorf("after key rotation, conn failed to change Key Phase bit")
}
}
func TestKeyUpdateRejectPacketFromPriorPhase(t *testing.T) {
// "Packets with higher packet numbers MUST be protected with either
// the same or newer packet protection keys than packets with lower packet numbers."
// https://www.rfc-editor.org/rfc/rfc9001#section-6.4-2
tc := newTestConn(t, serverSide)
tc.handshake()
tc.ignoreFrames = nil // ignore nothing
// Peer initiates a key update.
tc.sendKeyNumber = 1
tc.sendKeyPhaseBit = true
tc.writeFrames(packetType1RTT, debugFramePing{})
// We update to the new key.
tc.advanceToTimer()
tc.wantFrameType("conn ACKs last packet",
packetType1RTT, debugFrameAck{})
tc.wantFrame("first packet after a key update is always ack-eliciting",
packetType1RTT, debugFramePing{})
if got, want := tc.lastPacket.keyNumber, 1; got != want {
t.Errorf("after key rotation, conn sent packet with key %v, want %v", got, want)
}
if !tc.lastPacket.keyPhaseBit {
t.Errorf("after key rotation, conn failed to change Key Phase bit")
}
tc.wantIdle("conn has nothing to send")
// Peer sends an ack-eliciting packet using the prior phase keys.
// We fail to unprotect the packet and ignore it.
skipped := tc.peerNextPacketNum[appDataSpace]
tc.sendKeyNumber = 0
tc.sendKeyPhaseBit = false
tc.writeFrames(packetType1RTT, debugFramePing{})
// Peer sends an ack-eliciting packet using the current phase keys.
tc.sendKeyNumber = 1
tc.sendKeyPhaseBit = true
tc.writeFrames(packetType1RTT, debugFramePing{})
// We ack the peer's packets, not including the one sent with the wrong keys.
tc.wantFrame("conn ACKs packets, not including packet sent with wrong keys",
packetType1RTT, debugFrameAck{
ranges: []i64range[packetNumber]{
{0, skipped},
{skipped + 1, skipped + 2},
},
})
}
func TestKeyUpdateLocallyInitiated(t *testing.T) {
const updateAfter = 4 // initiate key update after 1-RTT packet 4
tc := newTestConn(t, serverSide)
tc.conn.keysAppData.updateAfter = updateAfter
tc.handshake()
for {
tc.writeFrames(packetType1RTT, debugFramePing{})
tc.advanceToTimer()
tc.wantFrameType("conn ACKs last packet",
packetType1RTT, debugFrameAck{})
if tc.lastPacket.num > updateAfter {
break
}
if got, want := tc.lastPacket.keyNumber, 0; got != want {
t.Errorf("before key update, conn sent packet with key %v, want %v", got, want)
}
if tc.lastPacket.keyPhaseBit {
t.Errorf("before key update, keyPhaseBit is set, want unset")
}
}
if got, want := tc.lastPacket.keyNumber, 1; got != want {
t.Errorf("after key update, conn sent packet with key %v, want %v", got, want)
}
if !tc.lastPacket.keyPhaseBit {
t.Errorf("after key update, keyPhaseBit is unset, want set")
}
tc.wantFrame("first packet after a key update is always ack-eliciting",
packetType1RTT, debugFramePing{})
tc.wantIdle("no more frames")
// Peer sends another packet using the prior phase keys.
tc.writeFrames(packetType1RTT, debugFramePing{})
tc.advanceToTimer()
tc.wantFrameType("conn ACKs packet in prior phase",
packetType1RTT, debugFrameAck{})
tc.wantIdle("packet is not ack-eliciting")
if got, want := tc.lastPacket.keyNumber, 1; got != want {
t.Errorf("after key update, conn sent packet with key %v, want %v", got, want)
}
// Peer updates to the next phase.
tc.sendKeyNumber = 1
tc.sendKeyPhaseBit = true
tc.writeAckForAll()
tc.writeFrames(packetType1RTT, debugFramePing{})
tc.advanceToTimer()
tc.wantFrameType("conn ACKs packet in current phase",
packetType1RTT, debugFrameAck{})
tc.wantIdle("packet is not ack-eliciting")
if got, want := tc.lastPacket.keyNumber, 1; got != want {
t.Errorf("after key update, conn sent packet with key %v, want %v", got, want)
}
// Peer initiates its own update.
tc.sendKeyNumber = 2
tc.sendKeyPhaseBit = false
tc.writeFrames(packetType1RTT, debugFramePing{})
tc.advanceToTimer()
tc.wantFrameType("conn ACKs packet in current phase",
packetType1RTT, debugFrameAck{})
tc.wantFrame("first packet after a key update is always ack-eliciting",
packetType1RTT, debugFramePing{})
if got, want := tc.lastPacket.keyNumber, 2; got != want {
t.Errorf("after peer key update, conn sent packet with key %v, want %v", got, want)
}
if tc.lastPacket.keyPhaseBit {
t.Errorf("after peer key update, keyPhaseBit is unset, want set")
}
}