You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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