u_public.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. // Copyright 2017 Google Inc. 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. "crypto"
  7. "crypto/ecdh"
  8. "crypto/x509"
  9. "hash"
  10. "time"
  11. "github.com/cloudflare/circl/kem"
  12. )
  13. // ClientHandshakeState includes both TLS 1.3-only and TLS 1.2-only states,
  14. // only one of them will be used, depending on negotiated version.
  15. //
  16. // ClientHandshakeState will be converted into and from either
  17. // - clientHandshakeState (TLS 1.2)
  18. // - clientHandshakeStateTLS13 (TLS 1.3)
  19. //
  20. // uTLS will call .handshake() on one of these private internal states,
  21. // to perform TLS handshake using standard crypto/tls implementation.
  22. type PubClientHandshakeState struct {
  23. C *Conn
  24. ServerHello *PubServerHelloMsg
  25. Hello *PubClientHelloMsg
  26. MasterSecret []byte
  27. Session *SessionState
  28. State12 TLS12OnlyState
  29. State13 TLS13OnlyState
  30. uconn *UConn
  31. }
  32. // TLS 1.3 only
  33. type TLS13OnlyState struct {
  34. Suite *PubCipherSuiteTLS13
  35. EcdheKey *ecdh.PrivateKey
  36. KeySharesParams *KeySharesParameters
  37. KEMKey *KemPrivateKey
  38. EarlySecret []byte
  39. BinderKey []byte
  40. CertReq *CertificateRequestMsgTLS13
  41. UsingPSK bool // don't set this field when building client hello
  42. SentDummyCCS bool
  43. Transcript hash.Hash
  44. TrafficSecret []byte // client_application_traffic_secret_0
  45. }
  46. // TLS 1.2 and before only
  47. type TLS12OnlyState struct {
  48. FinishedHash FinishedHash
  49. Suite PubCipherSuite
  50. }
  51. func (chs *PubClientHandshakeState) toPrivate13() *clientHandshakeStateTLS13 {
  52. if chs == nil {
  53. return nil
  54. } else {
  55. return &clientHandshakeStateTLS13{
  56. c: chs.C,
  57. serverHello: chs.ServerHello.getPrivatePtr(),
  58. hello: chs.Hello.getPrivatePtr(),
  59. ecdheKey: chs.State13.EcdheKey,
  60. keySharesParams: chs.State13.KeySharesParams,
  61. kemKey: chs.State13.KEMKey.ToPrivate(),
  62. session: chs.Session,
  63. earlySecret: chs.State13.EarlySecret,
  64. binderKey: chs.State13.BinderKey,
  65. certReq: chs.State13.CertReq.toPrivate(),
  66. usingPSK: chs.State13.UsingPSK,
  67. sentDummyCCS: chs.State13.SentDummyCCS,
  68. suite: chs.State13.Suite.toPrivate(),
  69. transcript: chs.State13.Transcript,
  70. masterSecret: chs.MasterSecret,
  71. trafficSecret: chs.State13.TrafficSecret,
  72. uconn: chs.uconn,
  73. }
  74. }
  75. }
  76. func (chs13 *clientHandshakeStateTLS13) toPublic13() *PubClientHandshakeState {
  77. if chs13 == nil {
  78. return nil
  79. } else {
  80. tls13State := TLS13OnlyState{
  81. KeySharesParams: chs13.keySharesParams,
  82. EcdheKey: chs13.ecdheKey,
  83. KEMKey: chs13.kemKey.ToPublic(),
  84. EarlySecret: chs13.earlySecret,
  85. BinderKey: chs13.binderKey,
  86. CertReq: chs13.certReq.toPublic(),
  87. UsingPSK: chs13.usingPSK,
  88. SentDummyCCS: chs13.sentDummyCCS,
  89. Suite: chs13.suite.toPublic(),
  90. TrafficSecret: chs13.trafficSecret,
  91. Transcript: chs13.transcript,
  92. }
  93. return &PubClientHandshakeState{
  94. C: chs13.c,
  95. ServerHello: chs13.serverHello.getPublicPtr(),
  96. Hello: chs13.hello.getPublicPtr(),
  97. Session: chs13.session,
  98. MasterSecret: chs13.masterSecret,
  99. State13: tls13State,
  100. uconn: chs13.uconn,
  101. }
  102. }
  103. }
  104. func (chs *PubClientHandshakeState) toPrivate12() *clientHandshakeState {
  105. if chs == nil {
  106. return nil
  107. } else {
  108. return &clientHandshakeState{
  109. c: chs.C,
  110. serverHello: chs.ServerHello.getPrivatePtr(),
  111. hello: chs.Hello.getPrivatePtr(),
  112. suite: chs.State12.Suite.getPrivatePtr(),
  113. session: chs.Session,
  114. masterSecret: chs.MasterSecret,
  115. finishedHash: chs.State12.FinishedHash.getPrivateObj(),
  116. uconn: chs.uconn,
  117. }
  118. }
  119. }
  120. func (chs12 *clientHandshakeState) toPublic12() *PubClientHandshakeState {
  121. if chs12 == nil {
  122. return nil
  123. } else {
  124. tls12State := TLS12OnlyState{
  125. Suite: chs12.suite.getPublicObj(),
  126. FinishedHash: chs12.finishedHash.getPublicObj(),
  127. }
  128. return &PubClientHandshakeState{
  129. C: chs12.c,
  130. ServerHello: chs12.serverHello.getPublicPtr(),
  131. Hello: chs12.hello.getPublicPtr(),
  132. Session: chs12.session,
  133. MasterSecret: chs12.masterSecret,
  134. State12: tls12State,
  135. uconn: chs12.uconn,
  136. }
  137. }
  138. }
  139. // type EcdheParameters interface {
  140. // ecdheParameters
  141. // }
  142. type CertificateRequestMsgTLS13 struct {
  143. Raw []byte
  144. OcspStapling bool
  145. Scts bool
  146. SupportedSignatureAlgorithms []SignatureScheme
  147. SupportedSignatureAlgorithmsCert []SignatureScheme
  148. CertificateAuthorities [][]byte
  149. }
  150. func (crm *certificateRequestMsgTLS13) toPublic() *CertificateRequestMsgTLS13 {
  151. if crm == nil {
  152. return nil
  153. } else {
  154. return &CertificateRequestMsgTLS13{
  155. Raw: crm.raw,
  156. OcspStapling: crm.ocspStapling,
  157. Scts: crm.scts,
  158. SupportedSignatureAlgorithms: crm.supportedSignatureAlgorithms,
  159. SupportedSignatureAlgorithmsCert: crm.supportedSignatureAlgorithmsCert,
  160. CertificateAuthorities: crm.certificateAuthorities,
  161. }
  162. }
  163. }
  164. func (crm *CertificateRequestMsgTLS13) toPrivate() *certificateRequestMsgTLS13 {
  165. if crm == nil {
  166. return nil
  167. } else {
  168. return &certificateRequestMsgTLS13{
  169. raw: crm.Raw,
  170. ocspStapling: crm.OcspStapling,
  171. scts: crm.Scts,
  172. supportedSignatureAlgorithms: crm.SupportedSignatureAlgorithms,
  173. supportedSignatureAlgorithmsCert: crm.SupportedSignatureAlgorithmsCert,
  174. certificateAuthorities: crm.CertificateAuthorities,
  175. }
  176. }
  177. }
  178. type PubCipherSuiteTLS13 struct {
  179. Id uint16
  180. KeyLen int
  181. Aead func(key, fixedNonce []byte) aead
  182. Hash crypto.Hash
  183. }
  184. func (c *cipherSuiteTLS13) toPublic() *PubCipherSuiteTLS13 {
  185. if c == nil {
  186. return nil
  187. } else {
  188. return &PubCipherSuiteTLS13{
  189. Id: c.id,
  190. KeyLen: c.keyLen,
  191. Aead: c.aead,
  192. Hash: c.hash,
  193. }
  194. }
  195. }
  196. func (c *PubCipherSuiteTLS13) toPrivate() *cipherSuiteTLS13 {
  197. if c == nil {
  198. return nil
  199. } else {
  200. return &cipherSuiteTLS13{
  201. id: c.Id,
  202. keyLen: c.KeyLen,
  203. aead: c.Aead,
  204. hash: c.Hash,
  205. }
  206. }
  207. }
  208. type PubServerHelloMsg struct {
  209. Raw []byte
  210. Vers uint16
  211. Random []byte
  212. SessionId []byte
  213. CipherSuite uint16
  214. CompressionMethod uint8
  215. NextProtoNeg bool
  216. NextProtos []string
  217. OcspStapling bool
  218. Scts [][]byte
  219. ExtendedMasterSecret bool
  220. TicketSupported bool // used by go tls to determine whether to add the session ticket ext
  221. SecureRenegotiation []byte
  222. SecureRenegotiationSupported bool
  223. AlpnProtocol string
  224. // 1.3
  225. SupportedVersion uint16
  226. ServerShare keyShare
  227. SelectedIdentityPresent bool
  228. SelectedIdentity uint16
  229. Cookie []byte // HelloRetryRequest extension
  230. SelectedGroup CurveID // HelloRetryRequest extension
  231. }
  232. func (shm *PubServerHelloMsg) getPrivatePtr() *serverHelloMsg {
  233. if shm == nil {
  234. return nil
  235. } else {
  236. return &serverHelloMsg{
  237. raw: shm.Raw,
  238. vers: shm.Vers,
  239. random: shm.Random,
  240. sessionId: shm.SessionId,
  241. cipherSuite: shm.CipherSuite,
  242. compressionMethod: shm.CompressionMethod,
  243. nextProtoNeg: shm.NextProtoNeg,
  244. nextProtos: shm.NextProtos,
  245. ocspStapling: shm.OcspStapling,
  246. scts: shm.Scts,
  247. extendedMasterSecret: shm.ExtendedMasterSecret,
  248. ticketSupported: shm.TicketSupported,
  249. secureRenegotiation: shm.SecureRenegotiation,
  250. secureRenegotiationSupported: shm.SecureRenegotiationSupported,
  251. alpnProtocol: shm.AlpnProtocol,
  252. supportedVersion: shm.SupportedVersion,
  253. serverShare: shm.ServerShare,
  254. selectedIdentityPresent: shm.SelectedIdentityPresent,
  255. selectedIdentity: shm.SelectedIdentity,
  256. cookie: shm.Cookie,
  257. selectedGroup: shm.SelectedGroup,
  258. }
  259. }
  260. }
  261. func (shm *serverHelloMsg) getPublicPtr() *PubServerHelloMsg {
  262. if shm == nil {
  263. return nil
  264. } else {
  265. return &PubServerHelloMsg{
  266. Raw: shm.raw,
  267. Vers: shm.vers,
  268. Random: shm.random,
  269. SessionId: shm.sessionId,
  270. CipherSuite: shm.cipherSuite,
  271. CompressionMethod: shm.compressionMethod,
  272. NextProtoNeg: shm.nextProtoNeg,
  273. NextProtos: shm.nextProtos,
  274. OcspStapling: shm.ocspStapling,
  275. Scts: shm.scts,
  276. ExtendedMasterSecret: shm.extendedMasterSecret,
  277. TicketSupported: shm.ticketSupported,
  278. SecureRenegotiation: shm.secureRenegotiation,
  279. SecureRenegotiationSupported: shm.secureRenegotiationSupported,
  280. AlpnProtocol: shm.alpnProtocol,
  281. SupportedVersion: shm.supportedVersion,
  282. ServerShare: shm.serverShare,
  283. SelectedIdentityPresent: shm.selectedIdentityPresent,
  284. SelectedIdentity: shm.selectedIdentity,
  285. Cookie: shm.cookie,
  286. SelectedGroup: shm.selectedGroup,
  287. }
  288. }
  289. }
  290. type PubClientHelloMsg struct {
  291. Raw []byte
  292. Vers uint16
  293. Random []byte
  294. SessionId []byte
  295. CipherSuites []uint16
  296. CompressionMethods []uint8
  297. NextProtoNeg bool
  298. ServerName string
  299. OcspStapling bool
  300. Scts bool
  301. Ems bool // [uTLS] actually implemented due to its prevalence
  302. SupportedCurves []CurveID
  303. SupportedPoints []uint8
  304. TicketSupported bool
  305. SessionTicket []uint8
  306. SupportedSignatureAlgorithms []SignatureScheme
  307. SecureRenegotiation []byte
  308. SecureRenegotiationSupported bool
  309. AlpnProtocols []string
  310. // 1.3
  311. SupportedSignatureAlgorithmsCert []SignatureScheme
  312. SupportedVersions []uint16
  313. Cookie []byte
  314. KeyShares []KeyShare
  315. EarlyData bool
  316. PskModes []uint8
  317. PskIdentities []PskIdentity
  318. PskBinders [][]byte
  319. QuicTransportParameters []byte
  320. cachedPrivateHello *clientHelloMsg // todo: further optimize to reduce clientHelloMsg construction
  321. }
  322. func (chm *PubClientHelloMsg) getPrivatePtr() *clientHelloMsg {
  323. if chm == nil {
  324. return nil
  325. } else {
  326. private := &clientHelloMsg{
  327. raw: chm.Raw,
  328. vers: chm.Vers,
  329. random: chm.Random,
  330. sessionId: chm.SessionId,
  331. cipherSuites: chm.CipherSuites,
  332. compressionMethods: chm.CompressionMethods,
  333. serverName: chm.ServerName,
  334. ocspStapling: chm.OcspStapling,
  335. supportedCurves: chm.SupportedCurves,
  336. supportedPoints: chm.SupportedPoints,
  337. ticketSupported: chm.TicketSupported,
  338. sessionTicket: chm.SessionTicket,
  339. supportedSignatureAlgorithms: chm.SupportedSignatureAlgorithms,
  340. supportedSignatureAlgorithmsCert: chm.SupportedSignatureAlgorithmsCert,
  341. secureRenegotiationSupported: chm.SecureRenegotiationSupported,
  342. secureRenegotiation: chm.SecureRenegotiation,
  343. extendedMasterSecret: chm.Ems,
  344. alpnProtocols: chm.AlpnProtocols,
  345. scts: chm.Scts,
  346. supportedVersions: chm.SupportedVersions,
  347. cookie: chm.Cookie,
  348. keyShares: KeyShares(chm.KeyShares).ToPrivate(),
  349. earlyData: chm.EarlyData,
  350. pskModes: chm.PskModes,
  351. pskIdentities: PskIdentities(chm.PskIdentities).ToPrivate(),
  352. pskBinders: chm.PskBinders,
  353. quicTransportParameters: chm.QuicTransportParameters,
  354. nextProtoNeg: chm.NextProtoNeg,
  355. }
  356. chm.cachedPrivateHello = private
  357. return private
  358. }
  359. }
  360. func (chm *PubClientHelloMsg) getCachedPrivatePtr() *clientHelloMsg {
  361. if chm == nil {
  362. return nil
  363. } else {
  364. return chm.cachedPrivateHello
  365. }
  366. }
  367. func (chm *clientHelloMsg) getPublicPtr() *PubClientHelloMsg {
  368. if chm == nil {
  369. return nil
  370. } else {
  371. return &PubClientHelloMsg{
  372. Raw: chm.raw,
  373. Vers: chm.vers,
  374. Random: chm.random,
  375. SessionId: chm.sessionId,
  376. CipherSuites: chm.cipherSuites,
  377. CompressionMethods: chm.compressionMethods,
  378. NextProtoNeg: chm.nextProtoNeg,
  379. ServerName: chm.serverName,
  380. OcspStapling: chm.ocspStapling,
  381. Scts: chm.scts,
  382. Ems: chm.extendedMasterSecret,
  383. SupportedCurves: chm.supportedCurves,
  384. SupportedPoints: chm.supportedPoints,
  385. TicketSupported: chm.ticketSupported,
  386. SessionTicket: chm.sessionTicket,
  387. SupportedSignatureAlgorithms: chm.supportedSignatureAlgorithms,
  388. SecureRenegotiation: chm.secureRenegotiation,
  389. SecureRenegotiationSupported: chm.secureRenegotiationSupported,
  390. AlpnProtocols: chm.alpnProtocols,
  391. SupportedSignatureAlgorithmsCert: chm.supportedSignatureAlgorithmsCert,
  392. SupportedVersions: chm.supportedVersions,
  393. Cookie: chm.cookie,
  394. KeyShares: keyShares(chm.keyShares).ToPublic(),
  395. EarlyData: chm.earlyData,
  396. PskModes: chm.pskModes,
  397. PskIdentities: pskIdentities(chm.pskIdentities).ToPublic(),
  398. PskBinders: chm.pskBinders,
  399. QuicTransportParameters: chm.quicTransportParameters,
  400. cachedPrivateHello: chm,
  401. }
  402. }
  403. }
  404. // UnmarshalClientHello allows external code to parse raw client hellos.
  405. // It returns nil on failure.
  406. func UnmarshalClientHello(data []byte) *PubClientHelloMsg {
  407. m := &clientHelloMsg{}
  408. if m.unmarshal(data) {
  409. return m.getPublicPtr()
  410. }
  411. return nil
  412. }
  413. // Marshal allows external code to convert a ClientHello object back into
  414. // raw bytes.
  415. func (chm *PubClientHelloMsg) Marshal() ([]byte, error) {
  416. return chm.getPrivatePtr().marshal()
  417. }
  418. // A CipherSuite is a specific combination of key agreement, cipher and MAC
  419. // function. All cipher suites currently assume RSA key agreement.
  420. type PubCipherSuite struct {
  421. Id uint16
  422. // the lengths, in bytes, of the key material needed for each component.
  423. KeyLen int
  424. MacLen int
  425. IvLen int
  426. Ka func(version uint16) keyAgreement
  427. // flags is a bitmask of the suite* values, above.
  428. Flags int
  429. Cipher func(key, iv []byte, isRead bool) interface{}
  430. Mac func(macKey []byte) hash.Hash
  431. Aead func(key, fixedNonce []byte) aead
  432. }
  433. func (cs *PubCipherSuite) getPrivatePtr() *cipherSuite {
  434. if cs == nil {
  435. return nil
  436. } else {
  437. return &cipherSuite{
  438. id: cs.Id,
  439. keyLen: cs.KeyLen,
  440. macLen: cs.MacLen,
  441. ivLen: cs.IvLen,
  442. ka: cs.Ka,
  443. flags: cs.Flags,
  444. cipher: cs.Cipher,
  445. mac: cs.Mac,
  446. aead: cs.Aead,
  447. }
  448. }
  449. }
  450. func (cs *cipherSuite) getPublicObj() PubCipherSuite {
  451. if cs == nil {
  452. return PubCipherSuite{}
  453. } else {
  454. return PubCipherSuite{
  455. Id: cs.id,
  456. KeyLen: cs.keyLen,
  457. MacLen: cs.macLen,
  458. IvLen: cs.ivLen,
  459. Ka: cs.ka,
  460. Flags: cs.flags,
  461. Cipher: cs.cipher,
  462. Mac: cs.mac,
  463. Aead: cs.aead,
  464. }
  465. }
  466. }
  467. // A FinishedHash calculates the hash of a set of handshake messages suitable
  468. // for including in a Finished message.
  469. type FinishedHash struct {
  470. Client hash.Hash
  471. Server hash.Hash
  472. // Prior to TLS 1.2, an additional MD5 hash is required.
  473. ClientMD5 hash.Hash
  474. ServerMD5 hash.Hash
  475. // In TLS 1.2, a full buffer is sadly required.
  476. Buffer []byte
  477. Version uint16
  478. Prf func(result, secret, label, seed []byte)
  479. }
  480. func (fh *FinishedHash) getPrivateObj() finishedHash {
  481. if fh == nil {
  482. return finishedHash{}
  483. } else {
  484. return finishedHash{
  485. client: fh.Client,
  486. server: fh.Server,
  487. clientMD5: fh.ClientMD5,
  488. serverMD5: fh.ServerMD5,
  489. buffer: fh.Buffer,
  490. version: fh.Version,
  491. prf: fh.Prf,
  492. }
  493. }
  494. }
  495. func (fh *finishedHash) getPublicObj() FinishedHash {
  496. if fh == nil {
  497. return FinishedHash{}
  498. } else {
  499. return FinishedHash{
  500. Client: fh.client,
  501. Server: fh.server,
  502. ClientMD5: fh.clientMD5,
  503. ServerMD5: fh.serverMD5,
  504. Buffer: fh.buffer,
  505. Version: fh.version,
  506. Prf: fh.prf}
  507. }
  508. }
  509. // TLS 1.3 Key Share. See RFC 8446, Section 4.2.8.
  510. type KeyShare struct {
  511. Group CurveID `json:"group"`
  512. Data []byte `json:"key_exchange,omitempty"` // optional
  513. }
  514. type KeyShares []KeyShare
  515. type keyShares []keyShare
  516. func (kss keyShares) ToPublic() []KeyShare {
  517. var KSS []KeyShare
  518. for _, ks := range kss {
  519. KSS = append(KSS, KeyShare{Data: ks.data, Group: ks.group})
  520. }
  521. return KSS
  522. }
  523. func (KSS KeyShares) ToPrivate() []keyShare {
  524. var kss []keyShare
  525. for _, KS := range KSS {
  526. kss = append(kss, keyShare{data: KS.Data, group: KS.Group})
  527. }
  528. return kss
  529. }
  530. // TLS 1.3 PSK Identity. Can be a Session Ticket, or a reference to a saved
  531. // session. See RFC 8446, Section 4.2.11.
  532. type PskIdentity struct {
  533. Label []byte `json:"identity"`
  534. ObfuscatedTicketAge uint32 `json:"obfuscated_ticket_age"`
  535. }
  536. type PskIdentities []PskIdentity
  537. type pskIdentities []pskIdentity
  538. func (pss pskIdentities) ToPublic() []PskIdentity {
  539. var PSS []PskIdentity
  540. for _, ps := range pss {
  541. PSS = append(PSS, PskIdentity{Label: ps.label, ObfuscatedTicketAge: ps.obfuscatedTicketAge})
  542. }
  543. return PSS
  544. }
  545. func (PSS PskIdentities) ToPrivate() []pskIdentity {
  546. var pss []pskIdentity
  547. for _, PS := range PSS {
  548. pss = append(pss, pskIdentity{label: PS.Label, obfuscatedTicketAge: PS.ObfuscatedTicketAge})
  549. }
  550. return pss
  551. }
  552. // ClientSessionState is public, but all its fields are private. Let's add setters, getters and constructor
  553. // ClientSessionState contains the state needed by clients to resume TLS sessions.
  554. func MakeClientSessionState(
  555. SessionTicket []uint8,
  556. Vers uint16,
  557. CipherSuite uint16,
  558. MasterSecret []byte,
  559. ServerCertificates []*x509.Certificate,
  560. VerifiedChains [][]*x509.Certificate) *ClientSessionState {
  561. // TODO: Add EMS to this constructor in uTLS v2
  562. css := &ClientSessionState{
  563. ticket: SessionTicket,
  564. session: &SessionState{
  565. version: Vers,
  566. cipherSuite: CipherSuite,
  567. secret: MasterSecret,
  568. peerCertificates: ServerCertificates,
  569. verifiedChains: VerifiedChains,
  570. },
  571. }
  572. return css
  573. }
  574. // Encrypted ticket used for session resumption with server
  575. func (css *ClientSessionState) SessionTicket() []uint8 {
  576. return css.ticket
  577. }
  578. // SSL/TLS version negotiated for the session
  579. func (css *ClientSessionState) Vers() uint16 {
  580. return css.session.version
  581. }
  582. // Ciphersuite negotiated for the session
  583. func (css *ClientSessionState) CipherSuite() uint16 {
  584. return css.session.cipherSuite
  585. }
  586. // MasterSecret generated by client on a full handshake
  587. func (css *ClientSessionState) MasterSecret() []byte {
  588. return css.session.secret
  589. }
  590. func (css *ClientSessionState) EMS() bool {
  591. return css.session.extMasterSecret
  592. }
  593. // Certificate chain presented by the server
  594. func (css *ClientSessionState) ServerCertificates() []*x509.Certificate {
  595. return css.session.peerCertificates
  596. }
  597. // Certificate chains we built for verification
  598. func (css *ClientSessionState) VerifiedChains() [][]*x509.Certificate {
  599. return css.session.verifiedChains
  600. }
  601. func (css *ClientSessionState) SetSessionTicket(SessionTicket []uint8) {
  602. css.ticket = SessionTicket
  603. }
  604. func (css *ClientSessionState) SetVers(Vers uint16) {
  605. if css.session == nil {
  606. css.session = &SessionState{}
  607. }
  608. css.session.version = Vers
  609. }
  610. func (css *ClientSessionState) SetCipherSuite(CipherSuite uint16) {
  611. if css.session == nil {
  612. css.session = &SessionState{}
  613. }
  614. css.session.cipherSuite = CipherSuite
  615. }
  616. func (css *ClientSessionState) SetCreatedAt(createdAt uint64) {
  617. if css.session == nil {
  618. css.session = &SessionState{}
  619. }
  620. css.session.createdAt = createdAt
  621. }
  622. func (css *ClientSessionState) SetMasterSecret(MasterSecret []byte) {
  623. if css.session == nil {
  624. css.session = &SessionState{}
  625. }
  626. css.session.secret = MasterSecret
  627. }
  628. func (css *ClientSessionState) SetEMS(ems bool) {
  629. if css.session == nil {
  630. css.session = &SessionState{}
  631. }
  632. css.session.extMasterSecret = ems
  633. }
  634. func (css *ClientSessionState) SetServerCertificates(ServerCertificates []*x509.Certificate) {
  635. if css.session == nil {
  636. css.session = &SessionState{}
  637. }
  638. css.session.peerCertificates = ServerCertificates
  639. }
  640. func (css *ClientSessionState) SetVerifiedChains(VerifiedChains [][]*x509.Certificate) {
  641. if css.session == nil {
  642. css.session = &SessionState{}
  643. }
  644. css.session.verifiedChains = VerifiedChains
  645. }
  646. func (css *ClientSessionState) SetUseBy(useBy uint64) {
  647. if css.session == nil {
  648. css.session = &SessionState{}
  649. }
  650. css.session.useBy = useBy
  651. }
  652. func (css *ClientSessionState) SetAgeAdd(ageAdd uint32) {
  653. if css.session == nil {
  654. css.session = &SessionState{}
  655. }
  656. css.session.ageAdd = ageAdd
  657. }
  658. // TicketKey is the internal representation of a session ticket key.
  659. type TicketKey struct {
  660. AesKey [16]byte
  661. HmacKey [16]byte
  662. // created is the time at which this ticket key was created. See Config.ticketKeys.
  663. Created time.Time
  664. }
  665. type TicketKeys []TicketKey
  666. type ticketKeys []ticketKey
  667. func TicketKeyFromBytes(b [32]byte) TicketKey {
  668. // [uTLS]
  669. // empty config is required
  670. config := &Config{}
  671. tk := config.ticketKeyFromBytes(b)
  672. return tk.ToPublic()
  673. }
  674. func (tk ticketKey) ToPublic() TicketKey {
  675. return TicketKey{
  676. AesKey: tk.aesKey,
  677. HmacKey: tk.hmacKey,
  678. Created: tk.created,
  679. }
  680. }
  681. func (TK TicketKey) ToPrivate() ticketKey {
  682. return ticketKey{
  683. aesKey: TK.AesKey,
  684. hmacKey: TK.HmacKey,
  685. created: TK.Created,
  686. }
  687. }
  688. func (tks ticketKeys) ToPublic() []TicketKey {
  689. var TKS []TicketKey
  690. for _, ks := range tks {
  691. TKS = append(TKS, ks.ToPublic())
  692. }
  693. return TKS
  694. }
  695. func (TKS TicketKeys) ToPrivate() []ticketKey {
  696. var tks []ticketKey
  697. for _, TK := range TKS {
  698. tks = append(tks, TK.ToPrivate())
  699. }
  700. return tks
  701. }
  702. type KemPrivateKey struct {
  703. SecretKey kem.PrivateKey
  704. CurveID CurveID
  705. }
  706. func (kpk *KemPrivateKey) ToPrivate() *kemPrivateKey {
  707. if kpk == nil {
  708. return nil
  709. } else {
  710. return &kemPrivateKey{
  711. secretKey: kpk.SecretKey,
  712. curveID: kpk.CurveID,
  713. }
  714. }
  715. }
  716. func (kpk *kemPrivateKey) ToPublic() *KemPrivateKey {
  717. if kpk == nil {
  718. return nil
  719. } else {
  720. return &KemPrivateKey{
  721. SecretKey: kpk.secretKey,
  722. CurveID: kpk.curveID,
  723. }
  724. }
  725. }