Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

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