Alternative TLS implementation in Go
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

1168 rader
23 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. supportedCurves []uint16
  17. supportedPoints []uint8
  18. ticketSupported bool
  19. sessionTicket []uint8
  20. }
  21. func (m *clientHelloMsg) equal(i interface{}) bool {
  22. m1, ok := i.(*clientHelloMsg)
  23. if !ok {
  24. return false
  25. }
  26. return bytes.Equal(m.raw, m1.raw) &&
  27. m.vers == m1.vers &&
  28. bytes.Equal(m.random, m1.random) &&
  29. bytes.Equal(m.sessionId, m1.sessionId) &&
  30. eqUint16s(m.cipherSuites, m1.cipherSuites) &&
  31. bytes.Equal(m.compressionMethods, m1.compressionMethods) &&
  32. m.nextProtoNeg == m1.nextProtoNeg &&
  33. m.serverName == m1.serverName &&
  34. m.ocspStapling == m1.ocspStapling &&
  35. eqUint16s(m.supportedCurves, m1.supportedCurves) &&
  36. bytes.Equal(m.supportedPoints, m1.supportedPoints) &&
  37. m.ticketSupported == m1.ticketSupported &&
  38. bytes.Equal(m.sessionTicket, m1.sessionTicket)
  39. }
  40. func (m *clientHelloMsg) marshal() []byte {
  41. if m.raw != nil {
  42. return m.raw
  43. }
  44. length := 2 + 32 + 1 + len(m.sessionId) + 2 + len(m.cipherSuites)*2 + 1 + len(m.compressionMethods)
  45. numExtensions := 0
  46. extensionsLength := 0
  47. if m.nextProtoNeg {
  48. numExtensions++
  49. }
  50. if m.ocspStapling {
  51. extensionsLength += 1 + 2 + 2
  52. numExtensions++
  53. }
  54. if len(m.serverName) > 0 {
  55. extensionsLength += 5 + len(m.serverName)
  56. numExtensions++
  57. }
  58. if len(m.supportedCurves) > 0 {
  59. extensionsLength += 2 + 2*len(m.supportedCurves)
  60. numExtensions++
  61. }
  62. if len(m.supportedPoints) > 0 {
  63. extensionsLength += 1 + len(m.supportedPoints)
  64. numExtensions++
  65. }
  66. if m.ticketSupported {
  67. extensionsLength += len(m.sessionTicket)
  68. numExtensions++
  69. }
  70. if numExtensions > 0 {
  71. extensionsLength += 4 * numExtensions
  72. length += 2 + extensionsLength
  73. }
  74. x := make([]byte, 4+length)
  75. x[0] = typeClientHello
  76. x[1] = uint8(length >> 16)
  77. x[2] = uint8(length >> 8)
  78. x[3] = uint8(length)
  79. x[4] = uint8(m.vers >> 8)
  80. x[5] = uint8(m.vers)
  81. copy(x[6:38], m.random)
  82. x[38] = uint8(len(m.sessionId))
  83. copy(x[39:39+len(m.sessionId)], m.sessionId)
  84. y := x[39+len(m.sessionId):]
  85. y[0] = uint8(len(m.cipherSuites) >> 7)
  86. y[1] = uint8(len(m.cipherSuites) << 1)
  87. for i, suite := range m.cipherSuites {
  88. y[2+i*2] = uint8(suite >> 8)
  89. y[3+i*2] = uint8(suite)
  90. }
  91. z := y[2+len(m.cipherSuites)*2:]
  92. z[0] = uint8(len(m.compressionMethods))
  93. copy(z[1:], m.compressionMethods)
  94. z = z[1+len(m.compressionMethods):]
  95. if numExtensions > 0 {
  96. z[0] = byte(extensionsLength >> 8)
  97. z[1] = byte(extensionsLength)
  98. z = z[2:]
  99. }
  100. if m.nextProtoNeg {
  101. z[0] = byte(extensionNextProtoNeg >> 8)
  102. z[1] = byte(extensionNextProtoNeg)
  103. // The length is always 0
  104. z = z[4:]
  105. }
  106. if len(m.serverName) > 0 {
  107. z[0] = byte(extensionServerName >> 8)
  108. z[1] = byte(extensionServerName)
  109. l := len(m.serverName) + 5
  110. z[2] = byte(l >> 8)
  111. z[3] = byte(l)
  112. z = z[4:]
  113. // RFC 3546, section 3.1
  114. //
  115. // struct {
  116. // NameType name_type;
  117. // select (name_type) {
  118. // case host_name: HostName;
  119. // } name;
  120. // } ServerName;
  121. //
  122. // enum {
  123. // host_name(0), (255)
  124. // } NameType;
  125. //
  126. // opaque HostName<1..2^16-1>;
  127. //
  128. // struct {
  129. // ServerName server_name_list<1..2^16-1>
  130. // } ServerNameList;
  131. z[0] = byte((len(m.serverName) + 3) >> 8)
  132. z[1] = byte(len(m.serverName) + 3)
  133. z[3] = byte(len(m.serverName) >> 8)
  134. z[4] = byte(len(m.serverName))
  135. copy(z[5:], []byte(m.serverName))
  136. z = z[l:]
  137. }
  138. if m.ocspStapling {
  139. // RFC 4366, section 3.6
  140. z[0] = byte(extensionStatusRequest >> 8)
  141. z[1] = byte(extensionStatusRequest)
  142. z[2] = 0
  143. z[3] = 5
  144. z[4] = 1 // OCSP type
  145. // Two zero valued uint16s for the two lengths.
  146. z = z[9:]
  147. }
  148. if len(m.supportedCurves) > 0 {
  149. // http://tools.ietf.org/html/rfc4492#section-5.5.1
  150. z[0] = byte(extensionSupportedCurves >> 8)
  151. z[1] = byte(extensionSupportedCurves)
  152. l := 2 + 2*len(m.supportedCurves)
  153. z[2] = byte(l >> 8)
  154. z[3] = byte(l)
  155. l -= 2
  156. z[4] = byte(l >> 8)
  157. z[5] = byte(l)
  158. z = z[6:]
  159. for _, curve := range m.supportedCurves {
  160. z[0] = byte(curve >> 8)
  161. z[1] = byte(curve)
  162. z = z[2:]
  163. }
  164. }
  165. if len(m.supportedPoints) > 0 {
  166. // http://tools.ietf.org/html/rfc4492#section-5.5.2
  167. z[0] = byte(extensionSupportedPoints >> 8)
  168. z[1] = byte(extensionSupportedPoints)
  169. l := 1 + len(m.supportedPoints)
  170. z[2] = byte(l >> 8)
  171. z[3] = byte(l)
  172. l--
  173. z[4] = byte(l)
  174. z = z[5:]
  175. for _, pointFormat := range m.supportedPoints {
  176. z[0] = byte(pointFormat)
  177. z = z[1:]
  178. }
  179. }
  180. if m.ticketSupported {
  181. // http://tools.ietf.org/html/rfc5077#section-3.2
  182. z[0] = byte(extensionSessionTicket >> 8)
  183. z[1] = byte(extensionSessionTicket)
  184. l := len(m.sessionTicket)
  185. z[2] = byte(l >> 8)
  186. z[3] = byte(l)
  187. z = z[4:]
  188. copy(z, m.sessionTicket)
  189. z = z[len(m.sessionTicket):]
  190. }
  191. m.raw = x
  192. return x
  193. }
  194. func (m *clientHelloMsg) unmarshal(data []byte) bool {
  195. if len(data) < 42 {
  196. return false
  197. }
  198. m.raw = data
  199. m.vers = uint16(data[4])<<8 | uint16(data[5])
  200. m.random = data[6:38]
  201. sessionIdLen := int(data[38])
  202. if sessionIdLen > 32 || len(data) < 39+sessionIdLen {
  203. return false
  204. }
  205. m.sessionId = data[39 : 39+sessionIdLen]
  206. data = data[39+sessionIdLen:]
  207. if len(data) < 2 {
  208. return false
  209. }
  210. // cipherSuiteLen is the number of bytes of cipher suite numbers. Since
  211. // they are uint16s, the number must be even.
  212. cipherSuiteLen := int(data[0])<<8 | int(data[1])
  213. if cipherSuiteLen%2 == 1 || len(data) < 2+cipherSuiteLen {
  214. return false
  215. }
  216. numCipherSuites := cipherSuiteLen / 2
  217. m.cipherSuites = make([]uint16, numCipherSuites)
  218. for i := 0; i < numCipherSuites; i++ {
  219. m.cipherSuites[i] = uint16(data[2+2*i])<<8 | uint16(data[3+2*i])
  220. }
  221. data = data[2+cipherSuiteLen:]
  222. if len(data) < 1 {
  223. return false
  224. }
  225. compressionMethodsLen := int(data[0])
  226. if len(data) < 1+compressionMethodsLen {
  227. return false
  228. }
  229. m.compressionMethods = data[1 : 1+compressionMethodsLen]
  230. data = data[1+compressionMethodsLen:]
  231. m.nextProtoNeg = false
  232. m.serverName = ""
  233. m.ocspStapling = false
  234. m.ticketSupported = false
  235. m.sessionTicket = nil
  236. if len(data) == 0 {
  237. // ClientHello is optionally followed by extension data
  238. return true
  239. }
  240. if len(data) < 2 {
  241. return false
  242. }
  243. extensionsLength := int(data[0])<<8 | int(data[1])
  244. data = data[2:]
  245. if extensionsLength != len(data) {
  246. return false
  247. }
  248. for len(data) != 0 {
  249. if len(data) < 4 {
  250. return false
  251. }
  252. extension := uint16(data[0])<<8 | uint16(data[1])
  253. length := int(data[2])<<8 | int(data[3])
  254. data = data[4:]
  255. if len(data) < length {
  256. return false
  257. }
  258. switch extension {
  259. case extensionServerName:
  260. if length < 2 {
  261. return false
  262. }
  263. numNames := int(data[0])<<8 | int(data[1])
  264. d := data[2:]
  265. for i := 0; i < numNames; i++ {
  266. if len(d) < 3 {
  267. return false
  268. }
  269. nameType := d[0]
  270. nameLen := int(d[1])<<8 | int(d[2])
  271. d = d[3:]
  272. if len(d) < nameLen {
  273. return false
  274. }
  275. if nameType == 0 {
  276. m.serverName = string(d[0:nameLen])
  277. break
  278. }
  279. d = d[nameLen:]
  280. }
  281. case extensionNextProtoNeg:
  282. if length > 0 {
  283. return false
  284. }
  285. m.nextProtoNeg = true
  286. case extensionStatusRequest:
  287. m.ocspStapling = length > 0 && data[0] == statusTypeOCSP
  288. case extensionSupportedCurves:
  289. // http://tools.ietf.org/html/rfc4492#section-5.5.1
  290. if length < 2 {
  291. return false
  292. }
  293. l := int(data[0])<<8 | int(data[1])
  294. if l%2 == 1 || length != l+2 {
  295. return false
  296. }
  297. numCurves := l / 2
  298. m.supportedCurves = make([]uint16, numCurves)
  299. d := data[2:]
  300. for i := 0; i < numCurves; i++ {
  301. m.supportedCurves[i] = uint16(d[0])<<8 | uint16(d[1])
  302. d = d[2:]
  303. }
  304. case extensionSupportedPoints:
  305. // http://tools.ietf.org/html/rfc4492#section-5.5.2
  306. if length < 1 {
  307. return false
  308. }
  309. l := int(data[0])
  310. if length != l+1 {
  311. return false
  312. }
  313. m.supportedPoints = make([]uint8, l)
  314. copy(m.supportedPoints, data[1:])
  315. case extensionSessionTicket:
  316. // http://tools.ietf.org/html/rfc5077#section-3.2
  317. m.ticketSupported = true
  318. m.sessionTicket = data[:length]
  319. }
  320. data = data[length:]
  321. }
  322. return true
  323. }
  324. type serverHelloMsg struct {
  325. raw []byte
  326. vers uint16
  327. random []byte
  328. sessionId []byte
  329. cipherSuite uint16
  330. compressionMethod uint8
  331. nextProtoNeg bool
  332. nextProtos []string
  333. ocspStapling bool
  334. ticketSupported bool
  335. }
  336. func (m *serverHelloMsg) equal(i interface{}) bool {
  337. m1, ok := i.(*serverHelloMsg)
  338. if !ok {
  339. return false
  340. }
  341. return bytes.Equal(m.raw, m1.raw) &&
  342. m.vers == m1.vers &&
  343. bytes.Equal(m.random, m1.random) &&
  344. bytes.Equal(m.sessionId, m1.sessionId) &&
  345. m.cipherSuite == m1.cipherSuite &&
  346. m.compressionMethod == m1.compressionMethod &&
  347. m.nextProtoNeg == m1.nextProtoNeg &&
  348. eqStrings(m.nextProtos, m1.nextProtos) &&
  349. m.ocspStapling == m1.ocspStapling &&
  350. m.ticketSupported == m1.ticketSupported
  351. }
  352. func (m *serverHelloMsg) marshal() []byte {
  353. if m.raw != nil {
  354. return m.raw
  355. }
  356. length := 38 + len(m.sessionId)
  357. numExtensions := 0
  358. extensionsLength := 0
  359. nextProtoLen := 0
  360. if m.nextProtoNeg {
  361. numExtensions++
  362. for _, v := range m.nextProtos {
  363. nextProtoLen += len(v)
  364. }
  365. nextProtoLen += len(m.nextProtos)
  366. extensionsLength += nextProtoLen
  367. }
  368. if m.ocspStapling {
  369. numExtensions++
  370. }
  371. if m.ticketSupported {
  372. numExtensions++
  373. }
  374. if numExtensions > 0 {
  375. extensionsLength += 4 * numExtensions
  376. length += 2 + extensionsLength
  377. }
  378. x := make([]byte, 4+length)
  379. x[0] = typeServerHello
  380. x[1] = uint8(length >> 16)
  381. x[2] = uint8(length >> 8)
  382. x[3] = uint8(length)
  383. x[4] = uint8(m.vers >> 8)
  384. x[5] = uint8(m.vers)
  385. copy(x[6:38], m.random)
  386. x[38] = uint8(len(m.sessionId))
  387. copy(x[39:39+len(m.sessionId)], m.sessionId)
  388. z := x[39+len(m.sessionId):]
  389. z[0] = uint8(m.cipherSuite >> 8)
  390. z[1] = uint8(m.cipherSuite)
  391. z[2] = uint8(m.compressionMethod)
  392. z = z[3:]
  393. if numExtensions > 0 {
  394. z[0] = byte(extensionsLength >> 8)
  395. z[1] = byte(extensionsLength)
  396. z = z[2:]
  397. }
  398. if m.nextProtoNeg {
  399. z[0] = byte(extensionNextProtoNeg >> 8)
  400. z[1] = byte(extensionNextProtoNeg)
  401. z[2] = byte(nextProtoLen >> 8)
  402. z[3] = byte(nextProtoLen)
  403. z = z[4:]
  404. for _, v := range m.nextProtos {
  405. l := len(v)
  406. if l > 255 {
  407. l = 255
  408. }
  409. z[0] = byte(l)
  410. copy(z[1:], []byte(v[0:l]))
  411. z = z[1+l:]
  412. }
  413. }
  414. if m.ocspStapling {
  415. z[0] = byte(extensionStatusRequest >> 8)
  416. z[1] = byte(extensionStatusRequest)
  417. z = z[4:]
  418. }
  419. if m.ticketSupported {
  420. z[0] = byte(extensionSessionTicket >> 8)
  421. z[1] = byte(extensionSessionTicket)
  422. z = z[4:]
  423. }
  424. m.raw = x
  425. return x
  426. }
  427. func (m *serverHelloMsg) unmarshal(data []byte) bool {
  428. if len(data) < 42 {
  429. return false
  430. }
  431. m.raw = data
  432. m.vers = uint16(data[4])<<8 | uint16(data[5])
  433. m.random = data[6:38]
  434. sessionIdLen := int(data[38])
  435. if sessionIdLen > 32 || len(data) < 39+sessionIdLen {
  436. return false
  437. }
  438. m.sessionId = data[39 : 39+sessionIdLen]
  439. data = data[39+sessionIdLen:]
  440. if len(data) < 3 {
  441. return false
  442. }
  443. m.cipherSuite = uint16(data[0])<<8 | uint16(data[1])
  444. m.compressionMethod = data[2]
  445. data = data[3:]
  446. m.nextProtoNeg = false
  447. m.nextProtos = nil
  448. m.ocspStapling = false
  449. m.ticketSupported = false
  450. if len(data) == 0 {
  451. // ServerHello is optionally followed by extension data
  452. return true
  453. }
  454. if len(data) < 2 {
  455. return false
  456. }
  457. extensionsLength := int(data[0])<<8 | int(data[1])
  458. data = data[2:]
  459. if len(data) != extensionsLength {
  460. return false
  461. }
  462. for len(data) != 0 {
  463. if len(data) < 4 {
  464. return false
  465. }
  466. extension := uint16(data[0])<<8 | uint16(data[1])
  467. length := int(data[2])<<8 | int(data[3])
  468. data = data[4:]
  469. if len(data) < length {
  470. return false
  471. }
  472. switch extension {
  473. case extensionNextProtoNeg:
  474. m.nextProtoNeg = true
  475. d := data[:length]
  476. for len(d) > 0 {
  477. l := int(d[0])
  478. d = d[1:]
  479. if l == 0 || l > len(d) {
  480. return false
  481. }
  482. m.nextProtos = append(m.nextProtos, string(d[:l]))
  483. d = d[l:]
  484. }
  485. case extensionStatusRequest:
  486. if length > 0 {
  487. return false
  488. }
  489. m.ocspStapling = true
  490. case extensionSessionTicket:
  491. if length > 0 {
  492. return false
  493. }
  494. m.ticketSupported = true
  495. }
  496. data = data[length:]
  497. }
  498. return true
  499. }
  500. type certificateMsg struct {
  501. raw []byte
  502. certificates [][]byte
  503. }
  504. func (m *certificateMsg) equal(i interface{}) bool {
  505. m1, ok := i.(*certificateMsg)
  506. if !ok {
  507. return false
  508. }
  509. return bytes.Equal(m.raw, m1.raw) &&
  510. eqByteSlices(m.certificates, m1.certificates)
  511. }
  512. func (m *certificateMsg) marshal() (x []byte) {
  513. if m.raw != nil {
  514. return m.raw
  515. }
  516. var i int
  517. for _, slice := range m.certificates {
  518. i += len(slice)
  519. }
  520. length := 3 + 3*len(m.certificates) + i
  521. x = make([]byte, 4+length)
  522. x[0] = typeCertificate
  523. x[1] = uint8(length >> 16)
  524. x[2] = uint8(length >> 8)
  525. x[3] = uint8(length)
  526. certificateOctets := length - 3
  527. x[4] = uint8(certificateOctets >> 16)
  528. x[5] = uint8(certificateOctets >> 8)
  529. x[6] = uint8(certificateOctets)
  530. y := x[7:]
  531. for _, slice := range m.certificates {
  532. y[0] = uint8(len(slice) >> 16)
  533. y[1] = uint8(len(slice) >> 8)
  534. y[2] = uint8(len(slice))
  535. copy(y[3:], slice)
  536. y = y[3+len(slice):]
  537. }
  538. m.raw = x
  539. return
  540. }
  541. func (m *certificateMsg) unmarshal(data []byte) bool {
  542. if len(data) < 7 {
  543. return false
  544. }
  545. m.raw = data
  546. certsLen := uint32(data[4])<<16 | uint32(data[5])<<8 | uint32(data[6])
  547. if uint32(len(data)) != certsLen+7 {
  548. return false
  549. }
  550. numCerts := 0
  551. d := data[7:]
  552. for certsLen > 0 {
  553. if len(d) < 4 {
  554. return false
  555. }
  556. certLen := uint32(d[0])<<16 | uint32(d[1])<<8 | uint32(d[2])
  557. if uint32(len(d)) < 3+certLen {
  558. return false
  559. }
  560. d = d[3+certLen:]
  561. certsLen -= 3 + certLen
  562. numCerts++
  563. }
  564. m.certificates = make([][]byte, numCerts)
  565. d = data[7:]
  566. for i := 0; i < numCerts; i++ {
  567. certLen := uint32(d[0])<<16 | uint32(d[1])<<8 | uint32(d[2])
  568. m.certificates[i] = d[3 : 3+certLen]
  569. d = d[3+certLen:]
  570. }
  571. return true
  572. }
  573. type serverKeyExchangeMsg struct {
  574. raw []byte
  575. key []byte
  576. }
  577. func (m *serverKeyExchangeMsg) equal(i interface{}) bool {
  578. m1, ok := i.(*serverKeyExchangeMsg)
  579. if !ok {
  580. return false
  581. }
  582. return bytes.Equal(m.raw, m1.raw) &&
  583. bytes.Equal(m.key, m1.key)
  584. }
  585. func (m *serverKeyExchangeMsg) marshal() []byte {
  586. if m.raw != nil {
  587. return m.raw
  588. }
  589. length := len(m.key)
  590. x := make([]byte, length+4)
  591. x[0] = typeServerKeyExchange
  592. x[1] = uint8(length >> 16)
  593. x[2] = uint8(length >> 8)
  594. x[3] = uint8(length)
  595. copy(x[4:], m.key)
  596. m.raw = x
  597. return x
  598. }
  599. func (m *serverKeyExchangeMsg) unmarshal(data []byte) bool {
  600. m.raw = data
  601. if len(data) < 4 {
  602. return false
  603. }
  604. m.key = data[4:]
  605. return true
  606. }
  607. type certificateStatusMsg struct {
  608. raw []byte
  609. statusType uint8
  610. response []byte
  611. }
  612. func (m *certificateStatusMsg) equal(i interface{}) bool {
  613. m1, ok := i.(*certificateStatusMsg)
  614. if !ok {
  615. return false
  616. }
  617. return bytes.Equal(m.raw, m1.raw) &&
  618. m.statusType == m1.statusType &&
  619. bytes.Equal(m.response, m1.response)
  620. }
  621. func (m *certificateStatusMsg) marshal() []byte {
  622. if m.raw != nil {
  623. return m.raw
  624. }
  625. var x []byte
  626. if m.statusType == statusTypeOCSP {
  627. x = make([]byte, 4+4+len(m.response))
  628. x[0] = typeCertificateStatus
  629. l := len(m.response) + 4
  630. x[1] = byte(l >> 16)
  631. x[2] = byte(l >> 8)
  632. x[3] = byte(l)
  633. x[4] = statusTypeOCSP
  634. l -= 4
  635. x[5] = byte(l >> 16)
  636. x[6] = byte(l >> 8)
  637. x[7] = byte(l)
  638. copy(x[8:], m.response)
  639. } else {
  640. x = []byte{typeCertificateStatus, 0, 0, 1, m.statusType}
  641. }
  642. m.raw = x
  643. return x
  644. }
  645. func (m *certificateStatusMsg) unmarshal(data []byte) bool {
  646. m.raw = data
  647. if len(data) < 5 {
  648. return false
  649. }
  650. m.statusType = data[4]
  651. m.response = nil
  652. if m.statusType == statusTypeOCSP {
  653. if len(data) < 8 {
  654. return false
  655. }
  656. respLen := uint32(data[5])<<16 | uint32(data[6])<<8 | uint32(data[7])
  657. if uint32(len(data)) != 4+4+respLen {
  658. return false
  659. }
  660. m.response = data[8:]
  661. }
  662. return true
  663. }
  664. type serverHelloDoneMsg struct{}
  665. func (m *serverHelloDoneMsg) equal(i interface{}) bool {
  666. _, ok := i.(*serverHelloDoneMsg)
  667. return ok
  668. }
  669. func (m *serverHelloDoneMsg) marshal() []byte {
  670. x := make([]byte, 4)
  671. x[0] = typeServerHelloDone
  672. return x
  673. }
  674. func (m *serverHelloDoneMsg) unmarshal(data []byte) bool {
  675. return len(data) == 4
  676. }
  677. type clientKeyExchangeMsg struct {
  678. raw []byte
  679. ciphertext []byte
  680. }
  681. func (m *clientKeyExchangeMsg) equal(i interface{}) bool {
  682. m1, ok := i.(*clientKeyExchangeMsg)
  683. if !ok {
  684. return false
  685. }
  686. return bytes.Equal(m.raw, m1.raw) &&
  687. bytes.Equal(m.ciphertext, m1.ciphertext)
  688. }
  689. func (m *clientKeyExchangeMsg) marshal() []byte {
  690. if m.raw != nil {
  691. return m.raw
  692. }
  693. length := len(m.ciphertext)
  694. x := make([]byte, length+4)
  695. x[0] = typeClientKeyExchange
  696. x[1] = uint8(length >> 16)
  697. x[2] = uint8(length >> 8)
  698. x[3] = uint8(length)
  699. copy(x[4:], m.ciphertext)
  700. m.raw = x
  701. return x
  702. }
  703. func (m *clientKeyExchangeMsg) unmarshal(data []byte) bool {
  704. m.raw = data
  705. if len(data) < 4 {
  706. return false
  707. }
  708. l := int(data[1])<<16 | int(data[2])<<8 | int(data[3])
  709. if l != len(data)-4 {
  710. return false
  711. }
  712. m.ciphertext = data[4:]
  713. return true
  714. }
  715. type finishedMsg struct {
  716. raw []byte
  717. verifyData []byte
  718. }
  719. func (m *finishedMsg) equal(i interface{}) bool {
  720. m1, ok := i.(*finishedMsg)
  721. if !ok {
  722. return false
  723. }
  724. return bytes.Equal(m.raw, m1.raw) &&
  725. bytes.Equal(m.verifyData, m1.verifyData)
  726. }
  727. func (m *finishedMsg) marshal() (x []byte) {
  728. if m.raw != nil {
  729. return m.raw
  730. }
  731. x = make([]byte, 4+len(m.verifyData))
  732. x[0] = typeFinished
  733. x[3] = byte(len(m.verifyData))
  734. copy(x[4:], m.verifyData)
  735. m.raw = x
  736. return
  737. }
  738. func (m *finishedMsg) unmarshal(data []byte) bool {
  739. m.raw = data
  740. if len(data) < 4 {
  741. return false
  742. }
  743. m.verifyData = data[4:]
  744. return true
  745. }
  746. type nextProtoMsg struct {
  747. raw []byte
  748. proto string
  749. }
  750. func (m *nextProtoMsg) equal(i interface{}) bool {
  751. m1, ok := i.(*nextProtoMsg)
  752. if !ok {
  753. return false
  754. }
  755. return bytes.Equal(m.raw, m1.raw) &&
  756. m.proto == m1.proto
  757. }
  758. func (m *nextProtoMsg) marshal() []byte {
  759. if m.raw != nil {
  760. return m.raw
  761. }
  762. l := len(m.proto)
  763. if l > 255 {
  764. l = 255
  765. }
  766. padding := 32 - (l+2)%32
  767. length := l + padding + 2
  768. x := make([]byte, length+4)
  769. x[0] = typeNextProtocol
  770. x[1] = uint8(length >> 16)
  771. x[2] = uint8(length >> 8)
  772. x[3] = uint8(length)
  773. y := x[4:]
  774. y[0] = byte(l)
  775. copy(y[1:], []byte(m.proto[0:l]))
  776. y = y[1+l:]
  777. y[0] = byte(padding)
  778. m.raw = x
  779. return x
  780. }
  781. func (m *nextProtoMsg) unmarshal(data []byte) bool {
  782. m.raw = data
  783. if len(data) < 5 {
  784. return false
  785. }
  786. data = data[4:]
  787. protoLen := int(data[0])
  788. data = data[1:]
  789. if len(data) < protoLen {
  790. return false
  791. }
  792. m.proto = string(data[0:protoLen])
  793. data = data[protoLen:]
  794. if len(data) < 1 {
  795. return false
  796. }
  797. paddingLen := int(data[0])
  798. data = data[1:]
  799. if len(data) != paddingLen {
  800. return false
  801. }
  802. return true
  803. }
  804. type certificateRequestMsg struct {
  805. raw []byte
  806. certificateTypes []byte
  807. certificateAuthorities [][]byte
  808. }
  809. func (m *certificateRequestMsg) equal(i interface{}) bool {
  810. m1, ok := i.(*certificateRequestMsg)
  811. if !ok {
  812. return false
  813. }
  814. return bytes.Equal(m.raw, m1.raw) &&
  815. bytes.Equal(m.certificateTypes, m1.certificateTypes) &&
  816. eqByteSlices(m.certificateAuthorities, m1.certificateAuthorities)
  817. }
  818. func (m *certificateRequestMsg) marshal() (x []byte) {
  819. if m.raw != nil {
  820. return m.raw
  821. }
  822. // See http://tools.ietf.org/html/rfc4346#section-7.4.4
  823. length := 1 + len(m.certificateTypes) + 2
  824. casLength := 0
  825. for _, ca := range m.certificateAuthorities {
  826. casLength += 2 + len(ca)
  827. }
  828. length += casLength
  829. x = make([]byte, 4+length)
  830. x[0] = typeCertificateRequest
  831. x[1] = uint8(length >> 16)
  832. x[2] = uint8(length >> 8)
  833. x[3] = uint8(length)
  834. x[4] = uint8(len(m.certificateTypes))
  835. copy(x[5:], m.certificateTypes)
  836. y := x[5+len(m.certificateTypes):]
  837. y[0] = uint8(casLength >> 8)
  838. y[1] = uint8(casLength)
  839. y = y[2:]
  840. for _, ca := range m.certificateAuthorities {
  841. y[0] = uint8(len(ca) >> 8)
  842. y[1] = uint8(len(ca))
  843. y = y[2:]
  844. copy(y, ca)
  845. y = y[len(ca):]
  846. }
  847. m.raw = x
  848. return
  849. }
  850. func (m *certificateRequestMsg) unmarshal(data []byte) bool {
  851. m.raw = data
  852. if len(data) < 5 {
  853. return false
  854. }
  855. length := uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
  856. if uint32(len(data))-4 != length {
  857. return false
  858. }
  859. numCertTypes := int(data[4])
  860. data = data[5:]
  861. if numCertTypes == 0 || len(data) <= numCertTypes {
  862. return false
  863. }
  864. m.certificateTypes = make([]byte, numCertTypes)
  865. if copy(m.certificateTypes, data) != numCertTypes {
  866. return false
  867. }
  868. data = data[numCertTypes:]
  869. if len(data) < 2 {
  870. return false
  871. }
  872. casLength := uint16(data[0])<<8 | uint16(data[1])
  873. data = data[2:]
  874. if len(data) < int(casLength) {
  875. return false
  876. }
  877. cas := make([]byte, casLength)
  878. copy(cas, data)
  879. data = data[casLength:]
  880. m.certificateAuthorities = nil
  881. for len(cas) > 0 {
  882. if len(cas) < 2 {
  883. return false
  884. }
  885. caLen := uint16(cas[0])<<8 | uint16(cas[1])
  886. cas = cas[2:]
  887. if len(cas) < int(caLen) {
  888. return false
  889. }
  890. m.certificateAuthorities = append(m.certificateAuthorities, cas[:caLen])
  891. cas = cas[caLen:]
  892. }
  893. if len(data) > 0 {
  894. return false
  895. }
  896. return true
  897. }
  898. type certificateVerifyMsg struct {
  899. raw []byte
  900. signature []byte
  901. }
  902. func (m *certificateVerifyMsg) equal(i interface{}) bool {
  903. m1, ok := i.(*certificateVerifyMsg)
  904. if !ok {
  905. return false
  906. }
  907. return bytes.Equal(m.raw, m1.raw) &&
  908. bytes.Equal(m.signature, m1.signature)
  909. }
  910. func (m *certificateVerifyMsg) marshal() (x []byte) {
  911. if m.raw != nil {
  912. return m.raw
  913. }
  914. // See http://tools.ietf.org/html/rfc4346#section-7.4.8
  915. siglength := len(m.signature)
  916. length := 2 + siglength
  917. x = make([]byte, 4+length)
  918. x[0] = typeCertificateVerify
  919. x[1] = uint8(length >> 16)
  920. x[2] = uint8(length >> 8)
  921. x[3] = uint8(length)
  922. x[4] = uint8(siglength >> 8)
  923. x[5] = uint8(siglength)
  924. copy(x[6:], m.signature)
  925. m.raw = x
  926. return
  927. }
  928. func (m *certificateVerifyMsg) unmarshal(data []byte) bool {
  929. m.raw = data
  930. if len(data) < 6 {
  931. return false
  932. }
  933. length := uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
  934. if uint32(len(data))-4 != length {
  935. return false
  936. }
  937. siglength := int(data[4])<<8 + int(data[5])
  938. if len(data)-6 != siglength {
  939. return false
  940. }
  941. m.signature = data[6:]
  942. return true
  943. }
  944. type newSessionTicketMsg struct {
  945. raw []byte
  946. ticket []byte
  947. }
  948. func (m *newSessionTicketMsg) equal(i interface{}) bool {
  949. m1, ok := i.(*newSessionTicketMsg)
  950. if !ok {
  951. return false
  952. }
  953. return bytes.Equal(m.raw, m1.raw) &&
  954. bytes.Equal(m.ticket, m1.ticket)
  955. }
  956. func (m *newSessionTicketMsg) marshal() (x []byte) {
  957. if m.raw != nil {
  958. return m.raw
  959. }
  960. // See http://tools.ietf.org/html/rfc5077#section-3.3
  961. ticketLen := len(m.ticket)
  962. length := 2 + 4 + ticketLen
  963. x = make([]byte, 4+length)
  964. x[0] = typeNewSessionTicket
  965. x[1] = uint8(length >> 16)
  966. x[2] = uint8(length >> 8)
  967. x[3] = uint8(length)
  968. x[8] = uint8(ticketLen >> 8)
  969. x[9] = uint8(ticketLen)
  970. copy(x[10:], m.ticket)
  971. m.raw = x
  972. return
  973. }
  974. func (m *newSessionTicketMsg) unmarshal(data []byte) bool {
  975. m.raw = data
  976. if len(data) < 10 {
  977. return false
  978. }
  979. length := uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
  980. if uint32(len(data))-4 != length {
  981. return false
  982. }
  983. ticketLen := int(data[8])<<8 + int(data[9])
  984. if len(data)-10 != ticketLen {
  985. return false
  986. }
  987. m.ticket = data[10:]
  988. return true
  989. }
  990. func eqUint16s(x, y []uint16) bool {
  991. if len(x) != len(y) {
  992. return false
  993. }
  994. for i, v := range x {
  995. if y[i] != v {
  996. return false
  997. }
  998. }
  999. return true
  1000. }
  1001. func eqStrings(x, y []string) bool {
  1002. if len(x) != len(y) {
  1003. return false
  1004. }
  1005. for i, v := range x {
  1006. if y[i] != v {
  1007. return false
  1008. }
  1009. }
  1010. return true
  1011. }
  1012. func eqByteSlices(x, y [][]byte) bool {
  1013. if len(x) != len(y) {
  1014. return false
  1015. }
  1016. for i, v := range x {
  1017. if !bytes.Equal(v, y[i]) {
  1018. return false
  1019. }
  1020. }
  1021. return true
  1022. }