handshake_server_test.go 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  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. "encoding/hex"
  11. "encoding/pem"
  12. "errors"
  13. "fmt"
  14. "io"
  15. "math/big"
  16. "net"
  17. "os"
  18. "os/exec"
  19. "path/filepath"
  20. "strings"
  21. "testing"
  22. "time"
  23. )
  24. // zeroSource is an io.Reader that returns an unlimited number of zero bytes.
  25. type zeroSource struct{}
  26. func (zeroSource) Read(b []byte) (n int, err error) {
  27. for i := range b {
  28. b[i] = 0
  29. }
  30. return len(b), nil
  31. }
  32. var testConfig *Config
  33. func allCipherSuites() []uint16 {
  34. ids := make([]uint16, len(cipherSuites))
  35. for i, suite := range cipherSuites {
  36. ids[i] = suite.id
  37. }
  38. return ids
  39. }
  40. func init() {
  41. testConfig = &Config{
  42. Time: func() time.Time { return time.Unix(0, 0) },
  43. Rand: zeroSource{},
  44. Certificates: make([]Certificate, 2),
  45. InsecureSkipVerify: true,
  46. MinVersion: VersionSSL30,
  47. MaxVersion: VersionTLS12,
  48. CipherSuites: allCipherSuites(),
  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. func testClientHello(t *testing.T, serverConfig *Config, m handshakeMessage) {
  57. testClientHelloFailure(t, serverConfig, m, "")
  58. }
  59. func testClientHelloFailure(t *testing.T, serverConfig *Config, m handshakeMessage, expectedSubStr string) {
  60. // Create in-memory network connection,
  61. // send message to server. Should return
  62. // expected error.
  63. c, s := net.Pipe()
  64. go func() {
  65. cli := Client(c, testConfig)
  66. if ch, ok := m.(*clientHelloMsg); ok {
  67. cli.vers = ch.vers
  68. }
  69. cli.writeRecord(recordTypeHandshake, m.marshal())
  70. c.Close()
  71. }()
  72. hs := serverHandshakeState{
  73. c: Server(s, serverConfig),
  74. }
  75. _, err := hs.readClientHello()
  76. s.Close()
  77. if len(expectedSubStr) == 0 {
  78. if err != nil && err != io.EOF {
  79. t.Errorf("Got error: %s; expected to succeed", err)
  80. }
  81. } else if err == nil || !strings.Contains(err.Error(), expectedSubStr) {
  82. t.Errorf("Got error: %s; expected to match substring '%s'", err, expectedSubStr)
  83. }
  84. }
  85. func TestSimpleError(t *testing.T) {
  86. testClientHelloFailure(t, testConfig, &serverHelloDoneMsg{}, "unexpected handshake message")
  87. }
  88. var badProtocolVersions = []uint16{0x0000, 0x0005, 0x0100, 0x0105, 0x0200, 0x0205}
  89. func TestRejectBadProtocolVersion(t *testing.T) {
  90. for _, v := range badProtocolVersions {
  91. testClientHelloFailure(t, testConfig, &clientHelloMsg{vers: v}, "unsupported, maximum protocol version")
  92. }
  93. }
  94. func TestNoSuiteOverlap(t *testing.T) {
  95. clientHello := &clientHelloMsg{
  96. vers: VersionTLS10,
  97. cipherSuites: []uint16{0xff00},
  98. compressionMethods: []uint8{compressionNone},
  99. }
  100. testClientHelloFailure(t, testConfig, clientHello, "no cipher suite supported by both client and server")
  101. }
  102. func TestNoCompressionOverlap(t *testing.T) {
  103. clientHello := &clientHelloMsg{
  104. vers: VersionTLS10,
  105. cipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
  106. compressionMethods: []uint8{0xff},
  107. }
  108. testClientHelloFailure(t, testConfig, clientHello, "client does not support uncompressed connections")
  109. }
  110. func TestNoRC4ByDefault(t *testing.T) {
  111. clientHello := &clientHelloMsg{
  112. vers: VersionTLS10,
  113. cipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
  114. compressionMethods: []uint8{compressionNone},
  115. }
  116. serverConfig := testConfig.clone()
  117. // Reset the enabled cipher suites to nil in order to test the
  118. // defaults.
  119. serverConfig.CipherSuites = nil
  120. testClientHelloFailure(t, serverConfig, clientHello, "no cipher suite supported by both client and server")
  121. }
  122. func TestDontSelectECDSAWithRSAKey(t *testing.T) {
  123. // Test that, even when both sides support an ECDSA cipher suite, it
  124. // won't be selected if the server's private key doesn't support it.
  125. clientHello := &clientHelloMsg{
  126. vers: VersionTLS10,
  127. cipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA},
  128. compressionMethods: []uint8{compressionNone},
  129. supportedCurves: []CurveID{CurveP256},
  130. supportedPoints: []uint8{pointFormatUncompressed},
  131. }
  132. serverConfig := testConfig.clone()
  133. serverConfig.CipherSuites = clientHello.cipherSuites
  134. serverConfig.Certificates = make([]Certificate, 1)
  135. serverConfig.Certificates[0].Certificate = [][]byte{testECDSACertificate}
  136. serverConfig.Certificates[0].PrivateKey = testECDSAPrivateKey
  137. serverConfig.BuildNameToCertificate()
  138. // First test that it *does* work when the server's key is ECDSA.
  139. testClientHello(t, serverConfig, clientHello)
  140. // Now test that switching to an RSA key causes the expected error (and
  141. // not an internal error about a signing failure).
  142. serverConfig.Certificates = testConfig.Certificates
  143. testClientHelloFailure(t, serverConfig, clientHello, "no cipher suite supported by both client and server")
  144. }
  145. func TestDontSelectRSAWithECDSAKey(t *testing.T) {
  146. // Test that, even when both sides support an RSA cipher suite, it
  147. // won't be selected if the server's private key doesn't support it.
  148. clientHello := &clientHelloMsg{
  149. vers: VersionTLS10,
  150. cipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA},
  151. compressionMethods: []uint8{compressionNone},
  152. supportedCurves: []CurveID{CurveP256},
  153. supportedPoints: []uint8{pointFormatUncompressed},
  154. }
  155. serverConfig := testConfig.clone()
  156. serverConfig.CipherSuites = clientHello.cipherSuites
  157. // First test that it *does* work when the server's key is RSA.
  158. testClientHello(t, serverConfig, clientHello)
  159. // Now test that switching to an ECDSA key causes the expected error
  160. // (and not an internal error about a signing failure).
  161. serverConfig.Certificates = make([]Certificate, 1)
  162. serverConfig.Certificates[0].Certificate = [][]byte{testECDSACertificate}
  163. serverConfig.Certificates[0].PrivateKey = testECDSAPrivateKey
  164. serverConfig.BuildNameToCertificate()
  165. testClientHelloFailure(t, serverConfig, clientHello, "no cipher suite supported by both client and server")
  166. }
  167. func TestRenegotiationExtension(t *testing.T) {
  168. clientHello := &clientHelloMsg{
  169. vers: VersionTLS12,
  170. compressionMethods: []uint8{compressionNone},
  171. random: make([]byte, 32),
  172. secureRenegotiationSupported: true,
  173. cipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
  174. }
  175. var buf []byte
  176. c, s := net.Pipe()
  177. go func() {
  178. cli := Client(c, testConfig)
  179. cli.vers = clientHello.vers
  180. cli.writeRecord(recordTypeHandshake, clientHello.marshal())
  181. buf = make([]byte, 1024)
  182. n, err := c.Read(buf)
  183. if err != nil {
  184. t.Fatalf("Server read returned error: %s", err)
  185. }
  186. buf = buf[:n]
  187. c.Close()
  188. }()
  189. Server(s, testConfig).Handshake()
  190. if len(buf) < 5+4 {
  191. t.Fatalf("Server returned short message of length %d", len(buf))
  192. }
  193. // buf contains a TLS record, with a 5 byte record header and a 4 byte
  194. // handshake header. The length of the ServerHello is taken from the
  195. // handshake header.
  196. serverHelloLen := int(buf[6])<<16 | int(buf[7])<<8 | int(buf[8])
  197. var serverHello serverHelloMsg
  198. // unmarshal expects to be given the handshake header, but
  199. // serverHelloLen doesn't include it.
  200. if !serverHello.unmarshal(buf[5 : 9+serverHelloLen]) {
  201. t.Fatalf("Failed to parse ServerHello")
  202. }
  203. if !serverHello.secureRenegotiationSupported {
  204. t.Errorf("Secure renegotiation extension was not echoed.")
  205. }
  206. }
  207. func TestTLS12OnlyCipherSuites(t *testing.T) {
  208. // Test that a Server doesn't select a TLS 1.2-only cipher suite when
  209. // the client negotiates TLS 1.1.
  210. var zeros [32]byte
  211. clientHello := &clientHelloMsg{
  212. vers: VersionTLS11,
  213. random: zeros[:],
  214. cipherSuites: []uint16{
  215. // The Server, by default, will use the client's
  216. // preference order. So the GCM cipher suite
  217. // will be selected unless it's excluded because
  218. // of the version in this ClientHello.
  219. TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  220. TLS_RSA_WITH_RC4_128_SHA,
  221. },
  222. compressionMethods: []uint8{compressionNone},
  223. supportedCurves: []CurveID{CurveP256, CurveP384, CurveP521},
  224. supportedPoints: []uint8{pointFormatUncompressed},
  225. }
  226. c, s := net.Pipe()
  227. var reply interface{}
  228. var clientErr error
  229. go func() {
  230. cli := Client(c, testConfig)
  231. cli.vers = clientHello.vers
  232. cli.writeRecord(recordTypeHandshake, clientHello.marshal())
  233. reply, clientErr = cli.readHandshake()
  234. c.Close()
  235. }()
  236. config := testConfig.clone()
  237. config.CipherSuites = clientHello.cipherSuites
  238. Server(s, config).Handshake()
  239. s.Close()
  240. if clientErr != nil {
  241. t.Fatal(clientErr)
  242. }
  243. serverHello, ok := reply.(*serverHelloMsg)
  244. if !ok {
  245. t.Fatalf("didn't get ServerHello message in reply. Got %v\n", reply)
  246. }
  247. if s := serverHello.cipherSuite; s != TLS_RSA_WITH_RC4_128_SHA {
  248. t.Fatalf("bad cipher suite from server: %x", s)
  249. }
  250. }
  251. func TestAlertForwarding(t *testing.T) {
  252. c, s := net.Pipe()
  253. go func() {
  254. Client(c, testConfig).sendAlert(alertUnknownCA)
  255. c.Close()
  256. }()
  257. err := Server(s, testConfig).Handshake()
  258. s.Close()
  259. if e, ok := err.(*net.OpError); !ok || e.Err != error(alertUnknownCA) {
  260. t.Errorf("Got error: %s; expected: %s", err, error(alertUnknownCA))
  261. }
  262. }
  263. func TestClose(t *testing.T) {
  264. c, s := net.Pipe()
  265. go c.Close()
  266. err := Server(s, testConfig).Handshake()
  267. s.Close()
  268. if err != io.EOF {
  269. t.Errorf("Got error: %s; expected: %s", err, io.EOF)
  270. }
  271. }
  272. func testHandshake(clientConfig, serverConfig *Config) (serverState, clientState ConnectionState, err error) {
  273. c, s := net.Pipe()
  274. done := make(chan bool)
  275. go func() {
  276. cli := Client(c, clientConfig)
  277. cli.Handshake()
  278. clientState = cli.ConnectionState()
  279. c.Close()
  280. done <- true
  281. }()
  282. server := Server(s, serverConfig)
  283. err = server.Handshake()
  284. if err == nil {
  285. serverState = server.ConnectionState()
  286. }
  287. s.Close()
  288. <-done
  289. return
  290. }
  291. func TestVersion(t *testing.T) {
  292. serverConfig := &Config{
  293. Certificates: testConfig.Certificates,
  294. MaxVersion: VersionTLS11,
  295. }
  296. clientConfig := &Config{
  297. InsecureSkipVerify: true,
  298. }
  299. state, _, err := testHandshake(clientConfig, serverConfig)
  300. if err != nil {
  301. t.Fatalf("handshake failed: %s", err)
  302. }
  303. if state.Version != VersionTLS11 {
  304. t.Fatalf("Incorrect version %x, should be %x", state.Version, VersionTLS11)
  305. }
  306. }
  307. func TestCipherSuitePreference(t *testing.T) {
  308. serverConfig := &Config{
  309. CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA, TLS_RSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_RSA_WITH_RC4_128_SHA},
  310. Certificates: testConfig.Certificates,
  311. MaxVersion: VersionTLS11,
  312. }
  313. clientConfig := &Config{
  314. CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA, TLS_RSA_WITH_RC4_128_SHA},
  315. InsecureSkipVerify: true,
  316. }
  317. state, _, err := testHandshake(clientConfig, serverConfig)
  318. if err != nil {
  319. t.Fatalf("handshake failed: %s", err)
  320. }
  321. if state.CipherSuite != TLS_RSA_WITH_AES_128_CBC_SHA {
  322. // By default the server should use the client's preference.
  323. t.Fatalf("Client's preference was not used, got %x", state.CipherSuite)
  324. }
  325. serverConfig.PreferServerCipherSuites = true
  326. state, _, err = testHandshake(clientConfig, serverConfig)
  327. if err != nil {
  328. t.Fatalf("handshake failed: %s", err)
  329. }
  330. if state.CipherSuite != TLS_RSA_WITH_RC4_128_SHA {
  331. t.Fatalf("Server's preference was not used, got %x", state.CipherSuite)
  332. }
  333. }
  334. func TestSCTHandshake(t *testing.T) {
  335. expected := [][]byte{[]byte("certificate"), []byte("transparency")}
  336. serverConfig := &Config{
  337. Certificates: []Certificate{{
  338. Certificate: [][]byte{testRSACertificate},
  339. PrivateKey: testRSAPrivateKey,
  340. SignedCertificateTimestamps: expected,
  341. }},
  342. }
  343. clientConfig := &Config{
  344. InsecureSkipVerify: true,
  345. }
  346. _, state, err := testHandshake(clientConfig, serverConfig)
  347. if err != nil {
  348. t.Fatalf("handshake failed: %s", err)
  349. }
  350. actual := state.SignedCertificateTimestamps
  351. if len(actual) != len(expected) {
  352. t.Fatalf("got %d scts, want %d", len(actual), len(expected))
  353. }
  354. for i, sct := range expected {
  355. if !bytes.Equal(sct, actual[i]) {
  356. t.Fatalf("SCT #%d was %x, but expected %x", i, actual[i], sct)
  357. }
  358. }
  359. }
  360. func TestCrossVersionResume(t *testing.T) {
  361. serverConfig := &Config{
  362. CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
  363. Certificates: testConfig.Certificates,
  364. }
  365. clientConfig := &Config{
  366. CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
  367. InsecureSkipVerify: true,
  368. ClientSessionCache: NewLRUClientSessionCache(1),
  369. ServerName: "servername",
  370. }
  371. // Establish a session at TLS 1.1.
  372. clientConfig.MaxVersion = VersionTLS11
  373. _, _, err := testHandshake(clientConfig, serverConfig)
  374. if err != nil {
  375. t.Fatalf("handshake failed: %s", err)
  376. }
  377. // The client session cache now contains a TLS 1.1 session.
  378. state, _, err := testHandshake(clientConfig, serverConfig)
  379. if err != nil {
  380. t.Fatalf("handshake failed: %s", err)
  381. }
  382. if !state.DidResume {
  383. t.Fatalf("handshake did not resume at the same version")
  384. }
  385. // Test that the server will decline to resume at a lower version.
  386. clientConfig.MaxVersion = VersionTLS10
  387. state, _, err = testHandshake(clientConfig, serverConfig)
  388. if err != nil {
  389. t.Fatalf("handshake failed: %s", err)
  390. }
  391. if state.DidResume {
  392. t.Fatalf("handshake resumed at a lower version")
  393. }
  394. // The client session cache now contains a TLS 1.0 session.
  395. state, _, err = testHandshake(clientConfig, serverConfig)
  396. if err != nil {
  397. t.Fatalf("handshake failed: %s", err)
  398. }
  399. if !state.DidResume {
  400. t.Fatalf("handshake did not resume at the same version")
  401. }
  402. // Test that the server will decline to resume at a higher version.
  403. clientConfig.MaxVersion = VersionTLS11
  404. state, _, err = testHandshake(clientConfig, serverConfig)
  405. if err != nil {
  406. t.Fatalf("handshake failed: %s", err)
  407. }
  408. if state.DidResume {
  409. t.Fatalf("handshake resumed at a higher version")
  410. }
  411. }
  412. // Note: see comment in handshake_test.go for details of how the reference
  413. // tests work.
  414. // serverTest represents a test of the TLS server handshake against a reference
  415. // implementation.
  416. type serverTest struct {
  417. // name is a freeform string identifying the test and the file in which
  418. // the expected results will be stored.
  419. name string
  420. // command, if not empty, contains a series of arguments for the
  421. // command to run for the reference server.
  422. command []string
  423. // expectedPeerCerts contains a list of PEM blocks of expected
  424. // certificates from the client.
  425. expectedPeerCerts []string
  426. // config, if not nil, contains a custom Config to use for this test.
  427. config *Config
  428. // expectHandshakeErrorIncluding, when not empty, contains a string
  429. // that must be a substring of the error resulting from the handshake.
  430. expectHandshakeErrorIncluding string
  431. // validate, if not nil, is a function that will be called with the
  432. // ConnectionState of the resulting connection. It returns false if the
  433. // ConnectionState is unacceptable.
  434. validate func(ConnectionState) error
  435. }
  436. var defaultClientCommand = []string{"openssl", "s_client", "-no_ticket"}
  437. // connFromCommand starts opens a listening socket and starts the reference
  438. // client to connect to it. It returns a recordingConn that wraps the resulting
  439. // connection.
  440. func (test *serverTest) connFromCommand() (conn *recordingConn, child *exec.Cmd, err error) {
  441. l, err := net.ListenTCP("tcp", &net.TCPAddr{
  442. IP: net.IPv4(127, 0, 0, 1),
  443. Port: 0,
  444. })
  445. if err != nil {
  446. return nil, nil, err
  447. }
  448. defer l.Close()
  449. port := l.Addr().(*net.TCPAddr).Port
  450. var command []string
  451. command = append(command, test.command...)
  452. if len(command) == 0 {
  453. command = defaultClientCommand
  454. }
  455. command = append(command, "-connect")
  456. command = append(command, fmt.Sprintf("127.0.0.1:%d", port))
  457. cmd := exec.Command(command[0], command[1:]...)
  458. cmd.Stdin = nil
  459. var output bytes.Buffer
  460. cmd.Stdout = &output
  461. cmd.Stderr = &output
  462. if err := cmd.Start(); err != nil {
  463. return nil, nil, err
  464. }
  465. connChan := make(chan interface{})
  466. go func() {
  467. tcpConn, err := l.Accept()
  468. if err != nil {
  469. connChan <- err
  470. }
  471. connChan <- tcpConn
  472. }()
  473. var tcpConn net.Conn
  474. select {
  475. case connOrError := <-connChan:
  476. if err, ok := connOrError.(error); ok {
  477. return nil, nil, err
  478. }
  479. tcpConn = connOrError.(net.Conn)
  480. case <-time.After(2 * time.Second):
  481. output.WriteTo(os.Stdout)
  482. return nil, nil, errors.New("timed out waiting for connection from child process")
  483. }
  484. record := &recordingConn{
  485. Conn: tcpConn,
  486. }
  487. return record, cmd, nil
  488. }
  489. func (test *serverTest) dataPath() string {
  490. return filepath.Join("testdata", "Server-"+test.name)
  491. }
  492. func (test *serverTest) loadData() (flows [][]byte, err error) {
  493. in, err := os.Open(test.dataPath())
  494. if err != nil {
  495. return nil, err
  496. }
  497. defer in.Close()
  498. return parseTestData(in)
  499. }
  500. func (test *serverTest) run(t *testing.T, write bool) {
  501. var clientConn, serverConn net.Conn
  502. var recordingConn *recordingConn
  503. var childProcess *exec.Cmd
  504. if write {
  505. var err error
  506. recordingConn, childProcess, err = test.connFromCommand()
  507. if err != nil {
  508. t.Fatalf("Failed to start subcommand: %s", err)
  509. }
  510. serverConn = recordingConn
  511. } else {
  512. clientConn, serverConn = net.Pipe()
  513. }
  514. config := test.config
  515. if config == nil {
  516. config = testConfig
  517. }
  518. server := Server(serverConn, config)
  519. connStateChan := make(chan ConnectionState, 1)
  520. go func() {
  521. _, err := server.Write([]byte("hello, world\n"))
  522. if len(test.expectHandshakeErrorIncluding) > 0 {
  523. if err == nil {
  524. t.Errorf("Error expected, but no error returned")
  525. } else if s := err.Error(); !strings.Contains(s, test.expectHandshakeErrorIncluding) {
  526. t.Errorf("Error expected containing '%s' but got '%s'", test.expectHandshakeErrorIncluding, s)
  527. }
  528. } else {
  529. if err != nil {
  530. t.Logf("Error from Server.Write: '%s'", err)
  531. }
  532. }
  533. server.Close()
  534. serverConn.Close()
  535. connStateChan <- server.ConnectionState()
  536. }()
  537. if !write {
  538. flows, err := test.loadData()
  539. if err != nil {
  540. t.Fatalf("%s: failed to load data from %s", test.name, test.dataPath())
  541. }
  542. for i, b := range flows {
  543. if i%2 == 0 {
  544. clientConn.Write(b)
  545. continue
  546. }
  547. bb := make([]byte, len(b))
  548. n, err := io.ReadFull(clientConn, bb)
  549. if err != nil {
  550. 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)
  551. }
  552. if !bytes.Equal(b, bb) {
  553. t.Fatalf("%s #%d: mismatch on read: got:%x want:%x", test.name, i+1, bb, b)
  554. }
  555. }
  556. clientConn.Close()
  557. }
  558. connState := <-connStateChan
  559. peerCerts := connState.PeerCertificates
  560. if len(peerCerts) == len(test.expectedPeerCerts) {
  561. for i, peerCert := range peerCerts {
  562. block, _ := pem.Decode([]byte(test.expectedPeerCerts[i]))
  563. if !bytes.Equal(block.Bytes, peerCert.Raw) {
  564. t.Fatalf("%s: mismatch on peer cert %d", test.name, i+1)
  565. }
  566. }
  567. } else {
  568. t.Fatalf("%s: mismatch on peer list length: %d (wanted) != %d (got)", test.name, len(test.expectedPeerCerts), len(peerCerts))
  569. }
  570. if test.validate != nil {
  571. if err := test.validate(connState); err != nil {
  572. t.Fatalf("validate callback returned error: %s", err)
  573. }
  574. }
  575. if write {
  576. path := test.dataPath()
  577. out, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
  578. if err != nil {
  579. t.Fatalf("Failed to create output file: %s", err)
  580. }
  581. defer out.Close()
  582. recordingConn.Close()
  583. if len(recordingConn.flows) < 3 {
  584. childProcess.Stdout.(*bytes.Buffer).WriteTo(os.Stdout)
  585. if len(test.expectHandshakeErrorIncluding) == 0 {
  586. t.Fatalf("Handshake failed")
  587. }
  588. }
  589. recordingConn.WriteTo(out)
  590. fmt.Printf("Wrote %s\n", path)
  591. childProcess.Wait()
  592. }
  593. }
  594. func runServerTestForVersion(t *testing.T, template *serverTest, prefix, option string) {
  595. test := *template
  596. test.name = prefix + test.name
  597. if len(test.command) == 0 {
  598. test.command = defaultClientCommand
  599. }
  600. test.command = append([]string(nil), test.command...)
  601. test.command = append(test.command, option)
  602. test.run(t, *update)
  603. }
  604. func runServerTestSSLv3(t *testing.T, template *serverTest) {
  605. runServerTestForVersion(t, template, "SSLv3-", "-ssl3")
  606. }
  607. func runServerTestTLS10(t *testing.T, template *serverTest) {
  608. runServerTestForVersion(t, template, "TLSv10-", "-tls1")
  609. }
  610. func runServerTestTLS11(t *testing.T, template *serverTest) {
  611. runServerTestForVersion(t, template, "TLSv11-", "-tls1_1")
  612. }
  613. func runServerTestTLS12(t *testing.T, template *serverTest) {
  614. runServerTestForVersion(t, template, "TLSv12-", "-tls1_2")
  615. }
  616. func TestHandshakeServerRSARC4(t *testing.T) {
  617. test := &serverTest{
  618. name: "RSA-RC4",
  619. command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "RC4-SHA"},
  620. }
  621. runServerTestSSLv3(t, test)
  622. runServerTestTLS10(t, test)
  623. runServerTestTLS11(t, test)
  624. runServerTestTLS12(t, test)
  625. }
  626. func TestHandshakeServerRSA3DES(t *testing.T) {
  627. test := &serverTest{
  628. name: "RSA-3DES",
  629. command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "DES-CBC3-SHA"},
  630. }
  631. runServerTestSSLv3(t, test)
  632. runServerTestTLS10(t, test)
  633. runServerTestTLS12(t, test)
  634. }
  635. func TestHandshakeServerRSAAES(t *testing.T) {
  636. test := &serverTest{
  637. name: "RSA-AES",
  638. command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "AES128-SHA"},
  639. }
  640. runServerTestSSLv3(t, test)
  641. runServerTestTLS10(t, test)
  642. runServerTestTLS12(t, test)
  643. }
  644. func TestHandshakeServerAESGCM(t *testing.T) {
  645. test := &serverTest{
  646. name: "RSA-AES-GCM",
  647. command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "ECDHE-RSA-AES128-GCM-SHA256"},
  648. }
  649. runServerTestTLS12(t, test)
  650. }
  651. func TestHandshakeServerAES256GCMSHA384(t *testing.T) {
  652. test := &serverTest{
  653. name: "RSA-AES256-GCM-SHA384",
  654. command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "ECDHE-RSA-AES256-GCM-SHA384"},
  655. }
  656. runServerTestTLS12(t, test)
  657. }
  658. func TestHandshakeServerECDHEECDSAAES(t *testing.T) {
  659. config := testConfig.clone()
  660. config.Certificates = make([]Certificate, 1)
  661. config.Certificates[0].Certificate = [][]byte{testECDSACertificate}
  662. config.Certificates[0].PrivateKey = testECDSAPrivateKey
  663. config.BuildNameToCertificate()
  664. test := &serverTest{
  665. name: "ECDHE-ECDSA-AES",
  666. command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "ECDHE-ECDSA-AES256-SHA"},
  667. config: config,
  668. }
  669. runServerTestTLS10(t, test)
  670. runServerTestTLS12(t, test)
  671. }
  672. func TestHandshakeServerALPN(t *testing.T) {
  673. config := testConfig.clone()
  674. config.NextProtos = []string{"proto1", "proto2"}
  675. test := &serverTest{
  676. name: "ALPN",
  677. // Note that this needs OpenSSL 1.0.2 because that is the first
  678. // version that supports the -alpn flag.
  679. command: []string{"openssl", "s_client", "-alpn", "proto2,proto1"},
  680. config: config,
  681. validate: func(state ConnectionState) error {
  682. // The server's preferences should override the client.
  683. if state.NegotiatedProtocol != "proto1" {
  684. return fmt.Errorf("Got protocol %q, wanted proto1", state.NegotiatedProtocol)
  685. }
  686. return nil
  687. },
  688. }
  689. runServerTestTLS12(t, test)
  690. }
  691. func TestHandshakeServerALPNNoMatch(t *testing.T) {
  692. config := testConfig.clone()
  693. config.NextProtos = []string{"proto3"}
  694. test := &serverTest{
  695. name: "ALPN-NoMatch",
  696. // Note that this needs OpenSSL 1.0.2 because that is the first
  697. // version that supports the -alpn flag.
  698. command: []string{"openssl", "s_client", "-alpn", "proto2,proto1"},
  699. config: config,
  700. validate: func(state ConnectionState) error {
  701. // Rather than reject the connection, Go doesn't select
  702. // a protocol when there is no overlap.
  703. if state.NegotiatedProtocol != "" {
  704. return fmt.Errorf("Got protocol %q, wanted ''", state.NegotiatedProtocol)
  705. }
  706. return nil
  707. },
  708. }
  709. runServerTestTLS12(t, test)
  710. }
  711. // TestHandshakeServerSNI involves a client sending an SNI extension of
  712. // "snitest.com", which happens to match the CN of testSNICertificate. The test
  713. // verifies that the server correctly selects that certificate.
  714. func TestHandshakeServerSNI(t *testing.T) {
  715. test := &serverTest{
  716. name: "SNI",
  717. command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "AES128-SHA", "-servername", "snitest.com"},
  718. }
  719. runServerTestTLS12(t, test)
  720. }
  721. // TestHandshakeServerSNICertForName is similar to TestHandshakeServerSNI, but
  722. // tests the dynamic GetCertificate method
  723. func TestHandshakeServerSNIGetCertificate(t *testing.T) {
  724. config := testConfig.clone()
  725. // Replace the NameToCertificate map with a GetCertificate function
  726. nameToCert := config.NameToCertificate
  727. config.NameToCertificate = nil
  728. config.GetCertificate = func(clientHello *ClientHelloInfo) (*Certificate, error) {
  729. cert, _ := nameToCert[clientHello.ServerName]
  730. return cert, nil
  731. }
  732. test := &serverTest{
  733. name: "SNI-GetCertificate",
  734. command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "AES128-SHA", "-servername", "snitest.com"},
  735. config: config,
  736. }
  737. runServerTestTLS12(t, test)
  738. }
  739. // TestHandshakeServerSNICertForNameNotFound is similar to
  740. // TestHandshakeServerSNICertForName, but tests to make sure that when the
  741. // GetCertificate method doesn't return a cert, we fall back to what's in
  742. // the NameToCertificate map.
  743. func TestHandshakeServerSNIGetCertificateNotFound(t *testing.T) {
  744. config := testConfig.clone()
  745. config.GetCertificate = func(clientHello *ClientHelloInfo) (*Certificate, error) {
  746. return nil, nil
  747. }
  748. test := &serverTest{
  749. name: "SNI-GetCertificateNotFound",
  750. command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "AES128-SHA", "-servername", "snitest.com"},
  751. config: config,
  752. }
  753. runServerTestTLS12(t, test)
  754. }
  755. // TestHandshakeServerSNICertForNameError tests to make sure that errors in
  756. // GetCertificate result in a tls alert.
  757. func TestHandshakeServerSNIGetCertificateError(t *testing.T) {
  758. const errMsg = "TestHandshakeServerSNIGetCertificateError error"
  759. serverConfig := testConfig.clone()
  760. serverConfig.GetCertificate = func(clientHello *ClientHelloInfo) (*Certificate, error) {
  761. return nil, errors.New(errMsg)
  762. }
  763. clientHello := &clientHelloMsg{
  764. vers: VersionTLS10,
  765. cipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
  766. compressionMethods: []uint8{compressionNone},
  767. serverName: "test",
  768. }
  769. testClientHelloFailure(t, serverConfig, clientHello, errMsg)
  770. }
  771. // TestHandshakeServerEmptyCertificates tests that GetCertificates is called in
  772. // the case that Certificates is empty, even without SNI.
  773. func TestHandshakeServerEmptyCertificates(t *testing.T) {
  774. const errMsg = "TestHandshakeServerEmptyCertificates error"
  775. serverConfig := testConfig.clone()
  776. serverConfig.GetCertificate = func(clientHello *ClientHelloInfo) (*Certificate, error) {
  777. return nil, errors.New(errMsg)
  778. }
  779. serverConfig.Certificates = nil
  780. clientHello := &clientHelloMsg{
  781. vers: VersionTLS10,
  782. cipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
  783. compressionMethods: []uint8{compressionNone},
  784. }
  785. testClientHelloFailure(t, serverConfig, clientHello, errMsg)
  786. // With an empty Certificates and a nil GetCertificate, the server
  787. // should always return a “no certificates” error.
  788. serverConfig.GetCertificate = nil
  789. clientHello = &clientHelloMsg{
  790. vers: VersionTLS10,
  791. cipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
  792. compressionMethods: []uint8{compressionNone},
  793. }
  794. testClientHelloFailure(t, serverConfig, clientHello, "no certificates")
  795. }
  796. // TestCipherSuiteCertPreferance ensures that we select an RSA ciphersuite with
  797. // an RSA certificate and an ECDSA ciphersuite with an ECDSA certificate.
  798. func TestCipherSuiteCertPreferenceECDSA(t *testing.T) {
  799. config := testConfig.clone()
  800. config.CipherSuites = []uint16{TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA}
  801. config.PreferServerCipherSuites = true
  802. test := &serverTest{
  803. name: "CipherSuiteCertPreferenceRSA",
  804. config: config,
  805. }
  806. runServerTestTLS12(t, test)
  807. config = testConfig.clone()
  808. config.CipherSuites = []uint16{TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA}
  809. config.Certificates = []Certificate{
  810. {
  811. Certificate: [][]byte{testECDSACertificate},
  812. PrivateKey: testECDSAPrivateKey,
  813. },
  814. }
  815. config.BuildNameToCertificate()
  816. config.PreferServerCipherSuites = true
  817. test = &serverTest{
  818. name: "CipherSuiteCertPreferenceECDSA",
  819. config: config,
  820. }
  821. runServerTestTLS12(t, test)
  822. }
  823. func TestResumption(t *testing.T) {
  824. sessionFilePath := tempFile("")
  825. defer os.Remove(sessionFilePath)
  826. test := &serverTest{
  827. name: "IssueTicket",
  828. command: []string{"openssl", "s_client", "-cipher", "RC4-SHA", "-sess_out", sessionFilePath},
  829. }
  830. runServerTestTLS12(t, test)
  831. test = &serverTest{
  832. name: "Resume",
  833. command: []string{"openssl", "s_client", "-cipher", "RC4-SHA", "-sess_in", sessionFilePath},
  834. }
  835. runServerTestTLS12(t, test)
  836. }
  837. func TestResumptionDisabled(t *testing.T) {
  838. sessionFilePath := tempFile("")
  839. defer os.Remove(sessionFilePath)
  840. config := testConfig.clone()
  841. test := &serverTest{
  842. name: "IssueTicketPreDisable",
  843. command: []string{"openssl", "s_client", "-cipher", "RC4-SHA", "-sess_out", sessionFilePath},
  844. config: config,
  845. }
  846. runServerTestTLS12(t, test)
  847. config.SessionTicketsDisabled = true
  848. test = &serverTest{
  849. name: "ResumeDisabled",
  850. command: []string{"openssl", "s_client", "-cipher", "RC4-SHA", "-sess_in", sessionFilePath},
  851. config: config,
  852. }
  853. runServerTestTLS12(t, test)
  854. // One needs to manually confirm that the handshake in the golden data
  855. // file for ResumeDisabled does not include a resumption handshake.
  856. }
  857. func TestFallbackSCSV(t *testing.T) {
  858. serverConfig := Config{
  859. Certificates: testConfig.Certificates,
  860. }
  861. test := &serverTest{
  862. name: "FallbackSCSV",
  863. config: &serverConfig,
  864. // OpenSSL 1.0.1j is needed for the -fallback_scsv option.
  865. command: []string{"openssl", "s_client", "-fallback_scsv"},
  866. expectHandshakeErrorIncluding: "inappropriate protocol fallback",
  867. }
  868. runServerTestTLS11(t, test)
  869. }
  870. // cert.pem and key.pem were generated with generate_cert.go
  871. // Thus, they have no ExtKeyUsage fields and trigger an error
  872. // when verification is turned on.
  873. const clientCertificatePEM = `
  874. -----BEGIN CERTIFICATE-----
  875. MIIB7TCCAVigAwIBAgIBADALBgkqhkiG9w0BAQUwJjEQMA4GA1UEChMHQWNtZSBD
  876. bzESMBAGA1UEAxMJMTI3LjAuMC4xMB4XDTExMTIwODA3NTUxMloXDTEyMTIwNzA4
  877. MDAxMlowJjEQMA4GA1UEChMHQWNtZSBDbzESMBAGA1UEAxMJMTI3LjAuMC4xMIGc
  878. MAsGCSqGSIb3DQEBAQOBjAAwgYgCgYBO0Hsx44Jk2VnAwoekXh6LczPHY1PfZpIG
  879. hPZk1Y/kNqcdK+izIDZFI7Xjla7t4PUgnI2V339aEu+H5Fto5OkOdOwEin/ekyfE
  880. ARl6vfLcPRSr0FTKIQzQTW6HLlzF0rtNS0/Otiz3fojsfNcCkXSmHgwa2uNKWi7e
  881. E5xMQIhZkwIDAQABozIwMDAOBgNVHQ8BAf8EBAMCAKAwDQYDVR0OBAYEBAECAwQw
  882. DwYDVR0jBAgwBoAEAQIDBDALBgkqhkiG9w0BAQUDgYEANh+zegx1yW43RmEr1b3A
  883. p0vMRpqBWHyFeSnIyMZn3TJWRSt1tukkqVCavh9a+hoV2cxVlXIWg7nCto/9iIw4
  884. hB2rXZIxE0/9gzvGnfERYraL7KtnvshksBFQRlgXa5kc0x38BvEO5ZaoDPl4ILdE
  885. GFGNEH5PlGffo05wc46QkYU=
  886. -----END CERTIFICATE-----`
  887. const clientKeyPEM = `
  888. -----BEGIN RSA PRIVATE KEY-----
  889. MIICWgIBAAKBgE7QezHjgmTZWcDCh6ReHotzM8djU99mkgaE9mTVj+Q2px0r6LMg
  890. NkUjteOVru3g9SCcjZXff1oS74fkW2jk6Q507ASKf96TJ8QBGXq98tw9FKvQVMoh
  891. DNBNbocuXMXSu01LT862LPd+iOx81wKRdKYeDBra40paLt4TnExAiFmTAgMBAAEC
  892. gYBxvXd8yNteFTns8A/2yomEMC4yeosJJSpp1CsN3BJ7g8/qTnrVPxBy+RU+qr63
  893. t2WquaOu/cr5P8iEsa6lk20tf8pjKLNXeX0b1RTzK8rJLbS7nGzP3tvOhL096VtQ
  894. dAo4ROEaro0TzYpHmpciSvxVIeEIAAdFDObDJPKqcJAxyQJBAJizfYgK8Gzx9fsx
  895. hxp+VteCbVPg2euASH5Yv3K5LukRdKoSzHE2grUVQgN/LafC0eZibRanxHegYSr7
  896. 7qaswKUCQQCEIWor/X4XTMdVj3Oj+vpiw75y/S9gh682+myZL+d/02IEkwnB098P
  897. RkKVpenBHyrGg0oeN5La7URILWKj7CPXAkBKo6F+d+phNjwIFoN1Xb/RA32w/D1I
  898. saG9sF+UEhRt9AxUfW/U/tIQ9V0ZHHcSg1XaCM5Nvp934brdKdvTOKnJAkBD5h/3
  899. Rybatlvg/fzBEaJFyq09zhngkxlZOUtBVTqzl17RVvY2orgH02U4HbCHy4phxOn7
  900. qTdQRYlHRftgnWK1AkANibn9PRYJ7mJyJ9Dyj2QeNcSkSTzrt0tPvUMf4+meJymN
  901. 1Ntu5+S1DLLzfxlaljWG6ylW6DNxujCyuXIV2rvA
  902. -----END RSA PRIVATE KEY-----`
  903. const clientECDSACertificatePEM = `
  904. -----BEGIN CERTIFICATE-----
  905. MIIB/DCCAV4CCQCaMIRsJjXZFzAJBgcqhkjOPQQBMEUxCzAJBgNVBAYTAkFVMRMw
  906. EQYDVQQIEwpTb21lLVN0YXRlMSEwHwYDVQQKExhJbnRlcm5ldCBXaWRnaXRzIFB0
  907. eSBMdGQwHhcNMTIxMTE0MTMyNTUzWhcNMjIxMTEyMTMyNTUzWjBBMQswCQYDVQQG
  908. EwJBVTEMMAoGA1UECBMDTlNXMRAwDgYDVQQHEwdQeXJtb250MRIwEAYDVQQDEwlK
  909. b2VsIFNpbmcwgZswEAYHKoZIzj0CAQYFK4EEACMDgYYABACVjJF1FMBexFe01MNv
  910. ja5oHt1vzobhfm6ySD6B5U7ixohLZNz1MLvT/2XMW/TdtWo+PtAd3kfDdq0Z9kUs
  911. jLzYHQFMH3CQRnZIi4+DzEpcj0B22uCJ7B0rxE4wdihBsmKo+1vx+U56jb0JuK7q
  912. ixgnTy5w/hOWusPTQBbNZU6sER7m8TAJBgcqhkjOPQQBA4GMADCBiAJCAOAUxGBg
  913. C3JosDJdYUoCdFzCgbkWqD8pyDbHgf9stlvZcPE4O1BIKJTLCRpS8V3ujfK58PDa
  914. 2RU6+b0DeoeiIzXsAkIBo9SKeDUcSpoj0gq+KxAxnZxfvuiRs9oa9V2jI/Umi0Vw
  915. jWVim34BmT0Y9hCaOGGbLlfk+syxis7iI6CH8OFnUes=
  916. -----END CERTIFICATE-----`
  917. const clientECDSAKeyPEM = `
  918. -----BEGIN EC PARAMETERS-----
  919. BgUrgQQAIw==
  920. -----END EC PARAMETERS-----
  921. -----BEGIN EC PRIVATE KEY-----
  922. MIHcAgEBBEIBkJN9X4IqZIguiEVKMqeBUP5xtRsEv4HJEtOpOGLELwO53SD78Ew8
  923. k+wLWoqizS3NpQyMtrU8JFdWfj+C57UNkOugBwYFK4EEACOhgYkDgYYABACVjJF1
  924. FMBexFe01MNvja5oHt1vzobhfm6ySD6B5U7ixohLZNz1MLvT/2XMW/TdtWo+PtAd
  925. 3kfDdq0Z9kUsjLzYHQFMH3CQRnZIi4+DzEpcj0B22uCJ7B0rxE4wdihBsmKo+1vx
  926. +U56jb0JuK7qixgnTy5w/hOWusPTQBbNZU6sER7m8Q==
  927. -----END EC PRIVATE KEY-----`
  928. func TestClientAuth(t *testing.T) {
  929. var certPath, keyPath, ecdsaCertPath, ecdsaKeyPath string
  930. if *update {
  931. certPath = tempFile(clientCertificatePEM)
  932. defer os.Remove(certPath)
  933. keyPath = tempFile(clientKeyPEM)
  934. defer os.Remove(keyPath)
  935. ecdsaCertPath = tempFile(clientECDSACertificatePEM)
  936. defer os.Remove(ecdsaCertPath)
  937. ecdsaKeyPath = tempFile(clientECDSAKeyPEM)
  938. defer os.Remove(ecdsaKeyPath)
  939. }
  940. config := testConfig.clone()
  941. config.ClientAuth = RequestClientCert
  942. test := &serverTest{
  943. name: "ClientAuthRequestedNotGiven",
  944. command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "RC4-SHA"},
  945. config: config,
  946. }
  947. runServerTestTLS12(t, test)
  948. test = &serverTest{
  949. name: "ClientAuthRequestedAndGiven",
  950. command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "RC4-SHA", "-cert", certPath, "-key", keyPath},
  951. config: config,
  952. expectedPeerCerts: []string{clientCertificatePEM},
  953. }
  954. runServerTestTLS12(t, test)
  955. test = &serverTest{
  956. name: "ClientAuthRequestedAndECDSAGiven",
  957. command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "RC4-SHA", "-cert", ecdsaCertPath, "-key", ecdsaKeyPath},
  958. config: config,
  959. expectedPeerCerts: []string{clientECDSACertificatePEM},
  960. }
  961. runServerTestTLS12(t, test)
  962. }
  963. func bigFromString(s string) *big.Int {
  964. ret := new(big.Int)
  965. ret.SetString(s, 10)
  966. return ret
  967. }
  968. func fromHex(s string) []byte {
  969. b, _ := hex.DecodeString(s)
  970. return b
  971. }
  972. var testRSACertificate = fromHex("30820263308201cca003020102020900a273000c8100cbf3300d06092a864886f70d01010b0500302b31173015060355040a130e476f6f676c652054455354494e473110300e06035504031307476f20526f6f74301e170d3135303130313030303030305a170d3235303130313030303030305a302631173015060355040a130e476f6f676c652054455354494e47310b300906035504031302476f30819f300d06092a864886f70d010101050003818d0030818902818100af8788f6201b95656c14ab4405af3b4514e3b76dfd00634d957ffe6a623586c04af9187cf6aa255e7a64316600baf48e92afc76bd876d4f35f41cb6e5615971b97c13c123921663d2b16d1bcdb1cc0a7dab7caadbadacbd52150ecde8dabd16b814b8902f3c4bec16c89b14484bd21d1047d9d164df98215f6effad60947f2fb0203010001a38193308190300e0603551d0f0101ff0404030205a0301d0603551d250416301406082b0601050507030106082b06010505070302300c0603551d130101ff0402300030190603551d0e0412041012508d896f1bd1dc544d6ecb695e06f4301b0603551d23041430128010bf3db6a966f2b840cfeab40378481a4130190603551d1104123010820e6578616d706c652e676f6c616e67300d06092a864886f70d01010b050003818100927caf91551218965931a64840d52dd5eebb02a0f5c21e7c9bb3307d3cdc76da4f3dc0faae2d33246b037b1b67591121b511bc77b9d9e06ea82d2e35fa645f223e63106bbeff14866d0df01531a814381e3b84872ccb98ed5176b9b14fdddb9b84048640fa51ddbab48debe346de46b94f86c7f9a4c24134acccf6eab0ab3918")
  973. var testRSACertificateIssuer = fromHex("3082024d308201b6a003020102020827326bd913b7c43d300d06092a864886f70d01010b0500302b31173015060355040a130e476f6f676c652054455354494e473110300e06035504031307476f20526f6f74301e170d3135303130313030303030305a170d3235303130313030303030305a302b31173015060355040a130e476f6f676c652054455354494e473110300e06035504031307476f20526f6f7430819f300d06092a864886f70d010101050003818d0030818902818100f0429a7b9f66a222c8453800452db355b34c4409fee09af2510a6589bfa35bdb4d453200d1de24338d6d5e5a91cc8301628445d6eb4e675927b9c1ea5c0f676acfb0f708ce4f19827e321c1898bf86df9823d5f0b05df2b6779888eff8abbc7f41c6e7d2667386a579b8cbaad3f6fd597cd7c4b187911a425aed1b555c1965190203010001a37a3078300e0603551d0f0101ff040403020204301d0603551d250416301406082b0601050507030106082b06010505070302300f0603551d130101ff040530030101ff30190603551d0e04120410bf3db6a966f2b840cfeab40378481a41301b0603551d23041430128010bf3db6a966f2b840cfeab40378481a41300d06092a864886f70d01010b050003818100586e68c1219ed4f5782b7cfd53cf1a55750a98781b2023f8694bb831fff6d7d4aad1f0ac782b1ec787f00a8956bdd06b4a1063444fcafe955c07d679163a730802c568886a2cf8a3c2ab41176957131c4b9e077ebd7ffbb91fdad8b08b932e9aeefac04923ffdc0aa145563f7f061995317400203578f350e3e566deb29dec5e")
  974. var testECDSACertificate = fromHex("3082020030820162020900b8bf2d47a0d2ebf4300906072a8648ce3d04013045310b3009060355040613024155311330110603550408130a536f6d652d53746174653121301f060355040a1318496e7465726e6574205769646769747320507479204c7464301e170d3132313132323135303633325a170d3232313132303135303633325a3045310b3009060355040613024155311330110603550408130a536f6d652d53746174653121301f060355040a1318496e7465726e6574205769646769747320507479204c746430819b301006072a8648ce3d020106052b81040023038186000400c4a1edbe98f90b4873367ec316561122f23d53c33b4d213dcd6b75e6f6b0dc9adf26c1bcb287f072327cb3642f1c90bcea6823107efee325c0483a69e0286dd33700ef0462dd0da09c706283d881d36431aa9e9731bd96b068c09b23de76643f1a5c7fe9120e5858b65f70dd9bd8ead5d7f5d5ccb9b69f30665b669a20e227e5bffe3b300906072a8648ce3d040103818c0030818802420188a24febe245c5487d1bacf5ed989dae4770c05e1bb62fbdf1b64db76140d311a2ceee0b7e927eff769dc33b7ea53fcefa10e259ec472d7cacda4e970e15a06fd00242014dfcbe67139c2d050ebd3fa38c25c13313830d9406bbd4377af6ec7ac9862eddd711697f857c56defb31782be4c7780daecbbe9e4e3624317b6a0f399512078f2a")
  975. var testSNICertificate = fromHex("308201f23082015da003020102020100300b06092a864886f70d01010530283110300e060355040a130741636d6520436f311430120603550403130b736e69746573742e636f6d301e170d3132303431313137343033355a170d3133303431313137343533355a30283110300e060355040a130741636d6520436f311430120603550403130b736e69746573742e636f6d30819d300b06092a864886f70d01010103818d0030818902818100bb79d6f517b5e5bf4610d0dc69bee62b07435ad0032d8a7a4385b71452e7a5654c2c78b8238cb5b482e5de1f953b7e62a52ca533d6fe125c7a56fcf506bffa587b263fb5cd04d3d0c921964ac7f4549f5abfef427100fe1899077f7e887d7df10439c4a22edb51c97ce3c04c3b326601cfafb11db8719a1ddbdb896baeda2d790203010001a3323030300e0603551d0f0101ff0404030200a0300d0603551d0e0406040401020304300f0603551d2304083006800401020304300b06092a864886f70d0101050381810089c6455f1c1f5ef8eb1ab174ee2439059f5c4259bb1a8d86cdb1d056f56a717da40e95ab90f59e8deaf627c157995094db0802266eb34fc6842dea8a4b68d9c1389103ab84fb9e1f85d9b5d23ff2312c8670fbb540148245a4ebafe264d90c8a4cf4f85b0fac12ac2fc4a3154bad52462868af96c62c6525d652b6e31845bdcc")
  976. var testRSAPrivateKey = &rsa.PrivateKey{
  977. PublicKey: rsa.PublicKey{
  978. N: bigFromString("123260960069105588390096594560395120585636206567569540256061833976822892593755073841963170165000086278069699238754008398039246547214989242849418349143232951701395321381739566687846006911427966669790845430647688107009232778985142860108863460556510585049041936029324503323373417214453307648498561956908810892027L"),
  979. E: 65537,
  980. },
  981. D: bigFromString("73196363031103823625826315929954946106043759818067219550565550066527203472294428548476778865091068522665312037075674791871635825938217363523103946045078950060973913307430314113074463630778799389010335923241901501086246276485964417618981733827707048660375428006201525399194575538037883519254056917253456403553L"),
  982. Primes: []*big.Int{
  983. bigFromString("11157426355495284553529769521954035649776033703833034489026848970480272318436419662860715175517581249375929775774910501512841707465207184924996975125010787L"),
  984. bigFromString("11047436580963564307160117670964629323534448585520694947919342920137706075617545637058809770319843170934495909554506529982972972247390145716507031692656521L"),
  985. },
  986. }
  987. var testECDSAPrivateKey = &ecdsa.PrivateKey{
  988. PublicKey: ecdsa.PublicKey{
  989. Curve: elliptic.P521(),
  990. X: bigFromString("2636411247892461147287360222306590634450676461695221912739908880441342231985950069527906976759812296359387337367668045707086543273113073382714101597903639351"),
  991. Y: bigFromString("3204695818431246682253994090650952614555094516658732116404513121125038617915183037601737180082382202488628239201196033284060130040574800684774115478859677243"),
  992. },
  993. D: bigFromString("5477294338614160138026852784385529180817726002953041720191098180813046231640184669647735805135001309477695746518160084669446643325196003346204701381388769751"),
  994. }