github.com/ader1990/go@v0.0.0-20140630135419-8c24447fa791/src/pkg/crypto/tls/handshake_server_test.go (about) 1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package tls 6 7 import ( 8 "bytes" 9 "crypto/ecdsa" 10 "crypto/elliptic" 11 "crypto/rsa" 12 "crypto/x509" 13 "encoding/hex" 14 "encoding/pem" 15 "errors" 16 "fmt" 17 "io" 18 "math/big" 19 "net" 20 "os" 21 "os/exec" 22 "path/filepath" 23 "strings" 24 "testing" 25 "time" 26 ) 27 28 // zeroSource is an io.Reader that returns an unlimited number of zero bytes. 29 type zeroSource struct{} 30 31 func (zeroSource) Read(b []byte) (n int, err error) { 32 for i := range b { 33 b[i] = 0 34 } 35 36 return len(b), nil 37 } 38 39 var testConfig *Config 40 41 func init() { 42 testConfig = &Config{ 43 Time: func() time.Time { return time.Unix(0, 0) }, 44 Rand: zeroSource{}, 45 Certificates: make([]Certificate, 2), 46 InsecureSkipVerify: true, 47 MinVersion: VersionSSL30, 48 MaxVersion: VersionTLS12, 49 } 50 testConfig.Certificates[0].Certificate = [][]byte{testRSACertificate} 51 testConfig.Certificates[0].PrivateKey = testRSAPrivateKey 52 testConfig.Certificates[1].Certificate = [][]byte{testSNICertificate} 53 testConfig.Certificates[1].PrivateKey = testRSAPrivateKey 54 testConfig.BuildNameToCertificate() 55 } 56 57 func testClientHelloFailure(t *testing.T, m handshakeMessage, expectedSubStr string) { 58 // Create in-memory network connection, 59 // send message to server. Should return 60 // expected error. 61 c, s := net.Pipe() 62 go func() { 63 cli := Client(c, testConfig) 64 if ch, ok := m.(*clientHelloMsg); ok { 65 cli.vers = ch.vers 66 } 67 cli.writeRecord(recordTypeHandshake, m.marshal()) 68 c.Close() 69 }() 70 err := Server(s, testConfig).Handshake() 71 s.Close() 72 if err == nil || !strings.Contains(err.Error(), expectedSubStr) { 73 t.Errorf("Got error: %s; expected to match substring '%s'", err, expectedSubStr) 74 } 75 } 76 77 func TestSimpleError(t *testing.T) { 78 testClientHelloFailure(t, &serverHelloDoneMsg{}, "unexpected handshake message") 79 } 80 81 var badProtocolVersions = []uint16{0x0000, 0x0005, 0x0100, 0x0105, 0x0200, 0x0205} 82 83 func TestRejectBadProtocolVersion(t *testing.T) { 84 for _, v := range badProtocolVersions { 85 testClientHelloFailure(t, &clientHelloMsg{vers: v}, "unsupported, maximum protocol version") 86 } 87 } 88 89 func TestNoSuiteOverlap(t *testing.T) { 90 clientHello := &clientHelloMsg{ 91 vers: 0x0301, 92 cipherSuites: []uint16{0xff00}, 93 compressionMethods: []uint8{0}, 94 } 95 testClientHelloFailure(t, clientHello, "no cipher suite supported by both client and server") 96 } 97 98 func TestNoCompressionOverlap(t *testing.T) { 99 clientHello := &clientHelloMsg{ 100 vers: 0x0301, 101 cipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA}, 102 compressionMethods: []uint8{0xff}, 103 } 104 testClientHelloFailure(t, clientHello, "client does not support uncompressed connections") 105 } 106 107 func TestTLS12OnlyCipherSuites(t *testing.T) { 108 // Test that a Server doesn't select a TLS 1.2-only cipher suite when 109 // the client negotiates TLS 1.1. 110 var zeros [32]byte 111 112 clientHello := &clientHelloMsg{ 113 vers: VersionTLS11, 114 random: zeros[:], 115 cipherSuites: []uint16{ 116 // The Server, by default, will use the client's 117 // preference order. So the GCM cipher suite 118 // will be selected unless it's excluded because 119 // of the version in this ClientHello. 120 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 121 TLS_RSA_WITH_RC4_128_SHA, 122 }, 123 compressionMethods: []uint8{compressionNone}, 124 supportedCurves: []CurveID{CurveP256, CurveP384, CurveP521}, 125 supportedPoints: []uint8{pointFormatUncompressed}, 126 } 127 128 c, s := net.Pipe() 129 var reply interface{} 130 var clientErr error 131 go func() { 132 cli := Client(c, testConfig) 133 cli.vers = clientHello.vers 134 cli.writeRecord(recordTypeHandshake, clientHello.marshal()) 135 reply, clientErr = cli.readHandshake() 136 c.Close() 137 }() 138 config := *testConfig 139 config.CipherSuites = clientHello.cipherSuites 140 Server(s, &config).Handshake() 141 s.Close() 142 if clientErr != nil { 143 t.Fatal(clientErr) 144 } 145 serverHello, ok := reply.(*serverHelloMsg) 146 if !ok { 147 t.Fatalf("didn't get ServerHello message in reply. Got %v\n", reply) 148 } 149 if s := serverHello.cipherSuite; s != TLS_RSA_WITH_RC4_128_SHA { 150 t.Fatalf("bad cipher suite from server: %x", s) 151 } 152 } 153 154 func TestAlertForwarding(t *testing.T) { 155 c, s := net.Pipe() 156 go func() { 157 Client(c, testConfig).sendAlert(alertUnknownCA) 158 c.Close() 159 }() 160 161 err := Server(s, testConfig).Handshake() 162 s.Close() 163 if e, ok := err.(*net.OpError); !ok || e.Err != error(alertUnknownCA) { 164 t.Errorf("Got error: %s; expected: %s", err, error(alertUnknownCA)) 165 } 166 } 167 168 func TestClose(t *testing.T) { 169 c, s := net.Pipe() 170 go c.Close() 171 172 err := Server(s, testConfig).Handshake() 173 s.Close() 174 if err != io.EOF { 175 t.Errorf("Got error: %s; expected: %s", err, io.EOF) 176 } 177 } 178 179 func testHandshake(clientConfig, serverConfig *Config) (state ConnectionState, err error) { 180 c, s := net.Pipe() 181 done := make(chan bool) 182 go func() { 183 cli := Client(c, clientConfig) 184 cli.Handshake() 185 c.Close() 186 done <- true 187 }() 188 server := Server(s, serverConfig) 189 err = server.Handshake() 190 if err == nil { 191 state = server.ConnectionState() 192 } 193 s.Close() 194 <-done 195 return 196 } 197 198 func TestVersion(t *testing.T) { 199 serverConfig := &Config{ 200 Certificates: testConfig.Certificates, 201 MaxVersion: VersionTLS11, 202 } 203 clientConfig := &Config{ 204 InsecureSkipVerify: true, 205 } 206 state, err := testHandshake(clientConfig, serverConfig) 207 if err != nil { 208 t.Fatalf("handshake failed: %s", err) 209 } 210 if state.Version != VersionTLS11 { 211 t.Fatalf("Incorrect version %x, should be %x", state.Version, VersionTLS11) 212 } 213 } 214 215 func TestCipherSuitePreference(t *testing.T) { 216 serverConfig := &Config{ 217 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA, TLS_RSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_RSA_WITH_RC4_128_SHA}, 218 Certificates: testConfig.Certificates, 219 MaxVersion: VersionTLS11, 220 } 221 clientConfig := &Config{ 222 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA, TLS_RSA_WITH_RC4_128_SHA}, 223 InsecureSkipVerify: true, 224 } 225 state, err := testHandshake(clientConfig, serverConfig) 226 if err != nil { 227 t.Fatalf("handshake failed: %s", err) 228 } 229 if state.CipherSuite != TLS_RSA_WITH_AES_128_CBC_SHA { 230 // By default the server should use the client's preference. 231 t.Fatalf("Client's preference was not used, got %x", state.CipherSuite) 232 } 233 234 serverConfig.PreferServerCipherSuites = true 235 state, err = testHandshake(clientConfig, serverConfig) 236 if err != nil { 237 t.Fatalf("handshake failed: %s", err) 238 } 239 if state.CipherSuite != TLS_RSA_WITH_RC4_128_SHA { 240 t.Fatalf("Server's preference was not used, got %x", state.CipherSuite) 241 } 242 } 243 244 // Note: see comment in handshake_test.go for details of how the reference 245 // tests work. 246 247 // serverTest represents a test of the TLS server handshake against a reference 248 // implementation. 249 type serverTest struct { 250 // name is a freeform string identifying the test and the file in which 251 // the expected results will be stored. 252 name string 253 // command, if not empty, contains a series of arguments for the 254 // command to run for the reference server. 255 command []string 256 // expectedPeerCerts contains a list of PEM blocks of expected 257 // certificates from the client. 258 expectedPeerCerts []string 259 // config, if not nil, contains a custom Config to use for this test. 260 config *Config 261 } 262 263 var defaultClientCommand = []string{"openssl", "s_client", "-no_ticket"} 264 265 // connFromCommand starts opens a listening socket and starts the reference 266 // client to connect to it. It returns a recordingConn that wraps the resulting 267 // connection. 268 func (test *serverTest) connFromCommand() (conn *recordingConn, child *exec.Cmd, err error) { 269 l, err := net.ListenTCP("tcp", &net.TCPAddr{ 270 IP: net.IPv4(127, 0, 0, 1), 271 Port: 0, 272 }) 273 if err != nil { 274 return nil, nil, err 275 } 276 defer l.Close() 277 278 port := l.Addr().(*net.TCPAddr).Port 279 280 var command []string 281 command = append(command, test.command...) 282 if len(command) == 0 { 283 command = defaultClientCommand 284 } 285 command = append(command, "-connect") 286 command = append(command, fmt.Sprintf("127.0.0.1:%d", port)) 287 cmd := exec.Command(command[0], command[1:]...) 288 cmd.Stdin = nil 289 var output bytes.Buffer 290 cmd.Stdout = &output 291 cmd.Stderr = &output 292 if err := cmd.Start(); err != nil { 293 return nil, nil, err 294 } 295 296 connChan := make(chan interface{}) 297 go func() { 298 tcpConn, err := l.Accept() 299 if err != nil { 300 connChan <- err 301 } 302 connChan <- tcpConn 303 }() 304 305 var tcpConn net.Conn 306 select { 307 case connOrError := <-connChan: 308 if err, ok := connOrError.(error); ok { 309 return nil, nil, err 310 } 311 tcpConn = connOrError.(net.Conn) 312 case <-time.After(2 * time.Second): 313 output.WriteTo(os.Stdout) 314 return nil, nil, errors.New("timed out waiting for connection from child process") 315 } 316 317 record := &recordingConn{ 318 Conn: tcpConn, 319 } 320 321 return record, cmd, nil 322 } 323 324 func (test *serverTest) dataPath() string { 325 return filepath.Join("testdata", "Server-"+test.name) 326 } 327 328 func (test *serverTest) loadData() (flows [][]byte, err error) { 329 in, err := os.Open(test.dataPath()) 330 if err != nil { 331 return nil, err 332 } 333 defer in.Close() 334 return parseTestData(in) 335 } 336 337 func (test *serverTest) run(t *testing.T, write bool) { 338 var clientConn, serverConn net.Conn 339 var recordingConn *recordingConn 340 var childProcess *exec.Cmd 341 342 if write { 343 var err error 344 recordingConn, childProcess, err = test.connFromCommand() 345 if err != nil { 346 t.Fatalf("Failed to start subcommand: %s", err) 347 } 348 serverConn = recordingConn 349 } else { 350 clientConn, serverConn = net.Pipe() 351 } 352 config := test.config 353 if config == nil { 354 config = testConfig 355 } 356 server := Server(serverConn, config) 357 peerCertsChan := make(chan []*x509.Certificate, 1) 358 go func() { 359 if _, err := server.Write([]byte("hello, world\n")); err != nil { 360 t.Logf("Error from Server.Write: %s", err) 361 } 362 server.Close() 363 serverConn.Close() 364 peerCertsChan <- server.ConnectionState().PeerCertificates 365 }() 366 367 if !write { 368 flows, err := test.loadData() 369 if err != nil { 370 t.Fatalf("%s: failed to load data from %s", test.name, test.dataPath()) 371 } 372 for i, b := range flows { 373 if i%2 == 0 { 374 clientConn.Write(b) 375 continue 376 } 377 bb := make([]byte, len(b)) 378 n, err := io.ReadFull(clientConn, bb) 379 if err != nil { 380 t.Fatalf("%s #%d: %s\nRead %d, wanted %d, got %x, wanted %x\n", test.name, i+1, err, n, len(bb), bb[:n], b) 381 } 382 if !bytes.Equal(b, bb) { 383 t.Fatalf("%s #%d: mismatch on read: got:%x want:%x", test.name, i+1, bb, b) 384 } 385 } 386 clientConn.Close() 387 } 388 389 peerCerts := <-peerCertsChan 390 if len(peerCerts) == len(test.expectedPeerCerts) { 391 for i, peerCert := range peerCerts { 392 block, _ := pem.Decode([]byte(test.expectedPeerCerts[i])) 393 if !bytes.Equal(block.Bytes, peerCert.Raw) { 394 t.Fatalf("%s: mismatch on peer cert %d", test.name, i+1) 395 } 396 } 397 } else { 398 t.Fatalf("%s: mismatch on peer list length: %d (wanted) != %d (got)", test.name, len(test.expectedPeerCerts), len(peerCerts)) 399 } 400 401 if write { 402 path := test.dataPath() 403 out, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) 404 if err != nil { 405 t.Fatalf("Failed to create output file: %s", err) 406 } 407 defer out.Close() 408 recordingConn.Close() 409 if len(recordingConn.flows) < 3 { 410 childProcess.Stdout.(*bytes.Buffer).WriteTo(os.Stdout) 411 t.Fatalf("Handshake failed") 412 } 413 recordingConn.WriteTo(out) 414 fmt.Printf("Wrote %s\n", path) 415 childProcess.Wait() 416 } 417 } 418 419 func runServerTestForVersion(t *testing.T, template *serverTest, prefix, option string) { 420 test := *template 421 test.name = prefix + test.name 422 if len(test.command) == 0 { 423 test.command = defaultClientCommand 424 } 425 test.command = append([]string(nil), test.command...) 426 test.command = append(test.command, option) 427 test.run(t, *update) 428 } 429 430 func runServerTestSSLv3(t *testing.T, template *serverTest) { 431 runServerTestForVersion(t, template, "SSLv3-", "-ssl3") 432 } 433 434 func runServerTestTLS10(t *testing.T, template *serverTest) { 435 runServerTestForVersion(t, template, "TLSv10-", "-tls1") 436 } 437 438 func runServerTestTLS11(t *testing.T, template *serverTest) { 439 runServerTestForVersion(t, template, "TLSv11-", "-tls1_1") 440 } 441 442 func runServerTestTLS12(t *testing.T, template *serverTest) { 443 runServerTestForVersion(t, template, "TLSv12-", "-tls1_2") 444 } 445 446 func TestHandshakeServerRSARC4(t *testing.T) { 447 test := &serverTest{ 448 name: "RSA-RC4", 449 command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "RC4-SHA"}, 450 } 451 runServerTestSSLv3(t, test) 452 runServerTestTLS10(t, test) 453 runServerTestTLS11(t, test) 454 runServerTestTLS12(t, test) 455 } 456 457 func TestHandshakeServerRSA3DES(t *testing.T) { 458 test := &serverTest{ 459 name: "RSA-3DES", 460 command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "DES-CBC3-SHA"}, 461 } 462 runServerTestSSLv3(t, test) 463 runServerTestTLS10(t, test) 464 runServerTestTLS12(t, test) 465 } 466 467 func TestHandshakeServerRSAAES(t *testing.T) { 468 test := &serverTest{ 469 name: "RSA-AES", 470 command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "AES128-SHA"}, 471 } 472 runServerTestSSLv3(t, test) 473 runServerTestTLS10(t, test) 474 runServerTestTLS12(t, test) 475 } 476 477 func TestHandshakeServerAESGCM(t *testing.T) { 478 test := &serverTest{ 479 name: "RSA-AES-GCM", 480 command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "ECDHE-RSA-AES128-GCM-SHA256"}, 481 } 482 runServerTestTLS12(t, test) 483 } 484 485 func TestHandshakeServerECDHEECDSAAES(t *testing.T) { 486 config := *testConfig 487 config.Certificates = make([]Certificate, 1) 488 config.Certificates[0].Certificate = [][]byte{testECDSACertificate} 489 config.Certificates[0].PrivateKey = testECDSAPrivateKey 490 config.BuildNameToCertificate() 491 492 test := &serverTest{ 493 name: "ECDHE-ECDSA-AES", 494 command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "ECDHE-ECDSA-AES256-SHA"}, 495 config: &config, 496 } 497 runServerTestTLS10(t, test) 498 runServerTestTLS12(t, test) 499 } 500 501 // TestHandshakeServerSNI involves a client sending an SNI extension of 502 // "snitest.com", which happens to match the CN of testSNICertificate. The test 503 // verifies that the server correctly selects that certificate. 504 func TestHandshakeServerSNI(t *testing.T) { 505 test := &serverTest{ 506 name: "SNI", 507 command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "AES128-SHA", "-servername", "snitest.com"}, 508 } 509 runServerTestTLS12(t, test) 510 } 511 512 // TestCipherSuiteCertPreferance ensures that we select an RSA ciphersuite with 513 // an RSA certificate and an ECDSA ciphersuite with an ECDSA certificate. 514 func TestCipherSuiteCertPreferenceECDSA(t *testing.T) { 515 config := *testConfig 516 config.CipherSuites = []uint16{TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA} 517 config.PreferServerCipherSuites = true 518 519 test := &serverTest{ 520 name: "CipherSuiteCertPreferenceRSA", 521 config: &config, 522 } 523 runServerTestTLS12(t, test) 524 525 config = *testConfig 526 config.CipherSuites = []uint16{TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA} 527 config.Certificates = []Certificate{ 528 Certificate{ 529 Certificate: [][]byte{testECDSACertificate}, 530 PrivateKey: testECDSAPrivateKey, 531 }, 532 } 533 config.BuildNameToCertificate() 534 config.PreferServerCipherSuites = true 535 536 test = &serverTest{ 537 name: "CipherSuiteCertPreferenceECDSA", 538 config: &config, 539 } 540 runServerTestTLS12(t, test) 541 } 542 543 func TestResumption(t *testing.T) { 544 sessionFilePath := tempFile("") 545 defer os.Remove(sessionFilePath) 546 547 test := &serverTest{ 548 name: "IssueTicket", 549 command: []string{"openssl", "s_client", "-cipher", "RC4-SHA", "-sess_out", sessionFilePath}, 550 } 551 runServerTestTLS12(t, test) 552 553 test = &serverTest{ 554 name: "Resume", 555 command: []string{"openssl", "s_client", "-cipher", "RC4-SHA", "-sess_in", sessionFilePath}, 556 } 557 runServerTestTLS12(t, test) 558 } 559 560 // cert.pem and key.pem were generated with generate_cert.go 561 // Thus, they have no ExtKeyUsage fields and trigger an error 562 // when verification is turned on. 563 564 const clientCertificatePEM = ` 565 -----BEGIN CERTIFICATE----- 566 MIIB7TCCAVigAwIBAgIBADALBgkqhkiG9w0BAQUwJjEQMA4GA1UEChMHQWNtZSBD 567 bzESMBAGA1UEAxMJMTI3LjAuMC4xMB4XDTExMTIwODA3NTUxMloXDTEyMTIwNzA4 568 MDAxMlowJjEQMA4GA1UEChMHQWNtZSBDbzESMBAGA1UEAxMJMTI3LjAuMC4xMIGc 569 MAsGCSqGSIb3DQEBAQOBjAAwgYgCgYBO0Hsx44Jk2VnAwoekXh6LczPHY1PfZpIG 570 hPZk1Y/kNqcdK+izIDZFI7Xjla7t4PUgnI2V339aEu+H5Fto5OkOdOwEin/ekyfE 571 ARl6vfLcPRSr0FTKIQzQTW6HLlzF0rtNS0/Otiz3fojsfNcCkXSmHgwa2uNKWi7e 572 E5xMQIhZkwIDAQABozIwMDAOBgNVHQ8BAf8EBAMCAKAwDQYDVR0OBAYEBAECAwQw 573 DwYDVR0jBAgwBoAEAQIDBDALBgkqhkiG9w0BAQUDgYEANh+zegx1yW43RmEr1b3A 574 p0vMRpqBWHyFeSnIyMZn3TJWRSt1tukkqVCavh9a+hoV2cxVlXIWg7nCto/9iIw4 575 hB2rXZIxE0/9gzvGnfERYraL7KtnvshksBFQRlgXa5kc0x38BvEO5ZaoDPl4ILdE 576 GFGNEH5PlGffo05wc46QkYU= 577 -----END CERTIFICATE-----` 578 579 const clientKeyPEM = ` 580 -----BEGIN RSA PRIVATE KEY----- 581 MIICWgIBAAKBgE7QezHjgmTZWcDCh6ReHotzM8djU99mkgaE9mTVj+Q2px0r6LMg 582 NkUjteOVru3g9SCcjZXff1oS74fkW2jk6Q507ASKf96TJ8QBGXq98tw9FKvQVMoh 583 DNBNbocuXMXSu01LT862LPd+iOx81wKRdKYeDBra40paLt4TnExAiFmTAgMBAAEC 584 gYBxvXd8yNteFTns8A/2yomEMC4yeosJJSpp1CsN3BJ7g8/qTnrVPxBy+RU+qr63 585 t2WquaOu/cr5P8iEsa6lk20tf8pjKLNXeX0b1RTzK8rJLbS7nGzP3tvOhL096VtQ 586 dAo4ROEaro0TzYpHmpciSvxVIeEIAAdFDObDJPKqcJAxyQJBAJizfYgK8Gzx9fsx 587 hxp+VteCbVPg2euASH5Yv3K5LukRdKoSzHE2grUVQgN/LafC0eZibRanxHegYSr7 588 7qaswKUCQQCEIWor/X4XTMdVj3Oj+vpiw75y/S9gh682+myZL+d/02IEkwnB098P 589 RkKVpenBHyrGg0oeN5La7URILWKj7CPXAkBKo6F+d+phNjwIFoN1Xb/RA32w/D1I 590 saG9sF+UEhRt9AxUfW/U/tIQ9V0ZHHcSg1XaCM5Nvp934brdKdvTOKnJAkBD5h/3 591 Rybatlvg/fzBEaJFyq09zhngkxlZOUtBVTqzl17RVvY2orgH02U4HbCHy4phxOn7 592 qTdQRYlHRftgnWK1AkANibn9PRYJ7mJyJ9Dyj2QeNcSkSTzrt0tPvUMf4+meJymN 593 1Ntu5+S1DLLzfxlaljWG6ylW6DNxujCyuXIV2rvA 594 -----END RSA PRIVATE KEY-----` 595 596 const clientECDSACertificatePEM = ` 597 -----BEGIN CERTIFICATE----- 598 MIIB/DCCAV4CCQCaMIRsJjXZFzAJBgcqhkjOPQQBMEUxCzAJBgNVBAYTAkFVMRMw 599 EQYDVQQIEwpTb21lLVN0YXRlMSEwHwYDVQQKExhJbnRlcm5ldCBXaWRnaXRzIFB0 600 eSBMdGQwHhcNMTIxMTE0MTMyNTUzWhcNMjIxMTEyMTMyNTUzWjBBMQswCQYDVQQG 601 EwJBVTEMMAoGA1UECBMDTlNXMRAwDgYDVQQHEwdQeXJtb250MRIwEAYDVQQDEwlK 602 b2VsIFNpbmcwgZswEAYHKoZIzj0CAQYFK4EEACMDgYYABACVjJF1FMBexFe01MNv 603 ja5oHt1vzobhfm6ySD6B5U7ixohLZNz1MLvT/2XMW/TdtWo+PtAd3kfDdq0Z9kUs 604 jLzYHQFMH3CQRnZIi4+DzEpcj0B22uCJ7B0rxE4wdihBsmKo+1vx+U56jb0JuK7q 605 ixgnTy5w/hOWusPTQBbNZU6sER7m8TAJBgcqhkjOPQQBA4GMADCBiAJCAOAUxGBg 606 C3JosDJdYUoCdFzCgbkWqD8pyDbHgf9stlvZcPE4O1BIKJTLCRpS8V3ujfK58PDa 607 2RU6+b0DeoeiIzXsAkIBo9SKeDUcSpoj0gq+KxAxnZxfvuiRs9oa9V2jI/Umi0Vw 608 jWVim34BmT0Y9hCaOGGbLlfk+syxis7iI6CH8OFnUes= 609 -----END CERTIFICATE-----` 610 611 const clientECDSAKeyPEM = ` 612 -----BEGIN EC PARAMETERS----- 613 BgUrgQQAIw== 614 -----END EC PARAMETERS----- 615 -----BEGIN EC PRIVATE KEY----- 616 MIHcAgEBBEIBkJN9X4IqZIguiEVKMqeBUP5xtRsEv4HJEtOpOGLELwO53SD78Ew8 617 k+wLWoqizS3NpQyMtrU8JFdWfj+C57UNkOugBwYFK4EEACOhgYkDgYYABACVjJF1 618 FMBexFe01MNvja5oHt1vzobhfm6ySD6B5U7ixohLZNz1MLvT/2XMW/TdtWo+PtAd 619 3kfDdq0Z9kUsjLzYHQFMH3CQRnZIi4+DzEpcj0B22uCJ7B0rxE4wdihBsmKo+1vx 620 +U56jb0JuK7qixgnTy5w/hOWusPTQBbNZU6sER7m8Q== 621 -----END EC PRIVATE KEY-----` 622 623 func TestClientAuth(t *testing.T) { 624 var certPath, keyPath, ecdsaCertPath, ecdsaKeyPath string 625 626 if *update { 627 certPath = tempFile(clientCertificatePEM) 628 defer os.Remove(certPath) 629 keyPath = tempFile(clientKeyPEM) 630 defer os.Remove(keyPath) 631 ecdsaCertPath = tempFile(clientECDSACertificatePEM) 632 defer os.Remove(ecdsaCertPath) 633 ecdsaKeyPath = tempFile(clientECDSAKeyPEM) 634 defer os.Remove(ecdsaKeyPath) 635 } 636 637 config := *testConfig 638 config.ClientAuth = RequestClientCert 639 640 test := &serverTest{ 641 name: "ClientAuthRequestedNotGiven", 642 command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "RC4-SHA"}, 643 config: &config, 644 } 645 runServerTestTLS12(t, test) 646 647 test = &serverTest{ 648 name: "ClientAuthRequestedAndGiven", 649 command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "RC4-SHA", "-cert", certPath, "-key", keyPath}, 650 config: &config, 651 expectedPeerCerts: []string{clientCertificatePEM}, 652 } 653 runServerTestTLS12(t, test) 654 655 test = &serverTest{ 656 name: "ClientAuthRequestedAndECDSAGiven", 657 command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "RC4-SHA", "-cert", ecdsaCertPath, "-key", ecdsaKeyPath}, 658 config: &config, 659 expectedPeerCerts: []string{clientECDSACertificatePEM}, 660 } 661 runServerTestTLS12(t, test) 662 } 663 664 func bigFromString(s string) *big.Int { 665 ret := new(big.Int) 666 ret.SetString(s, 10) 667 return ret 668 } 669 670 func fromHex(s string) []byte { 671 b, _ := hex.DecodeString(s) 672 return b 673 } 674 675 var testRSACertificate = fromHex("308202b030820219a00302010202090085b0bba48a7fb8ca300d06092a864886f70d01010505003045310b3009060355040613024155311330110603550408130a536f6d652d53746174653121301f060355040a1318496e7465726e6574205769646769747320507479204c7464301e170d3130303432343039303933385a170d3131303432343039303933385a3045310b3009060355040613024155311330110603550408130a536f6d652d53746174653121301f060355040a1318496e7465726e6574205769646769747320507479204c746430819f300d06092a864886f70d010101050003818d0030818902818100bb79d6f517b5e5bf4610d0dc69bee62b07435ad0032d8a7a4385b71452e7a5654c2c78b8238cb5b482e5de1f953b7e62a52ca533d6fe125c7a56fcf506bffa587b263fb5cd04d3d0c921964ac7f4549f5abfef427100fe1899077f7e887d7df10439c4a22edb51c97ce3c04c3b326601cfafb11db8719a1ddbdb896baeda2d790203010001a381a73081a4301d0603551d0e04160414b1ade2855acfcb28db69ce2369ded3268e18883930750603551d23046e306c8014b1ade2855acfcb28db69ce2369ded3268e188839a149a4473045310b3009060355040613024155311330110603550408130a536f6d652d53746174653121301f060355040a1318496e7465726e6574205769646769747320507479204c746482090085b0bba48a7fb8ca300c0603551d13040530030101ff300d06092a864886f70d010105050003818100086c4524c76bb159ab0c52ccf2b014d7879d7a6475b55a9566e4c52b8eae12661feb4f38b36e60d392fdf74108b52513b1187a24fb301dbaed98b917ece7d73159db95d31d78ea50565cd5825a2d5a5f33c4b6d8c97590968c0f5298b5cd981f89205ff2a01ca31b9694dda9fd57e970e8266d71999b266e3850296c90a7bdd9") 676 677 var testECDSACertificate = fromHex("3082020030820162020900b8bf2d47a0d2ebf4300906072a8648ce3d04013045310b3009060355040613024155311330110603550408130a536f6d652d53746174653121301f060355040a1318496e7465726e6574205769646769747320507479204c7464301e170d3132313132323135303633325a170d3232313132303135303633325a3045310b3009060355040613024155311330110603550408130a536f6d652d53746174653121301f060355040a1318496e7465726e6574205769646769747320507479204c746430819b301006072a8648ce3d020106052b81040023038186000400c4a1edbe98f90b4873367ec316561122f23d53c33b4d213dcd6b75e6f6b0dc9adf26c1bcb287f072327cb3642f1c90bcea6823107efee325c0483a69e0286dd33700ef0462dd0da09c706283d881d36431aa9e9731bd96b068c09b23de76643f1a5c7fe9120e5858b65f70dd9bd8ead5d7f5d5ccb9b69f30665b669a20e227e5bffe3b300906072a8648ce3d040103818c0030818802420188a24febe245c5487d1bacf5ed989dae4770c05e1bb62fbdf1b64db76140d311a2ceee0b7e927eff769dc33b7ea53fcefa10e259ec472d7cacda4e970e15a06fd00242014dfcbe67139c2d050ebd3fa38c25c13313830d9406bbd4377af6ec7ac9862eddd711697f857c56defb31782be4c7780daecbbe9e4e3624317b6a0f399512078f2a") 678 679 var testSNICertificate = fromHex("308201f23082015da003020102020100300b06092a864886f70d01010530283110300e060355040a130741636d6520436f311430120603550403130b736e69746573742e636f6d301e170d3132303431313137343033355a170d3133303431313137343533355a30283110300e060355040a130741636d6520436f311430120603550403130b736e69746573742e636f6d30819d300b06092a864886f70d01010103818d0030818902818100bb79d6f517b5e5bf4610d0dc69bee62b07435ad0032d8a7a4385b71452e7a5654c2c78b8238cb5b482e5de1f953b7e62a52ca533d6fe125c7a56fcf506bffa587b263fb5cd04d3d0c921964ac7f4549f5abfef427100fe1899077f7e887d7df10439c4a22edb51c97ce3c04c3b326601cfafb11db8719a1ddbdb896baeda2d790203010001a3323030300e0603551d0f0101ff0404030200a0300d0603551d0e0406040401020304300f0603551d2304083006800401020304300b06092a864886f70d0101050381810089c6455f1c1f5ef8eb1ab174ee2439059f5c4259bb1a8d86cdb1d056f56a717da40e95ab90f59e8deaf627c157995094db0802266eb34fc6842dea8a4b68d9c1389103ab84fb9e1f85d9b5d23ff2312c8670fbb540148245a4ebafe264d90c8a4cf4f85b0fac12ac2fc4a3154bad52462868af96c62c6525d652b6e31845bdcc") 680 681 var testRSAPrivateKey = &rsa.PrivateKey{ 682 PublicKey: rsa.PublicKey{ 683 N: bigFromString("131650079503776001033793877885499001334664249354723305978524647182322416328664556247316495448366990052837680518067798333412266673813370895702118944398081598789828837447552603077848001020611640547221687072142537202428102790818451901395596882588063427854225330436740647715202971973145151161964464812406232198521"), 684 E: 65537, 685 }, 686 D: bigFromString("29354450337804273969007277378287027274721892607543397931919078829901848876371746653677097639302788129485893852488285045793268732234230875671682624082413996177431586734171663258657462237320300610850244186316880055243099640544518318093544057213190320837094958164973959123058337475052510833916491060913053867729"), 687 Primes: []*big.Int{ 688 bigFromString("11969277782311800166562047708379380720136961987713178380670422671426759650127150688426177829077494755200794297055316163155755835813760102405344560929062149"), 689 bigFromString("10998999429884441391899182616418192492905073053684657075974935218461686523870125521822756579792315215543092255516093840728890783887287417039645833477273829"), 690 }, 691 } 692 693 var testECDSAPrivateKey = &ecdsa.PrivateKey{ 694 PublicKey: ecdsa.PublicKey{ 695 Curve: elliptic.P521(), 696 X: bigFromString("2636411247892461147287360222306590634450676461695221912739908880441342231985950069527906976759812296359387337367668045707086543273113073382714101597903639351"), 697 Y: bigFromString("3204695818431246682253994090650952614555094516658732116404513121125038617915183037601737180082382202488628239201196033284060130040574800684774115478859677243"), 698 }, 699 D: bigFromString("5477294338614160138026852784385529180817726002953041720191098180813046231640184669647735805135001309477695746518160084669446643325196003346204701381388769751"), 700 }