Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

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