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

2683 行
60 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. import (
  6. "bytes"
  7. "encoding/binary"
  8. "strings"
  9. )
  10. // signAlgosCertList helper function returns either list of signature algorithms in case
  11. // signature_algorithms_cert extension should be marshalled or nil in the other case.
  12. // signAlgos is a list of algorithms from signature_algorithms extension. signAlgosCert is a list
  13. // of algorithms from signature_algorithms_cert extension.
  14. func signAlgosCertList(signAlgos, signAlgosCert []SignatureScheme) []SignatureScheme {
  15. if eqSignatureAlgorithms(signAlgos, signAlgosCert) {
  16. // ensure that only supported_algorithms extension is send if supported_algorithms_cert
  17. // has identical content
  18. return nil
  19. }
  20. return signAlgosCert
  21. }
  22. type clientHelloMsg struct {
  23. raw []byte
  24. rawTruncated []byte // for PSK binding
  25. vers uint16
  26. random []byte
  27. sessionId []byte
  28. cipherSuites []uint16
  29. compressionMethods []uint8
  30. nextProtoNeg bool
  31. serverName string
  32. ocspStapling bool
  33. scts bool
  34. supportedCurves []CurveID
  35. supportedPoints []uint8
  36. ticketSupported bool
  37. sessionTicket []uint8
  38. supportedSignatureAlgorithms []SignatureScheme
  39. supportedSignatureAlgorithmsCert []SignatureScheme
  40. secureRenegotiation []byte
  41. secureRenegotiationSupported bool
  42. alpnProtocols []string
  43. keyShares []keyShare
  44. supportedVersions []uint16
  45. psks []psk
  46. pskKeyExchangeModes []uint8
  47. earlyData bool
  48. }
  49. // Helpers
  50. // Function used for signature_algorithms and signature_algorithrms_cert extensions only
  51. // (for more details, see TLS 1.3 draft 28, 4.2.3)
  52. // Function advances data slice and returns it, so that it can be used for further processing
  53. func marshalExtensionSignatureAlgorithms(extension uint16, data []byte, schemes []SignatureScheme) []byte {
  54. algNum := uint16(len(schemes))
  55. if algNum == 0 {
  56. return data
  57. }
  58. binary.BigEndian.PutUint16(data, extension)
  59. data = data[2:]
  60. binary.BigEndian.PutUint16(data, (2*algNum)+2) // +1 for length
  61. data = data[2:]
  62. binary.BigEndian.PutUint16(data, (2 * algNum))
  63. data = data[2:]
  64. for _, algo := range schemes {
  65. binary.BigEndian.PutUint16(data, uint16(algo))
  66. data = data[2:]
  67. }
  68. return data
  69. }
  70. // Function used for unmarshalling signature_algorithms or signature_algorithms_cert extensions only
  71. // (for more details, see TLS 1.3 draft 28, 4.2.3)
  72. // In case of error function returns alertDecoderError otherwise filled SignatureScheme slice and alertSuccess
  73. func unmarshalExtensionSignatureAlgorithms(data []byte, length int) ([]SignatureScheme, alert) {
  74. if length < 2 || length&1 != 0 {
  75. return nil, alertDecodeError
  76. }
  77. algLen := binary.BigEndian.Uint16(data)
  78. idx := 2
  79. if int(algLen) != length-2 {
  80. return nil, alertDecodeError
  81. }
  82. schemes := make([]SignatureScheme, algLen/2)
  83. for i := range schemes {
  84. schemes[i] = SignatureScheme(binary.BigEndian.Uint16(data[idx:]))
  85. idx += 2
  86. }
  87. return schemes, alertSuccess
  88. }
  89. func (m *clientHelloMsg) equal(i interface{}) bool {
  90. m1, ok := i.(*clientHelloMsg)
  91. if !ok {
  92. return false
  93. }
  94. return bytes.Equal(m.raw, m1.raw) &&
  95. m.vers == m1.vers &&
  96. bytes.Equal(m.random, m1.random) &&
  97. bytes.Equal(m.sessionId, m1.sessionId) &&
  98. eqUint16s(m.cipherSuites, m1.cipherSuites) &&
  99. bytes.Equal(m.compressionMethods, m1.compressionMethods) &&
  100. m.nextProtoNeg == m1.nextProtoNeg &&
  101. m.serverName == m1.serverName &&
  102. m.ocspStapling == m1.ocspStapling &&
  103. m.scts == m1.scts &&
  104. eqCurveIDs(m.supportedCurves, m1.supportedCurves) &&
  105. bytes.Equal(m.supportedPoints, m1.supportedPoints) &&
  106. m.ticketSupported == m1.ticketSupported &&
  107. bytes.Equal(m.sessionTicket, m1.sessionTicket) &&
  108. eqSignatureAlgorithms(m.supportedSignatureAlgorithms, m1.supportedSignatureAlgorithms) &&
  109. eqSignatureAlgorithms(m.supportedSignatureAlgorithmsCert, m1.supportedSignatureAlgorithmsCert) &&
  110. m.secureRenegotiationSupported == m1.secureRenegotiationSupported &&
  111. bytes.Equal(m.secureRenegotiation, m1.secureRenegotiation) &&
  112. eqStrings(m.alpnProtocols, m1.alpnProtocols) &&
  113. eqKeyShares(m.keyShares, m1.keyShares) &&
  114. eqUint16s(m.supportedVersions, m1.supportedVersions) &&
  115. m.earlyData == m1.earlyData
  116. }
  117. func (m *clientHelloMsg) marshal() []byte {
  118. if m.raw != nil {
  119. return m.raw
  120. }
  121. length := 2 + 32 + 1 + len(m.sessionId) + 2 + len(m.cipherSuites)*2 + 1 + len(m.compressionMethods)
  122. numExtensions := 0
  123. extensionsLength := 0
  124. // Indicates wether to send signature_algorithms_cert extension
  125. if m.nextProtoNeg {
  126. numExtensions++
  127. }
  128. if m.ocspStapling {
  129. extensionsLength += 1 + 2 + 2
  130. numExtensions++
  131. }
  132. if len(m.serverName) > 0 {
  133. extensionsLength += 5 + len(m.serverName)
  134. numExtensions++
  135. }
  136. if len(m.supportedCurves) > 0 {
  137. extensionsLength += 2 + 2*len(m.supportedCurves)
  138. numExtensions++
  139. }
  140. if len(m.supportedPoints) > 0 {
  141. extensionsLength += 1 + len(m.supportedPoints)
  142. numExtensions++
  143. }
  144. if m.ticketSupported {
  145. extensionsLength += len(m.sessionTicket)
  146. numExtensions++
  147. }
  148. if len(m.supportedSignatureAlgorithms) > 0 {
  149. extensionsLength += 2 + 2*len(m.supportedSignatureAlgorithms)
  150. numExtensions++
  151. }
  152. if m.getSignatureAlgorithmsCert() != nil {
  153. extensionsLength += 2 + 2*len(m.getSignatureAlgorithmsCert())
  154. numExtensions++
  155. }
  156. if m.secureRenegotiationSupported {
  157. extensionsLength += 1 + len(m.secureRenegotiation)
  158. numExtensions++
  159. }
  160. if len(m.alpnProtocols) > 0 {
  161. extensionsLength += 2
  162. for _, s := range m.alpnProtocols {
  163. if l := len(s); l == 0 || l > 255 {
  164. panic("invalid ALPN protocol")
  165. }
  166. extensionsLength++
  167. extensionsLength += len(s)
  168. }
  169. numExtensions++
  170. }
  171. if m.scts {
  172. numExtensions++
  173. }
  174. if len(m.keyShares) > 0 {
  175. extensionsLength += 2
  176. for _, k := range m.keyShares {
  177. extensionsLength += 4 + len(k.data)
  178. }
  179. numExtensions++
  180. }
  181. if len(m.supportedVersions) > 0 {
  182. extensionsLength += 1 + 2*len(m.supportedVersions)
  183. numExtensions++
  184. }
  185. if m.earlyData {
  186. numExtensions++
  187. }
  188. if numExtensions > 0 {
  189. extensionsLength += 4 * numExtensions
  190. length += 2 + extensionsLength
  191. }
  192. x := make([]byte, 4+length)
  193. x[0] = typeClientHello
  194. x[1] = uint8(length >> 16)
  195. x[2] = uint8(length >> 8)
  196. x[3] = uint8(length)
  197. x[4] = uint8(m.vers >> 8)
  198. x[5] = uint8(m.vers)
  199. copy(x[6:38], m.random)
  200. x[38] = uint8(len(m.sessionId))
  201. copy(x[39:39+len(m.sessionId)], m.sessionId)
  202. y := x[39+len(m.sessionId):]
  203. y[0] = uint8(len(m.cipherSuites) >> 7)
  204. y[1] = uint8(len(m.cipherSuites) << 1)
  205. for i, suite := range m.cipherSuites {
  206. y[2+i*2] = uint8(suite >> 8)
  207. y[3+i*2] = uint8(suite)
  208. }
  209. z := y[2+len(m.cipherSuites)*2:]
  210. z[0] = uint8(len(m.compressionMethods))
  211. copy(z[1:], m.compressionMethods)
  212. z = z[1+len(m.compressionMethods):]
  213. if numExtensions > 0 {
  214. z[0] = byte(extensionsLength >> 8)
  215. z[1] = byte(extensionsLength)
  216. z = z[2:]
  217. }
  218. if m.nextProtoNeg {
  219. z[0] = byte(extensionNextProtoNeg >> 8)
  220. z[1] = byte(extensionNextProtoNeg & 0xff)
  221. // The length is always 0
  222. z = z[4:]
  223. }
  224. if len(m.serverName) > 0 {
  225. z[0] = byte(extensionServerName >> 8)
  226. z[1] = byte(extensionServerName & 0xff)
  227. l := len(m.serverName) + 5
  228. z[2] = byte(l >> 8)
  229. z[3] = byte(l)
  230. z = z[4:]
  231. // RFC 3546, section 3.1
  232. //
  233. // struct {
  234. // NameType name_type;
  235. // select (name_type) {
  236. // case host_name: HostName;
  237. // } name;
  238. // } ServerName;
  239. //
  240. // enum {
  241. // host_name(0), (255)
  242. // } NameType;
  243. //
  244. // opaque HostName<1..2^16-1>;
  245. //
  246. // struct {
  247. // ServerName server_name_list<1..2^16-1>
  248. // } ServerNameList;
  249. z[0] = byte((len(m.serverName) + 3) >> 8)
  250. z[1] = byte(len(m.serverName) + 3)
  251. z[3] = byte(len(m.serverName) >> 8)
  252. z[4] = byte(len(m.serverName))
  253. copy(z[5:], []byte(m.serverName))
  254. z = z[l:]
  255. }
  256. if m.ocspStapling {
  257. // RFC 4366, section 3.6
  258. z[0] = byte(extensionStatusRequest >> 8)
  259. z[1] = byte(extensionStatusRequest)
  260. z[2] = 0
  261. z[3] = 5
  262. z[4] = 1 // OCSP type
  263. // Two zero valued uint16s for the two lengths.
  264. z = z[9:]
  265. }
  266. if len(m.supportedCurves) > 0 {
  267. // http://tools.ietf.org/html/rfc4492#section-5.5.1
  268. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.4
  269. z[0] = byte(extensionSupportedCurves >> 8)
  270. z[1] = byte(extensionSupportedCurves)
  271. l := 2 + 2*len(m.supportedCurves)
  272. z[2] = byte(l >> 8)
  273. z[3] = byte(l)
  274. l -= 2
  275. z[4] = byte(l >> 8)
  276. z[5] = byte(l)
  277. z = z[6:]
  278. for _, curve := range m.supportedCurves {
  279. z[0] = byte(curve >> 8)
  280. z[1] = byte(curve)
  281. z = z[2:]
  282. }
  283. }
  284. if len(m.supportedPoints) > 0 {
  285. // http://tools.ietf.org/html/rfc4492#section-5.5.2
  286. z[0] = byte(extensionSupportedPoints >> 8)
  287. z[1] = byte(extensionSupportedPoints)
  288. l := 1 + len(m.supportedPoints)
  289. z[2] = byte(l >> 8)
  290. z[3] = byte(l)
  291. l--
  292. z[4] = byte(l)
  293. z = z[5:]
  294. for _, pointFormat := range m.supportedPoints {
  295. z[0] = pointFormat
  296. z = z[1:]
  297. }
  298. }
  299. if m.ticketSupported {
  300. // http://tools.ietf.org/html/rfc5077#section-3.2
  301. z[0] = byte(extensionSessionTicket >> 8)
  302. z[1] = byte(extensionSessionTicket)
  303. l := len(m.sessionTicket)
  304. z[2] = byte(l >> 8)
  305. z[3] = byte(l)
  306. z = z[4:]
  307. copy(z, m.sessionTicket)
  308. z = z[len(m.sessionTicket):]
  309. }
  310. if len(m.supportedSignatureAlgorithms) > 0 {
  311. z = marshalExtensionSignatureAlgorithms(extensionSignatureAlgorithms, z, m.supportedSignatureAlgorithms)
  312. }
  313. if m.getSignatureAlgorithmsCert() != nil {
  314. // Ensure only one list of algorithms is sent if supported_algorithms and supported_algorithms_cert are the same
  315. z = marshalExtensionSignatureAlgorithms(extensionSignatureAlgorithmsCert, z, m.getSignatureAlgorithmsCert())
  316. }
  317. if m.secureRenegotiationSupported {
  318. z[0] = byte(extensionRenegotiationInfo >> 8)
  319. z[1] = byte(extensionRenegotiationInfo & 0xff)
  320. z[2] = 0
  321. z[3] = byte(len(m.secureRenegotiation) + 1)
  322. z[4] = byte(len(m.secureRenegotiation))
  323. z = z[5:]
  324. copy(z, m.secureRenegotiation)
  325. z = z[len(m.secureRenegotiation):]
  326. }
  327. if len(m.alpnProtocols) > 0 {
  328. z[0] = byte(extensionALPN >> 8)
  329. z[1] = byte(extensionALPN & 0xff)
  330. lengths := z[2:]
  331. z = z[6:]
  332. stringsLength := 0
  333. for _, s := range m.alpnProtocols {
  334. l := len(s)
  335. z[0] = byte(l)
  336. copy(z[1:], s)
  337. z = z[1+l:]
  338. stringsLength += 1 + l
  339. }
  340. lengths[2] = byte(stringsLength >> 8)
  341. lengths[3] = byte(stringsLength)
  342. stringsLength += 2
  343. lengths[0] = byte(stringsLength >> 8)
  344. lengths[1] = byte(stringsLength)
  345. }
  346. if m.scts {
  347. // https://tools.ietf.org/html/rfc6962#section-3.3.1
  348. z[0] = byte(extensionSCT >> 8)
  349. z[1] = byte(extensionSCT)
  350. // zero uint16 for the zero-length extension_data
  351. z = z[4:]
  352. }
  353. if len(m.keyShares) > 0 {
  354. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.5
  355. z[0] = byte(extensionKeyShare >> 8)
  356. z[1] = byte(extensionKeyShare)
  357. lengths := z[2:]
  358. z = z[6:]
  359. totalLength := 0
  360. for _, ks := range m.keyShares {
  361. z[0] = byte(ks.group >> 8)
  362. z[1] = byte(ks.group)
  363. z[2] = byte(len(ks.data) >> 8)
  364. z[3] = byte(len(ks.data))
  365. copy(z[4:], ks.data)
  366. z = z[4+len(ks.data):]
  367. totalLength += 4 + len(ks.data)
  368. }
  369. lengths[2] = byte(totalLength >> 8)
  370. lengths[3] = byte(totalLength)
  371. totalLength += 2
  372. lengths[0] = byte(totalLength >> 8)
  373. lengths[1] = byte(totalLength)
  374. }
  375. if len(m.supportedVersions) > 0 {
  376. z[0] = byte(extensionSupportedVersions >> 8)
  377. z[1] = byte(extensionSupportedVersions)
  378. l := 1 + 2*len(m.supportedVersions)
  379. z[2] = byte(l >> 8)
  380. z[3] = byte(l)
  381. l -= 1
  382. z[4] = byte(l)
  383. z = z[5:]
  384. for _, v := range m.supportedVersions {
  385. z[0] = byte(v >> 8)
  386. z[1] = byte(v)
  387. z = z[2:]
  388. }
  389. }
  390. if m.earlyData {
  391. z[0] = byte(extensionEarlyData >> 8)
  392. z[1] = byte(extensionEarlyData)
  393. z = z[4:]
  394. }
  395. m.raw = x
  396. return x
  397. }
  398. func (m *clientHelloMsg) unmarshal(data []byte) alert {
  399. if len(data) < 42 {
  400. return alertDecodeError
  401. }
  402. m.raw = data
  403. m.vers = uint16(data[4])<<8 | uint16(data[5])
  404. m.random = data[6:38]
  405. sessionIdLen := int(data[38])
  406. if sessionIdLen > 32 || len(data) < 39+sessionIdLen {
  407. return alertDecodeError
  408. }
  409. m.sessionId = data[39 : 39+sessionIdLen]
  410. data = data[39+sessionIdLen:]
  411. bindersOffset := 39 + sessionIdLen
  412. if len(data) < 2 {
  413. return alertDecodeError
  414. }
  415. // cipherSuiteLen is the number of bytes of cipher suite numbers. Since
  416. // they are uint16s, the number must be even.
  417. cipherSuiteLen := int(data[0])<<8 | int(data[1])
  418. if cipherSuiteLen%2 == 1 || len(data) < 2+cipherSuiteLen {
  419. return alertDecodeError
  420. }
  421. numCipherSuites := cipherSuiteLen / 2
  422. m.cipherSuites = make([]uint16, numCipherSuites)
  423. for i := 0; i < numCipherSuites; i++ {
  424. m.cipherSuites[i] = uint16(data[2+2*i])<<8 | uint16(data[3+2*i])
  425. if m.cipherSuites[i] == scsvRenegotiation {
  426. m.secureRenegotiationSupported = true
  427. }
  428. }
  429. data = data[2+cipherSuiteLen:]
  430. bindersOffset += 2 + cipherSuiteLen
  431. if len(data) < 1 {
  432. return alertDecodeError
  433. }
  434. compressionMethodsLen := int(data[0])
  435. if len(data) < 1+compressionMethodsLen {
  436. return alertDecodeError
  437. }
  438. m.compressionMethods = data[1 : 1+compressionMethodsLen]
  439. data = data[1+compressionMethodsLen:]
  440. bindersOffset += 1 + compressionMethodsLen
  441. m.nextProtoNeg = false
  442. m.serverName = ""
  443. m.ocspStapling = false
  444. m.ticketSupported = false
  445. m.sessionTicket = nil
  446. m.supportedSignatureAlgorithms = nil
  447. m.alpnProtocols = nil
  448. m.scts = false
  449. m.keyShares = nil
  450. m.supportedVersions = nil
  451. m.psks = nil
  452. m.pskKeyExchangeModes = nil
  453. m.earlyData = false
  454. if len(data) == 0 {
  455. // ClientHello is optionally followed by extension data
  456. return alertSuccess
  457. }
  458. if len(data) < 2 {
  459. return alertDecodeError
  460. }
  461. extensionsLength := int(data[0])<<8 | int(data[1])
  462. data = data[2:]
  463. bindersOffset += 2
  464. if extensionsLength != len(data) {
  465. return alertDecodeError
  466. }
  467. for len(data) != 0 {
  468. if len(data) < 4 {
  469. return alertDecodeError
  470. }
  471. extension := uint16(data[0])<<8 | uint16(data[1])
  472. length := int(data[2])<<8 | int(data[3])
  473. data = data[4:]
  474. bindersOffset += 4
  475. if len(data) < length {
  476. return alertDecodeError
  477. }
  478. switch extension {
  479. case extensionServerName:
  480. d := data[:length]
  481. if len(d) < 2 {
  482. return alertDecodeError
  483. }
  484. namesLen := int(d[0])<<8 | int(d[1])
  485. d = d[2:]
  486. if len(d) != namesLen {
  487. return alertDecodeError
  488. }
  489. for len(d) > 0 {
  490. if len(d) < 3 {
  491. return alertDecodeError
  492. }
  493. nameType := d[0]
  494. nameLen := int(d[1])<<8 | int(d[2])
  495. d = d[3:]
  496. if len(d) < nameLen {
  497. return alertDecodeError
  498. }
  499. if nameType == 0 {
  500. m.serverName = string(d[:nameLen])
  501. // An SNI value may not include a
  502. // trailing dot. See
  503. // https://tools.ietf.org/html/rfc6066#section-3.
  504. if strings.HasSuffix(m.serverName, ".") {
  505. // TODO use alertDecodeError?
  506. return alertUnexpectedMessage
  507. }
  508. break
  509. }
  510. d = d[nameLen:]
  511. }
  512. case extensionNextProtoNeg:
  513. if length > 0 {
  514. return alertDecodeError
  515. }
  516. m.nextProtoNeg = true
  517. case extensionStatusRequest:
  518. m.ocspStapling = length > 0 && data[0] == statusTypeOCSP
  519. case extensionSupportedCurves:
  520. // http://tools.ietf.org/html/rfc4492#section-5.5.1
  521. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.4
  522. if length < 2 {
  523. return alertDecodeError
  524. }
  525. l := int(data[0])<<8 | int(data[1])
  526. if l%2 == 1 || length != l+2 {
  527. return alertDecodeError
  528. }
  529. numCurves := l / 2
  530. m.supportedCurves = make([]CurveID, numCurves)
  531. d := data[2:]
  532. for i := 0; i < numCurves; i++ {
  533. m.supportedCurves[i] = CurveID(d[0])<<8 | CurveID(d[1])
  534. d = d[2:]
  535. }
  536. case extensionSupportedPoints:
  537. // http://tools.ietf.org/html/rfc4492#section-5.5.2
  538. if length < 1 {
  539. return alertDecodeError
  540. }
  541. l := int(data[0])
  542. if length != l+1 {
  543. return alertDecodeError
  544. }
  545. m.supportedPoints = make([]uint8, l)
  546. copy(m.supportedPoints, data[1:])
  547. case extensionSessionTicket:
  548. // http://tools.ietf.org/html/rfc5077#section-3.2
  549. m.ticketSupported = true
  550. m.sessionTicket = data[:length]
  551. case extensionSignatureAlgorithms:
  552. // https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
  553. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.3
  554. if length < 2 || length&1 != 0 {
  555. return alertDecodeError
  556. }
  557. l := int(data[0])<<8 | int(data[1])
  558. if l != length-2 {
  559. return alertDecodeError
  560. }
  561. n := l / 2
  562. d := data[2:]
  563. m.supportedSignatureAlgorithms = make([]SignatureScheme, n)
  564. for i := range m.supportedSignatureAlgorithms {
  565. m.supportedSignatureAlgorithms[i] = SignatureScheme(d[0])<<8 | SignatureScheme(d[1])
  566. d = d[2:]
  567. }
  568. case extensionRenegotiationInfo:
  569. if length == 0 {
  570. return alertDecodeError
  571. }
  572. d := data[:length]
  573. l := int(d[0])
  574. d = d[1:]
  575. if l != len(d) {
  576. return alertDecodeError
  577. }
  578. m.secureRenegotiation = d
  579. m.secureRenegotiationSupported = true
  580. case extensionALPN:
  581. if length < 2 {
  582. return alertDecodeError
  583. }
  584. l := int(data[0])<<8 | int(data[1])
  585. if l != length-2 {
  586. return alertDecodeError
  587. }
  588. d := data[2:length]
  589. for len(d) != 0 {
  590. stringLen := int(d[0])
  591. d = d[1:]
  592. if stringLen == 0 || stringLen > len(d) {
  593. return alertDecodeError
  594. }
  595. m.alpnProtocols = append(m.alpnProtocols, string(d[:stringLen]))
  596. d = d[stringLen:]
  597. }
  598. case extensionSCT:
  599. m.scts = true
  600. if length != 0 {
  601. return alertDecodeError
  602. }
  603. case extensionKeyShare:
  604. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.5
  605. if length < 2 {
  606. return alertDecodeError
  607. }
  608. l := int(data[0])<<8 | int(data[1])
  609. if l != length-2 {
  610. return alertDecodeError
  611. }
  612. d := data[2:length]
  613. for len(d) != 0 {
  614. if len(d) < 4 {
  615. return alertDecodeError
  616. }
  617. dataLen := int(d[2])<<8 | int(d[3])
  618. if dataLen == 0 || 4+dataLen > len(d) {
  619. return alertDecodeError
  620. }
  621. m.keyShares = append(m.keyShares, keyShare{
  622. group: CurveID(d[0])<<8 | CurveID(d[1]),
  623. data: d[4 : 4+dataLen],
  624. })
  625. d = d[4+dataLen:]
  626. }
  627. case extensionSupportedVersions:
  628. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.1
  629. if length < 1 {
  630. return alertDecodeError
  631. }
  632. l := int(data[0])
  633. if l%2 == 1 || length != l+1 {
  634. return alertDecodeError
  635. }
  636. n := l / 2
  637. d := data[1:]
  638. for i := 0; i < n; i++ {
  639. v := uint16(d[0])<<8 + uint16(d[1])
  640. m.supportedVersions = append(m.supportedVersions, v)
  641. d = d[2:]
  642. }
  643. case extensionPreSharedKey:
  644. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6
  645. if length < 2 {
  646. return alertDecodeError
  647. }
  648. // Ensure this extension is the last one in the Client Hello
  649. if len(data) != length {
  650. return alertIllegalParameter
  651. }
  652. li := int(data[0])<<8 | int(data[1])
  653. if 2+li+2 > length {
  654. return alertDecodeError
  655. }
  656. d := data[2 : 2+li]
  657. bindersOffset += 2 + li
  658. for len(d) > 0 {
  659. if len(d) < 6 {
  660. return alertDecodeError
  661. }
  662. l := int(d[0])<<8 | int(d[1])
  663. if len(d) < 2+l+4 {
  664. return alertDecodeError
  665. }
  666. m.psks = append(m.psks, psk{
  667. identity: d[2 : 2+l],
  668. obfTicketAge: uint32(d[l+2])<<24 | uint32(d[l+3])<<16 |
  669. uint32(d[l+4])<<8 | uint32(d[l+5]),
  670. })
  671. d = d[2+l+4:]
  672. }
  673. lb := int(data[li+2])<<8 | int(data[li+3])
  674. d = data[2+li+2:]
  675. if lb != len(d) || lb == 0 {
  676. return alertDecodeError
  677. }
  678. i := 0
  679. for len(d) > 0 {
  680. if i >= len(m.psks) {
  681. return alertIllegalParameter
  682. }
  683. if len(d) < 1 {
  684. return alertDecodeError
  685. }
  686. l := int(d[0])
  687. if l > len(d)-1 {
  688. return alertDecodeError
  689. }
  690. if i >= len(m.psks) {
  691. return alertIllegalParameter
  692. }
  693. m.psks[i].binder = d[1 : 1+l]
  694. d = d[1+l:]
  695. i++
  696. }
  697. if i != len(m.psks) {
  698. return alertIllegalParameter
  699. }
  700. m.rawTruncated = m.raw[:bindersOffset]
  701. case extensionPSKKeyExchangeModes:
  702. if length < 2 {
  703. return alertDecodeError
  704. }
  705. l := int(data[0])
  706. if length != l+1 {
  707. return alertDecodeError
  708. }
  709. m.pskKeyExchangeModes = data[1:length]
  710. case extensionEarlyData:
  711. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8
  712. m.earlyData = true
  713. }
  714. data = data[length:]
  715. bindersOffset += length
  716. }
  717. return alertSuccess
  718. }
  719. func (m *clientHelloMsg) getSignatureAlgorithmsCert() []SignatureScheme {
  720. return signAlgosCertList(m.supportedSignatureAlgorithms, m.supportedSignatureAlgorithmsCert)
  721. }
  722. type serverHelloMsg struct {
  723. raw []byte
  724. vers uint16
  725. random []byte
  726. sessionId []byte
  727. cipherSuite uint16
  728. compressionMethod uint8
  729. nextProtoNeg bool
  730. nextProtos []string
  731. ocspStapling bool
  732. scts [][]byte
  733. ticketSupported bool
  734. secureRenegotiation []byte
  735. secureRenegotiationSupported bool
  736. alpnProtocol string
  737. // TLS 1.3
  738. keyShare keyShare
  739. psk bool
  740. pskIdentity uint16
  741. }
  742. func (m *serverHelloMsg) equal(i interface{}) bool {
  743. m1, ok := i.(*serverHelloMsg)
  744. if !ok {
  745. return false
  746. }
  747. if len(m.scts) != len(m1.scts) {
  748. return false
  749. }
  750. for i, sct := range m.scts {
  751. if !bytes.Equal(sct, m1.scts[i]) {
  752. return false
  753. }
  754. }
  755. return bytes.Equal(m.raw, m1.raw) &&
  756. m.vers == m1.vers &&
  757. bytes.Equal(m.random, m1.random) &&
  758. bytes.Equal(m.sessionId, m1.sessionId) &&
  759. m.cipherSuite == m1.cipherSuite &&
  760. m.compressionMethod == m1.compressionMethod &&
  761. m.nextProtoNeg == m1.nextProtoNeg &&
  762. eqStrings(m.nextProtos, m1.nextProtos) &&
  763. m.ocspStapling == m1.ocspStapling &&
  764. m.ticketSupported == m1.ticketSupported &&
  765. m.secureRenegotiationSupported == m1.secureRenegotiationSupported &&
  766. bytes.Equal(m.secureRenegotiation, m1.secureRenegotiation) &&
  767. m.alpnProtocol == m1.alpnProtocol &&
  768. m.keyShare.group == m1.keyShare.group &&
  769. bytes.Equal(m.keyShare.data, m1.keyShare.data) &&
  770. m.psk == m1.psk &&
  771. m.pskIdentity == m1.pskIdentity
  772. }
  773. func (m *serverHelloMsg) marshal() []byte {
  774. if m.raw != nil {
  775. return m.raw
  776. }
  777. oldTLS13Draft := m.vers >= VersionTLS13Draft18 && m.vers <= VersionTLS13Draft21
  778. length := 38 + len(m.sessionId)
  779. if oldTLS13Draft {
  780. // no compression method, no session ID.
  781. length = 36
  782. }
  783. numExtensions := 0
  784. extensionsLength := 0
  785. nextProtoLen := 0
  786. if m.nextProtoNeg {
  787. numExtensions++
  788. for _, v := range m.nextProtos {
  789. nextProtoLen += len(v)
  790. }
  791. nextProtoLen += len(m.nextProtos)
  792. extensionsLength += nextProtoLen
  793. }
  794. if m.ocspStapling {
  795. numExtensions++
  796. }
  797. if m.ticketSupported {
  798. numExtensions++
  799. }
  800. if m.secureRenegotiationSupported {
  801. extensionsLength += 1 + len(m.secureRenegotiation)
  802. numExtensions++
  803. }
  804. if alpnLen := len(m.alpnProtocol); alpnLen > 0 {
  805. if alpnLen >= 256 {
  806. panic("invalid ALPN protocol")
  807. }
  808. extensionsLength += 2 + 1 + alpnLen
  809. numExtensions++
  810. }
  811. sctLen := 0
  812. if len(m.scts) > 0 {
  813. for _, sct := range m.scts {
  814. sctLen += len(sct) + 2
  815. }
  816. extensionsLength += 2 + sctLen
  817. numExtensions++
  818. }
  819. if m.keyShare.group != 0 {
  820. extensionsLength += 4 + len(m.keyShare.data)
  821. numExtensions++
  822. }
  823. if m.psk {
  824. extensionsLength += 2
  825. numExtensions++
  826. }
  827. // supported_versions extension
  828. if m.vers >= VersionTLS13 {
  829. extensionsLength += 2
  830. numExtensions++
  831. }
  832. if numExtensions > 0 {
  833. extensionsLength += 4 * numExtensions
  834. length += 2 + extensionsLength
  835. }
  836. x := make([]byte, 4+length)
  837. x[0] = typeServerHello
  838. x[1] = uint8(length >> 16)
  839. x[2] = uint8(length >> 8)
  840. x[3] = uint8(length)
  841. if m.vers >= VersionTLS13 {
  842. x[4] = 3
  843. x[5] = 3
  844. } else {
  845. x[4] = uint8(m.vers >> 8)
  846. x[5] = uint8(m.vers)
  847. }
  848. copy(x[6:38], m.random)
  849. z := x[38:]
  850. if !oldTLS13Draft {
  851. x[38] = uint8(len(m.sessionId))
  852. copy(x[39:39+len(m.sessionId)], m.sessionId)
  853. z = x[39+len(m.sessionId):]
  854. }
  855. z[0] = uint8(m.cipherSuite >> 8)
  856. z[1] = uint8(m.cipherSuite)
  857. if oldTLS13Draft {
  858. // no compression method in older TLS 1.3 drafts.
  859. z = z[2:]
  860. } else {
  861. z[2] = m.compressionMethod
  862. z = z[3:]
  863. }
  864. if numExtensions > 0 {
  865. z[0] = byte(extensionsLength >> 8)
  866. z[1] = byte(extensionsLength)
  867. z = z[2:]
  868. }
  869. if m.vers >= VersionTLS13 {
  870. z[0] = byte(extensionSupportedVersions >> 8)
  871. z[1] = byte(extensionSupportedVersions)
  872. z[3] = 2
  873. z[4] = uint8(m.vers >> 8)
  874. z[5] = uint8(m.vers)
  875. z = z[6:]
  876. }
  877. if m.nextProtoNeg {
  878. z[0] = byte(extensionNextProtoNeg >> 8)
  879. z[1] = byte(extensionNextProtoNeg & 0xff)
  880. z[2] = byte(nextProtoLen >> 8)
  881. z[3] = byte(nextProtoLen)
  882. z = z[4:]
  883. for _, v := range m.nextProtos {
  884. l := len(v)
  885. if l > 255 {
  886. l = 255
  887. }
  888. z[0] = byte(l)
  889. copy(z[1:], []byte(v[0:l]))
  890. z = z[1+l:]
  891. }
  892. }
  893. if m.ocspStapling {
  894. z[0] = byte(extensionStatusRequest >> 8)
  895. z[1] = byte(extensionStatusRequest)
  896. z = z[4:]
  897. }
  898. if m.ticketSupported {
  899. z[0] = byte(extensionSessionTicket >> 8)
  900. z[1] = byte(extensionSessionTicket)
  901. z = z[4:]
  902. }
  903. if m.secureRenegotiationSupported {
  904. z[0] = byte(extensionRenegotiationInfo >> 8)
  905. z[1] = byte(extensionRenegotiationInfo & 0xff)
  906. z[2] = 0
  907. z[3] = byte(len(m.secureRenegotiation) + 1)
  908. z[4] = byte(len(m.secureRenegotiation))
  909. z = z[5:]
  910. copy(z, m.secureRenegotiation)
  911. z = z[len(m.secureRenegotiation):]
  912. }
  913. if alpnLen := len(m.alpnProtocol); alpnLen > 0 {
  914. z[0] = byte(extensionALPN >> 8)
  915. z[1] = byte(extensionALPN & 0xff)
  916. l := 2 + 1 + alpnLen
  917. z[2] = byte(l >> 8)
  918. z[3] = byte(l)
  919. l -= 2
  920. z[4] = byte(l >> 8)
  921. z[5] = byte(l)
  922. l -= 1
  923. z[6] = byte(l)
  924. copy(z[7:], []byte(m.alpnProtocol))
  925. z = z[7+alpnLen:]
  926. }
  927. if sctLen > 0 {
  928. z[0] = byte(extensionSCT >> 8)
  929. z[1] = byte(extensionSCT)
  930. l := sctLen + 2
  931. z[2] = byte(l >> 8)
  932. z[3] = byte(l)
  933. z[4] = byte(sctLen >> 8)
  934. z[5] = byte(sctLen)
  935. z = z[6:]
  936. for _, sct := range m.scts {
  937. z[0] = byte(len(sct) >> 8)
  938. z[1] = byte(len(sct))
  939. copy(z[2:], sct)
  940. z = z[len(sct)+2:]
  941. }
  942. }
  943. if m.keyShare.group != 0 {
  944. z[0] = uint8(extensionKeyShare >> 8)
  945. z[1] = uint8(extensionKeyShare)
  946. l := 4 + len(m.keyShare.data)
  947. z[2] = uint8(l >> 8)
  948. z[3] = uint8(l)
  949. z[4] = uint8(m.keyShare.group >> 8)
  950. z[5] = uint8(m.keyShare.group)
  951. l -= 4
  952. z[6] = uint8(l >> 8)
  953. z[7] = uint8(l)
  954. copy(z[8:], m.keyShare.data)
  955. z = z[8+l:]
  956. }
  957. if m.psk {
  958. z[0] = byte(extensionPreSharedKey >> 8)
  959. z[1] = byte(extensionPreSharedKey)
  960. z[3] = 2
  961. z[4] = byte(m.pskIdentity >> 8)
  962. z[5] = byte(m.pskIdentity)
  963. z = z[6:]
  964. }
  965. m.raw = x
  966. return x
  967. }
  968. func (m *serverHelloMsg) unmarshal(data []byte) alert {
  969. if len(data) < 42 {
  970. return alertDecodeError
  971. }
  972. m.raw = data
  973. m.vers = uint16(data[4])<<8 | uint16(data[5])
  974. oldTLS13Draft := m.vers >= VersionTLS13Draft18 && m.vers <= VersionTLS13Draft21
  975. m.random = data[6:38]
  976. if !oldTLS13Draft {
  977. sessionIdLen := int(data[38])
  978. if sessionIdLen > 32 || len(data) < 39+sessionIdLen {
  979. return alertDecodeError
  980. }
  981. m.sessionId = data[39 : 39+sessionIdLen]
  982. data = data[39+sessionIdLen:]
  983. } else {
  984. data = data[38:]
  985. }
  986. if len(data) < 3 {
  987. return alertDecodeError
  988. }
  989. m.cipherSuite = uint16(data[0])<<8 | uint16(data[1])
  990. if oldTLS13Draft {
  991. // no compression method in older TLS 1.3 drafts.
  992. data = data[2:]
  993. } else {
  994. m.compressionMethod = data[2]
  995. data = data[3:]
  996. }
  997. m.nextProtoNeg = false
  998. m.nextProtos = nil
  999. m.ocspStapling = false
  1000. m.scts = nil
  1001. m.ticketSupported = false
  1002. m.alpnProtocol = ""
  1003. m.keyShare.group = 0
  1004. m.keyShare.data = nil
  1005. m.psk = false
  1006. m.pskIdentity = 0
  1007. if len(data) == 0 {
  1008. // ServerHello is optionally followed by extension data
  1009. return alertSuccess
  1010. }
  1011. if len(data) < 2 {
  1012. return alertDecodeError
  1013. }
  1014. extensionsLength := int(data[0])<<8 | int(data[1])
  1015. data = data[2:]
  1016. if len(data) != extensionsLength {
  1017. return alertDecodeError
  1018. }
  1019. svData := findExtension(data, extensionSupportedVersions)
  1020. if svData != nil {
  1021. if len(svData) != 2 {
  1022. return alertDecodeError
  1023. }
  1024. if m.vers != VersionTLS12 {
  1025. return alertDecodeError
  1026. }
  1027. m.vers = uint16(svData[0])<<8 | uint16(svData[1])
  1028. }
  1029. for len(data) != 0 {
  1030. if len(data) < 4 {
  1031. return alertDecodeError
  1032. }
  1033. extension := uint16(data[0])<<8 | uint16(data[1])
  1034. length := int(data[2])<<8 | int(data[3])
  1035. data = data[4:]
  1036. if len(data) < length {
  1037. return alertDecodeError
  1038. }
  1039. switch extension {
  1040. case extensionNextProtoNeg:
  1041. m.nextProtoNeg = true
  1042. d := data[:length]
  1043. for len(d) > 0 {
  1044. l := int(d[0])
  1045. d = d[1:]
  1046. if l == 0 || l > len(d) {
  1047. return alertDecodeError
  1048. }
  1049. m.nextProtos = append(m.nextProtos, string(d[:l]))
  1050. d = d[l:]
  1051. }
  1052. case extensionStatusRequest:
  1053. if length > 0 {
  1054. return alertDecodeError
  1055. }
  1056. m.ocspStapling = true
  1057. case extensionSessionTicket:
  1058. if length > 0 {
  1059. return alertDecodeError
  1060. }
  1061. m.ticketSupported = true
  1062. case extensionRenegotiationInfo:
  1063. if length == 0 {
  1064. return alertDecodeError
  1065. }
  1066. d := data[:length]
  1067. l := int(d[0])
  1068. d = d[1:]
  1069. if l != len(d) {
  1070. return alertDecodeError
  1071. }
  1072. m.secureRenegotiation = d
  1073. m.secureRenegotiationSupported = true
  1074. case extensionALPN:
  1075. d := data[:length]
  1076. if len(d) < 3 {
  1077. return alertDecodeError
  1078. }
  1079. l := int(d[0])<<8 | int(d[1])
  1080. if l != len(d)-2 {
  1081. return alertDecodeError
  1082. }
  1083. d = d[2:]
  1084. l = int(d[0])
  1085. if l != len(d)-1 {
  1086. return alertDecodeError
  1087. }
  1088. d = d[1:]
  1089. if len(d) == 0 {
  1090. // ALPN protocols must not be empty.
  1091. return alertDecodeError
  1092. }
  1093. m.alpnProtocol = string(d)
  1094. case extensionSCT:
  1095. d := data[:length]
  1096. if len(d) < 2 {
  1097. return alertDecodeError
  1098. }
  1099. l := int(d[0])<<8 | int(d[1])
  1100. d = d[2:]
  1101. if len(d) != l || l == 0 {
  1102. return alertDecodeError
  1103. }
  1104. m.scts = make([][]byte, 0, 3)
  1105. for len(d) != 0 {
  1106. if len(d) < 2 {
  1107. return alertDecodeError
  1108. }
  1109. sctLen := int(d[0])<<8 | int(d[1])
  1110. d = d[2:]
  1111. if sctLen == 0 || len(d) < sctLen {
  1112. return alertDecodeError
  1113. }
  1114. m.scts = append(m.scts, d[:sctLen])
  1115. d = d[sctLen:]
  1116. }
  1117. case extensionKeyShare:
  1118. d := data[:length]
  1119. if len(d) < 4 {
  1120. return alertDecodeError
  1121. }
  1122. m.keyShare.group = CurveID(d[0])<<8 | CurveID(d[1])
  1123. l := int(d[2])<<8 | int(d[3])
  1124. d = d[4:]
  1125. if len(d) != l {
  1126. return alertDecodeError
  1127. }
  1128. m.keyShare.data = d[:l]
  1129. case extensionPreSharedKey:
  1130. if length != 2 {
  1131. return alertDecodeError
  1132. }
  1133. m.psk = true
  1134. m.pskIdentity = uint16(data[0])<<8 | uint16(data[1])
  1135. }
  1136. data = data[length:]
  1137. }
  1138. return alertSuccess
  1139. }
  1140. type encryptedExtensionsMsg struct {
  1141. raw []byte
  1142. alpnProtocol string
  1143. earlyData bool
  1144. }
  1145. func (m *encryptedExtensionsMsg) equal(i interface{}) bool {
  1146. m1, ok := i.(*encryptedExtensionsMsg)
  1147. if !ok {
  1148. return false
  1149. }
  1150. return bytes.Equal(m.raw, m1.raw) &&
  1151. m.alpnProtocol == m1.alpnProtocol &&
  1152. m.earlyData == m1.earlyData
  1153. }
  1154. func (m *encryptedExtensionsMsg) marshal() []byte {
  1155. if m.raw != nil {
  1156. return m.raw
  1157. }
  1158. length := 2
  1159. if m.earlyData {
  1160. length += 4
  1161. }
  1162. alpnLen := len(m.alpnProtocol)
  1163. if alpnLen > 0 {
  1164. if alpnLen >= 256 {
  1165. panic("invalid ALPN protocol")
  1166. }
  1167. length += 2 + 2 + 2 + 1 + alpnLen
  1168. }
  1169. x := make([]byte, 4+length)
  1170. x[0] = typeEncryptedExtensions
  1171. x[1] = uint8(length >> 16)
  1172. x[2] = uint8(length >> 8)
  1173. x[3] = uint8(length)
  1174. length -= 2
  1175. x[4] = uint8(length >> 8)
  1176. x[5] = uint8(length)
  1177. z := x[6:]
  1178. if alpnLen > 0 {
  1179. z[0] = byte(extensionALPN >> 8)
  1180. z[1] = byte(extensionALPN)
  1181. l := 2 + 1 + alpnLen
  1182. z[2] = byte(l >> 8)
  1183. z[3] = byte(l)
  1184. l -= 2
  1185. z[4] = byte(l >> 8)
  1186. z[5] = byte(l)
  1187. l -= 1
  1188. z[6] = byte(l)
  1189. copy(z[7:], []byte(m.alpnProtocol))
  1190. z = z[7+alpnLen:]
  1191. }
  1192. if m.earlyData {
  1193. z[0] = byte(extensionEarlyData >> 8)
  1194. z[1] = byte(extensionEarlyData)
  1195. z = z[4:]
  1196. }
  1197. m.raw = x
  1198. return x
  1199. }
  1200. func (m *encryptedExtensionsMsg) unmarshal(data []byte) alert {
  1201. if len(data) < 6 {
  1202. return alertDecodeError
  1203. }
  1204. m.raw = data
  1205. m.alpnProtocol = ""
  1206. m.earlyData = false
  1207. extensionsLength := int(data[4])<<8 | int(data[5])
  1208. data = data[6:]
  1209. if len(data) != extensionsLength {
  1210. return alertDecodeError
  1211. }
  1212. for len(data) != 0 {
  1213. if len(data) < 4 {
  1214. return alertDecodeError
  1215. }
  1216. extension := uint16(data[0])<<8 | uint16(data[1])
  1217. length := int(data[2])<<8 | int(data[3])
  1218. data = data[4:]
  1219. if len(data) < length {
  1220. return alertDecodeError
  1221. }
  1222. switch extension {
  1223. case extensionALPN:
  1224. d := data[:length]
  1225. if len(d) < 3 {
  1226. return alertDecodeError
  1227. }
  1228. l := int(d[0])<<8 | int(d[1])
  1229. if l != len(d)-2 {
  1230. return alertDecodeError
  1231. }
  1232. d = d[2:]
  1233. l = int(d[0])
  1234. if l != len(d)-1 {
  1235. return alertDecodeError
  1236. }
  1237. d = d[1:]
  1238. if len(d) == 0 {
  1239. // ALPN protocols must not be empty.
  1240. return alertDecodeError
  1241. }
  1242. m.alpnProtocol = string(d)
  1243. case extensionEarlyData:
  1244. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8
  1245. m.earlyData = true
  1246. }
  1247. data = data[length:]
  1248. }
  1249. return alertSuccess
  1250. }
  1251. type certificateMsg struct {
  1252. raw []byte
  1253. certificates [][]byte
  1254. }
  1255. func (m *certificateMsg) equal(i interface{}) bool {
  1256. m1, ok := i.(*certificateMsg)
  1257. if !ok {
  1258. return false
  1259. }
  1260. return bytes.Equal(m.raw, m1.raw) &&
  1261. eqByteSlices(m.certificates, m1.certificates)
  1262. }
  1263. func (m *certificateMsg) marshal() (x []byte) {
  1264. if m.raw != nil {
  1265. return m.raw
  1266. }
  1267. var i int
  1268. for _, slice := range m.certificates {
  1269. i += len(slice)
  1270. }
  1271. length := 3 + 3*len(m.certificates) + i
  1272. x = make([]byte, 4+length)
  1273. x[0] = typeCertificate
  1274. x[1] = uint8(length >> 16)
  1275. x[2] = uint8(length >> 8)
  1276. x[3] = uint8(length)
  1277. certificateOctets := length - 3
  1278. x[4] = uint8(certificateOctets >> 16)
  1279. x[5] = uint8(certificateOctets >> 8)
  1280. x[6] = uint8(certificateOctets)
  1281. y := x[7:]
  1282. for _, slice := range m.certificates {
  1283. y[0] = uint8(len(slice) >> 16)
  1284. y[1] = uint8(len(slice) >> 8)
  1285. y[2] = uint8(len(slice))
  1286. copy(y[3:], slice)
  1287. y = y[3+len(slice):]
  1288. }
  1289. m.raw = x
  1290. return
  1291. }
  1292. func (m *certificateMsg) unmarshal(data []byte) alert {
  1293. if len(data) < 7 {
  1294. return alertDecodeError
  1295. }
  1296. m.raw = data
  1297. certsLen := uint32(data[4])<<16 | uint32(data[5])<<8 | uint32(data[6])
  1298. if uint32(len(data)) != certsLen+7 {
  1299. return alertDecodeError
  1300. }
  1301. numCerts := 0
  1302. d := data[7:]
  1303. for certsLen > 0 {
  1304. if len(d) < 4 {
  1305. return alertDecodeError
  1306. }
  1307. certLen := uint32(d[0])<<16 | uint32(d[1])<<8 | uint32(d[2])
  1308. if uint32(len(d)) < 3+certLen {
  1309. return alertDecodeError
  1310. }
  1311. d = d[3+certLen:]
  1312. certsLen -= 3 + certLen
  1313. numCerts++
  1314. }
  1315. m.certificates = make([][]byte, numCerts)
  1316. d = data[7:]
  1317. for i := 0; i < numCerts; i++ {
  1318. certLen := uint32(d[0])<<16 | uint32(d[1])<<8 | uint32(d[2])
  1319. m.certificates[i] = d[3 : 3+certLen]
  1320. d = d[3+certLen:]
  1321. }
  1322. return alertSuccess
  1323. }
  1324. type certificateEntry struct {
  1325. data []byte
  1326. ocspStaple []byte
  1327. sctList [][]byte
  1328. }
  1329. type certificateMsg13 struct {
  1330. raw []byte
  1331. requestContext []byte
  1332. certificates []certificateEntry
  1333. }
  1334. func (m *certificateMsg13) equal(i interface{}) bool {
  1335. m1, ok := i.(*certificateMsg13)
  1336. if !ok {
  1337. return false
  1338. }
  1339. if len(m.certificates) != len(m1.certificates) {
  1340. return false
  1341. }
  1342. for i, _ := range m.certificates {
  1343. ok := bytes.Equal(m.certificates[i].data, m1.certificates[i].data)
  1344. ok = ok && bytes.Equal(m.certificates[i].ocspStaple, m1.certificates[i].ocspStaple)
  1345. ok = ok && eqByteSlices(m.certificates[i].sctList, m1.certificates[i].sctList)
  1346. if !ok {
  1347. return false
  1348. }
  1349. }
  1350. return bytes.Equal(m.raw, m1.raw) &&
  1351. bytes.Equal(m.requestContext, m1.requestContext)
  1352. }
  1353. func (m *certificateMsg13) marshal() (x []byte) {
  1354. if m.raw != nil {
  1355. return m.raw
  1356. }
  1357. var i int
  1358. for _, cert := range m.certificates {
  1359. i += len(cert.data)
  1360. if len(cert.ocspStaple) != 0 {
  1361. i += 8 + len(cert.ocspStaple)
  1362. }
  1363. if len(cert.sctList) != 0 {
  1364. i += 6
  1365. for _, sct := range cert.sctList {
  1366. i += 2 + len(sct)
  1367. }
  1368. }
  1369. }
  1370. length := 3 + 3*len(m.certificates) + i
  1371. length += 2 * len(m.certificates) // extensions
  1372. length += 1 + len(m.requestContext)
  1373. x = make([]byte, 4+length)
  1374. x[0] = typeCertificate
  1375. x[1] = uint8(length >> 16)
  1376. x[2] = uint8(length >> 8)
  1377. x[3] = uint8(length)
  1378. z := x[4:]
  1379. z[0] = byte(len(m.requestContext))
  1380. copy(z[1:], m.requestContext)
  1381. z = z[1+len(m.requestContext):]
  1382. certificateOctets := len(z) - 3
  1383. z[0] = uint8(certificateOctets >> 16)
  1384. z[1] = uint8(certificateOctets >> 8)
  1385. z[2] = uint8(certificateOctets)
  1386. z = z[3:]
  1387. for _, cert := range m.certificates {
  1388. z[0] = uint8(len(cert.data) >> 16)
  1389. z[1] = uint8(len(cert.data) >> 8)
  1390. z[2] = uint8(len(cert.data))
  1391. copy(z[3:], cert.data)
  1392. z = z[3+len(cert.data):]
  1393. extLenPos := z[:2]
  1394. z = z[2:]
  1395. extensionLen := 0
  1396. if len(cert.ocspStaple) != 0 {
  1397. stapleLen := 4 + len(cert.ocspStaple)
  1398. z[0] = uint8(extensionStatusRequest >> 8)
  1399. z[1] = uint8(extensionStatusRequest)
  1400. z[2] = uint8(stapleLen >> 8)
  1401. z[3] = uint8(stapleLen)
  1402. stapleLen -= 4
  1403. z[4] = statusTypeOCSP
  1404. z[5] = uint8(stapleLen >> 16)
  1405. z[6] = uint8(stapleLen >> 8)
  1406. z[7] = uint8(stapleLen)
  1407. copy(z[8:], cert.ocspStaple)
  1408. z = z[8+stapleLen:]
  1409. extensionLen += 8 + stapleLen
  1410. }
  1411. if len(cert.sctList) != 0 {
  1412. z[0] = uint8(extensionSCT >> 8)
  1413. z[1] = uint8(extensionSCT)
  1414. sctLenPos := z[2:6]
  1415. z = z[6:]
  1416. extensionLen += 6
  1417. sctLen := 2
  1418. for _, sct := range cert.sctList {
  1419. z[0] = uint8(len(sct) >> 8)
  1420. z[1] = uint8(len(sct))
  1421. copy(z[2:], sct)
  1422. z = z[2+len(sct):]
  1423. extensionLen += 2 + len(sct)
  1424. sctLen += 2 + len(sct)
  1425. }
  1426. sctLenPos[0] = uint8(sctLen >> 8)
  1427. sctLenPos[1] = uint8(sctLen)
  1428. sctLen -= 2
  1429. sctLenPos[2] = uint8(sctLen >> 8)
  1430. sctLenPos[3] = uint8(sctLen)
  1431. }
  1432. extLenPos[0] = uint8(extensionLen >> 8)
  1433. extLenPos[1] = uint8(extensionLen)
  1434. }
  1435. m.raw = x
  1436. return
  1437. }
  1438. func (m *certificateMsg13) unmarshal(data []byte) alert {
  1439. if len(data) < 5 {
  1440. return alertDecodeError
  1441. }
  1442. m.raw = data
  1443. ctxLen := data[4]
  1444. if len(data) < int(ctxLen)+5+3 {
  1445. return alertDecodeError
  1446. }
  1447. m.requestContext = data[5 : 5+ctxLen]
  1448. d := data[5+ctxLen:]
  1449. certsLen := uint32(d[0])<<16 | uint32(d[1])<<8 | uint32(d[2])
  1450. if uint32(len(d)) != certsLen+3 {
  1451. return alertDecodeError
  1452. }
  1453. numCerts := 0
  1454. d = d[3:]
  1455. for certsLen > 0 {
  1456. if len(d) < 4 {
  1457. return alertDecodeError
  1458. }
  1459. certLen := uint32(d[0])<<16 | uint32(d[1])<<8 | uint32(d[2])
  1460. if uint32(len(d)) < 3+certLen {
  1461. return alertDecodeError
  1462. }
  1463. d = d[3+certLen:]
  1464. if len(d) < 2 {
  1465. return alertDecodeError
  1466. }
  1467. extLen := uint16(d[0])<<8 | uint16(d[1])
  1468. if uint16(len(d)) < 2+extLen {
  1469. return alertDecodeError
  1470. }
  1471. d = d[2+extLen:]
  1472. certsLen -= 3 + certLen + 2 + uint32(extLen)
  1473. numCerts++
  1474. }
  1475. m.certificates = make([]certificateEntry, numCerts)
  1476. d = data[8+ctxLen:]
  1477. for i := 0; i < numCerts; i++ {
  1478. certLen := uint32(d[0])<<16 | uint32(d[1])<<8 | uint32(d[2])
  1479. m.certificates[i].data = d[3 : 3+certLen]
  1480. d = d[3+certLen:]
  1481. extLen := uint16(d[0])<<8 | uint16(d[1])
  1482. d = d[2:]
  1483. for extLen > 0 {
  1484. if extLen < 4 {
  1485. return alertDecodeError
  1486. }
  1487. typ := uint16(d[0])<<8 | uint16(d[1])
  1488. bodyLen := uint16(d[2])<<8 | uint16(d[3])
  1489. if extLen < 4+bodyLen {
  1490. return alertDecodeError
  1491. }
  1492. body := d[4 : 4+bodyLen]
  1493. d = d[4+bodyLen:]
  1494. extLen -= 4 + bodyLen
  1495. switch typ {
  1496. case extensionStatusRequest:
  1497. if len(body) < 4 || body[0] != 0x01 {
  1498. return alertDecodeError
  1499. }
  1500. ocspLen := int(body[1])<<16 | int(body[2])<<8 | int(body[3])
  1501. if len(body) != 4+ocspLen {
  1502. return alertDecodeError
  1503. }
  1504. m.certificates[i].ocspStaple = body[4:]
  1505. case extensionSCT:
  1506. if len(body) < 2 {
  1507. return alertDecodeError
  1508. }
  1509. listLen := int(body[0])<<8 | int(body[1])
  1510. body = body[2:]
  1511. if len(body) != listLen {
  1512. return alertDecodeError
  1513. }
  1514. for len(body) > 0 {
  1515. if len(body) < 2 {
  1516. return alertDecodeError
  1517. }
  1518. sctLen := int(body[0])<<8 | int(body[1])
  1519. if len(body) < 2+sctLen {
  1520. return alertDecodeError
  1521. }
  1522. m.certificates[i].sctList = append(m.certificates[i].sctList, body[2:2+sctLen])
  1523. body = body[2+sctLen:]
  1524. }
  1525. }
  1526. }
  1527. }
  1528. return alertSuccess
  1529. }
  1530. type serverKeyExchangeMsg struct {
  1531. raw []byte
  1532. key []byte
  1533. }
  1534. func (m *serverKeyExchangeMsg) equal(i interface{}) bool {
  1535. m1, ok := i.(*serverKeyExchangeMsg)
  1536. if !ok {
  1537. return false
  1538. }
  1539. return bytes.Equal(m.raw, m1.raw) &&
  1540. bytes.Equal(m.key, m1.key)
  1541. }
  1542. func (m *serverKeyExchangeMsg) marshal() []byte {
  1543. if m.raw != nil {
  1544. return m.raw
  1545. }
  1546. length := len(m.key)
  1547. x := make([]byte, length+4)
  1548. x[0] = typeServerKeyExchange
  1549. x[1] = uint8(length >> 16)
  1550. x[2] = uint8(length >> 8)
  1551. x[3] = uint8(length)
  1552. copy(x[4:], m.key)
  1553. m.raw = x
  1554. return x
  1555. }
  1556. func (m *serverKeyExchangeMsg) unmarshal(data []byte) alert {
  1557. m.raw = data
  1558. if len(data) < 4 {
  1559. return alertDecodeError
  1560. }
  1561. m.key = data[4:]
  1562. return alertSuccess
  1563. }
  1564. type certificateStatusMsg struct {
  1565. raw []byte
  1566. statusType uint8
  1567. response []byte
  1568. }
  1569. func (m *certificateStatusMsg) equal(i interface{}) bool {
  1570. m1, ok := i.(*certificateStatusMsg)
  1571. if !ok {
  1572. return false
  1573. }
  1574. return bytes.Equal(m.raw, m1.raw) &&
  1575. m.statusType == m1.statusType &&
  1576. bytes.Equal(m.response, m1.response)
  1577. }
  1578. func (m *certificateStatusMsg) marshal() []byte {
  1579. if m.raw != nil {
  1580. return m.raw
  1581. }
  1582. var x []byte
  1583. if m.statusType == statusTypeOCSP {
  1584. x = make([]byte, 4+4+len(m.response))
  1585. x[0] = typeCertificateStatus
  1586. l := len(m.response) + 4
  1587. x[1] = byte(l >> 16)
  1588. x[2] = byte(l >> 8)
  1589. x[3] = byte(l)
  1590. x[4] = statusTypeOCSP
  1591. l -= 4
  1592. x[5] = byte(l >> 16)
  1593. x[6] = byte(l >> 8)
  1594. x[7] = byte(l)
  1595. copy(x[8:], m.response)
  1596. } else {
  1597. x = []byte{typeCertificateStatus, 0, 0, 1, m.statusType}
  1598. }
  1599. m.raw = x
  1600. return x
  1601. }
  1602. func (m *certificateStatusMsg) unmarshal(data []byte) alert {
  1603. m.raw = data
  1604. if len(data) < 5 {
  1605. return alertDecodeError
  1606. }
  1607. m.statusType = data[4]
  1608. m.response = nil
  1609. if m.statusType == statusTypeOCSP {
  1610. if len(data) < 8 {
  1611. return alertDecodeError
  1612. }
  1613. respLen := uint32(data[5])<<16 | uint32(data[6])<<8 | uint32(data[7])
  1614. if uint32(len(data)) != 4+4+respLen {
  1615. return alertDecodeError
  1616. }
  1617. m.response = data[8:]
  1618. }
  1619. return alertSuccess
  1620. }
  1621. type serverHelloDoneMsg struct{}
  1622. func (m *serverHelloDoneMsg) equal(i interface{}) bool {
  1623. _, ok := i.(*serverHelloDoneMsg)
  1624. return ok
  1625. }
  1626. func (m *serverHelloDoneMsg) marshal() []byte {
  1627. x := make([]byte, 4)
  1628. x[0] = typeServerHelloDone
  1629. return x
  1630. }
  1631. func (m *serverHelloDoneMsg) unmarshal(data []byte) alert {
  1632. if len(data) != 4 {
  1633. return alertDecodeError
  1634. }
  1635. return alertSuccess
  1636. }
  1637. type clientKeyExchangeMsg struct {
  1638. raw []byte
  1639. ciphertext []byte
  1640. }
  1641. func (m *clientKeyExchangeMsg) equal(i interface{}) bool {
  1642. m1, ok := i.(*clientKeyExchangeMsg)
  1643. if !ok {
  1644. return false
  1645. }
  1646. return bytes.Equal(m.raw, m1.raw) &&
  1647. bytes.Equal(m.ciphertext, m1.ciphertext)
  1648. }
  1649. func (m *clientKeyExchangeMsg) marshal() []byte {
  1650. if m.raw != nil {
  1651. return m.raw
  1652. }
  1653. length := len(m.ciphertext)
  1654. x := make([]byte, length+4)
  1655. x[0] = typeClientKeyExchange
  1656. x[1] = uint8(length >> 16)
  1657. x[2] = uint8(length >> 8)
  1658. x[3] = uint8(length)
  1659. copy(x[4:], m.ciphertext)
  1660. m.raw = x
  1661. return x
  1662. }
  1663. func (m *clientKeyExchangeMsg) unmarshal(data []byte) alert {
  1664. m.raw = data
  1665. if len(data) < 4 {
  1666. return alertDecodeError
  1667. }
  1668. l := int(data[1])<<16 | int(data[2])<<8 | int(data[3])
  1669. if l != len(data)-4 {
  1670. return alertDecodeError
  1671. }
  1672. m.ciphertext = data[4:]
  1673. return alertSuccess
  1674. }
  1675. type finishedMsg struct {
  1676. raw []byte
  1677. verifyData []byte
  1678. }
  1679. func (m *finishedMsg) equal(i interface{}) bool {
  1680. m1, ok := i.(*finishedMsg)
  1681. if !ok {
  1682. return false
  1683. }
  1684. return bytes.Equal(m.raw, m1.raw) &&
  1685. bytes.Equal(m.verifyData, m1.verifyData)
  1686. }
  1687. func (m *finishedMsg) marshal() (x []byte) {
  1688. if m.raw != nil {
  1689. return m.raw
  1690. }
  1691. x = make([]byte, 4+len(m.verifyData))
  1692. x[0] = typeFinished
  1693. x[3] = byte(len(m.verifyData))
  1694. copy(x[4:], m.verifyData)
  1695. m.raw = x
  1696. return
  1697. }
  1698. func (m *finishedMsg) unmarshal(data []byte) alert {
  1699. m.raw = data
  1700. if len(data) < 4 {
  1701. return alertDecodeError
  1702. }
  1703. m.verifyData = data[4:]
  1704. return alertSuccess
  1705. }
  1706. type nextProtoMsg struct {
  1707. raw []byte
  1708. proto string
  1709. }
  1710. func (m *nextProtoMsg) equal(i interface{}) bool {
  1711. m1, ok := i.(*nextProtoMsg)
  1712. if !ok {
  1713. return false
  1714. }
  1715. return bytes.Equal(m.raw, m1.raw) &&
  1716. m.proto == m1.proto
  1717. }
  1718. func (m *nextProtoMsg) marshal() []byte {
  1719. if m.raw != nil {
  1720. return m.raw
  1721. }
  1722. l := len(m.proto)
  1723. if l > 255 {
  1724. l = 255
  1725. }
  1726. padding := 32 - (l+2)%32
  1727. length := l + padding + 2
  1728. x := make([]byte, length+4)
  1729. x[0] = typeNextProtocol
  1730. x[1] = uint8(length >> 16)
  1731. x[2] = uint8(length >> 8)
  1732. x[3] = uint8(length)
  1733. y := x[4:]
  1734. y[0] = byte(l)
  1735. copy(y[1:], []byte(m.proto[0:l]))
  1736. y = y[1+l:]
  1737. y[0] = byte(padding)
  1738. m.raw = x
  1739. return x
  1740. }
  1741. func (m *nextProtoMsg) unmarshal(data []byte) alert {
  1742. m.raw = data
  1743. if len(data) < 5 {
  1744. return alertDecodeError
  1745. }
  1746. data = data[4:]
  1747. protoLen := int(data[0])
  1748. data = data[1:]
  1749. if len(data) < protoLen {
  1750. return alertDecodeError
  1751. }
  1752. m.proto = string(data[0:protoLen])
  1753. data = data[protoLen:]
  1754. if len(data) < 1 {
  1755. return alertDecodeError
  1756. }
  1757. paddingLen := int(data[0])
  1758. data = data[1:]
  1759. if len(data) != paddingLen {
  1760. return alertDecodeError
  1761. }
  1762. return alertSuccess
  1763. }
  1764. type certificateRequestMsg struct {
  1765. raw []byte
  1766. // hasSignatureAndHash indicates whether this message includes a list
  1767. // of signature and hash functions. This change was introduced with TLS
  1768. // 1.2.
  1769. hasSignatureAndHash bool
  1770. certificateTypes []byte
  1771. supportedSignatureAlgorithms []SignatureScheme
  1772. certificateAuthorities [][]byte
  1773. }
  1774. func (m *certificateRequestMsg) equal(i interface{}) bool {
  1775. m1, ok := i.(*certificateRequestMsg)
  1776. if !ok {
  1777. return false
  1778. }
  1779. return bytes.Equal(m.raw, m1.raw) &&
  1780. bytes.Equal(m.certificateTypes, m1.certificateTypes) &&
  1781. eqByteSlices(m.certificateAuthorities, m1.certificateAuthorities) &&
  1782. eqSignatureAlgorithms(m.supportedSignatureAlgorithms, m1.supportedSignatureAlgorithms)
  1783. }
  1784. func (m *certificateRequestMsg) marshal() (x []byte) {
  1785. if m.raw != nil {
  1786. return m.raw
  1787. }
  1788. // See http://tools.ietf.org/html/rfc4346#section-7.4.4
  1789. length := 1 + len(m.certificateTypes) + 2
  1790. casLength := 0
  1791. for _, ca := range m.certificateAuthorities {
  1792. casLength += 2 + len(ca)
  1793. }
  1794. length += casLength
  1795. if m.hasSignatureAndHash {
  1796. length += 2 + 2*len(m.supportedSignatureAlgorithms)
  1797. }
  1798. x = make([]byte, 4+length)
  1799. x[0] = typeCertificateRequest
  1800. x[1] = uint8(length >> 16)
  1801. x[2] = uint8(length >> 8)
  1802. x[3] = uint8(length)
  1803. x[4] = uint8(len(m.certificateTypes))
  1804. copy(x[5:], m.certificateTypes)
  1805. y := x[5+len(m.certificateTypes):]
  1806. if m.hasSignatureAndHash {
  1807. n := len(m.supportedSignatureAlgorithms) * 2
  1808. y[0] = uint8(n >> 8)
  1809. y[1] = uint8(n)
  1810. y = y[2:]
  1811. for _, sigAlgo := range m.supportedSignatureAlgorithms {
  1812. y[0] = uint8(sigAlgo >> 8)
  1813. y[1] = uint8(sigAlgo)
  1814. y = y[2:]
  1815. }
  1816. }
  1817. y[0] = uint8(casLength >> 8)
  1818. y[1] = uint8(casLength)
  1819. y = y[2:]
  1820. for _, ca := range m.certificateAuthorities {
  1821. y[0] = uint8(len(ca) >> 8)
  1822. y[1] = uint8(len(ca))
  1823. y = y[2:]
  1824. copy(y, ca)
  1825. y = y[len(ca):]
  1826. }
  1827. m.raw = x
  1828. return
  1829. }
  1830. func (m *certificateRequestMsg) unmarshal(data []byte) alert {
  1831. m.raw = data
  1832. if len(data) < 5 {
  1833. return alertDecodeError
  1834. }
  1835. length := uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
  1836. if uint32(len(data))-4 != length {
  1837. return alertDecodeError
  1838. }
  1839. numCertTypes := int(data[4])
  1840. data = data[5:]
  1841. if numCertTypes == 0 || len(data) <= numCertTypes {
  1842. return alertDecodeError
  1843. }
  1844. m.certificateTypes = make([]byte, numCertTypes)
  1845. if copy(m.certificateTypes, data) != numCertTypes {
  1846. return alertDecodeError
  1847. }
  1848. data = data[numCertTypes:]
  1849. if m.hasSignatureAndHash {
  1850. if len(data) < 2 {
  1851. return alertDecodeError
  1852. }
  1853. sigAndHashLen := uint16(data[0])<<8 | uint16(data[1])
  1854. data = data[2:]
  1855. if sigAndHashLen&1 != 0 {
  1856. return alertDecodeError
  1857. }
  1858. if len(data) < int(sigAndHashLen) {
  1859. return alertDecodeError
  1860. }
  1861. numSigAlgos := sigAndHashLen / 2
  1862. m.supportedSignatureAlgorithms = make([]SignatureScheme, numSigAlgos)
  1863. for i := range m.supportedSignatureAlgorithms {
  1864. m.supportedSignatureAlgorithms[i] = SignatureScheme(data[0])<<8 | SignatureScheme(data[1])
  1865. data = data[2:]
  1866. }
  1867. }
  1868. if len(data) < 2 {
  1869. return alertDecodeError
  1870. }
  1871. casLength := uint16(data[0])<<8 | uint16(data[1])
  1872. data = data[2:]
  1873. if len(data) < int(casLength) {
  1874. return alertDecodeError
  1875. }
  1876. cas := make([]byte, casLength)
  1877. copy(cas, data)
  1878. data = data[casLength:]
  1879. m.certificateAuthorities = nil
  1880. for len(cas) > 0 {
  1881. if len(cas) < 2 {
  1882. return alertDecodeError
  1883. }
  1884. caLen := uint16(cas[0])<<8 | uint16(cas[1])
  1885. cas = cas[2:]
  1886. if len(cas) < int(caLen) {
  1887. return alertDecodeError
  1888. }
  1889. m.certificateAuthorities = append(m.certificateAuthorities, cas[:caLen])
  1890. cas = cas[caLen:]
  1891. }
  1892. if len(data) != 0 {
  1893. return alertDecodeError
  1894. }
  1895. return alertSuccess
  1896. }
  1897. type certificateRequestMsg13 struct {
  1898. raw []byte
  1899. requestContext []byte
  1900. supportedSignatureAlgorithms []SignatureScheme
  1901. supportedSignatureAlgorithmsCert []SignatureScheme
  1902. certificateAuthorities [][]byte
  1903. }
  1904. func (m *certificateRequestMsg13) equal(i interface{}) bool {
  1905. m1, ok := i.(*certificateRequestMsg13)
  1906. return ok &&
  1907. bytes.Equal(m.raw, m1.raw) &&
  1908. bytes.Equal(m.requestContext, m1.requestContext) &&
  1909. eqByteSlices(m.certificateAuthorities, m1.certificateAuthorities) &&
  1910. eqSignatureAlgorithms(m.supportedSignatureAlgorithms, m1.supportedSignatureAlgorithms) &&
  1911. eqSignatureAlgorithms(m.supportedSignatureAlgorithmsCert, m1.supportedSignatureAlgorithmsCert)
  1912. }
  1913. func (m *certificateRequestMsg13) marshal() (x []byte) {
  1914. if m.raw != nil {
  1915. return m.raw
  1916. }
  1917. // See https://tools.ietf.org/html/draft-ietf-tls-tls13-21#section-4.3.2
  1918. length := 1 + len(m.requestContext)
  1919. numExtensions := 1
  1920. extensionsLength := 2 + 2*len(m.supportedSignatureAlgorithms)
  1921. if m.getSignatureAlgorithmsCert() != nil {
  1922. numExtensions += 1
  1923. extensionsLength += 2 + 2*len(m.getSignatureAlgorithmsCert())
  1924. }
  1925. casLength := 0
  1926. if len(m.certificateAuthorities) > 0 {
  1927. for _, ca := range m.certificateAuthorities {
  1928. casLength += 2 + len(ca)
  1929. }
  1930. extensionsLength += 2 + casLength
  1931. numExtensions++
  1932. }
  1933. extensionsLength += 4 * numExtensions
  1934. length += 2 + extensionsLength
  1935. x = make([]byte, 4+length)
  1936. x[0] = typeCertificateRequest
  1937. x[1] = uint8(length >> 16)
  1938. x[2] = uint8(length >> 8)
  1939. x[3] = uint8(length)
  1940. x[4] = uint8(len(m.requestContext))
  1941. copy(x[5:], m.requestContext)
  1942. z := x[5+len(m.requestContext):]
  1943. z[0] = byte(extensionsLength >> 8)
  1944. z[1] = byte(extensionsLength)
  1945. z = z[2:]
  1946. // TODO: this function should be reused by CH
  1947. z = marshalExtensionSignatureAlgorithms(extensionSignatureAlgorithms, z, m.supportedSignatureAlgorithms)
  1948. if m.getSignatureAlgorithmsCert() != nil {
  1949. z = marshalExtensionSignatureAlgorithms(extensionSignatureAlgorithmsCert, z, m.getSignatureAlgorithmsCert())
  1950. }
  1951. // certificate_authorities
  1952. if casLength > 0 {
  1953. z[0] = byte(extensionCAs >> 8)
  1954. z[1] = byte(extensionCAs)
  1955. l := 2 + casLength
  1956. z[2] = byte(l >> 8)
  1957. z[3] = byte(l)
  1958. z = z[4:]
  1959. z[0] = uint8(casLength >> 8)
  1960. z[1] = uint8(casLength)
  1961. z = z[2:]
  1962. for _, ca := range m.certificateAuthorities {
  1963. z[0] = uint8(len(ca) >> 8)
  1964. z[1] = uint8(len(ca))
  1965. z = z[2:]
  1966. copy(z, ca)
  1967. z = z[len(ca):]
  1968. }
  1969. }
  1970. m.raw = x
  1971. return
  1972. }
  1973. func (m *certificateRequestMsg13) unmarshal(data []byte) alert {
  1974. m.raw = data
  1975. m.supportedSignatureAlgorithms = nil
  1976. m.certificateAuthorities = nil
  1977. if len(data) < 5 {
  1978. return alertDecodeError
  1979. }
  1980. length := uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
  1981. if uint32(len(data))-4 != length {
  1982. return alertDecodeError
  1983. }
  1984. ctxLen := data[4]
  1985. if len(data) < 5+int(ctxLen)+2 {
  1986. return alertDecodeError
  1987. }
  1988. m.requestContext = data[5 : 5+ctxLen]
  1989. data = data[5+ctxLen:]
  1990. extensionsLength := int(data[0])<<8 | int(data[1])
  1991. data = data[2:]
  1992. if len(data) != extensionsLength {
  1993. return alertDecodeError
  1994. }
  1995. for len(data) != 0 {
  1996. if len(data) < 4 {
  1997. return alertDecodeError
  1998. }
  1999. extension := uint16(data[0])<<8 | uint16(data[1])
  2000. length := int(data[2])<<8 | int(data[3])
  2001. data = data[4:]
  2002. if len(data) < length {
  2003. return alertDecodeError
  2004. }
  2005. switch extension {
  2006. case extensionSignatureAlgorithms:
  2007. // TODO: unmarshalExtensionSignatureAlgorithms should be shared with CH and pre-1.3 CV
  2008. // https://tools.ietf.org/html/draft-ietf-tls-tls13-21#section-4.2.3
  2009. var err alert
  2010. m.supportedSignatureAlgorithms, err = unmarshalExtensionSignatureAlgorithms(data, length)
  2011. if err != alertSuccess {
  2012. return err
  2013. }
  2014. case extensionSignatureAlgorithmsCert:
  2015. var err alert
  2016. m.supportedSignatureAlgorithmsCert, err = unmarshalExtensionSignatureAlgorithms(data, length)
  2017. if err != alertSuccess {
  2018. return err
  2019. }
  2020. case extensionCAs:
  2021. // TODO DRY: share code with CH
  2022. if length < 2 {
  2023. return alertDecodeError
  2024. }
  2025. l := int(data[0])<<8 | int(data[1])
  2026. if l != length-2 || l < 3 {
  2027. return alertDecodeError
  2028. }
  2029. cas := make([]byte, l)
  2030. copy(cas, data[2:])
  2031. m.certificateAuthorities = nil
  2032. for len(cas) > 0 {
  2033. if len(cas) < 2 {
  2034. return alertDecodeError
  2035. }
  2036. caLen := uint16(cas[0])<<8 | uint16(cas[1])
  2037. cas = cas[2:]
  2038. if len(cas) < int(caLen) {
  2039. return alertDecodeError
  2040. }
  2041. m.certificateAuthorities = append(m.certificateAuthorities, cas[:caLen])
  2042. cas = cas[caLen:]
  2043. }
  2044. }
  2045. data = data[length:]
  2046. }
  2047. if len(m.supportedSignatureAlgorithms) == 0 {
  2048. return alertDecodeError
  2049. }
  2050. return alertSuccess
  2051. }
  2052. func (m *certificateRequestMsg13) getSignatureAlgorithmsCert() []SignatureScheme {
  2053. return signAlgosCertList(m.supportedSignatureAlgorithms, m.supportedSignatureAlgorithmsCert)
  2054. }
  2055. type certificateVerifyMsg struct {
  2056. raw []byte
  2057. hasSignatureAndHash bool
  2058. signatureAlgorithm SignatureScheme
  2059. signature []byte
  2060. }
  2061. func (m *certificateVerifyMsg) equal(i interface{}) bool {
  2062. m1, ok := i.(*certificateVerifyMsg)
  2063. if !ok {
  2064. return false
  2065. }
  2066. return bytes.Equal(m.raw, m1.raw) &&
  2067. m.hasSignatureAndHash == m1.hasSignatureAndHash &&
  2068. m.signatureAlgorithm == m1.signatureAlgorithm &&
  2069. bytes.Equal(m.signature, m1.signature)
  2070. }
  2071. func (m *certificateVerifyMsg) marshal() (x []byte) {
  2072. if m.raw != nil {
  2073. return m.raw
  2074. }
  2075. // See http://tools.ietf.org/html/rfc4346#section-7.4.8
  2076. siglength := len(m.signature)
  2077. length := 2 + siglength
  2078. if m.hasSignatureAndHash {
  2079. length += 2
  2080. }
  2081. x = make([]byte, 4+length)
  2082. x[0] = typeCertificateVerify
  2083. x[1] = uint8(length >> 16)
  2084. x[2] = uint8(length >> 8)
  2085. x[3] = uint8(length)
  2086. y := x[4:]
  2087. if m.hasSignatureAndHash {
  2088. y[0] = uint8(m.signatureAlgorithm >> 8)
  2089. y[1] = uint8(m.signatureAlgorithm)
  2090. y = y[2:]
  2091. }
  2092. y[0] = uint8(siglength >> 8)
  2093. y[1] = uint8(siglength)
  2094. copy(y[2:], m.signature)
  2095. m.raw = x
  2096. return
  2097. }
  2098. func (m *certificateVerifyMsg) unmarshal(data []byte) alert {
  2099. m.raw = data
  2100. if len(data) < 6 {
  2101. return alertDecodeError
  2102. }
  2103. length := uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
  2104. if uint32(len(data))-4 != length {
  2105. return alertDecodeError
  2106. }
  2107. data = data[4:]
  2108. if m.hasSignatureAndHash {
  2109. m.signatureAlgorithm = SignatureScheme(data[0])<<8 | SignatureScheme(data[1])
  2110. data = data[2:]
  2111. }
  2112. if len(data) < 2 {
  2113. return alertDecodeError
  2114. }
  2115. siglength := int(data[0])<<8 + int(data[1])
  2116. data = data[2:]
  2117. if len(data) != siglength {
  2118. return alertDecodeError
  2119. }
  2120. m.signature = data
  2121. return alertSuccess
  2122. }
  2123. type newSessionTicketMsg struct {
  2124. raw []byte
  2125. ticket []byte
  2126. }
  2127. func (m *newSessionTicketMsg) equal(i interface{}) bool {
  2128. m1, ok := i.(*newSessionTicketMsg)
  2129. if !ok {
  2130. return false
  2131. }
  2132. return bytes.Equal(m.raw, m1.raw) &&
  2133. bytes.Equal(m.ticket, m1.ticket)
  2134. }
  2135. func (m *newSessionTicketMsg) marshal() (x []byte) {
  2136. if m.raw != nil {
  2137. return m.raw
  2138. }
  2139. // See http://tools.ietf.org/html/rfc5077#section-3.3
  2140. ticketLen := len(m.ticket)
  2141. length := 2 + 4 + ticketLen
  2142. x = make([]byte, 4+length)
  2143. x[0] = typeNewSessionTicket
  2144. x[1] = uint8(length >> 16)
  2145. x[2] = uint8(length >> 8)
  2146. x[3] = uint8(length)
  2147. x[8] = uint8(ticketLen >> 8)
  2148. x[9] = uint8(ticketLen)
  2149. copy(x[10:], m.ticket)
  2150. m.raw = x
  2151. return
  2152. }
  2153. func (m *newSessionTicketMsg) unmarshal(data []byte) alert {
  2154. m.raw = data
  2155. if len(data) < 10 {
  2156. return alertDecodeError
  2157. }
  2158. length := uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
  2159. if uint32(len(data))-4 != length {
  2160. return alertDecodeError
  2161. }
  2162. ticketLen := int(data[8])<<8 + int(data[9])
  2163. if len(data)-10 != ticketLen {
  2164. return alertDecodeError
  2165. }
  2166. m.ticket = data[10:]
  2167. return alertSuccess
  2168. }
  2169. type newSessionTicketMsg13 struct {
  2170. raw []byte
  2171. lifetime uint32
  2172. ageAdd uint32
  2173. nonce []byte
  2174. ticket []byte
  2175. withEarlyDataInfo bool
  2176. maxEarlyDataLength uint32
  2177. }
  2178. func (m *newSessionTicketMsg13) equal(i interface{}) bool {
  2179. m1, ok := i.(*newSessionTicketMsg13)
  2180. if !ok {
  2181. return false
  2182. }
  2183. return bytes.Equal(m.raw, m1.raw) &&
  2184. m.lifetime == m1.lifetime &&
  2185. m.ageAdd == m1.ageAdd &&
  2186. bytes.Equal(m.nonce, m1.nonce) &&
  2187. bytes.Equal(m.ticket, m1.ticket) &&
  2188. m.withEarlyDataInfo == m1.withEarlyDataInfo &&
  2189. m.maxEarlyDataLength == m1.maxEarlyDataLength
  2190. }
  2191. func (m *newSessionTicketMsg13) marshal() (x []byte) {
  2192. if m.raw != nil {
  2193. return m.raw
  2194. }
  2195. // See https://tools.ietf.org/html/draft-ietf-tls-tls13-21#section-4.6.1
  2196. nonceLen := len(m.nonce)
  2197. ticketLen := len(m.ticket)
  2198. length := 13 + nonceLen + ticketLen
  2199. if m.withEarlyDataInfo {
  2200. length += 8
  2201. }
  2202. x = make([]byte, 4+length)
  2203. x[0] = typeNewSessionTicket
  2204. x[1] = uint8(length >> 16)
  2205. x[2] = uint8(length >> 8)
  2206. x[3] = uint8(length)
  2207. x[4] = uint8(m.lifetime >> 24)
  2208. x[5] = uint8(m.lifetime >> 16)
  2209. x[6] = uint8(m.lifetime >> 8)
  2210. x[7] = uint8(m.lifetime)
  2211. x[8] = uint8(m.ageAdd >> 24)
  2212. x[9] = uint8(m.ageAdd >> 16)
  2213. x[10] = uint8(m.ageAdd >> 8)
  2214. x[11] = uint8(m.ageAdd)
  2215. x[12] = uint8(nonceLen)
  2216. copy(x[13:13+nonceLen], m.nonce)
  2217. y := x[13+nonceLen:]
  2218. y[0] = uint8(ticketLen >> 8)
  2219. y[1] = uint8(ticketLen)
  2220. copy(y[2:2+ticketLen], m.ticket)
  2221. if m.withEarlyDataInfo {
  2222. z := y[2+ticketLen:]
  2223. // z[0] is already 0, this is the extensions vector length.
  2224. z[1] = 8
  2225. z[2] = uint8(extensionEarlyData >> 8)
  2226. z[3] = uint8(extensionEarlyData)
  2227. z[5] = 4
  2228. z[6] = uint8(m.maxEarlyDataLength >> 24)
  2229. z[7] = uint8(m.maxEarlyDataLength >> 16)
  2230. z[8] = uint8(m.maxEarlyDataLength >> 8)
  2231. z[9] = uint8(m.maxEarlyDataLength)
  2232. }
  2233. m.raw = x
  2234. return
  2235. }
  2236. func (m *newSessionTicketMsg13) unmarshal(data []byte) alert {
  2237. m.raw = data
  2238. m.maxEarlyDataLength = 0
  2239. m.withEarlyDataInfo = false
  2240. if len(data) < 17 {
  2241. return alertDecodeError
  2242. }
  2243. length := uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
  2244. if uint32(len(data))-4 != length {
  2245. return alertDecodeError
  2246. }
  2247. m.lifetime = uint32(data[4])<<24 | uint32(data[5])<<16 |
  2248. uint32(data[6])<<8 | uint32(data[7])
  2249. m.ageAdd = uint32(data[8])<<24 | uint32(data[9])<<16 |
  2250. uint32(data[10])<<8 | uint32(data[11])
  2251. nonceLen := int(data[12])
  2252. if nonceLen == 0 || 13+nonceLen+2 > len(data) {
  2253. return alertDecodeError
  2254. }
  2255. m.nonce = data[13 : 13+nonceLen]
  2256. data = data[13+nonceLen:]
  2257. ticketLen := int(data[0])<<8 + int(data[1])
  2258. if ticketLen == 0 || 2+ticketLen+2 > len(data) {
  2259. return alertDecodeError
  2260. }
  2261. m.ticket = data[2 : 2+ticketLen]
  2262. data = data[2+ticketLen:]
  2263. extLen := int(data[0])<<8 + int(data[1])
  2264. if extLen != len(data)-2 {
  2265. return alertDecodeError
  2266. }
  2267. data = data[2:]
  2268. for len(data) > 0 {
  2269. if len(data) < 4 {
  2270. return alertDecodeError
  2271. }
  2272. extType := uint16(data[0])<<8 + uint16(data[1])
  2273. length := int(data[2])<<8 + int(data[3])
  2274. data = data[4:]
  2275. switch extType {
  2276. case extensionEarlyData:
  2277. if length != 4 {
  2278. return alertDecodeError
  2279. }
  2280. m.withEarlyDataInfo = true
  2281. m.maxEarlyDataLength = uint32(data[0])<<24 | uint32(data[1])<<16 |
  2282. uint32(data[2])<<8 | uint32(data[3])
  2283. }
  2284. data = data[length:]
  2285. }
  2286. return alertSuccess
  2287. }
  2288. type endOfEarlyDataMsg struct {
  2289. }
  2290. func (*endOfEarlyDataMsg) marshal() []byte {
  2291. return []byte{typeEndOfEarlyData, 0, 0, 0}
  2292. }
  2293. func (*endOfEarlyDataMsg) unmarshal(data []byte) alert {
  2294. if len(data) != 4 {
  2295. return alertDecodeError
  2296. }
  2297. return alertSuccess
  2298. }
  2299. type helloRequestMsg struct {
  2300. }
  2301. func (*helloRequestMsg) marshal() []byte {
  2302. return []byte{typeHelloRequest, 0, 0, 0}
  2303. }
  2304. func (*helloRequestMsg) unmarshal(data []byte) alert {
  2305. if len(data) != 4 {
  2306. return alertDecodeError
  2307. }
  2308. return alertSuccess
  2309. }
  2310. func eqUint16s(x, y []uint16) bool {
  2311. if len(x) != len(y) {
  2312. return false
  2313. }
  2314. for i, v := range x {
  2315. if y[i] != v {
  2316. return false
  2317. }
  2318. }
  2319. return true
  2320. }
  2321. func eqCurveIDs(x, y []CurveID) bool {
  2322. if len(x) != len(y) {
  2323. return false
  2324. }
  2325. for i, v := range x {
  2326. if y[i] != v {
  2327. return false
  2328. }
  2329. }
  2330. return true
  2331. }
  2332. func eqStrings(x, y []string) bool {
  2333. if len(x) != len(y) {
  2334. return false
  2335. }
  2336. for i, v := range x {
  2337. if y[i] != v {
  2338. return false
  2339. }
  2340. }
  2341. return true
  2342. }
  2343. func eqByteSlices(x, y [][]byte) bool {
  2344. if len(x) != len(y) {
  2345. return false
  2346. }
  2347. for i, v := range x {
  2348. if !bytes.Equal(v, y[i]) {
  2349. return false
  2350. }
  2351. }
  2352. return true
  2353. }
  2354. func eqSignatureAlgorithms(x, y []SignatureScheme) bool {
  2355. if len(x) != len(y) {
  2356. return false
  2357. }
  2358. for i, v := range x {
  2359. if v != y[i] {
  2360. return false
  2361. }
  2362. }
  2363. return true
  2364. }
  2365. func eqKeyShares(x, y []keyShare) bool {
  2366. if len(x) != len(y) {
  2367. return false
  2368. }
  2369. for i := range x {
  2370. if x[i].group != y[i].group {
  2371. return false
  2372. }
  2373. if !bytes.Equal(x[i].data, y[i].data) {
  2374. return false
  2375. }
  2376. }
  2377. return true
  2378. }
  2379. func findExtension(data []byte, extensionType uint16) []byte {
  2380. for len(data) != 0 {
  2381. if len(data) < 4 {
  2382. return nil
  2383. }
  2384. extension := uint16(data[0])<<8 | uint16(data[1])
  2385. length := int(data[2])<<8 | int(data[3])
  2386. data = data[4:]
  2387. if len(data) < length {
  2388. return nil
  2389. }
  2390. if extension == extensionType {
  2391. return data[:length]
  2392. }
  2393. data = data[length:]
  2394. }
  2395. return nil
  2396. }