u_public.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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/x509"
  8. "hash"
  9. )
  10. // ClientHandshakeState includes both TLS 1.3-only and TLS 1.2-only states,
  11. // only one of them will be used, depending on negotiated version.
  12. //
  13. // ClientHandshakeState will be converted into and from either
  14. // - clientHandshakeState (TLS 1.2)
  15. // - clientHandshakeStateTLS13 (TLS 1.3)
  16. // uTLS will call .handshake() on one of these private internal states,
  17. // to perform TLS handshake using standard crypto/tls implementation.
  18. type ClientHandshakeState struct {
  19. C *Conn
  20. ServerHello *ServerHelloMsg
  21. Hello *ClientHelloMsg
  22. MasterSecret []byte
  23. Session *ClientSessionState
  24. State12 TLS12OnlyState
  25. State13 TLS13OnlyState
  26. }
  27. // TLS 1.3 only
  28. type TLS13OnlyState struct {
  29. Suite *CipherSuiteTLS13
  30. EcdheParams EcdheParameters
  31. EarlySecret []byte
  32. BinderKey []byte
  33. CertReq *CertificateRequestMsgTLS13
  34. UsingPSK bool
  35. SentDummyCCS bool
  36. Transcript hash.Hash
  37. TrafficSecret []byte // client_application_traffic_secret_0
  38. }
  39. // TLS 1.2 and before only
  40. type TLS12OnlyState struct {
  41. FinishedHash FinishedHash
  42. Suite CipherSuite
  43. }
  44. func (chs *ClientHandshakeState) toPrivate13() *clientHandshakeStateTLS13 {
  45. if chs == nil {
  46. return nil
  47. } else {
  48. return &clientHandshakeStateTLS13{
  49. c: chs.C,
  50. serverHello: chs.ServerHello.getPrivatePtr(),
  51. hello: chs.Hello.getPrivatePtr(),
  52. ecdheParams: chs.State13.EcdheParams,
  53. session: chs.Session,
  54. earlySecret: chs.State13.EarlySecret,
  55. binderKey: chs.State13.BinderKey,
  56. certReq: chs.State13.CertReq.toPrivate(),
  57. usingPSK: chs.State13.UsingPSK,
  58. sentDummyCCS: chs.State13.SentDummyCCS,
  59. suite: chs.State13.Suite.toPrivate(),
  60. transcript: chs.State13.Transcript,
  61. masterSecret: chs.MasterSecret,
  62. trafficSecret: chs.State13.TrafficSecret,
  63. }
  64. }
  65. }
  66. func (chs13 *clientHandshakeStateTLS13) toPublic13() *ClientHandshakeState {
  67. if chs13 == nil {
  68. return nil
  69. } else {
  70. tls13State := TLS13OnlyState{
  71. EcdheParams: chs13.ecdheParams,
  72. EarlySecret: chs13.earlySecret,
  73. BinderKey: chs13.binderKey,
  74. CertReq: chs13.certReq.toPublic(),
  75. UsingPSK: chs13.usingPSK,
  76. SentDummyCCS: chs13.sentDummyCCS,
  77. Suite: chs13.suite.toPublic(),
  78. TrafficSecret: chs13.trafficSecret,
  79. Transcript: chs13.transcript,
  80. }
  81. return &ClientHandshakeState{
  82. C: chs13.c,
  83. ServerHello: chs13.serverHello.getPublicPtr(),
  84. Hello: chs13.hello.getPublicPtr(),
  85. Session: chs13.session,
  86. MasterSecret: chs13.masterSecret,
  87. State13: tls13State,
  88. }
  89. }
  90. }
  91. func (chs *ClientHandshakeState) toPrivate12() *clientHandshakeState {
  92. if chs == nil {
  93. return nil
  94. } else {
  95. return &clientHandshakeState{
  96. c: chs.C,
  97. serverHello: chs.ServerHello.getPrivatePtr(),
  98. hello: chs.Hello.getPrivatePtr(),
  99. suite: chs.State12.Suite.getPrivatePtr(),
  100. session: chs.Session,
  101. masterSecret: chs.MasterSecret,
  102. finishedHash: *chs.State12.FinishedHash.getPrivatePtr(),
  103. }
  104. }
  105. }
  106. func (chs12 *clientHandshakeState) toPublic13() *ClientHandshakeState {
  107. if chs12 == nil {
  108. return nil
  109. } else {
  110. tls12State := TLS12OnlyState{
  111. Suite: *chs12.suite.getPublicPtr(),
  112. FinishedHash: *chs12.finishedHash.getPublicPtr(),
  113. }
  114. return &ClientHandshakeState{
  115. C: chs12.c,
  116. ServerHello: chs12.serverHello.getPublicPtr(),
  117. Hello: chs12.hello.getPublicPtr(),
  118. Session: chs12.session,
  119. MasterSecret: chs12.masterSecret,
  120. State12: tls12State,
  121. }
  122. }
  123. }
  124. type EcdheParameters interface {
  125. ecdheParameters
  126. }
  127. type CertificateRequestMsgTLS13 struct {
  128. Raw []byte
  129. OcspStapling bool
  130. Scts bool
  131. SupportedSignatureAlgorithms []SignatureScheme
  132. SupportedSignatureAlgorithmsCert []SignatureScheme
  133. CertificateAuthorities [][]byte
  134. }
  135. func (crm *certificateRequestMsgTLS13) toPublic() *CertificateRequestMsgTLS13 {
  136. if crm == nil {
  137. return nil
  138. } else {
  139. return &CertificateRequestMsgTLS13{
  140. Raw: crm.raw,
  141. OcspStapling: crm.ocspStapling,
  142. Scts: crm.scts,
  143. SupportedSignatureAlgorithms: crm.supportedSignatureAlgorithms,
  144. SupportedSignatureAlgorithmsCert: crm.supportedSignatureAlgorithmsCert,
  145. CertificateAuthorities: crm.certificateAuthorities,
  146. }
  147. }
  148. }
  149. func (crm *CertificateRequestMsgTLS13) toPrivate() *certificateRequestMsgTLS13 {
  150. if crm == nil {
  151. return nil
  152. } else {
  153. return &certificateRequestMsgTLS13{
  154. raw: crm.Raw,
  155. ocspStapling: crm.OcspStapling,
  156. scts: crm.Scts,
  157. supportedSignatureAlgorithms: crm.SupportedSignatureAlgorithms,
  158. supportedSignatureAlgorithmsCert: crm.SupportedSignatureAlgorithmsCert,
  159. certificateAuthorities: crm.CertificateAuthorities,
  160. }
  161. }
  162. }
  163. type CipherSuiteTLS13 struct {
  164. Id uint16
  165. KeyLen int
  166. Aead func(key, fixedNonce []byte) aead
  167. Hash crypto.Hash
  168. }
  169. func (c *cipherSuiteTLS13) toPublic() *CipherSuiteTLS13 {
  170. if c == nil {
  171. return nil
  172. } else {
  173. return &CipherSuiteTLS13{
  174. Id: c.id,
  175. KeyLen: c.keyLen,
  176. Aead: c.aead,
  177. Hash: c.hash,
  178. }
  179. }
  180. }
  181. func (c *CipherSuiteTLS13) toPrivate() *cipherSuiteTLS13 {
  182. if c == nil {
  183. return nil
  184. } else {
  185. return &cipherSuiteTLS13{
  186. id: c.Id,
  187. keyLen: c.KeyLen,
  188. aead: c.Aead,
  189. hash: c.Hash,
  190. }
  191. }
  192. }
  193. type ServerHelloMsg struct {
  194. Raw []byte
  195. Vers uint16
  196. Random []byte
  197. SessionId []byte
  198. CipherSuite uint16
  199. CompressionMethod uint8
  200. NextProtoNeg bool
  201. NextProtos []string
  202. OcspStapling bool
  203. Scts [][]byte
  204. Ems bool
  205. TicketSupported bool
  206. SecureRenegotiation []byte
  207. SecureRenegotiationSupported bool
  208. AlpnProtocol string
  209. // 1.3
  210. SupportedVersion uint16
  211. ServerShare keyShare
  212. SelectedIdentityPresent bool
  213. SelectedIdentity uint16
  214. Cookie []byte // HelloRetryRequest extension
  215. SelectedGroup CurveID // HelloRetryRequest extension
  216. }
  217. func (shm *ServerHelloMsg) getPrivatePtr() *serverHelloMsg {
  218. if shm == nil {
  219. return nil
  220. } else {
  221. return &serverHelloMsg{
  222. raw: shm.Raw,
  223. vers: shm.Vers,
  224. random: shm.Random,
  225. sessionId: shm.SessionId,
  226. cipherSuite: shm.CipherSuite,
  227. compressionMethod: shm.CompressionMethod,
  228. nextProtoNeg: shm.NextProtoNeg,
  229. nextProtos: shm.NextProtos,
  230. ocspStapling: shm.OcspStapling,
  231. scts: shm.Scts,
  232. ems: shm.Ems,
  233. ticketSupported: shm.TicketSupported,
  234. secureRenegotiation: shm.SecureRenegotiation,
  235. secureRenegotiationSupported: shm.SecureRenegotiationSupported,
  236. alpnProtocol: shm.AlpnProtocol,
  237. supportedVersion: shm.SupportedVersion,
  238. serverShare: shm.ServerShare,
  239. selectedIdentityPresent: shm.SelectedIdentityPresent,
  240. selectedIdentity: shm.SelectedIdentity,
  241. cookie: shm.Cookie,
  242. selectedGroup: shm.SelectedGroup,
  243. }
  244. }
  245. }
  246. func (shm *serverHelloMsg) getPublicPtr() *ServerHelloMsg {
  247. if shm == nil {
  248. return nil
  249. } else {
  250. return &ServerHelloMsg{
  251. Raw: shm.raw,
  252. Vers: shm.vers,
  253. Random: shm.random,
  254. SessionId: shm.sessionId,
  255. CipherSuite: shm.cipherSuite,
  256. CompressionMethod: shm.compressionMethod,
  257. NextProtoNeg: shm.nextProtoNeg,
  258. NextProtos: shm.nextProtos,
  259. OcspStapling: shm.ocspStapling,
  260. Scts: shm.scts,
  261. Ems: shm.ems,
  262. TicketSupported: shm.ticketSupported,
  263. SecureRenegotiation: shm.secureRenegotiation,
  264. SecureRenegotiationSupported: shm.secureRenegotiationSupported,
  265. AlpnProtocol: shm.alpnProtocol,
  266. SupportedVersion: shm.supportedVersion,
  267. ServerShare: shm.serverShare,
  268. SelectedIdentityPresent: shm.selectedIdentityPresent,
  269. SelectedIdentity: shm.selectedIdentity,
  270. Cookie: shm.cookie,
  271. SelectedGroup: shm.selectedGroup,
  272. }
  273. }
  274. }
  275. type ClientHelloMsg struct {
  276. Raw []byte
  277. Vers uint16
  278. Random []byte
  279. SessionId []byte
  280. CipherSuites []uint16
  281. CompressionMethods []uint8
  282. NextProtoNeg bool
  283. ServerName string
  284. OcspStapling bool
  285. Scts bool
  286. Ems bool // [UTLS] actually implemented due to its prevalence
  287. SupportedCurves []CurveID
  288. SupportedPoints []uint8
  289. TicketSupported bool
  290. SessionTicket []uint8
  291. SupportedSignatureAlgorithms []SignatureScheme
  292. SecureRenegotiation []byte
  293. SecureRenegotiationSupported bool
  294. AlpnProtocols []string
  295. // 1.3
  296. SupportedSignatureAlgorithmsCert []SignatureScheme
  297. SupportedVersions []uint16
  298. Cookie []byte
  299. KeyShares []KeyShare
  300. EarlyData bool
  301. PskModes []uint8
  302. PskIdentities []pskIdentity
  303. PskBinders [][]byte
  304. }
  305. func (chm *ClientHelloMsg) getPrivatePtr() *clientHelloMsg {
  306. if chm == nil {
  307. return nil
  308. } else {
  309. return &clientHelloMsg{
  310. raw: chm.Raw,
  311. vers: chm.Vers,
  312. random: chm.Random,
  313. sessionId: chm.SessionId,
  314. cipherSuites: chm.CipherSuites,
  315. compressionMethods: chm.CompressionMethods,
  316. nextProtoNeg: chm.NextProtoNeg,
  317. serverName: chm.ServerName,
  318. ocspStapling: chm.OcspStapling,
  319. scts: chm.Scts,
  320. ems: chm.Ems,
  321. supportedCurves: chm.SupportedCurves,
  322. supportedPoints: chm.SupportedPoints,
  323. ticketSupported: chm.TicketSupported,
  324. sessionTicket: chm.SessionTicket,
  325. supportedSignatureAlgorithms: chm.SupportedSignatureAlgorithms,
  326. secureRenegotiation: chm.SecureRenegotiation,
  327. secureRenegotiationSupported: chm.SecureRenegotiationSupported,
  328. alpnProtocols: chm.AlpnProtocols,
  329. supportedSignatureAlgorithmsCert: chm.SupportedSignatureAlgorithmsCert,
  330. supportedVersions: chm.SupportedVersions,
  331. cookie: chm.Cookie,
  332. keyShares: KeyShares(chm.KeyShares).ToPrivate(),
  333. earlyData: chm.EarlyData,
  334. pskModes: chm.PskModes,
  335. pskIdentities: chm.PskIdentities,
  336. pskBinders: chm.PskBinders,
  337. }
  338. }
  339. }
  340. func (chm *clientHelloMsg) getPublicPtr() *ClientHelloMsg {
  341. if chm == nil {
  342. return nil
  343. } else {
  344. return &ClientHelloMsg{
  345. Raw: chm.raw,
  346. Vers: chm.vers,
  347. Random: chm.random,
  348. SessionId: chm.sessionId,
  349. CipherSuites: chm.cipherSuites,
  350. CompressionMethods: chm.compressionMethods,
  351. NextProtoNeg: chm.nextProtoNeg,
  352. ServerName: chm.serverName,
  353. OcspStapling: chm.ocspStapling,
  354. Scts: chm.scts,
  355. Ems: chm.ems,
  356. SupportedCurves: chm.supportedCurves,
  357. SupportedPoints: chm.supportedPoints,
  358. TicketSupported: chm.ticketSupported,
  359. SessionTicket: chm.sessionTicket,
  360. SupportedSignatureAlgorithms: chm.supportedSignatureAlgorithms,
  361. SecureRenegotiation: chm.secureRenegotiation,
  362. SecureRenegotiationSupported: chm.secureRenegotiationSupported,
  363. AlpnProtocols: chm.alpnProtocols,
  364. SupportedSignatureAlgorithmsCert: chm.supportedSignatureAlgorithmsCert,
  365. SupportedVersions: chm.supportedVersions,
  366. Cookie: chm.cookie,
  367. KeyShares: keyShares(chm.keyShares).ToPublic(),
  368. EarlyData: chm.earlyData,
  369. PskModes: chm.pskModes,
  370. PskIdentities: chm.pskIdentities,
  371. PskBinders: chm.pskBinders,
  372. }
  373. }
  374. }
  375. // A CipherSuite is a specific combination of key agreement, cipher and MAC
  376. // function. All cipher suites currently assume RSA key agreement.
  377. type CipherSuite struct {
  378. Id uint16
  379. // the lengths, in bytes, of the key material needed for each component.
  380. KeyLen int
  381. MacLen int
  382. IvLen int
  383. Ka func(version uint16) keyAgreement
  384. // flags is a bitmask of the suite* values, above.
  385. Flags int
  386. Cipher func(key, iv []byte, isRead bool) interface{}
  387. Mac func(version uint16, macKey []byte) macFunction
  388. Aead func(key, fixedNonce []byte) aead
  389. }
  390. func (cs *CipherSuite) getPrivatePtr() *cipherSuite {
  391. if cs == nil {
  392. return nil
  393. } else {
  394. return &cipherSuite{
  395. id: cs.Id,
  396. keyLen: cs.KeyLen,
  397. macLen: cs.MacLen,
  398. ivLen: cs.IvLen,
  399. ka: cs.Ka,
  400. flags: cs.Flags,
  401. cipher: cs.Cipher,
  402. mac: cs.Mac,
  403. aead: cs.Aead,
  404. }
  405. }
  406. }
  407. func (cs *cipherSuite) getPublicPtr() *CipherSuite {
  408. if cs == nil {
  409. return nil
  410. } else {
  411. return &CipherSuite{
  412. Id: cs.id,
  413. KeyLen: cs.keyLen,
  414. MacLen: cs.macLen,
  415. IvLen: cs.ivLen,
  416. Ka: cs.ka,
  417. Flags: cs.flags,
  418. Cipher: cs.cipher,
  419. Mac: cs.mac,
  420. Aead: cs.aead,
  421. }
  422. }
  423. }
  424. // A FinishedHash calculates the hash of a set of handshake messages suitable
  425. // for including in a Finished message.
  426. type FinishedHash struct {
  427. Client hash.Hash
  428. Server hash.Hash
  429. // Prior to TLS 1.2, an additional MD5 hash is required.
  430. ClientMD5 hash.Hash
  431. ServerMD5 hash.Hash
  432. // In TLS 1.2, a full buffer is sadly required.
  433. Buffer []byte
  434. Version uint16
  435. Prf func(result, secret, label, seed []byte)
  436. }
  437. func (fh *FinishedHash) getPrivatePtr() *finishedHash {
  438. if fh == nil {
  439. return nil
  440. } else {
  441. return &finishedHash{
  442. client: fh.Client,
  443. server: fh.Server,
  444. clientMD5: fh.ClientMD5,
  445. serverMD5: fh.ServerMD5,
  446. buffer: fh.Buffer,
  447. version: fh.Version,
  448. prf: fh.Prf,
  449. }
  450. }
  451. }
  452. func (fh *finishedHash) getPublicPtr() *FinishedHash {
  453. if fh == nil {
  454. return nil
  455. } else {
  456. return &FinishedHash{
  457. Client: fh.client,
  458. Server: fh.server,
  459. ClientMD5: fh.clientMD5,
  460. ServerMD5: fh.serverMD5,
  461. Buffer: fh.buffer,
  462. Version: fh.version,
  463. Prf: fh.prf}
  464. }
  465. }
  466. // TLS 1.3 Key Share. See RFC 8446, Section 4.2.8.
  467. type KeyShare struct {
  468. Group CurveID
  469. Data []byte
  470. }
  471. type KeyShares []KeyShare
  472. type keyShares []keyShare
  473. func (kss keyShares) ToPublic() []KeyShare {
  474. var KSS []KeyShare
  475. for _, ks := range kss {
  476. KSS = append(KSS, KeyShare{Data: ks.data, Group: ks.group})
  477. }
  478. return KSS
  479. }
  480. func (KSS KeyShares) ToPrivate() []keyShare {
  481. var kss []keyShare
  482. for _, KS := range KSS {
  483. kss = append(kss, keyShare{data: KS.Data, group: KS.Group})
  484. }
  485. return kss
  486. }
  487. // ClientSessionState is public, but all its fields are private. Let's add setters, getters and constructor
  488. // ClientSessionState contains the state needed by clients to resume TLS sessions.
  489. func MakeClientSessionState(
  490. SessionTicket []uint8,
  491. Vers uint16,
  492. CipherSuite uint16,
  493. MasterSecret []byte,
  494. ServerCertificates []*x509.Certificate,
  495. VerifiedChains [][]*x509.Certificate) *ClientSessionState {
  496. css := ClientSessionState{sessionTicket: SessionTicket,
  497. vers: Vers,
  498. cipherSuite: CipherSuite,
  499. masterSecret: MasterSecret,
  500. serverCertificates: ServerCertificates,
  501. verifiedChains: VerifiedChains}
  502. return &css
  503. }
  504. // Encrypted ticket used for session resumption with server
  505. func (css *ClientSessionState) SessionTicket() []uint8 {
  506. return css.sessionTicket
  507. }
  508. // SSL/TLS version negotiated for the session
  509. func (css *ClientSessionState) Vers() uint16 {
  510. return css.vers
  511. }
  512. // Ciphersuite negotiated for the session
  513. func (css *ClientSessionState) CipherSuite() uint16 {
  514. return css.cipherSuite
  515. }
  516. // MasterSecret generated by client on a full handshake
  517. func (css *ClientSessionState) MasterSecret() []byte {
  518. return css.masterSecret
  519. }
  520. // Certificate chain presented by the server
  521. func (css *ClientSessionState) ServerCertificates() []*x509.Certificate {
  522. return css.serverCertificates
  523. }
  524. // Certificate chains we built for verification
  525. func (css *ClientSessionState) VerifiedChains() [][]*x509.Certificate {
  526. return css.verifiedChains
  527. }
  528. func (css *ClientSessionState) SetSessionTicket(SessionTicket []uint8) {
  529. css.sessionTicket = SessionTicket
  530. }
  531. func (css *ClientSessionState) SetVers(Vers uint16) {
  532. css.vers = Vers
  533. }
  534. func (css *ClientSessionState) SetCipherSuite(CipherSuite uint16) {
  535. css.cipherSuite = CipherSuite
  536. }
  537. func (css *ClientSessionState) SetMasterSecret(MasterSecret []byte) {
  538. css.masterSecret = MasterSecret
  539. }
  540. func (css *ClientSessionState) SetServerCertificates(ServerCertificates []*x509.Certificate) {
  541. css.serverCertificates = ServerCertificates
  542. }
  543. func (css *ClientSessionState) SetVerifiedChains(VerifiedChains [][]*x509.Certificate) {
  544. css.verifiedChains = VerifiedChains
  545. }