handshake_messages.go 37 KB

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