Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

582 Zeilen
11 KiB

  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. }
  15. func (m *clientHelloMsg) marshal() []byte {
  16. if m.raw != nil {
  17. return m.raw
  18. }
  19. length := 2 + 32 + 1 + len(m.sessionId) + 2 + len(m.cipherSuites)*2 + 1 + len(m.compressionMethods)
  20. numExtensions := 0
  21. extensionsLength := 0
  22. if m.nextProtoNeg {
  23. numExtensions++
  24. }
  25. if len(m.serverName) > 0 {
  26. extensionsLength += 5 + len(m.serverName)
  27. numExtensions++
  28. }
  29. if numExtensions > 0 {
  30. extensionsLength += 4 * numExtensions
  31. length += 2 + extensionsLength
  32. }
  33. x := make([]byte, 4+length)
  34. x[0] = typeClientHello
  35. x[1] = uint8(length >> 16)
  36. x[2] = uint8(length >> 8)
  37. x[3] = uint8(length)
  38. x[4] = uint8(m.vers >> 8)
  39. x[5] = uint8(m.vers)
  40. copy(x[6:38], m.random)
  41. x[38] = uint8(len(m.sessionId))
  42. copy(x[39:39+len(m.sessionId)], m.sessionId)
  43. y := x[39+len(m.sessionId):]
  44. y[0] = uint8(len(m.cipherSuites) >> 7)
  45. y[1] = uint8(len(m.cipherSuites) << 1)
  46. for i, suite := range m.cipherSuites {
  47. y[2+i*2] = uint8(suite >> 8)
  48. y[3+i*2] = uint8(suite)
  49. }
  50. z := y[2+len(m.cipherSuites)*2:]
  51. z[0] = uint8(len(m.compressionMethods))
  52. copy(z[1:], m.compressionMethods)
  53. z = z[1+len(m.compressionMethods):]
  54. if numExtensions > 0 {
  55. z[0] = byte(extensionsLength >> 8)
  56. z[1] = byte(extensionsLength)
  57. z = z[2:]
  58. }
  59. if m.nextProtoNeg {
  60. z[0] = byte(extensionNextProtoNeg >> 8)
  61. z[1] = byte(extensionNextProtoNeg)
  62. // The length is always 0
  63. z = z[4:]
  64. }
  65. if len(m.serverName) > 0 {
  66. z[0] = byte(extensionServerName >> 8)
  67. z[1] = byte(extensionServerName)
  68. l := len(m.serverName) + 5
  69. z[2] = byte(l >> 8)
  70. z[3] = byte(l)
  71. z = z[4:]
  72. // RFC 3546, section 3.1
  73. //
  74. // struct {
  75. // NameType name_type;
  76. // select (name_type) {
  77. // case host_name: HostName;
  78. // } name;
  79. // } ServerName;
  80. //
  81. // enum {
  82. // host_name(0), (255)
  83. // } NameType;
  84. //
  85. // opaque HostName<1..2^16-1>;
  86. //
  87. // struct {
  88. // ServerName server_name_list<1..2^16-1>
  89. // } ServerNameList;
  90. z[1] = 1
  91. z[3] = byte(len(m.serverName) >> 8)
  92. z[4] = byte(len(m.serverName))
  93. copy(z[5:], []byte(m.serverName))
  94. z = z[l:]
  95. }
  96. m.raw = x
  97. return x
  98. }
  99. func (m *clientHelloMsg) unmarshal(data []byte) bool {
  100. if len(data) < 42 {
  101. return false
  102. }
  103. m.raw = data
  104. m.vers = uint16(data[4])<<8 | uint16(data[5])
  105. m.random = data[6:38]
  106. sessionIdLen := int(data[38])
  107. if sessionIdLen > 32 || len(data) < 39+sessionIdLen {
  108. return false
  109. }
  110. m.sessionId = data[39 : 39+sessionIdLen]
  111. data = data[39+sessionIdLen:]
  112. if len(data) < 2 {
  113. return false
  114. }
  115. // cipherSuiteLen is the number of bytes of cipher suite numbers. Since
  116. // they are uint16s, the number must be even.
  117. cipherSuiteLen := int(data[0])<<8 | int(data[1])
  118. if cipherSuiteLen%2 == 1 || len(data) < 2+cipherSuiteLen {
  119. return false
  120. }
  121. numCipherSuites := cipherSuiteLen / 2
  122. m.cipherSuites = make([]uint16, numCipherSuites)
  123. for i := 0; i < numCipherSuites; i++ {
  124. m.cipherSuites[i] = uint16(data[2+2*i])<<8 | uint16(data[3+2*i])
  125. }
  126. data = data[2+cipherSuiteLen:]
  127. if len(data) < 1 {
  128. return false
  129. }
  130. compressionMethodsLen := int(data[0])
  131. if len(data) < 1+compressionMethodsLen {
  132. return false
  133. }
  134. m.compressionMethods = data[1 : 1+compressionMethodsLen]
  135. data = data[1+compressionMethodsLen:]
  136. m.nextProtoNeg = false
  137. m.serverName = ""
  138. if len(data) == 0 {
  139. // ClientHello is optionally followed by extension data
  140. return true
  141. }
  142. if len(data) < 2 {
  143. return false
  144. }
  145. extensionsLength := int(data[0])<<8 | int(data[1])
  146. data = data[2:]
  147. if extensionsLength != len(data) {
  148. return false
  149. }
  150. for len(data) != 0 {
  151. if len(data) < 4 {
  152. return false
  153. }
  154. extension := uint16(data[0])<<8 | uint16(data[1])
  155. length := int(data[2])<<8 | int(data[3])
  156. data = data[4:]
  157. if len(data) < length {
  158. return false
  159. }
  160. switch extension {
  161. case extensionServerName:
  162. if length < 2 {
  163. return false
  164. }
  165. numNames := int(data[0])<<8 | int(data[1])
  166. d := data[2:]
  167. for i := 0; i < numNames; i++ {
  168. if len(d) < 3 {
  169. return false
  170. }
  171. nameType := d[0]
  172. nameLen := int(d[1])<<8 | int(d[2])
  173. d = d[3:]
  174. if len(d) < nameLen {
  175. return false
  176. }
  177. if nameType == 0 {
  178. m.serverName = string(d[0:nameLen])
  179. break
  180. }
  181. d = d[nameLen:]
  182. }
  183. case extensionNextProtoNeg:
  184. if length > 0 {
  185. return false
  186. }
  187. m.nextProtoNeg = true
  188. }
  189. data = data[length:]
  190. }
  191. return true
  192. }
  193. type serverHelloMsg struct {
  194. raw []byte
  195. vers uint16
  196. random []byte
  197. sessionId []byte
  198. cipherSuite uint16
  199. compressionMethod uint8
  200. nextProtoNeg bool
  201. nextProtos []string
  202. }
  203. func (m *serverHelloMsg) marshal() []byte {
  204. if m.raw != nil {
  205. return m.raw
  206. }
  207. length := 38 + len(m.sessionId)
  208. numExtensions := 0
  209. extensionsLength := 0
  210. nextProtoLen := 0
  211. if m.nextProtoNeg {
  212. numExtensions++
  213. for _, v := range m.nextProtos {
  214. nextProtoLen += len(v)
  215. }
  216. nextProtoLen += len(m.nextProtos)
  217. extensionsLength += nextProtoLen
  218. }
  219. if numExtensions > 0 {
  220. extensionsLength += 4 * numExtensions
  221. length += 2 + extensionsLength
  222. }
  223. x := make([]byte, 4+length)
  224. x[0] = typeServerHello
  225. x[1] = uint8(length >> 16)
  226. x[2] = uint8(length >> 8)
  227. x[3] = uint8(length)
  228. x[4] = uint8(m.vers >> 8)
  229. x[5] = uint8(m.vers)
  230. copy(x[6:38], m.random)
  231. x[38] = uint8(len(m.sessionId))
  232. copy(x[39:39+len(m.sessionId)], m.sessionId)
  233. z := x[39+len(m.sessionId):]
  234. z[0] = uint8(m.cipherSuite >> 8)
  235. z[1] = uint8(m.cipherSuite)
  236. z[2] = uint8(m.compressionMethod)
  237. z = z[3:]
  238. if numExtensions > 0 {
  239. z[0] = byte(extensionsLength >> 8)
  240. z[1] = byte(extensionsLength)
  241. z = z[2:]
  242. }
  243. if m.nextProtoNeg {
  244. z[0] = byte(extensionNextProtoNeg >> 8)
  245. z[1] = byte(extensionNextProtoNeg)
  246. z[2] = byte(nextProtoLen >> 8)
  247. z[3] = byte(nextProtoLen)
  248. z = z[4:]
  249. for _, v := range m.nextProtos {
  250. l := len(v)
  251. if l > 255 {
  252. l = 255
  253. }
  254. z[0] = byte(l)
  255. copy(z[1:], []byte(v[0:l]))
  256. z = z[1+l:]
  257. }
  258. }
  259. m.raw = x
  260. return x
  261. }
  262. func append(slice []string, elem string) []string {
  263. if len(slice) < cap(slice) {
  264. slice = slice[0 : len(slice)+1]
  265. slice[len(slice)-1] = elem
  266. return slice
  267. }
  268. fresh := make([]string, len(slice)+1, cap(slice)*2+1)
  269. copy(fresh, slice)
  270. fresh[len(slice)] = elem
  271. return fresh
  272. }
  273. func (m *serverHelloMsg) unmarshal(data []byte) bool {
  274. if len(data) < 42 {
  275. return false
  276. }
  277. m.raw = data
  278. m.vers = uint16(data[4])<<8 | uint16(data[5])
  279. m.random = data[6:38]
  280. sessionIdLen := int(data[38])
  281. if sessionIdLen > 32 || len(data) < 39+sessionIdLen {
  282. return false
  283. }
  284. m.sessionId = data[39 : 39+sessionIdLen]
  285. data = data[39+sessionIdLen:]
  286. if len(data) < 3 {
  287. return false
  288. }
  289. m.cipherSuite = uint16(data[0])<<8 | uint16(data[1])
  290. m.compressionMethod = data[2]
  291. data = data[3:]
  292. m.nextProtoNeg = false
  293. m.nextProtos = nil
  294. if len(data) == 0 {
  295. // ServerHello is optionally followed by extension data
  296. return true
  297. }
  298. if len(data) < 2 {
  299. return false
  300. }
  301. extensionsLength := int(data[0])<<8 | int(data[1])
  302. data = data[2:]
  303. if len(data) != extensionsLength {
  304. return false
  305. }
  306. for len(data) != 0 {
  307. if len(data) < 4 {
  308. return false
  309. }
  310. extension := uint16(data[0])<<8 | uint16(data[1])
  311. length := int(data[2])<<8 | int(data[3])
  312. data = data[4:]
  313. if len(data) < length {
  314. return false
  315. }
  316. switch extension {
  317. case extensionNextProtoNeg:
  318. m.nextProtoNeg = true
  319. d := data
  320. for len(d) > 0 {
  321. l := int(d[0])
  322. d = d[1:]
  323. if l == 0 || l > len(d) {
  324. return false
  325. }
  326. m.nextProtos = append(m.nextProtos, string(d[0:l]))
  327. d = d[l:]
  328. }
  329. }
  330. data = data[length:]
  331. }
  332. return true
  333. }
  334. type certificateMsg struct {
  335. raw []byte
  336. certificates [][]byte
  337. }
  338. func (m *certificateMsg) marshal() (x []byte) {
  339. if m.raw != nil {
  340. return m.raw
  341. }
  342. var i int
  343. for _, slice := range m.certificates {
  344. i += len(slice)
  345. }
  346. length := 3 + 3*len(m.certificates) + i
  347. x = make([]byte, 4+length)
  348. x[0] = typeCertificate
  349. x[1] = uint8(length >> 16)
  350. x[2] = uint8(length >> 8)
  351. x[3] = uint8(length)
  352. certificateOctets := length - 3
  353. x[4] = uint8(certificateOctets >> 16)
  354. x[5] = uint8(certificateOctets >> 8)
  355. x[6] = uint8(certificateOctets)
  356. y := x[7:]
  357. for _, slice := range m.certificates {
  358. y[0] = uint8(len(slice) >> 16)
  359. y[1] = uint8(len(slice) >> 8)
  360. y[2] = uint8(len(slice))
  361. copy(y[3:], slice)
  362. y = y[3+len(slice):]
  363. }
  364. m.raw = x
  365. return
  366. }
  367. func (m *certificateMsg) unmarshal(data []byte) bool {
  368. if len(data) < 7 {
  369. return false
  370. }
  371. m.raw = data
  372. certsLen := uint32(data[4])<<16 | uint32(data[5])<<8 | uint32(data[6])
  373. if uint32(len(data)) != certsLen+7 {
  374. return false
  375. }
  376. numCerts := 0
  377. d := data[7:]
  378. for certsLen > 0 {
  379. if len(d) < 4 {
  380. return false
  381. }
  382. certLen := uint32(d[0])<<24 | uint32(d[1])<<8 | uint32(d[2])
  383. if uint32(len(d)) < 3+certLen {
  384. return false
  385. }
  386. d = d[3+certLen:]
  387. certsLen -= 3 + certLen
  388. numCerts++
  389. }
  390. m.certificates = make([][]byte, numCerts)
  391. d = data[7:]
  392. for i := 0; i < numCerts; i++ {
  393. certLen := uint32(d[0])<<24 | uint32(d[1])<<8 | uint32(d[2])
  394. m.certificates[i] = d[3 : 3+certLen]
  395. d = d[3+certLen:]
  396. }
  397. return true
  398. }
  399. type serverHelloDoneMsg struct{}
  400. func (m *serverHelloDoneMsg) marshal() []byte {
  401. x := make([]byte, 4)
  402. x[0] = typeServerHelloDone
  403. return x
  404. }
  405. func (m *serverHelloDoneMsg) unmarshal(data []byte) bool {
  406. return len(data) == 4
  407. }
  408. type clientKeyExchangeMsg struct {
  409. raw []byte
  410. ciphertext []byte
  411. }
  412. func (m *clientKeyExchangeMsg) marshal() []byte {
  413. if m.raw != nil {
  414. return m.raw
  415. }
  416. length := len(m.ciphertext) + 2
  417. x := make([]byte, length+4)
  418. x[0] = typeClientKeyExchange
  419. x[1] = uint8(length >> 16)
  420. x[2] = uint8(length >> 8)
  421. x[3] = uint8(length)
  422. x[4] = uint8(len(m.ciphertext) >> 8)
  423. x[5] = uint8(len(m.ciphertext))
  424. copy(x[6:], m.ciphertext)
  425. m.raw = x
  426. return x
  427. }
  428. func (m *clientKeyExchangeMsg) unmarshal(data []byte) bool {
  429. m.raw = data
  430. if len(data) < 7 {
  431. return false
  432. }
  433. cipherTextLen := int(data[4])<<8 | int(data[5])
  434. if len(data) != 6+cipherTextLen {
  435. return false
  436. }
  437. m.ciphertext = data[6:]
  438. return true
  439. }
  440. type finishedMsg struct {
  441. raw []byte
  442. verifyData []byte
  443. }
  444. func (m *finishedMsg) marshal() (x []byte) {
  445. if m.raw != nil {
  446. return m.raw
  447. }
  448. x = make([]byte, 16)
  449. x[0] = typeFinished
  450. x[3] = 12
  451. copy(x[4:], m.verifyData)
  452. m.raw = x
  453. return
  454. }
  455. func (m *finishedMsg) unmarshal(data []byte) bool {
  456. m.raw = data
  457. if len(data) != 4+12 {
  458. return false
  459. }
  460. m.verifyData = data[4:]
  461. return true
  462. }
  463. type nextProtoMsg struct {
  464. raw []byte
  465. proto string
  466. }
  467. func (m *nextProtoMsg) marshal() []byte {
  468. if m.raw != nil {
  469. return m.raw
  470. }
  471. l := len(m.proto)
  472. if l > 255 {
  473. l = 255
  474. }
  475. padding := 32 - (l+2)%32
  476. length := l + padding + 2
  477. x := make([]byte, length+4)
  478. x[0] = typeNextProtocol
  479. x[1] = uint8(length >> 16)
  480. x[2] = uint8(length >> 8)
  481. x[3] = uint8(length)
  482. y := x[4:]
  483. y[0] = byte(l)
  484. copy(y[1:], []byte(m.proto[0:l]))
  485. y = y[1+l:]
  486. y[0] = byte(padding)
  487. m.raw = x
  488. return x
  489. }
  490. func (m *nextProtoMsg) unmarshal(data []byte) bool {
  491. m.raw = data
  492. if len(data) < 5 {
  493. return false
  494. }
  495. data = data[4:]
  496. protoLen := int(data[0])
  497. data = data[1:]
  498. if len(data) < protoLen {
  499. return false
  500. }
  501. m.proto = string(data[0:protoLen])
  502. data = data[protoLen:]
  503. if len(data) < 1 {
  504. return false
  505. }
  506. paddingLen := int(data[0])
  507. data = data[1:]
  508. if len(data) != paddingLen {
  509. return false
  510. }
  511. return true
  512. }