Alternative TLS implementation in Go
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

2399 рядки
52 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 certificateEntry struct {
  1283. data []byte
  1284. ocspStaple []byte
  1285. sctList [][]byte
  1286. }
  1287. type certificateMsg13 struct {
  1288. raw []byte
  1289. requestContext []byte
  1290. certificates []certificateEntry
  1291. }
  1292. func (m *certificateMsg13) equal(i interface{}) bool {
  1293. m1, ok := i.(*certificateMsg13)
  1294. if !ok {
  1295. return false
  1296. }
  1297. if len(m.certificates) != len(m1.certificates) {
  1298. return false
  1299. }
  1300. for i, _ := range m.certificates {
  1301. ok := bytes.Equal(m.certificates[i].data, m1.certificates[i].data)
  1302. ok = ok && bytes.Equal(m.certificates[i].ocspStaple, m1.certificates[i].ocspStaple)
  1303. ok = ok && eqByteSlices(m.certificates[i].sctList, m1.certificates[i].sctList)
  1304. if !ok {
  1305. return false
  1306. }
  1307. }
  1308. return bytes.Equal(m.raw, m1.raw) &&
  1309. bytes.Equal(m.requestContext, m1.requestContext)
  1310. }
  1311. func (m *certificateMsg13) marshal() (x []byte) {
  1312. if m.raw != nil {
  1313. return m.raw
  1314. }
  1315. var i int
  1316. for _, cert := range m.certificates {
  1317. i += len(cert.data)
  1318. if len(cert.ocspStaple) != 0 {
  1319. i += 8 + len(cert.ocspStaple)
  1320. }
  1321. if len(cert.sctList) != 0 {
  1322. i += 4
  1323. for _, sct := range cert.sctList {
  1324. i += 2 + len(sct)
  1325. }
  1326. }
  1327. }
  1328. length := 3 + 3*len(m.certificates) + i
  1329. length += 2 * len(m.certificates) // extensions
  1330. length += 1 + len(m.requestContext)
  1331. x = make([]byte, 4+length)
  1332. x[0] = typeCertificate
  1333. x[1] = uint8(length >> 16)
  1334. x[2] = uint8(length >> 8)
  1335. x[3] = uint8(length)
  1336. z := x[4:]
  1337. z[0] = byte(len(m.requestContext))
  1338. copy(z[1:], m.requestContext)
  1339. z = z[1+len(m.requestContext):]
  1340. certificateOctets := len(z) - 3
  1341. z[0] = uint8(certificateOctets >> 16)
  1342. z[1] = uint8(certificateOctets >> 8)
  1343. z[2] = uint8(certificateOctets)
  1344. z = z[3:]
  1345. for _, cert := range m.certificates {
  1346. z[0] = uint8(len(cert.data) >> 16)
  1347. z[1] = uint8(len(cert.data) >> 8)
  1348. z[2] = uint8(len(cert.data))
  1349. copy(z[3:], cert.data)
  1350. z = z[3+len(cert.data):]
  1351. extLenPos := z[:2]
  1352. z = z[2:]
  1353. extensionLen := 0
  1354. if len(cert.ocspStaple) != 0 {
  1355. stapleLen := 4 + len(cert.ocspStaple)
  1356. z[0] = uint8(extensionStatusRequest >> 8)
  1357. z[1] = uint8(extensionStatusRequest)
  1358. z[2] = uint8(stapleLen >> 8)
  1359. z[3] = uint8(stapleLen)
  1360. stapleLen -= 4
  1361. z[4] = statusTypeOCSP
  1362. z[5] = uint8(stapleLen >> 16)
  1363. z[6] = uint8(stapleLen >> 8)
  1364. z[7] = uint8(stapleLen)
  1365. copy(z[8:], cert.ocspStaple)
  1366. z = z[8+stapleLen:]
  1367. extensionLen += 8 + stapleLen
  1368. }
  1369. if len(cert.sctList) != 0 {
  1370. z[0] = uint8(extensionSCT >> 8)
  1371. z[1] = uint8(extensionSCT)
  1372. sctLenPos := z[2:4]
  1373. z = z[4:]
  1374. extensionLen += 4
  1375. sctLen := 0
  1376. for _, sct := range cert.sctList {
  1377. z[0] = uint8(len(sct) >> 8)
  1378. z[1] = uint8(len(sct))
  1379. copy(z[2:], sct)
  1380. z = z[2+len(sct):]
  1381. extensionLen += 2 + len(sct)
  1382. sctLen += 2 + len(sct)
  1383. }
  1384. sctLenPos[0] = uint8(sctLen >> 8)
  1385. sctLenPos[1] = uint8(sctLen)
  1386. }
  1387. extLenPos[0] = uint8(extensionLen >> 8)
  1388. extLenPos[1] = uint8(extensionLen)
  1389. }
  1390. m.raw = x
  1391. return
  1392. }
  1393. func (m *certificateMsg13) unmarshal(data []byte) alert {
  1394. if len(data) < 5 {
  1395. return alertDecodeError
  1396. }
  1397. m.raw = data
  1398. ctxLen := data[4]
  1399. if len(data) < int(ctxLen)+5+3 {
  1400. return alertDecodeError
  1401. }
  1402. m.requestContext = data[5 : 5+ctxLen]
  1403. d := data[5+ctxLen:]
  1404. certsLen := uint32(d[0])<<16 | uint32(d[1])<<8 | uint32(d[2])
  1405. if uint32(len(d)) != certsLen+3 {
  1406. return alertDecodeError
  1407. }
  1408. numCerts := 0
  1409. d = d[3:]
  1410. for certsLen > 0 {
  1411. if len(d) < 4 {
  1412. return alertDecodeError
  1413. }
  1414. certLen := uint32(d[0])<<16 | uint32(d[1])<<8 | uint32(d[2])
  1415. if uint32(len(d)) < 3+certLen {
  1416. return alertDecodeError
  1417. }
  1418. d = d[3+certLen:]
  1419. if len(d) < 2 {
  1420. return alertDecodeError
  1421. }
  1422. extLen := uint16(d[0])<<8 | uint16(d[1])
  1423. if uint16(len(d)) < 2+extLen {
  1424. return alertDecodeError
  1425. }
  1426. d = d[2+extLen:]
  1427. certsLen -= 3 + certLen + 2 + uint32(extLen)
  1428. numCerts++
  1429. }
  1430. m.certificates = make([]certificateEntry, numCerts)
  1431. d = data[8+ctxLen:]
  1432. for i := 0; i < numCerts; i++ {
  1433. certLen := uint32(d[0])<<16 | uint32(d[1])<<8 | uint32(d[2])
  1434. m.certificates[i].data = d[3 : 3+certLen]
  1435. d = d[3+certLen:]
  1436. extLen := uint16(d[0])<<8 | uint16(d[1])
  1437. d = d[2:]
  1438. for extLen > 0 {
  1439. if extLen < 4 {
  1440. return alertDecodeError
  1441. }
  1442. typ := uint16(d[0])<<8 | uint16(d[1])
  1443. bodyLen := uint16(d[2])<<8 | uint16(d[3])
  1444. if extLen < 4+bodyLen {
  1445. return alertDecodeError
  1446. }
  1447. body := d[4 : 4+bodyLen]
  1448. d = d[4+bodyLen:]
  1449. extLen -= 4 + bodyLen
  1450. switch typ {
  1451. case extensionStatusRequest:
  1452. if len(body) < 4 || body[0] != 0x01 {
  1453. return alertDecodeError
  1454. }
  1455. ocspLen := int(body[1])<<16 | int(body[2])<<8 | int(body[3])
  1456. if len(body) != 4+ocspLen {
  1457. return alertDecodeError
  1458. }
  1459. m.certificates[i].ocspStaple = body[4:]
  1460. case extensionSCT:
  1461. for len(body) > 0 {
  1462. if len(body) < 2 {
  1463. return alertDecodeError
  1464. }
  1465. sctLen := int(body[0]<<8) | int(body[1])
  1466. if len(body) < 2+sctLen {
  1467. return alertDecodeError
  1468. }
  1469. m.certificates[i].sctList = append(m.certificates[i].sctList, body[2:2+sctLen])
  1470. body = body[2+sctLen:]
  1471. }
  1472. }
  1473. }
  1474. }
  1475. return alertSuccess
  1476. }
  1477. type serverKeyExchangeMsg struct {
  1478. raw []byte
  1479. key []byte
  1480. }
  1481. func (m *serverKeyExchangeMsg) equal(i interface{}) bool {
  1482. m1, ok := i.(*serverKeyExchangeMsg)
  1483. if !ok {
  1484. return false
  1485. }
  1486. return bytes.Equal(m.raw, m1.raw) &&
  1487. bytes.Equal(m.key, m1.key)
  1488. }
  1489. func (m *serverKeyExchangeMsg) marshal() []byte {
  1490. if m.raw != nil {
  1491. return m.raw
  1492. }
  1493. length := len(m.key)
  1494. x := make([]byte, length+4)
  1495. x[0] = typeServerKeyExchange
  1496. x[1] = uint8(length >> 16)
  1497. x[2] = uint8(length >> 8)
  1498. x[3] = uint8(length)
  1499. copy(x[4:], m.key)
  1500. m.raw = x
  1501. return x
  1502. }
  1503. func (m *serverKeyExchangeMsg) unmarshal(data []byte) alert {
  1504. m.raw = data
  1505. if len(data) < 4 {
  1506. return alertDecodeError
  1507. }
  1508. m.key = data[4:]
  1509. return alertSuccess
  1510. }
  1511. type certificateStatusMsg struct {
  1512. raw []byte
  1513. statusType uint8
  1514. response []byte
  1515. }
  1516. func (m *certificateStatusMsg) equal(i interface{}) bool {
  1517. m1, ok := i.(*certificateStatusMsg)
  1518. if !ok {
  1519. return false
  1520. }
  1521. return bytes.Equal(m.raw, m1.raw) &&
  1522. m.statusType == m1.statusType &&
  1523. bytes.Equal(m.response, m1.response)
  1524. }
  1525. func (m *certificateStatusMsg) marshal() []byte {
  1526. if m.raw != nil {
  1527. return m.raw
  1528. }
  1529. var x []byte
  1530. if m.statusType == statusTypeOCSP {
  1531. x = make([]byte, 4+4+len(m.response))
  1532. x[0] = typeCertificateStatus
  1533. l := len(m.response) + 4
  1534. x[1] = byte(l >> 16)
  1535. x[2] = byte(l >> 8)
  1536. x[3] = byte(l)
  1537. x[4] = statusTypeOCSP
  1538. l -= 4
  1539. x[5] = byte(l >> 16)
  1540. x[6] = byte(l >> 8)
  1541. x[7] = byte(l)
  1542. copy(x[8:], m.response)
  1543. } else {
  1544. x = []byte{typeCertificateStatus, 0, 0, 1, m.statusType}
  1545. }
  1546. m.raw = x
  1547. return x
  1548. }
  1549. func (m *certificateStatusMsg) unmarshal(data []byte) alert {
  1550. m.raw = data
  1551. if len(data) < 5 {
  1552. return alertDecodeError
  1553. }
  1554. m.statusType = data[4]
  1555. m.response = nil
  1556. if m.statusType == statusTypeOCSP {
  1557. if len(data) < 8 {
  1558. return alertDecodeError
  1559. }
  1560. respLen := uint32(data[5])<<16 | uint32(data[6])<<8 | uint32(data[7])
  1561. if uint32(len(data)) != 4+4+respLen {
  1562. return alertDecodeError
  1563. }
  1564. m.response = data[8:]
  1565. }
  1566. return alertSuccess
  1567. }
  1568. type serverHelloDoneMsg struct{}
  1569. func (m *serverHelloDoneMsg) equal(i interface{}) bool {
  1570. _, ok := i.(*serverHelloDoneMsg)
  1571. return ok
  1572. }
  1573. func (m *serverHelloDoneMsg) marshal() []byte {
  1574. x := make([]byte, 4)
  1575. x[0] = typeServerHelloDone
  1576. return x
  1577. }
  1578. func (m *serverHelloDoneMsg) unmarshal(data []byte) alert {
  1579. if len(data) != 4 {
  1580. return alertDecodeError
  1581. }
  1582. return alertSuccess
  1583. }
  1584. type clientKeyExchangeMsg struct {
  1585. raw []byte
  1586. ciphertext []byte
  1587. }
  1588. func (m *clientKeyExchangeMsg) equal(i interface{}) bool {
  1589. m1, ok := i.(*clientKeyExchangeMsg)
  1590. if !ok {
  1591. return false
  1592. }
  1593. return bytes.Equal(m.raw, m1.raw) &&
  1594. bytes.Equal(m.ciphertext, m1.ciphertext)
  1595. }
  1596. func (m *clientKeyExchangeMsg) marshal() []byte {
  1597. if m.raw != nil {
  1598. return m.raw
  1599. }
  1600. length := len(m.ciphertext)
  1601. x := make([]byte, length+4)
  1602. x[0] = typeClientKeyExchange
  1603. x[1] = uint8(length >> 16)
  1604. x[2] = uint8(length >> 8)
  1605. x[3] = uint8(length)
  1606. copy(x[4:], m.ciphertext)
  1607. m.raw = x
  1608. return x
  1609. }
  1610. func (m *clientKeyExchangeMsg) unmarshal(data []byte) alert {
  1611. m.raw = data
  1612. if len(data) < 4 {
  1613. return alertDecodeError
  1614. }
  1615. l := int(data[1])<<16 | int(data[2])<<8 | int(data[3])
  1616. if l != len(data)-4 {
  1617. return alertDecodeError
  1618. }
  1619. m.ciphertext = data[4:]
  1620. return alertSuccess
  1621. }
  1622. type finishedMsg struct {
  1623. raw []byte
  1624. verifyData []byte
  1625. }
  1626. func (m *finishedMsg) equal(i interface{}) bool {
  1627. m1, ok := i.(*finishedMsg)
  1628. if !ok {
  1629. return false
  1630. }
  1631. return bytes.Equal(m.raw, m1.raw) &&
  1632. bytes.Equal(m.verifyData, m1.verifyData)
  1633. }
  1634. func (m *finishedMsg) marshal() (x []byte) {
  1635. if m.raw != nil {
  1636. return m.raw
  1637. }
  1638. x = make([]byte, 4+len(m.verifyData))
  1639. x[0] = typeFinished
  1640. x[3] = byte(len(m.verifyData))
  1641. copy(x[4:], m.verifyData)
  1642. m.raw = x
  1643. return
  1644. }
  1645. func (m *finishedMsg) unmarshal(data []byte) alert {
  1646. m.raw = data
  1647. if len(data) < 4 {
  1648. return alertDecodeError
  1649. }
  1650. m.verifyData = data[4:]
  1651. return alertSuccess
  1652. }
  1653. type nextProtoMsg struct {
  1654. raw []byte
  1655. proto string
  1656. }
  1657. func (m *nextProtoMsg) equal(i interface{}) bool {
  1658. m1, ok := i.(*nextProtoMsg)
  1659. if !ok {
  1660. return false
  1661. }
  1662. return bytes.Equal(m.raw, m1.raw) &&
  1663. m.proto == m1.proto
  1664. }
  1665. func (m *nextProtoMsg) marshal() []byte {
  1666. if m.raw != nil {
  1667. return m.raw
  1668. }
  1669. l := len(m.proto)
  1670. if l > 255 {
  1671. l = 255
  1672. }
  1673. padding := 32 - (l+2)%32
  1674. length := l + padding + 2
  1675. x := make([]byte, length+4)
  1676. x[0] = typeNextProtocol
  1677. x[1] = uint8(length >> 16)
  1678. x[2] = uint8(length >> 8)
  1679. x[3] = uint8(length)
  1680. y := x[4:]
  1681. y[0] = byte(l)
  1682. copy(y[1:], []byte(m.proto[0:l]))
  1683. y = y[1+l:]
  1684. y[0] = byte(padding)
  1685. m.raw = x
  1686. return x
  1687. }
  1688. func (m *nextProtoMsg) unmarshal(data []byte) alert {
  1689. m.raw = data
  1690. if len(data) < 5 {
  1691. return alertDecodeError
  1692. }
  1693. data = data[4:]
  1694. protoLen := int(data[0])
  1695. data = data[1:]
  1696. if len(data) < protoLen {
  1697. return alertDecodeError
  1698. }
  1699. m.proto = string(data[0:protoLen])
  1700. data = data[protoLen:]
  1701. if len(data) < 1 {
  1702. return alertDecodeError
  1703. }
  1704. paddingLen := int(data[0])
  1705. data = data[1:]
  1706. if len(data) != paddingLen {
  1707. return alertDecodeError
  1708. }
  1709. return alertSuccess
  1710. }
  1711. type certificateRequestMsg struct {
  1712. raw []byte
  1713. // hasSignatureAndHash indicates whether this message includes a list
  1714. // of signature and hash functions. This change was introduced with TLS
  1715. // 1.2.
  1716. hasSignatureAndHash bool
  1717. certificateTypes []byte
  1718. signatureAndHashes []signatureAndHash
  1719. certificateAuthorities [][]byte
  1720. }
  1721. func (m *certificateRequestMsg) equal(i interface{}) bool {
  1722. m1, ok := i.(*certificateRequestMsg)
  1723. if !ok {
  1724. return false
  1725. }
  1726. return bytes.Equal(m.raw, m1.raw) &&
  1727. bytes.Equal(m.certificateTypes, m1.certificateTypes) &&
  1728. eqByteSlices(m.certificateAuthorities, m1.certificateAuthorities) &&
  1729. eqSignatureAndHashes(m.signatureAndHashes, m1.signatureAndHashes)
  1730. }
  1731. func (m *certificateRequestMsg) marshal() (x []byte) {
  1732. if m.raw != nil {
  1733. return m.raw
  1734. }
  1735. // See http://tools.ietf.org/html/rfc4346#section-7.4.4
  1736. length := 1 + len(m.certificateTypes) + 2
  1737. casLength := 0
  1738. for _, ca := range m.certificateAuthorities {
  1739. casLength += 2 + len(ca)
  1740. }
  1741. length += casLength
  1742. if m.hasSignatureAndHash {
  1743. length += 2 + 2*len(m.signatureAndHashes)
  1744. }
  1745. x = make([]byte, 4+length)
  1746. x[0] = typeCertificateRequest
  1747. x[1] = uint8(length >> 16)
  1748. x[2] = uint8(length >> 8)
  1749. x[3] = uint8(length)
  1750. x[4] = uint8(len(m.certificateTypes))
  1751. copy(x[5:], m.certificateTypes)
  1752. y := x[5+len(m.certificateTypes):]
  1753. if m.hasSignatureAndHash {
  1754. n := len(m.signatureAndHashes) * 2
  1755. y[0] = uint8(n >> 8)
  1756. y[1] = uint8(n)
  1757. y = y[2:]
  1758. for _, sigAndHash := range m.signatureAndHashes {
  1759. y[0] = sigAndHash.hash
  1760. y[1] = sigAndHash.signature
  1761. y = y[2:]
  1762. }
  1763. }
  1764. y[0] = uint8(casLength >> 8)
  1765. y[1] = uint8(casLength)
  1766. y = y[2:]
  1767. for _, ca := range m.certificateAuthorities {
  1768. y[0] = uint8(len(ca) >> 8)
  1769. y[1] = uint8(len(ca))
  1770. y = y[2:]
  1771. copy(y, ca)
  1772. y = y[len(ca):]
  1773. }
  1774. m.raw = x
  1775. return
  1776. }
  1777. func (m *certificateRequestMsg) unmarshal(data []byte) alert {
  1778. m.raw = data
  1779. if len(data) < 5 {
  1780. return alertDecodeError
  1781. }
  1782. length := uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
  1783. if uint32(len(data))-4 != length {
  1784. return alertDecodeError
  1785. }
  1786. numCertTypes := int(data[4])
  1787. data = data[5:]
  1788. if numCertTypes == 0 || len(data) <= numCertTypes {
  1789. return alertDecodeError
  1790. }
  1791. m.certificateTypes = make([]byte, numCertTypes)
  1792. if copy(m.certificateTypes, data) != numCertTypes {
  1793. return alertDecodeError
  1794. }
  1795. data = data[numCertTypes:]
  1796. if m.hasSignatureAndHash {
  1797. if len(data) < 2 {
  1798. return alertDecodeError
  1799. }
  1800. sigAndHashLen := uint16(data[0])<<8 | uint16(data[1])
  1801. data = data[2:]
  1802. if sigAndHashLen&1 != 0 {
  1803. return alertDecodeError
  1804. }
  1805. if len(data) < int(sigAndHashLen) {
  1806. return alertDecodeError
  1807. }
  1808. numSigAndHash := sigAndHashLen / 2
  1809. m.signatureAndHashes = make([]signatureAndHash, numSigAndHash)
  1810. for i := range m.signatureAndHashes {
  1811. m.signatureAndHashes[i].hash = data[0]
  1812. m.signatureAndHashes[i].signature = data[1]
  1813. data = data[2:]
  1814. }
  1815. }
  1816. if len(data) < 2 {
  1817. return alertDecodeError
  1818. }
  1819. casLength := uint16(data[0])<<8 | uint16(data[1])
  1820. data = data[2:]
  1821. if len(data) < int(casLength) {
  1822. return alertDecodeError
  1823. }
  1824. cas := make([]byte, casLength)
  1825. copy(cas, data)
  1826. data = data[casLength:]
  1827. m.certificateAuthorities = nil
  1828. for len(cas) > 0 {
  1829. if len(cas) < 2 {
  1830. return alertDecodeError
  1831. }
  1832. caLen := uint16(cas[0])<<8 | uint16(cas[1])
  1833. cas = cas[2:]
  1834. if len(cas) < int(caLen) {
  1835. return alertDecodeError
  1836. }
  1837. m.certificateAuthorities = append(m.certificateAuthorities, cas[:caLen])
  1838. cas = cas[caLen:]
  1839. }
  1840. if len(data) != 0 {
  1841. return alertDecodeError
  1842. }
  1843. return alertSuccess
  1844. }
  1845. type certificateVerifyMsg struct {
  1846. raw []byte
  1847. hasSignatureAndHash bool
  1848. signatureAndHash signatureAndHash
  1849. signature []byte
  1850. }
  1851. func (m *certificateVerifyMsg) equal(i interface{}) bool {
  1852. m1, ok := i.(*certificateVerifyMsg)
  1853. if !ok {
  1854. return false
  1855. }
  1856. return bytes.Equal(m.raw, m1.raw) &&
  1857. m.hasSignatureAndHash == m1.hasSignatureAndHash &&
  1858. m.signatureAndHash.hash == m1.signatureAndHash.hash &&
  1859. m.signatureAndHash.signature == m1.signatureAndHash.signature &&
  1860. bytes.Equal(m.signature, m1.signature)
  1861. }
  1862. func (m *certificateVerifyMsg) marshal() (x []byte) {
  1863. if m.raw != nil {
  1864. return m.raw
  1865. }
  1866. // See http://tools.ietf.org/html/rfc4346#section-7.4.8
  1867. siglength := len(m.signature)
  1868. length := 2 + siglength
  1869. if m.hasSignatureAndHash {
  1870. length += 2
  1871. }
  1872. x = make([]byte, 4+length)
  1873. x[0] = typeCertificateVerify
  1874. x[1] = uint8(length >> 16)
  1875. x[2] = uint8(length >> 8)
  1876. x[3] = uint8(length)
  1877. y := x[4:]
  1878. if m.hasSignatureAndHash {
  1879. y[0] = m.signatureAndHash.hash
  1880. y[1] = m.signatureAndHash.signature
  1881. y = y[2:]
  1882. }
  1883. y[0] = uint8(siglength >> 8)
  1884. y[1] = uint8(siglength)
  1885. copy(y[2:], m.signature)
  1886. m.raw = x
  1887. return
  1888. }
  1889. func (m *certificateVerifyMsg) unmarshal(data []byte) alert {
  1890. m.raw = data
  1891. if len(data) < 6 {
  1892. return alertDecodeError
  1893. }
  1894. length := uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
  1895. if uint32(len(data))-4 != length {
  1896. return alertDecodeError
  1897. }
  1898. data = data[4:]
  1899. if m.hasSignatureAndHash {
  1900. m.signatureAndHash.hash = data[0]
  1901. m.signatureAndHash.signature = data[1]
  1902. data = data[2:]
  1903. }
  1904. if len(data) < 2 {
  1905. return alertDecodeError
  1906. }
  1907. siglength := int(data[0])<<8 + int(data[1])
  1908. data = data[2:]
  1909. if len(data) != siglength {
  1910. return alertDecodeError
  1911. }
  1912. m.signature = data
  1913. return alertSuccess
  1914. }
  1915. type newSessionTicketMsg struct {
  1916. raw []byte
  1917. ticket []byte
  1918. }
  1919. func (m *newSessionTicketMsg) equal(i interface{}) bool {
  1920. m1, ok := i.(*newSessionTicketMsg)
  1921. if !ok {
  1922. return false
  1923. }
  1924. return bytes.Equal(m.raw, m1.raw) &&
  1925. bytes.Equal(m.ticket, m1.ticket)
  1926. }
  1927. func (m *newSessionTicketMsg) marshal() (x []byte) {
  1928. if m.raw != nil {
  1929. return m.raw
  1930. }
  1931. // See http://tools.ietf.org/html/rfc5077#section-3.3
  1932. ticketLen := len(m.ticket)
  1933. length := 2 + 4 + ticketLen
  1934. x = make([]byte, 4+length)
  1935. x[0] = typeNewSessionTicket
  1936. x[1] = uint8(length >> 16)
  1937. x[2] = uint8(length >> 8)
  1938. x[3] = uint8(length)
  1939. x[8] = uint8(ticketLen >> 8)
  1940. x[9] = uint8(ticketLen)
  1941. copy(x[10:], m.ticket)
  1942. m.raw = x
  1943. return
  1944. }
  1945. func (m *newSessionTicketMsg) unmarshal(data []byte) alert {
  1946. m.raw = data
  1947. if len(data) < 10 {
  1948. return alertDecodeError
  1949. }
  1950. length := uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
  1951. if uint32(len(data))-4 != length {
  1952. return alertDecodeError
  1953. }
  1954. ticketLen := int(data[8])<<8 + int(data[9])
  1955. if len(data)-10 != ticketLen {
  1956. return alertDecodeError
  1957. }
  1958. m.ticket = data[10:]
  1959. return alertSuccess
  1960. }
  1961. type newSessionTicketMsg13 struct {
  1962. raw []byte
  1963. lifetime uint32
  1964. ageAdd uint32
  1965. ticket []byte
  1966. withEarlyDataInfo bool
  1967. maxEarlyDataLength uint32
  1968. }
  1969. func (m *newSessionTicketMsg13) equal(i interface{}) bool {
  1970. m1, ok := i.(*newSessionTicketMsg13)
  1971. if !ok {
  1972. return false
  1973. }
  1974. return bytes.Equal(m.raw, m1.raw) &&
  1975. m.lifetime == m1.lifetime &&
  1976. m.ageAdd == m1.ageAdd &&
  1977. bytes.Equal(m.ticket, m1.ticket) &&
  1978. m.withEarlyDataInfo == m1.withEarlyDataInfo &&
  1979. m.maxEarlyDataLength == m1.maxEarlyDataLength
  1980. }
  1981. func (m *newSessionTicketMsg13) marshal() (x []byte) {
  1982. if m.raw != nil {
  1983. return m.raw
  1984. }
  1985. // See https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6
  1986. ticketLen := len(m.ticket)
  1987. length := 12 + ticketLen
  1988. if m.withEarlyDataInfo {
  1989. length += 8
  1990. }
  1991. x = make([]byte, 4+length)
  1992. x[0] = typeNewSessionTicket
  1993. x[1] = uint8(length >> 16)
  1994. x[2] = uint8(length >> 8)
  1995. x[3] = uint8(length)
  1996. x[4] = uint8(m.lifetime >> 24)
  1997. x[5] = uint8(m.lifetime >> 16)
  1998. x[6] = uint8(m.lifetime >> 8)
  1999. x[7] = uint8(m.lifetime)
  2000. x[8] = uint8(m.ageAdd >> 24)
  2001. x[9] = uint8(m.ageAdd >> 16)
  2002. x[10] = uint8(m.ageAdd >> 8)
  2003. x[11] = uint8(m.ageAdd)
  2004. x[12] = uint8(ticketLen >> 8)
  2005. x[13] = uint8(ticketLen)
  2006. copy(x[14:], m.ticket)
  2007. if m.withEarlyDataInfo {
  2008. z := x[14+ticketLen:]
  2009. z[1] = 8
  2010. z[2] = uint8(extensionTicketEarlyDataInfo >> 8)
  2011. z[3] = uint8(extensionTicketEarlyDataInfo)
  2012. z[5] = 4
  2013. z[6] = uint8(m.maxEarlyDataLength >> 24)
  2014. z[7] = uint8(m.maxEarlyDataLength >> 16)
  2015. z[8] = uint8(m.maxEarlyDataLength >> 8)
  2016. z[9] = uint8(m.maxEarlyDataLength)
  2017. }
  2018. m.raw = x
  2019. return
  2020. }
  2021. func (m *newSessionTicketMsg13) unmarshal(data []byte) alert {
  2022. m.raw = data
  2023. m.maxEarlyDataLength = 0
  2024. m.withEarlyDataInfo = false
  2025. if len(data) < 16 {
  2026. return alertDecodeError
  2027. }
  2028. length := uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
  2029. if uint32(len(data))-4 != length {
  2030. return alertDecodeError
  2031. }
  2032. m.lifetime = uint32(data[4])<<24 | uint32(data[5])<<16 |
  2033. uint32(data[6])<<8 | uint32(data[7])
  2034. m.ageAdd = uint32(data[8])<<24 | uint32(data[9])<<16 |
  2035. uint32(data[10])<<8 | uint32(data[11])
  2036. ticketLen := int(data[12])<<8 + int(data[13])
  2037. if 14+ticketLen > len(data) {
  2038. return alertDecodeError
  2039. }
  2040. m.ticket = data[14 : 14+ticketLen]
  2041. data = data[14+ticketLen:]
  2042. extLen := int(data[0])<<8 + int(data[1])
  2043. if extLen != len(data)-2 {
  2044. return alertDecodeError
  2045. }
  2046. data = data[2:]
  2047. for len(data) > 0 {
  2048. if len(data) < 4 {
  2049. return alertDecodeError
  2050. }
  2051. extType := uint16(data[0])<<8 + uint16(data[1])
  2052. length := int(data[2])<<8 + int(data[3])
  2053. data = data[4:]
  2054. switch extType {
  2055. case extensionTicketEarlyDataInfo:
  2056. if length != 4 {
  2057. return alertDecodeError
  2058. }
  2059. m.withEarlyDataInfo = true
  2060. m.maxEarlyDataLength = uint32(data[0])<<24 | uint32(data[1])<<16 |
  2061. uint32(data[2])<<8 | uint32(data[3])
  2062. }
  2063. data = data[length:]
  2064. }
  2065. return alertSuccess
  2066. }
  2067. type helloRequestMsg struct {
  2068. }
  2069. func (*helloRequestMsg) marshal() []byte {
  2070. return []byte{typeHelloRequest, 0, 0, 0}
  2071. }
  2072. func (*helloRequestMsg) unmarshal(data []byte) alert {
  2073. if len(data) != 4 {
  2074. return alertDecodeError
  2075. }
  2076. return alertSuccess
  2077. }
  2078. func eqUint16s(x, y []uint16) bool {
  2079. if len(x) != len(y) {
  2080. return false
  2081. }
  2082. for i, v := range x {
  2083. if y[i] != v {
  2084. return false
  2085. }
  2086. }
  2087. return true
  2088. }
  2089. func eqCurveIDs(x, y []CurveID) bool {
  2090. if len(x) != len(y) {
  2091. return false
  2092. }
  2093. for i, v := range x {
  2094. if y[i] != v {
  2095. return false
  2096. }
  2097. }
  2098. return true
  2099. }
  2100. func eqStrings(x, y []string) bool {
  2101. if len(x) != len(y) {
  2102. return false
  2103. }
  2104. for i, v := range x {
  2105. if y[i] != v {
  2106. return false
  2107. }
  2108. }
  2109. return true
  2110. }
  2111. func eqByteSlices(x, y [][]byte) bool {
  2112. if len(x) != len(y) {
  2113. return false
  2114. }
  2115. for i, v := range x {
  2116. if !bytes.Equal(v, y[i]) {
  2117. return false
  2118. }
  2119. }
  2120. return true
  2121. }
  2122. func eqSignatureAndHashes(x, y []signatureAndHash) bool {
  2123. if len(x) != len(y) {
  2124. return false
  2125. }
  2126. for i, v := range x {
  2127. v2 := y[i]
  2128. if v.hash != v2.hash || v.signature != v2.signature {
  2129. return false
  2130. }
  2131. }
  2132. return true
  2133. }
  2134. func eqKeyShares(x, y []keyShare) bool {
  2135. if len(x) != len(y) {
  2136. return false
  2137. }
  2138. for i := range x {
  2139. if x[i].group != y[i].group {
  2140. return false
  2141. }
  2142. if !bytes.Equal(x[i].data, y[i].data) {
  2143. return false
  2144. }
  2145. }
  2146. return true
  2147. }