Alternative TLS implementation in Go
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

701 lignes
25 KiB

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