Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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