mirror of
https://github.com/refraction-networking/utls.git
synced 2025-04-03 03:57:36 +03:00
To disable TLS 1.3, simply remove VersionTLS13 from supportedVersions, as tested by TestEscapeRoute, and amend documentation. To make it opt-in, revert the change to (*Config).supportedVersions from this CL. I did not have the heart to implement the early data skipping feature when I realized that it did not offer a choice between two abstraction-breaking options, but demanded them both (look for handshake type in case of HelloRetryRequest, trial decryption otherwise). It's a lot of complexity for an apparently small gain, but if anyone has strong opinions about it let me know. Note that in TLS 1.3 alerts are encrypted, so the close_notify peeking to return (n > 0, io.EOF) from Read doesn't work. If we are lucky, those servers that unexpectedly close connections after serving a single request will have stopped (maybe thanks to H/2) before they got updated to TLS 1.3. Relatedly, session tickets are now provisioned on the client first Read instead of at Handshake time, because they are, well, post-handshake messages. If this proves to be a problem we might try to peek at them. Doubled the tests that cover logic that's different in TLS 1.3. The benchmarks for TLS 1.2 compared to be0f3c286b5 (before TLS 1.3 and its refactors, after CL 142817 changed them to use real connections) show little movement. name old time/op new time/op delta HandshakeServer/RSA-8 795µs ± 1% 798µs ± 1% ~ (p=0.057 n=10+18) HandshakeServer/ECDHE-P256-RSA-8 903µs ± 0% 909µs ± 1% +0.68% (p=0.000 n=8+17) HandshakeServer/ECDHE-P256-ECDSA-P256-8 198µs ± 0% 204µs ± 1% +3.24% (p=0.000 n=9+18) HandshakeServer/ECDHE-X25519-ECDSA-P256-8 202µs ± 3% 208µs ± 1% +2.98% (p=0.000 n=9+20) HandshakeServer/ECDHE-P521-ECDSA-P521-8 15.5ms ± 1% 15.9ms ± 2% +2.49% (p=0.000 n=10+20) Throughput/MaxPacket/1MB-8 5.81ms ±23% 6.14ms ±44% ~ (p=0.605 n=8+18) Throughput/MaxPacket/2MB-8 8.91ms ±22% 8.74ms ±33% ~ (p=0.498 n=9+19) Throughput/MaxPacket/4MB-8 12.8ms ± 3% 14.0ms ±10% +9.74% (p=0.000 n=10+17) Throughput/MaxPacket/8MB-8 25.1ms ± 7% 24.6ms ±16% ~ (p=0.129 n=9+19) Throughput/MaxPacket/16MB-8 46.3ms ± 4% 45.9ms ±12% ~ (p=0.340 n=9+20) Throughput/MaxPacket/32MB-8 88.5ms ± 4% 86.0ms ± 4% -2.82% (p=0.004 n=10+20) Throughput/MaxPacket/64MB-8 173ms ± 2% 167ms ± 7% -3.42% (p=0.001 n=10+19) Throughput/DynamicPacket/1MB-8 5.88ms ± 4% 6.59ms ±64% ~ (p=0.232 n=9+18) Throughput/DynamicPacket/2MB-8 9.08ms ±12% 8.73ms ±21% ~ (p=0.408 n=10+18) Throughput/DynamicPacket/4MB-8 14.2ms ± 5% 14.0ms ±11% ~ (p=0.188 n=9+19) Throughput/DynamicPacket/8MB-8 25.1ms ± 6% 24.0ms ± 7% -4.39% (p=0.000 n=10+18) Throughput/DynamicPacket/16MB-8 45.6ms ± 3% 43.3ms ± 1% -5.22% (p=0.000 n=10+8) Throughput/DynamicPacket/32MB-8 88.4ms ± 3% 84.8ms ± 2% -4.06% (p=0.000 n=10+10) Throughput/DynamicPacket/64MB-8 175ms ± 3% 167ms ± 2% -4.63% (p=0.000 n=10+10) Latency/MaxPacket/200kbps-8 694ms ± 0% 694ms ± 0% -0.02% (p=0.000 n=9+9) Latency/MaxPacket/500kbps-8 279ms ± 0% 279ms ± 0% -0.09% (p=0.000 n=10+10) Latency/MaxPacket/1000kbps-8 140ms ± 0% 140ms ± 0% -0.15% (p=0.000 n=10+9) Latency/MaxPacket/2000kbps-8 71.1ms ± 0% 71.0ms ± 0% -0.09% (p=0.001 n=8+9) Latency/MaxPacket/5000kbps-8 30.5ms ± 6% 30.1ms ± 6% ~ (p=0.905 n=10+9) Latency/DynamicPacket/200kbps-8 134ms ± 0% 134ms ± 0% ~ (p=0.796 n=9+9) Latency/DynamicPacket/500kbps-8 54.8ms ± 0% 54.7ms ± 0% -0.18% (p=0.000 n=8+10) Latency/DynamicPacket/1000kbps-8 28.5ms ± 0% 29.1ms ± 8% ~ (p=0.173 n=8+10) Latency/DynamicPacket/2000kbps-8 15.3ms ± 6% 15.9ms ±10% ~ (p=0.905 n=9+10) Latency/DynamicPacket/5000kbps-8 9.14ms ±21% 9.65ms ±82% ~ (p=0.529 n=10+10) name old speed new speed delta Throughput/MaxPacket/1MB-8 175MB/s ±13% 167MB/s ±64% ~ (p=0.646 n=7+20) Throughput/MaxPacket/2MB-8 241MB/s ±25% 241MB/s ±40% ~ (p=0.660 n=9+20) Throughput/MaxPacket/4MB-8 328MB/s ± 3% 300MB/s ± 9% -8.70% (p=0.000 n=10+17) Throughput/MaxPacket/8MB-8 335MB/s ± 7% 340MB/s ±17% ~ (p=0.212 n=9+20) Throughput/MaxPacket/16MB-8 363MB/s ± 4% 367MB/s ±11% ~ (p=0.340 n=9+20) Throughput/MaxPacket/32MB-8 379MB/s ± 4% 390MB/s ± 4% +2.93% (p=0.004 n=10+20) Throughput/MaxPacket/64MB-8 388MB/s ± 2% 401MB/s ± 7% +3.25% (p=0.004 n=10+20) Throughput/DynamicPacket/1MB-8 178MB/s ± 4% 157MB/s ±73% ~ (p=0.127 n=9+20) Throughput/DynamicPacket/2MB-8 232MB/s ±11% 243MB/s ±18% ~ (p=0.415 n=10+18) Throughput/DynamicPacket/4MB-8 296MB/s ± 5% 299MB/s ±15% ~ (p=0.295 n=9+20) Throughput/DynamicPacket/8MB-8 334MB/s ± 6% 350MB/s ± 7% +4.58% (p=0.000 n=10+18) Throughput/DynamicPacket/16MB-8 368MB/s ± 3% 388MB/s ± 1% +5.48% (p=0.000 n=10+8) Throughput/DynamicPacket/32MB-8 380MB/s ± 3% 396MB/s ± 2% +4.20% (p=0.000 n=10+10) Throughput/DynamicPacket/64MB-8 384MB/s ± 3% 403MB/s ± 2% +4.83% (p=0.000 n=10+10) Comparing TLS 1.2 and TLS 1.3 at tip shows a slight (~5-10%) slowdown of handshakes, which might be worth looking at next cycle, but the latency improvements are expected to overshadow that. name old time/op new time/op delta HandshakeServer/ECDHE-P256-RSA-8 909µs ± 1% 963µs ± 0% +5.87% (p=0.000 n=17+18) HandshakeServer/ECDHE-P256-ECDSA-P256-8 204µs ± 1% 225µs ± 2% +10.20% (p=0.000 n=18+20) HandshakeServer/ECDHE-X25519-ECDSA-P256-8 208µs ± 1% 230µs ± 2% +10.35% (p=0.000 n=20+18) HandshakeServer/ECDHE-P521-ECDSA-P521-8 15.9ms ± 2% 15.9ms ± 1% ~ (p=0.444 n=20+19) Throughput/MaxPacket/1MB-8 6.14ms ±44% 7.07ms ±46% ~ (p=0.057 n=18+19) Throughput/MaxPacket/2MB-8 8.74ms ±33% 8.61ms ± 9% ~ (p=0.552 n=19+17) Throughput/MaxPacket/4MB-8 14.0ms ±10% 14.1ms ±12% ~ (p=0.707 n=17+20) Throughput/MaxPacket/8MB-8 24.6ms ±16% 25.6ms ±14% ~ (p=0.107 n=19+20) Throughput/MaxPacket/16MB-8 45.9ms ±12% 44.7ms ± 6% ~ (p=0.607 n=20+19) Throughput/MaxPacket/32MB-8 86.0ms ± 4% 87.9ms ± 8% ~ (p=0.113 n=20+19) Throughput/MaxPacket/64MB-8 167ms ± 7% 169ms ± 2% +1.26% (p=0.011 n=19+19) Throughput/DynamicPacket/1MB-8 6.59ms ±64% 6.79ms ±43% ~ (p=0.480 n=18+19) Throughput/DynamicPacket/2MB-8 8.73ms ±21% 9.58ms ±13% +9.71% (p=0.006 n=18+20) Throughput/DynamicPacket/4MB-8 14.0ms ±11% 13.9ms ±10% ~ (p=0.687 n=19+20) Throughput/DynamicPacket/8MB-8 24.0ms ± 7% 24.6ms ± 8% +2.36% (p=0.045 n=18+17) Throughput/DynamicPacket/16MB-8 43.3ms ± 1% 44.3ms ± 2% +2.48% (p=0.001 n=8+9) Throughput/DynamicPacket/32MB-8 84.8ms ± 2% 86.7ms ± 2% +2.27% (p=0.000 n=10+10) Throughput/DynamicPacket/64MB-8 167ms ± 2% 170ms ± 3% +1.89% (p=0.005 n=10+10) Latency/MaxPacket/200kbps-8 694ms ± 0% 699ms ± 0% +0.65% (p=0.000 n=9+10) Latency/MaxPacket/500kbps-8 279ms ± 0% 280ms ± 0% +0.68% (p=0.000 n=10+10) Latency/MaxPacket/1000kbps-8 140ms ± 0% 141ms ± 0% +0.59% (p=0.000 n=9+9) Latency/MaxPacket/2000kbps-8 71.0ms ± 0% 71.3ms ± 0% +0.42% (p=0.000 n=9+9) Latency/MaxPacket/5000kbps-8 30.1ms ± 6% 30.7ms ±10% +1.93% (p=0.019 n=9+9) Latency/DynamicPacket/200kbps-8 134ms ± 0% 138ms ± 0% +3.22% (p=0.000 n=9+10) Latency/DynamicPacket/500kbps-8 54.7ms ± 0% 56.3ms ± 0% +3.03% (p=0.000 n=10+8) Latency/DynamicPacket/1000kbps-8 29.1ms ± 8% 29.1ms ± 0% ~ (p=0.173 n=10+8) Latency/DynamicPacket/2000kbps-8 15.9ms ±10% 16.4ms ±36% ~ (p=0.633 n=10+8) Latency/DynamicPacket/5000kbps-8 9.65ms ±82% 8.32ms ± 8% ~ (p=0.573 n=10+8) name old speed new speed delta Throughput/MaxPacket/1MB-8 167MB/s ±64% 155MB/s ±55% ~ (p=0.224 n=20+19) Throughput/MaxPacket/2MB-8 241MB/s ±40% 244MB/s ± 9% ~ (p=0.407 n=20+17) Throughput/MaxPacket/4MB-8 300MB/s ± 9% 298MB/s ±11% ~ (p=0.707 n=17+20) Throughput/MaxPacket/8MB-8 340MB/s ±17% 330MB/s ±13% ~ (p=0.201 n=20+20) Throughput/MaxPacket/16MB-8 367MB/s ±11% 375MB/s ± 5% ~ (p=0.607 n=20+19) Throughput/MaxPacket/32MB-8 390MB/s ± 4% 382MB/s ± 8% ~ (p=0.113 n=20+19) Throughput/MaxPacket/64MB-8 401MB/s ± 7% 397MB/s ± 2% -0.96% (p=0.030 n=20+19) Throughput/DynamicPacket/1MB-8 157MB/s ±73% 156MB/s ±39% ~ (p=0.738 n=20+20) Throughput/DynamicPacket/2MB-8 243MB/s ±18% 220MB/s ±14% -9.65% (p=0.006 n=18+20) Throughput/DynamicPacket/4MB-8 299MB/s ±15% 303MB/s ± 9% ~ (p=0.512 n=20+20) Throughput/DynamicPacket/8MB-8 350MB/s ± 7% 342MB/s ± 8% -2.27% (p=0.045 n=18+17) Throughput/DynamicPacket/16MB-8 388MB/s ± 1% 378MB/s ± 2% -2.41% (p=0.001 n=8+9) Throughput/DynamicPacket/32MB-8 396MB/s ± 2% 387MB/s ± 2% -2.21% (p=0.000 n=10+10) Throughput/DynamicPacket/64MB-8 403MB/s ± 2% 396MB/s ± 3% -1.84% (p=0.005 n=10+10) Fixes #9671 Change-Id: Ieb57c5140eb2c083b8be0d42b240cd2eeec0dcf6 Reviewed-on: https://go-review.googlesource.com/c/147638 Run-TryBot: Filippo Valsorda <filippo@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Adam Langley <agl@golang.org>
294 lines
11 KiB
Go
294 lines
11 KiB
Go
// Copyright 2010 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.
|
|
|
|
package tls
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"net"
|
|
"testing"
|
|
)
|
|
|
|
func TestRoundUp(t *testing.T) {
|
|
if roundUp(0, 16) != 0 ||
|
|
roundUp(1, 16) != 16 ||
|
|
roundUp(15, 16) != 16 ||
|
|
roundUp(16, 16) != 16 ||
|
|
roundUp(17, 16) != 32 {
|
|
t.Error("roundUp broken")
|
|
}
|
|
}
|
|
|
|
// will be initialized with {0, 255, 255, ..., 255}
|
|
var padding255Bad = [256]byte{}
|
|
|
|
// will be initialized with {255, 255, 255, ..., 255}
|
|
var padding255Good = [256]byte{255}
|
|
|
|
var paddingTests = []struct {
|
|
in []byte
|
|
good bool
|
|
expectedLen int
|
|
}{
|
|
{[]byte{1, 2, 3, 4, 0}, true, 4},
|
|
{[]byte{1, 2, 3, 4, 0, 1}, false, 0},
|
|
{[]byte{1, 2, 3, 4, 99, 99}, false, 0},
|
|
{[]byte{1, 2, 3, 4, 1, 1}, true, 4},
|
|
{[]byte{1, 2, 3, 2, 2, 2}, true, 3},
|
|
{[]byte{1, 2, 3, 3, 3, 3}, true, 2},
|
|
{[]byte{1, 2, 3, 4, 3, 3}, false, 0},
|
|
{[]byte{1, 4, 4, 4, 4, 4}, true, 1},
|
|
{[]byte{5, 5, 5, 5, 5, 5}, true, 0},
|
|
{[]byte{6, 6, 6, 6, 6, 6}, false, 0},
|
|
{padding255Bad[:], false, 0},
|
|
{padding255Good[:], true, 0},
|
|
}
|
|
|
|
func TestRemovePadding(t *testing.T) {
|
|
for i := 1; i < len(padding255Bad); i++ {
|
|
padding255Bad[i] = 255
|
|
padding255Good[i] = 255
|
|
}
|
|
for i, test := range paddingTests {
|
|
paddingLen, good := extractPadding(test.in)
|
|
expectedGood := byte(255)
|
|
if !test.good {
|
|
expectedGood = 0
|
|
}
|
|
if good != expectedGood {
|
|
t.Errorf("#%d: wrong validity, want:%d got:%d", i, expectedGood, good)
|
|
}
|
|
if good == 255 && len(test.in)-paddingLen != test.expectedLen {
|
|
t.Errorf("#%d: got %d, want %d", i, len(test.in)-paddingLen, test.expectedLen)
|
|
}
|
|
}
|
|
}
|
|
|
|
var certExampleCom = `308201713082011ba003020102021005a75ddf21014d5f417083b7a010ba2e300d06092a864886f70d01010b050030123110300e060355040a130741636d6520436f301e170d3136303831373231343135335a170d3137303831373231343135335a30123110300e060355040a130741636d6520436f305c300d06092a864886f70d0101010500034b003048024100b37f0fdd67e715bf532046ac34acbd8fdc4dabe2b598588f3f58b1f12e6219a16cbfe54d2b4b665396013589262360b6721efa27d546854f17cc9aeec6751db10203010001a34d304b300e0603551d0f0101ff0404030205a030130603551d25040c300a06082b06010505070301300c0603551d130101ff0402300030160603551d11040f300d820b6578616d706c652e636f6d300d06092a864886f70d01010b050003410059fc487866d3d855503c8e064ca32aac5e9babcece89ec597f8b2b24c17867f4a5d3b4ece06e795bfc5448ccbd2ffca1b3433171ebf3557a4737b020565350a0`
|
|
|
|
var certWildcardExampleCom = `308201743082011ea003020102021100a7aa6297c9416a4633af8bec2958c607300d06092a864886f70d01010b050030123110300e060355040a130741636d6520436f301e170d3136303831373231343231395a170d3137303831373231343231395a30123110300e060355040a130741636d6520436f305c300d06092a864886f70d0101010500034b003048024100b105afc859a711ee864114e7d2d46c2dcbe392d3506249f6c2285b0eb342cc4bf2d803677c61c0abde443f084745c1a6d62080e5664ef2cc8f50ad8a0ab8870b0203010001a34f304d300e0603551d0f0101ff0404030205a030130603551d25040c300a06082b06010505070301300c0603551d130101ff0402300030180603551d110411300f820d2a2e6578616d706c652e636f6d300d06092a864886f70d01010b0500034100af26088584d266e3f6566360cf862c7fecc441484b098b107439543144a2b93f20781988281e108c6d7656934e56950e1e5f2bcf38796b814ccb729445856c34`
|
|
|
|
var certFooExampleCom = `308201753082011fa00302010202101bbdb6070b0aeffc49008cde74deef29300d06092a864886f70d01010b050030123110300e060355040a130741636d6520436f301e170d3136303831373231343234345a170d3137303831373231343234345a30123110300e060355040a130741636d6520436f305c300d06092a864886f70d0101010500034b003048024100f00ac69d8ca2829f26216c7b50f1d4bbabad58d447706476cd89a2f3e1859943748aa42c15eedc93ac7c49e40d3b05ed645cb6b81c4efba60d961f44211a54eb0203010001a351304f300e0603551d0f0101ff0404030205a030130603551d25040c300a06082b06010505070301300c0603551d130101ff04023000301a0603551d1104133011820f666f6f2e6578616d706c652e636f6d300d06092a864886f70d01010b0500034100a0957fca6d1e0f1ef4b247348c7a8ca092c29c9c0ecc1898ea6b8065d23af6d922a410dd2335a0ea15edd1394cef9f62c9e876a21e35250a0b4fe1ddceba0f36`
|
|
|
|
var certDoubleWildcardExampleCom = `308201753082011fa003020102021039d262d8538db8ffba30d204e02ddeb5300d06092a864886f70d01010b050030123110300e060355040a130741636d6520436f301e170d3136303831373231343331335a170d3137303831373231343331335a30123110300e060355040a130741636d6520436f305c300d06092a864886f70d0101010500034b003048024100abb6bd84b8b9be3fb9415d00f22b4ddcaec7c99855b9d818c09003e084578430e5cfd2e35faa3561f036d496aa43a9ca6e6cf23c72a763c04ae324004f6cbdbb0203010001a351304f300e0603551d0f0101ff0404030205a030130603551d25040c300a06082b06010505070301300c0603551d130101ff04023000301a0603551d1104133011820f2a2e2a2e6578616d706c652e636f6d300d06092a864886f70d01010b05000341004837521004a5b6bc7ad5d6c0dae60bb7ee0fa5e4825be35e2bb6ef07ee29396ca30ceb289431bcfd363888ba2207139933ac7c6369fa8810c819b2e2966abb4b`
|
|
|
|
func TestCertificateSelection(t *testing.T) {
|
|
config := Config{
|
|
Certificates: []Certificate{
|
|
{
|
|
Certificate: [][]byte{fromHex(certExampleCom)},
|
|
},
|
|
{
|
|
Certificate: [][]byte{fromHex(certWildcardExampleCom)},
|
|
},
|
|
{
|
|
Certificate: [][]byte{fromHex(certFooExampleCom)},
|
|
},
|
|
{
|
|
Certificate: [][]byte{fromHex(certDoubleWildcardExampleCom)},
|
|
},
|
|
},
|
|
}
|
|
|
|
config.BuildNameToCertificate()
|
|
|
|
pointerToIndex := func(c *Certificate) int {
|
|
for i := range config.Certificates {
|
|
if c == &config.Certificates[i] {
|
|
return i
|
|
}
|
|
}
|
|
return -1
|
|
}
|
|
|
|
certificateForName := func(name string) *Certificate {
|
|
clientHello := &ClientHelloInfo{
|
|
ServerName: name,
|
|
}
|
|
if cert, err := config.getCertificate(clientHello); err != nil {
|
|
t.Errorf("unable to get certificate for name '%s': %s", name, err)
|
|
return nil
|
|
} else {
|
|
return cert
|
|
}
|
|
}
|
|
|
|
if n := pointerToIndex(certificateForName("example.com")); n != 0 {
|
|
t.Errorf("example.com returned certificate %d, not 0", n)
|
|
}
|
|
if n := pointerToIndex(certificateForName("bar.example.com")); n != 1 {
|
|
t.Errorf("bar.example.com returned certificate %d, not 1", n)
|
|
}
|
|
if n := pointerToIndex(certificateForName("foo.example.com")); n != 2 {
|
|
t.Errorf("foo.example.com returned certificate %d, not 2", n)
|
|
}
|
|
if n := pointerToIndex(certificateForName("foo.bar.example.com")); n != 3 {
|
|
t.Errorf("foo.bar.example.com returned certificate %d, not 3", n)
|
|
}
|
|
if n := pointerToIndex(certificateForName("foo.bar.baz.example.com")); n != 0 {
|
|
t.Errorf("foo.bar.baz.example.com returned certificate %d, not 0", n)
|
|
}
|
|
}
|
|
|
|
// Run with multiple crypto configs to test the logic for computing TLS record overheads.
|
|
func runDynamicRecordSizingTest(t *testing.T, config *Config) {
|
|
clientConn, serverConn := localPipe(t)
|
|
|
|
serverConfig := config.Clone()
|
|
serverConfig.DynamicRecordSizingDisabled = false
|
|
tlsConn := Server(serverConn, serverConfig)
|
|
|
|
handshakeDone := make(chan struct{})
|
|
recordSizesChan := make(chan []int, 1)
|
|
go func() {
|
|
// This goroutine performs a TLS handshake over clientConn and
|
|
// then reads TLS records until EOF. It writes a slice that
|
|
// contains all the record sizes to recordSizesChan.
|
|
defer close(recordSizesChan)
|
|
defer clientConn.Close()
|
|
|
|
tlsConn := Client(clientConn, config)
|
|
if err := tlsConn.Handshake(); err != nil {
|
|
t.Errorf("Error from client handshake: %v", err)
|
|
return
|
|
}
|
|
close(handshakeDone)
|
|
|
|
var recordHeader [recordHeaderLen]byte
|
|
var record []byte
|
|
var recordSizes []int
|
|
|
|
for {
|
|
n, err := io.ReadFull(clientConn, recordHeader[:])
|
|
if err == io.EOF {
|
|
break
|
|
}
|
|
if err != nil || n != len(recordHeader) {
|
|
t.Errorf("io.ReadFull = %d, %v", n, err)
|
|
return
|
|
}
|
|
|
|
length := int(recordHeader[3])<<8 | int(recordHeader[4])
|
|
if len(record) < length {
|
|
record = make([]byte, length)
|
|
}
|
|
|
|
n, err = io.ReadFull(clientConn, record[:length])
|
|
if err != nil || n != length {
|
|
t.Errorf("io.ReadFull = %d, %v", n, err)
|
|
return
|
|
}
|
|
|
|
recordSizes = append(recordSizes, recordHeaderLen+length)
|
|
}
|
|
|
|
recordSizesChan <- recordSizes
|
|
}()
|
|
|
|
if err := tlsConn.Handshake(); err != nil {
|
|
t.Fatalf("Error from server handshake: %s", err)
|
|
}
|
|
<-handshakeDone
|
|
|
|
// The server writes these plaintexts in order.
|
|
plaintext := bytes.Join([][]byte{
|
|
bytes.Repeat([]byte("x"), recordSizeBoostThreshold),
|
|
bytes.Repeat([]byte("y"), maxPlaintext*2),
|
|
bytes.Repeat([]byte("z"), maxPlaintext),
|
|
}, nil)
|
|
|
|
if _, err := tlsConn.Write(plaintext); err != nil {
|
|
t.Fatalf("Error from server write: %s", err)
|
|
}
|
|
if err := tlsConn.Close(); err != nil {
|
|
t.Fatalf("Error from server close: %s", err)
|
|
}
|
|
|
|
recordSizes := <-recordSizesChan
|
|
if recordSizes == nil {
|
|
t.Fatalf("Client encountered an error")
|
|
}
|
|
|
|
// Drop the size of the second to last record, which is likely to be
|
|
// truncated, and the last record, which is a close_notify alert.
|
|
recordSizes = recordSizes[:len(recordSizes)-2]
|
|
|
|
// recordSizes should contain a series of records smaller than
|
|
// tcpMSSEstimate followed by some larger than maxPlaintext.
|
|
seenLargeRecord := false
|
|
for i, size := range recordSizes {
|
|
if !seenLargeRecord {
|
|
if size > (i+1)*tcpMSSEstimate {
|
|
t.Fatalf("Record #%d has size %d, which is too large too soon", i, size)
|
|
}
|
|
if size >= maxPlaintext {
|
|
seenLargeRecord = true
|
|
}
|
|
} else if size <= maxPlaintext {
|
|
t.Fatalf("Record #%d has size %d but should be full sized", i, size)
|
|
}
|
|
}
|
|
|
|
if !seenLargeRecord {
|
|
t.Fatalf("No large records observed")
|
|
}
|
|
}
|
|
|
|
func TestDynamicRecordSizingWithStreamCipher(t *testing.T) {
|
|
config := testConfig.Clone()
|
|
config.MaxVersion = VersionTLS12
|
|
config.CipherSuites = []uint16{TLS_RSA_WITH_RC4_128_SHA}
|
|
runDynamicRecordSizingTest(t, config)
|
|
}
|
|
|
|
func TestDynamicRecordSizingWithCBC(t *testing.T) {
|
|
config := testConfig.Clone()
|
|
config.MaxVersion = VersionTLS12
|
|
config.CipherSuites = []uint16{TLS_RSA_WITH_AES_256_CBC_SHA}
|
|
runDynamicRecordSizingTest(t, config)
|
|
}
|
|
|
|
func TestDynamicRecordSizingWithAEAD(t *testing.T) {
|
|
config := testConfig.Clone()
|
|
config.MaxVersion = VersionTLS12
|
|
config.CipherSuites = []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256}
|
|
runDynamicRecordSizingTest(t, config)
|
|
}
|
|
|
|
func TestDynamicRecordSizingWithTLSv13(t *testing.T) {
|
|
config := testConfig.Clone()
|
|
runDynamicRecordSizingTest(t, config)
|
|
}
|
|
|
|
// hairpinConn is a net.Conn that makes a “hairpin” call when closed, back into
|
|
// the tls.Conn which is calling it.
|
|
type hairpinConn struct {
|
|
net.Conn
|
|
tlsConn *Conn
|
|
}
|
|
|
|
func (conn *hairpinConn) Close() error {
|
|
conn.tlsConn.ConnectionState()
|
|
return nil
|
|
}
|
|
|
|
func TestHairpinInClose(t *testing.T) {
|
|
// This tests that the underlying net.Conn can call back into the
|
|
// tls.Conn when being closed without deadlocking.
|
|
client, server := localPipe(t)
|
|
defer server.Close()
|
|
defer client.Close()
|
|
|
|
conn := &hairpinConn{client, nil}
|
|
tlsConn := Server(conn, &Config{
|
|
GetCertificate: func(*ClientHelloInfo) (*Certificate, error) {
|
|
panic("unreachable")
|
|
},
|
|
})
|
|
conn.tlsConn = tlsConn
|
|
|
|
// This call should not deadlock.
|
|
tlsConn.Close()
|
|
}
|