Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
David Benjamin | 407a10c | 2014-07-16 12:58:59 -0400 | [diff] [blame] | 5 | "crypto/x509" |
David Benjamin | 2561dc3 | 2014-08-24 01:25:27 -0400 | [diff] [blame^] | 6 | "encoding/base64" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 7 | "flag" |
| 8 | "fmt" |
| 9 | "io" |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 10 | "io/ioutil" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 11 | "net" |
| 12 | "os" |
| 13 | "os/exec" |
David Benjamin | 884fdf1 | 2014-08-02 15:28:23 -0400 | [diff] [blame] | 14 | "path" |
David Benjamin | 2bc8e6f | 2014-08-02 15:22:37 -0400 | [diff] [blame] | 15 | "runtime" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 16 | "strings" |
| 17 | "sync" |
| 18 | "syscall" |
| 19 | ) |
| 20 | |
| 21 | var useValgrind = flag.Bool("valgrind", false, "If true, run code under valgrind") |
| 22 | |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 23 | const ( |
| 24 | rsaCertificateFile = "cert.pem" |
| 25 | ecdsaCertificateFile = "ecdsa_cert.pem" |
| 26 | ) |
| 27 | |
| 28 | const ( |
| 29 | rsaKeyFile = "key.pem" |
| 30 | ecdsaKeyFile = "ecdsa_key.pem" |
| 31 | ) |
| 32 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 33 | var rsaCertificate, ecdsaCertificate Certificate |
| 34 | |
| 35 | func initCertificates() { |
| 36 | var err error |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 37 | rsaCertificate, err = LoadX509KeyPair(rsaCertificateFile, rsaKeyFile) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 38 | if err != nil { |
| 39 | panic(err) |
| 40 | } |
| 41 | |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 42 | ecdsaCertificate, err = LoadX509KeyPair(ecdsaCertificateFile, ecdsaKeyFile) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 43 | if err != nil { |
| 44 | panic(err) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | var certificateOnce sync.Once |
| 49 | |
| 50 | func getRSACertificate() Certificate { |
| 51 | certificateOnce.Do(initCertificates) |
| 52 | return rsaCertificate |
| 53 | } |
| 54 | |
| 55 | func getECDSACertificate() Certificate { |
| 56 | certificateOnce.Do(initCertificates) |
| 57 | return ecdsaCertificate |
| 58 | } |
| 59 | |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 60 | type testType int |
| 61 | |
| 62 | const ( |
| 63 | clientTest testType = iota |
| 64 | serverTest |
| 65 | ) |
| 66 | |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 67 | type protocol int |
| 68 | |
| 69 | const ( |
| 70 | tls protocol = iota |
| 71 | dtls |
| 72 | ) |
| 73 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 74 | type testCase struct { |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 75 | testType testType |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 76 | protocol protocol |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 77 | name string |
| 78 | config Config |
| 79 | shouldFail bool |
| 80 | expectedError string |
Adam Langley | ac61fa3 | 2014-06-23 12:03:11 -0700 | [diff] [blame] | 81 | // expectedLocalError, if not empty, contains a substring that must be |
| 82 | // found in the local error. |
| 83 | expectedLocalError string |
David Benjamin | 7e2e6cf | 2014-08-07 17:44:24 -0400 | [diff] [blame] | 84 | // expectedVersion, if non-zero, specifies the TLS version that must be |
| 85 | // negotiated. |
| 86 | expectedVersion uint16 |
Adam Langley | 80842bd | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 87 | // messageLen is the length, in bytes, of the test message that will be |
| 88 | // sent. |
| 89 | messageLen int |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 90 | // certFile is the path to the certificate to use for the server. |
| 91 | certFile string |
| 92 | // keyFile is the path to the private key to use for the server. |
| 93 | keyFile string |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 94 | // resumeSession controls whether a second connection should be tested |
| 95 | // which resumes the first session. |
| 96 | resumeSession bool |
David Benjamin | 98e882e | 2014-08-08 13:24:34 -0400 | [diff] [blame] | 97 | // sendPrefix sends a prefix on the socket before actually performing a |
| 98 | // handshake. |
| 99 | sendPrefix string |
David Benjamin | 325b5c3 | 2014-07-01 19:40:31 -0400 | [diff] [blame] | 100 | // flags, if not empty, contains a list of command-line flags that will |
| 101 | // be passed to the shim program. |
| 102 | flags []string |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 103 | } |
| 104 | |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 105 | var testCases = []testCase{ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 106 | { |
| 107 | name: "BadRSASignature", |
| 108 | config: Config{ |
| 109 | CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256}, |
| 110 | Bugs: ProtocolBugs{ |
| 111 | InvalidSKXSignature: true, |
| 112 | }, |
| 113 | }, |
| 114 | shouldFail: true, |
| 115 | expectedError: ":BAD_SIGNATURE:", |
| 116 | }, |
| 117 | { |
| 118 | name: "BadECDSASignature", |
| 119 | config: Config{ |
| 120 | CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256}, |
| 121 | Bugs: ProtocolBugs{ |
| 122 | InvalidSKXSignature: true, |
| 123 | }, |
| 124 | Certificates: []Certificate{getECDSACertificate()}, |
| 125 | }, |
| 126 | shouldFail: true, |
| 127 | expectedError: ":BAD_SIGNATURE:", |
| 128 | }, |
| 129 | { |
| 130 | name: "BadECDSACurve", |
| 131 | config: Config{ |
| 132 | CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256}, |
| 133 | Bugs: ProtocolBugs{ |
| 134 | InvalidSKXCurve: true, |
| 135 | }, |
| 136 | Certificates: []Certificate{getECDSACertificate()}, |
| 137 | }, |
| 138 | shouldFail: true, |
| 139 | expectedError: ":WRONG_CURVE:", |
| 140 | }, |
Adam Langley | ac61fa3 | 2014-06-23 12:03:11 -0700 | [diff] [blame] | 141 | { |
David Benjamin | a8e3e0e | 2014-08-06 22:11:10 -0400 | [diff] [blame] | 142 | testType: serverTest, |
| 143 | name: "BadRSAVersion", |
| 144 | config: Config{ |
| 145 | CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA}, |
| 146 | Bugs: ProtocolBugs{ |
| 147 | RsaClientKeyExchangeVersion: VersionTLS11, |
| 148 | }, |
| 149 | }, |
| 150 | shouldFail: true, |
| 151 | expectedError: ":DECRYPTION_FAILED_OR_BAD_RECORD_MAC:", |
| 152 | }, |
| 153 | { |
David Benjamin | 325b5c3 | 2014-07-01 19:40:31 -0400 | [diff] [blame] | 154 | name: "NoFallbackSCSV", |
Adam Langley | ac61fa3 | 2014-06-23 12:03:11 -0700 | [diff] [blame] | 155 | config: Config{ |
| 156 | Bugs: ProtocolBugs{ |
| 157 | FailIfNotFallbackSCSV: true, |
| 158 | }, |
| 159 | }, |
| 160 | shouldFail: true, |
| 161 | expectedLocalError: "no fallback SCSV found", |
| 162 | }, |
David Benjamin | 325b5c3 | 2014-07-01 19:40:31 -0400 | [diff] [blame] | 163 | { |
David Benjamin | 2a0c496 | 2014-08-22 23:46:35 -0400 | [diff] [blame] | 164 | name: "SendFallbackSCSV", |
David Benjamin | 325b5c3 | 2014-07-01 19:40:31 -0400 | [diff] [blame] | 165 | config: Config{ |
| 166 | Bugs: ProtocolBugs{ |
| 167 | FailIfNotFallbackSCSV: true, |
| 168 | }, |
| 169 | }, |
| 170 | flags: []string{"-fallback-scsv"}, |
| 171 | }, |
David Benjamin | 197b3ab | 2014-07-02 18:37:33 -0400 | [diff] [blame] | 172 | { |
| 173 | testType: serverTest, |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 174 | name: "ServerNameExtension", |
David Benjamin | 197b3ab | 2014-07-02 18:37:33 -0400 | [diff] [blame] | 175 | config: Config{ |
| 176 | ServerName: "example.com", |
| 177 | }, |
| 178 | flags: []string{"-expect-server-name", "example.com"}, |
| 179 | }, |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 180 | { |
| 181 | testType: clientTest, |
| 182 | name: "DuplicateExtensionClient", |
| 183 | config: Config{ |
| 184 | Bugs: ProtocolBugs{ |
| 185 | DuplicateExtension: true, |
| 186 | }, |
| 187 | }, |
| 188 | shouldFail: true, |
| 189 | expectedLocalError: "remote error: error decoding message", |
| 190 | }, |
| 191 | { |
| 192 | testType: serverTest, |
| 193 | name: "DuplicateExtensionServer", |
| 194 | config: Config{ |
| 195 | Bugs: ProtocolBugs{ |
| 196 | DuplicateExtension: true, |
| 197 | }, |
| 198 | }, |
| 199 | shouldFail: true, |
| 200 | expectedLocalError: "remote error: error decoding message", |
| 201 | }, |
David Benjamin | 7b03051 | 2014-07-08 17:30:11 -0400 | [diff] [blame] | 202 | { |
| 203 | name: "ClientCertificateTypes", |
| 204 | config: Config{ |
| 205 | ClientAuth: RequestClientCert, |
| 206 | ClientCertificateTypes: []byte{ |
| 207 | CertTypeDSSSign, |
| 208 | CertTypeRSASign, |
| 209 | CertTypeECDSASign, |
| 210 | }, |
| 211 | }, |
David Benjamin | 2561dc3 | 2014-08-24 01:25:27 -0400 | [diff] [blame^] | 212 | flags: []string{ |
| 213 | "-expect-certificate-types", |
| 214 | base64.StdEncoding.EncodeToString([]byte{ |
| 215 | CertTypeDSSSign, |
| 216 | CertTypeRSASign, |
| 217 | CertTypeECDSASign, |
| 218 | }), |
| 219 | }, |
David Benjamin | 7b03051 | 2014-07-08 17:30:11 -0400 | [diff] [blame] | 220 | }, |
David Benjamin | 636293b | 2014-07-08 17:59:18 -0400 | [diff] [blame] | 221 | { |
| 222 | name: "NoClientCertificate", |
| 223 | config: Config{ |
| 224 | ClientAuth: RequireAnyClientCert, |
| 225 | }, |
| 226 | shouldFail: true, |
| 227 | expectedLocalError: "client didn't provide a certificate", |
| 228 | }, |
David Benjamin | 1c375dd | 2014-07-12 00:48:23 -0400 | [diff] [blame] | 229 | { |
| 230 | name: "UnauthenticatedECDH", |
| 231 | config: Config{ |
| 232 | CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256}, |
| 233 | Bugs: ProtocolBugs{ |
| 234 | UnauthenticatedECDH: true, |
| 235 | }, |
| 236 | }, |
| 237 | shouldFail: true, |
David Benjamin | e8f3d66 | 2014-07-12 01:10:19 -0400 | [diff] [blame] | 238 | expectedError: ":UNEXPECTED_MESSAGE:", |
David Benjamin | 1c375dd | 2014-07-12 00:48:23 -0400 | [diff] [blame] | 239 | }, |
David Benjamin | 9c651c9 | 2014-07-12 13:27:45 -0400 | [diff] [blame] | 240 | { |
| 241 | name: "SkipServerKeyExchange", |
| 242 | config: Config{ |
| 243 | CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256}, |
| 244 | Bugs: ProtocolBugs{ |
| 245 | SkipServerKeyExchange: true, |
| 246 | }, |
| 247 | }, |
| 248 | shouldFail: true, |
| 249 | expectedError: ":UNEXPECTED_MESSAGE:", |
| 250 | }, |
David Benjamin | 1f5f62b | 2014-07-12 16:18:02 -0400 | [diff] [blame] | 251 | { |
David Benjamin | a0e5223 | 2014-07-19 17:39:58 -0400 | [diff] [blame] | 252 | name: "SkipChangeCipherSpec-Client", |
| 253 | config: Config{ |
| 254 | Bugs: ProtocolBugs{ |
| 255 | SkipChangeCipherSpec: true, |
| 256 | }, |
| 257 | }, |
| 258 | shouldFail: true, |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 259 | expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:", |
David Benjamin | a0e5223 | 2014-07-19 17:39:58 -0400 | [diff] [blame] | 260 | }, |
| 261 | { |
| 262 | testType: serverTest, |
| 263 | name: "SkipChangeCipherSpec-Server", |
| 264 | config: Config{ |
| 265 | Bugs: ProtocolBugs{ |
| 266 | SkipChangeCipherSpec: true, |
| 267 | }, |
| 268 | }, |
| 269 | shouldFail: true, |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 270 | expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:", |
David Benjamin | a0e5223 | 2014-07-19 17:39:58 -0400 | [diff] [blame] | 271 | }, |
David Benjamin | 42be645 | 2014-07-21 14:50:23 -0400 | [diff] [blame] | 272 | { |
| 273 | testType: serverTest, |
| 274 | name: "SkipChangeCipherSpec-Server-NPN", |
| 275 | config: Config{ |
| 276 | NextProtos: []string{"bar"}, |
| 277 | Bugs: ProtocolBugs{ |
| 278 | SkipChangeCipherSpec: true, |
| 279 | }, |
| 280 | }, |
| 281 | flags: []string{ |
| 282 | "-advertise-npn", "\x03foo\x03bar\x03baz", |
| 283 | }, |
| 284 | shouldFail: true, |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 285 | expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:", |
| 286 | }, |
| 287 | { |
| 288 | name: "FragmentAcrossChangeCipherSpec-Client", |
| 289 | config: Config{ |
| 290 | Bugs: ProtocolBugs{ |
| 291 | FragmentAcrossChangeCipherSpec: true, |
| 292 | }, |
| 293 | }, |
| 294 | shouldFail: true, |
| 295 | expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:", |
| 296 | }, |
| 297 | { |
| 298 | testType: serverTest, |
| 299 | name: "FragmentAcrossChangeCipherSpec-Server", |
| 300 | config: Config{ |
| 301 | Bugs: ProtocolBugs{ |
| 302 | FragmentAcrossChangeCipherSpec: true, |
| 303 | }, |
| 304 | }, |
| 305 | shouldFail: true, |
| 306 | expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:", |
| 307 | }, |
| 308 | { |
| 309 | testType: serverTest, |
| 310 | name: "FragmentAcrossChangeCipherSpec-Server-NPN", |
| 311 | config: Config{ |
| 312 | NextProtos: []string{"bar"}, |
| 313 | Bugs: ProtocolBugs{ |
| 314 | FragmentAcrossChangeCipherSpec: true, |
| 315 | }, |
| 316 | }, |
| 317 | flags: []string{ |
| 318 | "-advertise-npn", "\x03foo\x03bar\x03baz", |
| 319 | }, |
| 320 | shouldFail: true, |
| 321 | expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:", |
David Benjamin | 42be645 | 2014-07-21 14:50:23 -0400 | [diff] [blame] | 322 | }, |
David Benjamin | f3ec83d | 2014-07-21 22:42:34 -0400 | [diff] [blame] | 323 | { |
| 324 | testType: serverTest, |
| 325 | name: "EarlyChangeCipherSpec-server-1", |
| 326 | config: Config{ |
| 327 | Bugs: ProtocolBugs{ |
| 328 | EarlyChangeCipherSpec: 1, |
| 329 | }, |
| 330 | }, |
| 331 | shouldFail: true, |
| 332 | expectedError: ":CCS_RECEIVED_EARLY:", |
| 333 | }, |
| 334 | { |
| 335 | testType: serverTest, |
| 336 | name: "EarlyChangeCipherSpec-server-2", |
| 337 | config: Config{ |
| 338 | Bugs: ProtocolBugs{ |
| 339 | EarlyChangeCipherSpec: 2, |
| 340 | }, |
| 341 | }, |
| 342 | shouldFail: true, |
| 343 | expectedError: ":CCS_RECEIVED_EARLY:", |
| 344 | }, |
David Benjamin | d23f412 | 2014-07-23 15:09:48 -0400 | [diff] [blame] | 345 | { |
David Benjamin | d23f412 | 2014-07-23 15:09:48 -0400 | [diff] [blame] | 346 | name: "SkipNewSessionTicket", |
| 347 | config: Config{ |
| 348 | Bugs: ProtocolBugs{ |
| 349 | SkipNewSessionTicket: true, |
| 350 | }, |
| 351 | }, |
| 352 | shouldFail: true, |
| 353 | expectedError: ":CCS_RECEIVED_EARLY:", |
| 354 | }, |
David Benjamin | 7e3305e | 2014-07-28 14:52:32 -0400 | [diff] [blame] | 355 | { |
David Benjamin | d86c767 | 2014-08-02 04:07:12 -0400 | [diff] [blame] | 356 | testType: serverTest, |
David Benjamin | bef270a | 2014-08-02 04:22:02 -0400 | [diff] [blame] | 357 | name: "FallbackSCSV", |
| 358 | config: Config{ |
| 359 | MaxVersion: VersionTLS11, |
| 360 | Bugs: ProtocolBugs{ |
| 361 | SendFallbackSCSV: true, |
| 362 | }, |
| 363 | }, |
| 364 | shouldFail: true, |
| 365 | expectedError: ":INAPPROPRIATE_FALLBACK:", |
| 366 | }, |
| 367 | { |
| 368 | testType: serverTest, |
| 369 | name: "FallbackSCSV-VersionMatch", |
| 370 | config: Config{ |
| 371 | Bugs: ProtocolBugs{ |
| 372 | SendFallbackSCSV: true, |
| 373 | }, |
| 374 | }, |
| 375 | }, |
David Benjamin | 9821454 | 2014-08-07 18:02:39 -0400 | [diff] [blame] | 376 | { |
| 377 | testType: serverTest, |
| 378 | name: "FragmentedClientVersion", |
| 379 | config: Config{ |
| 380 | Bugs: ProtocolBugs{ |
| 381 | MaxHandshakeRecordLength: 1, |
| 382 | FragmentClientVersion: true, |
| 383 | }, |
| 384 | }, |
| 385 | shouldFail: true, |
| 386 | expectedError: ":RECORD_TOO_SMALL:", |
| 387 | }, |
David Benjamin | 98e882e | 2014-08-08 13:24:34 -0400 | [diff] [blame] | 388 | { |
| 389 | testType: serverTest, |
| 390 | name: "MinorVersionTolerance", |
| 391 | config: Config{ |
| 392 | Bugs: ProtocolBugs{ |
| 393 | SendClientVersion: 0x03ff, |
| 394 | }, |
| 395 | }, |
| 396 | expectedVersion: VersionTLS12, |
| 397 | }, |
| 398 | { |
| 399 | testType: serverTest, |
| 400 | name: "MajorVersionTolerance", |
| 401 | config: Config{ |
| 402 | Bugs: ProtocolBugs{ |
| 403 | SendClientVersion: 0x0400, |
| 404 | }, |
| 405 | }, |
| 406 | expectedVersion: VersionTLS12, |
| 407 | }, |
| 408 | { |
| 409 | testType: serverTest, |
| 410 | name: "VersionTooLow", |
| 411 | config: Config{ |
| 412 | Bugs: ProtocolBugs{ |
| 413 | SendClientVersion: 0x0200, |
| 414 | }, |
| 415 | }, |
| 416 | shouldFail: true, |
| 417 | expectedError: ":UNSUPPORTED_PROTOCOL:", |
| 418 | }, |
| 419 | { |
| 420 | testType: serverTest, |
| 421 | name: "HttpGET", |
| 422 | sendPrefix: "GET / HTTP/1.0\n", |
| 423 | shouldFail: true, |
| 424 | expectedError: ":HTTP_REQUEST:", |
| 425 | }, |
| 426 | { |
| 427 | testType: serverTest, |
| 428 | name: "HttpPOST", |
| 429 | sendPrefix: "POST / HTTP/1.0\n", |
| 430 | shouldFail: true, |
| 431 | expectedError: ":HTTP_REQUEST:", |
| 432 | }, |
| 433 | { |
| 434 | testType: serverTest, |
| 435 | name: "HttpHEAD", |
| 436 | sendPrefix: "HEAD / HTTP/1.0\n", |
| 437 | shouldFail: true, |
| 438 | expectedError: ":HTTP_REQUEST:", |
| 439 | }, |
| 440 | { |
| 441 | testType: serverTest, |
| 442 | name: "HttpPUT", |
| 443 | sendPrefix: "PUT / HTTP/1.0\n", |
| 444 | shouldFail: true, |
| 445 | expectedError: ":HTTP_REQUEST:", |
| 446 | }, |
| 447 | { |
| 448 | testType: serverTest, |
| 449 | name: "HttpCONNECT", |
| 450 | sendPrefix: "CONNECT www.google.com:443 HTTP/1.0\n", |
| 451 | shouldFail: true, |
| 452 | expectedError: ":HTTPS_PROXY_REQUEST:", |
| 453 | }, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 454 | } |
| 455 | |
David Benjamin | 7e2e6cf | 2014-08-07 17:44:24 -0400 | [diff] [blame] | 456 | func doExchange(test *testCase, config *Config, conn net.Conn, messageLen int) error { |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 457 | if test.protocol == dtls { |
| 458 | conn = newPacketAdaptor(conn) |
| 459 | } |
| 460 | |
| 461 | if test.sendPrefix != "" { |
| 462 | if _, err := conn.Write([]byte(test.sendPrefix)); err != nil { |
| 463 | return err |
| 464 | } |
David Benjamin | 98e882e | 2014-08-08 13:24:34 -0400 | [diff] [blame] | 465 | } |
| 466 | |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 467 | var tlsConn *Conn |
David Benjamin | 7e2e6cf | 2014-08-07 17:44:24 -0400 | [diff] [blame] | 468 | if test.testType == clientTest { |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 469 | if test.protocol == dtls { |
| 470 | tlsConn = DTLSServer(conn, config) |
| 471 | } else { |
| 472 | tlsConn = Server(conn, config) |
| 473 | } |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 474 | } else { |
| 475 | config.InsecureSkipVerify = true |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 476 | if test.protocol == dtls { |
| 477 | tlsConn = DTLSClient(conn, config) |
| 478 | } else { |
| 479 | tlsConn = Client(conn, config) |
| 480 | } |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 481 | } |
| 482 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 483 | if err := tlsConn.Handshake(); err != nil { |
| 484 | return err |
| 485 | } |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 486 | |
David Benjamin | 7e2e6cf | 2014-08-07 17:44:24 -0400 | [diff] [blame] | 487 | if vers := tlsConn.ConnectionState().Version; test.expectedVersion != 0 && vers != test.expectedVersion { |
| 488 | return fmt.Errorf("got version %x, expected %x", vers, test.expectedVersion) |
| 489 | } |
| 490 | |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 491 | if messageLen < 0 { |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 492 | if test.protocol == dtls { |
| 493 | return fmt.Errorf("messageLen < 0 not supported for DTLS tests") |
| 494 | } |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 495 | // Read until EOF. |
| 496 | _, err := io.Copy(ioutil.Discard, tlsConn) |
| 497 | return err |
| 498 | } |
| 499 | |
Adam Langley | 80842bd | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 500 | if messageLen == 0 { |
| 501 | messageLen = 32 |
| 502 | } |
| 503 | testMessage := make([]byte, messageLen) |
| 504 | for i := range testMessage { |
| 505 | testMessage[i] = 0x42 |
| 506 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 507 | tlsConn.Write(testMessage) |
| 508 | |
| 509 | buf := make([]byte, len(testMessage)) |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 510 | if test.protocol == dtls { |
| 511 | bufTmp := make([]byte, len(buf)+1) |
| 512 | n, err := tlsConn.Read(bufTmp) |
| 513 | if err != nil { |
| 514 | return err |
| 515 | } |
| 516 | if n != len(buf) { |
| 517 | return fmt.Errorf("bad reply; length mismatch (%d vs %d)", n, len(buf)) |
| 518 | } |
| 519 | copy(buf, bufTmp) |
| 520 | } else { |
| 521 | _, err := io.ReadFull(tlsConn, buf) |
| 522 | if err != nil { |
| 523 | return err |
| 524 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | for i, v := range buf { |
| 528 | if v != testMessage[i]^0xff { |
| 529 | return fmt.Errorf("bad reply contents at byte %d", i) |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | return nil |
| 534 | } |
| 535 | |
David Benjamin | 325b5c3 | 2014-07-01 19:40:31 -0400 | [diff] [blame] | 536 | func valgrindOf(dbAttach bool, path string, args ...string) *exec.Cmd { |
| 537 | valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full"} |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 538 | if dbAttach { |
David Benjamin | 325b5c3 | 2014-07-01 19:40:31 -0400 | [diff] [blame] | 539 | valgrindArgs = append(valgrindArgs, "--db-attach=yes", "--db-command=xterm -e gdb -nw %f %p") |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 540 | } |
David Benjamin | 325b5c3 | 2014-07-01 19:40:31 -0400 | [diff] [blame] | 541 | valgrindArgs = append(valgrindArgs, path) |
| 542 | valgrindArgs = append(valgrindArgs, args...) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 543 | |
David Benjamin | 325b5c3 | 2014-07-01 19:40:31 -0400 | [diff] [blame] | 544 | return exec.Command("valgrind", valgrindArgs...) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 545 | } |
| 546 | |
David Benjamin | 325b5c3 | 2014-07-01 19:40:31 -0400 | [diff] [blame] | 547 | func gdbOf(path string, args ...string) *exec.Cmd { |
| 548 | xtermArgs := []string{"-e", "gdb", "--args"} |
| 549 | xtermArgs = append(xtermArgs, path) |
| 550 | xtermArgs = append(xtermArgs, args...) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 551 | |
David Benjamin | 325b5c3 | 2014-07-01 19:40:31 -0400 | [diff] [blame] | 552 | return exec.Command("xterm", xtermArgs...) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 553 | } |
| 554 | |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 555 | func openSocketPair() (shimEnd *os.File, conn net.Conn) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 556 | socks, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_STREAM, 0) |
| 557 | if err != nil { |
| 558 | panic(err) |
| 559 | } |
| 560 | |
| 561 | syscall.CloseOnExec(socks[0]) |
| 562 | syscall.CloseOnExec(socks[1]) |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 563 | shimEnd = os.NewFile(uintptr(socks[0]), "shim end") |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 564 | connFile := os.NewFile(uintptr(socks[1]), "our end") |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 565 | conn, err = net.FileConn(connFile) |
| 566 | if err != nil { |
| 567 | panic(err) |
| 568 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 569 | connFile.Close() |
| 570 | if err != nil { |
| 571 | panic(err) |
| 572 | } |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 573 | return shimEnd, conn |
| 574 | } |
| 575 | |
David Benjamin | 884fdf1 | 2014-08-02 15:28:23 -0400 | [diff] [blame] | 576 | func runTest(test *testCase, buildDir string) error { |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 577 | shimEnd, conn := openSocketPair() |
| 578 | shimEndResume, connResume := openSocketPair() |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 579 | |
David Benjamin | 884fdf1 | 2014-08-02 15:28:23 -0400 | [diff] [blame] | 580 | shim_path := path.Join(buildDir, "ssl/test/bssl_shim") |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 581 | var flags []string |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 582 | if test.testType == serverTest { |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 583 | flags = append(flags, "-server") |
| 584 | |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 585 | flags = append(flags, "-key-file") |
| 586 | if test.keyFile == "" { |
| 587 | flags = append(flags, rsaKeyFile) |
| 588 | } else { |
| 589 | flags = append(flags, test.keyFile) |
| 590 | } |
| 591 | |
| 592 | flags = append(flags, "-cert-file") |
| 593 | if test.certFile == "" { |
| 594 | flags = append(flags, rsaCertificateFile) |
| 595 | } else { |
| 596 | flags = append(flags, test.certFile) |
| 597 | } |
| 598 | } |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 599 | |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 600 | if test.protocol == dtls { |
| 601 | flags = append(flags, "-dtls") |
| 602 | } |
| 603 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 604 | if test.resumeSession { |
| 605 | flags = append(flags, "-resume") |
| 606 | } |
| 607 | |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 608 | flags = append(flags, test.flags...) |
| 609 | |
| 610 | var shim *exec.Cmd |
| 611 | if *useValgrind { |
| 612 | shim = valgrindOf(false, shim_path, flags...) |
| 613 | } else { |
| 614 | shim = exec.Command(shim_path, flags...) |
| 615 | } |
| 616 | // shim = gdbOf(shim_path, flags...) |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 617 | shim.ExtraFiles = []*os.File{shimEnd, shimEndResume} |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 618 | shim.Stdin = os.Stdin |
| 619 | var stdoutBuf, stderrBuf bytes.Buffer |
| 620 | shim.Stdout = &stdoutBuf |
| 621 | shim.Stderr = &stderrBuf |
| 622 | |
| 623 | if err := shim.Start(); err != nil { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 624 | panic(err) |
| 625 | } |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 626 | shimEnd.Close() |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 627 | shimEndResume.Close() |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 628 | |
| 629 | config := test.config |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 630 | config.ClientSessionCache = NewLRUClientSessionCache(1) |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 631 | if test.testType == clientTest { |
| 632 | if len(config.Certificates) == 0 { |
| 633 | config.Certificates = []Certificate{getRSACertificate()} |
| 634 | } |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 635 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 636 | |
David Benjamin | 7e2e6cf | 2014-08-07 17:44:24 -0400 | [diff] [blame] | 637 | err := doExchange(test, &config, conn, test.messageLen) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 638 | conn.Close() |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 639 | if err == nil && test.resumeSession { |
David Benjamin | 7e2e6cf | 2014-08-07 17:44:24 -0400 | [diff] [blame] | 640 | err = doExchange(test, &config, connResume, test.messageLen) |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 641 | connResume.Close() |
| 642 | } |
| 643 | |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 644 | childErr := shim.Wait() |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 645 | |
| 646 | stdout := string(stdoutBuf.Bytes()) |
| 647 | stderr := string(stderrBuf.Bytes()) |
| 648 | failed := err != nil || childErr != nil |
| 649 | correctFailure := len(test.expectedError) == 0 || strings.Contains(stdout, test.expectedError) |
Adam Langley | ac61fa3 | 2014-06-23 12:03:11 -0700 | [diff] [blame] | 650 | localError := "none" |
| 651 | if err != nil { |
| 652 | localError = err.Error() |
| 653 | } |
| 654 | if len(test.expectedLocalError) != 0 { |
| 655 | correctFailure = correctFailure && strings.Contains(localError, test.expectedLocalError) |
| 656 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 657 | |
| 658 | if failed != test.shouldFail || failed && !correctFailure { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 659 | childError := "none" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 660 | if childErr != nil { |
| 661 | childError = childErr.Error() |
| 662 | } |
| 663 | |
| 664 | var msg string |
| 665 | switch { |
| 666 | case failed && !test.shouldFail: |
| 667 | msg = "unexpected failure" |
| 668 | case !failed && test.shouldFail: |
| 669 | msg = "unexpected success" |
| 670 | case failed && !correctFailure: |
Adam Langley | ac61fa3 | 2014-06-23 12:03:11 -0700 | [diff] [blame] | 671 | msg = "bad error (wanted '" + test.expectedError + "' / '" + test.expectedLocalError + "')" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 672 | default: |
| 673 | panic("internal error") |
| 674 | } |
| 675 | |
| 676 | return fmt.Errorf("%s: local error '%s', child error '%s', stdout:\n%s\nstderr:\n%s", msg, localError, childError, string(stdoutBuf.Bytes()), stderr) |
| 677 | } |
| 678 | |
| 679 | if !*useValgrind && len(stderr) > 0 { |
| 680 | println(stderr) |
| 681 | } |
| 682 | |
| 683 | return nil |
| 684 | } |
| 685 | |
| 686 | var tlsVersions = []struct { |
| 687 | name string |
| 688 | version uint16 |
David Benjamin | 7e2e6cf | 2014-08-07 17:44:24 -0400 | [diff] [blame] | 689 | flag string |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 690 | }{ |
David Benjamin | 7e2e6cf | 2014-08-07 17:44:24 -0400 | [diff] [blame] | 691 | {"SSL3", VersionSSL30, "-no-ssl3"}, |
| 692 | {"TLS1", VersionTLS10, "-no-tls1"}, |
| 693 | {"TLS11", VersionTLS11, "-no-tls11"}, |
| 694 | {"TLS12", VersionTLS12, "-no-tls12"}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | var testCipherSuites = []struct { |
| 698 | name string |
| 699 | id uint16 |
| 700 | }{ |
| 701 | {"3DES-SHA", TLS_RSA_WITH_3DES_EDE_CBC_SHA}, |
David Benjamin | f4e5c4e | 2014-08-02 17:35:45 -0400 | [diff] [blame] | 702 | {"AES128-GCM", TLS_RSA_WITH_AES_128_GCM_SHA256}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 703 | {"AES128-SHA", TLS_RSA_WITH_AES_128_CBC_SHA}, |
David Benjamin | f4e5c4e | 2014-08-02 17:35:45 -0400 | [diff] [blame] | 704 | {"AES256-GCM", TLS_RSA_WITH_AES_256_GCM_SHA384}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 705 | {"AES256-SHA", TLS_RSA_WITH_AES_256_CBC_SHA}, |
David Benjamin | f4e5c4e | 2014-08-02 17:35:45 -0400 | [diff] [blame] | 706 | {"DHE-RSA-AES128-GCM", TLS_DHE_RSA_WITH_AES_128_GCM_SHA256}, |
| 707 | {"DHE-RSA-AES128-SHA", TLS_DHE_RSA_WITH_AES_128_CBC_SHA}, |
| 708 | {"DHE-RSA-AES256-GCM", TLS_DHE_RSA_WITH_AES_256_GCM_SHA384}, |
| 709 | {"DHE-RSA-AES256-SHA", TLS_DHE_RSA_WITH_AES_256_CBC_SHA}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 710 | {"ECDHE-ECDSA-AES128-GCM", TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256}, |
| 711 | {"ECDHE-ECDSA-AES128-SHA", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA}, |
| 712 | {"ECDHE-ECDSA-AES256-SHA", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA}, |
| 713 | {"ECDHE-ECDSA-RC4-SHA", TLS_ECDHE_ECDSA_WITH_RC4_128_SHA}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 714 | {"ECDHE-RSA-AES128-GCM", TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 715 | {"ECDHE-RSA-AES128-SHA", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA}, |
David Benjamin | f4e5c4e | 2014-08-02 17:35:45 -0400 | [diff] [blame] | 716 | {"ECDHE-RSA-AES256-GCM", TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 717 | {"ECDHE-RSA-AES256-SHA", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA}, |
| 718 | {"ECDHE-RSA-RC4-SHA", TLS_ECDHE_RSA_WITH_RC4_128_SHA}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 719 | {"RC4-MD5", TLS_RSA_WITH_RC4_128_MD5}, |
David Benjamin | f4e5c4e | 2014-08-02 17:35:45 -0400 | [diff] [blame] | 720 | {"RC4-SHA", TLS_RSA_WITH_RC4_128_SHA}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | func addCipherSuiteTests() { |
| 724 | for _, suite := range testCipherSuites { |
| 725 | var cert Certificate |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 726 | var certFile string |
| 727 | var keyFile string |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 728 | if strings.Contains(suite.name, "ECDSA") { |
| 729 | cert = getECDSACertificate() |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 730 | certFile = ecdsaCertificateFile |
| 731 | keyFile = ecdsaKeyFile |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 732 | } else { |
| 733 | cert = getRSACertificate() |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 734 | certFile = rsaCertificateFile |
| 735 | keyFile = rsaKeyFile |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | for _, ver := range tlsVersions { |
| 739 | if ver.version != VersionTLS12 && strings.HasSuffix(suite.name, "-GCM") { |
| 740 | continue |
| 741 | } |
| 742 | |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 743 | // Go's TLS implementation only implements session |
| 744 | // resumption with tickets, so SSLv3 cannot resume |
| 745 | // sessions. |
| 746 | resumeSession := ver.version != VersionSSL30 |
| 747 | |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 748 | testCases = append(testCases, testCase{ |
| 749 | testType: clientTest, |
| 750 | name: ver.name + "-" + suite.name + "-client", |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 751 | config: Config{ |
| 752 | MinVersion: ver.version, |
| 753 | MaxVersion: ver.version, |
| 754 | CipherSuites: []uint16{suite.id}, |
| 755 | Certificates: []Certificate{cert}, |
| 756 | }, |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 757 | resumeSession: resumeSession, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 758 | }) |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 759 | |
David Benjamin | 76d8abe | 2014-08-14 16:25:34 -0400 | [diff] [blame] | 760 | testCases = append(testCases, testCase{ |
| 761 | testType: serverTest, |
| 762 | name: ver.name + "-" + suite.name + "-server", |
| 763 | config: Config{ |
| 764 | MinVersion: ver.version, |
| 765 | MaxVersion: ver.version, |
| 766 | CipherSuites: []uint16{suite.id}, |
| 767 | Certificates: []Certificate{cert}, |
| 768 | }, |
| 769 | certFile: certFile, |
| 770 | keyFile: keyFile, |
| 771 | resumeSession: resumeSession, |
| 772 | }) |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 773 | |
| 774 | // TODO(davidben): Fix DTLS 1.2 support and test that. |
| 775 | if ver.version == VersionTLS10 && strings.Index(suite.name, "RC4") == -1 { |
| 776 | testCases = append(testCases, testCase{ |
| 777 | testType: clientTest, |
| 778 | protocol: dtls, |
| 779 | name: "D" + ver.name + "-" + suite.name + "-client", |
| 780 | config: Config{ |
| 781 | MinVersion: ver.version, |
| 782 | MaxVersion: ver.version, |
| 783 | CipherSuites: []uint16{suite.id}, |
| 784 | Certificates: []Certificate{cert}, |
| 785 | }, |
| 786 | resumeSession: resumeSession, |
| 787 | }) |
| 788 | testCases = append(testCases, testCase{ |
| 789 | testType: serverTest, |
| 790 | protocol: dtls, |
| 791 | name: "D" + ver.name + "-" + suite.name + "-server", |
| 792 | config: Config{ |
| 793 | MinVersion: ver.version, |
| 794 | MaxVersion: ver.version, |
| 795 | CipherSuites: []uint16{suite.id}, |
| 796 | Certificates: []Certificate{cert}, |
| 797 | }, |
| 798 | certFile: certFile, |
| 799 | keyFile: keyFile, |
| 800 | resumeSession: resumeSession, |
| 801 | }) |
| 802 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 803 | } |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | func addBadECDSASignatureTests() { |
| 808 | for badR := BadValue(1); badR < NumBadValues; badR++ { |
| 809 | for badS := BadValue(1); badS < NumBadValues; badS++ { |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 810 | testCases = append(testCases, testCase{ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 811 | name: fmt.Sprintf("BadECDSA-%d-%d", badR, badS), |
| 812 | config: Config{ |
| 813 | CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256}, |
| 814 | Certificates: []Certificate{getECDSACertificate()}, |
| 815 | Bugs: ProtocolBugs{ |
| 816 | BadECDSAR: badR, |
| 817 | BadECDSAS: badS, |
| 818 | }, |
| 819 | }, |
| 820 | shouldFail: true, |
| 821 | expectedError: "SIGNATURE", |
| 822 | }) |
| 823 | } |
| 824 | } |
| 825 | } |
| 826 | |
Adam Langley | 80842bd | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 827 | func addCBCPaddingTests() { |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 828 | testCases = append(testCases, testCase{ |
Adam Langley | 80842bd | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 829 | name: "MaxCBCPadding", |
| 830 | config: Config{ |
| 831 | CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA}, |
| 832 | Bugs: ProtocolBugs{ |
| 833 | MaxPadding: true, |
| 834 | }, |
| 835 | }, |
| 836 | messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size |
| 837 | }) |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 838 | testCases = append(testCases, testCase{ |
Adam Langley | 80842bd | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 839 | name: "BadCBCPadding", |
| 840 | config: Config{ |
| 841 | CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA}, |
| 842 | Bugs: ProtocolBugs{ |
| 843 | PaddingFirstByteBad: true, |
| 844 | }, |
| 845 | }, |
| 846 | shouldFail: true, |
| 847 | expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC", |
| 848 | }) |
| 849 | // OpenSSL previously had an issue where the first byte of padding in |
| 850 | // 255 bytes of padding wasn't checked. |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 851 | testCases = append(testCases, testCase{ |
Adam Langley | 80842bd | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 852 | name: "BadCBCPadding255", |
| 853 | config: Config{ |
| 854 | CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA}, |
| 855 | Bugs: ProtocolBugs{ |
| 856 | MaxPadding: true, |
| 857 | PaddingFirstByteBadIf255: true, |
| 858 | }, |
| 859 | }, |
| 860 | messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size |
| 861 | shouldFail: true, |
| 862 | expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC", |
| 863 | }) |
| 864 | } |
| 865 | |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 866 | func addCBCSplittingTests() { |
| 867 | testCases = append(testCases, testCase{ |
| 868 | name: "CBCRecordSplitting", |
| 869 | config: Config{ |
| 870 | MaxVersion: VersionTLS10, |
| 871 | MinVersion: VersionTLS10, |
| 872 | CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA}, |
| 873 | }, |
| 874 | messageLen: -1, // read until EOF |
| 875 | flags: []string{ |
| 876 | "-async", |
| 877 | "-write-different-record-sizes", |
| 878 | "-cbc-record-splitting", |
| 879 | }, |
David Benjamin | a8e3e0e | 2014-08-06 22:11:10 -0400 | [diff] [blame] | 880 | }) |
| 881 | testCases = append(testCases, testCase{ |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 882 | name: "CBCRecordSplittingPartialWrite", |
| 883 | config: Config{ |
| 884 | MaxVersion: VersionTLS10, |
| 885 | MinVersion: VersionTLS10, |
| 886 | CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA}, |
| 887 | }, |
| 888 | messageLen: -1, // read until EOF |
| 889 | flags: []string{ |
| 890 | "-async", |
| 891 | "-write-different-record-sizes", |
| 892 | "-cbc-record-splitting", |
| 893 | "-partial-write", |
| 894 | }, |
| 895 | }) |
| 896 | } |
| 897 | |
David Benjamin | 636293b | 2014-07-08 17:59:18 -0400 | [diff] [blame] | 898 | func addClientAuthTests() { |
David Benjamin | 407a10c | 2014-07-16 12:58:59 -0400 | [diff] [blame] | 899 | // Add a dummy cert pool to stress certificate authority parsing. |
| 900 | // TODO(davidben): Add tests that those values parse out correctly. |
| 901 | certPool := x509.NewCertPool() |
| 902 | cert, err := x509.ParseCertificate(rsaCertificate.Certificate[0]) |
| 903 | if err != nil { |
| 904 | panic(err) |
| 905 | } |
| 906 | certPool.AddCert(cert) |
| 907 | |
David Benjamin | 636293b | 2014-07-08 17:59:18 -0400 | [diff] [blame] | 908 | for _, ver := range tlsVersions { |
| 909 | if ver.version == VersionSSL30 { |
| 910 | // TODO(davidben): The Go implementation does not |
| 911 | // correctly compute CertificateVerify hashes for SSLv3. |
| 912 | continue |
| 913 | } |
| 914 | |
| 915 | var cipherSuites []uint16 |
| 916 | if ver.version >= VersionTLS12 { |
| 917 | // Pick a SHA-256 cipher suite. The Go implementation |
| 918 | // does not correctly handle client auth with a SHA-384 |
| 919 | // cipher suite. |
| 920 | cipherSuites = []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256} |
| 921 | } |
| 922 | |
| 923 | testCases = append(testCases, testCase{ |
| 924 | testType: clientTest, |
David Benjamin | 67666e7 | 2014-07-12 15:47:52 -0400 | [diff] [blame] | 925 | name: ver.name + "-Client-ClientAuth-RSA", |
David Benjamin | 636293b | 2014-07-08 17:59:18 -0400 | [diff] [blame] | 926 | config: Config{ |
| 927 | MinVersion: ver.version, |
| 928 | MaxVersion: ver.version, |
| 929 | CipherSuites: cipherSuites, |
| 930 | ClientAuth: RequireAnyClientCert, |
David Benjamin | 407a10c | 2014-07-16 12:58:59 -0400 | [diff] [blame] | 931 | ClientCAs: certPool, |
David Benjamin | 636293b | 2014-07-08 17:59:18 -0400 | [diff] [blame] | 932 | }, |
| 933 | flags: []string{ |
| 934 | "-cert-file", rsaCertificateFile, |
| 935 | "-key-file", rsaKeyFile, |
| 936 | }, |
| 937 | }) |
| 938 | testCases = append(testCases, testCase{ |
| 939 | testType: clientTest, |
David Benjamin | 67666e7 | 2014-07-12 15:47:52 -0400 | [diff] [blame] | 940 | name: ver.name + "-Client-ClientAuth-ECDSA", |
David Benjamin | 636293b | 2014-07-08 17:59:18 -0400 | [diff] [blame] | 941 | config: Config{ |
| 942 | MinVersion: ver.version, |
| 943 | MaxVersion: ver.version, |
| 944 | CipherSuites: cipherSuites, |
| 945 | ClientAuth: RequireAnyClientCert, |
David Benjamin | 407a10c | 2014-07-16 12:58:59 -0400 | [diff] [blame] | 946 | ClientCAs: certPool, |
David Benjamin | 636293b | 2014-07-08 17:59:18 -0400 | [diff] [blame] | 947 | }, |
| 948 | flags: []string{ |
| 949 | "-cert-file", ecdsaCertificateFile, |
| 950 | "-key-file", ecdsaKeyFile, |
| 951 | }, |
| 952 | }) |
David Benjamin | 67666e7 | 2014-07-12 15:47:52 -0400 | [diff] [blame] | 953 | testCases = append(testCases, testCase{ |
| 954 | testType: serverTest, |
| 955 | name: ver.name + "-Server-ClientAuth-RSA", |
| 956 | config: Config{ |
| 957 | Certificates: []Certificate{rsaCertificate}, |
| 958 | }, |
| 959 | flags: []string{"-require-any-client-certificate"}, |
| 960 | }) |
| 961 | testCases = append(testCases, testCase{ |
| 962 | testType: serverTest, |
| 963 | name: ver.name + "-Server-ClientAuth-ECDSA", |
| 964 | config: Config{ |
| 965 | Certificates: []Certificate{ecdsaCertificate}, |
| 966 | }, |
| 967 | flags: []string{"-require-any-client-certificate"}, |
| 968 | }) |
David Benjamin | 636293b | 2014-07-08 17:59:18 -0400 | [diff] [blame] | 969 | } |
| 970 | } |
| 971 | |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 972 | // Adds tests that try to cover the range of the handshake state machine, under |
| 973 | // various conditions. Some of these are redundant with other tests, but they |
| 974 | // only cover the synchronous case. |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 975 | func addStateMachineCoverageTests(async, splitHandshake bool, protocol protocol) { |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 976 | var suffix string |
| 977 | var flags []string |
| 978 | var maxHandshakeRecordLength int |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 979 | if protocol == dtls { |
| 980 | suffix = "-DTLS" |
| 981 | } |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 982 | if async { |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 983 | suffix += "-Async" |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 984 | flags = append(flags, "-async") |
| 985 | } else { |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 986 | suffix += "-Sync" |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 987 | } |
| 988 | if splitHandshake { |
| 989 | suffix += "-SplitHandshakeRecords" |
David Benjamin | 9821454 | 2014-08-07 18:02:39 -0400 | [diff] [blame] | 990 | maxHandshakeRecordLength = 1 |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | // Basic handshake, with resumption. Client and server. |
| 994 | testCases = append(testCases, testCase{ |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 995 | protocol: protocol, |
| 996 | name: "Basic-Client" + suffix, |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 997 | config: Config{ |
| 998 | Bugs: ProtocolBugs{ |
| 999 | MaxHandshakeRecordLength: maxHandshakeRecordLength, |
| 1000 | }, |
| 1001 | }, |
David Benjamin | bed9aae | 2014-08-07 19:13:38 -0400 | [diff] [blame] | 1002 | flags: flags, |
| 1003 | resumeSession: true, |
| 1004 | }) |
| 1005 | testCases = append(testCases, testCase{ |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 1006 | protocol: protocol, |
| 1007 | name: "Basic-Client-RenewTicket" + suffix, |
David Benjamin | bed9aae | 2014-08-07 19:13:38 -0400 | [diff] [blame] | 1008 | config: Config{ |
| 1009 | Bugs: ProtocolBugs{ |
| 1010 | MaxHandshakeRecordLength: maxHandshakeRecordLength, |
| 1011 | RenewTicketOnResume: true, |
| 1012 | }, |
| 1013 | }, |
| 1014 | flags: flags, |
| 1015 | resumeSession: true, |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 1016 | }) |
| 1017 | testCases = append(testCases, testCase{ |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 1018 | protocol: protocol, |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 1019 | testType: serverTest, |
| 1020 | name: "Basic-Server" + suffix, |
| 1021 | config: Config{ |
| 1022 | Bugs: ProtocolBugs{ |
| 1023 | MaxHandshakeRecordLength: maxHandshakeRecordLength, |
| 1024 | }, |
| 1025 | }, |
David Benjamin | bed9aae | 2014-08-07 19:13:38 -0400 | [diff] [blame] | 1026 | flags: flags, |
| 1027 | resumeSession: true, |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 1028 | }) |
| 1029 | |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 1030 | // TLS client auth. |
| 1031 | testCases = append(testCases, testCase{ |
| 1032 | protocol: protocol, |
| 1033 | testType: clientTest, |
| 1034 | name: "ClientAuth-Client" + suffix, |
| 1035 | config: Config{ |
| 1036 | CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA}, |
| 1037 | ClientAuth: RequireAnyClientCert, |
| 1038 | Bugs: ProtocolBugs{ |
| 1039 | MaxHandshakeRecordLength: maxHandshakeRecordLength, |
| 1040 | }, |
| 1041 | }, |
| 1042 | flags: append(flags, |
| 1043 | "-cert-file", rsaCertificateFile, |
| 1044 | "-key-file", rsaKeyFile), |
| 1045 | }) |
| 1046 | testCases = append(testCases, testCase{ |
| 1047 | protocol: protocol, |
| 1048 | testType: serverTest, |
| 1049 | name: "ClientAuth-Server" + suffix, |
| 1050 | config: Config{ |
| 1051 | Certificates: []Certificate{rsaCertificate}, |
| 1052 | }, |
| 1053 | flags: append(flags, "-require-any-client-certificate"), |
| 1054 | }) |
| 1055 | |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 1056 | // No session ticket support; server doesn't send NewSessionTicket. |
| 1057 | testCases = append(testCases, testCase{ |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 1058 | protocol: protocol, |
| 1059 | name: "SessionTicketsDisabled-Client" + suffix, |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 1060 | config: Config{ |
| 1061 | SessionTicketsDisabled: true, |
| 1062 | Bugs: ProtocolBugs{ |
| 1063 | MaxHandshakeRecordLength: maxHandshakeRecordLength, |
| 1064 | }, |
| 1065 | }, |
| 1066 | flags: flags, |
| 1067 | }) |
| 1068 | testCases = append(testCases, testCase{ |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 1069 | protocol: protocol, |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 1070 | testType: serverTest, |
| 1071 | name: "SessionTicketsDisabled-Server" + suffix, |
| 1072 | config: Config{ |
| 1073 | SessionTicketsDisabled: true, |
| 1074 | Bugs: ProtocolBugs{ |
| 1075 | MaxHandshakeRecordLength: maxHandshakeRecordLength, |
| 1076 | }, |
| 1077 | }, |
| 1078 | flags: flags, |
| 1079 | }) |
| 1080 | |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 1081 | if protocol == tls { |
| 1082 | // NPN on client and server; results in post-handshake message. |
| 1083 | testCases = append(testCases, testCase{ |
| 1084 | protocol: protocol, |
| 1085 | name: "NPN-Client" + suffix, |
| 1086 | config: Config{ |
| 1087 | CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256}, |
| 1088 | NextProtos: []string{"foo"}, |
| 1089 | Bugs: ProtocolBugs{ |
| 1090 | MaxHandshakeRecordLength: maxHandshakeRecordLength, |
| 1091 | }, |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 1092 | }, |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 1093 | flags: append(flags, "-select-next-proto", "foo"), |
| 1094 | }) |
| 1095 | testCases = append(testCases, testCase{ |
| 1096 | protocol: protocol, |
| 1097 | testType: serverTest, |
| 1098 | name: "NPN-Server" + suffix, |
| 1099 | config: Config{ |
| 1100 | NextProtos: []string{"bar"}, |
| 1101 | Bugs: ProtocolBugs{ |
| 1102 | MaxHandshakeRecordLength: maxHandshakeRecordLength, |
| 1103 | }, |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 1104 | }, |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 1105 | flags: append(flags, |
| 1106 | "-advertise-npn", "\x03foo\x03bar\x03baz", |
| 1107 | "-expect-next-proto", "bar"), |
| 1108 | }) |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 1109 | |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 1110 | // Client does False Start and negotiates NPN. |
| 1111 | testCases = append(testCases, testCase{ |
| 1112 | protocol: protocol, |
| 1113 | name: "FalseStart" + suffix, |
| 1114 | config: Config{ |
| 1115 | CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256}, |
| 1116 | NextProtos: []string{"foo"}, |
| 1117 | Bugs: ProtocolBugs{ |
| 1118 | MaxHandshakeRecordLength: maxHandshakeRecordLength, |
| 1119 | }, |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 1120 | }, |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 1121 | flags: append(flags, |
| 1122 | "-false-start", |
| 1123 | "-select-next-proto", "foo"), |
| 1124 | resumeSession: true, |
| 1125 | }) |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 1126 | |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 1127 | // False Start without session tickets. |
| 1128 | testCases = append(testCases, testCase{ |
| 1129 | name: "FalseStart-SessionTicketsDisabled", |
| 1130 | config: Config{ |
| 1131 | CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256}, |
| 1132 | NextProtos: []string{"foo"}, |
| 1133 | SessionTicketsDisabled: true, |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 1134 | }, |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 1135 | flags: []string{ |
| 1136 | "-false-start", |
| 1137 | "-select-next-proto", "foo", |
| 1138 | }, |
| 1139 | }) |
David Benjamin | 1e7f8d7 | 2014-08-08 12:27:04 -0400 | [diff] [blame] | 1140 | |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 1141 | // Client sends a V2ClientHello. |
| 1142 | testCases = append(testCases, testCase{ |
| 1143 | protocol: protocol, |
| 1144 | testType: serverTest, |
| 1145 | name: "SendV2ClientHello" + suffix, |
| 1146 | config: Config{ |
| 1147 | // Choose a cipher suite that does not involve |
| 1148 | // elliptic curves, so no extensions are |
| 1149 | // involved. |
| 1150 | CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA}, |
| 1151 | Bugs: ProtocolBugs{ |
| 1152 | MaxHandshakeRecordLength: maxHandshakeRecordLength, |
| 1153 | SendV2ClientHello: true, |
| 1154 | }, |
David Benjamin | 1e7f8d7 | 2014-08-08 12:27:04 -0400 | [diff] [blame] | 1155 | }, |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 1156 | flags: flags, |
| 1157 | }) |
| 1158 | } else { |
| 1159 | testCases = append(testCases, testCase{ |
| 1160 | protocol: protocol, |
| 1161 | name: "SkipHelloVerifyRequest" + suffix, |
| 1162 | config: Config{ |
| 1163 | Bugs: ProtocolBugs{ |
| 1164 | MaxHandshakeRecordLength: maxHandshakeRecordLength, |
| 1165 | SkipHelloVerifyRequest: true, |
| 1166 | }, |
| 1167 | }, |
| 1168 | flags: flags, |
| 1169 | }) |
| 1170 | |
| 1171 | testCases = append(testCases, testCase{ |
| 1172 | testType: serverTest, |
| 1173 | protocol: protocol, |
| 1174 | name: "CookieExchange" + suffix, |
| 1175 | config: Config{ |
| 1176 | Bugs: ProtocolBugs{ |
| 1177 | MaxHandshakeRecordLength: maxHandshakeRecordLength, |
| 1178 | }, |
| 1179 | }, |
| 1180 | flags: append(flags, "-cookie-exchange"), |
| 1181 | }) |
| 1182 | } |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 1183 | } |
| 1184 | |
David Benjamin | 7e2e6cf | 2014-08-07 17:44:24 -0400 | [diff] [blame] | 1185 | func addVersionNegotiationTests() { |
| 1186 | for i, shimVers := range tlsVersions { |
| 1187 | // Assemble flags to disable all newer versions on the shim. |
| 1188 | var flags []string |
| 1189 | for _, vers := range tlsVersions[i+1:] { |
| 1190 | flags = append(flags, vers.flag) |
| 1191 | } |
| 1192 | |
| 1193 | for _, runnerVers := range tlsVersions { |
| 1194 | expectedVersion := shimVers.version |
| 1195 | if runnerVers.version < shimVers.version { |
| 1196 | expectedVersion = runnerVers.version |
| 1197 | } |
| 1198 | suffix := shimVers.name + "-" + runnerVers.name |
| 1199 | |
| 1200 | testCases = append(testCases, testCase{ |
| 1201 | testType: clientTest, |
| 1202 | name: "VersionNegotiation-Client-" + suffix, |
| 1203 | config: Config{ |
| 1204 | MaxVersion: runnerVers.version, |
| 1205 | }, |
| 1206 | flags: flags, |
| 1207 | expectedVersion: expectedVersion, |
| 1208 | }) |
| 1209 | |
David Benjamin | 76d8abe | 2014-08-14 16:25:34 -0400 | [diff] [blame] | 1210 | testCases = append(testCases, testCase{ |
| 1211 | testType: serverTest, |
| 1212 | name: "VersionNegotiation-Server-" + suffix, |
| 1213 | config: Config{ |
| 1214 | MaxVersion: runnerVers.version, |
| 1215 | }, |
| 1216 | flags: flags, |
| 1217 | expectedVersion: expectedVersion, |
| 1218 | }) |
David Benjamin | 7e2e6cf | 2014-08-07 17:44:24 -0400 | [diff] [blame] | 1219 | } |
| 1220 | } |
| 1221 | } |
| 1222 | |
David Benjamin | 884fdf1 | 2014-08-02 15:28:23 -0400 | [diff] [blame] | 1223 | func worker(statusChan chan statusMsg, c chan *testCase, buildDir string, wg *sync.WaitGroup) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1224 | defer wg.Done() |
| 1225 | |
| 1226 | for test := range c { |
| 1227 | statusChan <- statusMsg{test: test, started: true} |
David Benjamin | 884fdf1 | 2014-08-02 15:28:23 -0400 | [diff] [blame] | 1228 | err := runTest(test, buildDir) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1229 | statusChan <- statusMsg{test: test, err: err} |
| 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | type statusMsg struct { |
| 1234 | test *testCase |
| 1235 | started bool |
| 1236 | err error |
| 1237 | } |
| 1238 | |
| 1239 | func statusPrinter(doneChan chan struct{}, statusChan chan statusMsg, total int) { |
| 1240 | var started, done, failed, lineLen int |
| 1241 | defer close(doneChan) |
| 1242 | |
| 1243 | for msg := range statusChan { |
| 1244 | if msg.started { |
| 1245 | started++ |
| 1246 | } else { |
| 1247 | done++ |
| 1248 | } |
| 1249 | |
| 1250 | fmt.Printf("\x1b[%dD\x1b[K", lineLen) |
| 1251 | |
| 1252 | if msg.err != nil { |
| 1253 | fmt.Printf("FAILED (%s)\n%s\n", msg.test.name, msg.err) |
| 1254 | failed++ |
| 1255 | } |
| 1256 | line := fmt.Sprintf("%d/%d/%d/%d", failed, done, started, total) |
| 1257 | lineLen = len(line) |
| 1258 | os.Stdout.WriteString(line) |
| 1259 | } |
| 1260 | } |
| 1261 | |
| 1262 | func main() { |
| 1263 | var flagTest *string = flag.String("test", "", "The name of a test to run, or empty to run all tests") |
David Benjamin | 2bc8e6f | 2014-08-02 15:22:37 -0400 | [diff] [blame] | 1264 | var flagNumWorkers *int = flag.Int("num-workers", runtime.NumCPU(), "The number of workers to run in parallel.") |
David Benjamin | 884fdf1 | 2014-08-02 15:28:23 -0400 | [diff] [blame] | 1265 | var flagBuildDir *string = flag.String("build-dir", "../../../build", "The build directory to run the shim from.") |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1266 | |
| 1267 | flag.Parse() |
| 1268 | |
| 1269 | addCipherSuiteTests() |
| 1270 | addBadECDSASignatureTests() |
Adam Langley | 80842bd | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1271 | addCBCPaddingTests() |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 1272 | addCBCSplittingTests() |
David Benjamin | 636293b | 2014-07-08 17:59:18 -0400 | [diff] [blame] | 1273 | addClientAuthTests() |
David Benjamin | 7e2e6cf | 2014-08-07 17:44:24 -0400 | [diff] [blame] | 1274 | addVersionNegotiationTests() |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 1275 | for _, async := range []bool{false, true} { |
| 1276 | for _, splitHandshake := range []bool{false, true} { |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 1277 | for _, protocol := range []protocol{tls, dtls} { |
| 1278 | addStateMachineCoverageTests(async, splitHandshake, protocol) |
| 1279 | } |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 1280 | } |
| 1281 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1282 | |
| 1283 | var wg sync.WaitGroup |
| 1284 | |
David Benjamin | 2bc8e6f | 2014-08-02 15:22:37 -0400 | [diff] [blame] | 1285 | numWorkers := *flagNumWorkers |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1286 | |
| 1287 | statusChan := make(chan statusMsg, numWorkers) |
| 1288 | testChan := make(chan *testCase, numWorkers) |
| 1289 | doneChan := make(chan struct{}) |
| 1290 | |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 1291 | go statusPrinter(doneChan, statusChan, len(testCases)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1292 | |
| 1293 | for i := 0; i < numWorkers; i++ { |
| 1294 | wg.Add(1) |
David Benjamin | 884fdf1 | 2014-08-02 15:28:23 -0400 | [diff] [blame] | 1295 | go worker(statusChan, testChan, *flagBuildDir, &wg) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1296 | } |
| 1297 | |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 1298 | for i := range testCases { |
| 1299 | if len(*flagTest) == 0 || *flagTest == testCases[i].name { |
| 1300 | testChan <- &testCases[i] |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1301 | } |
| 1302 | } |
| 1303 | |
| 1304 | close(testChan) |
| 1305 | wg.Wait() |
| 1306 | close(statusChan) |
| 1307 | <-doneChan |
| 1308 | |
| 1309 | fmt.Printf("\n") |
| 1310 | } |