Alternative TLS implementation in Go
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

2274 lignes
48 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. rawTruncated []byte // for PSK binding
  12. vers uint16
  13. random []byte
  14. sessionId []byte
  15. cipherSuites []uint16
  16. compressionMethods []uint8
  17. nextProtoNeg bool
  18. serverName string
  19. ocspStapling bool
  20. scts bool
  21. supportedCurves []CurveID
  22. supportedPoints []uint8
  23. ticketSupported bool
  24. sessionTicket []uint8
  25. signatureAndHashes []signatureAndHash
  26. secureRenegotiation []byte
  27. secureRenegotiationSupported bool
  28. alpnProtocols []string
  29. keyShares []keyShare
  30. supportedVersions []uint16
  31. psks []psk
  32. pskKeyExchangeModes []uint8
  33. earlyData bool
  34. }
  35. func (m *clientHelloMsg) equal(i interface{}) bool {
  36. m1, ok := i.(*clientHelloMsg)
  37. if !ok {
  38. return false
  39. }
  40. return bytes.Equal(m.raw, m1.raw) &&
  41. m.vers == m1.vers &&
  42. bytes.Equal(m.random, m1.random) &&
  43. bytes.Equal(m.sessionId, m1.sessionId) &&
  44. eqUint16s(m.cipherSuites, m1.cipherSuites) &&
  45. bytes.Equal(m.compressionMethods, m1.compressionMethods) &&
  46. m.nextProtoNeg == m1.nextProtoNeg &&
  47. m.serverName == m1.serverName &&
  48. m.ocspStapling == m1.ocspStapling &&
  49. m.scts == m1.scts &&
  50. eqCurveIDs(m.supportedCurves, m1.supportedCurves) &&
  51. bytes.Equal(m.supportedPoints, m1.supportedPoints) &&
  52. m.ticketSupported == m1.ticketSupported &&
  53. bytes.Equal(m.sessionTicket, m1.sessionTicket) &&
  54. eqSignatureAndHashes(m.signatureAndHashes, m1.signatureAndHashes) &&
  55. m.secureRenegotiationSupported == m1.secureRenegotiationSupported &&
  56. bytes.Equal(m.secureRenegotiation, m1.secureRenegotiation) &&
  57. eqStrings(m.alpnProtocols, m1.alpnProtocols) &&
  58. eqKeyShares(m.keyShares, m1.keyShares) &&
  59. eqUint16s(m.supportedVersions, m1.supportedVersions) &&
  60. m.earlyData == m1.earlyData
  61. }
  62. func (m *clientHelloMsg) marshal() []byte {
  63. if m.raw != nil {
  64. return m.raw
  65. }
  66. length := 2 + 32 + 1 + len(m.sessionId) + 2 + len(m.cipherSuites)*2 + 1 + len(m.compressionMethods)
  67. numExtensions := 0
  68. extensionsLength := 0
  69. if m.nextProtoNeg {
  70. numExtensions++
  71. }
  72. if m.ocspStapling {
  73. extensionsLength += 1 + 2 + 2
  74. numExtensions++
  75. }
  76. if len(m.serverName) > 0 {
  77. extensionsLength += 5 + len(m.serverName)
  78. numExtensions++
  79. }
  80. if len(m.supportedCurves) > 0 {
  81. extensionsLength += 2 + 2*len(m.supportedCurves)
  82. numExtensions++
  83. }
  84. if len(m.supportedPoints) > 0 {
  85. extensionsLength += 1 + len(m.supportedPoints)
  86. numExtensions++
  87. }
  88. if m.ticketSupported {
  89. extensionsLength += len(m.sessionTicket)
  90. numExtensions++
  91. }
  92. if len(m.signatureAndHashes) > 0 {
  93. extensionsLength += 2 + 2*len(m.signatureAndHashes)
  94. numExtensions++
  95. }
  96. if m.secureRenegotiationSupported {
  97. extensionsLength += 1 + len(m.secureRenegotiation)
  98. numExtensions++
  99. }
  100. if len(m.alpnProtocols) > 0 {
  101. extensionsLength += 2
  102. for _, s := range m.alpnProtocols {
  103. if l := len(s); l == 0 || l > 255 {
  104. panic("invalid ALPN protocol")
  105. }
  106. extensionsLength++
  107. extensionsLength += len(s)
  108. }
  109. numExtensions++
  110. }
  111. if m.scts {
  112. numExtensions++
  113. }
  114. if len(m.keyShares) > 0 {
  115. extensionsLength += 2
  116. for _, k := range m.keyShares {
  117. extensionsLength += 4 + len(k.data)
  118. }
  119. numExtensions++
  120. }
  121. if len(m.supportedVersions) > 0 {
  122. extensionsLength += 1 + 2*len(m.supportedVersions)
  123. numExtensions++
  124. }
  125. if m.earlyData {
  126. numExtensions++
  127. }
  128. if numExtensions > 0 {
  129. extensionsLength += 4 * numExtensions
  130. length += 2 + extensionsLength
  131. }
  132. x := make([]byte, 4+length)
  133. x[0] = typeClientHello
  134. x[1] = uint8(length >> 16)
  135. x[2] = uint8(length >> 8)
  136. x[3] = uint8(length)
  137. x[4] = uint8(m.vers >> 8)
  138. x[5] = uint8(m.vers)
  139. copy(x[6:38], m.random)
  140. x[38] = uint8(len(m.sessionId))
  141. copy(x[39:39+len(m.sessionId)], m.sessionId)
  142. y := x[39+len(m.sessionId):]
  143. y[0] = uint8(len(m.cipherSuites) >> 7)
  144. y[1] = uint8(len(m.cipherSuites) << 1)
  145. for i, suite := range m.cipherSuites {
  146. y[2+i*2] = uint8(suite >> 8)
  147. y[3+i*2] = uint8(suite)
  148. }
  149. z := y[2+len(m.cipherSuites)*2:]
  150. z[0] = uint8(len(m.compressionMethods))
  151. copy(z[1:], m.compressionMethods)
  152. z = z[1+len(m.compressionMethods):]
  153. if numExtensions > 0 {
  154. z[0] = byte(extensionsLength >> 8)
  155. z[1] = byte(extensionsLength)
  156. z = z[2:]
  157. }
  158. if m.nextProtoNeg {
  159. z[0] = byte(extensionNextProtoNeg >> 8)
  160. z[1] = byte(extensionNextProtoNeg & 0xff)
  161. // The length is always 0
  162. z = z[4:]
  163. }
  164. if len(m.serverName) > 0 {
  165. z[0] = byte(extensionServerName >> 8)
  166. z[1] = byte(extensionServerName & 0xff)
  167. l := len(m.serverName) + 5
  168. z[2] = byte(l >> 8)
  169. z[3] = byte(l)
  170. z = z[4:]
  171. // RFC 3546, section 3.1
  172. //
  173. // struct {
  174. // NameType name_type;
  175. // select (name_type) {
  176. // case host_name: HostName;
  177. // } name;
  178. // } ServerName;
  179. //
  180. // enum {
  181. // host_name(0), (255)
  182. // } NameType;
  183. //
  184. // opaque HostName<1..2^16-1>;
  185. //
  186. // struct {
  187. // ServerName server_name_list<1..2^16-1>
  188. // } ServerNameList;
  189. z[0] = byte((len(m.serverName) + 3) >> 8)
  190. z[1] = byte(len(m.serverName) + 3)
  191. z[3] = byte(len(m.serverName) >> 8)
  192. z[4] = byte(len(m.serverName))
  193. copy(z[5:], []byte(m.serverName))
  194. z = z[l:]
  195. }
  196. if m.ocspStapling {
  197. // RFC 4366, section 3.6
  198. z[0] = byte(extensionStatusRequest >> 8)
  199. z[1] = byte(extensionStatusRequest)
  200. z[2] = 0
  201. z[3] = 5
  202. z[4] = 1 // OCSP type
  203. // Two zero valued uint16s for the two lengths.
  204. z = z[9:]
  205. }
  206. if len(m.supportedCurves) > 0 {
  207. // http://tools.ietf.org/html/rfc4492#section-5.5.1
  208. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.4
  209. z[0] = byte(extensionSupportedCurves >> 8)
  210. z[1] = byte(extensionSupportedCurves)
  211. l := 2 + 2*len(m.supportedCurves)
  212. z[2] = byte(l >> 8)
  213. z[3] = byte(l)
  214. l -= 2
  215. z[4] = byte(l >> 8)
  216. z[5] = byte(l)
  217. z = z[6:]
  218. for _, curve := range m.supportedCurves {
  219. z[0] = byte(curve >> 8)
  220. z[1] = byte(curve)
  221. z = z[2:]
  222. }
  223. }
  224. if len(m.supportedPoints) > 0 {
  225. // http://tools.ietf.org/html/rfc4492#section-5.5.2
  226. z[0] = byte(extensionSupportedPoints >> 8)
  227. z[1] = byte(extensionSupportedPoints)
  228. l := 1 + len(m.supportedPoints)
  229. z[2] = byte(l >> 8)
  230. z[3] = byte(l)
  231. l--
  232. z[4] = byte(l)
  233. z = z[5:]
  234. for _, pointFormat := range m.supportedPoints {
  235. z[0] = pointFormat
  236. z = z[1:]
  237. }
  238. }
  239. if m.ticketSupported {
  240. // http://tools.ietf.org/html/rfc5077#section-3.2
  241. z[0] = byte(extensionSessionTicket >> 8)
  242. z[1] = byte(extensionSessionTicket)
  243. l := len(m.sessionTicket)
  244. z[2] = byte(l >> 8)
  245. z[3] = byte(l)
  246. z = z[4:]
  247. copy(z, m.sessionTicket)
  248. z = z[len(m.sessionTicket):]
  249. }
  250. if len(m.signatureAndHashes) > 0 {
  251. // https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
  252. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.3
  253. z[0] = byte(extensionSignatureAlgorithms >> 8)
  254. z[1] = byte(extensionSignatureAlgorithms)
  255. l := 2 + 2*len(m.signatureAndHashes)
  256. z[2] = byte(l >> 8)
  257. z[3] = byte(l)
  258. z = z[4:]
  259. l -= 2
  260. z[0] = byte(l >> 8)
  261. z[1] = byte(l)
  262. z = z[2:]
  263. for _, sigAndHash := range m.signatureAndHashes {
  264. z[0] = sigAndHash.hash
  265. z[1] = sigAndHash.signature
  266. z = z[2:]
  267. }
  268. }
  269. if m.secureRenegotiationSupported {
  270. z[0] = byte(extensionRenegotiationInfo >> 8)
  271. z[1] = byte(extensionRenegotiationInfo & 0xff)
  272. z[2] = 0
  273. z[3] = byte(len(m.secureRenegotiation) + 1)
  274. z[4] = byte(len(m.secureRenegotiation))
  275. z = z[5:]
  276. copy(z, m.secureRenegotiation)
  277. z = z[len(m.secureRenegotiation):]
  278. }
  279. if len(m.alpnProtocols) > 0 {
  280. z[0] = byte(extensionALPN >> 8)
  281. z[1] = byte(extensionALPN & 0xff)
  282. lengths := z[2:]
  283. z = z[6:]
  284. stringsLength := 0
  285. for _, s := range m.alpnProtocols {
  286. l := len(s)
  287. z[0] = byte(l)
  288. copy(z[1:], s)
  289. z = z[1+l:]
  290. stringsLength += 1 + l
  291. }
  292. lengths[2] = byte(stringsLength >> 8)
  293. lengths[3] = byte(stringsLength)
  294. stringsLength += 2
  295. lengths[0] = byte(stringsLength >> 8)
  296. lengths[1] = byte(stringsLength)
  297. }
  298. if m.scts {
  299. // https://tools.ietf.org/html/rfc6962#section-3.3.1
  300. z[0] = byte(extensionSCT >> 8)
  301. z[1] = byte(extensionSCT)
  302. // zero uint16 for the zero-length extension_data
  303. z = z[4:]
  304. }
  305. if len(m.keyShares) > 0 {
  306. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.5
  307. z[0] = byte(extensionKeyShare >> 8)
  308. z[1] = byte(extensionKeyShare)
  309. lengths := z[2:]
  310. z = z[6:]
  311. totalLength := 0
  312. for _, ks := range m.keyShares {
  313. z[0] = byte(ks.group >> 8)
  314. z[1] = byte(ks.group)
  315. z[2] = byte(len(ks.data) >> 8)
  316. z[3] = byte(len(ks.data))
  317. copy(z[4:], ks.data)
  318. z = z[4+len(ks.data):]
  319. totalLength += 4 + len(ks.data)
  320. }
  321. lengths[2] = byte(totalLength >> 8)
  322. lengths[3] = byte(totalLength)
  323. totalLength += 2
  324. lengths[0] = byte(totalLength >> 8)
  325. lengths[1] = byte(totalLength)
  326. }
  327. if len(m.supportedVersions) > 0 {
  328. z[0] = byte(extensionSupportedVersions >> 8)
  329. z[1] = byte(extensionSupportedVersions)
  330. l := 1 + 2*len(m.supportedVersions)
  331. z[2] = byte(l >> 8)
  332. z[3] = byte(l)
  333. l -= 1
  334. z[4] = byte(l)
  335. z = z[5:]
  336. for _, v := range m.supportedVersions {
  337. z[0] = byte(v >> 8)
  338. z[1] = byte(v)
  339. z = z[2:]
  340. }
  341. }
  342. if m.earlyData {
  343. z[0] = byte(extensionEarlyData >> 8)
  344. z[1] = byte(extensionEarlyData)
  345. z = z[4:]
  346. }
  347. m.raw = x
  348. return x
  349. }
  350. func (m *clientHelloMsg) unmarshal(data []byte) bool {
  351. if len(data) < 42 {
  352. return false
  353. }
  354. m.raw = data
  355. m.vers = uint16(data[4])<<8 | uint16(data[5])
  356. m.random = data[6:38]
  357. sessionIdLen := int(data[38])
  358. if sessionIdLen > 32 || len(data) < 39+sessionIdLen {
  359. return false
  360. }
  361. m.sessionId = data[39 : 39+sessionIdLen]
  362. data = data[39+sessionIdLen:]
  363. bindersOffset := 39 + sessionIdLen
  364. if len(data) < 2 {
  365. return false
  366. }
  367. // cipherSuiteLen is the number of bytes of cipher suite numbers. Since
  368. // they are uint16s, the number must be even.
  369. cipherSuiteLen := int(data[0])<<8 | int(data[1])
  370. if cipherSuiteLen%2 == 1 || len(data) < 2+cipherSuiteLen {
  371. return false
  372. }
  373. numCipherSuites := cipherSuiteLen / 2
  374. m.cipherSuites = make([]uint16, numCipherSuites)
  375. for i := 0; i < numCipherSuites; i++ {
  376. m.cipherSuites[i] = uint16(data[2+2*i])<<8 | uint16(data[3+2*i])
  377. if m.cipherSuites[i] == scsvRenegotiation {
  378. m.secureRenegotiationSupported = true
  379. }
  380. }
  381. data = data[2+cipherSuiteLen:]
  382. bindersOffset += 2 + cipherSuiteLen
  383. if len(data) < 1 {
  384. return false
  385. }
  386. compressionMethodsLen := int(data[0])
  387. if len(data) < 1+compressionMethodsLen {
  388. return false
  389. }
  390. m.compressionMethods = data[1 : 1+compressionMethodsLen]
  391. data = data[1+compressionMethodsLen:]
  392. bindersOffset += 1 + compressionMethodsLen
  393. m.nextProtoNeg = false
  394. m.serverName = ""
  395. m.ocspStapling = false
  396. m.ticketSupported = false
  397. m.sessionTicket = nil
  398. m.signatureAndHashes = nil
  399. m.alpnProtocols = nil
  400. m.scts = false
  401. m.keyShares = nil
  402. m.supportedVersions = nil
  403. m.psks = nil
  404. m.pskKeyExchangeModes = nil
  405. m.earlyData = false
  406. if len(data) == 0 {
  407. // ClientHello is optionally followed by extension data
  408. return true
  409. }
  410. if len(data) < 2 {
  411. return false
  412. }
  413. extensionsLength := int(data[0])<<8 | int(data[1])
  414. data = data[2:]
  415. bindersOffset += 2
  416. if extensionsLength != len(data) {
  417. return false
  418. }
  419. for len(data) != 0 {
  420. if len(data) < 4 {
  421. return false
  422. }
  423. extension := uint16(data[0])<<8 | uint16(data[1])
  424. length := int(data[2])<<8 | int(data[3])
  425. data = data[4:]
  426. bindersOffset += 4
  427. if len(data) < length {
  428. return false
  429. }
  430. switch extension {
  431. case extensionServerName:
  432. d := data[:length]
  433. if len(d) < 2 {
  434. return false
  435. }
  436. namesLen := int(d[0])<<8 | int(d[1])
  437. d = d[2:]
  438. if len(d) != namesLen {
  439. return false
  440. }
  441. for len(d) > 0 {
  442. if len(d) < 3 {
  443. return false
  444. }
  445. nameType := d[0]
  446. nameLen := int(d[1])<<8 | int(d[2])
  447. d = d[3:]
  448. if len(d) < nameLen {
  449. return false
  450. }
  451. if nameType == 0 {
  452. m.serverName = string(d[:nameLen])
  453. // An SNI value may not include a
  454. // trailing dot. See
  455. // https://tools.ietf.org/html/rfc6066#section-3.
  456. if strings.HasSuffix(m.serverName, ".") {
  457. return false
  458. }
  459. break
  460. }
  461. d = d[nameLen:]
  462. }
  463. case extensionNextProtoNeg:
  464. if length > 0 {
  465. return false
  466. }
  467. m.nextProtoNeg = true
  468. case extensionStatusRequest:
  469. m.ocspStapling = length > 0 && data[0] == statusTypeOCSP
  470. case extensionSupportedCurves:
  471. // http://tools.ietf.org/html/rfc4492#section-5.5.1
  472. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.4
  473. if length < 2 {
  474. return false
  475. }
  476. l := int(data[0])<<8 | int(data[1])
  477. if l%2 == 1 || length != l+2 {
  478. return false
  479. }
  480. numCurves := l / 2
  481. m.supportedCurves = make([]CurveID, numCurves)
  482. d := data[2:]
  483. for i := 0; i < numCurves; i++ {
  484. m.supportedCurves[i] = CurveID(d[0])<<8 | CurveID(d[1])
  485. d = d[2:]
  486. }
  487. case extensionSupportedPoints:
  488. // http://tools.ietf.org/html/rfc4492#section-5.5.2
  489. if length < 1 {
  490. return false
  491. }
  492. l := int(data[0])
  493. if length != l+1 {
  494. return false
  495. }
  496. m.supportedPoints = make([]uint8, l)
  497. copy(m.supportedPoints, data[1:])
  498. case extensionSessionTicket:
  499. // http://tools.ietf.org/html/rfc5077#section-3.2
  500. m.ticketSupported = true
  501. m.sessionTicket = data[:length]
  502. case extensionSignatureAlgorithms:
  503. // https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
  504. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.3
  505. if length < 2 || length&1 != 0 {
  506. return false
  507. }
  508. l := int(data[0])<<8 | int(data[1])
  509. if l != length-2 {
  510. return false
  511. }
  512. n := l / 2
  513. d := data[2:]
  514. m.signatureAndHashes = make([]signatureAndHash, n)
  515. for i := range m.signatureAndHashes {
  516. m.signatureAndHashes[i].hash = d[0]
  517. m.signatureAndHashes[i].signature = d[1]
  518. d = d[2:]
  519. }
  520. case extensionRenegotiationInfo:
  521. if length == 0 {
  522. return false
  523. }
  524. d := data[:length]
  525. l := int(d[0])
  526. d = d[1:]
  527. if l != len(d) {
  528. return false
  529. }
  530. m.secureRenegotiation = d
  531. m.secureRenegotiationSupported = true
  532. case extensionALPN:
  533. if length < 2 {
  534. return false
  535. }
  536. l := int(data[0])<<8 | int(data[1])
  537. if l != length-2 {
  538. return false
  539. }
  540. d := data[2:length]
  541. for len(d) != 0 {
  542. stringLen := int(d[0])
  543. d = d[1:]
  544. if stringLen == 0 || stringLen > len(d) {
  545. return false
  546. }
  547. m.alpnProtocols = append(m.alpnProtocols, string(d[:stringLen]))
  548. d = d[stringLen:]
  549. }
  550. case extensionSCT:
  551. m.scts = true
  552. if length != 0 {
  553. return false
  554. }
  555. case extensionKeyShare:
  556. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.5
  557. if length < 2 {
  558. return false
  559. }
  560. l := int(data[0])<<8 | int(data[1])
  561. if l != length-2 {
  562. return false
  563. }
  564. d := data[2:length]
  565. for len(d) != 0 {
  566. if len(d) < 4 {
  567. return false
  568. }
  569. dataLen := int(d[2])<<8 | int(d[3])
  570. if dataLen == 0 || 4+dataLen > len(d) {
  571. return false
  572. }
  573. m.keyShares = append(m.keyShares, keyShare{
  574. group: CurveID(d[0])<<8 | CurveID(d[1]),
  575. data: d[4 : 4+dataLen],
  576. })
  577. d = d[4+dataLen:]
  578. }
  579. case extensionSupportedVersions:
  580. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.1
  581. if length < 1 {
  582. return false
  583. }
  584. l := int(data[0])
  585. if l%2 == 1 || length != l+1 {
  586. return false
  587. }
  588. n := l / 2
  589. d := data[1:]
  590. for i := 0; i < n; i++ {
  591. v := uint16(d[0])<<8 + uint16(d[1])
  592. m.supportedVersions = append(m.supportedVersions, v)
  593. d = d[2:]
  594. }
  595. case extensionPreSharedKey:
  596. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6
  597. if length < 43 {
  598. return false
  599. }
  600. li := int(data[0])<<8 | int(data[1])
  601. if li > length-37 {
  602. return false
  603. }
  604. d := data[2 : 2+li]
  605. bindersOffset += 2 + li
  606. for len(d) > 0 {
  607. if len(d) < 6 {
  608. return false
  609. }
  610. l := int(d[0])<<8 | int(d[1])
  611. if len(d) < 2+l+4 {
  612. return false
  613. }
  614. m.psks = append(m.psks, psk{
  615. identity: d[2 : 2+l],
  616. obfTicketAge: uint32(d[l+2])<<24 | uint32(d[l+3])<<16 |
  617. uint32(d[l+4])<<8 | uint32(d[l+5]),
  618. })
  619. d = d[2+l+4:]
  620. }
  621. lb := int(data[li+2])<<8 | int(data[li+3])
  622. d = data[2+li+2:]
  623. if lb != len(d) {
  624. return false
  625. }
  626. i := 0
  627. for len(d) > 0 {
  628. if len(d) < 1 {
  629. return false
  630. }
  631. l := int(d[0])
  632. if l > len(d)-1 {
  633. return false
  634. }
  635. m.psks[i].binder = d[1 : 1+l]
  636. d = d[1+l:]
  637. i++
  638. }
  639. if i != len(m.psks) {
  640. return false
  641. }
  642. // Ensure this extension is the last one in the Client Hello
  643. if len(data) != length {
  644. return false
  645. }
  646. m.rawTruncated = m.raw[:bindersOffset]
  647. case extensionPSKKeyExchangeModes:
  648. if length < 2 {
  649. return false
  650. }
  651. l := int(data[0])
  652. if length != l+1 {
  653. return false
  654. }
  655. m.pskKeyExchangeModes = data[1:length]
  656. case extensionEarlyData:
  657. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8
  658. m.earlyData = true
  659. }
  660. data = data[length:]
  661. bindersOffset += length
  662. }
  663. return true
  664. }
  665. type serverHelloMsg struct {
  666. raw []byte
  667. vers uint16
  668. random []byte
  669. sessionId []byte
  670. cipherSuite uint16
  671. compressionMethod uint8
  672. nextProtoNeg bool
  673. nextProtos []string
  674. ocspStapling bool
  675. scts [][]byte
  676. ticketSupported bool
  677. secureRenegotiation []byte
  678. secureRenegotiationSupported bool
  679. alpnProtocol string
  680. }
  681. func (m *serverHelloMsg) equal(i interface{}) bool {
  682. m1, ok := i.(*serverHelloMsg)
  683. if !ok {
  684. return false
  685. }
  686. if len(m.scts) != len(m1.scts) {
  687. return false
  688. }
  689. for i, sct := range m.scts {
  690. if !bytes.Equal(sct, m1.scts[i]) {
  691. return false
  692. }
  693. }
  694. return bytes.Equal(m.raw, m1.raw) &&
  695. m.vers == m1.vers &&
  696. bytes.Equal(m.random, m1.random) &&
  697. bytes.Equal(m.sessionId, m1.sessionId) &&
  698. m.cipherSuite == m1.cipherSuite &&
  699. m.compressionMethod == m1.compressionMethod &&
  700. m.nextProtoNeg == m1.nextProtoNeg &&
  701. eqStrings(m.nextProtos, m1.nextProtos) &&
  702. m.ocspStapling == m1.ocspStapling &&
  703. m.ticketSupported == m1.ticketSupported &&
  704. m.secureRenegotiationSupported == m1.secureRenegotiationSupported &&
  705. bytes.Equal(m.secureRenegotiation, m1.secureRenegotiation) &&
  706. m.alpnProtocol == m1.alpnProtocol
  707. }
  708. func (m *serverHelloMsg) marshal() []byte {
  709. if m.raw != nil {
  710. return m.raw
  711. }
  712. length := 38 + len(m.sessionId)
  713. numExtensions := 0
  714. extensionsLength := 0
  715. nextProtoLen := 0
  716. if m.nextProtoNeg {
  717. numExtensions++
  718. for _, v := range m.nextProtos {
  719. nextProtoLen += len(v)
  720. }
  721. nextProtoLen += len(m.nextProtos)
  722. extensionsLength += nextProtoLen
  723. }
  724. if m.ocspStapling {
  725. numExtensions++
  726. }
  727. if m.ticketSupported {
  728. numExtensions++
  729. }
  730. if m.secureRenegotiationSupported {
  731. extensionsLength += 1 + len(m.secureRenegotiation)
  732. numExtensions++
  733. }
  734. if alpnLen := len(m.alpnProtocol); alpnLen > 0 {
  735. if alpnLen >= 256 {
  736. panic("invalid ALPN protocol")
  737. }
  738. extensionsLength += 2 + 1 + alpnLen
  739. numExtensions++
  740. }
  741. sctLen := 0
  742. if len(m.scts) > 0 {
  743. for _, sct := range m.scts {
  744. sctLen += len(sct) + 2
  745. }
  746. extensionsLength += 2 + sctLen
  747. numExtensions++
  748. }
  749. if numExtensions > 0 {
  750. extensionsLength += 4 * numExtensions
  751. length += 2 + extensionsLength
  752. }
  753. x := make([]byte, 4+length)
  754. x[0] = typeServerHello
  755. x[1] = uint8(length >> 16)
  756. x[2] = uint8(length >> 8)
  757. x[3] = uint8(length)
  758. x[4] = uint8(m.vers >> 8)
  759. x[5] = uint8(m.vers)
  760. copy(x[6:38], m.random)
  761. x[38] = uint8(len(m.sessionId))
  762. copy(x[39:39+len(m.sessionId)], m.sessionId)
  763. z := x[39+len(m.sessionId):]
  764. z[0] = uint8(m.cipherSuite >> 8)
  765. z[1] = uint8(m.cipherSuite)
  766. z[2] = m.compressionMethod
  767. z = z[3:]
  768. if numExtensions > 0 {
  769. z[0] = byte(extensionsLength >> 8)
  770. z[1] = byte(extensionsLength)
  771. z = z[2:]
  772. }
  773. if m.nextProtoNeg {
  774. z[0] = byte(extensionNextProtoNeg >> 8)
  775. z[1] = byte(extensionNextProtoNeg & 0xff)
  776. z[2] = byte(nextProtoLen >> 8)
  777. z[3] = byte(nextProtoLen)
  778. z = z[4:]
  779. for _, v := range m.nextProtos {
  780. l := len(v)
  781. if l > 255 {
  782. l = 255
  783. }
  784. z[0] = byte(l)
  785. copy(z[1:], []byte(v[0:l]))
  786. z = z[1+l:]
  787. }
  788. }
  789. if m.ocspStapling {
  790. z[0] = byte(extensionStatusRequest >> 8)
  791. z[1] = byte(extensionStatusRequest)
  792. z = z[4:]
  793. }
  794. if m.ticketSupported {
  795. z[0] = byte(extensionSessionTicket >> 8)
  796. z[1] = byte(extensionSessionTicket)
  797. z = z[4:]
  798. }
  799. if m.secureRenegotiationSupported {
  800. z[0] = byte(extensionRenegotiationInfo >> 8)
  801. z[1] = byte(extensionRenegotiationInfo & 0xff)
  802. z[2] = 0
  803. z[3] = byte(len(m.secureRenegotiation) + 1)
  804. z[4] = byte(len(m.secureRenegotiation))
  805. z = z[5:]
  806. copy(z, m.secureRenegotiation)
  807. z = z[len(m.secureRenegotiation):]
  808. }
  809. if alpnLen := len(m.alpnProtocol); alpnLen > 0 {
  810. z[0] = byte(extensionALPN >> 8)
  811. z[1] = byte(extensionALPN & 0xff)
  812. l := 2 + 1 + alpnLen
  813. z[2] = byte(l >> 8)
  814. z[3] = byte(l)
  815. l -= 2
  816. z[4] = byte(l >> 8)
  817. z[5] = byte(l)
  818. l -= 1
  819. z[6] = byte(l)
  820. copy(z[7:], []byte(m.alpnProtocol))
  821. z = z[7+alpnLen:]
  822. }
  823. if sctLen > 0 {
  824. z[0] = byte(extensionSCT >> 8)
  825. z[1] = byte(extensionSCT)
  826. l := sctLen + 2
  827. z[2] = byte(l >> 8)
  828. z[3] = byte(l)
  829. z[4] = byte(sctLen >> 8)
  830. z[5] = byte(sctLen)
  831. z = z[6:]
  832. for _, sct := range m.scts {
  833. z[0] = byte(len(sct) >> 8)
  834. z[1] = byte(len(sct))
  835. copy(z[2:], sct)
  836. z = z[len(sct)+2:]
  837. }
  838. }
  839. m.raw = x
  840. return x
  841. }
  842. func (m *serverHelloMsg) unmarshal(data []byte) bool {
  843. if len(data) < 42 {
  844. return false
  845. }
  846. m.raw = data
  847. m.vers = uint16(data[4])<<8 | uint16(data[5])
  848. m.random = data[6:38]
  849. sessionIdLen := int(data[38])
  850. if sessionIdLen > 32 || len(data) < 39+sessionIdLen {
  851. return false
  852. }
  853. m.sessionId = data[39 : 39+sessionIdLen]
  854. data = data[39+sessionIdLen:]
  855. if len(data) < 3 {
  856. return false
  857. }
  858. m.cipherSuite = uint16(data[0])<<8 | uint16(data[1])
  859. m.compressionMethod = data[2]
  860. data = data[3:]
  861. m.nextProtoNeg = false
  862. m.nextProtos = nil
  863. m.ocspStapling = false
  864. m.scts = nil
  865. m.ticketSupported = false
  866. m.alpnProtocol = ""
  867. if len(data) == 0 {
  868. // ServerHello is optionally followed by extension data
  869. return true
  870. }
  871. if len(data) < 2 {
  872. return false
  873. }
  874. extensionsLength := int(data[0])<<8 | int(data[1])
  875. data = data[2:]
  876. if len(data) != extensionsLength {
  877. return false
  878. }
  879. for len(data) != 0 {
  880. if len(data) < 4 {
  881. return false
  882. }
  883. extension := uint16(data[0])<<8 | uint16(data[1])
  884. length := int(data[2])<<8 | int(data[3])
  885. data = data[4:]
  886. if len(data) < length {
  887. return false
  888. }
  889. switch extension {
  890. case extensionNextProtoNeg:
  891. m.nextProtoNeg = true
  892. d := data[:length]
  893. for len(d) > 0 {
  894. l := int(d[0])
  895. d = d[1:]
  896. if l == 0 || l > len(d) {
  897. return false
  898. }
  899. m.nextProtos = append(m.nextProtos, string(d[:l]))
  900. d = d[l:]
  901. }
  902. case extensionStatusRequest:
  903. if length > 0 {
  904. return false
  905. }
  906. m.ocspStapling = true
  907. case extensionSessionTicket:
  908. if length > 0 {
  909. return false
  910. }
  911. m.ticketSupported = true
  912. case extensionRenegotiationInfo:
  913. if length == 0 {
  914. return false
  915. }
  916. d := data[:length]
  917. l := int(d[0])
  918. d = d[1:]
  919. if l != len(d) {
  920. return false
  921. }
  922. m.secureRenegotiation = d
  923. m.secureRenegotiationSupported = true
  924. case extensionALPN:
  925. d := data[:length]
  926. if len(d) < 3 {
  927. return false
  928. }
  929. l := int(d[0])<<8 | int(d[1])
  930. if l != len(d)-2 {
  931. return false
  932. }
  933. d = d[2:]
  934. l = int(d[0])
  935. if l != len(d)-1 {
  936. return false
  937. }
  938. d = d[1:]
  939. if len(d) == 0 {
  940. // ALPN protocols must not be empty.
  941. return false
  942. }
  943. m.alpnProtocol = string(d)
  944. case extensionSCT:
  945. d := data[:length]
  946. if len(d) < 2 {
  947. return false
  948. }
  949. l := int(d[0])<<8 | int(d[1])
  950. d = d[2:]
  951. if len(d) != l || l == 0 {
  952. return false
  953. }
  954. m.scts = make([][]byte, 0, 3)
  955. for len(d) != 0 {
  956. if len(d) < 2 {
  957. return false
  958. }
  959. sctLen := int(d[0])<<8 | int(d[1])
  960. d = d[2:]
  961. if sctLen == 0 || len(d) < sctLen {
  962. return false
  963. }
  964. m.scts = append(m.scts, d[:sctLen])
  965. d = d[sctLen:]
  966. }
  967. }
  968. data = data[length:]
  969. }
  970. return true
  971. }
  972. type serverHelloMsg13 struct {
  973. raw []byte
  974. vers uint16
  975. random []byte
  976. cipherSuite uint16
  977. keyShare keyShare
  978. psk bool
  979. pskIdentity uint16
  980. }
  981. func (m *serverHelloMsg13) equal(i interface{}) bool {
  982. m1, ok := i.(*serverHelloMsg13)
  983. if !ok {
  984. return false
  985. }
  986. return bytes.Equal(m.raw, m1.raw) &&
  987. m.vers == m1.vers &&
  988. bytes.Equal(m.random, m1.random) &&
  989. m.cipherSuite == m1.cipherSuite &&
  990. m.keyShare.group == m1.keyShare.group &&
  991. bytes.Equal(m.keyShare.data, m1.keyShare.data) &&
  992. m.psk == m1.psk &&
  993. m.pskIdentity == m1.pskIdentity
  994. }
  995. func (m *serverHelloMsg13) marshal() []byte {
  996. if m.raw != nil {
  997. return m.raw
  998. }
  999. length := 38
  1000. if m.keyShare.group != 0 {
  1001. length += 8 + len(m.keyShare.data)
  1002. }
  1003. if m.psk {
  1004. length += 6
  1005. }
  1006. x := make([]byte, 4+length)
  1007. x[0] = typeServerHello
  1008. x[1] = uint8(length >> 16)
  1009. x[2] = uint8(length >> 8)
  1010. x[3] = uint8(length)
  1011. x[4] = uint8(m.vers >> 8)
  1012. x[5] = uint8(m.vers)
  1013. copy(x[6:38], m.random)
  1014. x[38] = uint8(m.cipherSuite >> 8)
  1015. x[39] = uint8(m.cipherSuite)
  1016. z := x[42:]
  1017. x[40] = uint8(len(z) >> 8)
  1018. x[41] = uint8(len(z))
  1019. if m.psk {
  1020. z[0] = byte(extensionPreSharedKey >> 8)
  1021. z[1] = byte(extensionPreSharedKey)
  1022. z[3] = 2
  1023. z[4] = byte(m.pskIdentity >> 8)
  1024. z[5] = byte(m.pskIdentity)
  1025. z = z[6:]
  1026. }
  1027. if m.keyShare.group != 0 {
  1028. z[0] = uint8(extensionKeyShare >> 8)
  1029. z[1] = uint8(extensionKeyShare)
  1030. l := 4 + len(m.keyShare.data)
  1031. z[2] = uint8(l >> 8)
  1032. z[3] = uint8(l)
  1033. z[4] = uint8(m.keyShare.group >> 8)
  1034. z[5] = uint8(m.keyShare.group)
  1035. l -= 4
  1036. z[6] = uint8(l >> 8)
  1037. z[7] = uint8(l)
  1038. copy(z[8:], m.keyShare.data)
  1039. }
  1040. m.raw = x
  1041. return x
  1042. }
  1043. func (m *serverHelloMsg13) unmarshal(data []byte) bool {
  1044. if len(data) < 50 {
  1045. return false
  1046. }
  1047. m.raw = data
  1048. m.vers = uint16(data[4])<<8 | uint16(data[5])
  1049. m.random = data[6:38]
  1050. m.cipherSuite = uint16(data[38])<<8 | uint16(data[39])
  1051. m.psk = false
  1052. m.pskIdentity = 0
  1053. extensionsLength := int(data[40])<<8 | int(data[41])
  1054. data = data[42:]
  1055. if len(data) != extensionsLength {
  1056. return false
  1057. }
  1058. for len(data) != 0 {
  1059. if len(data) < 4 {
  1060. return false
  1061. }
  1062. extension := uint16(data[0])<<8 | uint16(data[1])
  1063. length := int(data[2])<<8 | int(data[3])
  1064. data = data[4:]
  1065. if len(data) < length {
  1066. return false
  1067. }
  1068. switch extension {
  1069. default:
  1070. return false
  1071. case extensionPreSharedKey:
  1072. if length != 2 {
  1073. return false
  1074. }
  1075. m.psk = true
  1076. m.pskIdentity = uint16(data[0])<<8 | uint16(data[1])
  1077. case extensionKeyShare:
  1078. if length < 2 {
  1079. return false
  1080. }
  1081. m.keyShare.group = CurveID(data[0])<<8 | CurveID(data[1])
  1082. if length-4 != int(data[2])<<8|int(data[3]) {
  1083. return false
  1084. }
  1085. m.keyShare.data = data[4:length]
  1086. }
  1087. data = data[length:]
  1088. }
  1089. return true
  1090. }
  1091. type encryptedExtensionsMsg struct {
  1092. raw []byte
  1093. alpnProtocol string
  1094. earlyData bool
  1095. }
  1096. func (m *encryptedExtensionsMsg) equal(i interface{}) bool {
  1097. m1, ok := i.(*encryptedExtensionsMsg)
  1098. if !ok {
  1099. return false
  1100. }
  1101. return bytes.Equal(m.raw, m1.raw) &&
  1102. m.alpnProtocol == m1.alpnProtocol &&
  1103. m.earlyData == m1.earlyData
  1104. }
  1105. func (m *encryptedExtensionsMsg) marshal() []byte {
  1106. if m.raw != nil {
  1107. return m.raw
  1108. }
  1109. length := 2
  1110. if m.earlyData {
  1111. length += 4
  1112. }
  1113. alpnLen := len(m.alpnProtocol)
  1114. if alpnLen > 0 {
  1115. if alpnLen >= 256 {
  1116. panic("invalid ALPN protocol")
  1117. }
  1118. length += 2 + 2 + 2 + 1 + alpnLen
  1119. }
  1120. x := make([]byte, 4+length)
  1121. x[0] = typeEncryptedExtensions
  1122. x[1] = uint8(length >> 16)
  1123. x[2] = uint8(length >> 8)
  1124. x[3] = uint8(length)
  1125. length -= 2
  1126. x[4] = uint8(length >> 8)
  1127. x[5] = uint8(length)
  1128. z := x[6:]
  1129. if alpnLen > 0 {
  1130. z[0] = byte(extensionALPN >> 8)
  1131. z[1] = byte(extensionALPN)
  1132. l := 2 + 1 + alpnLen
  1133. z[2] = byte(l >> 8)
  1134. z[3] = byte(l)
  1135. l -= 2
  1136. z[4] = byte(l >> 8)
  1137. z[5] = byte(l)
  1138. l -= 1
  1139. z[6] = byte(l)
  1140. copy(z[7:], []byte(m.alpnProtocol))
  1141. z = z[7+alpnLen:]
  1142. }
  1143. if m.earlyData {
  1144. z[0] = byte(extensionEarlyData >> 8)
  1145. z[1] = byte(extensionEarlyData)
  1146. z = z[4:]
  1147. }
  1148. m.raw = x
  1149. return x
  1150. }
  1151. func (m *encryptedExtensionsMsg) unmarshal(data []byte) bool {
  1152. if len(data) < 6 {
  1153. return false
  1154. }
  1155. m.raw = data
  1156. m.alpnProtocol = ""
  1157. m.earlyData = false
  1158. extensionsLength := int(data[4])<<8 | int(data[5])
  1159. data = data[6:]
  1160. if len(data) != extensionsLength {
  1161. return false
  1162. }
  1163. for len(data) != 0 {
  1164. if len(data) < 4 {
  1165. return false
  1166. }
  1167. extension := uint16(data[0])<<8 | uint16(data[1])
  1168. length := int(data[2])<<8 | int(data[3])
  1169. data = data[4:]
  1170. if len(data) < length {
  1171. return false
  1172. }
  1173. switch extension {
  1174. case extensionALPN:
  1175. d := data[:length]
  1176. if len(d) < 3 {
  1177. return false
  1178. }
  1179. l := int(d[0])<<8 | int(d[1])
  1180. if l != len(d)-2 {
  1181. return false
  1182. }
  1183. d = d[2:]
  1184. l = int(d[0])
  1185. if l != len(d)-1 {
  1186. return false
  1187. }
  1188. d = d[1:]
  1189. if len(d) == 0 {
  1190. // ALPN protocols must not be empty.
  1191. return false
  1192. }
  1193. m.alpnProtocol = string(d)
  1194. case extensionEarlyData:
  1195. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8
  1196. m.earlyData = true
  1197. }
  1198. data = data[length:]
  1199. }
  1200. return true
  1201. }
  1202. type certificateMsg struct {
  1203. raw []byte
  1204. certificates [][]byte
  1205. }
  1206. func (m *certificateMsg) equal(i interface{}) bool {
  1207. m1, ok := i.(*certificateMsg)
  1208. if !ok {
  1209. return false
  1210. }
  1211. return bytes.Equal(m.raw, m1.raw) &&
  1212. eqByteSlices(m.certificates, m1.certificates)
  1213. }
  1214. func (m *certificateMsg) marshal() (x []byte) {
  1215. if m.raw != nil {
  1216. return m.raw
  1217. }
  1218. var i int
  1219. for _, slice := range m.certificates {
  1220. i += len(slice)
  1221. }
  1222. length := 3 + 3*len(m.certificates) + i
  1223. x = make([]byte, 4+length)
  1224. x[0] = typeCertificate
  1225. x[1] = uint8(length >> 16)
  1226. x[2] = uint8(length >> 8)
  1227. x[3] = uint8(length)
  1228. certificateOctets := length - 3
  1229. x[4] = uint8(certificateOctets >> 16)
  1230. x[5] = uint8(certificateOctets >> 8)
  1231. x[6] = uint8(certificateOctets)
  1232. y := x[7:]
  1233. for _, slice := range m.certificates {
  1234. y[0] = uint8(len(slice) >> 16)
  1235. y[1] = uint8(len(slice) >> 8)
  1236. y[2] = uint8(len(slice))
  1237. copy(y[3:], slice)
  1238. y = y[3+len(slice):]
  1239. }
  1240. m.raw = x
  1241. return
  1242. }
  1243. func (m *certificateMsg) unmarshal(data []byte) bool {
  1244. if len(data) < 7 {
  1245. return false
  1246. }
  1247. m.raw = data
  1248. certsLen := uint32(data[4])<<16 | uint32(data[5])<<8 | uint32(data[6])
  1249. if uint32(len(data)) != certsLen+7 {
  1250. return false
  1251. }
  1252. numCerts := 0
  1253. d := data[7:]
  1254. for certsLen > 0 {
  1255. if len(d) < 4 {
  1256. return false
  1257. }
  1258. certLen := uint32(d[0])<<16 | uint32(d[1])<<8 | uint32(d[2])
  1259. if uint32(len(d)) < 3+certLen {
  1260. return false
  1261. }
  1262. d = d[3+certLen:]
  1263. certsLen -= 3 + certLen
  1264. numCerts++
  1265. }
  1266. m.certificates = make([][]byte, numCerts)
  1267. d = data[7:]
  1268. for i := 0; i < numCerts; i++ {
  1269. certLen := uint32(d[0])<<16 | uint32(d[1])<<8 | uint32(d[2])
  1270. m.certificates[i] = d[3 : 3+certLen]
  1271. d = d[3+certLen:]
  1272. }
  1273. return true
  1274. }
  1275. type certificateMsg13 struct {
  1276. raw []byte
  1277. requestContext []byte
  1278. certificates [][]byte
  1279. }
  1280. func (m *certificateMsg13) equal(i interface{}) bool {
  1281. m1, ok := i.(*certificateMsg13)
  1282. if !ok {
  1283. return false
  1284. }
  1285. return bytes.Equal(m.raw, m1.raw) &&
  1286. bytes.Equal(m.requestContext, m1.requestContext) &&
  1287. eqByteSlices(m.certificates, m1.certificates)
  1288. }
  1289. func (m *certificateMsg13) marshal() (x []byte) {
  1290. if m.raw != nil {
  1291. return m.raw
  1292. }
  1293. var i int
  1294. for _, slice := range m.certificates {
  1295. i += len(slice)
  1296. }
  1297. length := 3 + 3*len(m.certificates) + i
  1298. length += 2 * len(m.certificates) // extensions
  1299. length += 1 + len(m.requestContext)
  1300. x = make([]byte, 4+length)
  1301. x[0] = typeCertificate
  1302. x[1] = uint8(length >> 16)
  1303. x[2] = uint8(length >> 8)
  1304. x[3] = uint8(length)
  1305. z := x[4:]
  1306. z[0] = byte(len(m.requestContext))
  1307. copy(z[1:], m.requestContext)
  1308. z = z[1+len(m.requestContext):]
  1309. certificateOctets := len(z) - 3
  1310. z[0] = uint8(certificateOctets >> 16)
  1311. z[1] = uint8(certificateOctets >> 8)
  1312. z[2] = uint8(certificateOctets)
  1313. z = z[3:]
  1314. for _, slice := range m.certificates {
  1315. z[0] = uint8(len(slice) >> 16)
  1316. z[1] = uint8(len(slice) >> 8)
  1317. z[2] = uint8(len(slice))
  1318. copy(z[3:], slice)
  1319. z = z[3+len(slice)+2:]
  1320. }
  1321. m.raw = x
  1322. return
  1323. }
  1324. func (m *certificateMsg13) unmarshal(data []byte) bool {
  1325. if len(data) < 5 {
  1326. return false
  1327. }
  1328. m.raw = data
  1329. ctxLen := data[4]
  1330. if len(data) < int(ctxLen)+5+3 {
  1331. return false
  1332. }
  1333. m.requestContext = data[5 : 5+ctxLen]
  1334. d := data[5+ctxLen:]
  1335. certsLen := uint32(d[0])<<16 | uint32(d[1])<<8 | uint32(d[2])
  1336. if uint32(len(d)) != certsLen+3 {
  1337. return false
  1338. }
  1339. numCerts := 0
  1340. d = d[3:]
  1341. for certsLen > 0 {
  1342. if len(d) < 4 {
  1343. return false
  1344. }
  1345. certLen := uint32(d[0])<<16 | uint32(d[1])<<8 | uint32(d[2])
  1346. if uint32(len(d)) < 3+certLen {
  1347. return false
  1348. }
  1349. d = d[3+certLen:]
  1350. if len(d) < 2 {
  1351. return false
  1352. }
  1353. extLen := uint16(d[0])<<8 | uint16(d[1])
  1354. if uint16(len(d)) < 2+extLen {
  1355. return false
  1356. }
  1357. d = d[2+extLen:]
  1358. certsLen -= 3 + certLen + 2 + uint32(extLen)
  1359. numCerts++
  1360. }
  1361. m.certificates = make([][]byte, numCerts)
  1362. d = data[8+ctxLen:]
  1363. for i := 0; i < numCerts; i++ {
  1364. certLen := uint32(d[0])<<16 | uint32(d[1])<<8 | uint32(d[2])
  1365. m.certificates[i] = d[3 : 3+certLen]
  1366. d = d[3+certLen:]
  1367. extLen := uint16(d[0])<<8 | uint16(d[1])
  1368. d = d[2+extLen:]
  1369. }
  1370. return true
  1371. }
  1372. type serverKeyExchangeMsg struct {
  1373. raw []byte
  1374. key []byte
  1375. }
  1376. func (m *serverKeyExchangeMsg) equal(i interface{}) bool {
  1377. m1, ok := i.(*serverKeyExchangeMsg)
  1378. if !ok {
  1379. return false
  1380. }
  1381. return bytes.Equal(m.raw, m1.raw) &&
  1382. bytes.Equal(m.key, m1.key)
  1383. }
  1384. func (m *serverKeyExchangeMsg) marshal() []byte {
  1385. if m.raw != nil {
  1386. return m.raw
  1387. }
  1388. length := len(m.key)
  1389. x := make([]byte, length+4)
  1390. x[0] = typeServerKeyExchange
  1391. x[1] = uint8(length >> 16)
  1392. x[2] = uint8(length >> 8)
  1393. x[3] = uint8(length)
  1394. copy(x[4:], m.key)
  1395. m.raw = x
  1396. return x
  1397. }
  1398. func (m *serverKeyExchangeMsg) unmarshal(data []byte) bool {
  1399. m.raw = data
  1400. if len(data) < 4 {
  1401. return false
  1402. }
  1403. m.key = data[4:]
  1404. return true
  1405. }
  1406. type certificateStatusMsg struct {
  1407. raw []byte
  1408. statusType uint8
  1409. response []byte
  1410. }
  1411. func (m *certificateStatusMsg) equal(i interface{}) bool {
  1412. m1, ok := i.(*certificateStatusMsg)
  1413. if !ok {
  1414. return false
  1415. }
  1416. return bytes.Equal(m.raw, m1.raw) &&
  1417. m.statusType == m1.statusType &&
  1418. bytes.Equal(m.response, m1.response)
  1419. }
  1420. func (m *certificateStatusMsg) marshal() []byte {
  1421. if m.raw != nil {
  1422. return m.raw
  1423. }
  1424. var x []byte
  1425. if m.statusType == statusTypeOCSP {
  1426. x = make([]byte, 4+4+len(m.response))
  1427. x[0] = typeCertificateStatus
  1428. l := len(m.response) + 4
  1429. x[1] = byte(l >> 16)
  1430. x[2] = byte(l >> 8)
  1431. x[3] = byte(l)
  1432. x[4] = statusTypeOCSP
  1433. l -= 4
  1434. x[5] = byte(l >> 16)
  1435. x[6] = byte(l >> 8)
  1436. x[7] = byte(l)
  1437. copy(x[8:], m.response)
  1438. } else {
  1439. x = []byte{typeCertificateStatus, 0, 0, 1, m.statusType}
  1440. }
  1441. m.raw = x
  1442. return x
  1443. }
  1444. func (m *certificateStatusMsg) unmarshal(data []byte) bool {
  1445. m.raw = data
  1446. if len(data) < 5 {
  1447. return false
  1448. }
  1449. m.statusType = data[4]
  1450. m.response = nil
  1451. if m.statusType == statusTypeOCSP {
  1452. if len(data) < 8 {
  1453. return false
  1454. }
  1455. respLen := uint32(data[5])<<16 | uint32(data[6])<<8 | uint32(data[7])
  1456. if uint32(len(data)) != 4+4+respLen {
  1457. return false
  1458. }
  1459. m.response = data[8:]
  1460. }
  1461. return true
  1462. }
  1463. type serverHelloDoneMsg struct{}
  1464. func (m *serverHelloDoneMsg) equal(i interface{}) bool {
  1465. _, ok := i.(*serverHelloDoneMsg)
  1466. return ok
  1467. }
  1468. func (m *serverHelloDoneMsg) marshal() []byte {
  1469. x := make([]byte, 4)
  1470. x[0] = typeServerHelloDone
  1471. return x
  1472. }
  1473. func (m *serverHelloDoneMsg) unmarshal(data []byte) bool {
  1474. return len(data) == 4
  1475. }
  1476. type clientKeyExchangeMsg struct {
  1477. raw []byte
  1478. ciphertext []byte
  1479. }
  1480. func (m *clientKeyExchangeMsg) equal(i interface{}) bool {
  1481. m1, ok := i.(*clientKeyExchangeMsg)
  1482. if !ok {
  1483. return false
  1484. }
  1485. return bytes.Equal(m.raw, m1.raw) &&
  1486. bytes.Equal(m.ciphertext, m1.ciphertext)
  1487. }
  1488. func (m *clientKeyExchangeMsg) marshal() []byte {
  1489. if m.raw != nil {
  1490. return m.raw
  1491. }
  1492. length := len(m.ciphertext)
  1493. x := make([]byte, length+4)
  1494. x[0] = typeClientKeyExchange
  1495. x[1] = uint8(length >> 16)
  1496. x[2] = uint8(length >> 8)
  1497. x[3] = uint8(length)
  1498. copy(x[4:], m.ciphertext)
  1499. m.raw = x
  1500. return x
  1501. }
  1502. func (m *clientKeyExchangeMsg) unmarshal(data []byte) bool {
  1503. m.raw = data
  1504. if len(data) < 4 {
  1505. return false
  1506. }
  1507. l := int(data[1])<<16 | int(data[2])<<8 | int(data[3])
  1508. if l != len(data)-4 {
  1509. return false
  1510. }
  1511. m.ciphertext = data[4:]
  1512. return true
  1513. }
  1514. type finishedMsg struct {
  1515. raw []byte
  1516. verifyData []byte
  1517. }
  1518. func (m *finishedMsg) equal(i interface{}) bool {
  1519. m1, ok := i.(*finishedMsg)
  1520. if !ok {
  1521. return false
  1522. }
  1523. return bytes.Equal(m.raw, m1.raw) &&
  1524. bytes.Equal(m.verifyData, m1.verifyData)
  1525. }
  1526. func (m *finishedMsg) marshal() (x []byte) {
  1527. if m.raw != nil {
  1528. return m.raw
  1529. }
  1530. x = make([]byte, 4+len(m.verifyData))
  1531. x[0] = typeFinished
  1532. x[3] = byte(len(m.verifyData))
  1533. copy(x[4:], m.verifyData)
  1534. m.raw = x
  1535. return
  1536. }
  1537. func (m *finishedMsg) unmarshal(data []byte) bool {
  1538. m.raw = data
  1539. if len(data) < 4 {
  1540. return false
  1541. }
  1542. m.verifyData = data[4:]
  1543. return true
  1544. }
  1545. type nextProtoMsg struct {
  1546. raw []byte
  1547. proto string
  1548. }
  1549. func (m *nextProtoMsg) equal(i interface{}) bool {
  1550. m1, ok := i.(*nextProtoMsg)
  1551. if !ok {
  1552. return false
  1553. }
  1554. return bytes.Equal(m.raw, m1.raw) &&
  1555. m.proto == m1.proto
  1556. }
  1557. func (m *nextProtoMsg) marshal() []byte {
  1558. if m.raw != nil {
  1559. return m.raw
  1560. }
  1561. l := len(m.proto)
  1562. if l > 255 {
  1563. l = 255
  1564. }
  1565. padding := 32 - (l+2)%32
  1566. length := l + padding + 2
  1567. x := make([]byte, length+4)
  1568. x[0] = typeNextProtocol
  1569. x[1] = uint8(length >> 16)
  1570. x[2] = uint8(length >> 8)
  1571. x[3] = uint8(length)
  1572. y := x[4:]
  1573. y[0] = byte(l)
  1574. copy(y[1:], []byte(m.proto[0:l]))
  1575. y = y[1+l:]
  1576. y[0] = byte(padding)
  1577. m.raw = x
  1578. return x
  1579. }
  1580. func (m *nextProtoMsg) unmarshal(data []byte) bool {
  1581. m.raw = data
  1582. if len(data) < 5 {
  1583. return false
  1584. }
  1585. data = data[4:]
  1586. protoLen := int(data[0])
  1587. data = data[1:]
  1588. if len(data) < protoLen {
  1589. return false
  1590. }
  1591. m.proto = string(data[0:protoLen])
  1592. data = data[protoLen:]
  1593. if len(data) < 1 {
  1594. return false
  1595. }
  1596. paddingLen := int(data[0])
  1597. data = data[1:]
  1598. if len(data) != paddingLen {
  1599. return false
  1600. }
  1601. return true
  1602. }
  1603. type certificateRequestMsg struct {
  1604. raw []byte
  1605. // hasSignatureAndHash indicates whether this message includes a list
  1606. // of signature and hash functions. This change was introduced with TLS
  1607. // 1.2.
  1608. hasSignatureAndHash bool
  1609. certificateTypes []byte
  1610. signatureAndHashes []signatureAndHash
  1611. certificateAuthorities [][]byte
  1612. }
  1613. func (m *certificateRequestMsg) equal(i interface{}) bool {
  1614. m1, ok := i.(*certificateRequestMsg)
  1615. if !ok {
  1616. return false
  1617. }
  1618. return bytes.Equal(m.raw, m1.raw) &&
  1619. bytes.Equal(m.certificateTypes, m1.certificateTypes) &&
  1620. eqByteSlices(m.certificateAuthorities, m1.certificateAuthorities) &&
  1621. eqSignatureAndHashes(m.signatureAndHashes, m1.signatureAndHashes)
  1622. }
  1623. func (m *certificateRequestMsg) marshal() (x []byte) {
  1624. if m.raw != nil {
  1625. return m.raw
  1626. }
  1627. // See http://tools.ietf.org/html/rfc4346#section-7.4.4
  1628. length := 1 + len(m.certificateTypes) + 2
  1629. casLength := 0
  1630. for _, ca := range m.certificateAuthorities {
  1631. casLength += 2 + len(ca)
  1632. }
  1633. length += casLength
  1634. if m.hasSignatureAndHash {
  1635. length += 2 + 2*len(m.signatureAndHashes)
  1636. }
  1637. x = make([]byte, 4+length)
  1638. x[0] = typeCertificateRequest
  1639. x[1] = uint8(length >> 16)
  1640. x[2] = uint8(length >> 8)
  1641. x[3] = uint8(length)
  1642. x[4] = uint8(len(m.certificateTypes))
  1643. copy(x[5:], m.certificateTypes)
  1644. y := x[5+len(m.certificateTypes):]
  1645. if m.hasSignatureAndHash {
  1646. n := len(m.signatureAndHashes) * 2
  1647. y[0] = uint8(n >> 8)
  1648. y[1] = uint8(n)
  1649. y = y[2:]
  1650. for _, sigAndHash := range m.signatureAndHashes {
  1651. y[0] = sigAndHash.hash
  1652. y[1] = sigAndHash.signature
  1653. y = y[2:]
  1654. }
  1655. }
  1656. y[0] = uint8(casLength >> 8)
  1657. y[1] = uint8(casLength)
  1658. y = y[2:]
  1659. for _, ca := range m.certificateAuthorities {
  1660. y[0] = uint8(len(ca) >> 8)
  1661. y[1] = uint8(len(ca))
  1662. y = y[2:]
  1663. copy(y, ca)
  1664. y = y[len(ca):]
  1665. }
  1666. m.raw = x
  1667. return
  1668. }
  1669. func (m *certificateRequestMsg) unmarshal(data []byte) bool {
  1670. m.raw = data
  1671. if len(data) < 5 {
  1672. return false
  1673. }
  1674. length := uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
  1675. if uint32(len(data))-4 != length {
  1676. return false
  1677. }
  1678. numCertTypes := int(data[4])
  1679. data = data[5:]
  1680. if numCertTypes == 0 || len(data) <= numCertTypes {
  1681. return false
  1682. }
  1683. m.certificateTypes = make([]byte, numCertTypes)
  1684. if copy(m.certificateTypes, data) != numCertTypes {
  1685. return false
  1686. }
  1687. data = data[numCertTypes:]
  1688. if m.hasSignatureAndHash {
  1689. if len(data) < 2 {
  1690. return false
  1691. }
  1692. sigAndHashLen := uint16(data[0])<<8 | uint16(data[1])
  1693. data = data[2:]
  1694. if sigAndHashLen&1 != 0 {
  1695. return false
  1696. }
  1697. if len(data) < int(sigAndHashLen) {
  1698. return false
  1699. }
  1700. numSigAndHash := sigAndHashLen / 2
  1701. m.signatureAndHashes = make([]signatureAndHash, numSigAndHash)
  1702. for i := range m.signatureAndHashes {
  1703. m.signatureAndHashes[i].hash = data[0]
  1704. m.signatureAndHashes[i].signature = data[1]
  1705. data = data[2:]
  1706. }
  1707. }
  1708. if len(data) < 2 {
  1709. return false
  1710. }
  1711. casLength := uint16(data[0])<<8 | uint16(data[1])
  1712. data = data[2:]
  1713. if len(data) < int(casLength) {
  1714. return false
  1715. }
  1716. cas := make([]byte, casLength)
  1717. copy(cas, data)
  1718. data = data[casLength:]
  1719. m.certificateAuthorities = nil
  1720. for len(cas) > 0 {
  1721. if len(cas) < 2 {
  1722. return false
  1723. }
  1724. caLen := uint16(cas[0])<<8 | uint16(cas[1])
  1725. cas = cas[2:]
  1726. if len(cas) < int(caLen) {
  1727. return false
  1728. }
  1729. m.certificateAuthorities = append(m.certificateAuthorities, cas[:caLen])
  1730. cas = cas[caLen:]
  1731. }
  1732. return len(data) == 0
  1733. }
  1734. type certificateVerifyMsg struct {
  1735. raw []byte
  1736. hasSignatureAndHash bool
  1737. signatureAndHash signatureAndHash
  1738. signature []byte
  1739. }
  1740. func (m *certificateVerifyMsg) equal(i interface{}) bool {
  1741. m1, ok := i.(*certificateVerifyMsg)
  1742. if !ok {
  1743. return false
  1744. }
  1745. return bytes.Equal(m.raw, m1.raw) &&
  1746. m.hasSignatureAndHash == m1.hasSignatureAndHash &&
  1747. m.signatureAndHash.hash == m1.signatureAndHash.hash &&
  1748. m.signatureAndHash.signature == m1.signatureAndHash.signature &&
  1749. bytes.Equal(m.signature, m1.signature)
  1750. }
  1751. func (m *certificateVerifyMsg) marshal() (x []byte) {
  1752. if m.raw != nil {
  1753. return m.raw
  1754. }
  1755. // See http://tools.ietf.org/html/rfc4346#section-7.4.8
  1756. siglength := len(m.signature)
  1757. length := 2 + siglength
  1758. if m.hasSignatureAndHash {
  1759. length += 2
  1760. }
  1761. x = make([]byte, 4+length)
  1762. x[0] = typeCertificateVerify
  1763. x[1] = uint8(length >> 16)
  1764. x[2] = uint8(length >> 8)
  1765. x[3] = uint8(length)
  1766. y := x[4:]
  1767. if m.hasSignatureAndHash {
  1768. y[0] = m.signatureAndHash.hash
  1769. y[1] = m.signatureAndHash.signature
  1770. y = y[2:]
  1771. }
  1772. y[0] = uint8(siglength >> 8)
  1773. y[1] = uint8(siglength)
  1774. copy(y[2:], m.signature)
  1775. m.raw = x
  1776. return
  1777. }
  1778. func (m *certificateVerifyMsg) unmarshal(data []byte) bool {
  1779. m.raw = data
  1780. if len(data) < 6 {
  1781. return false
  1782. }
  1783. length := uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
  1784. if uint32(len(data))-4 != length {
  1785. return false
  1786. }
  1787. data = data[4:]
  1788. if m.hasSignatureAndHash {
  1789. m.signatureAndHash.hash = data[0]
  1790. m.signatureAndHash.signature = data[1]
  1791. data = data[2:]
  1792. }
  1793. if len(data) < 2 {
  1794. return false
  1795. }
  1796. siglength := int(data[0])<<8 + int(data[1])
  1797. data = data[2:]
  1798. if len(data) != siglength {
  1799. return false
  1800. }
  1801. m.signature = data
  1802. return true
  1803. }
  1804. type newSessionTicketMsg struct {
  1805. raw []byte
  1806. ticket []byte
  1807. }
  1808. func (m *newSessionTicketMsg) equal(i interface{}) bool {
  1809. m1, ok := i.(*newSessionTicketMsg)
  1810. if !ok {
  1811. return false
  1812. }
  1813. return bytes.Equal(m.raw, m1.raw) &&
  1814. bytes.Equal(m.ticket, m1.ticket)
  1815. }
  1816. func (m *newSessionTicketMsg) marshal() (x []byte) {
  1817. if m.raw != nil {
  1818. return m.raw
  1819. }
  1820. // See http://tools.ietf.org/html/rfc5077#section-3.3
  1821. ticketLen := len(m.ticket)
  1822. length := 2 + 4 + ticketLen
  1823. x = make([]byte, 4+length)
  1824. x[0] = typeNewSessionTicket
  1825. x[1] = uint8(length >> 16)
  1826. x[2] = uint8(length >> 8)
  1827. x[3] = uint8(length)
  1828. x[8] = uint8(ticketLen >> 8)
  1829. x[9] = uint8(ticketLen)
  1830. copy(x[10:], m.ticket)
  1831. m.raw = x
  1832. return
  1833. }
  1834. func (m *newSessionTicketMsg) unmarshal(data []byte) bool {
  1835. m.raw = data
  1836. if len(data) < 10 {
  1837. return false
  1838. }
  1839. length := uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
  1840. if uint32(len(data))-4 != length {
  1841. return false
  1842. }
  1843. ticketLen := int(data[8])<<8 + int(data[9])
  1844. if len(data)-10 != ticketLen {
  1845. return false
  1846. }
  1847. m.ticket = data[10:]
  1848. return true
  1849. }
  1850. type newSessionTicketMsg13 struct {
  1851. raw []byte
  1852. lifetime uint32
  1853. ageAdd uint32
  1854. ticket []byte
  1855. withEarlyDataInfo bool
  1856. maxEarlyDataLength uint32
  1857. }
  1858. func (m *newSessionTicketMsg13) equal(i interface{}) bool {
  1859. m1, ok := i.(*newSessionTicketMsg13)
  1860. if !ok {
  1861. return false
  1862. }
  1863. return bytes.Equal(m.raw, m1.raw) &&
  1864. m.lifetime == m1.lifetime &&
  1865. m.ageAdd == m1.ageAdd &&
  1866. bytes.Equal(m.ticket, m1.ticket) &&
  1867. m.withEarlyDataInfo == m1.withEarlyDataInfo &&
  1868. m.maxEarlyDataLength == m1.maxEarlyDataLength
  1869. }
  1870. func (m *newSessionTicketMsg13) marshal() (x []byte) {
  1871. if m.raw != nil {
  1872. return m.raw
  1873. }
  1874. // See https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6
  1875. ticketLen := len(m.ticket)
  1876. length := 12 + ticketLen
  1877. if m.withEarlyDataInfo {
  1878. length += 8
  1879. }
  1880. x = make([]byte, 4+length)
  1881. x[0] = typeNewSessionTicket
  1882. x[1] = uint8(length >> 16)
  1883. x[2] = uint8(length >> 8)
  1884. x[3] = uint8(length)
  1885. x[4] = uint8(m.lifetime >> 24)
  1886. x[5] = uint8(m.lifetime >> 16)
  1887. x[6] = uint8(m.lifetime >> 8)
  1888. x[7] = uint8(m.lifetime)
  1889. x[8] = uint8(m.ageAdd >> 24)
  1890. x[9] = uint8(m.ageAdd >> 16)
  1891. x[10] = uint8(m.ageAdd >> 8)
  1892. x[11] = uint8(m.ageAdd)
  1893. x[12] = uint8(ticketLen >> 8)
  1894. x[13] = uint8(ticketLen)
  1895. copy(x[14:], m.ticket)
  1896. if m.withEarlyDataInfo {
  1897. z := x[14+ticketLen:]
  1898. z[1] = 8
  1899. z[2] = uint8(extensionTicketEarlyDataInfo >> 8)
  1900. z[3] = uint8(extensionTicketEarlyDataInfo)
  1901. z[5] = 4
  1902. z[6] = uint8(m.maxEarlyDataLength >> 24)
  1903. z[7] = uint8(m.maxEarlyDataLength >> 16)
  1904. z[8] = uint8(m.maxEarlyDataLength >> 8)
  1905. z[9] = uint8(m.maxEarlyDataLength)
  1906. }
  1907. m.raw = x
  1908. return
  1909. }
  1910. func (m *newSessionTicketMsg13) unmarshal(data []byte) bool {
  1911. m.raw = data
  1912. m.maxEarlyDataLength = 0
  1913. m.withEarlyDataInfo = false
  1914. if len(data) < 16 {
  1915. return false
  1916. }
  1917. length := uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
  1918. if uint32(len(data))-4 != length {
  1919. return false
  1920. }
  1921. m.lifetime = uint32(data[4])<<24 | uint32(data[5])<<16 |
  1922. uint32(data[6])<<8 | uint32(data[7])
  1923. m.ageAdd = uint32(data[8])<<24 | uint32(data[9])<<16 |
  1924. uint32(data[10])<<8 | uint32(data[11])
  1925. ticketLen := int(data[12])<<8 + int(data[13])
  1926. if 14+ticketLen > len(data) {
  1927. return false
  1928. }
  1929. m.ticket = data[14 : 14+ticketLen]
  1930. data = data[14+ticketLen:]
  1931. extLen := int(data[0])<<8 + int(data[1])
  1932. if extLen != len(data)-2 {
  1933. return false
  1934. }
  1935. data = data[2:]
  1936. for len(data) > 0 {
  1937. if len(data) < 4 {
  1938. return false
  1939. }
  1940. extType := uint16(data[0])<<8 + uint16(data[1])
  1941. length := int(data[2])<<8 + int(data[3])
  1942. data = data[4:]
  1943. switch extType {
  1944. case extensionTicketEarlyDataInfo:
  1945. if length != 4 {
  1946. return false
  1947. }
  1948. m.withEarlyDataInfo = true
  1949. m.maxEarlyDataLength = uint32(data[0])<<24 | uint32(data[1])<<16 |
  1950. uint32(data[2])<<8 | uint32(data[3])
  1951. }
  1952. data = data[length:]
  1953. }
  1954. return true
  1955. }
  1956. type helloRequestMsg struct {
  1957. }
  1958. func (*helloRequestMsg) marshal() []byte {
  1959. return []byte{typeHelloRequest, 0, 0, 0}
  1960. }
  1961. func (*helloRequestMsg) unmarshal(data []byte) bool {
  1962. return len(data) == 4
  1963. }
  1964. func eqUint16s(x, y []uint16) bool {
  1965. if len(x) != len(y) {
  1966. return false
  1967. }
  1968. for i, v := range x {
  1969. if y[i] != v {
  1970. return false
  1971. }
  1972. }
  1973. return true
  1974. }
  1975. func eqCurveIDs(x, y []CurveID) bool {
  1976. if len(x) != len(y) {
  1977. return false
  1978. }
  1979. for i, v := range x {
  1980. if y[i] != v {
  1981. return false
  1982. }
  1983. }
  1984. return true
  1985. }
  1986. func eqStrings(x, y []string) bool {
  1987. if len(x) != len(y) {
  1988. return false
  1989. }
  1990. for i, v := range x {
  1991. if y[i] != v {
  1992. return false
  1993. }
  1994. }
  1995. return true
  1996. }
  1997. func eqByteSlices(x, y [][]byte) bool {
  1998. if len(x) != len(y) {
  1999. return false
  2000. }
  2001. for i, v := range x {
  2002. if !bytes.Equal(v, y[i]) {
  2003. return false
  2004. }
  2005. }
  2006. return true
  2007. }
  2008. func eqSignatureAndHashes(x, y []signatureAndHash) bool {
  2009. if len(x) != len(y) {
  2010. return false
  2011. }
  2012. for i, v := range x {
  2013. v2 := y[i]
  2014. if v.hash != v2.hash || v.signature != v2.signature {
  2015. return false
  2016. }
  2017. }
  2018. return true
  2019. }
  2020. func eqKeyShares(x, y []keyShare) bool {
  2021. if len(x) != len(y) {
  2022. return false
  2023. }
  2024. for i := range x {
  2025. if x[i].group != y[i].group {
  2026. return false
  2027. }
  2028. if !bytes.Equal(x[i].data, y[i].data) {
  2029. return false
  2030. }
  2031. }
  2032. return true
  2033. }