handshake_messages.go 33 KB

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