Alternative TLS implementation in Go
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

handshake_messages.go 18 KiB

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