Deflake DTLS12-SendExtraFinished-Reordered

The aim of MaxHandshakeRecordLength + ReorderHandshakeFragments was to
make it likely that we received at least one fragment of the extra
Finished before the actual Finished, thus exercising the case where we
detect it before the handshake instead of after.

There was some probability that this didn't happen and we actually
receive the entire correct Finished before learning of the extra
Finished's existence. Now that we have the DTLSController machinery, we
can program in the order we actually want.

Separately, it happened that our DTLS 1.2 implementation returned a
different error depending on whether we detected it before or
afterwards. This is due to a combination of DTLS 1.2's renegotiation
ambiguity, and the very hacky place where we hook in the retransmit
check. If we resolve crbug.com/383016430, we might end up changing that.

Change-Id: Idfe4c8c1247e25aa70ce3ec3560833109cef195a
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/86989
Reviewed-by: Lily Chen <chlily@google.com>
Auto-Submit: David Benjamin <davidben@google.com>
Commit-Queue: Lily Chen <chlily@google.com>
diff --git a/ssl/test/runner/basic_tests.go b/ssl/test/runner/basic_tests.go
index 08de8fa..faeb352 100644
--- a/ssl/test/runner/basic_tests.go
+++ b/ssl/test/runner/basic_tests.go
@@ -14,7 +14,10 @@
 
 package runner
 
-import "strconv"
+import (
+	"slices"
+	"strconv"
+)
 
 func addBasicTests() {
 	basicTests := []testCase{
@@ -1348,9 +1351,17 @@
 			config: Config{
 				MaxVersion: VersionTLS12,
 				Bugs: ProtocolBugs{
-					MaxHandshakeRecordLength:  2,
-					ReorderHandshakeFragments: true,
-					SendExtraFinished:         true,
+					SendExtraFinished: true,
+					WriteFlightDTLS: func(c *DTLSController, prev, received, next []DTLSMessage, records []DTLSRecordNumberInfo) {
+						if next[len(next)-1].Type == typeFinished {
+							// Reorder such that the extra Finished is sent first.
+							if len(next) < 2 || next[len(next)-2].Type != typeFinished {
+								panic("expected two Finished messages")
+							}
+							slices.Reverse(next[len(next)-2:])
+						}
+						c.WriteFlight(next)
+					},
 				},
 			},
 			shouldFail:         true,
@@ -1390,9 +1401,17 @@
 			config: Config{
 				MaxVersion: VersionTLS13,
 				Bugs: ProtocolBugs{
-					MaxHandshakeRecordLength:  2,
-					ReorderHandshakeFragments: true,
-					SendExtraFinished:         true,
+					SendExtraFinished: true,
+					WriteFlightDTLS: func(c *DTLSController, prev, received, next []DTLSMessage, records []DTLSRecordNumberInfo) {
+						if next[len(next)-1].Type == typeFinished {
+							// Reorder such that the extra Finished is sent first.
+							if len(next) < 2 || next[len(next)-2].Type != typeFinished {
+								panic("expected two Finished messages")
+							}
+							slices.Reverse(next[len(next)-2:])
+						}
+						c.WriteFlight(next)
+					},
 				},
 			},
 			shouldFail:         true,