您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

2290 行
49 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) alert {
  351. if len(data) < 42 {
  352. return alertDecodeError
  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 alertDecodeError
  360. }
  361. m.sessionId = data[39 : 39+sessionIdLen]
  362. data = data[39+sessionIdLen:]
  363. bindersOffset := 39 + sessionIdLen
  364. if len(data) < 2 {
  365. return alertDecodeError
  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 alertDecodeError
  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 alertDecodeError
  385. }
  386. compressionMethodsLen := int(data[0])
  387. if len(data) < 1+compressionMethodsLen {
  388. return alertDecodeError
  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 alertSuccess
  409. }
  410. if len(data) < 2 {
  411. return alertDecodeError
  412. }
  413. extensionsLength := int(data[0])<<8 | int(data[1])
  414. data = data[2:]
  415. bindersOffset += 2
  416. if extensionsLength != len(data) {
  417. return alertDecodeError
  418. }
  419. for len(data) != 0 {
  420. if len(data) < 4 {
  421. return alertDecodeError
  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 alertDecodeError
  429. }
  430. switch extension {
  431. case extensionServerName:
  432. d := data[:length]
  433. if len(d) < 2 {
  434. return alertDecodeError
  435. }
  436. namesLen := int(d[0])<<8 | int(d[1])
  437. d = d[2:]
  438. if len(d) != namesLen {
  439. return alertDecodeError
  440. }
  441. for len(d) > 0 {
  442. if len(d) < 3 {
  443. return alertDecodeError
  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 alertDecodeError
  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. // TODO use alertDecodeError?
  458. return alertUnexpectedMessage
  459. }
  460. break
  461. }
  462. d = d[nameLen:]
  463. }
  464. case extensionNextProtoNeg:
  465. if length > 0 {
  466. return alertDecodeError
  467. }
  468. m.nextProtoNeg = true
  469. case extensionStatusRequest:
  470. m.ocspStapling = length > 0 && data[0] == statusTypeOCSP
  471. case extensionSupportedCurves:
  472. // http://tools.ietf.org/html/rfc4492#section-5.5.1
  473. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.4
  474. if length < 2 {
  475. return alertDecodeError
  476. }
  477. l := int(data[0])<<8 | int(data[1])
  478. if l%2 == 1 || length != l+2 {
  479. return alertDecodeError
  480. }
  481. numCurves := l / 2
  482. m.supportedCurves = make([]CurveID, numCurves)
  483. d := data[2:]
  484. for i := 0; i < numCurves; i++ {
  485. m.supportedCurves[i] = CurveID(d[0])<<8 | CurveID(d[1])
  486. d = d[2:]
  487. }
  488. case extensionSupportedPoints:
  489. // http://tools.ietf.org/html/rfc4492#section-5.5.2
  490. if length < 1 {
  491. return alertDecodeError
  492. }
  493. l := int(data[0])
  494. if length != l+1 {
  495. return alertDecodeError
  496. }
  497. m.supportedPoints = make([]uint8, l)
  498. copy(m.supportedPoints, data[1:])
  499. case extensionSessionTicket:
  500. // http://tools.ietf.org/html/rfc5077#section-3.2
  501. m.ticketSupported = true
  502. m.sessionTicket = data[:length]
  503. case extensionSignatureAlgorithms:
  504. // https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
  505. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.3
  506. if length < 2 || length&1 != 0 {
  507. return alertDecodeError
  508. }
  509. l := int(data[0])<<8 | int(data[1])
  510. if l != length-2 {
  511. return alertDecodeError
  512. }
  513. n := l / 2
  514. d := data[2:]
  515. m.signatureAndHashes = make([]signatureAndHash, n)
  516. for i := range m.signatureAndHashes {
  517. m.signatureAndHashes[i].hash = d[0]
  518. m.signatureAndHashes[i].signature = d[1]
  519. d = d[2:]
  520. }
  521. case extensionRenegotiationInfo:
  522. if length == 0 {
  523. return alertDecodeError
  524. }
  525. d := data[:length]
  526. l := int(d[0])
  527. d = d[1:]
  528. if l != len(d) {
  529. return alertDecodeError
  530. }
  531. m.secureRenegotiation = d
  532. m.secureRenegotiationSupported = true
  533. case extensionALPN:
  534. if length < 2 {
  535. return alertDecodeError
  536. }
  537. l := int(data[0])<<8 | int(data[1])
  538. if l != length-2 {
  539. return alertDecodeError
  540. }
  541. d := data[2:length]
  542. for len(d) != 0 {
  543. stringLen := int(d[0])
  544. d = d[1:]
  545. if stringLen == 0 || stringLen > len(d) {
  546. return alertDecodeError
  547. }
  548. m.alpnProtocols = append(m.alpnProtocols, string(d[:stringLen]))
  549. d = d[stringLen:]
  550. }
  551. case extensionSCT:
  552. m.scts = true
  553. if length != 0 {
  554. return alertDecodeError
  555. }
  556. case extensionKeyShare:
  557. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.5
  558. if length < 2 {
  559. return alertDecodeError
  560. }
  561. l := int(data[0])<<8 | int(data[1])
  562. if l != length-2 {
  563. return alertDecodeError
  564. }
  565. d := data[2:length]
  566. for len(d) != 0 {
  567. if len(d) < 4 {
  568. return alertDecodeError
  569. }
  570. dataLen := int(d[2])<<8 | int(d[3])
  571. if dataLen == 0 || 4+dataLen > len(d) {
  572. return alertDecodeError
  573. }
  574. m.keyShares = append(m.keyShares, keyShare{
  575. group: CurveID(d[0])<<8 | CurveID(d[1]),
  576. data: d[4 : 4+dataLen],
  577. })
  578. d = d[4+dataLen:]
  579. }
  580. case extensionSupportedVersions:
  581. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.1
  582. if length < 1 {
  583. return alertDecodeError
  584. }
  585. l := int(data[0])
  586. if l%2 == 1 || length != l+1 {
  587. return alertDecodeError
  588. }
  589. n := l / 2
  590. d := data[1:]
  591. for i := 0; i < n; i++ {
  592. v := uint16(d[0])<<8 + uint16(d[1])
  593. m.supportedVersions = append(m.supportedVersions, v)
  594. d = d[2:]
  595. }
  596. case extensionPreSharedKey:
  597. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6
  598. if length < 2 {
  599. return alertDecodeError
  600. }
  601. // Ensure this extension is the last one in the Client Hello
  602. if len(data) != length {
  603. return alertIllegalParameter
  604. }
  605. li := int(data[0])<<8 | int(data[1])
  606. if 2+li+2 > length {
  607. return alertDecodeError
  608. }
  609. d := data[2 : 2+li]
  610. bindersOffset += 2 + li
  611. for len(d) > 0 {
  612. if len(d) < 6 {
  613. return alertDecodeError
  614. }
  615. l := int(d[0])<<8 | int(d[1])
  616. if len(d) < 2+l+4 {
  617. return alertDecodeError
  618. }
  619. m.psks = append(m.psks, psk{
  620. identity: d[2 : 2+l],
  621. obfTicketAge: uint32(d[l+2])<<24 | uint32(d[l+3])<<16 |
  622. uint32(d[l+4])<<8 | uint32(d[l+5]),
  623. })
  624. d = d[2+l+4:]
  625. }
  626. lb := int(data[li+2])<<8 | int(data[li+3])
  627. d = data[2+li+2:]
  628. if lb != len(d) || lb == 0 {
  629. return alertDecodeError
  630. }
  631. i := 0
  632. for len(d) > 0 {
  633. if i >= len(m.psks) {
  634. return alertIllegalParameter
  635. }
  636. if len(d) < 1 {
  637. return alertDecodeError
  638. }
  639. l := int(d[0])
  640. if l > len(d)-1 {
  641. return alertDecodeError
  642. }
  643. if i >= len(m.psks) {
  644. return alertIllegalParameter
  645. }
  646. m.psks[i].binder = d[1 : 1+l]
  647. d = d[1+l:]
  648. i++
  649. }
  650. if i != len(m.psks) {
  651. return alertIllegalParameter
  652. }
  653. m.rawTruncated = m.raw[:bindersOffset]
  654. case extensionPSKKeyExchangeModes:
  655. if length < 2 {
  656. return alertDecodeError
  657. }
  658. l := int(data[0])
  659. if length != l+1 {
  660. return alertDecodeError
  661. }
  662. m.pskKeyExchangeModes = data[1:length]
  663. case extensionEarlyData:
  664. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8
  665. m.earlyData = true
  666. }
  667. data = data[length:]
  668. bindersOffset += length
  669. }
  670. return alertSuccess
  671. }
  672. type serverHelloMsg struct {
  673. raw []byte
  674. vers uint16
  675. random []byte
  676. sessionId []byte
  677. cipherSuite uint16
  678. compressionMethod uint8
  679. nextProtoNeg bool
  680. nextProtos []string
  681. ocspStapling bool
  682. scts [][]byte
  683. ticketSupported bool
  684. secureRenegotiation []byte
  685. secureRenegotiationSupported bool
  686. alpnProtocol string
  687. }
  688. func (m *serverHelloMsg) equal(i interface{}) bool {
  689. m1, ok := i.(*serverHelloMsg)
  690. if !ok {
  691. return false
  692. }
  693. if len(m.scts) != len(m1.scts) {
  694. return false
  695. }
  696. for i, sct := range m.scts {
  697. if !bytes.Equal(sct, m1.scts[i]) {
  698. return false
  699. }
  700. }
  701. return bytes.Equal(m.raw, m1.raw) &&
  702. m.vers == m1.vers &&
  703. bytes.Equal(m.random, m1.random) &&
  704. bytes.Equal(m.sessionId, m1.sessionId) &&
  705. m.cipherSuite == m1.cipherSuite &&
  706. m.compressionMethod == m1.compressionMethod &&
  707. m.nextProtoNeg == m1.nextProtoNeg &&
  708. eqStrings(m.nextProtos, m1.nextProtos) &&
  709. m.ocspStapling == m1.ocspStapling &&
  710. m.ticketSupported == m1.ticketSupported &&
  711. m.secureRenegotiationSupported == m1.secureRenegotiationSupported &&
  712. bytes.Equal(m.secureRenegotiation, m1.secureRenegotiation) &&
  713. m.alpnProtocol == m1.alpnProtocol
  714. }
  715. func (m *serverHelloMsg) marshal() []byte {
  716. if m.raw != nil {
  717. return m.raw
  718. }
  719. length := 38 + len(m.sessionId)
  720. numExtensions := 0
  721. extensionsLength := 0
  722. nextProtoLen := 0
  723. if m.nextProtoNeg {
  724. numExtensions++
  725. for _, v := range m.nextProtos {
  726. nextProtoLen += len(v)
  727. }
  728. nextProtoLen += len(m.nextProtos)
  729. extensionsLength += nextProtoLen
  730. }
  731. if m.ocspStapling {
  732. numExtensions++
  733. }
  734. if m.ticketSupported {
  735. numExtensions++
  736. }
  737. if m.secureRenegotiationSupported {
  738. extensionsLength += 1 + len(m.secureRenegotiation)
  739. numExtensions++
  740. }
  741. if alpnLen := len(m.alpnProtocol); alpnLen > 0 {
  742. if alpnLen >= 256 {
  743. panic("invalid ALPN protocol")
  744. }
  745. extensionsLength += 2 + 1 + alpnLen
  746. numExtensions++
  747. }
  748. sctLen := 0
  749. if len(m.scts) > 0 {
  750. for _, sct := range m.scts {
  751. sctLen += len(sct) + 2
  752. }
  753. extensionsLength += 2 + sctLen
  754. numExtensions++
  755. }
  756. if numExtensions > 0 {
  757. extensionsLength += 4 * numExtensions
  758. length += 2 + extensionsLength
  759. }
  760. x := make([]byte, 4+length)
  761. x[0] = typeServerHello
  762. x[1] = uint8(length >> 16)
  763. x[2] = uint8(length >> 8)
  764. x[3] = uint8(length)
  765. x[4] = uint8(m.vers >> 8)
  766. x[5] = uint8(m.vers)
  767. copy(x[6:38], m.random)
  768. x[38] = uint8(len(m.sessionId))
  769. copy(x[39:39+len(m.sessionId)], m.sessionId)
  770. z := x[39+len(m.sessionId):]
  771. z[0] = uint8(m.cipherSuite >> 8)
  772. z[1] = uint8(m.cipherSuite)
  773. z[2] = m.compressionMethod
  774. z = z[3:]
  775. if numExtensions > 0 {
  776. z[0] = byte(extensionsLength >> 8)
  777. z[1] = byte(extensionsLength)
  778. z = z[2:]
  779. }
  780. if m.nextProtoNeg {
  781. z[0] = byte(extensionNextProtoNeg >> 8)
  782. z[1] = byte(extensionNextProtoNeg & 0xff)
  783. z[2] = byte(nextProtoLen >> 8)
  784. z[3] = byte(nextProtoLen)
  785. z = z[4:]
  786. for _, v := range m.nextProtos {
  787. l := len(v)
  788. if l > 255 {
  789. l = 255
  790. }
  791. z[0] = byte(l)
  792. copy(z[1:], []byte(v[0:l]))
  793. z = z[1+l:]
  794. }
  795. }
  796. if m.ocspStapling {
  797. z[0] = byte(extensionStatusRequest >> 8)
  798. z[1] = byte(extensionStatusRequest)
  799. z = z[4:]
  800. }
  801. if m.ticketSupported {
  802. z[0] = byte(extensionSessionTicket >> 8)
  803. z[1] = byte(extensionSessionTicket)
  804. z = z[4:]
  805. }
  806. if m.secureRenegotiationSupported {
  807. z[0] = byte(extensionRenegotiationInfo >> 8)
  808. z[1] = byte(extensionRenegotiationInfo & 0xff)
  809. z[2] = 0
  810. z[3] = byte(len(m.secureRenegotiation) + 1)
  811. z[4] = byte(len(m.secureRenegotiation))
  812. z = z[5:]
  813. copy(z, m.secureRenegotiation)
  814. z = z[len(m.secureRenegotiation):]
  815. }
  816. if alpnLen := len(m.alpnProtocol); alpnLen > 0 {
  817. z[0] = byte(extensionALPN >> 8)
  818. z[1] = byte(extensionALPN & 0xff)
  819. l := 2 + 1 + alpnLen
  820. z[2] = byte(l >> 8)
  821. z[3] = byte(l)
  822. l -= 2
  823. z[4] = byte(l >> 8)
  824. z[5] = byte(l)
  825. l -= 1
  826. z[6] = byte(l)
  827. copy(z[7:], []byte(m.alpnProtocol))
  828. z = z[7+alpnLen:]
  829. }
  830. if sctLen > 0 {
  831. z[0] = byte(extensionSCT >> 8)
  832. z[1] = byte(extensionSCT)
  833. l := sctLen + 2
  834. z[2] = byte(l >> 8)
  835. z[3] = byte(l)
  836. z[4] = byte(sctLen >> 8)
  837. z[5] = byte(sctLen)
  838. z = z[6:]
  839. for _, sct := range m.scts {
  840. z[0] = byte(len(sct) >> 8)
  841. z[1] = byte(len(sct))
  842. copy(z[2:], sct)
  843. z = z[len(sct)+2:]
  844. }
  845. }
  846. m.raw = x
  847. return x
  848. }
  849. func (m *serverHelloMsg) unmarshal(data []byte) alert {
  850. if len(data) < 42 {
  851. return alertDecodeError
  852. }
  853. m.raw = data
  854. m.vers = uint16(data[4])<<8 | uint16(data[5])
  855. m.random = data[6:38]
  856. sessionIdLen := int(data[38])
  857. if sessionIdLen > 32 || len(data) < 39+sessionIdLen {
  858. return alertDecodeError
  859. }
  860. m.sessionId = data[39 : 39+sessionIdLen]
  861. data = data[39+sessionIdLen:]
  862. if len(data) < 3 {
  863. return alertDecodeError
  864. }
  865. m.cipherSuite = uint16(data[0])<<8 | uint16(data[1])
  866. m.compressionMethod = data[2]
  867. data = data[3:]
  868. m.nextProtoNeg = false
  869. m.nextProtos = nil
  870. m.ocspStapling = false
  871. m.scts = nil
  872. m.ticketSupported = false
  873. m.alpnProtocol = ""
  874. if len(data) == 0 {
  875. // ServerHello is optionally followed by extension data
  876. return alertSuccess
  877. }
  878. if len(data) < 2 {
  879. return alertDecodeError
  880. }
  881. extensionsLength := int(data[0])<<8 | int(data[1])
  882. data = data[2:]
  883. if len(data) != extensionsLength {
  884. return alertDecodeError
  885. }
  886. for len(data) != 0 {
  887. if len(data) < 4 {
  888. return alertDecodeError
  889. }
  890. extension := uint16(data[0])<<8 | uint16(data[1])
  891. length := int(data[2])<<8 | int(data[3])
  892. data = data[4:]
  893. if len(data) < length {
  894. return alertDecodeError
  895. }
  896. switch extension {
  897. case extensionNextProtoNeg:
  898. m.nextProtoNeg = true
  899. d := data[:length]
  900. for len(d) > 0 {
  901. l := int(d[0])
  902. d = d[1:]
  903. if l == 0 || l > len(d) {
  904. return alertDecodeError
  905. }
  906. m.nextProtos = append(m.nextProtos, string(d[:l]))
  907. d = d[l:]
  908. }
  909. case extensionStatusRequest:
  910. if length > 0 {
  911. return alertDecodeError
  912. }
  913. m.ocspStapling = true
  914. case extensionSessionTicket:
  915. if length > 0 {
  916. return alertDecodeError
  917. }
  918. m.ticketSupported = true
  919. case extensionRenegotiationInfo:
  920. if length == 0 {
  921. return alertDecodeError
  922. }
  923. d := data[:length]
  924. l := int(d[0])
  925. d = d[1:]
  926. if l != len(d) {
  927. return alertDecodeError
  928. }
  929. m.secureRenegotiation = d
  930. m.secureRenegotiationSupported = true
  931. case extensionALPN:
  932. d := data[:length]
  933. if len(d) < 3 {
  934. return alertDecodeError
  935. }
  936. l := int(d[0])<<8 | int(d[1])
  937. if l != len(d)-2 {
  938. return alertDecodeError
  939. }
  940. d = d[2:]
  941. l = int(d[0])
  942. if l != len(d)-1 {
  943. return alertDecodeError
  944. }
  945. d = d[1:]
  946. if len(d) == 0 {
  947. // ALPN protocols must not be empty.
  948. return alertDecodeError
  949. }
  950. m.alpnProtocol = string(d)
  951. case extensionSCT:
  952. d := data[:length]
  953. if len(d) < 2 {
  954. return alertDecodeError
  955. }
  956. l := int(d[0])<<8 | int(d[1])
  957. d = d[2:]
  958. if len(d) != l || l == 0 {
  959. return alertDecodeError
  960. }
  961. m.scts = make([][]byte, 0, 3)
  962. for len(d) != 0 {
  963. if len(d) < 2 {
  964. return alertDecodeError
  965. }
  966. sctLen := int(d[0])<<8 | int(d[1])
  967. d = d[2:]
  968. if sctLen == 0 || len(d) < sctLen {
  969. return alertDecodeError
  970. }
  971. m.scts = append(m.scts, d[:sctLen])
  972. d = d[sctLen:]
  973. }
  974. }
  975. data = data[length:]
  976. }
  977. return alertSuccess
  978. }
  979. type serverHelloMsg13 struct {
  980. raw []byte
  981. vers uint16
  982. random []byte
  983. cipherSuite uint16
  984. keyShare keyShare
  985. psk bool
  986. pskIdentity uint16
  987. }
  988. func (m *serverHelloMsg13) equal(i interface{}) bool {
  989. m1, ok := i.(*serverHelloMsg13)
  990. if !ok {
  991. return false
  992. }
  993. return bytes.Equal(m.raw, m1.raw) &&
  994. m.vers == m1.vers &&
  995. bytes.Equal(m.random, m1.random) &&
  996. m.cipherSuite == m1.cipherSuite &&
  997. m.keyShare.group == m1.keyShare.group &&
  998. bytes.Equal(m.keyShare.data, m1.keyShare.data) &&
  999. m.psk == m1.psk &&
  1000. m.pskIdentity == m1.pskIdentity
  1001. }
  1002. func (m *serverHelloMsg13) marshal() []byte {
  1003. if m.raw != nil {
  1004. return m.raw
  1005. }
  1006. length := 38
  1007. if m.keyShare.group != 0 {
  1008. length += 8 + len(m.keyShare.data)
  1009. }
  1010. if m.psk {
  1011. length += 6
  1012. }
  1013. x := make([]byte, 4+length)
  1014. x[0] = typeServerHello
  1015. x[1] = uint8(length >> 16)
  1016. x[2] = uint8(length >> 8)
  1017. x[3] = uint8(length)
  1018. x[4] = uint8(m.vers >> 8)
  1019. x[5] = uint8(m.vers)
  1020. copy(x[6:38], m.random)
  1021. x[38] = uint8(m.cipherSuite >> 8)
  1022. x[39] = uint8(m.cipherSuite)
  1023. z := x[42:]
  1024. x[40] = uint8(len(z) >> 8)
  1025. x[41] = uint8(len(z))
  1026. if m.psk {
  1027. z[0] = byte(extensionPreSharedKey >> 8)
  1028. z[1] = byte(extensionPreSharedKey)
  1029. z[3] = 2
  1030. z[4] = byte(m.pskIdentity >> 8)
  1031. z[5] = byte(m.pskIdentity)
  1032. z = z[6:]
  1033. }
  1034. if m.keyShare.group != 0 {
  1035. z[0] = uint8(extensionKeyShare >> 8)
  1036. z[1] = uint8(extensionKeyShare)
  1037. l := 4 + len(m.keyShare.data)
  1038. z[2] = uint8(l >> 8)
  1039. z[3] = uint8(l)
  1040. z[4] = uint8(m.keyShare.group >> 8)
  1041. z[5] = uint8(m.keyShare.group)
  1042. l -= 4
  1043. z[6] = uint8(l >> 8)
  1044. z[7] = uint8(l)
  1045. copy(z[8:], m.keyShare.data)
  1046. }
  1047. m.raw = x
  1048. return x
  1049. }
  1050. func (m *serverHelloMsg13) unmarshal(data []byte) alert {
  1051. if len(data) < 50 {
  1052. return alertDecodeError
  1053. }
  1054. m.raw = data
  1055. m.vers = uint16(data[4])<<8 | uint16(data[5])
  1056. m.random = data[6:38]
  1057. m.cipherSuite = uint16(data[38])<<8 | uint16(data[39])
  1058. m.psk = false
  1059. m.pskIdentity = 0
  1060. extensionsLength := int(data[40])<<8 | int(data[41])
  1061. data = data[42:]
  1062. if len(data) != extensionsLength {
  1063. return alertDecodeError
  1064. }
  1065. for len(data) != 0 {
  1066. if len(data) < 4 {
  1067. return alertDecodeError
  1068. }
  1069. extension := uint16(data[0])<<8 | uint16(data[1])
  1070. length := int(data[2])<<8 | int(data[3])
  1071. data = data[4:]
  1072. if len(data) < length {
  1073. return alertDecodeError
  1074. }
  1075. switch extension {
  1076. default:
  1077. return alertDecodeError
  1078. case extensionPreSharedKey:
  1079. if length != 2 {
  1080. return alertDecodeError
  1081. }
  1082. m.psk = true
  1083. m.pskIdentity = uint16(data[0])<<8 | uint16(data[1])
  1084. case extensionKeyShare:
  1085. if length < 2 {
  1086. return alertDecodeError
  1087. }
  1088. m.keyShare.group = CurveID(data[0])<<8 | CurveID(data[1])
  1089. if length-4 != int(data[2])<<8|int(data[3]) {
  1090. return alertDecodeError
  1091. }
  1092. m.keyShare.data = data[4:length]
  1093. }
  1094. data = data[length:]
  1095. }
  1096. return alertSuccess
  1097. }
  1098. type encryptedExtensionsMsg struct {
  1099. raw []byte
  1100. alpnProtocol string
  1101. earlyData bool
  1102. }
  1103. func (m *encryptedExtensionsMsg) equal(i interface{}) bool {
  1104. m1, ok := i.(*encryptedExtensionsMsg)
  1105. if !ok {
  1106. return false
  1107. }
  1108. return bytes.Equal(m.raw, m1.raw) &&
  1109. m.alpnProtocol == m1.alpnProtocol &&
  1110. m.earlyData == m1.earlyData
  1111. }
  1112. func (m *encryptedExtensionsMsg) marshal() []byte {
  1113. if m.raw != nil {
  1114. return m.raw
  1115. }
  1116. length := 2
  1117. if m.earlyData {
  1118. length += 4
  1119. }
  1120. alpnLen := len(m.alpnProtocol)
  1121. if alpnLen > 0 {
  1122. if alpnLen >= 256 {
  1123. panic("invalid ALPN protocol")
  1124. }
  1125. length += 2 + 2 + 2 + 1 + alpnLen
  1126. }
  1127. x := make([]byte, 4+length)
  1128. x[0] = typeEncryptedExtensions
  1129. x[1] = uint8(length >> 16)
  1130. x[2] = uint8(length >> 8)
  1131. x[3] = uint8(length)
  1132. length -= 2
  1133. x[4] = uint8(length >> 8)
  1134. x[5] = uint8(length)
  1135. z := x[6:]
  1136. if alpnLen > 0 {
  1137. z[0] = byte(extensionALPN >> 8)
  1138. z[1] = byte(extensionALPN)
  1139. l := 2 + 1 + alpnLen
  1140. z[2] = byte(l >> 8)
  1141. z[3] = byte(l)
  1142. l -= 2
  1143. z[4] = byte(l >> 8)
  1144. z[5] = byte(l)
  1145. l -= 1
  1146. z[6] = byte(l)
  1147. copy(z[7:], []byte(m.alpnProtocol))
  1148. z = z[7+alpnLen:]
  1149. }
  1150. if m.earlyData {
  1151. z[0] = byte(extensionEarlyData >> 8)
  1152. z[1] = byte(extensionEarlyData)
  1153. z = z[4:]
  1154. }
  1155. m.raw = x
  1156. return x
  1157. }
  1158. func (m *encryptedExtensionsMsg) unmarshal(data []byte) alert {
  1159. if len(data) < 6 {
  1160. return alertDecodeError
  1161. }
  1162. m.raw = data
  1163. m.alpnProtocol = ""
  1164. m.earlyData = false
  1165. extensionsLength := int(data[4])<<8 | int(data[5])
  1166. data = data[6:]
  1167. if len(data) != extensionsLength {
  1168. return alertDecodeError
  1169. }
  1170. for len(data) != 0 {
  1171. if len(data) < 4 {
  1172. return alertDecodeError
  1173. }
  1174. extension := uint16(data[0])<<8 | uint16(data[1])
  1175. length := int(data[2])<<8 | int(data[3])
  1176. data = data[4:]
  1177. if len(data) < length {
  1178. return alertDecodeError
  1179. }
  1180. switch extension {
  1181. case extensionALPN:
  1182. d := data[:length]
  1183. if len(d) < 3 {
  1184. return alertDecodeError
  1185. }
  1186. l := int(d[0])<<8 | int(d[1])
  1187. if l != len(d)-2 {
  1188. return alertDecodeError
  1189. }
  1190. d = d[2:]
  1191. l = int(d[0])
  1192. if l != len(d)-1 {
  1193. return alertDecodeError
  1194. }
  1195. d = d[1:]
  1196. if len(d) == 0 {
  1197. // ALPN protocols must not be empty.
  1198. return alertDecodeError
  1199. }
  1200. m.alpnProtocol = string(d)
  1201. case extensionEarlyData:
  1202. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8
  1203. m.earlyData = true
  1204. }
  1205. data = data[length:]
  1206. }
  1207. return alertSuccess
  1208. }
  1209. type certificateMsg struct {
  1210. raw []byte
  1211. certificates [][]byte
  1212. }
  1213. func (m *certificateMsg) equal(i interface{}) bool {
  1214. m1, ok := i.(*certificateMsg)
  1215. if !ok {
  1216. return false
  1217. }
  1218. return bytes.Equal(m.raw, m1.raw) &&
  1219. eqByteSlices(m.certificates, m1.certificates)
  1220. }
  1221. func (m *certificateMsg) marshal() (x []byte) {
  1222. if m.raw != nil {
  1223. return m.raw
  1224. }
  1225. var i int
  1226. for _, slice := range m.certificates {
  1227. i += len(slice)
  1228. }
  1229. length := 3 + 3*len(m.certificates) + i
  1230. x = make([]byte, 4+length)
  1231. x[0] = typeCertificate
  1232. x[1] = uint8(length >> 16)
  1233. x[2] = uint8(length >> 8)
  1234. x[3] = uint8(length)
  1235. certificateOctets := length - 3
  1236. x[4] = uint8(certificateOctets >> 16)
  1237. x[5] = uint8(certificateOctets >> 8)
  1238. x[6] = uint8(certificateOctets)
  1239. y := x[7:]
  1240. for _, slice := range m.certificates {
  1241. y[0] = uint8(len(slice) >> 16)
  1242. y[1] = uint8(len(slice) >> 8)
  1243. y[2] = uint8(len(slice))
  1244. copy(y[3:], slice)
  1245. y = y[3+len(slice):]
  1246. }
  1247. m.raw = x
  1248. return
  1249. }
  1250. func (m *certificateMsg) unmarshal(data []byte) alert {
  1251. if len(data) < 7 {
  1252. return alertDecodeError
  1253. }
  1254. m.raw = data
  1255. certsLen := uint32(data[4])<<16 | uint32(data[5])<<8 | uint32(data[6])
  1256. if uint32(len(data)) != certsLen+7 {
  1257. return alertDecodeError
  1258. }
  1259. numCerts := 0
  1260. d := data[7:]
  1261. for certsLen > 0 {
  1262. if len(d) < 4 {
  1263. return alertDecodeError
  1264. }
  1265. certLen := uint32(d[0])<<16 | uint32(d[1])<<8 | uint32(d[2])
  1266. if uint32(len(d)) < 3+certLen {
  1267. return alertDecodeError
  1268. }
  1269. d = d[3+certLen:]
  1270. certsLen -= 3 + certLen
  1271. numCerts++
  1272. }
  1273. m.certificates = make([][]byte, numCerts)
  1274. d = data[7:]
  1275. for i := 0; i < numCerts; i++ {
  1276. certLen := uint32(d[0])<<16 | uint32(d[1])<<8 | uint32(d[2])
  1277. m.certificates[i] = d[3 : 3+certLen]
  1278. d = d[3+certLen:]
  1279. }
  1280. return alertSuccess
  1281. }
  1282. type certificateMsg13 struct {
  1283. raw []byte
  1284. requestContext []byte
  1285. certificates [][]byte
  1286. }
  1287. func (m *certificateMsg13) equal(i interface{}) bool {
  1288. m1, ok := i.(*certificateMsg13)
  1289. if !ok {
  1290. return false
  1291. }
  1292. return bytes.Equal(m.raw, m1.raw) &&
  1293. bytes.Equal(m.requestContext, m1.requestContext) &&
  1294. eqByteSlices(m.certificates, m1.certificates)
  1295. }
  1296. func (m *certificateMsg13) marshal() (x []byte) {
  1297. if m.raw != nil {
  1298. return m.raw
  1299. }
  1300. var i int
  1301. for _, slice := range m.certificates {
  1302. i += len(slice)
  1303. }
  1304. length := 3 + 3*len(m.certificates) + i
  1305. length += 2 * len(m.certificates) // extensions
  1306. length += 1 + len(m.requestContext)
  1307. x = make([]byte, 4+length)
  1308. x[0] = typeCertificate
  1309. x[1] = uint8(length >> 16)
  1310. x[2] = uint8(length >> 8)
  1311. x[3] = uint8(length)
  1312. z := x[4:]
  1313. z[0] = byte(len(m.requestContext))
  1314. copy(z[1:], m.requestContext)
  1315. z = z[1+len(m.requestContext):]
  1316. certificateOctets := len(z) - 3
  1317. z[0] = uint8(certificateOctets >> 16)
  1318. z[1] = uint8(certificateOctets >> 8)
  1319. z[2] = uint8(certificateOctets)
  1320. z = z[3:]
  1321. for _, slice := range m.certificates {
  1322. z[0] = uint8(len(slice) >> 16)
  1323. z[1] = uint8(len(slice) >> 8)
  1324. z[2] = uint8(len(slice))
  1325. copy(z[3:], slice)
  1326. z = z[3+len(slice)+2:]
  1327. }
  1328. m.raw = x
  1329. return
  1330. }
  1331. func (m *certificateMsg13) unmarshal(data []byte) alert {
  1332. if len(data) < 5 {
  1333. return alertDecodeError
  1334. }
  1335. m.raw = data
  1336. ctxLen := data[4]
  1337. if len(data) < int(ctxLen)+5+3 {
  1338. return alertDecodeError
  1339. }
  1340. m.requestContext = data[5 : 5+ctxLen]
  1341. d := data[5+ctxLen:]
  1342. certsLen := uint32(d[0])<<16 | uint32(d[1])<<8 | uint32(d[2])
  1343. if uint32(len(d)) != certsLen+3 {
  1344. return alertDecodeError
  1345. }
  1346. numCerts := 0
  1347. d = d[3:]
  1348. for certsLen > 0 {
  1349. if len(d) < 4 {
  1350. return alertDecodeError
  1351. }
  1352. certLen := uint32(d[0])<<16 | uint32(d[1])<<8 | uint32(d[2])
  1353. if uint32(len(d)) < 3+certLen {
  1354. return alertDecodeError
  1355. }
  1356. d = d[3+certLen:]
  1357. if len(d) < 2 {
  1358. return alertDecodeError
  1359. }
  1360. extLen := uint16(d[0])<<8 | uint16(d[1])
  1361. if uint16(len(d)) < 2+extLen {
  1362. return alertDecodeError
  1363. }
  1364. d = d[2+extLen:]
  1365. certsLen -= 3 + certLen + 2 + uint32(extLen)
  1366. numCerts++
  1367. }
  1368. m.certificates = make([][]byte, numCerts)
  1369. d = data[8+ctxLen:]
  1370. for i := 0; i < numCerts; i++ {
  1371. certLen := uint32(d[0])<<16 | uint32(d[1])<<8 | uint32(d[2])
  1372. m.certificates[i] = d[3 : 3+certLen]
  1373. d = d[3+certLen:]
  1374. extLen := uint16(d[0])<<8 | uint16(d[1])
  1375. d = d[2+extLen:]
  1376. }
  1377. return alertSuccess
  1378. }
  1379. type serverKeyExchangeMsg struct {
  1380. raw []byte
  1381. key []byte
  1382. }
  1383. func (m *serverKeyExchangeMsg) equal(i interface{}) bool {
  1384. m1, ok := i.(*serverKeyExchangeMsg)
  1385. if !ok {
  1386. return false
  1387. }
  1388. return bytes.Equal(m.raw, m1.raw) &&
  1389. bytes.Equal(m.key, m1.key)
  1390. }
  1391. func (m *serverKeyExchangeMsg) marshal() []byte {
  1392. if m.raw != nil {
  1393. return m.raw
  1394. }
  1395. length := len(m.key)
  1396. x := make([]byte, length+4)
  1397. x[0] = typeServerKeyExchange
  1398. x[1] = uint8(length >> 16)
  1399. x[2] = uint8(length >> 8)
  1400. x[3] = uint8(length)
  1401. copy(x[4:], m.key)
  1402. m.raw = x
  1403. return x
  1404. }
  1405. func (m *serverKeyExchangeMsg) unmarshal(data []byte) alert {
  1406. m.raw = data
  1407. if len(data) < 4 {
  1408. return alertDecodeError
  1409. }
  1410. m.key = data[4:]
  1411. return alertSuccess
  1412. }
  1413. type certificateStatusMsg struct {
  1414. raw []byte
  1415. statusType uint8
  1416. response []byte
  1417. }
  1418. func (m *certificateStatusMsg) equal(i interface{}) bool {
  1419. m1, ok := i.(*certificateStatusMsg)
  1420. if !ok {
  1421. return false
  1422. }
  1423. return bytes.Equal(m.raw, m1.raw) &&
  1424. m.statusType == m1.statusType &&
  1425. bytes.Equal(m.response, m1.response)
  1426. }
  1427. func (m *certificateStatusMsg) marshal() []byte {
  1428. if m.raw != nil {
  1429. return m.raw
  1430. }
  1431. var x []byte
  1432. if m.statusType == statusTypeOCSP {
  1433. x = make([]byte, 4+4+len(m.response))
  1434. x[0] = typeCertificateStatus
  1435. l := len(m.response) + 4
  1436. x[1] = byte(l >> 16)
  1437. x[2] = byte(l >> 8)
  1438. x[3] = byte(l)
  1439. x[4] = statusTypeOCSP
  1440. l -= 4
  1441. x[5] = byte(l >> 16)
  1442. x[6] = byte(l >> 8)
  1443. x[7] = byte(l)
  1444. copy(x[8:], m.response)
  1445. } else {
  1446. x = []byte{typeCertificateStatus, 0, 0, 1, m.statusType}
  1447. }
  1448. m.raw = x
  1449. return x
  1450. }
  1451. func (m *certificateStatusMsg) unmarshal(data []byte) alert {
  1452. m.raw = data
  1453. if len(data) < 5 {
  1454. return alertDecodeError
  1455. }
  1456. m.statusType = data[4]
  1457. m.response = nil
  1458. if m.statusType == statusTypeOCSP {
  1459. if len(data) < 8 {
  1460. return alertDecodeError
  1461. }
  1462. respLen := uint32(data[5])<<16 | uint32(data[6])<<8 | uint32(data[7])
  1463. if uint32(len(data)) != 4+4+respLen {
  1464. return alertDecodeError
  1465. }
  1466. m.response = data[8:]
  1467. }
  1468. return alertSuccess
  1469. }
  1470. type serverHelloDoneMsg struct{}
  1471. func (m *serverHelloDoneMsg) equal(i interface{}) bool {
  1472. _, ok := i.(*serverHelloDoneMsg)
  1473. return ok
  1474. }
  1475. func (m *serverHelloDoneMsg) marshal() []byte {
  1476. x := make([]byte, 4)
  1477. x[0] = typeServerHelloDone
  1478. return x
  1479. }
  1480. func (m *serverHelloDoneMsg) unmarshal(data []byte) alert {
  1481. if len(data) != 4 {
  1482. return alertDecodeError
  1483. }
  1484. return alertSuccess
  1485. }
  1486. type clientKeyExchangeMsg struct {
  1487. raw []byte
  1488. ciphertext []byte
  1489. }
  1490. func (m *clientKeyExchangeMsg) equal(i interface{}) bool {
  1491. m1, ok := i.(*clientKeyExchangeMsg)
  1492. if !ok {
  1493. return false
  1494. }
  1495. return bytes.Equal(m.raw, m1.raw) &&
  1496. bytes.Equal(m.ciphertext, m1.ciphertext)
  1497. }
  1498. func (m *clientKeyExchangeMsg) marshal() []byte {
  1499. if m.raw != nil {
  1500. return m.raw
  1501. }
  1502. length := len(m.ciphertext)
  1503. x := make([]byte, length+4)
  1504. x[0] = typeClientKeyExchange
  1505. x[1] = uint8(length >> 16)
  1506. x[2] = uint8(length >> 8)
  1507. x[3] = uint8(length)
  1508. copy(x[4:], m.ciphertext)
  1509. m.raw = x
  1510. return x
  1511. }
  1512. func (m *clientKeyExchangeMsg) unmarshal(data []byte) alert {
  1513. m.raw = data
  1514. if len(data) < 4 {
  1515. return alertDecodeError
  1516. }
  1517. l := int(data[1])<<16 | int(data[2])<<8 | int(data[3])
  1518. if l != len(data)-4 {
  1519. return alertDecodeError
  1520. }
  1521. m.ciphertext = data[4:]
  1522. return alertSuccess
  1523. }
  1524. type finishedMsg struct {
  1525. raw []byte
  1526. verifyData []byte
  1527. }
  1528. func (m *finishedMsg) equal(i interface{}) bool {
  1529. m1, ok := i.(*finishedMsg)
  1530. if !ok {
  1531. return false
  1532. }
  1533. return bytes.Equal(m.raw, m1.raw) &&
  1534. bytes.Equal(m.verifyData, m1.verifyData)
  1535. }
  1536. func (m *finishedMsg) marshal() (x []byte) {
  1537. if m.raw != nil {
  1538. return m.raw
  1539. }
  1540. x = make([]byte, 4+len(m.verifyData))
  1541. x[0] = typeFinished
  1542. x[3] = byte(len(m.verifyData))
  1543. copy(x[4:], m.verifyData)
  1544. m.raw = x
  1545. return
  1546. }
  1547. func (m *finishedMsg) unmarshal(data []byte) alert {
  1548. m.raw = data
  1549. if len(data) < 4 {
  1550. return alertDecodeError
  1551. }
  1552. m.verifyData = data[4:]
  1553. return alertSuccess
  1554. }
  1555. type nextProtoMsg struct {
  1556. raw []byte
  1557. proto string
  1558. }
  1559. func (m *nextProtoMsg) equal(i interface{}) bool {
  1560. m1, ok := i.(*nextProtoMsg)
  1561. if !ok {
  1562. return false
  1563. }
  1564. return bytes.Equal(m.raw, m1.raw) &&
  1565. m.proto == m1.proto
  1566. }
  1567. func (m *nextProtoMsg) marshal() []byte {
  1568. if m.raw != nil {
  1569. return m.raw
  1570. }
  1571. l := len(m.proto)
  1572. if l > 255 {
  1573. l = 255
  1574. }
  1575. padding := 32 - (l+2)%32
  1576. length := l + padding + 2
  1577. x := make([]byte, length+4)
  1578. x[0] = typeNextProtocol
  1579. x[1] = uint8(length >> 16)
  1580. x[2] = uint8(length >> 8)
  1581. x[3] = uint8(length)
  1582. y := x[4:]
  1583. y[0] = byte(l)
  1584. copy(y[1:], []byte(m.proto[0:l]))
  1585. y = y[1+l:]
  1586. y[0] = byte(padding)
  1587. m.raw = x
  1588. return x
  1589. }
  1590. func (m *nextProtoMsg) unmarshal(data []byte) alert {
  1591. m.raw = data
  1592. if len(data) < 5 {
  1593. return alertDecodeError
  1594. }
  1595. data = data[4:]
  1596. protoLen := int(data[0])
  1597. data = data[1:]
  1598. if len(data) < protoLen {
  1599. return alertDecodeError
  1600. }
  1601. m.proto = string(data[0:protoLen])
  1602. data = data[protoLen:]
  1603. if len(data) < 1 {
  1604. return alertDecodeError
  1605. }
  1606. paddingLen := int(data[0])
  1607. data = data[1:]
  1608. if len(data) != paddingLen {
  1609. return alertDecodeError
  1610. }
  1611. return alertSuccess
  1612. }
  1613. type certificateRequestMsg struct {
  1614. raw []byte
  1615. // hasSignatureAndHash indicates whether this message includes a list
  1616. // of signature and hash functions. This change was introduced with TLS
  1617. // 1.2.
  1618. hasSignatureAndHash bool
  1619. certificateTypes []byte
  1620. signatureAndHashes []signatureAndHash
  1621. certificateAuthorities [][]byte
  1622. }
  1623. func (m *certificateRequestMsg) equal(i interface{}) bool {
  1624. m1, ok := i.(*certificateRequestMsg)
  1625. if !ok {
  1626. return false
  1627. }
  1628. return bytes.Equal(m.raw, m1.raw) &&
  1629. bytes.Equal(m.certificateTypes, m1.certificateTypes) &&
  1630. eqByteSlices(m.certificateAuthorities, m1.certificateAuthorities) &&
  1631. eqSignatureAndHashes(m.signatureAndHashes, m1.signatureAndHashes)
  1632. }
  1633. func (m *certificateRequestMsg) marshal() (x []byte) {
  1634. if m.raw != nil {
  1635. return m.raw
  1636. }
  1637. // See http://tools.ietf.org/html/rfc4346#section-7.4.4
  1638. length := 1 + len(m.certificateTypes) + 2
  1639. casLength := 0
  1640. for _, ca := range m.certificateAuthorities {
  1641. casLength += 2 + len(ca)
  1642. }
  1643. length += casLength
  1644. if m.hasSignatureAndHash {
  1645. length += 2 + 2*len(m.signatureAndHashes)
  1646. }
  1647. x = make([]byte, 4+length)
  1648. x[0] = typeCertificateRequest
  1649. x[1] = uint8(length >> 16)
  1650. x[2] = uint8(length >> 8)
  1651. x[3] = uint8(length)
  1652. x[4] = uint8(len(m.certificateTypes))
  1653. copy(x[5:], m.certificateTypes)
  1654. y := x[5+len(m.certificateTypes):]
  1655. if m.hasSignatureAndHash {
  1656. n := len(m.signatureAndHashes) * 2
  1657. y[0] = uint8(n >> 8)
  1658. y[1] = uint8(n)
  1659. y = y[2:]
  1660. for _, sigAndHash := range m.signatureAndHashes {
  1661. y[0] = sigAndHash.hash
  1662. y[1] = sigAndHash.signature
  1663. y = y[2:]
  1664. }
  1665. }
  1666. y[0] = uint8(casLength >> 8)
  1667. y[1] = uint8(casLength)
  1668. y = y[2:]
  1669. for _, ca := range m.certificateAuthorities {
  1670. y[0] = uint8(len(ca) >> 8)
  1671. y[1] = uint8(len(ca))
  1672. y = y[2:]
  1673. copy(y, ca)
  1674. y = y[len(ca):]
  1675. }
  1676. m.raw = x
  1677. return
  1678. }
  1679. func (m *certificateRequestMsg) unmarshal(data []byte) alert {
  1680. m.raw = data
  1681. if len(data) < 5 {
  1682. return alertDecodeError
  1683. }
  1684. length := uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
  1685. if uint32(len(data))-4 != length {
  1686. return alertDecodeError
  1687. }
  1688. numCertTypes := int(data[4])
  1689. data = data[5:]
  1690. if numCertTypes == 0 || len(data) <= numCertTypes {
  1691. return alertDecodeError
  1692. }
  1693. m.certificateTypes = make([]byte, numCertTypes)
  1694. if copy(m.certificateTypes, data) != numCertTypes {
  1695. return alertDecodeError
  1696. }
  1697. data = data[numCertTypes:]
  1698. if m.hasSignatureAndHash {
  1699. if len(data) < 2 {
  1700. return alertDecodeError
  1701. }
  1702. sigAndHashLen := uint16(data[0])<<8 | uint16(data[1])
  1703. data = data[2:]
  1704. if sigAndHashLen&1 != 0 {
  1705. return alertDecodeError
  1706. }
  1707. if len(data) < int(sigAndHashLen) {
  1708. return alertDecodeError
  1709. }
  1710. numSigAndHash := sigAndHashLen / 2
  1711. m.signatureAndHashes = make([]signatureAndHash, numSigAndHash)
  1712. for i := range m.signatureAndHashes {
  1713. m.signatureAndHashes[i].hash = data[0]
  1714. m.signatureAndHashes[i].signature = data[1]
  1715. data = data[2:]
  1716. }
  1717. }
  1718. if len(data) < 2 {
  1719. return alertDecodeError
  1720. }
  1721. casLength := uint16(data[0])<<8 | uint16(data[1])
  1722. data = data[2:]
  1723. if len(data) < int(casLength) {
  1724. return alertDecodeError
  1725. }
  1726. cas := make([]byte, casLength)
  1727. copy(cas, data)
  1728. data = data[casLength:]
  1729. m.certificateAuthorities = nil
  1730. for len(cas) > 0 {
  1731. if len(cas) < 2 {
  1732. return alertDecodeError
  1733. }
  1734. caLen := uint16(cas[0])<<8 | uint16(cas[1])
  1735. cas = cas[2:]
  1736. if len(cas) < int(caLen) {
  1737. return alertDecodeError
  1738. }
  1739. m.certificateAuthorities = append(m.certificateAuthorities, cas[:caLen])
  1740. cas = cas[caLen:]
  1741. }
  1742. if len(data) != 0 {
  1743. return alertDecodeError
  1744. }
  1745. return alertSuccess
  1746. }
  1747. type certificateVerifyMsg struct {
  1748. raw []byte
  1749. hasSignatureAndHash bool
  1750. signatureAndHash signatureAndHash
  1751. signature []byte
  1752. }
  1753. func (m *certificateVerifyMsg) equal(i interface{}) bool {
  1754. m1, ok := i.(*certificateVerifyMsg)
  1755. if !ok {
  1756. return false
  1757. }
  1758. return bytes.Equal(m.raw, m1.raw) &&
  1759. m.hasSignatureAndHash == m1.hasSignatureAndHash &&
  1760. m.signatureAndHash.hash == m1.signatureAndHash.hash &&
  1761. m.signatureAndHash.signature == m1.signatureAndHash.signature &&
  1762. bytes.Equal(m.signature, m1.signature)
  1763. }
  1764. func (m *certificateVerifyMsg) marshal() (x []byte) {
  1765. if m.raw != nil {
  1766. return m.raw
  1767. }
  1768. // See http://tools.ietf.org/html/rfc4346#section-7.4.8
  1769. siglength := len(m.signature)
  1770. length := 2 + siglength
  1771. if m.hasSignatureAndHash {
  1772. length += 2
  1773. }
  1774. x = make([]byte, 4+length)
  1775. x[0] = typeCertificateVerify
  1776. x[1] = uint8(length >> 16)
  1777. x[2] = uint8(length >> 8)
  1778. x[3] = uint8(length)
  1779. y := x[4:]
  1780. if m.hasSignatureAndHash {
  1781. y[0] = m.signatureAndHash.hash
  1782. y[1] = m.signatureAndHash.signature
  1783. y = y[2:]
  1784. }
  1785. y[0] = uint8(siglength >> 8)
  1786. y[1] = uint8(siglength)
  1787. copy(y[2:], m.signature)
  1788. m.raw = x
  1789. return
  1790. }
  1791. func (m *certificateVerifyMsg) unmarshal(data []byte) alert {
  1792. m.raw = data
  1793. if len(data) < 6 {
  1794. return alertDecodeError
  1795. }
  1796. length := uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
  1797. if uint32(len(data))-4 != length {
  1798. return alertDecodeError
  1799. }
  1800. data = data[4:]
  1801. if m.hasSignatureAndHash {
  1802. m.signatureAndHash.hash = data[0]
  1803. m.signatureAndHash.signature = data[1]
  1804. data = data[2:]
  1805. }
  1806. if len(data) < 2 {
  1807. return alertDecodeError
  1808. }
  1809. siglength := int(data[0])<<8 + int(data[1])
  1810. data = data[2:]
  1811. if len(data) != siglength {
  1812. return alertDecodeError
  1813. }
  1814. m.signature = data
  1815. return alertSuccess
  1816. }
  1817. type newSessionTicketMsg struct {
  1818. raw []byte
  1819. ticket []byte
  1820. }
  1821. func (m *newSessionTicketMsg) equal(i interface{}) bool {
  1822. m1, ok := i.(*newSessionTicketMsg)
  1823. if !ok {
  1824. return false
  1825. }
  1826. return bytes.Equal(m.raw, m1.raw) &&
  1827. bytes.Equal(m.ticket, m1.ticket)
  1828. }
  1829. func (m *newSessionTicketMsg) marshal() (x []byte) {
  1830. if m.raw != nil {
  1831. return m.raw
  1832. }
  1833. // See http://tools.ietf.org/html/rfc5077#section-3.3
  1834. ticketLen := len(m.ticket)
  1835. length := 2 + 4 + ticketLen
  1836. x = make([]byte, 4+length)
  1837. x[0] = typeNewSessionTicket
  1838. x[1] = uint8(length >> 16)
  1839. x[2] = uint8(length >> 8)
  1840. x[3] = uint8(length)
  1841. x[8] = uint8(ticketLen >> 8)
  1842. x[9] = uint8(ticketLen)
  1843. copy(x[10:], m.ticket)
  1844. m.raw = x
  1845. return
  1846. }
  1847. func (m *newSessionTicketMsg) unmarshal(data []byte) alert {
  1848. m.raw = data
  1849. if len(data) < 10 {
  1850. return alertDecodeError
  1851. }
  1852. length := uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
  1853. if uint32(len(data))-4 != length {
  1854. return alertDecodeError
  1855. }
  1856. ticketLen := int(data[8])<<8 + int(data[9])
  1857. if len(data)-10 != ticketLen {
  1858. return alertDecodeError
  1859. }
  1860. m.ticket = data[10:]
  1861. return alertSuccess
  1862. }
  1863. type newSessionTicketMsg13 struct {
  1864. raw []byte
  1865. lifetime uint32
  1866. ageAdd uint32
  1867. ticket []byte
  1868. withEarlyDataInfo bool
  1869. maxEarlyDataLength uint32
  1870. }
  1871. func (m *newSessionTicketMsg13) equal(i interface{}) bool {
  1872. m1, ok := i.(*newSessionTicketMsg13)
  1873. if !ok {
  1874. return false
  1875. }
  1876. return bytes.Equal(m.raw, m1.raw) &&
  1877. m.lifetime == m1.lifetime &&
  1878. m.ageAdd == m1.ageAdd &&
  1879. bytes.Equal(m.ticket, m1.ticket) &&
  1880. m.withEarlyDataInfo == m1.withEarlyDataInfo &&
  1881. m.maxEarlyDataLength == m1.maxEarlyDataLength
  1882. }
  1883. func (m *newSessionTicketMsg13) marshal() (x []byte) {
  1884. if m.raw != nil {
  1885. return m.raw
  1886. }
  1887. // See https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6
  1888. ticketLen := len(m.ticket)
  1889. length := 12 + ticketLen
  1890. if m.withEarlyDataInfo {
  1891. length += 8
  1892. }
  1893. x = make([]byte, 4+length)
  1894. x[0] = typeNewSessionTicket
  1895. x[1] = uint8(length >> 16)
  1896. x[2] = uint8(length >> 8)
  1897. x[3] = uint8(length)
  1898. x[4] = uint8(m.lifetime >> 24)
  1899. x[5] = uint8(m.lifetime >> 16)
  1900. x[6] = uint8(m.lifetime >> 8)
  1901. x[7] = uint8(m.lifetime)
  1902. x[8] = uint8(m.ageAdd >> 24)
  1903. x[9] = uint8(m.ageAdd >> 16)
  1904. x[10] = uint8(m.ageAdd >> 8)
  1905. x[11] = uint8(m.ageAdd)
  1906. x[12] = uint8(ticketLen >> 8)
  1907. x[13] = uint8(ticketLen)
  1908. copy(x[14:], m.ticket)
  1909. if m.withEarlyDataInfo {
  1910. z := x[14+ticketLen:]
  1911. z[1] = 8
  1912. z[2] = uint8(extensionTicketEarlyDataInfo >> 8)
  1913. z[3] = uint8(extensionTicketEarlyDataInfo)
  1914. z[5] = 4
  1915. z[6] = uint8(m.maxEarlyDataLength >> 24)
  1916. z[7] = uint8(m.maxEarlyDataLength >> 16)
  1917. z[8] = uint8(m.maxEarlyDataLength >> 8)
  1918. z[9] = uint8(m.maxEarlyDataLength)
  1919. }
  1920. m.raw = x
  1921. return
  1922. }
  1923. func (m *newSessionTicketMsg13) unmarshal(data []byte) alert {
  1924. m.raw = data
  1925. m.maxEarlyDataLength = 0
  1926. m.withEarlyDataInfo = false
  1927. if len(data) < 16 {
  1928. return alertDecodeError
  1929. }
  1930. length := uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
  1931. if uint32(len(data))-4 != length {
  1932. return alertDecodeError
  1933. }
  1934. m.lifetime = uint32(data[4])<<24 | uint32(data[5])<<16 |
  1935. uint32(data[6])<<8 | uint32(data[7])
  1936. m.ageAdd = uint32(data[8])<<24 | uint32(data[9])<<16 |
  1937. uint32(data[10])<<8 | uint32(data[11])
  1938. ticketLen := int(data[12])<<8 + int(data[13])
  1939. if 14+ticketLen > len(data) {
  1940. return alertDecodeError
  1941. }
  1942. m.ticket = data[14 : 14+ticketLen]
  1943. data = data[14+ticketLen:]
  1944. extLen := int(data[0])<<8 + int(data[1])
  1945. if extLen != len(data)-2 {
  1946. return alertDecodeError
  1947. }
  1948. data = data[2:]
  1949. for len(data) > 0 {
  1950. if len(data) < 4 {
  1951. return alertDecodeError
  1952. }
  1953. extType := uint16(data[0])<<8 + uint16(data[1])
  1954. length := int(data[2])<<8 + int(data[3])
  1955. data = data[4:]
  1956. switch extType {
  1957. case extensionTicketEarlyDataInfo:
  1958. if length != 4 {
  1959. return alertDecodeError
  1960. }
  1961. m.withEarlyDataInfo = true
  1962. m.maxEarlyDataLength = uint32(data[0])<<24 | uint32(data[1])<<16 |
  1963. uint32(data[2])<<8 | uint32(data[3])
  1964. }
  1965. data = data[length:]
  1966. }
  1967. return alertSuccess
  1968. }
  1969. type helloRequestMsg struct {
  1970. }
  1971. func (*helloRequestMsg) marshal() []byte {
  1972. return []byte{typeHelloRequest, 0, 0, 0}
  1973. }
  1974. func (*helloRequestMsg) unmarshal(data []byte) alert {
  1975. if len(data) != 4 {
  1976. return alertDecodeError
  1977. }
  1978. return alertSuccess
  1979. }
  1980. func eqUint16s(x, y []uint16) bool {
  1981. if len(x) != len(y) {
  1982. return false
  1983. }
  1984. for i, v := range x {
  1985. if y[i] != v {
  1986. return false
  1987. }
  1988. }
  1989. return true
  1990. }
  1991. func eqCurveIDs(x, y []CurveID) bool {
  1992. if len(x) != len(y) {
  1993. return false
  1994. }
  1995. for i, v := range x {
  1996. if y[i] != v {
  1997. return false
  1998. }
  1999. }
  2000. return true
  2001. }
  2002. func eqStrings(x, y []string) bool {
  2003. if len(x) != len(y) {
  2004. return false
  2005. }
  2006. for i, v := range x {
  2007. if y[i] != v {
  2008. return false
  2009. }
  2010. }
  2011. return true
  2012. }
  2013. func eqByteSlices(x, y [][]byte) bool {
  2014. if len(x) != len(y) {
  2015. return false
  2016. }
  2017. for i, v := range x {
  2018. if !bytes.Equal(v, y[i]) {
  2019. return false
  2020. }
  2021. }
  2022. return true
  2023. }
  2024. func eqSignatureAndHashes(x, y []signatureAndHash) bool {
  2025. if len(x) != len(y) {
  2026. return false
  2027. }
  2028. for i, v := range x {
  2029. v2 := y[i]
  2030. if v.hash != v2.hash || v.signature != v2.signature {
  2031. return false
  2032. }
  2033. }
  2034. return true
  2035. }
  2036. func eqKeyShares(x, y []keyShare) bool {
  2037. if len(x) != len(y) {
  2038. return false
  2039. }
  2040. for i := range x {
  2041. if x[i].group != y[i].group {
  2042. return false
  2043. }
  2044. if !bytes.Equal(x[i].data, y[i].data) {
  2045. return false
  2046. }
  2047. }
  2048. return true
  2049. }