Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

handshake_server_test.go 38 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  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
  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
  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
  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. secureRenegotiation: 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.secureRenegotiation {
  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
  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. // Note: see comment in handshake_test.go for details of how the reference
  361. // tests work.
  362. // serverTest represents a test of the TLS server handshake against a reference
  363. // implementation.
  364. type serverTest struct {
  365. // name is a freeform string identifying the test and the file in which
  366. // the expected results will be stored.
  367. name string
  368. // command, if not empty, contains a series of arguments for the
  369. // command to run for the reference server.
  370. command []string
  371. // expectedPeerCerts contains a list of PEM blocks of expected
  372. // certificates from the client.
  373. expectedPeerCerts []string
  374. // config, if not nil, contains a custom Config to use for this test.
  375. config *Config
  376. // expectHandshakeErrorIncluding, when not empty, contains a string
  377. // that must be a substring of the error resulting from the handshake.
  378. expectHandshakeErrorIncluding string
  379. // validate, if not nil, is a function that will be called with the
  380. // ConnectionState of the resulting connection. It returns false if the
  381. // ConnectionState is unacceptable.
  382. validate func(ConnectionState) error
  383. }
  384. var defaultClientCommand = []string{"openssl", "s_client", "-no_ticket"}
  385. // connFromCommand starts opens a listening socket and starts the reference
  386. // client to connect to it. It returns a recordingConn that wraps the resulting
  387. // connection.
  388. func (test *serverTest) connFromCommand() (conn *recordingConn, child *exec.Cmd, err error) {
  389. l, err := net.ListenTCP("tcp", &net.TCPAddr{
  390. IP: net.IPv4(127, 0, 0, 1),
  391. Port: 0,
  392. })
  393. if err != nil {
  394. return nil, nil, err
  395. }
  396. defer l.Close()
  397. port := l.Addr().(*net.TCPAddr).Port
  398. var command []string
  399. command = append(command, test.command...)
  400. if len(command) == 0 {
  401. command = defaultClientCommand
  402. }
  403. command = append(command, "-connect")
  404. command = append(command, fmt.Sprintf("127.0.0.1:%d", port))
  405. cmd := exec.Command(command[0], command[1:]...)
  406. cmd.Stdin = nil
  407. var output bytes.Buffer
  408. cmd.Stdout = &output
  409. cmd.Stderr = &output
  410. if err := cmd.Start(); err != nil {
  411. return nil, nil, err
  412. }
  413. connChan := make(chan interface{})
  414. go func() {
  415. tcpConn, err := l.Accept()
  416. if err != nil {
  417. connChan <- err
  418. }
  419. connChan <- tcpConn
  420. }()
  421. var tcpConn net.Conn
  422. select {
  423. case connOrError := <-connChan:
  424. if err, ok := connOrError.(error); ok {
  425. return nil, nil, err
  426. }
  427. tcpConn = connOrError.(net.Conn)
  428. case <-time.After(2 * time.Second):
  429. output.WriteTo(os.Stdout)
  430. return nil, nil, errors.New("timed out waiting for connection from child process")
  431. }
  432. record := &recordingConn{
  433. Conn: tcpConn,
  434. }
  435. return record, cmd, nil
  436. }
  437. func (test *serverTest) dataPath() string {
  438. return filepath.Join("testdata", "Server-"+test.name)
  439. }
  440. func (test *serverTest) loadData() (flows [][]byte, err error) {
  441. in, err := os.Open(test.dataPath())
  442. if err != nil {
  443. return nil, err
  444. }
  445. defer in.Close()
  446. return parseTestData(in)
  447. }
  448. func (test *serverTest) run(t *testing.T, write bool) {
  449. var clientConn, serverConn net.Conn
  450. var recordingConn *recordingConn
  451. var childProcess *exec.Cmd
  452. if write {
  453. var err error
  454. recordingConn, childProcess, err = test.connFromCommand()
  455. if err != nil {
  456. t.Fatalf("Failed to start subcommand: %s", err)
  457. }
  458. serverConn = recordingConn
  459. } else {
  460. clientConn, serverConn = net.Pipe()
  461. }
  462. config := test.config
  463. if config == nil {
  464. config = testConfig
  465. }
  466. server := Server(serverConn, config)
  467. connStateChan := make(chan ConnectionState, 1)
  468. go func() {
  469. _, err := server.Write([]byte("hello, world\n"))
  470. if len(test.expectHandshakeErrorIncluding) > 0 {
  471. if err == nil {
  472. t.Errorf("Error expected, but no error returned")
  473. } else if s := err.Error(); !strings.Contains(s, test.expectHandshakeErrorIncluding) {
  474. t.Errorf("Error expected containing '%s' but got '%s'", test.expectHandshakeErrorIncluding, s)
  475. }
  476. } else {
  477. if err != nil {
  478. t.Logf("Error from Server.Write: '%s'", err)
  479. }
  480. }
  481. server.Close()
  482. serverConn.Close()
  483. connStateChan <- server.ConnectionState()
  484. }()
  485. if !write {
  486. flows, err := test.loadData()
  487. if err != nil {
  488. t.Fatalf("%s: failed to load data from %s", test.name, test.dataPath())
  489. }
  490. for i, b := range flows {
  491. if i%2 == 0 {
  492. clientConn.Write(b)
  493. continue
  494. }
  495. bb := make([]byte, len(b))
  496. n, err := io.ReadFull(clientConn, bb)
  497. if err != nil {
  498. 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)
  499. }
  500. if !bytes.Equal(b, bb) {
  501. t.Fatalf("%s #%d: mismatch on read: got:%x want:%x", test.name, i+1, bb, b)
  502. }
  503. }
  504. clientConn.Close()
  505. }
  506. connState := <-connStateChan
  507. peerCerts := connState.PeerCertificates
  508. if len(peerCerts) == len(test.expectedPeerCerts) {
  509. for i, peerCert := range peerCerts {
  510. block, _ := pem.Decode([]byte(test.expectedPeerCerts[i]))
  511. if !bytes.Equal(block.Bytes, peerCert.Raw) {
  512. t.Fatalf("%s: mismatch on peer cert %d", test.name, i+1)
  513. }
  514. }
  515. } else {
  516. t.Fatalf("%s: mismatch on peer list length: %d (wanted) != %d (got)", test.name, len(test.expectedPeerCerts), len(peerCerts))
  517. }
  518. if test.validate != nil {
  519. if err := test.validate(connState); err != nil {
  520. t.Fatalf("validate callback returned error: %s", err)
  521. }
  522. }
  523. if write {
  524. path := test.dataPath()
  525. out, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
  526. if err != nil {
  527. t.Fatalf("Failed to create output file: %s", err)
  528. }
  529. defer out.Close()
  530. recordingConn.Close()
  531. if len(recordingConn.flows) < 3 {
  532. childProcess.Stdout.(*bytes.Buffer).WriteTo(os.Stdout)
  533. if len(test.expectHandshakeErrorIncluding) == 0 {
  534. t.Fatalf("Handshake failed")
  535. }
  536. }
  537. recordingConn.WriteTo(out)
  538. fmt.Printf("Wrote %s\n", path)
  539. childProcess.Wait()
  540. }
  541. }
  542. func runServerTestForVersion(t *testing.T, template *serverTest, prefix, option string) {
  543. test := *template
  544. test.name = prefix + test.name
  545. if len(test.command) == 0 {
  546. test.command = defaultClientCommand
  547. }
  548. test.command = append([]string(nil), test.command...)
  549. test.command = append(test.command, option)
  550. test.run(t, *update)
  551. }
  552. func runServerTestSSLv3(t *testing.T, template *serverTest) {
  553. runServerTestForVersion(t, template, "SSLv3-", "-ssl3")
  554. }
  555. func runServerTestTLS10(t *testing.T, template *serverTest) {
  556. runServerTestForVersion(t, template, "TLSv10-", "-tls1")
  557. }
  558. func runServerTestTLS11(t *testing.T, template *serverTest) {
  559. runServerTestForVersion(t, template, "TLSv11-", "-tls1_1")
  560. }
  561. func runServerTestTLS12(t *testing.T, template *serverTest) {
  562. runServerTestForVersion(t, template, "TLSv12-", "-tls1_2")
  563. }
  564. func TestHandshakeServerRSARC4(t *testing.T) {
  565. test := &serverTest{
  566. name: "RSA-RC4",
  567. command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "RC4-SHA"},
  568. }
  569. runServerTestSSLv3(t, test)
  570. runServerTestTLS10(t, test)
  571. runServerTestTLS11(t, test)
  572. runServerTestTLS12(t, test)
  573. }
  574. func TestHandshakeServerRSA3DES(t *testing.T) {
  575. test := &serverTest{
  576. name: "RSA-3DES",
  577. command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "DES-CBC3-SHA"},
  578. }
  579. runServerTestSSLv3(t, test)
  580. runServerTestTLS10(t, test)
  581. runServerTestTLS12(t, test)
  582. }
  583. func TestHandshakeServerRSAAES(t *testing.T) {
  584. test := &serverTest{
  585. name: "RSA-AES",
  586. command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "AES128-SHA"},
  587. }
  588. runServerTestSSLv3(t, test)
  589. runServerTestTLS10(t, test)
  590. runServerTestTLS12(t, test)
  591. }
  592. func TestHandshakeServerAESGCM(t *testing.T) {
  593. test := &serverTest{
  594. name: "RSA-AES-GCM",
  595. command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "ECDHE-RSA-AES128-GCM-SHA256"},
  596. }
  597. runServerTestTLS12(t, test)
  598. }
  599. func TestHandshakeServerAES256GCMSHA384(t *testing.T) {
  600. test := &serverTest{
  601. name: "RSA-AES256-GCM-SHA384",
  602. command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "ECDHE-RSA-AES256-GCM-SHA384"},
  603. }
  604. runServerTestTLS12(t, test)
  605. }
  606. func TestHandshakeServerECDHEECDSAAES(t *testing.T) {
  607. config := *testConfig
  608. config.Certificates = make([]Certificate, 1)
  609. config.Certificates[0].Certificate = [][]byte{testECDSACertificate}
  610. config.Certificates[0].PrivateKey = testECDSAPrivateKey
  611. config.BuildNameToCertificate()
  612. test := &serverTest{
  613. name: "ECDHE-ECDSA-AES",
  614. command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "ECDHE-ECDSA-AES256-SHA"},
  615. config: &config,
  616. }
  617. runServerTestTLS10(t, test)
  618. runServerTestTLS12(t, test)
  619. }
  620. func TestHandshakeServerALPN(t *testing.T) {
  621. config := *testConfig
  622. config.NextProtos = []string{"proto1", "proto2"}
  623. test := &serverTest{
  624. name: "ALPN",
  625. // Note that this needs OpenSSL 1.0.2 because that is the first
  626. // version that supports the -alpn flag.
  627. command: []string{"openssl", "s_client", "-alpn", "proto2,proto1"},
  628. config: &config,
  629. validate: func(state ConnectionState) error {
  630. // The server's preferences should override the client.
  631. if state.NegotiatedProtocol != "proto1" {
  632. return fmt.Errorf("Got protocol %q, wanted proto1", state.NegotiatedProtocol)
  633. }
  634. return nil
  635. },
  636. }
  637. runServerTestTLS12(t, test)
  638. }
  639. func TestHandshakeServerALPNNoMatch(t *testing.T) {
  640. config := *testConfig
  641. config.NextProtos = []string{"proto3"}
  642. test := &serverTest{
  643. name: "ALPN-NoMatch",
  644. // Note that this needs OpenSSL 1.0.2 because that is the first
  645. // version that supports the -alpn flag.
  646. command: []string{"openssl", "s_client", "-alpn", "proto2,proto1"},
  647. config: &config,
  648. validate: func(state ConnectionState) error {
  649. // Rather than reject the connection, Go doesn't select
  650. // a protocol when there is no overlap.
  651. if state.NegotiatedProtocol != "" {
  652. return fmt.Errorf("Got protocol %q, wanted ''", state.NegotiatedProtocol)
  653. }
  654. return nil
  655. },
  656. }
  657. runServerTestTLS12(t, test)
  658. }
  659. // TestHandshakeServerSNI involves a client sending an SNI extension of
  660. // "snitest.com", which happens to match the CN of testSNICertificate. The test
  661. // verifies that the server correctly selects that certificate.
  662. func TestHandshakeServerSNI(t *testing.T) {
  663. test := &serverTest{
  664. name: "SNI",
  665. command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "AES128-SHA", "-servername", "snitest.com"},
  666. }
  667. runServerTestTLS12(t, test)
  668. }
  669. // TestHandshakeServerSNICertForName is similar to TestHandshakeServerSNI, but
  670. // tests the dynamic GetCertificate method
  671. func TestHandshakeServerSNIGetCertificate(t *testing.T) {
  672. config := *testConfig
  673. // Replace the NameToCertificate map with a GetCertificate function
  674. nameToCert := config.NameToCertificate
  675. config.NameToCertificate = nil
  676. config.GetCertificate = func(clientHello *ClientHelloInfo) (*Certificate, error) {
  677. cert, _ := nameToCert[clientHello.ServerName]
  678. return cert, nil
  679. }
  680. test := &serverTest{
  681. name: "SNI-GetCertificate",
  682. command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "AES128-SHA", "-servername", "snitest.com"},
  683. config: &config,
  684. }
  685. runServerTestTLS12(t, test)
  686. }
  687. // TestHandshakeServerSNICertForNameNotFound is similar to
  688. // TestHandshakeServerSNICertForName, but tests to make sure that when the
  689. // GetCertificate method doesn't return a cert, we fall back to what's in
  690. // the NameToCertificate map.
  691. func TestHandshakeServerSNIGetCertificateNotFound(t *testing.T) {
  692. config := *testConfig
  693. config.GetCertificate = func(clientHello *ClientHelloInfo) (*Certificate, error) {
  694. return nil, nil
  695. }
  696. test := &serverTest{
  697. name: "SNI-GetCertificateNotFound",
  698. command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "AES128-SHA", "-servername", "snitest.com"},
  699. config: &config,
  700. }
  701. runServerTestTLS12(t, test)
  702. }
  703. // TestHandshakeServerSNICertForNameError tests to make sure that errors in
  704. // GetCertificate result in a tls alert.
  705. func TestHandshakeServerSNIGetCertificateError(t *testing.T) {
  706. const errMsg = "TestHandshakeServerSNIGetCertificateError error"
  707. serverConfig := *testConfig
  708. serverConfig.GetCertificate = func(clientHello *ClientHelloInfo) (*Certificate, error) {
  709. return nil, errors.New(errMsg)
  710. }
  711. clientHello := &clientHelloMsg{
  712. vers: VersionTLS10,
  713. cipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
  714. compressionMethods: []uint8{compressionNone},
  715. serverName: "test",
  716. }
  717. testClientHelloFailure(t, &serverConfig, clientHello, errMsg)
  718. }
  719. // TestHandshakeServerEmptyCertificates tests that GetCertificates is called in
  720. // the case that Certificates is empty, even without SNI.
  721. func TestHandshakeServerEmptyCertificates(t *testing.T) {
  722. const errMsg = "TestHandshakeServerEmptyCertificates error"
  723. serverConfig := *testConfig
  724. serverConfig.GetCertificate = func(clientHello *ClientHelloInfo) (*Certificate, error) {
  725. return nil, errors.New(errMsg)
  726. }
  727. serverConfig.Certificates = nil
  728. clientHello := &clientHelloMsg{
  729. vers: VersionTLS10,
  730. cipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
  731. compressionMethods: []uint8{compressionNone},
  732. }
  733. testClientHelloFailure(t, &serverConfig, clientHello, errMsg)
  734. // With an empty Certificates and a nil GetCertificate, the server
  735. // should always return a “no certificates” error.
  736. serverConfig.GetCertificate = nil
  737. clientHello = &clientHelloMsg{
  738. vers: VersionTLS10,
  739. cipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
  740. compressionMethods: []uint8{compressionNone},
  741. }
  742. testClientHelloFailure(t, &serverConfig, clientHello, "no certificates")
  743. }
  744. // TestCipherSuiteCertPreferance ensures that we select an RSA ciphersuite with
  745. // an RSA certificate and an ECDSA ciphersuite with an ECDSA certificate.
  746. func TestCipherSuiteCertPreferenceECDSA(t *testing.T) {
  747. config := *testConfig
  748. config.CipherSuites = []uint16{TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA}
  749. config.PreferServerCipherSuites = true
  750. test := &serverTest{
  751. name: "CipherSuiteCertPreferenceRSA",
  752. config: &config,
  753. }
  754. runServerTestTLS12(t, test)
  755. config = *testConfig
  756. config.CipherSuites = []uint16{TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA}
  757. config.Certificates = []Certificate{
  758. {
  759. Certificate: [][]byte{testECDSACertificate},
  760. PrivateKey: testECDSAPrivateKey,
  761. },
  762. }
  763. config.BuildNameToCertificate()
  764. config.PreferServerCipherSuites = true
  765. test = &serverTest{
  766. name: "CipherSuiteCertPreferenceECDSA",
  767. config: &config,
  768. }
  769. runServerTestTLS12(t, test)
  770. }
  771. func TestResumption(t *testing.T) {
  772. sessionFilePath := tempFile("")
  773. defer os.Remove(sessionFilePath)
  774. test := &serverTest{
  775. name: "IssueTicket",
  776. command: []string{"openssl", "s_client", "-cipher", "RC4-SHA", "-sess_out", sessionFilePath},
  777. }
  778. runServerTestTLS12(t, test)
  779. test = &serverTest{
  780. name: "Resume",
  781. command: []string{"openssl", "s_client", "-cipher", "RC4-SHA", "-sess_in", sessionFilePath},
  782. }
  783. runServerTestTLS12(t, test)
  784. }
  785. func TestResumptionDisabled(t *testing.T) {
  786. sessionFilePath := tempFile("")
  787. defer os.Remove(sessionFilePath)
  788. config := *testConfig
  789. test := &serverTest{
  790. name: "IssueTicketPreDisable",
  791. command: []string{"openssl", "s_client", "-cipher", "RC4-SHA", "-sess_out", sessionFilePath},
  792. config: &config,
  793. }
  794. runServerTestTLS12(t, test)
  795. config.SessionTicketsDisabled = true
  796. test = &serverTest{
  797. name: "ResumeDisabled",
  798. command: []string{"openssl", "s_client", "-cipher", "RC4-SHA", "-sess_in", sessionFilePath},
  799. config: &config,
  800. }
  801. runServerTestTLS12(t, test)
  802. // One needs to manually confirm that the handshake in the golden data
  803. // file for ResumeDisabled does not include a resumption handshake.
  804. }
  805. func TestFallbackSCSV(t *testing.T) {
  806. serverConfig := &Config{
  807. Certificates: testConfig.Certificates,
  808. }
  809. test := &serverTest{
  810. name: "FallbackSCSV",
  811. config: serverConfig,
  812. // OpenSSL 1.0.1j is needed for the -fallback_scsv option.
  813. command: []string{"openssl", "s_client", "-fallback_scsv"},
  814. expectHandshakeErrorIncluding: "inappropriate protocol fallback",
  815. }
  816. runServerTestTLS11(t, test)
  817. }
  818. // cert.pem and key.pem were generated with generate_cert.go
  819. // Thus, they have no ExtKeyUsage fields and trigger an error
  820. // when verification is turned on.
  821. const clientCertificatePEM = `
  822. -----BEGIN CERTIFICATE-----
  823. MIIB7TCCAVigAwIBAgIBADALBgkqhkiG9w0BAQUwJjEQMA4GA1UEChMHQWNtZSBD
  824. bzESMBAGA1UEAxMJMTI3LjAuMC4xMB4XDTExMTIwODA3NTUxMloXDTEyMTIwNzA4
  825. MDAxMlowJjEQMA4GA1UEChMHQWNtZSBDbzESMBAGA1UEAxMJMTI3LjAuMC4xMIGc
  826. MAsGCSqGSIb3DQEBAQOBjAAwgYgCgYBO0Hsx44Jk2VnAwoekXh6LczPHY1PfZpIG
  827. hPZk1Y/kNqcdK+izIDZFI7Xjla7t4PUgnI2V339aEu+H5Fto5OkOdOwEin/ekyfE
  828. ARl6vfLcPRSr0FTKIQzQTW6HLlzF0rtNS0/Otiz3fojsfNcCkXSmHgwa2uNKWi7e
  829. E5xMQIhZkwIDAQABozIwMDAOBgNVHQ8BAf8EBAMCAKAwDQYDVR0OBAYEBAECAwQw
  830. DwYDVR0jBAgwBoAEAQIDBDALBgkqhkiG9w0BAQUDgYEANh+zegx1yW43RmEr1b3A
  831. p0vMRpqBWHyFeSnIyMZn3TJWRSt1tukkqVCavh9a+hoV2cxVlXIWg7nCto/9iIw4
  832. hB2rXZIxE0/9gzvGnfERYraL7KtnvshksBFQRlgXa5kc0x38BvEO5ZaoDPl4ILdE
  833. GFGNEH5PlGffo05wc46QkYU=
  834. -----END CERTIFICATE-----`
  835. const clientKeyPEM = `
  836. -----BEGIN RSA PRIVATE KEY-----
  837. MIICWgIBAAKBgE7QezHjgmTZWcDCh6ReHotzM8djU99mkgaE9mTVj+Q2px0r6LMg
  838. NkUjteOVru3g9SCcjZXff1oS74fkW2jk6Q507ASKf96TJ8QBGXq98tw9FKvQVMoh
  839. DNBNbocuXMXSu01LT862LPd+iOx81wKRdKYeDBra40paLt4TnExAiFmTAgMBAAEC
  840. gYBxvXd8yNteFTns8A/2yomEMC4yeosJJSpp1CsN3BJ7g8/qTnrVPxBy+RU+qr63
  841. t2WquaOu/cr5P8iEsa6lk20tf8pjKLNXeX0b1RTzK8rJLbS7nGzP3tvOhL096VtQ
  842. dAo4ROEaro0TzYpHmpciSvxVIeEIAAdFDObDJPKqcJAxyQJBAJizfYgK8Gzx9fsx
  843. hxp+VteCbVPg2euASH5Yv3K5LukRdKoSzHE2grUVQgN/LafC0eZibRanxHegYSr7
  844. 7qaswKUCQQCEIWor/X4XTMdVj3Oj+vpiw75y/S9gh682+myZL+d/02IEkwnB098P
  845. RkKVpenBHyrGg0oeN5La7URILWKj7CPXAkBKo6F+d+phNjwIFoN1Xb/RA32w/D1I
  846. saG9sF+UEhRt9AxUfW/U/tIQ9V0ZHHcSg1XaCM5Nvp934brdKdvTOKnJAkBD5h/3
  847. Rybatlvg/fzBEaJFyq09zhngkxlZOUtBVTqzl17RVvY2orgH02U4HbCHy4phxOn7
  848. qTdQRYlHRftgnWK1AkANibn9PRYJ7mJyJ9Dyj2QeNcSkSTzrt0tPvUMf4+meJymN
  849. 1Ntu5+S1DLLzfxlaljWG6ylW6DNxujCyuXIV2rvA
  850. -----END RSA PRIVATE KEY-----`
  851. const clientECDSACertificatePEM = `
  852. -----BEGIN CERTIFICATE-----
  853. MIIB/DCCAV4CCQCaMIRsJjXZFzAJBgcqhkjOPQQBMEUxCzAJBgNVBAYTAkFVMRMw
  854. EQYDVQQIEwpTb21lLVN0YXRlMSEwHwYDVQQKExhJbnRlcm5ldCBXaWRnaXRzIFB0
  855. eSBMdGQwHhcNMTIxMTE0MTMyNTUzWhcNMjIxMTEyMTMyNTUzWjBBMQswCQYDVQQG
  856. EwJBVTEMMAoGA1UECBMDTlNXMRAwDgYDVQQHEwdQeXJtb250MRIwEAYDVQQDEwlK
  857. b2VsIFNpbmcwgZswEAYHKoZIzj0CAQYFK4EEACMDgYYABACVjJF1FMBexFe01MNv
  858. ja5oHt1vzobhfm6ySD6B5U7ixohLZNz1MLvT/2XMW/TdtWo+PtAd3kfDdq0Z9kUs
  859. jLzYHQFMH3CQRnZIi4+DzEpcj0B22uCJ7B0rxE4wdihBsmKo+1vx+U56jb0JuK7q
  860. ixgnTy5w/hOWusPTQBbNZU6sER7m8TAJBgcqhkjOPQQBA4GMADCBiAJCAOAUxGBg
  861. C3JosDJdYUoCdFzCgbkWqD8pyDbHgf9stlvZcPE4O1BIKJTLCRpS8V3ujfK58PDa
  862. 2RU6+b0DeoeiIzXsAkIBo9SKeDUcSpoj0gq+KxAxnZxfvuiRs9oa9V2jI/Umi0Vw
  863. jWVim34BmT0Y9hCaOGGbLlfk+syxis7iI6CH8OFnUes=
  864. -----END CERTIFICATE-----`
  865. const clientECDSAKeyPEM = `
  866. -----BEGIN EC PARAMETERS-----
  867. BgUrgQQAIw==
  868. -----END EC PARAMETERS-----
  869. -----BEGIN EC PRIVATE KEY-----
  870. MIHcAgEBBEIBkJN9X4IqZIguiEVKMqeBUP5xtRsEv4HJEtOpOGLELwO53SD78Ew8
  871. k+wLWoqizS3NpQyMtrU8JFdWfj+C57UNkOugBwYFK4EEACOhgYkDgYYABACVjJF1
  872. FMBexFe01MNvja5oHt1vzobhfm6ySD6B5U7ixohLZNz1MLvT/2XMW/TdtWo+PtAd
  873. 3kfDdq0Z9kUsjLzYHQFMH3CQRnZIi4+DzEpcj0B22uCJ7B0rxE4wdihBsmKo+1vx
  874. +U56jb0JuK7qixgnTy5w/hOWusPTQBbNZU6sER7m8Q==
  875. -----END EC PRIVATE KEY-----`
  876. func TestClientAuth(t *testing.T) {
  877. var certPath, keyPath, ecdsaCertPath, ecdsaKeyPath string
  878. if *update {
  879. certPath = tempFile(clientCertificatePEM)
  880. defer os.Remove(certPath)
  881. keyPath = tempFile(clientKeyPEM)
  882. defer os.Remove(keyPath)
  883. ecdsaCertPath = tempFile(clientECDSACertificatePEM)
  884. defer os.Remove(ecdsaCertPath)
  885. ecdsaKeyPath = tempFile(clientECDSAKeyPEM)
  886. defer os.Remove(ecdsaKeyPath)
  887. }
  888. config := *testConfig
  889. config.ClientAuth = RequestClientCert
  890. test := &serverTest{
  891. name: "ClientAuthRequestedNotGiven",
  892. command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "RC4-SHA"},
  893. config: &config,
  894. }
  895. runServerTestTLS12(t, test)
  896. test = &serverTest{
  897. name: "ClientAuthRequestedAndGiven",
  898. command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "RC4-SHA", "-cert", certPath, "-key", keyPath},
  899. config: &config,
  900. expectedPeerCerts: []string{clientCertificatePEM},
  901. }
  902. runServerTestTLS12(t, test)
  903. test = &serverTest{
  904. name: "ClientAuthRequestedAndECDSAGiven",
  905. command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "RC4-SHA", "-cert", ecdsaCertPath, "-key", ecdsaKeyPath},
  906. config: &config,
  907. expectedPeerCerts: []string{clientECDSACertificatePEM},
  908. }
  909. runServerTestTLS12(t, test)
  910. }
  911. func bigFromString(s string) *big.Int {
  912. ret := new(big.Int)
  913. ret.SetString(s, 10)
  914. return ret
  915. }
  916. func fromHex(s string) []byte {
  917. b, _ := hex.DecodeString(s)
  918. return b
  919. }
  920. var testRSACertificate = fromHex("30820263308201cca003020102020900a273000c8100cbf3300d06092a864886f70d01010b0500302b31173015060355040a130e476f6f676c652054455354494e473110300e06035504031307476f20526f6f74301e170d3135303130313030303030305a170d3235303130313030303030305a302631173015060355040a130e476f6f676c652054455354494e47310b300906035504031302476f30819f300d06092a864886f70d010101050003818d0030818902818100af8788f6201b95656c14ab4405af3b4514e3b76dfd00634d957ffe6a623586c04af9187cf6aa255e7a64316600baf48e92afc76bd876d4f35f41cb6e5615971b97c13c123921663d2b16d1bcdb1cc0a7dab7caadbadacbd52150ecde8dabd16b814b8902f3c4bec16c89b14484bd21d1047d9d164df98215f6effad60947f2fb0203010001a38193308190300e0603551d0f0101ff0404030205a0301d0603551d250416301406082b0601050507030106082b06010505070302300c0603551d130101ff0402300030190603551d0e0412041012508d896f1bd1dc544d6ecb695e06f4301b0603551d23041430128010bf3db6a966f2b840cfeab40378481a4130190603551d1104123010820e6578616d706c652e676f6c616e67300d06092a864886f70d01010b050003818100927caf91551218965931a64840d52dd5eebb02a0f5c21e7c9bb3307d3cdc76da4f3dc0faae2d33246b037b1b67591121b511bc77b9d9e06ea82d2e35fa645f223e63106bbeff14866d0df01531a814381e3b84872ccb98ed5176b9b14fdddb9b84048640fa51ddbab48debe346de46b94f86c7f9a4c24134acccf6eab0ab3918")
  921. var testRSACertificateIssuer = fromHex("3082024d308201b6a003020102020827326bd913b7c43d300d06092a864886f70d01010b0500302b31173015060355040a130e476f6f676c652054455354494e473110300e06035504031307476f20526f6f74301e170d3135303130313030303030305a170d3235303130313030303030305a302b31173015060355040a130e476f6f676c652054455354494e473110300e06035504031307476f20526f6f7430819f300d06092a864886f70d010101050003818d0030818902818100f0429a7b9f66a222c8453800452db355b34c4409fee09af2510a6589bfa35bdb4d453200d1de24338d6d5e5a91cc8301628445d6eb4e675927b9c1ea5c0f676acfb0f708ce4f19827e321c1898bf86df9823d5f0b05df2b6779888eff8abbc7f41c6e7d2667386a579b8cbaad3f6fd597cd7c4b187911a425aed1b555c1965190203010001a37a3078300e0603551d0f0101ff040403020204301d0603551d250416301406082b0601050507030106082b06010505070302300f0603551d130101ff040530030101ff30190603551d0e04120410bf3db6a966f2b840cfeab40378481a41301b0603551d23041430128010bf3db6a966f2b840cfeab40378481a41300d06092a864886f70d01010b050003818100586e68c1219ed4f5782b7cfd53cf1a55750a98781b2023f8694bb831fff6d7d4aad1f0ac782b1ec787f00a8956bdd06b4a1063444fcafe955c07d679163a730802c568886a2cf8a3c2ab41176957131c4b9e077ebd7ffbb91fdad8b08b932e9aeefac04923ffdc0aa145563f7f061995317400203578f350e3e566deb29dec5e")
  922. var testECDSACertificate = fromHex("3082020030820162020900b8bf2d47a0d2ebf4300906072a8648ce3d04013045310b3009060355040613024155311330110603550408130a536f6d652d53746174653121301f060355040a1318496e7465726e6574205769646769747320507479204c7464301e170d3132313132323135303633325a170d3232313132303135303633325a3045310b3009060355040613024155311330110603550408130a536f6d652d53746174653121301f060355040a1318496e7465726e6574205769646769747320507479204c746430819b301006072a8648ce3d020106052b81040023038186000400c4a1edbe98f90b4873367ec316561122f23d53c33b4d213dcd6b75e6f6b0dc9adf26c1bcb287f072327cb3642f1c90bcea6823107efee325c0483a69e0286dd33700ef0462dd0da09c706283d881d36431aa9e9731bd96b068c09b23de76643f1a5c7fe9120e5858b65f70dd9bd8ead5d7f5d5ccb9b69f30665b669a20e227e5bffe3b300906072a8648ce3d040103818c0030818802420188a24febe245c5487d1bacf5ed989dae4770c05e1bb62fbdf1b64db76140d311a2ceee0b7e927eff769dc33b7ea53fcefa10e259ec472d7cacda4e970e15a06fd00242014dfcbe67139c2d050ebd3fa38c25c13313830d9406bbd4377af6ec7ac9862eddd711697f857c56defb31782be4c7780daecbbe9e4e3624317b6a0f399512078f2a")
  923. var testSNICertificate = fromHex("308201f23082015da003020102020100300b06092a864886f70d01010530283110300e060355040a130741636d6520436f311430120603550403130b736e69746573742e636f6d301e170d3132303431313137343033355a170d3133303431313137343533355a30283110300e060355040a130741636d6520436f311430120603550403130b736e69746573742e636f6d30819d300b06092a864886f70d01010103818d0030818902818100bb79d6f517b5e5bf4610d0dc69bee62b07435ad0032d8a7a4385b71452e7a5654c2c78b8238cb5b482e5de1f953b7e62a52ca533d6fe125c7a56fcf506bffa587b263fb5cd04d3d0c921964ac7f4549f5abfef427100fe1899077f7e887d7df10439c4a22edb51c97ce3c04c3b326601cfafb11db8719a1ddbdb896baeda2d790203010001a3323030300e0603551d0f0101ff0404030200a0300d0603551d0e0406040401020304300f0603551d2304083006800401020304300b06092a864886f70d0101050381810089c6455f1c1f5ef8eb1ab174ee2439059f5c4259bb1a8d86cdb1d056f56a717da40e95ab90f59e8deaf627c157995094db0802266eb34fc6842dea8a4b68d9c1389103ab84fb9e1f85d9b5d23ff2312c8670fbb540148245a4ebafe264d90c8a4cf4f85b0fac12ac2fc4a3154bad52462868af96c62c6525d652b6e31845bdcc")
  924. var testRSAPrivateKey = &rsa.PrivateKey{
  925. PublicKey: rsa.PublicKey{
  926. N: bigFromString("123260960069105588390096594560395120585636206567569540256061833976822892593755073841963170165000086278069699238754008398039246547214989242849418349143232951701395321381739566687846006911427966669790845430647688107009232778985142860108863460556510585049041936029324503323373417214453307648498561956908810892027L"),
  927. E: 65537,
  928. },
  929. D: bigFromString("73196363031103823625826315929954946106043759818067219550565550066527203472294428548476778865091068522665312037075674791871635825938217363523103946045078950060973913307430314113074463630778799389010335923241901501086246276485964417618981733827707048660375428006201525399194575538037883519254056917253456403553L"),
  930. Primes: []*big.Int{
  931. bigFromString("11157426355495284553529769521954035649776033703833034489026848970480272318436419662860715175517581249375929775774910501512841707465207184924996975125010787L"),
  932. bigFromString("11047436580963564307160117670964629323534448585520694947919342920137706075617545637058809770319843170934495909554506529982972972247390145716507031692656521L"),
  933. },
  934. }
  935. var testECDSAPrivateKey = &ecdsa.PrivateKey{
  936. PublicKey: ecdsa.PublicKey{
  937. Curve: elliptic.P521(),
  938. X: bigFromString("2636411247892461147287360222306590634450676461695221912739908880441342231985950069527906976759812296359387337367668045707086543273113073382714101597903639351"),
  939. Y: bigFromString("3204695818431246682253994090650952614555094516658732116404513121125038617915183037601737180082382202488628239201196033284060130040574800684774115478859677243"),
  940. },
  941. D: bigFromString("5477294338614160138026852784385529180817726002953041720191098180813046231640184669647735805135001309477695746518160084669446643325196003346204701381388769751"),
  942. }