25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

handshake_messages.go 30 KiB

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