No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

1560 líneas
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 || l == 0 {
  759. return false
  760. }
  761. m.scts = make([][]byte, 0, 3)
  762. for len(d) != 0 {
  763. if len(d) < 2 {
  764. return false
  765. }
  766. sctLen := int(d[0])<<8 | int(d[1])
  767. d = d[2:]
  768. if sctLen == 0 || len(d) < sctLen {
  769. return false
  770. }
  771. m.scts = append(m.scts, d[:sctLen])
  772. d = d[sctLen:]
  773. }
  774. }
  775. data = data[length:]
  776. }
  777. return true
  778. }
  779. type certificateMsg struct {
  780. raw []byte
  781. certificates [][]byte
  782. }
  783. func (m *certificateMsg) equal(i interface{}) bool {
  784. m1, ok := i.(*certificateMsg)
  785. if !ok {
  786. return false
  787. }
  788. return bytes.Equal(m.raw, m1.raw) &&
  789. eqByteSlices(m.certificates, m1.certificates)
  790. }
  791. func (m *certificateMsg) marshal() (x []byte) {
  792. if m.raw != nil {
  793. return m.raw
  794. }
  795. var i int
  796. for _, slice := range m.certificates {
  797. i += len(slice)
  798. }
  799. length := 3 + 3*len(m.certificates) + i
  800. x = make([]byte, 4+length)
  801. x[0] = typeCertificate
  802. x[1] = uint8(length >> 16)
  803. x[2] = uint8(length >> 8)
  804. x[3] = uint8(length)
  805. certificateOctets := length - 3
  806. x[4] = uint8(certificateOctets >> 16)
  807. x[5] = uint8(certificateOctets >> 8)
  808. x[6] = uint8(certificateOctets)
  809. y := x[7:]
  810. for _, slice := range m.certificates {
  811. y[0] = uint8(len(slice) >> 16)
  812. y[1] = uint8(len(slice) >> 8)
  813. y[2] = uint8(len(slice))
  814. copy(y[3:], slice)
  815. y = y[3+len(slice):]
  816. }
  817. m.raw = x
  818. return
  819. }
  820. func (m *certificateMsg) unmarshal(data []byte) bool {
  821. if len(data) < 7 {
  822. return false
  823. }
  824. m.raw = data
  825. certsLen := uint32(data[4])<<16 | uint32(data[5])<<8 | uint32(data[6])
  826. if uint32(len(data)) != certsLen+7 {
  827. return false
  828. }
  829. numCerts := 0
  830. d := data[7:]
  831. for certsLen > 0 {
  832. if len(d) < 4 {
  833. return false
  834. }
  835. certLen := uint32(d[0])<<16 | uint32(d[1])<<8 | uint32(d[2])
  836. if uint32(len(d)) < 3+certLen {
  837. return false
  838. }
  839. d = d[3+certLen:]
  840. certsLen -= 3 + certLen
  841. numCerts++
  842. }
  843. m.certificates = make([][]byte, numCerts)
  844. d = data[7:]
  845. for i := 0; i < numCerts; i++ {
  846. certLen := uint32(d[0])<<16 | uint32(d[1])<<8 | uint32(d[2])
  847. m.certificates[i] = d[3 : 3+certLen]
  848. d = d[3+certLen:]
  849. }
  850. return true
  851. }
  852. type serverKeyExchangeMsg struct {
  853. raw []byte
  854. key []byte
  855. }
  856. func (m *serverKeyExchangeMsg) equal(i interface{}) bool {
  857. m1, ok := i.(*serverKeyExchangeMsg)
  858. if !ok {
  859. return false
  860. }
  861. return bytes.Equal(m.raw, m1.raw) &&
  862. bytes.Equal(m.key, m1.key)
  863. }
  864. func (m *serverKeyExchangeMsg) marshal() []byte {
  865. if m.raw != nil {
  866. return m.raw
  867. }
  868. length := len(m.key)
  869. x := make([]byte, length+4)
  870. x[0] = typeServerKeyExchange
  871. x[1] = uint8(length >> 16)
  872. x[2] = uint8(length >> 8)
  873. x[3] = uint8(length)
  874. copy(x[4:], m.key)
  875. m.raw = x
  876. return x
  877. }
  878. func (m *serverKeyExchangeMsg) unmarshal(data []byte) bool {
  879. m.raw = data
  880. if len(data) < 4 {
  881. return false
  882. }
  883. m.key = data[4:]
  884. return true
  885. }
  886. type certificateStatusMsg struct {
  887. raw []byte
  888. statusType uint8
  889. response []byte
  890. }
  891. func (m *certificateStatusMsg) equal(i interface{}) bool {
  892. m1, ok := i.(*certificateStatusMsg)
  893. if !ok {
  894. return false
  895. }
  896. return bytes.Equal(m.raw, m1.raw) &&
  897. m.statusType == m1.statusType &&
  898. bytes.Equal(m.response, m1.response)
  899. }
  900. func (m *certificateStatusMsg) marshal() []byte {
  901. if m.raw != nil {
  902. return m.raw
  903. }
  904. var x []byte
  905. if m.statusType == statusTypeOCSP {
  906. x = make([]byte, 4+4+len(m.response))
  907. x[0] = typeCertificateStatus
  908. l := len(m.response) + 4
  909. x[1] = byte(l >> 16)
  910. x[2] = byte(l >> 8)
  911. x[3] = byte(l)
  912. x[4] = statusTypeOCSP
  913. l -= 4
  914. x[5] = byte(l >> 16)
  915. x[6] = byte(l >> 8)
  916. x[7] = byte(l)
  917. copy(x[8:], m.response)
  918. } else {
  919. x = []byte{typeCertificateStatus, 0, 0, 1, m.statusType}
  920. }
  921. m.raw = x
  922. return x
  923. }
  924. func (m *certificateStatusMsg) unmarshal(data []byte) bool {
  925. m.raw = data
  926. if len(data) < 5 {
  927. return false
  928. }
  929. m.statusType = data[4]
  930. m.response = nil
  931. if m.statusType == statusTypeOCSP {
  932. if len(data) < 8 {
  933. return false
  934. }
  935. respLen := uint32(data[5])<<16 | uint32(data[6])<<8 | uint32(data[7])
  936. if uint32(len(data)) != 4+4+respLen {
  937. return false
  938. }
  939. m.response = data[8:]
  940. }
  941. return true
  942. }
  943. type serverHelloDoneMsg struct{}
  944. func (m *serverHelloDoneMsg) equal(i interface{}) bool {
  945. _, ok := i.(*serverHelloDoneMsg)
  946. return ok
  947. }
  948. func (m *serverHelloDoneMsg) marshal() []byte {
  949. x := make([]byte, 4)
  950. x[0] = typeServerHelloDone
  951. return x
  952. }
  953. func (m *serverHelloDoneMsg) unmarshal(data []byte) bool {
  954. return len(data) == 4
  955. }
  956. type clientKeyExchangeMsg struct {
  957. raw []byte
  958. ciphertext []byte
  959. }
  960. func (m *clientKeyExchangeMsg) equal(i interface{}) bool {
  961. m1, ok := i.(*clientKeyExchangeMsg)
  962. if !ok {
  963. return false
  964. }
  965. return bytes.Equal(m.raw, m1.raw) &&
  966. bytes.Equal(m.ciphertext, m1.ciphertext)
  967. }
  968. func (m *clientKeyExchangeMsg) marshal() []byte {
  969. if m.raw != nil {
  970. return m.raw
  971. }
  972. length := len(m.ciphertext)
  973. x := make([]byte, length+4)
  974. x[0] = typeClientKeyExchange
  975. x[1] = uint8(length >> 16)
  976. x[2] = uint8(length >> 8)
  977. x[3] = uint8(length)
  978. copy(x[4:], m.ciphertext)
  979. m.raw = x
  980. return x
  981. }
  982. func (m *clientKeyExchangeMsg) unmarshal(data []byte) bool {
  983. m.raw = data
  984. if len(data) < 4 {
  985. return false
  986. }
  987. l := int(data[1])<<16 | int(data[2])<<8 | int(data[3])
  988. if l != len(data)-4 {
  989. return false
  990. }
  991. m.ciphertext = data[4:]
  992. return true
  993. }
  994. type finishedMsg struct {
  995. raw []byte
  996. verifyData []byte
  997. }
  998. func (m *finishedMsg) equal(i interface{}) bool {
  999. m1, ok := i.(*finishedMsg)
  1000. if !ok {
  1001. return false
  1002. }
  1003. return bytes.Equal(m.raw, m1.raw) &&
  1004. bytes.Equal(m.verifyData, m1.verifyData)
  1005. }
  1006. func (m *finishedMsg) marshal() (x []byte) {
  1007. if m.raw != nil {
  1008. return m.raw
  1009. }
  1010. x = make([]byte, 4+len(m.verifyData))
  1011. x[0] = typeFinished
  1012. x[3] = byte(len(m.verifyData))
  1013. copy(x[4:], m.verifyData)
  1014. m.raw = x
  1015. return
  1016. }
  1017. func (m *finishedMsg) unmarshal(data []byte) bool {
  1018. m.raw = data
  1019. if len(data) < 4 {
  1020. return false
  1021. }
  1022. m.verifyData = data[4:]
  1023. return true
  1024. }
  1025. type nextProtoMsg struct {
  1026. raw []byte
  1027. proto string
  1028. }
  1029. func (m *nextProtoMsg) equal(i interface{}) bool {
  1030. m1, ok := i.(*nextProtoMsg)
  1031. if !ok {
  1032. return false
  1033. }
  1034. return bytes.Equal(m.raw, m1.raw) &&
  1035. m.proto == m1.proto
  1036. }
  1037. func (m *nextProtoMsg) marshal() []byte {
  1038. if m.raw != nil {
  1039. return m.raw
  1040. }
  1041. l := len(m.proto)
  1042. if l > 255 {
  1043. l = 255
  1044. }
  1045. padding := 32 - (l+2)%32
  1046. length := l + padding + 2
  1047. x := make([]byte, length+4)
  1048. x[0] = typeNextProtocol
  1049. x[1] = uint8(length >> 16)
  1050. x[2] = uint8(length >> 8)
  1051. x[3] = uint8(length)
  1052. y := x[4:]
  1053. y[0] = byte(l)
  1054. copy(y[1:], []byte(m.proto[0:l]))
  1055. y = y[1+l:]
  1056. y[0] = byte(padding)
  1057. m.raw = x
  1058. return x
  1059. }
  1060. func (m *nextProtoMsg) unmarshal(data []byte) bool {
  1061. m.raw = data
  1062. if len(data) < 5 {
  1063. return false
  1064. }
  1065. data = data[4:]
  1066. protoLen := int(data[0])
  1067. data = data[1:]
  1068. if len(data) < protoLen {
  1069. return false
  1070. }
  1071. m.proto = string(data[0:protoLen])
  1072. data = data[protoLen:]
  1073. if len(data) < 1 {
  1074. return false
  1075. }
  1076. paddingLen := int(data[0])
  1077. data = data[1:]
  1078. if len(data) != paddingLen {
  1079. return false
  1080. }
  1081. return true
  1082. }
  1083. type certificateRequestMsg struct {
  1084. raw []byte
  1085. // hasSignatureAndHash indicates whether this message includes a list
  1086. // of signature and hash functions. This change was introduced with TLS
  1087. // 1.2.
  1088. hasSignatureAndHash bool
  1089. certificateTypes []byte
  1090. signatureAndHashes []signatureAndHash
  1091. certificateAuthorities [][]byte
  1092. }
  1093. func (m *certificateRequestMsg) equal(i interface{}) bool {
  1094. m1, ok := i.(*certificateRequestMsg)
  1095. if !ok {
  1096. return false
  1097. }
  1098. return bytes.Equal(m.raw, m1.raw) &&
  1099. bytes.Equal(m.certificateTypes, m1.certificateTypes) &&
  1100. eqByteSlices(m.certificateAuthorities, m1.certificateAuthorities) &&
  1101. eqSignatureAndHashes(m.signatureAndHashes, m1.signatureAndHashes)
  1102. }
  1103. func (m *certificateRequestMsg) marshal() (x []byte) {
  1104. if m.raw != nil {
  1105. return m.raw
  1106. }
  1107. // See http://tools.ietf.org/html/rfc4346#section-7.4.4
  1108. length := 1 + len(m.certificateTypes) + 2
  1109. casLength := 0
  1110. for _, ca := range m.certificateAuthorities {
  1111. casLength += 2 + len(ca)
  1112. }
  1113. length += casLength
  1114. if m.hasSignatureAndHash {
  1115. length += 2 + 2*len(m.signatureAndHashes)
  1116. }
  1117. x = make([]byte, 4+length)
  1118. x[0] = typeCertificateRequest
  1119. x[1] = uint8(length >> 16)
  1120. x[2] = uint8(length >> 8)
  1121. x[3] = uint8(length)
  1122. x[4] = uint8(len(m.certificateTypes))
  1123. copy(x[5:], m.certificateTypes)
  1124. y := x[5+len(m.certificateTypes):]
  1125. if m.hasSignatureAndHash {
  1126. n := len(m.signatureAndHashes) * 2
  1127. y[0] = uint8(n >> 8)
  1128. y[1] = uint8(n)
  1129. y = y[2:]
  1130. for _, sigAndHash := range m.signatureAndHashes {
  1131. y[0] = sigAndHash.hash
  1132. y[1] = sigAndHash.signature
  1133. y = y[2:]
  1134. }
  1135. }
  1136. y[0] = uint8(casLength >> 8)
  1137. y[1] = uint8(casLength)
  1138. y = y[2:]
  1139. for _, ca := range m.certificateAuthorities {
  1140. y[0] = uint8(len(ca) >> 8)
  1141. y[1] = uint8(len(ca))
  1142. y = y[2:]
  1143. copy(y, ca)
  1144. y = y[len(ca):]
  1145. }
  1146. m.raw = x
  1147. return
  1148. }
  1149. func (m *certificateRequestMsg) unmarshal(data []byte) bool {
  1150. m.raw = data
  1151. if len(data) < 5 {
  1152. return false
  1153. }
  1154. length := uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
  1155. if uint32(len(data))-4 != length {
  1156. return false
  1157. }
  1158. numCertTypes := int(data[4])
  1159. data = data[5:]
  1160. if numCertTypes == 0 || len(data) <= numCertTypes {
  1161. return false
  1162. }
  1163. m.certificateTypes = make([]byte, numCertTypes)
  1164. if copy(m.certificateTypes, data) != numCertTypes {
  1165. return false
  1166. }
  1167. data = data[numCertTypes:]
  1168. if m.hasSignatureAndHash {
  1169. if len(data) < 2 {
  1170. return false
  1171. }
  1172. sigAndHashLen := uint16(data[0])<<8 | uint16(data[1])
  1173. data = data[2:]
  1174. if sigAndHashLen&1 != 0 {
  1175. return false
  1176. }
  1177. if len(data) < int(sigAndHashLen) {
  1178. return false
  1179. }
  1180. numSigAndHash := sigAndHashLen / 2
  1181. m.signatureAndHashes = make([]signatureAndHash, numSigAndHash)
  1182. for i := range m.signatureAndHashes {
  1183. m.signatureAndHashes[i].hash = data[0]
  1184. m.signatureAndHashes[i].signature = data[1]
  1185. data = data[2:]
  1186. }
  1187. }
  1188. if len(data) < 2 {
  1189. return false
  1190. }
  1191. casLength := uint16(data[0])<<8 | uint16(data[1])
  1192. data = data[2:]
  1193. if len(data) < int(casLength) {
  1194. return false
  1195. }
  1196. cas := make([]byte, casLength)
  1197. copy(cas, data)
  1198. data = data[casLength:]
  1199. m.certificateAuthorities = nil
  1200. for len(cas) > 0 {
  1201. if len(cas) < 2 {
  1202. return false
  1203. }
  1204. caLen := uint16(cas[0])<<8 | uint16(cas[1])
  1205. cas = cas[2:]
  1206. if len(cas) < int(caLen) {
  1207. return false
  1208. }
  1209. m.certificateAuthorities = append(m.certificateAuthorities, cas[:caLen])
  1210. cas = cas[caLen:]
  1211. }
  1212. return len(data) == 0
  1213. }
  1214. type certificateVerifyMsg struct {
  1215. raw []byte
  1216. hasSignatureAndHash bool
  1217. signatureAndHash signatureAndHash
  1218. signature []byte
  1219. }
  1220. func (m *certificateVerifyMsg) equal(i interface{}) bool {
  1221. m1, ok := i.(*certificateVerifyMsg)
  1222. if !ok {
  1223. return false
  1224. }
  1225. return bytes.Equal(m.raw, m1.raw) &&
  1226. m.hasSignatureAndHash == m1.hasSignatureAndHash &&
  1227. m.signatureAndHash.hash == m1.signatureAndHash.hash &&
  1228. m.signatureAndHash.signature == m1.signatureAndHash.signature &&
  1229. bytes.Equal(m.signature, m1.signature)
  1230. }
  1231. func (m *certificateVerifyMsg) marshal() (x []byte) {
  1232. if m.raw != nil {
  1233. return m.raw
  1234. }
  1235. // See http://tools.ietf.org/html/rfc4346#section-7.4.8
  1236. siglength := len(m.signature)
  1237. length := 2 + siglength
  1238. if m.hasSignatureAndHash {
  1239. length += 2
  1240. }
  1241. x = make([]byte, 4+length)
  1242. x[0] = typeCertificateVerify
  1243. x[1] = uint8(length >> 16)
  1244. x[2] = uint8(length >> 8)
  1245. x[3] = uint8(length)
  1246. y := x[4:]
  1247. if m.hasSignatureAndHash {
  1248. y[0] = m.signatureAndHash.hash
  1249. y[1] = m.signatureAndHash.signature
  1250. y = y[2:]
  1251. }
  1252. y[0] = uint8(siglength >> 8)
  1253. y[1] = uint8(siglength)
  1254. copy(y[2:], m.signature)
  1255. m.raw = x
  1256. return
  1257. }
  1258. func (m *certificateVerifyMsg) unmarshal(data []byte) bool {
  1259. m.raw = data
  1260. if len(data) < 6 {
  1261. return false
  1262. }
  1263. length := uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
  1264. if uint32(len(data))-4 != length {
  1265. return false
  1266. }
  1267. data = data[4:]
  1268. if m.hasSignatureAndHash {
  1269. m.signatureAndHash.hash = data[0]
  1270. m.signatureAndHash.signature = data[1]
  1271. data = data[2:]
  1272. }
  1273. if len(data) < 2 {
  1274. return false
  1275. }
  1276. siglength := int(data[0])<<8 + int(data[1])
  1277. data = data[2:]
  1278. if len(data) != siglength {
  1279. return false
  1280. }
  1281. m.signature = data
  1282. return true
  1283. }
  1284. type newSessionTicketMsg struct {
  1285. raw []byte
  1286. ticket []byte
  1287. }
  1288. func (m *newSessionTicketMsg) equal(i interface{}) bool {
  1289. m1, ok := i.(*newSessionTicketMsg)
  1290. if !ok {
  1291. return false
  1292. }
  1293. return bytes.Equal(m.raw, m1.raw) &&
  1294. bytes.Equal(m.ticket, m1.ticket)
  1295. }
  1296. func (m *newSessionTicketMsg) marshal() (x []byte) {
  1297. if m.raw != nil {
  1298. return m.raw
  1299. }
  1300. // See http://tools.ietf.org/html/rfc5077#section-3.3
  1301. ticketLen := len(m.ticket)
  1302. length := 2 + 4 + ticketLen
  1303. x = make([]byte, 4+length)
  1304. x[0] = typeNewSessionTicket
  1305. x[1] = uint8(length >> 16)
  1306. x[2] = uint8(length >> 8)
  1307. x[3] = uint8(length)
  1308. x[8] = uint8(ticketLen >> 8)
  1309. x[9] = uint8(ticketLen)
  1310. copy(x[10:], m.ticket)
  1311. m.raw = x
  1312. return
  1313. }
  1314. func (m *newSessionTicketMsg) unmarshal(data []byte) bool {
  1315. m.raw = data
  1316. if len(data) < 10 {
  1317. return false
  1318. }
  1319. length := uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
  1320. if uint32(len(data))-4 != length {
  1321. return false
  1322. }
  1323. ticketLen := int(data[8])<<8 + int(data[9])
  1324. if len(data)-10 != ticketLen {
  1325. return false
  1326. }
  1327. m.ticket = data[10:]
  1328. return true
  1329. }
  1330. type helloRequestMsg struct {
  1331. }
  1332. func (*helloRequestMsg) marshal() []byte {
  1333. return []byte{typeHelloRequest, 0, 0, 0}
  1334. }
  1335. func (*helloRequestMsg) unmarshal(data []byte) bool {
  1336. return len(data) == 4
  1337. }
  1338. func eqUint16s(x, y []uint16) bool {
  1339. if len(x) != len(y) {
  1340. return false
  1341. }
  1342. for i, v := range x {
  1343. if y[i] != v {
  1344. return false
  1345. }
  1346. }
  1347. return true
  1348. }
  1349. func eqCurveIDs(x, y []CurveID) bool {
  1350. if len(x) != len(y) {
  1351. return false
  1352. }
  1353. for i, v := range x {
  1354. if y[i] != v {
  1355. return false
  1356. }
  1357. }
  1358. return true
  1359. }
  1360. func eqStrings(x, y []string) bool {
  1361. if len(x) != len(y) {
  1362. return false
  1363. }
  1364. for i, v := range x {
  1365. if y[i] != v {
  1366. return false
  1367. }
  1368. }
  1369. return true
  1370. }
  1371. func eqByteSlices(x, y [][]byte) bool {
  1372. if len(x) != len(y) {
  1373. return false
  1374. }
  1375. for i, v := range x {
  1376. if !bytes.Equal(v, y[i]) {
  1377. return false
  1378. }
  1379. }
  1380. return true
  1381. }
  1382. func eqSignatureAndHashes(x, y []signatureAndHash) bool {
  1383. if len(x) != len(y) {
  1384. return false
  1385. }
  1386. for i, v := range x {
  1387. v2 := y[i]
  1388. if v.hash != v2.hash || v.signature != v2.signature {
  1389. return false
  1390. }
  1391. }
  1392. return true
  1393. }