Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

1569 Zeilen
33 KiB

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