Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

handshake_messages.go 28 KiB

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