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

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