Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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