Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

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