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.
 
 
 
 
 
 

1563 linhas
32 KiB

  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package tls
  5. import "bytes"
  6. type clientHelloMsg struct {
  7. raw []byte
  8. vers uint16
  9. random []byte
  10. sessionId []byte
  11. cipherSuites []uint16
  12. compressionMethods []uint8
  13. nextProtoNeg bool
  14. serverName string
  15. ocspStapling bool
  16. scts bool
  17. supportedCurves []CurveID
  18. supportedPoints []uint8
  19. ticketSupported bool
  20. sessionTicket []uint8
  21. signatureAndHashes []signatureAndHash
  22. secureRenegotiation []byte
  23. secureRenegotiationSupported bool
  24. alpnProtocols []string
  25. }
  26. func (m *clientHelloMsg) equal(i interface{}) bool {
  27. m1, ok := i.(*clientHelloMsg)
  28. if !ok {
  29. return false
  30. }
  31. return bytes.Equal(m.raw, m1.raw) &&
  32. m.vers == m1.vers &&
  33. bytes.Equal(m.random, m1.random) &&
  34. bytes.Equal(m.sessionId, m1.sessionId) &&
  35. eqUint16s(m.cipherSuites, m1.cipherSuites) &&
  36. bytes.Equal(m.compressionMethods, m1.compressionMethods) &&
  37. m.nextProtoNeg == m1.nextProtoNeg &&
  38. m.serverName == m1.serverName &&
  39. m.ocspStapling == m1.ocspStapling &&
  40. m.scts == m1.scts &&
  41. eqCurveIDs(m.supportedCurves, m1.supportedCurves) &&
  42. bytes.Equal(m.supportedPoints, m1.supportedPoints) &&
  43. m.ticketSupported == m1.ticketSupported &&
  44. bytes.Equal(m.sessionTicket, m1.sessionTicket) &&
  45. eqSignatureAndHashes(m.signatureAndHashes, m1.signatureAndHashes) &&
  46. m.secureRenegotiationSupported == m1.secureRenegotiationSupported &&
  47. bytes.Equal(m.secureRenegotiation, m1.secureRenegotiation) &&
  48. eqStrings(m.alpnProtocols, m1.alpnProtocols)
  49. }
  50. func (m *clientHelloMsg) marshal() []byte {
  51. if m.raw != nil {
  52. return m.raw
  53. }
  54. length := 2 + 32 + 1 + len(m.sessionId) + 2 + len(m.cipherSuites)*2 + 1 + len(m.compressionMethods)
  55. numExtensions := 0
  56. extensionsLength := 0
  57. if m.nextProtoNeg {
  58. numExtensions++
  59. }
  60. if m.ocspStapling {
  61. extensionsLength += 1 + 2 + 2
  62. numExtensions++
  63. }
  64. if len(m.serverName) > 0 {
  65. extensionsLength += 5 + len(m.serverName)
  66. numExtensions++
  67. }
  68. if len(m.supportedCurves) > 0 {
  69. extensionsLength += 2 + 2*len(m.supportedCurves)
  70. numExtensions++
  71. }
  72. if len(m.supportedPoints) > 0 {
  73. extensionsLength += 1 + len(m.supportedPoints)
  74. numExtensions++
  75. }
  76. if m.ticketSupported {
  77. extensionsLength += len(m.sessionTicket)
  78. numExtensions++
  79. }
  80. if len(m.signatureAndHashes) > 0 {
  81. extensionsLength += 2 + 2*len(m.signatureAndHashes)
  82. numExtensions++
  83. }
  84. if m.secureRenegotiationSupported {
  85. extensionsLength += 1 + len(m.secureRenegotiation)
  86. numExtensions++
  87. }
  88. if len(m.alpnProtocols) > 0 {
  89. extensionsLength += 2
  90. for _, s := range m.alpnProtocols {
  91. if l := len(s); l == 0 || l > 255 {
  92. panic("invalid ALPN protocol")
  93. }
  94. extensionsLength++
  95. extensionsLength += len(s)
  96. }
  97. numExtensions++
  98. }
  99. if m.scts {
  100. numExtensions++
  101. }
  102. if numExtensions > 0 {
  103. extensionsLength += 4 * numExtensions
  104. length += 2 + extensionsLength
  105. }
  106. x := make([]byte, 4+length)
  107. x[0] = typeClientHello
  108. x[1] = uint8(length >> 16)
  109. x[2] = uint8(length >> 8)
  110. x[3] = uint8(length)
  111. x[4] = uint8(m.vers >> 8)
  112. x[5] = uint8(m.vers)
  113. copy(x[6:38], m.random)
  114. x[38] = uint8(len(m.sessionId))
  115. copy(x[39:39+len(m.sessionId)], m.sessionId)
  116. y := x[39+len(m.sessionId):]
  117. y[0] = uint8(len(m.cipherSuites) >> 7)
  118. y[1] = uint8(len(m.cipherSuites) << 1)
  119. for i, suite := range m.cipherSuites {
  120. y[2+i*2] = uint8(suite >> 8)
  121. y[3+i*2] = uint8(suite)
  122. }
  123. z := y[2+len(m.cipherSuites)*2:]
  124. z[0] = uint8(len(m.compressionMethods))
  125. copy(z[1:], m.compressionMethods)
  126. z = z[1+len(m.compressionMethods):]
  127. if numExtensions > 0 {
  128. z[0] = byte(extensionsLength >> 8)
  129. z[1] = byte(extensionsLength)
  130. z = z[2:]
  131. }
  132. if m.nextProtoNeg {
  133. z[0] = byte(extensionNextProtoNeg >> 8)
  134. z[1] = byte(extensionNextProtoNeg & 0xff)
  135. // The length is always 0
  136. z = z[4:]
  137. }
  138. if len(m.serverName) > 0 {
  139. z[0] = byte(extensionServerName >> 8)
  140. z[1] = byte(extensionServerName & 0xff)
  141. l := len(m.serverName) + 5
  142. z[2] = byte(l >> 8)
  143. z[3] = byte(l)
  144. z = z[4:]
  145. // RFC 3546, section 3.1
  146. //
  147. // struct {
  148. // NameType name_type;
  149. // select (name_type) {
  150. // case host_name: HostName;
  151. // } name;
  152. // } ServerName;
  153. //
  154. // enum {
  155. // host_name(0), (255)
  156. // } NameType;
  157. //
  158. // opaque HostName<1..2^16-1>;
  159. //
  160. // struct {
  161. // ServerName server_name_list<1..2^16-1>
  162. // } ServerNameList;
  163. z[0] = byte((len(m.serverName) + 3) >> 8)
  164. z[1] = byte(len(m.serverName) + 3)
  165. z[3] = byte(len(m.serverName) >> 8)
  166. z[4] = byte(len(m.serverName))
  167. copy(z[5:], []byte(m.serverName))
  168. z = z[l:]
  169. }
  170. if m.ocspStapling {
  171. // RFC 4366, section 3.6
  172. z[0] = byte(extensionStatusRequest >> 8)
  173. z[1] = byte(extensionStatusRequest)
  174. z[2] = 0
  175. z[3] = 5
  176. z[4] = 1 // OCSP type
  177. // Two zero valued uint16s for the two lengths.
  178. z = z[9:]
  179. }
  180. if len(m.supportedCurves) > 0 {
  181. // http://tools.ietf.org/html/rfc4492#section-5.5.1
  182. z[0] = byte(extensionSupportedCurves >> 8)
  183. z[1] = byte(extensionSupportedCurves)
  184. l := 2 + 2*len(m.supportedCurves)
  185. z[2] = byte(l >> 8)
  186. z[3] = byte(l)
  187. l -= 2
  188. z[4] = byte(l >> 8)
  189. z[5] = byte(l)
  190. z = z[6:]
  191. for _, curve := range m.supportedCurves {
  192. z[0] = byte(curve >> 8)
  193. z[1] = byte(curve)
  194. z = z[2:]
  195. }
  196. }
  197. if len(m.supportedPoints) > 0 {
  198. // http://tools.ietf.org/html/rfc4492#section-5.5.2
  199. z[0] = byte(extensionSupportedPoints >> 8)
  200. z[1] = byte(extensionSupportedPoints)
  201. l := 1 + len(m.supportedPoints)
  202. z[2] = byte(l >> 8)
  203. z[3] = byte(l)
  204. l--
  205. z[4] = byte(l)
  206. z = z[5:]
  207. for _, pointFormat := range m.supportedPoints {
  208. z[0] = pointFormat
  209. z = z[1:]
  210. }
  211. }
  212. if m.ticketSupported {
  213. // http://tools.ietf.org/html/rfc5077#section-3.2
  214. z[0] = byte(extensionSessionTicket >> 8)
  215. z[1] = byte(extensionSessionTicket)
  216. l := len(m.sessionTicket)
  217. z[2] = byte(l >> 8)
  218. z[3] = byte(l)
  219. z = z[4:]
  220. copy(z, m.sessionTicket)
  221. z = z[len(m.sessionTicket):]
  222. }
  223. if len(m.signatureAndHashes) > 0 {
  224. // https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
  225. z[0] = byte(extensionSignatureAlgorithms >> 8)
  226. z[1] = byte(extensionSignatureAlgorithms)
  227. l := 2 + 2*len(m.signatureAndHashes)
  228. z[2] = byte(l >> 8)
  229. z[3] = byte(l)
  230. z = z[4:]
  231. l -= 2
  232. z[0] = byte(l >> 8)
  233. z[1] = byte(l)
  234. z = z[2:]
  235. for _, sigAndHash := range m.signatureAndHashes {
  236. z[0] = sigAndHash.hash
  237. z[1] = sigAndHash.signature
  238. z = z[2:]
  239. }
  240. }
  241. if m.secureRenegotiationSupported {
  242. z[0] = byte(extensionRenegotiationInfo >> 8)
  243. z[1] = byte(extensionRenegotiationInfo & 0xff)
  244. z[2] = 0
  245. z[3] = byte(len(m.secureRenegotiation) + 1)
  246. z[4] = byte(len(m.secureRenegotiation))
  247. z = z[5:]
  248. copy(z, m.secureRenegotiation)
  249. z = z[len(m.secureRenegotiation):]
  250. }
  251. if len(m.alpnProtocols) > 0 {
  252. z[0] = byte(extensionALPN >> 8)
  253. z[1] = byte(extensionALPN & 0xff)
  254. lengths := z[2:]
  255. z = z[6:]
  256. stringsLength := 0
  257. for _, s := range m.alpnProtocols {
  258. l := len(s)
  259. z[0] = byte(l)
  260. copy(z[1:], s)
  261. z = z[1+l:]
  262. stringsLength += 1 + l
  263. }
  264. lengths[2] = byte(stringsLength >> 8)
  265. lengths[3] = byte(stringsLength)
  266. stringsLength += 2
  267. lengths[0] = byte(stringsLength >> 8)
  268. lengths[1] = byte(stringsLength)
  269. }
  270. if m.scts {
  271. // https://tools.ietf.org/html/rfc6962#section-3.3.1
  272. z[0] = byte(extensionSCT >> 8)
  273. z[1] = byte(extensionSCT)
  274. // zero uint16 for the zero-length extension_data
  275. z = z[4:]
  276. }
  277. m.raw = x
  278. return x
  279. }
  280. func (m *clientHelloMsg) unmarshal(data []byte) bool {
  281. if len(data) < 42 {
  282. return false
  283. }
  284. m.raw = data
  285. m.vers = uint16(data[4])<<8 | uint16(data[5])
  286. m.random = data[6:38]
  287. sessionIdLen := int(data[38])
  288. if sessionIdLen > 32 || len(data) < 39+sessionIdLen {
  289. return false
  290. }
  291. m.sessionId = data[39 : 39+sessionIdLen]
  292. data = data[39+sessionIdLen:]
  293. if len(data) < 2 {
  294. return false
  295. }
  296. // cipherSuiteLen is the number of bytes of cipher suite numbers. Since
  297. // they are uint16s, the number must be even.
  298. cipherSuiteLen := int(data[0])<<8 | int(data[1])
  299. if cipherSuiteLen%2 == 1 || len(data) < 2+cipherSuiteLen {
  300. return false
  301. }
  302. numCipherSuites := cipherSuiteLen / 2
  303. m.cipherSuites = make([]uint16, numCipherSuites)
  304. for i := 0; i < numCipherSuites; i++ {
  305. m.cipherSuites[i] = uint16(data[2+2*i])<<8 | uint16(data[3+2*i])
  306. if m.cipherSuites[i] == scsvRenegotiation {
  307. m.secureRenegotiationSupported = true
  308. }
  309. }
  310. data = data[2+cipherSuiteLen:]
  311. if len(data) < 1 {
  312. return false
  313. }
  314. compressionMethodsLen := int(data[0])
  315. if len(data) < 1+compressionMethodsLen {
  316. return false
  317. }
  318. m.compressionMethods = data[1 : 1+compressionMethodsLen]
  319. data = data[1+compressionMethodsLen:]
  320. m.nextProtoNeg = false
  321. m.serverName = ""
  322. m.ocspStapling = false
  323. m.ticketSupported = false
  324. m.sessionTicket = nil
  325. m.signatureAndHashes = nil
  326. m.alpnProtocols = nil
  327. m.scts = false
  328. if len(data) == 0 {
  329. // ClientHello is optionally followed by extension data
  330. return true
  331. }
  332. if len(data) < 2 {
  333. return false
  334. }
  335. extensionsLength := int(data[0])<<8 | int(data[1])
  336. data = data[2:]
  337. if extensionsLength != len(data) {
  338. return false
  339. }
  340. for len(data) != 0 {
  341. if len(data) < 4 {
  342. return false
  343. }
  344. extension := uint16(data[0])<<8 | uint16(data[1])
  345. length := int(data[2])<<8 | int(data[3])
  346. data = data[4:]
  347. if len(data) < length {
  348. return false
  349. }
  350. switch extension {
  351. case extensionServerName:
  352. d := data[:length]
  353. if len(d) < 2 {
  354. return false
  355. }
  356. namesLen := int(d[0])<<8 | int(d[1])
  357. d = d[2:]
  358. if len(d) != namesLen {
  359. return false
  360. }
  361. for len(d) > 0 {
  362. if len(d) < 3 {
  363. return false
  364. }
  365. nameType := d[0]
  366. nameLen := int(d[1])<<8 | int(d[2])
  367. d = d[3:]
  368. if len(d) < nameLen {
  369. return false
  370. }
  371. if nameType == 0 {
  372. m.serverName = string(d[:nameLen])
  373. break
  374. }
  375. d = d[nameLen:]
  376. }
  377. case extensionNextProtoNeg:
  378. if length > 0 {
  379. return false
  380. }
  381. m.nextProtoNeg = true
  382. case extensionStatusRequest:
  383. m.ocspStapling = length > 0 && data[0] == statusTypeOCSP
  384. case extensionSupportedCurves:
  385. // http://tools.ietf.org/html/rfc4492#section-5.5.1
  386. if length < 2 {
  387. return false
  388. }
  389. l := int(data[0])<<8 | int(data[1])
  390. if l%2 == 1 || length != l+2 {
  391. return false
  392. }
  393. numCurves := l / 2
  394. m.supportedCurves = make([]CurveID, numCurves)
  395. d := data[2:]
  396. for i := 0; i < numCurves; i++ {
  397. m.supportedCurves[i] = CurveID(d[0])<<8 | CurveID(d[1])
  398. d = d[2:]
  399. }
  400. case extensionSupportedPoints:
  401. // http://tools.ietf.org/html/rfc4492#section-5.5.2
  402. if length < 1 {
  403. return false
  404. }
  405. l := int(data[0])
  406. if length != l+1 {
  407. return false
  408. }
  409. m.supportedPoints = make([]uint8, l)
  410. copy(m.supportedPoints, data[1:])
  411. case extensionSessionTicket:
  412. // http://tools.ietf.org/html/rfc5077#section-3.2
  413. m.ticketSupported = true
  414. m.sessionTicket = data[:length]
  415. case extensionSignatureAlgorithms:
  416. // https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
  417. if length < 2 || length&1 != 0 {
  418. return false
  419. }
  420. l := int(data[0])<<8 | int(data[1])
  421. if l != length-2 {
  422. return false
  423. }
  424. n := l / 2
  425. d := data[2:]
  426. m.signatureAndHashes = make([]signatureAndHash, n)
  427. for i := range m.signatureAndHashes {
  428. m.signatureAndHashes[i].hash = d[0]
  429. m.signatureAndHashes[i].signature = d[1]
  430. d = d[2:]
  431. }
  432. case extensionRenegotiationInfo:
  433. if length == 0 {
  434. return false
  435. }
  436. d := data[:length]
  437. l := int(d[0])
  438. d = d[1:]
  439. if l != len(d) {
  440. return false
  441. }
  442. m.secureRenegotiation = d
  443. m.secureRenegotiationSupported = true
  444. case extensionALPN:
  445. if length < 2 {
  446. return false
  447. }
  448. l := int(data[0])<<8 | int(data[1])
  449. if l != length-2 {
  450. return false
  451. }
  452. d := data[2:length]
  453. for len(d) != 0 {
  454. stringLen := int(d[0])
  455. d = d[1:]
  456. if stringLen == 0 || stringLen > len(d) {
  457. return false
  458. }
  459. m.alpnProtocols = append(m.alpnProtocols, string(d[:stringLen]))
  460. d = d[stringLen:]
  461. }
  462. case extensionSCT:
  463. m.scts = true
  464. if length != 0 {
  465. return false
  466. }
  467. }
  468. data = data[length:]
  469. }
  470. return true
  471. }
  472. type serverHelloMsg struct {
  473. raw []byte
  474. vers uint16
  475. random []byte
  476. sessionId []byte
  477. cipherSuite uint16
  478. compressionMethod uint8
  479. nextProtoNeg bool
  480. nextProtos []string
  481. ocspStapling bool
  482. scts [][]byte
  483. ticketSupported bool
  484. secureRenegotiation []byte
  485. secureRenegotiationSupported bool
  486. alpnProtocol string
  487. }
  488. func (m *serverHelloMsg) equal(i interface{}) bool {
  489. m1, ok := i.(*serverHelloMsg)
  490. if !ok {
  491. return false
  492. }
  493. if len(m.scts) != len(m1.scts) {
  494. return false
  495. }
  496. for i, sct := range m.scts {
  497. if !bytes.Equal(sct, m1.scts[i]) {
  498. return false
  499. }
  500. }
  501. return bytes.Equal(m.raw, m1.raw) &&
  502. m.vers == m1.vers &&
  503. bytes.Equal(m.random, m1.random) &&
  504. bytes.Equal(m.sessionId, m1.sessionId) &&
  505. m.cipherSuite == m1.cipherSuite &&
  506. m.compressionMethod == m1.compressionMethod &&
  507. m.nextProtoNeg == m1.nextProtoNeg &&
  508. eqStrings(m.nextProtos, m1.nextProtos) &&
  509. m.ocspStapling == m1.ocspStapling &&
  510. m.ticketSupported == m1.ticketSupported &&
  511. m.secureRenegotiationSupported == m1.secureRenegotiationSupported &&
  512. bytes.Equal(m.secureRenegotiation, m1.secureRenegotiation) &&
  513. m.alpnProtocol == m1.alpnProtocol
  514. }
  515. func (m *serverHelloMsg) marshal() []byte {
  516. if m.raw != nil {
  517. return m.raw
  518. }
  519. length := 38 + len(m.sessionId)
  520. numExtensions := 0
  521. extensionsLength := 0
  522. nextProtoLen := 0
  523. if m.nextProtoNeg {
  524. numExtensions++
  525. for _, v := range m.nextProtos {
  526. nextProtoLen += len(v)
  527. }
  528. nextProtoLen += len(m.nextProtos)
  529. extensionsLength += nextProtoLen
  530. }
  531. if m.ocspStapling {
  532. numExtensions++
  533. }
  534. if m.ticketSupported {
  535. numExtensions++
  536. }
  537. if m.secureRenegotiationSupported {
  538. extensionsLength += 1 + len(m.secureRenegotiation)
  539. numExtensions++
  540. }
  541. if alpnLen := len(m.alpnProtocol); alpnLen > 0 {
  542. if alpnLen >= 256 {
  543. panic("invalid ALPN protocol")
  544. }
  545. extensionsLength += 2 + 1 + alpnLen
  546. numExtensions++
  547. }
  548. sctLen := 0
  549. if len(m.scts) > 0 {
  550. for _, sct := range m.scts {
  551. sctLen += len(sct) + 2
  552. }
  553. extensionsLength += 2 + sctLen
  554. numExtensions++
  555. }
  556. if numExtensions > 0 {
  557. extensionsLength += 4 * numExtensions
  558. length += 2 + extensionsLength
  559. }
  560. x := make([]byte, 4+length)
  561. x[0] = typeServerHello
  562. x[1] = uint8(length >> 16)
  563. x[2] = uint8(length >> 8)
  564. x[3] = uint8(length)
  565. x[4] = uint8(m.vers >> 8)
  566. x[5] = uint8(m.vers)
  567. copy(x[6:38], m.random)
  568. x[38] = uint8(len(m.sessionId))
  569. copy(x[39:39+len(m.sessionId)], m.sessionId)
  570. z := x[39+len(m.sessionId):]
  571. z[0] = uint8(m.cipherSuite >> 8)
  572. z[1] = uint8(m.cipherSuite)
  573. z[2] = m.compressionMethod
  574. z = z[3:]
  575. if numExtensions > 0 {
  576. z[0] = byte(extensionsLength >> 8)
  577. z[1] = byte(extensionsLength)
  578. z = z[2:]
  579. }
  580. if m.nextProtoNeg {
  581. z[0] = byte(extensionNextProtoNeg >> 8)
  582. z[1] = byte(extensionNextProtoNeg & 0xff)
  583. z[2] = byte(nextProtoLen >> 8)
  584. z[3] = byte(nextProtoLen)
  585. z = z[4:]
  586. for _, v := range m.nextProtos {
  587. l := len(v)
  588. if l > 255 {
  589. l = 255
  590. }
  591. z[0] = byte(l)
  592. copy(z[1:], []byte(v[0:l]))
  593. z = z[1+l:]
  594. }
  595. }
  596. if m.ocspStapling {
  597. z[0] = byte(extensionStatusRequest >> 8)
  598. z[1] = byte(extensionStatusRequest)
  599. z = z[4:]
  600. }
  601. if m.ticketSupported {
  602. z[0] = byte(extensionSessionTicket >> 8)
  603. z[1] = byte(extensionSessionTicket)
  604. z = z[4:]
  605. }
  606. if m.secureRenegotiationSupported {
  607. z[0] = byte(extensionRenegotiationInfo >> 8)
  608. z[1] = byte(extensionRenegotiationInfo & 0xff)
  609. z[2] = 0
  610. z[3] = byte(len(m.secureRenegotiation) + 1)
  611. z[4] = byte(len(m.secureRenegotiation))
  612. z = z[5:]
  613. copy(z, m.secureRenegotiation)
  614. z = z[len(m.secureRenegotiation):]
  615. }
  616. if alpnLen := len(m.alpnProtocol); alpnLen > 0 {
  617. z[0] = byte(extensionALPN >> 8)
  618. z[1] = byte(extensionALPN & 0xff)
  619. l := 2 + 1 + alpnLen
  620. z[2] = byte(l >> 8)
  621. z[3] = byte(l)
  622. l -= 2
  623. z[4] = byte(l >> 8)
  624. z[5] = byte(l)
  625. l -= 1
  626. z[6] = byte(l)
  627. copy(z[7:], []byte(m.alpnProtocol))
  628. z = z[7+alpnLen:]
  629. }
  630. if sctLen > 0 {
  631. z[0] = byte(extensionSCT >> 8)
  632. z[1] = byte(extensionSCT)
  633. l := sctLen + 2
  634. z[2] = byte(l >> 8)
  635. z[3] = byte(l)
  636. z[4] = byte(sctLen >> 8)
  637. z[5] = byte(sctLen)
  638. z = z[6:]
  639. for _, sct := range m.scts {
  640. z[0] = byte(len(sct) >> 8)
  641. z[1] = byte(len(sct))
  642. copy(z[2:], sct)
  643. z = z[len(sct)+2:]
  644. }
  645. }
  646. m.raw = x
  647. return x
  648. }
  649. func (m *serverHelloMsg) unmarshal(data []byte) bool {
  650. if len(data) < 42 {
  651. return false
  652. }
  653. m.raw = data
  654. m.vers = uint16(data[4])<<8 | uint16(data[5])
  655. m.random = data[6:38]
  656. sessionIdLen := int(data[38])
  657. if sessionIdLen > 32 || len(data) < 39+sessionIdLen {
  658. return false
  659. }
  660. m.sessionId = data[39 : 39+sessionIdLen]
  661. data = data[39+sessionIdLen:]
  662. if len(data) < 3 {
  663. return false
  664. }
  665. m.cipherSuite = uint16(data[0])<<8 | uint16(data[1])
  666. m.compressionMethod = data[2]
  667. data = data[3:]
  668. m.nextProtoNeg = false
  669. m.nextProtos = nil
  670. m.ocspStapling = false
  671. m.scts = nil
  672. m.ticketSupported = false
  673. m.alpnProtocol = ""
  674. if len(data) == 0 {
  675. // ServerHello is optionally followed by extension data
  676. return true
  677. }
  678. if len(data) < 2 {
  679. return false
  680. }
  681. extensionsLength := int(data[0])<<8 | int(data[1])
  682. data = data[2:]
  683. if len(data) != extensionsLength {
  684. return false
  685. }
  686. for len(data) != 0 {
  687. if len(data) < 4 {
  688. return false
  689. }
  690. extension := uint16(data[0])<<8 | uint16(data[1])
  691. length := int(data[2])<<8 | int(data[3])
  692. data = data[4:]
  693. if len(data) < length {
  694. return false
  695. }
  696. switch extension {
  697. case extensionNextProtoNeg:
  698. m.nextProtoNeg = true
  699. d := data[:length]
  700. for len(d) > 0 {
  701. l := int(d[0])
  702. d = d[1:]
  703. if l == 0 || l > len(d) {
  704. return false
  705. }
  706. m.nextProtos = append(m.nextProtos, string(d[:l]))
  707. d = d[l:]
  708. }
  709. case extensionStatusRequest:
  710. if length > 0 {
  711. return false
  712. }
  713. m.ocspStapling = true
  714. case extensionSessionTicket:
  715. if length > 0 {
  716. return false
  717. }
  718. m.ticketSupported = true
  719. case extensionRenegotiationInfo:
  720. if length == 0 {
  721. return false
  722. }
  723. d := data[:length]
  724. l := int(d[0])
  725. d = d[1:]
  726. if l != len(d) {
  727. return false
  728. }
  729. m.secureRenegotiation = d
  730. m.secureRenegotiationSupported = true
  731. case extensionALPN:
  732. d := data[:length]
  733. if len(d) < 3 {
  734. return false
  735. }
  736. l := int(d[0])<<8 | int(d[1])
  737. if l != len(d)-2 {
  738. return false
  739. }
  740. d = d[2:]
  741. l = int(d[0])
  742. if l != len(d)-1 {
  743. return false
  744. }
  745. d = d[1:]
  746. if len(d) == 0 {
  747. // ALPN protocols must not be empty.
  748. return false
  749. }
  750. m.alpnProtocol = string(d)
  751. case extensionSCT:
  752. d := data[:length]
  753. if len(d) < 2 {
  754. return false
  755. }
  756. l := int(d[0])<<8 | int(d[1])
  757. d = d[2:]
  758. if len(d) != l {
  759. return false
  760. }
  761. if l == 0 {
  762. continue
  763. }
  764. m.scts = make([][]byte, 0, 3)
  765. for len(d) != 0 {
  766. if len(d) < 2 {
  767. return false
  768. }
  769. sctLen := int(d[0])<<8 | int(d[1])
  770. d = d[2:]
  771. if len(d) < sctLen {
  772. return false
  773. }
  774. m.scts = append(m.scts, d[:sctLen])
  775. d = d[sctLen:]
  776. }
  777. }
  778. data = data[length:]
  779. }
  780. return true
  781. }
  782. type certificateMsg struct {
  783. raw []byte
  784. certificates [][]byte
  785. }
  786. func (m *certificateMsg) equal(i interface{}) bool {
  787. m1, ok := i.(*certificateMsg)
  788. if !ok {
  789. return false
  790. }
  791. return bytes.Equal(m.raw, m1.raw) &&
  792. eqByteSlices(m.certificates, m1.certificates)
  793. }
  794. func (m *certificateMsg) marshal() (x []byte) {
  795. if m.raw != nil {
  796. return m.raw
  797. }
  798. var i int
  799. for _, slice := range m.certificates {
  800. i += len(slice)
  801. }
  802. length := 3 + 3*len(m.certificates) + i
  803. x = make([]byte, 4+length)
  804. x[0] = typeCertificate
  805. x[1] = uint8(length >> 16)
  806. x[2] = uint8(length >> 8)
  807. x[3] = uint8(length)
  808. certificateOctets := length - 3
  809. x[4] = uint8(certificateOctets >> 16)
  810. x[5] = uint8(certificateOctets >> 8)
  811. x[6] = uint8(certificateOctets)
  812. y := x[7:]
  813. for _, slice := range m.certificates {
  814. y[0] = uint8(len(slice) >> 16)
  815. y[1] = uint8(len(slice) >> 8)
  816. y[2] = uint8(len(slice))
  817. copy(y[3:], slice)
  818. y = y[3+len(slice):]
  819. }
  820. m.raw = x
  821. return
  822. }
  823. func (m *certificateMsg) unmarshal(data []byte) bool {
  824. if len(data) < 7 {
  825. return false
  826. }
  827. m.raw = data
  828. certsLen := uint32(data[4])<<16 | uint32(data[5])<<8 | uint32(data[6])
  829. if uint32(len(data)) != certsLen+7 {
  830. return false
  831. }
  832. numCerts := 0
  833. d := data[7:]
  834. for certsLen > 0 {
  835. if len(d) < 4 {
  836. return false
  837. }
  838. certLen := uint32(d[0])<<16 | uint32(d[1])<<8 | uint32(d[2])
  839. if uint32(len(d)) < 3+certLen {
  840. return false
  841. }
  842. d = d[3+certLen:]
  843. certsLen -= 3 + certLen
  844. numCerts++
  845. }
  846. m.certificates = make([][]byte, numCerts)
  847. d = data[7:]
  848. for i := 0; i < numCerts; i++ {
  849. certLen := uint32(d[0])<<16 | uint32(d[1])<<8 | uint32(d[2])
  850. m.certificates[i] = d[3 : 3+certLen]
  851. d = d[3+certLen:]
  852. }
  853. return true
  854. }
  855. type serverKeyExchangeMsg struct {
  856. raw []byte
  857. key []byte
  858. }
  859. func (m *serverKeyExchangeMsg) equal(i interface{}) bool {
  860. m1, ok := i.(*serverKeyExchangeMsg)
  861. if !ok {
  862. return false
  863. }
  864. return bytes.Equal(m.raw, m1.raw) &&
  865. bytes.Equal(m.key, m1.key)
  866. }
  867. func (m *serverKeyExchangeMsg) marshal() []byte {
  868. if m.raw != nil {
  869. return m.raw
  870. }
  871. length := len(m.key)
  872. x := make([]byte, length+4)
  873. x[0] = typeServerKeyExchange
  874. x[1] = uint8(length >> 16)
  875. x[2] = uint8(length >> 8)
  876. x[3] = uint8(length)
  877. copy(x[4:], m.key)
  878. m.raw = x
  879. return x
  880. }
  881. func (m *serverKeyExchangeMsg) unmarshal(data []byte) bool {
  882. m.raw = data
  883. if len(data) < 4 {
  884. return false
  885. }
  886. m.key = data[4:]
  887. return true
  888. }
  889. type certificateStatusMsg struct {
  890. raw []byte
  891. statusType uint8
  892. response []byte
  893. }
  894. func (m *certificateStatusMsg) equal(i interface{}) bool {
  895. m1, ok := i.(*certificateStatusMsg)
  896. if !ok {
  897. return false
  898. }
  899. return bytes.Equal(m.raw, m1.raw) &&
  900. m.statusType == m1.statusType &&
  901. bytes.Equal(m.response, m1.response)
  902. }
  903. func (m *certificateStatusMsg) marshal() []byte {
  904. if m.raw != nil {
  905. return m.raw
  906. }
  907. var x []byte
  908. if m.statusType == statusTypeOCSP {
  909. x = make([]byte, 4+4+len(m.response))
  910. x[0] = typeCertificateStatus
  911. l := len(m.response) + 4
  912. x[1] = byte(l >> 16)
  913. x[2] = byte(l >> 8)
  914. x[3] = byte(l)
  915. x[4] = statusTypeOCSP
  916. l -= 4
  917. x[5] = byte(l >> 16)
  918. x[6] = byte(l >> 8)
  919. x[7] = byte(l)
  920. copy(x[8:], m.response)
  921. } else {
  922. x = []byte{typeCertificateStatus, 0, 0, 1, m.statusType}
  923. }
  924. m.raw = x
  925. return x
  926. }
  927. func (m *certificateStatusMsg) unmarshal(data []byte) bool {
  928. m.raw = data
  929. if len(data) < 5 {
  930. return false
  931. }
  932. m.statusType = data[4]
  933. m.response = nil
  934. if m.statusType == statusTypeOCSP {
  935. if len(data) < 8 {
  936. return false
  937. }
  938. respLen := uint32(data[5])<<16 | uint32(data[6])<<8 | uint32(data[7])
  939. if uint32(len(data)) != 4+4+respLen {
  940. return false
  941. }
  942. m.response = data[8:]
  943. }
  944. return true
  945. }
  946. type serverHelloDoneMsg struct{}
  947. func (m *serverHelloDoneMsg) equal(i interface{}) bool {
  948. _, ok := i.(*serverHelloDoneMsg)
  949. return ok
  950. }
  951. func (m *serverHelloDoneMsg) marshal() []byte {
  952. x := make([]byte, 4)
  953. x[0] = typeServerHelloDone
  954. return x
  955. }
  956. func (m *serverHelloDoneMsg) unmarshal(data []byte) bool {
  957. return len(data) == 4
  958. }
  959. type clientKeyExchangeMsg struct {
  960. raw []byte
  961. ciphertext []byte
  962. }
  963. func (m *clientKeyExchangeMsg) equal(i interface{}) bool {
  964. m1, ok := i.(*clientKeyExchangeMsg)
  965. if !ok {
  966. return false
  967. }
  968. return bytes.Equal(m.raw, m1.raw) &&
  969. bytes.Equal(m.ciphertext, m1.ciphertext)
  970. }
  971. func (m *clientKeyExchangeMsg) marshal() []byte {
  972. if m.raw != nil {
  973. return m.raw
  974. }
  975. length := len(m.ciphertext)
  976. x := make([]byte, length+4)
  977. x[0] = typeClientKeyExchange
  978. x[1] = uint8(length >> 16)
  979. x[2] = uint8(length >> 8)
  980. x[3] = uint8(length)
  981. copy(x[4:], m.ciphertext)
  982. m.raw = x
  983. return x
  984. }
  985. func (m *clientKeyExchangeMsg) unmarshal(data []byte) bool {
  986. m.raw = data
  987. if len(data) < 4 {
  988. return false
  989. }
  990. l := int(data[1])<<16 | int(data[2])<<8 | int(data[3])
  991. if l != len(data)-4 {
  992. return false
  993. }
  994. m.ciphertext = data[4:]
  995. return true
  996. }
  997. type finishedMsg struct {
  998. raw []byte
  999. verifyData []byte
  1000. }
  1001. func (m *finishedMsg) equal(i interface{}) bool {
  1002. m1, ok := i.(*finishedMsg)
  1003. if !ok {
  1004. return false
  1005. }
  1006. return bytes.Equal(m.raw, m1.raw) &&
  1007. bytes.Equal(m.verifyData, m1.verifyData)
  1008. }
  1009. func (m *finishedMsg) marshal() (x []byte) {
  1010. if m.raw != nil {
  1011. return m.raw
  1012. }
  1013. x = make([]byte, 4+len(m.verifyData))
  1014. x[0] = typeFinished
  1015. x[3] = byte(len(m.verifyData))
  1016. copy(x[4:], m.verifyData)
  1017. m.raw = x
  1018. return
  1019. }
  1020. func (m *finishedMsg) unmarshal(data []byte) bool {
  1021. m.raw = data
  1022. if len(data) < 4 {
  1023. return false
  1024. }
  1025. m.verifyData = data[4:]
  1026. return true
  1027. }
  1028. type nextProtoMsg struct {
  1029. raw []byte
  1030. proto string
  1031. }
  1032. func (m *nextProtoMsg) equal(i interface{}) bool {
  1033. m1, ok := i.(*nextProtoMsg)
  1034. if !ok {
  1035. return false
  1036. }
  1037. return bytes.Equal(m.raw, m1.raw) &&
  1038. m.proto == m1.proto
  1039. }
  1040. func (m *nextProtoMsg) marshal() []byte {
  1041. if m.raw != nil {
  1042. return m.raw
  1043. }
  1044. l := len(m.proto)
  1045. if l > 255 {
  1046. l = 255
  1047. }
  1048. padding := 32 - (l+2)%32
  1049. length := l + padding + 2
  1050. x := make([]byte, length+4)
  1051. x[0] = typeNextProtocol
  1052. x[1] = uint8(length >> 16)
  1053. x[2] = uint8(length >> 8)
  1054. x[3] = uint8(length)
  1055. y := x[4:]
  1056. y[0] = byte(l)
  1057. copy(y[1:], []byte(m.proto[0:l]))
  1058. y = y[1+l:]
  1059. y[0] = byte(padding)
  1060. m.raw = x
  1061. return x
  1062. }
  1063. func (m *nextProtoMsg) unmarshal(data []byte) bool {
  1064. m.raw = data
  1065. if len(data) < 5 {
  1066. return false
  1067. }
  1068. data = data[4:]
  1069. protoLen := int(data[0])
  1070. data = data[1:]
  1071. if len(data) < protoLen {
  1072. return false
  1073. }
  1074. m.proto = string(data[0:protoLen])
  1075. data = data[protoLen:]
  1076. if len(data) < 1 {
  1077. return false
  1078. }
  1079. paddingLen := int(data[0])
  1080. data = data[1:]
  1081. if len(data) != paddingLen {
  1082. return false
  1083. }
  1084. return true
  1085. }
  1086. type certificateRequestMsg struct {
  1087. raw []byte
  1088. // hasSignatureAndHash indicates whether this message includes a list
  1089. // of signature and hash functions. This change was introduced with TLS
  1090. // 1.2.
  1091. hasSignatureAndHash bool
  1092. certificateTypes []byte
  1093. signatureAndHashes []signatureAndHash
  1094. certificateAuthorities [][]byte
  1095. }
  1096. func (m *certificateRequestMsg) equal(i interface{}) bool {
  1097. m1, ok := i.(*certificateRequestMsg)
  1098. if !ok {
  1099. return false
  1100. }
  1101. return bytes.Equal(m.raw, m1.raw) &&
  1102. bytes.Equal(m.certificateTypes, m1.certificateTypes) &&
  1103. eqByteSlices(m.certificateAuthorities, m1.certificateAuthorities) &&
  1104. eqSignatureAndHashes(m.signatureAndHashes, m1.signatureAndHashes)
  1105. }
  1106. func (m *certificateRequestMsg) marshal() (x []byte) {
  1107. if m.raw != nil {
  1108. return m.raw
  1109. }
  1110. // See http://tools.ietf.org/html/rfc4346#section-7.4.4
  1111. length := 1 + len(m.certificateTypes) + 2
  1112. casLength := 0
  1113. for _, ca := range m.certificateAuthorities {
  1114. casLength += 2 + len(ca)
  1115. }
  1116. length += casLength
  1117. if m.hasSignatureAndHash {
  1118. length += 2 + 2*len(m.signatureAndHashes)
  1119. }
  1120. x = make([]byte, 4+length)
  1121. x[0] = typeCertificateRequest
  1122. x[1] = uint8(length >> 16)
  1123. x[2] = uint8(length >> 8)
  1124. x[3] = uint8(length)
  1125. x[4] = uint8(len(m.certificateTypes))
  1126. copy(x[5:], m.certificateTypes)
  1127. y := x[5+len(m.certificateTypes):]
  1128. if m.hasSignatureAndHash {
  1129. n := len(m.signatureAndHashes) * 2
  1130. y[0] = uint8(n >> 8)
  1131. y[1] = uint8(n)
  1132. y = y[2:]
  1133. for _, sigAndHash := range m.signatureAndHashes {
  1134. y[0] = sigAndHash.hash
  1135. y[1] = sigAndHash.signature
  1136. y = y[2:]
  1137. }
  1138. }
  1139. y[0] = uint8(casLength >> 8)
  1140. y[1] = uint8(casLength)
  1141. y = y[2:]
  1142. for _, ca := range m.certificateAuthorities {
  1143. y[0] = uint8(len(ca) >> 8)
  1144. y[1] = uint8(len(ca))
  1145. y = y[2:]
  1146. copy(y, ca)
  1147. y = y[len(ca):]
  1148. }
  1149. m.raw = x
  1150. return
  1151. }
  1152. func (m *certificateRequestMsg) unmarshal(data []byte) bool {
  1153. m.raw = data
  1154. if len(data) < 5 {
  1155. return false
  1156. }
  1157. length := uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
  1158. if uint32(len(data))-4 != length {
  1159. return false
  1160. }
  1161. numCertTypes := int(data[4])
  1162. data = data[5:]
  1163. if numCertTypes == 0 || len(data) <= numCertTypes {
  1164. return false
  1165. }
  1166. m.certificateTypes = make([]byte, numCertTypes)
  1167. if copy(m.certificateTypes, data) != numCertTypes {
  1168. return false
  1169. }
  1170. data = data[numCertTypes:]
  1171. if m.hasSignatureAndHash {
  1172. if len(data) < 2 {
  1173. return false
  1174. }
  1175. sigAndHashLen := uint16(data[0])<<8 | uint16(data[1])
  1176. data = data[2:]
  1177. if sigAndHashLen&1 != 0 {
  1178. return false
  1179. }
  1180. if len(data) < int(sigAndHashLen) {
  1181. return false
  1182. }
  1183. numSigAndHash := sigAndHashLen / 2
  1184. m.signatureAndHashes = make([]signatureAndHash, numSigAndHash)
  1185. for i := range m.signatureAndHashes {
  1186. m.signatureAndHashes[i].hash = data[0]
  1187. m.signatureAndHashes[i].signature = data[1]
  1188. data = data[2:]
  1189. }
  1190. }
  1191. if len(data) < 2 {
  1192. return false
  1193. }
  1194. casLength := uint16(data[0])<<8 | uint16(data[1])
  1195. data = data[2:]
  1196. if len(data) < int(casLength) {
  1197. return false
  1198. }
  1199. cas := make([]byte, casLength)
  1200. copy(cas, data)
  1201. data = data[casLength:]
  1202. m.certificateAuthorities = nil
  1203. for len(cas) > 0 {
  1204. if len(cas) < 2 {
  1205. return false
  1206. }
  1207. caLen := uint16(cas[0])<<8 | uint16(cas[1])
  1208. cas = cas[2:]
  1209. if len(cas) < int(caLen) {
  1210. return false
  1211. }
  1212. m.certificateAuthorities = append(m.certificateAuthorities, cas[:caLen])
  1213. cas = cas[caLen:]
  1214. }
  1215. return len(data) == 0
  1216. }
  1217. type certificateVerifyMsg struct {
  1218. raw []byte
  1219. hasSignatureAndHash bool
  1220. signatureAndHash signatureAndHash
  1221. signature []byte
  1222. }
  1223. func (m *certificateVerifyMsg) equal(i interface{}) bool {
  1224. m1, ok := i.(*certificateVerifyMsg)
  1225. if !ok {
  1226. return false
  1227. }
  1228. return bytes.Equal(m.raw, m1.raw) &&
  1229. m.hasSignatureAndHash == m1.hasSignatureAndHash &&
  1230. m.signatureAndHash.hash == m1.signatureAndHash.hash &&
  1231. m.signatureAndHash.signature == m1.signatureAndHash.signature &&
  1232. bytes.Equal(m.signature, m1.signature)
  1233. }
  1234. func (m *certificateVerifyMsg) marshal() (x []byte) {
  1235. if m.raw != nil {
  1236. return m.raw
  1237. }
  1238. // See http://tools.ietf.org/html/rfc4346#section-7.4.8
  1239. siglength := len(m.signature)
  1240. length := 2 + siglength
  1241. if m.hasSignatureAndHash {
  1242. length += 2
  1243. }
  1244. x = make([]byte, 4+length)
  1245. x[0] = typeCertificateVerify
  1246. x[1] = uint8(length >> 16)
  1247. x[2] = uint8(length >> 8)
  1248. x[3] = uint8(length)
  1249. y := x[4:]
  1250. if m.hasSignatureAndHash {
  1251. y[0] = m.signatureAndHash.hash
  1252. y[1] = m.signatureAndHash.signature
  1253. y = y[2:]
  1254. }
  1255. y[0] = uint8(siglength >> 8)
  1256. y[1] = uint8(siglength)
  1257. copy(y[2:], m.signature)
  1258. m.raw = x
  1259. return
  1260. }
  1261. func (m *certificateVerifyMsg) unmarshal(data []byte) bool {
  1262. m.raw = data
  1263. if len(data) < 6 {
  1264. return false
  1265. }
  1266. length := uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
  1267. if uint32(len(data))-4 != length {
  1268. return false
  1269. }
  1270. data = data[4:]
  1271. if m.hasSignatureAndHash {
  1272. m.signatureAndHash.hash = data[0]
  1273. m.signatureAndHash.signature = data[1]
  1274. data = data[2:]
  1275. }
  1276. if len(data) < 2 {
  1277. return false
  1278. }
  1279. siglength := int(data[0])<<8 + int(data[1])
  1280. data = data[2:]
  1281. if len(data) != siglength {
  1282. return false
  1283. }
  1284. m.signature = data
  1285. return true
  1286. }
  1287. type newSessionTicketMsg struct {
  1288. raw []byte
  1289. ticket []byte
  1290. }
  1291. func (m *newSessionTicketMsg) equal(i interface{}) bool {
  1292. m1, ok := i.(*newSessionTicketMsg)
  1293. if !ok {
  1294. return false
  1295. }
  1296. return bytes.Equal(m.raw, m1.raw) &&
  1297. bytes.Equal(m.ticket, m1.ticket)
  1298. }
  1299. func (m *newSessionTicketMsg) marshal() (x []byte) {
  1300. if m.raw != nil {
  1301. return m.raw
  1302. }
  1303. // See http://tools.ietf.org/html/rfc5077#section-3.3
  1304. ticketLen := len(m.ticket)
  1305. length := 2 + 4 + ticketLen
  1306. x = make([]byte, 4+length)
  1307. x[0] = typeNewSessionTicket
  1308. x[1] = uint8(length >> 16)
  1309. x[2] = uint8(length >> 8)
  1310. x[3] = uint8(length)
  1311. x[8] = uint8(ticketLen >> 8)
  1312. x[9] = uint8(ticketLen)
  1313. copy(x[10:], m.ticket)
  1314. m.raw = x
  1315. return
  1316. }
  1317. func (m *newSessionTicketMsg) unmarshal(data []byte) bool {
  1318. m.raw = data
  1319. if len(data) < 10 {
  1320. return false
  1321. }
  1322. length := uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
  1323. if uint32(len(data))-4 != length {
  1324. return false
  1325. }
  1326. ticketLen := int(data[8])<<8 + int(data[9])
  1327. if len(data)-10 != ticketLen {
  1328. return false
  1329. }
  1330. m.ticket = data[10:]
  1331. return true
  1332. }
  1333. type helloRequestMsg struct {
  1334. }
  1335. func (*helloRequestMsg) marshal() []byte {
  1336. return []byte{typeHelloRequest, 0, 0, 0}
  1337. }
  1338. func (*helloRequestMsg) unmarshal(data []byte) bool {
  1339. return len(data) == 4
  1340. }
  1341. func eqUint16s(x, y []uint16) bool {
  1342. if len(x) != len(y) {
  1343. return false
  1344. }
  1345. for i, v := range x {
  1346. if y[i] != v {
  1347. return false
  1348. }
  1349. }
  1350. return true
  1351. }
  1352. func eqCurveIDs(x, y []CurveID) bool {
  1353. if len(x) != len(y) {
  1354. return false
  1355. }
  1356. for i, v := range x {
  1357. if y[i] != v {
  1358. return false
  1359. }
  1360. }
  1361. return true
  1362. }
  1363. func eqStrings(x, y []string) bool {
  1364. if len(x) != len(y) {
  1365. return false
  1366. }
  1367. for i, v := range x {
  1368. if y[i] != v {
  1369. return false
  1370. }
  1371. }
  1372. return true
  1373. }
  1374. func eqByteSlices(x, y [][]byte) bool {
  1375. if len(x) != len(y) {
  1376. return false
  1377. }
  1378. for i, v := range x {
  1379. if !bytes.Equal(v, y[i]) {
  1380. return false
  1381. }
  1382. }
  1383. return true
  1384. }
  1385. func eqSignatureAndHashes(x, y []signatureAndHash) bool {
  1386. if len(x) != len(y) {
  1387. return false
  1388. }
  1389. for i, v := range x {
  1390. v2 := y[i]
  1391. if v.hash != v2.hash || v.signature != v2.signature {
  1392. return false
  1393. }
  1394. }
  1395. return true
  1396. }