handshake_messages.go 33 KB

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