Alternative TLS implementation in Go
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

handshake_messages.go 21 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  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 []uint16
  17. supportedPoints []uint8
  18. }
  19. func (m *clientHelloMsg) equal(i interface{}) bool {
  20. m1, ok := i.(*clientHelloMsg)
  21. if !ok {
  22. return false
  23. }
  24. return bytes.Equal(m.raw, m1.raw) &&
  25. m.vers == m1.vers &&
  26. bytes.Equal(m.random, m1.random) &&
  27. bytes.Equal(m.sessionId, m1.sessionId) &&
  28. eqUint16s(m.cipherSuites, m1.cipherSuites) &&
  29. bytes.Equal(m.compressionMethods, m1.compressionMethods) &&
  30. m.nextProtoNeg == m1.nextProtoNeg &&
  31. m.serverName == m1.serverName &&
  32. m.ocspStapling == m1.ocspStapling &&
  33. eqUint16s(m.supportedCurves, m1.supportedCurves) &&
  34. bytes.Equal(m.supportedPoints, m1.supportedPoints)
  35. }
  36. func (m *clientHelloMsg) marshal() []byte {
  37. if m.raw != nil {
  38. return m.raw
  39. }
  40. length := 2 + 32 + 1 + len(m.sessionId) + 2 + len(m.cipherSuites)*2 + 1 + len(m.compressionMethods)
  41. numExtensions := 0
  42. extensionsLength := 0
  43. if m.nextProtoNeg {
  44. numExtensions++
  45. }
  46. if m.ocspStapling {
  47. extensionsLength += 1 + 2 + 2
  48. numExtensions++
  49. }
  50. if len(m.serverName) > 0 {
  51. extensionsLength += 5 + len(m.serverName)
  52. numExtensions++
  53. }
  54. if len(m.supportedCurves) > 0 {
  55. extensionsLength += 2 + 2*len(m.supportedCurves)
  56. numExtensions++
  57. }
  58. if len(m.supportedPoints) > 0 {
  59. extensionsLength += 1 + len(m.supportedPoints)
  60. numExtensions++
  61. }
  62. if numExtensions > 0 {
  63. extensionsLength += 4 * numExtensions
  64. length += 2 + extensionsLength
  65. }
  66. x := make([]byte, 4+length)
  67. x[0] = typeClientHello
  68. x[1] = uint8(length >> 16)
  69. x[2] = uint8(length >> 8)
  70. x[3] = uint8(length)
  71. x[4] = uint8(m.vers >> 8)
  72. x[5] = uint8(m.vers)
  73. copy(x[6:38], m.random)
  74. x[38] = uint8(len(m.sessionId))
  75. copy(x[39:39+len(m.sessionId)], m.sessionId)
  76. y := x[39+len(m.sessionId):]
  77. y[0] = uint8(len(m.cipherSuites) >> 7)
  78. y[1] = uint8(len(m.cipherSuites) << 1)
  79. for i, suite := range m.cipherSuites {
  80. y[2+i*2] = uint8(suite >> 8)
  81. y[3+i*2] = uint8(suite)
  82. }
  83. z := y[2+len(m.cipherSuites)*2:]
  84. z[0] = uint8(len(m.compressionMethods))
  85. copy(z[1:], m.compressionMethods)
  86. z = z[1+len(m.compressionMethods):]
  87. if numExtensions > 0 {
  88. z[0] = byte(extensionsLength >> 8)
  89. z[1] = byte(extensionsLength)
  90. z = z[2:]
  91. }
  92. if m.nextProtoNeg {
  93. z[0] = byte(extensionNextProtoNeg >> 8)
  94. z[1] = byte(extensionNextProtoNeg)
  95. // The length is always 0
  96. z = z[4:]
  97. }
  98. if len(m.serverName) > 0 {
  99. z[0] = byte(extensionServerName >> 8)
  100. z[1] = byte(extensionServerName)
  101. l := len(m.serverName) + 5
  102. z[2] = byte(l >> 8)
  103. z[3] = byte(l)
  104. z = z[4:]
  105. // RFC 3546, section 3.1
  106. //
  107. // struct {
  108. // NameType name_type;
  109. // select (name_type) {
  110. // case host_name: HostName;
  111. // } name;
  112. // } ServerName;
  113. //
  114. // enum {
  115. // host_name(0), (255)
  116. // } NameType;
  117. //
  118. // opaque HostName<1..2^16-1>;
  119. //
  120. // struct {
  121. // ServerName server_name_list<1..2^16-1>
  122. // } ServerNameList;
  123. z[0] = byte((len(m.serverName) + 3) >> 8)
  124. z[1] = byte(len(m.serverName) + 3)
  125. z[3] = byte(len(m.serverName) >> 8)
  126. z[4] = byte(len(m.serverName))
  127. copy(z[5:], []byte(m.serverName))
  128. z = z[l:]
  129. }
  130. if m.ocspStapling {
  131. // RFC 4366, section 3.6
  132. z[0] = byte(extensionStatusRequest >> 8)
  133. z[1] = byte(extensionStatusRequest)
  134. z[2] = 0
  135. z[3] = 5
  136. z[4] = 1 // OCSP type
  137. // Two zero valued uint16s for the two lengths.
  138. z = z[9:]
  139. }
  140. if len(m.supportedCurves) > 0 {
  141. // http://tools.ietf.org/html/rfc4492#section-5.5.1
  142. z[0] = byte(extensionSupportedCurves >> 8)
  143. z[1] = byte(extensionSupportedCurves)
  144. l := 2 + 2*len(m.supportedCurves)
  145. z[2] = byte(l >> 8)
  146. z[3] = byte(l)
  147. l -= 2
  148. z[4] = byte(l >> 8)
  149. z[5] = byte(l)
  150. z = z[6:]
  151. for _, curve := range m.supportedCurves {
  152. z[0] = byte(curve >> 8)
  153. z[1] = byte(curve)
  154. z = z[2:]
  155. }
  156. }
  157. if len(m.supportedPoints) > 0 {
  158. // http://tools.ietf.org/html/rfc4492#section-5.5.2
  159. z[0] = byte(extensionSupportedPoints >> 8)
  160. z[1] = byte(extensionSupportedPoints)
  161. l := 1 + len(m.supportedPoints)
  162. z[2] = byte(l >> 8)
  163. z[3] = byte(l)
  164. l--
  165. z[4] = byte(l)
  166. z = z[5:]
  167. for _, pointFormat := range m.supportedPoints {
  168. z[0] = byte(pointFormat)
  169. z = z[1:]
  170. }
  171. }
  172. m.raw = x
  173. return x
  174. }
  175. func (m *clientHelloMsg) unmarshal(data []byte) bool {
  176. if len(data) < 42 {
  177. return false
  178. }
  179. m.raw = data
  180. m.vers = uint16(data[4])<<8 | uint16(data[5])
  181. m.random = data[6:38]
  182. sessionIdLen := int(data[38])
  183. if sessionIdLen > 32 || len(data) < 39+sessionIdLen {
  184. return false
  185. }
  186. m.sessionId = data[39 : 39+sessionIdLen]
  187. data = data[39+sessionIdLen:]
  188. if len(data) < 2 {
  189. return false
  190. }
  191. // cipherSuiteLen is the number of bytes of cipher suite numbers. Since
  192. // they are uint16s, the number must be even.
  193. cipherSuiteLen := int(data[0])<<8 | int(data[1])
  194. if cipherSuiteLen%2 == 1 || len(data) < 2+cipherSuiteLen {
  195. return false
  196. }
  197. numCipherSuites := cipherSuiteLen / 2
  198. m.cipherSuites = make([]uint16, numCipherSuites)
  199. for i := 0; i < numCipherSuites; i++ {
  200. m.cipherSuites[i] = uint16(data[2+2*i])<<8 | uint16(data[3+2*i])
  201. }
  202. data = data[2+cipherSuiteLen:]
  203. if len(data) < 1 {
  204. return false
  205. }
  206. compressionMethodsLen := int(data[0])
  207. if len(data) < 1+compressionMethodsLen {
  208. return false
  209. }
  210. m.compressionMethods = data[1 : 1+compressionMethodsLen]
  211. data = data[1+compressionMethodsLen:]
  212. m.nextProtoNeg = false
  213. m.serverName = ""
  214. m.ocspStapling = false
  215. if len(data) == 0 {
  216. // ClientHello is optionally followed by extension data
  217. return true
  218. }
  219. if len(data) < 2 {
  220. return false
  221. }
  222. extensionsLength := int(data[0])<<8 | int(data[1])
  223. data = data[2:]
  224. if extensionsLength != len(data) {
  225. return false
  226. }
  227. for len(data) != 0 {
  228. if len(data) < 4 {
  229. return false
  230. }
  231. extension := uint16(data[0])<<8 | uint16(data[1])
  232. length := int(data[2])<<8 | int(data[3])
  233. data = data[4:]
  234. if len(data) < length {
  235. return false
  236. }
  237. switch extension {
  238. case extensionServerName:
  239. if length < 2 {
  240. return false
  241. }
  242. numNames := int(data[0])<<8 | int(data[1])
  243. d := data[2:]
  244. for i := 0; i < numNames; i++ {
  245. if len(d) < 3 {
  246. return false
  247. }
  248. nameType := d[0]
  249. nameLen := int(d[1])<<8 | int(d[2])
  250. d = d[3:]
  251. if len(d) < nameLen {
  252. return false
  253. }
  254. if nameType == 0 {
  255. m.serverName = string(d[0:nameLen])
  256. break
  257. }
  258. d = d[nameLen:]
  259. }
  260. case extensionNextProtoNeg:
  261. if length > 0 {
  262. return false
  263. }
  264. m.nextProtoNeg = true
  265. case extensionStatusRequest:
  266. m.ocspStapling = length > 0 && data[0] == statusTypeOCSP
  267. case extensionSupportedCurves:
  268. // http://tools.ietf.org/html/rfc4492#section-5.5.1
  269. if length < 2 {
  270. return false
  271. }
  272. l := int(data[0])<<8 | int(data[1])
  273. if l%2 == 1 || length != l+2 {
  274. return false
  275. }
  276. numCurves := l / 2
  277. m.supportedCurves = make([]uint16, numCurves)
  278. d := data[2:]
  279. for i := 0; i < numCurves; i++ {
  280. m.supportedCurves[i] = uint16(d[0])<<8 | uint16(d[1])
  281. d = d[2:]
  282. }
  283. case extensionSupportedPoints:
  284. // http://tools.ietf.org/html/rfc4492#section-5.5.2
  285. if length < 1 {
  286. return false
  287. }
  288. l := int(data[0])
  289. if length != l+1 {
  290. return false
  291. }
  292. m.supportedPoints = make([]uint8, l)
  293. copy(m.supportedPoints, data[1:])
  294. }
  295. data = data[length:]
  296. }
  297. return true
  298. }
  299. type serverHelloMsg struct {
  300. raw []byte
  301. vers uint16
  302. random []byte
  303. sessionId []byte
  304. cipherSuite uint16
  305. compressionMethod uint8
  306. nextProtoNeg bool
  307. nextProtos []string
  308. ocspStapling bool
  309. }
  310. func (m *serverHelloMsg) equal(i interface{}) bool {
  311. m1, ok := i.(*serverHelloMsg)
  312. if !ok {
  313. return false
  314. }
  315. return bytes.Equal(m.raw, m1.raw) &&
  316. m.vers == m1.vers &&
  317. bytes.Equal(m.random, m1.random) &&
  318. bytes.Equal(m.sessionId, m1.sessionId) &&
  319. m.cipherSuite == m1.cipherSuite &&
  320. m.compressionMethod == m1.compressionMethod &&
  321. m.nextProtoNeg == m1.nextProtoNeg &&
  322. eqStrings(m.nextProtos, m1.nextProtos) &&
  323. m.ocspStapling == m1.ocspStapling
  324. }
  325. func (m *serverHelloMsg) marshal() []byte {
  326. if m.raw != nil {
  327. return m.raw
  328. }
  329. length := 38 + len(m.sessionId)
  330. numExtensions := 0
  331. extensionsLength := 0
  332. nextProtoLen := 0
  333. if m.nextProtoNeg {
  334. numExtensions++
  335. for _, v := range m.nextProtos {
  336. nextProtoLen += len(v)
  337. }
  338. nextProtoLen += len(m.nextProtos)
  339. extensionsLength += nextProtoLen
  340. }
  341. if m.ocspStapling {
  342. numExtensions++
  343. }
  344. if numExtensions > 0 {
  345. extensionsLength += 4 * numExtensions
  346. length += 2 + extensionsLength
  347. }
  348. x := make([]byte, 4+length)
  349. x[0] = typeServerHello
  350. x[1] = uint8(length >> 16)
  351. x[2] = uint8(length >> 8)
  352. x[3] = uint8(length)
  353. x[4] = uint8(m.vers >> 8)
  354. x[5] = uint8(m.vers)
  355. copy(x[6:38], m.random)
  356. x[38] = uint8(len(m.sessionId))
  357. copy(x[39:39+len(m.sessionId)], m.sessionId)
  358. z := x[39+len(m.sessionId):]
  359. z[0] = uint8(m.cipherSuite >> 8)
  360. z[1] = uint8(m.cipherSuite)
  361. z[2] = uint8(m.compressionMethod)
  362. z = z[3:]
  363. if numExtensions > 0 {
  364. z[0] = byte(extensionsLength >> 8)
  365. z[1] = byte(extensionsLength)
  366. z = z[2:]
  367. }
  368. if m.nextProtoNeg {
  369. z[0] = byte(extensionNextProtoNeg >> 8)
  370. z[1] = byte(extensionNextProtoNeg)
  371. z[2] = byte(nextProtoLen >> 8)
  372. z[3] = byte(nextProtoLen)
  373. z = z[4:]
  374. for _, v := range m.nextProtos {
  375. l := len(v)
  376. if l > 255 {
  377. l = 255
  378. }
  379. z[0] = byte(l)
  380. copy(z[1:], []byte(v[0:l]))
  381. z = z[1+l:]
  382. }
  383. }
  384. if m.ocspStapling {
  385. z[0] = byte(extensionStatusRequest >> 8)
  386. z[1] = byte(extensionStatusRequest)
  387. z = z[4:]
  388. }
  389. m.raw = x
  390. return x
  391. }
  392. func (m *serverHelloMsg) unmarshal(data []byte) bool {
  393. if len(data) < 42 {
  394. return false
  395. }
  396. m.raw = data
  397. m.vers = uint16(data[4])<<8 | uint16(data[5])
  398. m.random = data[6:38]
  399. sessionIdLen := int(data[38])
  400. if sessionIdLen > 32 || len(data) < 39+sessionIdLen {
  401. return false
  402. }
  403. m.sessionId = data[39 : 39+sessionIdLen]
  404. data = data[39+sessionIdLen:]
  405. if len(data) < 3 {
  406. return false
  407. }
  408. m.cipherSuite = uint16(data[0])<<8 | uint16(data[1])
  409. m.compressionMethod = data[2]
  410. data = data[3:]
  411. m.nextProtoNeg = false
  412. m.nextProtos = nil
  413. m.ocspStapling = false
  414. if len(data) == 0 {
  415. // ServerHello is optionally followed by extension data
  416. return true
  417. }
  418. if len(data) < 2 {
  419. return false
  420. }
  421. extensionsLength := int(data[0])<<8 | int(data[1])
  422. data = data[2:]
  423. if len(data) != extensionsLength {
  424. return false
  425. }
  426. for len(data) != 0 {
  427. if len(data) < 4 {
  428. return false
  429. }
  430. extension := uint16(data[0])<<8 | uint16(data[1])
  431. length := int(data[2])<<8 | int(data[3])
  432. data = data[4:]
  433. if len(data) < length {
  434. return false
  435. }
  436. switch extension {
  437. case extensionNextProtoNeg:
  438. m.nextProtoNeg = true
  439. d := data
  440. for len(d) > 0 {
  441. l := int(d[0])
  442. d = d[1:]
  443. if l == 0 || l > len(d) {
  444. return false
  445. }
  446. m.nextProtos = append(m.nextProtos, string(d[0:l]))
  447. d = d[l:]
  448. }
  449. case extensionStatusRequest:
  450. if length > 0 {
  451. return false
  452. }
  453. m.ocspStapling = true
  454. }
  455. data = data[length:]
  456. }
  457. return true
  458. }
  459. type certificateMsg struct {
  460. raw []byte
  461. certificates [][]byte
  462. }
  463. func (m *certificateMsg) equal(i interface{}) bool {
  464. m1, ok := i.(*certificateMsg)
  465. if !ok {
  466. return false
  467. }
  468. return bytes.Equal(m.raw, m1.raw) &&
  469. eqByteSlices(m.certificates, m1.certificates)
  470. }
  471. func (m *certificateMsg) marshal() (x []byte) {
  472. if m.raw != nil {
  473. return m.raw
  474. }
  475. var i int
  476. for _, slice := range m.certificates {
  477. i += len(slice)
  478. }
  479. length := 3 + 3*len(m.certificates) + i
  480. x = make([]byte, 4+length)
  481. x[0] = typeCertificate
  482. x[1] = uint8(length >> 16)
  483. x[2] = uint8(length >> 8)
  484. x[3] = uint8(length)
  485. certificateOctets := length - 3
  486. x[4] = uint8(certificateOctets >> 16)
  487. x[5] = uint8(certificateOctets >> 8)
  488. x[6] = uint8(certificateOctets)
  489. y := x[7:]
  490. for _, slice := range m.certificates {
  491. y[0] = uint8(len(slice) >> 16)
  492. y[1] = uint8(len(slice) >> 8)
  493. y[2] = uint8(len(slice))
  494. copy(y[3:], slice)
  495. y = y[3+len(slice):]
  496. }
  497. m.raw = x
  498. return
  499. }
  500. func (m *certificateMsg) unmarshal(data []byte) bool {
  501. if len(data) < 7 {
  502. return false
  503. }
  504. m.raw = data
  505. certsLen := uint32(data[4])<<16 | uint32(data[5])<<8 | uint32(data[6])
  506. if uint32(len(data)) != certsLen+7 {
  507. return false
  508. }
  509. numCerts := 0
  510. d := data[7:]
  511. for certsLen > 0 {
  512. if len(d) < 4 {
  513. return false
  514. }
  515. certLen := uint32(d[0])<<24 | uint32(d[1])<<8 | uint32(d[2])
  516. if uint32(len(d)) < 3+certLen {
  517. return false
  518. }
  519. d = d[3+certLen:]
  520. certsLen -= 3 + certLen
  521. numCerts++
  522. }
  523. m.certificates = make([][]byte, numCerts)
  524. d = data[7:]
  525. for i := 0; i < numCerts; i++ {
  526. certLen := uint32(d[0])<<24 | uint32(d[1])<<8 | uint32(d[2])
  527. m.certificates[i] = d[3 : 3+certLen]
  528. d = d[3+certLen:]
  529. }
  530. return true
  531. }
  532. type serverKeyExchangeMsg struct {
  533. raw []byte
  534. key []byte
  535. }
  536. func (m *serverKeyExchangeMsg) equal(i interface{}) bool {
  537. m1, ok := i.(*serverKeyExchangeMsg)
  538. if !ok {
  539. return false
  540. }
  541. return bytes.Equal(m.raw, m1.raw) &&
  542. bytes.Equal(m.key, m1.key)
  543. }
  544. func (m *serverKeyExchangeMsg) marshal() []byte {
  545. if m.raw != nil {
  546. return m.raw
  547. }
  548. length := len(m.key)
  549. x := make([]byte, length+4)
  550. x[0] = typeServerKeyExchange
  551. x[1] = uint8(length >> 16)
  552. x[2] = uint8(length >> 8)
  553. x[3] = uint8(length)
  554. copy(x[4:], m.key)
  555. m.raw = x
  556. return x
  557. }
  558. func (m *serverKeyExchangeMsg) unmarshal(data []byte) bool {
  559. m.raw = data
  560. if len(data) < 4 {
  561. return false
  562. }
  563. m.key = data[4:]
  564. return true
  565. }
  566. type certificateStatusMsg struct {
  567. raw []byte
  568. statusType uint8
  569. response []byte
  570. }
  571. func (m *certificateStatusMsg) equal(i interface{}) bool {
  572. m1, ok := i.(*certificateStatusMsg)
  573. if !ok {
  574. return false
  575. }
  576. return bytes.Equal(m.raw, m1.raw) &&
  577. m.statusType == m1.statusType &&
  578. bytes.Equal(m.response, m1.response)
  579. }
  580. func (m *certificateStatusMsg) marshal() []byte {
  581. if m.raw != nil {
  582. return m.raw
  583. }
  584. var x []byte
  585. if m.statusType == statusTypeOCSP {
  586. x = make([]byte, 4+4+len(m.response))
  587. x[0] = typeCertificateStatus
  588. l := len(m.response) + 4
  589. x[1] = byte(l >> 16)
  590. x[2] = byte(l >> 8)
  591. x[3] = byte(l)
  592. x[4] = statusTypeOCSP
  593. l -= 4
  594. x[5] = byte(l >> 16)
  595. x[6] = byte(l >> 8)
  596. x[7] = byte(l)
  597. copy(x[8:], m.response)
  598. } else {
  599. x = []byte{typeCertificateStatus, 0, 0, 1, m.statusType}
  600. }
  601. m.raw = x
  602. return x
  603. }
  604. func (m *certificateStatusMsg) unmarshal(data []byte) bool {
  605. m.raw = data
  606. if len(data) < 5 {
  607. return false
  608. }
  609. m.statusType = data[4]
  610. m.response = nil
  611. if m.statusType == statusTypeOCSP {
  612. if len(data) < 8 {
  613. return false
  614. }
  615. respLen := uint32(data[5])<<16 | uint32(data[6])<<8 | uint32(data[7])
  616. if uint32(len(data)) != 4+4+respLen {
  617. return false
  618. }
  619. m.response = data[8:]
  620. }
  621. return true
  622. }
  623. type serverHelloDoneMsg struct{}
  624. func (m *serverHelloDoneMsg) equal(i interface{}) bool {
  625. _, ok := i.(*serverHelloDoneMsg)
  626. return ok
  627. }
  628. func (m *serverHelloDoneMsg) marshal() []byte {
  629. x := make([]byte, 4)
  630. x[0] = typeServerHelloDone
  631. return x
  632. }
  633. func (m *serverHelloDoneMsg) unmarshal(data []byte) bool {
  634. return len(data) == 4
  635. }
  636. type clientKeyExchangeMsg struct {
  637. raw []byte
  638. ciphertext []byte
  639. }
  640. func (m *clientKeyExchangeMsg) equal(i interface{}) bool {
  641. m1, ok := i.(*clientKeyExchangeMsg)
  642. if !ok {
  643. return false
  644. }
  645. return bytes.Equal(m.raw, m1.raw) &&
  646. bytes.Equal(m.ciphertext, m1.ciphertext)
  647. }
  648. func (m *clientKeyExchangeMsg) marshal() []byte {
  649. if m.raw != nil {
  650. return m.raw
  651. }
  652. length := len(m.ciphertext)
  653. x := make([]byte, length+4)
  654. x[0] = typeClientKeyExchange
  655. x[1] = uint8(length >> 16)
  656. x[2] = uint8(length >> 8)
  657. x[3] = uint8(length)
  658. copy(x[4:], m.ciphertext)
  659. m.raw = x
  660. return x
  661. }
  662. func (m *clientKeyExchangeMsg) unmarshal(data []byte) bool {
  663. m.raw = data
  664. if len(data) < 4 {
  665. return false
  666. }
  667. l := int(data[1])<<16 | int(data[2])<<8 | int(data[3])
  668. if l != len(data)-4 {
  669. return false
  670. }
  671. m.ciphertext = data[4:]
  672. return true
  673. }
  674. type finishedMsg struct {
  675. raw []byte
  676. verifyData []byte
  677. }
  678. func (m *finishedMsg) equal(i interface{}) bool {
  679. m1, ok := i.(*finishedMsg)
  680. if !ok {
  681. return false
  682. }
  683. return bytes.Equal(m.raw, m1.raw) &&
  684. bytes.Equal(m.verifyData, m1.verifyData)
  685. }
  686. func (m *finishedMsg) marshal() (x []byte) {
  687. if m.raw != nil {
  688. return m.raw
  689. }
  690. x = make([]byte, 4+len(m.verifyData))
  691. x[0] = typeFinished
  692. x[3] = byte(len(m.verifyData))
  693. copy(x[4:], m.verifyData)
  694. m.raw = x
  695. return
  696. }
  697. func (m *finishedMsg) unmarshal(data []byte) bool {
  698. m.raw = data
  699. if len(data) < 4 {
  700. return false
  701. }
  702. m.verifyData = data[4:]
  703. return true
  704. }
  705. type nextProtoMsg struct {
  706. raw []byte
  707. proto string
  708. }
  709. func (m *nextProtoMsg) equal(i interface{}) bool {
  710. m1, ok := i.(*nextProtoMsg)
  711. if !ok {
  712. return false
  713. }
  714. return bytes.Equal(m.raw, m1.raw) &&
  715. m.proto == m1.proto
  716. }
  717. func (m *nextProtoMsg) marshal() []byte {
  718. if m.raw != nil {
  719. return m.raw
  720. }
  721. l := len(m.proto)
  722. if l > 255 {
  723. l = 255
  724. }
  725. padding := 32 - (l+2)%32
  726. length := l + padding + 2
  727. x := make([]byte, length+4)
  728. x[0] = typeNextProtocol
  729. x[1] = uint8(length >> 16)
  730. x[2] = uint8(length >> 8)
  731. x[3] = uint8(length)
  732. y := x[4:]
  733. y[0] = byte(l)
  734. copy(y[1:], []byte(m.proto[0:l]))
  735. y = y[1+l:]
  736. y[0] = byte(padding)
  737. m.raw = x
  738. return x
  739. }
  740. func (m *nextProtoMsg) unmarshal(data []byte) bool {
  741. m.raw = data
  742. if len(data) < 5 {
  743. return false
  744. }
  745. data = data[4:]
  746. protoLen := int(data[0])
  747. data = data[1:]
  748. if len(data) < protoLen {
  749. return false
  750. }
  751. m.proto = string(data[0:protoLen])
  752. data = data[protoLen:]
  753. if len(data) < 1 {
  754. return false
  755. }
  756. paddingLen := int(data[0])
  757. data = data[1:]
  758. if len(data) != paddingLen {
  759. return false
  760. }
  761. return true
  762. }
  763. type certificateRequestMsg struct {
  764. raw []byte
  765. certificateTypes []byte
  766. certificateAuthorities [][]byte
  767. }
  768. func (m *certificateRequestMsg) equal(i interface{}) bool {
  769. m1, ok := i.(*certificateRequestMsg)
  770. if !ok {
  771. return false
  772. }
  773. return bytes.Equal(m.raw, m1.raw) &&
  774. bytes.Equal(m.certificateTypes, m1.certificateTypes) &&
  775. eqByteSlices(m.certificateAuthorities, m1.certificateAuthorities)
  776. }
  777. func (m *certificateRequestMsg) marshal() (x []byte) {
  778. if m.raw != nil {
  779. return m.raw
  780. }
  781. // See http://tools.ietf.org/html/rfc4346#section-7.4.4
  782. length := 1 + len(m.certificateTypes) + 2
  783. for _, ca := range m.certificateAuthorities {
  784. length += 2 + len(ca)
  785. }
  786. x = make([]byte, 4+length)
  787. x[0] = typeCertificateRequest
  788. x[1] = uint8(length >> 16)
  789. x[2] = uint8(length >> 8)
  790. x[3] = uint8(length)
  791. x[4] = uint8(len(m.certificateTypes))
  792. copy(x[5:], m.certificateTypes)
  793. y := x[5+len(m.certificateTypes):]
  794. numCA := len(m.certificateAuthorities)
  795. y[0] = uint8(numCA >> 8)
  796. y[1] = uint8(numCA)
  797. y = y[2:]
  798. for _, ca := range m.certificateAuthorities {
  799. y[0] = uint8(len(ca) >> 8)
  800. y[1] = uint8(len(ca))
  801. y = y[2:]
  802. copy(y, ca)
  803. y = y[len(ca):]
  804. }
  805. m.raw = x
  806. return
  807. }
  808. func (m *certificateRequestMsg) unmarshal(data []byte) bool {
  809. m.raw = data
  810. if len(data) < 5 {
  811. return false
  812. }
  813. length := uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
  814. if uint32(len(data))-4 != length {
  815. return false
  816. }
  817. numCertTypes := int(data[4])
  818. data = data[5:]
  819. if numCertTypes == 0 || len(data) <= numCertTypes {
  820. return false
  821. }
  822. m.certificateTypes = make([]byte, numCertTypes)
  823. if copy(m.certificateTypes, data) != numCertTypes {
  824. return false
  825. }
  826. data = data[numCertTypes:]
  827. if len(data) < 2 {
  828. return false
  829. }
  830. numCAs := uint16(data[0])<<16 | uint16(data[1])
  831. data = data[2:]
  832. m.certificateAuthorities = make([][]byte, numCAs)
  833. for i := uint16(0); i < numCAs; i++ {
  834. if len(data) < 2 {
  835. return false
  836. }
  837. caLen := uint16(data[0])<<16 | uint16(data[1])
  838. data = data[2:]
  839. if len(data) < int(caLen) {
  840. return false
  841. }
  842. ca := make([]byte, caLen)
  843. copy(ca, data)
  844. m.certificateAuthorities[i] = ca
  845. data = data[caLen:]
  846. }
  847. if len(data) > 0 {
  848. return false
  849. }
  850. return true
  851. }
  852. type certificateVerifyMsg struct {
  853. raw []byte
  854. signature []byte
  855. }
  856. func (m *certificateVerifyMsg) equal(i interface{}) bool {
  857. m1, ok := i.(*certificateVerifyMsg)
  858. if !ok {
  859. return false
  860. }
  861. return bytes.Equal(m.raw, m1.raw) &&
  862. bytes.Equal(m.signature, m1.signature)
  863. }
  864. func (m *certificateVerifyMsg) marshal() (x []byte) {
  865. if m.raw != nil {
  866. return m.raw
  867. }
  868. // See http://tools.ietf.org/html/rfc4346#section-7.4.8
  869. siglength := len(m.signature)
  870. length := 2 + siglength
  871. x = make([]byte, 4+length)
  872. x[0] = typeCertificateVerify
  873. x[1] = uint8(length >> 16)
  874. x[2] = uint8(length >> 8)
  875. x[3] = uint8(length)
  876. x[4] = uint8(siglength >> 8)
  877. x[5] = uint8(siglength)
  878. copy(x[6:], m.signature)
  879. m.raw = x
  880. return
  881. }
  882. func (m *certificateVerifyMsg) unmarshal(data []byte) bool {
  883. m.raw = data
  884. if len(data) < 6 {
  885. return false
  886. }
  887. length := uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
  888. if uint32(len(data))-4 != length {
  889. return false
  890. }
  891. siglength := int(data[4])<<8 + int(data[5])
  892. if len(data)-6 != siglength {
  893. return false
  894. }
  895. m.signature = data[6:]
  896. return true
  897. }
  898. func eqUint16s(x, y []uint16) bool {
  899. if len(x) != len(y) {
  900. return false
  901. }
  902. for i, v := range x {
  903. if y[i] != v {
  904. return false
  905. }
  906. }
  907. return true
  908. }
  909. func eqStrings(x, y []string) bool {
  910. if len(x) != len(y) {
  911. return false
  912. }
  913. for i, v := range x {
  914. if y[i] != v {
  915. return false
  916. }
  917. }
  918. return true
  919. }
  920. func eqByteSlices(x, y [][]byte) bool {
  921. if len(x) != len(y) {
  922. return false
  923. }
  924. for i, v := range x {
  925. if !bytes.Equal(v, y[i]) {
  926. return false
  927. }
  928. }
  929. return true
  930. }