Alternative TLS implementation in Go
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

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