| 1 |
package mint |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"testing" |
| 6 |
) |
| 7 |
|
| 8 |
func TestVersionNegotiation(t *testing.T) { |
| 9 |
|
| 10 |
ok, negotiated := VersionNegotiation([]uint16{0x0301, 0x7f12}, []uint16{0x0302, 0x7f12}) |
| 11 |
assertEquals(t, ok, true) |
| 12 |
assertEquals(t, negotiated, uint16(0x7f12)) |
| 13 |
|
| 14 |
|
| 15 |
ok, negotiated = VersionNegotiation([]uint16{0x0300}, []uint16{0x0400}) |
| 16 |
assertEquals(t, ok, false) |
| 17 |
} |
| 18 |
|
| 19 |
func TestDHNegotiation(t *testing.T) { |
| 20 |
keyShares := []KeyShareEntry{ |
| 21 |
{Group: P256, KeyExchange: random(keyExchangeSizeFromNamedGroup(P256))}, |
| 22 |
{Group: X25519, KeyExchange: random(keyExchangeSizeFromNamedGroup(X25519))}, |
| 23 |
} |
| 24 |
badKeyShares := []KeyShareEntry{ |
| 25 |
{Group: P256, KeyExchange: random(keyExchangeSizeFromNamedGroup(P256) - 2)}, |
| 26 |
{Group: X25519, KeyExchange: random(keyExchangeSizeFromNamedGroup(X25519))}, |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
ok, group, pub, secret := DHNegotiation(keyShares, []NamedGroup{X25519}) |
| 31 |
assertEquals(t, ok, true) |
| 32 |
assertEquals(t, group, X25519) |
| 33 |
assertNotNil(t, pub, "Nil public key") |
| 34 |
assertNotNil(t, secret, "Nil DH secret") |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
originalPRNG := prng |
| 40 |
prng = bytes.NewBuffer(nil) |
| 41 |
ok, group, pub, secret = DHNegotiation(badKeyShares, []NamedGroup{P256, X25519}) |
| 42 |
assertEquals(t, ok, false) |
| 43 |
prng = originalPRNG |
| 44 |
|
| 45 |
|
| 46 |
ok, group, pub, secret = DHNegotiation(badKeyShares, []NamedGroup{P256, X25519}) |
| 47 |
assertEquals(t, ok, true) |
| 48 |
assertEquals(t, group, X25519) |
| 49 |
assertNotNil(t, pub, "Nil public key") |
| 50 |
assertNotNil(t, secret, "Nil DH secret") |
| 51 |
|
| 52 |
|
| 53 |
ok, _, _, _ = DHNegotiation(keyShares, []NamedGroup{P521}) |
| 54 |
assertEquals(t, ok, false) |
| 55 |
} |
| 56 |
|
| 57 |
func TestPSKNegotiation(t *testing.T) { |
| 58 |
chTrunc := unhex("0001020304050607") |
| 59 |
binderValue := unhex("13a468af471adc19b94dcc0b888135423a11911f2c13050238b579d0f19d41c9") |
| 60 |
|
| 61 |
identities := []PSKIdentity{ |
| 62 |
{Identity: []byte{0, 1, 2, 3}}, |
| 63 |
{Identity: []byte{4, 5, 6, 7}}, |
| 64 |
} |
| 65 |
binders := []PSKBinderEntry{ |
| 66 |
{Binder: binderValue}, |
| 67 |
{Binder: binderValue}, |
| 68 |
} |
| 69 |
badBinders := []PSKBinderEntry{ |
| 70 |
{Binder: []byte{}}, |
| 71 |
{Binder: []byte{}}, |
| 72 |
} |
| 73 |
psks := &PSKMapCache{ |
| 74 |
"04050607": { |
| 75 |
CipherSuite: TLS_AES_128_GCM_SHA256, |
| 76 |
Identity: []byte{4, 5, 6, 7}, |
| 77 |
Key: []byte{0, 1, 2, 3}, |
| 78 |
}, |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
ok, selected, psk, params, err := PSKNegotiation(identities, binders, chTrunc, psks) |
| 83 |
assertEquals(t, ok, true) |
| 84 |
assertEquals(t, selected, 1) |
| 85 |
assertNotNil(t, psk, "PSK not set") |
| 86 |
assertEquals(t, params.Suite, psk.CipherSuite) |
| 87 |
assertNotError(t, err, "Valid PSK negotiation failed") |
| 88 |
|
| 89 |
|
| 90 |
ok, _, _, _, err = PSKNegotiation(identities, badBinders, chTrunc, psks) |
| 91 |
assertEquals(t, ok, false) |
| 92 |
assertError(t, err, "Failed to error on binder failure") |
| 93 |
|
| 94 |
|
| 95 |
ok, _, _, _, err = PSKNegotiation(identities, binders, chTrunc, &PSKMapCache{}) |
| 96 |
assertEquals(t, ok, false) |
| 97 |
assertNotError(t, err, "Errored on PSK negotiation failure") |
| 98 |
} |
| 99 |
|
| 100 |
func TestPSKModeNegotiation(t *testing.T) { |
| 101 |
|
| 102 |
usingDH, usingPSK := PSKModeNegotiation(true, true, []PSKKeyExchangeMode{PSKModeKE, PSKModeDHEKE}) |
| 103 |
assertTrue(t, usingDH, "Unnecessarily disabled DH") |
| 104 |
assertTrue(t, usingPSK, "Unnecessarily disabled PSK") |
| 105 |
|
| 106 |
|
| 107 |
usingDH, usingPSK = PSKModeNegotiation(true, true, []PSKKeyExchangeMode{PSKModeKE}) |
| 108 |
assertTrue(t, !usingDH, "Should not have enabled DH") |
| 109 |
assertTrue(t, usingPSK, "Unnecessarily disabled PSK") |
| 110 |
|
| 111 |
|
| 112 |
usingDH, usingPSK = PSKModeNegotiation(false, true, []PSKKeyExchangeMode{PSKModeDHEKE}) |
| 113 |
assertTrue(t, !usingDH, "Should not have enabled DH") |
| 114 |
assertTrue(t, !usingPSK, "Should not have enabled PSK") |
| 115 |
} |
| 116 |
|
| 117 |
func TestCertificateSelection(t *testing.T) { |
| 118 |
goodName := "example.com" |
| 119 |
badName := "not-example.com" |
| 120 |
rsa := []SignatureScheme{ECDSA_P256_SHA256} |
| 121 |
eddsa := []SignatureScheme{Ed25519} |
| 122 |
|
| 123 |
|
| 124 |
cert, scheme, err := CertificateSelection(&goodName, rsa, certificates) |
| 125 |
assertNotError(t, err, "Failed to find certificate in a valid set") |
| 126 |
assertNotNil(t, cert, "Failed to set certificate") |
| 127 |
assertEquals(t, scheme, ECDSA_P256_SHA256) |
| 128 |
|
| 129 |
|
| 130 |
cert, scheme, err = CertificateSelection(nil, rsa, certificates) |
| 131 |
assertNotError(t, err, "Failed to find certificate in a valid set") |
| 132 |
assertNotNil(t, cert, "Failed to set certificate") |
| 133 |
assertEquals(t, scheme, ECDSA_P256_SHA256) |
| 134 |
|
| 135 |
|
| 136 |
_, _, err = CertificateSelection(&badName, rsa, certificates) |
| 137 |
assertError(t, err, "Found a certificate for an incorrect host name") |
| 138 |
|
| 139 |
|
| 140 |
_, _, err = CertificateSelection(&goodName, eddsa, certificates) |
| 141 |
assertError(t, err, "Found a certificate for an incorrect signature scheme") |
| 142 |
} |
| 143 |
|
| 144 |
func TestEarlyDataNegotiation(t *testing.T) { |
| 145 |
useEarlyData, rejected := EarlyDataNegotiation(true, true, true) |
| 146 |
assertTrue(t, useEarlyData, "Did not use early data when allowed") |
| 147 |
assertTrue(t, !rejected, "Rejected when allowed") |
| 148 |
|
| 149 |
useEarlyData, rejected = EarlyDataNegotiation(false, true, true) |
| 150 |
assertTrue(t, !useEarlyData, "Allowed early data when not using PSK") |
| 151 |
assertTrue(t, rejected, "Rejected not set") |
| 152 |
|
| 153 |
useEarlyData, rejected = EarlyDataNegotiation(true, false, true) |
| 154 |
assertTrue(t, !useEarlyData, "Allowed early data when not signaled") |
| 155 |
assertTrue(t, !rejected, "Rejected when not signaled") |
| 156 |
|
| 157 |
useEarlyData, rejected = EarlyDataNegotiation(true, true, false) |
| 158 |
assertTrue(t, !useEarlyData, "Allowed early data when not allowed") |
| 159 |
assertTrue(t, rejected, "Rejected not set") |
| 160 |
} |
| 161 |
|
| 162 |
func TestCipherSuiteNegotiation(t *testing.T) { |
| 163 |
offered := []CipherSuite{TLS_AES_128_GCM_SHA256, TLS_AES_256_GCM_SHA384, TLS_CHACHA20_POLY1305_SHA256} |
| 164 |
supported := []CipherSuite{TLS_AES_256_GCM_SHA384, TLS_CHACHA20_POLY1305_SHA256} |
| 165 |
psk := &PreSharedKey{CipherSuite: TLS_CHACHA20_POLY1305_SHA256} |
| 166 |
|
| 167 |
|
| 168 |
suite, err := CipherSuiteNegotiation(psk, offered, supported) |
| 169 |
assertNotError(t, err, "CipherSuite negotiation with PSK failed") |
| 170 |
assertEquals(t, suite, psk.CipherSuite) |
| 171 |
|
| 172 |
|
| 173 |
suite, err = CipherSuiteNegotiation(nil, offered, supported) |
| 174 |
assertNotError(t, err, "CipherSuite negotiation without PSK failed") |
| 175 |
assertEquals(t, suite, TLS_AES_256_GCM_SHA384) |
| 176 |
|
| 177 |
|
| 178 |
_, err = CipherSuiteNegotiation(nil, []CipherSuite{TLS_AES_128_GCM_SHA256}, supported) |
| 179 |
assertError(t, err, "CipherSuite negotiation succeeded with no overlap") |
| 180 |
} |
| 181 |
|
| 182 |
func TestALPNNegotiation(t *testing.T) { |
| 183 |
offered := []string{"http/1.1", "h2"} |
| 184 |
supported := []string{"h2", "spdy/1.1"} |
| 185 |
psk := &PreSharedKey{NextProto: "h2", IsResumption: true} |
| 186 |
|
| 187 |
|
| 188 |
proto, err := ALPNNegotiation(psk, offered, supported) |
| 189 |
assertNotError(t, err, "ALPN negotiation with PSK failed") |
| 190 |
assertEquals(t, proto, psk.NextProto) |
| 191 |
|
| 192 |
|
| 193 |
proto, err = ALPNNegotiation(nil, offered, supported) |
| 194 |
assertNotError(t, err, "ALPN negotiation without PSK failed") |
| 195 |
assertEquals(t, proto, "h2") |
| 196 |
|
| 197 |
|
| 198 |
proto, err = ALPNNegotiation(psk, []string{"http/1.1"}, []string{}) |
| 199 |
assertError(t, err, "Resumption allowed without offer having previous ALPN") |
| 200 |
|
| 201 |
|
| 202 |
proto, err = ALPNNegotiation(nil, []string{"http/1.1"}, []string{}) |
| 203 |
assertNotError(t, err, "ALPN mismatch caused an error") |
| 204 |
assertEquals(t, proto, "") |
| 205 |
} |