session.go 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182
  1. /*
  2. * Copyright (c) 2023, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package inproxy
  20. import (
  21. "bytes"
  22. "context"
  23. "crypto/ed25519"
  24. "crypto/rand"
  25. "crypto/sha256"
  26. "crypto/sha512"
  27. "encoding/base64"
  28. "math"
  29. "sync"
  30. "time"
  31. "filippo.io/edwards25519"
  32. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  33. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/prng"
  34. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
  35. lrucache "github.com/cognusion/go-cache-lru"
  36. "github.com/flynn/noise"
  37. "github.com/marusama/semaphore"
  38. "golang.org/x/crypto/curve25519"
  39. "golang.zx2c4.com/wireguard/replay"
  40. )
  41. const (
  42. sessionsTTL = 24 * time.Hour
  43. sessionsMaxSize = 1000000
  44. sessionObfuscationPaddingMinSize = 0
  45. sessionObfuscationPaddingMaxSize = 256
  46. resetSessionTokenName = "psiphon-inproxy-session-reset-session-token"
  47. resetSessionTokenNonceSize = 32
  48. maxResponderConcurrentNewSessions = 32768
  49. )
  50. const (
  51. SessionProtocolName = "psiphon-inproxy-session"
  52. SessionProtocolVersion1 = 1
  53. )
  54. // SessionPrologue is a Noise protocol prologue, which binds the session ID to
  55. // the session.
  56. type SessionPrologue struct {
  57. SessionProtocolName string `cbor:"1,keyasint,omitempty"`
  58. SessionProtocolVersion uint32 `cbor:"2,keyasint,omitempty"`
  59. SessionID ID `cbor:"3,keyasint,omitempty"`
  60. }
  61. // SessionPacket is a Noise protocol message, which may be a session handshake
  62. // message, or secured application data, a SessionRoundTrip.
  63. type SessionPacket struct {
  64. SessionID ID `cbor:"1,keyasint,omitempty"`
  65. Nonce uint64 `cbor:"2,keyasint,omitempty"`
  66. Payload []byte `cbor:"3,keyasint,omitempty"`
  67. ResetSessionToken []byte `cbor:"4,keyasint,omitempty"`
  68. }
  69. // SessionRoundTrip is an application data request or response, which is
  70. // secured by the Noise protocol session. Each request is assigned a unique
  71. // RoundTripID, and each corresponding response has the same RoundTripID.
  72. type SessionRoundTrip struct {
  73. RoundTripID ID `cbor:"1,keyasint,omitempty"`
  74. Payload []byte `cbor:"2,keyasint,omitempty"`
  75. }
  76. // SessionPrivateKey is a Noise protocol private key.
  77. type SessionPrivateKey [ed25519.PrivateKeySize]byte
  78. // GenerateSessionPrivateKey creates a new session private key using
  79. // crypto/rand.
  80. //
  81. // GenerateSessionPrivateKey generates an Ed25519 private key, which is used
  82. // directly for digital signatures and, when converted to Curve25519, as the
  83. // Noise protocol ECDH private key.
  84. //
  85. // The Ed25519 representation is the canonical representation since there's a
  86. // 1:1 conversion from Ed25519 to Curve25519, but not the other way.
  87. //
  88. // Digital signing use cases include signing a reset session token. In
  89. // addition, externally, digital signing can be used in a challenge/response
  90. // protocol that demonstrates ownership of a proxy private key corresponding
  91. // to a claimed proxy public key.
  92. func GenerateSessionPrivateKey() (SessionPrivateKey, error) {
  93. var k SessionPrivateKey
  94. _, privateKey, err := ed25519.GenerateKey(rand.Reader)
  95. if err != nil {
  96. return k, errors.Trace(err)
  97. }
  98. if len(privateKey) != len(k) {
  99. return k, errors.TraceNew("unexpected private key length")
  100. }
  101. copy(k[:], privateKey)
  102. return k, nil
  103. }
  104. // SessionPrivateKeyFromString returns a SessionPrivateKey given its base64
  105. // string encoding.
  106. func SessionPrivateKeyFromString(s string) (SessionPrivateKey, error) {
  107. var k SessionPrivateKey
  108. return k, errors.Trace(fromBase64String(s, k[:]))
  109. }
  110. // String emits SessionPrivateKey as base64.
  111. func (k SessionPrivateKey) String() string {
  112. return base64.RawStdEncoding.EncodeToString([]byte(k[:]))
  113. }
  114. // IsZero indicates if the private key is zero-value.
  115. func (k SessionPrivateKey) IsZero() bool {
  116. var zero SessionPrivateKey
  117. return bytes.Equal(k[:], zero[:])
  118. }
  119. // GetPublicKey returns the public key corresponding to the private key.
  120. func (k SessionPrivateKey) GetPublicKey() (SessionPublicKey, error) {
  121. var sessionPublicKey SessionPublicKey
  122. // See ed25519.PrivateKey.Public.
  123. copy(sessionPublicKey[:], k[32:])
  124. return sessionPublicKey, nil
  125. }
  126. // ToCurve25519 converts the Ed25519 SessionPrivateKey to the unique
  127. // corresponding Curve25519 private key for use in the Noise protocol.
  128. func (k SessionPrivateKey) ToCurve25519() []byte {
  129. h := sha512.New()
  130. h.Write(ed25519.PrivateKey(k[:]).Seed())
  131. return h.Sum(nil)[:curve25519.ScalarSize]
  132. }
  133. // SessionPublicKey is a Noise protocol public key.
  134. type SessionPublicKey [ed25519.PublicKeySize]byte
  135. // SessionPublicKeyFromString returns a SessionPublicKey given its base64
  136. // string encoding.
  137. func SessionPublicKeyFromString(s string) (SessionPublicKey, error) {
  138. var k SessionPublicKey
  139. return k, errors.Trace(fromBase64String(s, k[:]))
  140. }
  141. // SessionPublicKeysFromStrings returns a list of SessionPublicKeys given the
  142. // base64 string encodings.
  143. func SessionPublicKeysFromStrings(strs []string) ([]SessionPublicKey, error) {
  144. keys := make([]SessionPublicKey, len(strs))
  145. for i, s := range strs {
  146. err := fromBase64String(s, keys[i][:])
  147. if err != nil {
  148. return nil, errors.Trace(err)
  149. }
  150. }
  151. return keys, nil
  152. }
  153. // String emits SessionPublicKey as base64.
  154. func (k SessionPublicKey) String() string {
  155. return base64.RawStdEncoding.EncodeToString([]byte(k[:]))
  156. }
  157. // ToCurve25519 converts the Ed25519 SessionPublicKey to the unique
  158. // corresponding Curve25519 public key for use in the Noise protocol.
  159. func (k SessionPublicKey) ToCurve25519() (SessionPublicKeyCurve25519, error) {
  160. var c SessionPublicKeyCurve25519
  161. // Copyright 2019 The age Authors. All rights reserved.
  162. // Use of this source code is governed by a BSD-style
  163. // license that can be found in the LICENSE file.
  164. //
  165. // See https://blog.filippo.io/using-ed25519-keys-for-encryption and
  166. // https://pkg.go.dev/filippo.io/edwards25519#Point.BytesMontgomery.
  167. p, err := new(edwards25519.Point).SetBytes(k[:])
  168. if err != nil {
  169. return c, err
  170. }
  171. copy(c[:], p.BytesMontgomery())
  172. return c, nil
  173. }
  174. // SessionPublicKeyCurve25519 is a representation of a Curve25519 public key
  175. // as a fixed-size array that may be used as a map key.
  176. type SessionPublicKeyCurve25519 [curve25519.PointSize]byte
  177. // String emits SessionPublicKeyCurve25519 as base64.
  178. func (k SessionPublicKeyCurve25519) String() string {
  179. return base64.RawStdEncoding.EncodeToString([]byte(k[:]))
  180. }
  181. // InitiatorSessions is a set of secure Noise protocol sessions for an
  182. // initiator. For in-proxy, clients and proxies will initiate sessions with
  183. // one more brokers and brokers will initiate sessions with multiple Psiphon
  184. // servers.
  185. //
  186. // Secure sessions provide encryption, authentication of the responder,
  187. // identity hiding for the initiator, forward secrecy, and anti-replay for
  188. // application data.
  189. //
  190. // Maintaining a set of established sessions minimizes round trips and
  191. // overhead, as established sessions can be shared and reused for many client
  192. // requests to one broker or many broker requests to one server.
  193. //
  194. // Currently, InitiatorSessions doesn't not cap the number of sessions or use
  195. // an LRU cache since the number of peers is bounded in the in-proxy
  196. // architecture; clients will typically use one or no more than a handful of
  197. // brokers and brokers will exchange requests with a subset of Psiphon
  198. // servers bounded by the in-proxy capability.
  199. //
  200. // InitiatorSessions are used via the RoundTrip function or InitiatorRoundTrip
  201. // type. RoundTrip is a synchronous function which performs any necessary
  202. // session establishment handshake along with the request/response exchange.
  203. // InitiatorRoundTrip offers an iterator interface, with stepwise invocations
  204. // for each step of the handshake and round trip.
  205. //
  206. // All round trips attempt to share and reuse any existing, established
  207. // session to a given peer. For a given peer, the waitToShareSession option
  208. // determines whether round trips will block and wait if a session handshake
  209. // is already in progress, or proceed with a concurrent handshake. For
  210. // in-proxy, clients and proxies use waitToShareSession; as broker/server
  211. // round trips are relayed through clients, brokers do not use
  212. // waitToShareSession so as to not rely on any single client.
  213. //
  214. // Round trips can be performed concurrently and requests can arrive out-of-
  215. // order. The higher level transport for sessions is responsible for
  216. // multiplexing round trips and maintaining the association between a request
  217. // and it's corresponding response.
  218. type InitiatorSessions struct {
  219. privateKey SessionPrivateKey
  220. mutex sync.Mutex
  221. sessions map[SessionPublicKey]*session
  222. }
  223. // NewInitiatorSessions creates a new InitiatorSessions with the specified
  224. // initator private key.
  225. func NewInitiatorSessions(
  226. initiatorPrivateKey SessionPrivateKey) *InitiatorSessions {
  227. return &InitiatorSessions{
  228. privateKey: initiatorPrivateKey,
  229. sessions: make(map[SessionPublicKey]*session),
  230. }
  231. }
  232. // RoundTrip sends the request to the specified responder and returns the
  233. // response.
  234. //
  235. // RoundTrip will establish a session when required, or reuse an existing
  236. // session when available.
  237. //
  238. // When waitToShareSession is true, RoundTrip will block until an existing,
  239. // non-established session is available to be shared.
  240. //
  241. // When making initial network round trips to establish a session,
  242. // sessionHandshakeTimeout is applied as the round trip timeout.
  243. //
  244. // When making the application-level request round trip, requestDelay, when >
  245. // 0, is applied before the request network round trip begins; requestDelay
  246. // may be used to spread out many concurrent requests, such as batch proxy
  247. // announcements, to avoid CDN rate limits.
  248. //
  249. // requestTimeout is applied to the application-level request network round
  250. // trip, and excludes any requestDelay; the distinct requestTimeout may be
  251. // used to set a longer timeout for long-polling requests, such as proxy
  252. // announcements.
  253. //
  254. // Any time spent blocking on waitToShareSession is not included in
  255. // requestDelay or requestTimeout.
  256. //
  257. // RoundTrip returns immediately when ctx becomes done.
  258. func (s *InitiatorSessions) RoundTrip(
  259. ctx context.Context,
  260. roundTripper RoundTripper,
  261. responderPublicKey SessionPublicKey,
  262. responderRootObfuscationSecret ObfuscationSecret,
  263. waitToShareSession bool,
  264. sessionHandshakeTimeout time.Duration,
  265. requestDelay time.Duration,
  266. requestTimeout time.Duration,
  267. request []byte) ([]byte, error) {
  268. rt, err := s.NewRoundTrip(
  269. responderPublicKey,
  270. responderRootObfuscationSecret,
  271. waitToShareSession,
  272. request)
  273. if err != nil {
  274. return nil, errors.Trace(err)
  275. }
  276. var in []byte
  277. for {
  278. out, isRequestPacket, err := rt.Next(ctx, in)
  279. if err != nil {
  280. return nil, errors.Trace(err)
  281. }
  282. if out == nil {
  283. response, err := rt.Response()
  284. if err != nil {
  285. return nil, errors.Trace(err)
  286. }
  287. return response, nil
  288. }
  289. // At this point, if sharing a session, any blocking on
  290. // waitToShareSession is complete, and time elapsed in that blocking
  291. // will not collapse delays or reduce timeouts. If not sharing, and
  292. // establishing a new session, Noise session handshake round trips
  293. // are required before the request payload round trip.
  294. //
  295. // Select the delay and timeout. For Noise session handshake round
  296. // trips, use sessionHandshakeTimeout, which should be appropriate
  297. // for a fast turn-around from the broker, and no delay. When sending
  298. // the application-level request packet, use requestDelay and
  299. // requestTimeout, which allows for applying a delay -- to spread out
  300. // requests -- and a potentially longer timeout appropriate for a
  301. // long-polling, slower turn-around from the broker.
  302. //
  303. // Delays and timeouts are passed down into the round tripper
  304. // provider. Having the round tripper perform the delay sleep allows
  305. // all delays to be interruped by any round tripper close, due to an
  306. // overall broker client reset. Passing the timeout seperately, as
  307. // opposed to adding to ctx, explicitly ensures that the timeout is
  308. // applied only right before the network round trip and no sooner.
  309. var delay, timeout time.Duration
  310. if isRequestPacket {
  311. delay = requestDelay
  312. timeout = requestTimeout
  313. } else {
  314. // No delay for session handshake packet round trips.
  315. timeout = sessionHandshakeTimeout
  316. }
  317. in, err = roundTripper.RoundTrip(ctx, delay, timeout, out)
  318. if err != nil {
  319. // There are no explicit retries here. Retrying in the case where
  320. // the initiator attempts to use an expired session is covered by
  321. // the reset session token logic in InitiatorRoundTrip. Higher
  322. // levels implicitly provide additional retries to cover other
  323. // cases; Psiphon client tunnel establishment will retry in-proxy
  324. // dials; the proxy will retry its announce requests if they
  325. // fail.
  326. // If this round trip owns its session and there are any
  327. // waitToShareSession initiators awaiting the session, signal them
  328. // that the session will not become ready.
  329. rt.TransportFailed()
  330. return nil, errors.Trace(err)
  331. }
  332. }
  333. }
  334. // NewRoundTrip creates a new InitiatorRoundTrip which will perform a
  335. // request/response round trip with the specified responder, sending the
  336. // input request. The InitiatorRoundTrip will establish a session when
  337. // required, or reuse an existing session when available.
  338. //
  339. // When waitToShareSession is true, InitiatorRoundTrip.Next will block until
  340. // an existing, non-established session is available to be shared.
  341. //
  342. // Limitation with waitToShareSession: currently, any new session must
  343. // complete an _application-level_ round trip (e.g., ProxyAnnounce/ClientOffer
  344. // request _and_ response) before the session becomes ready to share since
  345. // the first application-level request is sent in the same packet as the last
  346. // handshake message and ready-to-share is only signalled after a subsequent
  347. // packet is received. This means that, for example, a long-polling
  348. // ProxyAnnounce will block any additional ProxyAnnounce requests attempting
  349. // to share the same InitiatorSessions. In practice, an initial
  350. // ProxyAnnounce/ClientOffer request is expected to block only as long as
  351. // there is no match, so the impact of blocking other concurrent requests is
  352. // limited. See comment in InitiatorRoundTrip.Next for a related future
  353. // enhancement.
  354. //
  355. // NewRoundTrip does not block or perform any session operations; the
  356. // operations begin on the first InitiatorRoundTrip.Next call. The content of
  357. // request should not be modified after calling NewRoundTrip.
  358. func (s *InitiatorSessions) NewRoundTrip(
  359. responderPublicKey SessionPublicKey,
  360. responderRootObfuscationSecret ObfuscationSecret,
  361. waitToShareSession bool,
  362. request []byte) (*InitiatorRoundTrip, error) {
  363. s.mutex.Lock()
  364. defer s.mutex.Unlock()
  365. // Generate a new round trip ID for the session round trip. The response
  366. // is expected to echo back the same round trip ID. This check detects
  367. // any potential misrouting of multiplexed round trip exchanges.
  368. roundTripID, err := MakeID()
  369. if err != nil {
  370. return nil, errors.Trace(err)
  371. }
  372. requestPayload, err := marshalRecord(
  373. SessionRoundTrip{RoundTripID: roundTripID, Payload: request},
  374. recordTypeSessionRoundTrip)
  375. if err != nil {
  376. return nil, errors.Trace(err)
  377. }
  378. return &InitiatorRoundTrip{
  379. initiatorSessions: s,
  380. responderPublicKey: responderPublicKey,
  381. responderRootObfuscationSecret: responderRootObfuscationSecret,
  382. waitToShareSession: waitToShareSession,
  383. roundTripID: roundTripID,
  384. requestPayload: requestPayload,
  385. }, nil
  386. }
  387. // getSession looks for an existing session for the peer specified by public
  388. // key. When none is found, newSession is called to create a new session, and
  389. // this is stored, associated with the key. If an existing session is found,
  390. // indicate if it is ready to be shared or not.
  391. func (s *InitiatorSessions) getSession(
  392. publicKey SessionPublicKey,
  393. newSession func() (*session, error)) (
  394. retSession *session, retIsNew bool, retIsReady bool, retErr error) {
  395. s.mutex.Lock()
  396. defer s.mutex.Unlock()
  397. // Note: unlike in ResponderSessions.getSession, there is no indication,
  398. // in profiling, of high lock contention and blocking here when holding
  399. // the mutex lock while calling newSession. The lock is left in place to
  400. // preserve the semantics of only one concurrent newSession call,
  401. // particularly for brokers initiating new sessions with servers.
  402. session, ok := s.sessions[publicKey]
  403. if ok {
  404. return session, false, session.isReadyToShare(nil), nil
  405. }
  406. session, err := newSession()
  407. if err != nil {
  408. return nil, false, false, errors.Trace(err)
  409. }
  410. s.sessions[publicKey] = session
  411. return session, true, session.isReadyToShare(nil), nil
  412. }
  413. // setSession sets the session associated with the peer's public key.
  414. func (s *InitiatorSessions) setSession(publicKey SessionPublicKey, session *session) {
  415. s.mutex.Lock()
  416. defer s.mutex.Unlock()
  417. s.sessions[publicKey] = session
  418. }
  419. // removeIfSession removes the session associated with the peer's public key,
  420. // if it's the specified session.
  421. func (s *InitiatorSessions) removeIfSession(publicKey SessionPublicKey, session *session) {
  422. s.mutex.Lock()
  423. defer s.mutex.Unlock()
  424. currentSession, ok := s.sessions[publicKey]
  425. if !ok || session != currentSession {
  426. return
  427. }
  428. delete(s.sessions, publicKey)
  429. }
  430. // InitiatorRoundTrip represents the state of a session round trip, including
  431. // a session handshake if required. The session handshake and round trip is
  432. // advanced by calling InitiatorRoundTrip.Next.
  433. type InitiatorRoundTrip struct {
  434. initiatorSessions *InitiatorSessions
  435. responderPublicKey SessionPublicKey
  436. responderRootObfuscationSecret ObfuscationSecret
  437. waitToShareSession bool
  438. roundTripID ID
  439. requestPayload []byte
  440. mutex sync.Mutex
  441. sharingSession bool
  442. session *session
  443. lastSentPacket bytes.Buffer
  444. response []byte
  445. }
  446. // Next advances a round trip, as well as any session handshake that may be
  447. // first required. Next takes the next packet received from the responder and
  448. // returns the next packet to send to the responder. To begin, pass a nil
  449. // receivedPacket. The round trip is complete when Next returns nil for the
  450. // next packet to send; the response can be fetched from
  451. // InitiatorRoundTrip.Response.
  452. //
  453. // When waitToShareSession is set, Next will block until an existing,
  454. // non-established session is available to be shared.
  455. //
  456. // Multiple concurrent round trips are supported and requests from different
  457. // round trips can arrive at the responder out-of-order. The provided
  458. // transport is responsible for multiplexing round trips and maintaining an
  459. // association between sent and received packets for a given round trip.
  460. //
  461. // Next returns immediately when ctx becomes done.
  462. func (r *InitiatorRoundTrip) Next(
  463. ctx context.Context,
  464. receivedPacket []byte) (retSendPacket []byte, retIsRequestPacket bool, retErr error) {
  465. // Note: don't clear or reset a session in the event of a bad/rejected
  466. // packet as that would allow a malicious relay client to interrupt a
  467. // valid broker/server session with a malformed packet. Just drop the
  468. // packet and return an error.
  469. // beginOrShareSession returns the next packet to send.
  470. beginOrShareSession := func() ([]byte, bool, error) {
  471. // Check for an existing session, or create a new one if there's no
  472. // existing session.
  473. //
  474. // To ensure the concurrent waitToShareSession cases don't start
  475. // multiple handshakes, getSession populates the initiatorSessions
  476. // session map with a new, unestablished session.
  477. newSession := func() (*session, error) {
  478. sendObfuscationSecret, receiveObfuscationSecret, err :=
  479. deriveSessionPacketObfuscationSecrets(r.responderRootObfuscationSecret, false)
  480. if err != nil {
  481. return nil, errors.Trace(err)
  482. }
  483. session, err := newSession(
  484. true, // isInitiator
  485. r.initiatorSessions.privateKey,
  486. sendObfuscationSecret,
  487. receiveObfuscationSecret,
  488. nil, // No obfuscation replay history
  489. &r.responderPublicKey,
  490. r.requestPayload,
  491. nil,
  492. nil)
  493. if err != nil {
  494. return nil, errors.Trace(err)
  495. }
  496. return session, nil
  497. }
  498. session, isNew, isReady, err := r.initiatorSessions.getSession(
  499. r.responderPublicKey, newSession)
  500. if err != nil {
  501. return nil, false, errors.Trace(err)
  502. }
  503. if isNew {
  504. // When isNew is true, this InitiatorRoundTrip owns the session
  505. // and will perform the handshake.
  506. r.session = session
  507. r.sharingSession = false
  508. } else {
  509. if isReady {
  510. // When isReady is true, this shared session is fully
  511. // established and ready for immediate use.
  512. r.session = session
  513. r.sharingSession = true
  514. } else {
  515. // The existing session is not yet ready for use.
  516. if r.waitToShareSession {
  517. // Wait for the owning InitiatorRoundTrip to complete the
  518. // session handshake and then share the session.
  519. // Limitation with waitToShareSession: isReadyToShare
  520. // becomes true only once the session completes
  521. // an _application-level_ round trip
  522. // (e.g., ProxyAnnounce/ClientOffer request _and_
  523. // response) since the first application-level request is
  524. // bundled with the last handshake message and
  525. // ready-to-share is true only after a subsequent packet
  526. // is received, guaranteeing that the handshake is completed.
  527. //
  528. // Future enhancement: for shared sessions, don't bundle
  529. // the request payload with the handshake. This implies
  530. // one extra round trip for the initial requester, but
  531. // allows all sharers to proceed at once.
  532. signal := make(chan struct{})
  533. if !session.isReadyToShare(signal) {
  534. select {
  535. case <-signal:
  536. if !session.isReadyToShare(nil) {
  537. // The session failed to become ready to share due to a transport
  538. // failure during the handshake. Fail this round trip. Don't
  539. // create a new, unshared session since waitToShareSession was
  540. // specified. It's expected that there will be retries by the
  541. // RoundTrip caller.
  542. return nil, false, errors.TraceNew("waitToShareSession failed")
  543. }
  544. // else, use the session
  545. case <-ctx.Done():
  546. return nil, false, errors.Trace(ctx.Err())
  547. }
  548. }
  549. r.session = session
  550. r.sharingSession = true
  551. } else {
  552. // Don't wait: create a new, unshared session.
  553. r.session, err = newSession()
  554. if err != nil {
  555. return nil, false, errors.Trace(err)
  556. }
  557. r.sharingSession = false
  558. }
  559. }
  560. }
  561. if r.sharingSession {
  562. // The shared session was either ready for immediate use, or we
  563. // waited. Send the round trip request payload.
  564. sendPacket, err := r.session.sendPacket(r.requestPayload)
  565. if err != nil {
  566. return nil, false, errors.Trace(err)
  567. }
  568. return sendPacket, true, nil
  569. }
  570. // Begin the handshake for a new session.
  571. _, sendPacket, _, err := r.session.nextHandshakePacket(nil)
  572. if err != nil {
  573. return nil, false, errors.Trace(err)
  574. }
  575. return sendPacket, false, nil
  576. }
  577. // Return immediately if the context is already done.
  578. if ctx != nil {
  579. err := ctx.Err()
  580. if err != nil {
  581. return nil, false, errors.Trace(err)
  582. }
  583. }
  584. r.mutex.Lock()
  585. defer r.mutex.Unlock()
  586. // Store the output send packet, which is used to verify that any
  587. // subsequent ResetSessionToken isn't replayed.
  588. defer func() {
  589. if retSendPacket != nil {
  590. r.lastSentPacket.Reset()
  591. r.lastSentPacket.Write(retSendPacket)
  592. }
  593. }()
  594. if r.session == nil {
  595. // If the session is nil, this is the first call to Next, and no
  596. // packet from the peer is expected.
  597. if receivedPacket != nil {
  598. return nil, false, errors.TraceNew("unexpected received packet")
  599. }
  600. sendPacket, isRequestPacket, err := beginOrShareSession()
  601. if err != nil {
  602. return nil, false, errors.Trace(err)
  603. }
  604. return sendPacket, isRequestPacket, nil
  605. }
  606. // Not the first Next call, so a packet from the peer is expected.
  607. if receivedPacket == nil {
  608. return nil, false, errors.TraceNew("missing received packet")
  609. }
  610. if r.sharingSession || r.session.isEstablished() {
  611. // When sharing an established and ready session, or once an owned
  612. // session is established, the next packet is post-handshake and
  613. // should be the round trip request response.
  614. // Pre-unwrap here to check for a ResetSessionToken packet.
  615. sessionPacket, err := unwrapSessionPacket(
  616. r.session.receiveObfuscationSecret, true, nil, receivedPacket)
  617. if err != nil {
  618. return nil, false, errors.Trace(err)
  619. }
  620. // Reset the session when the packet is a valid ResetSessionToken. The
  621. // responder sends a ResetSessionToken when this initiator attempts
  622. // to use an expired session. A ResetSessionToken is valid when it's
  623. // signed by the responder's public key and is bound to the last
  624. // packet sent from this initiator (which protects against replay).
  625. if sessionPacket.ResetSessionToken != nil &&
  626. isValidResetSessionToken(
  627. r.responderPublicKey,
  628. r.lastSentPacket.Bytes(),
  629. sessionPacket.ResetSessionToken) {
  630. // removeIfSession won't clobber any other, concurrently
  631. // established session for the same responder.
  632. r.initiatorSessions.removeIfSession(r.responderPublicKey, r.session)
  633. r.session = nil
  634. sendPacket, isRequestPacket, err := beginOrShareSession()
  635. if err != nil {
  636. return nil, false, errors.Trace(err)
  637. }
  638. return sendPacket, isRequestPacket, nil
  639. }
  640. responsePayload, err := r.session.receiveUnmarshaledPacket(sessionPacket)
  641. if err != nil {
  642. return nil, false, errors.Trace(err)
  643. }
  644. var sessionRoundTrip SessionRoundTrip
  645. err = unmarshalRecord(recordTypeSessionRoundTrip, responsePayload, &sessionRoundTrip)
  646. if err != nil {
  647. return nil, false, errors.Trace(err)
  648. }
  649. // Check that the response RoundTripID matches the request RoundTripID.
  650. if sessionRoundTrip.RoundTripID != r.roundTripID {
  651. return nil, false, errors.TraceNew("unexpected round trip ID")
  652. }
  653. // Store the response so it can be retrieved later.
  654. r.response = sessionRoundTrip.Payload
  655. return nil, false, nil
  656. }
  657. // Continue the handshake. Since the first payload is sent to the
  658. // responder along with the initiator's last handshake message, there's
  659. // no sendPacket call in the owned session case. The last
  660. // nextHandshakePacket will bundle it. Also, the payload output of
  661. // nextHandshakePacket is ignored, as only a responder will receive a
  662. // payload in a handshake message.
  663. isEstablished, sendPacket, _, err := r.session.nextHandshakePacket(receivedPacket)
  664. if err != nil {
  665. return nil, false, errors.Trace(err)
  666. }
  667. if isEstablished {
  668. // Retain the most recently established session as the cached session
  669. // for reuse. This should be a no-op in the isNew case and only have
  670. // an effect for !inNew and !waitToShareSession. Modifying the
  671. // initiatorSessions map entry should not impact any concurrent
  672. // handshakes, as each InitiatorRoundTrip maintains its own reference
  673. // to its session.
  674. r.initiatorSessions.setSession(r.responderPublicKey, r.session)
  675. }
  676. return sendPacket, isEstablished, nil
  677. }
  678. // TransportFailed marks any owned, not yet ready-to-share session as failed
  679. // and signals any other initiators waiting to share the session.
  680. //
  681. // TransportFailed should be called when using waitToShareSession and when
  682. // there is a transport level failure to relay a session packet.
  683. func (r *InitiatorRoundTrip) TransportFailed() {
  684. r.mutex.Lock()
  685. defer r.mutex.Unlock()
  686. if !r.sharingSession && !r.session.isReadyToShare(nil) {
  687. r.session.transportFailed()
  688. r.initiatorSessions.removeIfSession(r.responderPublicKey, r.session)
  689. }
  690. }
  691. // Response returns the round trip response. Call Response after Next returns
  692. // nil for the next packet to send, indicating that the round trip is
  693. // complete.
  694. func (r *InitiatorRoundTrip) Response() ([]byte, error) {
  695. r.mutex.Lock()
  696. defer r.mutex.Unlock()
  697. if r.response == nil {
  698. return nil, errors.TraceNew("no response")
  699. }
  700. return r.response, nil
  701. }
  702. // ResponderSessions is a set of secure Noise protocol sessions for a
  703. // responder. For in-proxy, brokers respond to clients and proxies and
  704. // servers respond to brokers.
  705. //
  706. // Secure sessions provide encryption, authentication of the responder,
  707. // identity hiding for the initiator, forward secrecy, and anti-replay for
  708. // application data.
  709. //
  710. // ResponderSessions maintains a cache of established sessions to minimizes
  711. // round trips and overhead as initiators are expected to make multiple round
  712. // trips. The cache has a TTL and maximum size with LRU to cap overall memory
  713. // usage. A broker may receive requests from millions of clients and proxies
  714. // and so only more recent sessions will be retained. Servers will receive
  715. // requests from only a handful of brokers, and so the TTL is not applied.
  716. //
  717. // Multiple, concurrent sessions for a single initiator public key are
  718. // supported.
  719. type ResponderSessions struct {
  720. privateKey SessionPrivateKey
  721. sendObfuscationSecret ObfuscationSecret
  722. receiveObfuscationSecret ObfuscationSecret
  723. applyTTL bool
  724. obfuscationReplayHistory *obfuscationReplayHistory
  725. expectedInitiatorPublicKeys *sessionPublicKeyLookup
  726. mutex sync.RWMutex
  727. sessions *lrucache.Cache
  728. concurrentNewSessions semaphore.Semaphore
  729. }
  730. // NewResponderSessions creates a new ResponderSessions which allows any
  731. // initiators to establish a session. A TTL is applied to cached sessions.
  732. func NewResponderSessions(
  733. responderPrivateKey SessionPrivateKey,
  734. responderRootObfuscationSecret ObfuscationSecret) (*ResponderSessions, error) {
  735. sendObfuscationSecret, receiveObfuscationSecret, err :=
  736. deriveSessionPacketObfuscationSecrets(responderRootObfuscationSecret, true)
  737. if err != nil {
  738. return nil, errors.Trace(err)
  739. }
  740. return &ResponderSessions{
  741. privateKey: responderPrivateKey,
  742. sendObfuscationSecret: sendObfuscationSecret,
  743. receiveObfuscationSecret: receiveObfuscationSecret,
  744. applyTTL: true,
  745. obfuscationReplayHistory: newObfuscationReplayHistory(),
  746. sessions: lrucache.NewWithLRU(sessionsTTL, 1*time.Minute, sessionsMaxSize),
  747. concurrentNewSessions: semaphore.New(maxResponderConcurrentNewSessions),
  748. }, nil
  749. }
  750. // NewResponderSessionsForKnownInitiators creates a new ResponderSessions
  751. // which allows only allow-listed initiators to establish a session. No TTL
  752. // is applied to cached sessions.
  753. //
  754. // The NewResponderSessionsForKnownInitiators configuration is for Psiphon
  755. // servers responding to brokers. Only a handful of brokers are expected to
  756. // be deployed. A relatively small allow list of expected broker public keys
  757. // is easy to manage, deploy, and update. No TTL is applied to keep the
  758. // sessions established as much as possible and avoid extra client-relayed
  759. // round trips for BrokerServerRequests.
  760. func NewResponderSessionsForKnownInitiators(
  761. responderPrivateKey SessionPrivateKey,
  762. responderRootObfuscationKey ObfuscationSecret,
  763. initiatorPublicKeys []SessionPublicKey) (*ResponderSessions, error) {
  764. s, err := NewResponderSessions(responderPrivateKey, responderRootObfuscationKey)
  765. if err != nil {
  766. return nil, errors.Trace(err)
  767. }
  768. s.applyTTL = false
  769. s.expectedInitiatorPublicKeys, err = newSessionPublicKeyLookup(initiatorPublicKeys)
  770. if err != nil {
  771. return nil, errors.Trace(err)
  772. }
  773. return s, nil
  774. }
  775. // SetKnownInitiatorPublicKeys updates the set of initiator public keys which
  776. // are allowed to establish sessions with the responder. Any existing
  777. // sessions with keys not in the new list are deleted. Existing sessions with
  778. // keys which remain in the list are retained.
  779. func (s *ResponderSessions) SetKnownInitiatorPublicKeys(
  780. initiatorPublicKeys []SessionPublicKey) error {
  781. s.mutex.Lock()
  782. defer s.mutex.Unlock()
  783. changed, err := s.expectedInitiatorPublicKeys.set(initiatorPublicKeys)
  784. if err != nil {
  785. return errors.Trace(err)
  786. }
  787. if !changed {
  788. // With an identical public key set there are no sessions to be reset
  789. return nil
  790. }
  791. // Delete sessions for removed keys; retain established sessions for
  792. // still-valid keys.
  793. //
  794. // Limitations:
  795. // - Doesn't interrupt a concurrent request in progress which has already
  796. // called getSession
  797. // - lrucache doesn't have iterator; Items creates a full copy of the
  798. // cache state
  799. for sessionIDStr, entry := range s.sessions.Items() {
  800. // Each session.hasUnexpectedInitiatorPublicKey indirectly references
  801. // s.expectedInitiatorPublicKeys, which was updated above with the
  802. // new set of valid public keys.
  803. if entry.Object.(*session).hasUnexpectedInitiatorPublicKey() {
  804. s.sessions.Delete(sessionIDStr)
  805. }
  806. }
  807. return nil
  808. }
  809. // GetEstablishedKnownInitiatorIDs returns a list of known initiator IDs, the
  810. // Curve21559 equivalents of known initiator public keys, with currently
  811. // established sessions.
  812. //
  813. // The return value is a map that may be used for lookups, supporting the
  814. // ProxyQualityReporter use case of sending server proxy quality requests
  815. // only to brokers that are expected to already trust the server's session
  816. // public key.
  817. //
  818. // GetEstablishedKnownInitiatorIDs requires KnownInitiators mode, and is
  819. // intended for use with only a small number of known initiators.
  820. func (s *ResponderSessions) GetEstablishedKnownInitiatorIDs() map[ID]struct{} {
  821. s.mutex.Lock()
  822. defer s.mutex.Unlock()
  823. initiatorIDs := make(map[ID]struct{})
  824. if s.expectedInitiatorPublicKeys == nil {
  825. // Exit immediately when not in known initiator mode. Don't
  826. // accidentally iterator over potentially millions of sessions.
  827. return initiatorIDs
  828. }
  829. for _, entry := range s.sessions.Items() {
  830. session := entry.Object.(*session)
  831. initiatorID, err := session.getPeerID()
  832. if err != nil {
  833. // When getPeerID fails, the session is not yet established.
  834. continue
  835. }
  836. initiatorIDs[initiatorID] = struct{}{}
  837. }
  838. return initiatorIDs
  839. }
  840. // RequestHandler is an application-level handler that receives the decrypted
  841. // request payload and returns a response payload to be encrypted and sent to
  842. // the initiator. The initiatorID is the authenticated identifier of the
  843. // initiator: client, proxy, or broker.
  844. //
  845. // In cases where a request is a one-way message, with no response, such as a
  846. // BrokerServerReport, RequestHandler should return a nil packet.
  847. type RequestHandler func(initiatorID ID, request []byte) ([]byte, error)
  848. // HandlePacket takes a session packet, as received at the transport level,
  849. // and handles session handshake and request decryption. While a session
  850. // handshakes, HandlePacket returns the next handshake message to be relayed
  851. // back to the initiator over the transport.
  852. //
  853. // Once a session is fully established and a request is decrypted, the inner
  854. // request payload is passed to the RequestHandler for application-level
  855. // processing. The response received from the RequestHandler will be
  856. // encrypted with the session and returned from HandlePacket as the next
  857. // packet to send back over the transport. If there is no response to
  858. // be returned, HandlePacket returns a nil packet.
  859. //
  860. // The session packet contains a session ID that is used to route packets from
  861. // many initiators to the correct session state.
  862. //
  863. // Above the Noise protocol security layer, session packets have an
  864. // obfuscation layer. If a packet doesn't authenticate with the expected
  865. // obfuscation secret, or if a packet is replayed, HandlePacket returns an
  866. // error. The obfuscation anti-replay layer covers replays of Noise handshake
  867. // messages which aren't covered by the Noise nonce anti-replay. When
  868. // HandlePacket returns an error, the caller should invoke anti-probing
  869. // behavior, such as returning a generic 404 error from an HTTP server for
  870. // HTTPS transports.
  871. //
  872. // There is one expected error case with legitimate initiators: when an
  873. // initiator reuses a session that is expired or no longer in the responder
  874. // cache. In this case HandlePacket will return a reset session token in
  875. // outPacket along with an error, and the caller should log the error and
  876. // also send the packet to the initiator.
  877. //
  878. // The HandlePacket caller should implement initiator rate limiting in its
  879. // transport level.
  880. func (s *ResponderSessions) HandlePacket(
  881. inPacket []byte,
  882. requestHandler RequestHandler) (retOutPacket []byte, retErr error) {
  883. // Concurrency: no locks are held for this function, only in specific
  884. // helper functions.
  885. // unwrapSessionPacket deobfuscates the session packet, and unmarshals a
  886. // SessionPacket. The SessionPacket.SessionID is used to route the
  887. // session packet to an existing session or to create a new one. The
  888. // SessionPacket.Payload is a Noise handshake message or an encrypted
  889. // request and that will be handled below.
  890. sessionPacket, err := unwrapSessionPacket(
  891. s.receiveObfuscationSecret, false, s.obfuscationReplayHistory, inPacket)
  892. if err != nil {
  893. return nil, errors.Trace(err)
  894. }
  895. sessionID := sessionPacket.SessionID
  896. // Check for an existing session with this session ID, or create a new one
  897. // if not found. If the session _was_ in the cache but is now expired, a
  898. // new session is created, but subsequent Noise operations will fail.
  899. session, err := s.getSession(sessionID)
  900. if err != nil {
  901. return nil, errors.Trace(err)
  902. }
  903. retainSession := false
  904. defer func() {
  905. if retErr != nil && !retainSession {
  906. // If an error is returned, the session has failed, so don't
  907. // retain it in the cache as it could be more recently used than
  908. // an older but still valid session.
  909. //
  910. // TODO: should we retain the session if it has completed the
  911. // handshake? As with initiator error signals, and depending on
  912. // the transport security level, a SessionPacket with a
  913. // legitimate session ID but corrupt Noise payload could be
  914. // forged, terminating a legitimate session.
  915. s.removeSession(sessionID)
  916. }
  917. }()
  918. var requestPayload []byte
  919. if session.isEstablished() {
  920. // When the session is already established, decrypt the packet to get
  921. // the request.
  922. payload, err := session.receiveUnmarshaledPacket(sessionPacket)
  923. if err != nil {
  924. return nil, errors.Trace(err)
  925. }
  926. requestPayload = payload
  927. } else {
  928. // When the session is not established, the packet is the next
  929. // handshake message. The initiator appends the request payload to
  930. // the end of its last XK handshake message, and in that case payload
  931. // will contain the request.
  932. isEstablished, outPacket, payload, err :=
  933. session.nextUnmarshaledHandshakePacket(sessionPacket)
  934. if err != nil {
  935. if _, ok := err.(potentialExpiredSessionError); !ok {
  936. return nil, errors.Trace(err)
  937. }
  938. // The initiator may be trying to use a previously valid session
  939. // which is now expired or flushed, due to a full cache or a
  940. // server reboot. Craft and send a secure reset session token,
  941. // signed with the responder public key (the Ed25519
  942. // representation), bound to the packet just received from the
  943. // initiator (to defend against replay).
  944. outPacket, wrapErr := wrapSessionPacket(
  945. s.sendObfuscationSecret,
  946. false,
  947. &SessionPacket{
  948. SessionID: sessionPacket.SessionID,
  949. ResetSessionToken: makeResetSessionToken(s.privateKey, inPacket),
  950. })
  951. if wrapErr != nil {
  952. return nil, errors.Trace(wrapErr)
  953. }
  954. return outPacket, errors.Trace(err)
  955. }
  956. if outPacket != nil {
  957. // The handshake is not complete until outPacket is nil; send the
  958. // next handshake packet.
  959. if payload != nil {
  960. // A payload is not expected unless the handshake is complete.
  961. return nil, errors.TraceNew("unexpected handshake payload")
  962. }
  963. // The session TTL is not extended here. Initiators, including
  964. // clients and proxies, are given sessionsTTL to complete the
  965. // entire handshake.
  966. return outPacket, nil
  967. }
  968. if !isEstablished || payload == nil {
  969. // When outPacket is nil, the handshake should be complete --
  970. // isEstablished -- and, by convention, the first request payload
  971. // should be available.
  972. return nil, errors.TraceNew("unexpected established state")
  973. }
  974. requestPayload = payload
  975. }
  976. // Extend the session TTL.
  977. s.touchSession(sessionID, session)
  978. initiatorID, err := session.getPeerID()
  979. if err != nil {
  980. return nil, errors.Trace(err)
  981. }
  982. var sessionRoundTrip SessionRoundTrip
  983. err = unmarshalRecord(recordTypeSessionRoundTrip, requestPayload, &sessionRoundTrip)
  984. if err != nil {
  985. return nil, errors.Trace(err)
  986. }
  987. request := sessionRoundTrip.Payload
  988. response, err := requestHandler(initiatorID, request)
  989. if err != nil {
  990. // Don't delete the session if the application-level request handler
  991. // returns an error, as there is no problem with the Noise session.
  992. // Non-failure application-level errors can include cases like a
  993. // fronting CDN aborting a request due to timeout misalignment.
  994. retainSession = true
  995. return nil, errors.Trace(err)
  996. }
  997. if response == nil {
  998. // There is no response.
  999. return nil, nil
  1000. }
  1001. // The response is assigned the same RoundTripID as the request.
  1002. sessionRoundTrip = SessionRoundTrip{
  1003. RoundTripID: sessionRoundTrip.RoundTripID,
  1004. Payload: response,
  1005. }
  1006. responsePayload, err := marshalRecord(
  1007. sessionRoundTrip, recordTypeSessionRoundTrip)
  1008. if err != nil {
  1009. return nil, errors.Trace(err)
  1010. }
  1011. responsePacket, err := session.sendPacket(responsePayload)
  1012. if err != nil {
  1013. return nil, errors.Trace(err)
  1014. }
  1015. return responsePacket, nil
  1016. }
  1017. // touchSession sets a cached session for the specified session ID; if the
  1018. // session is already in the cache, its TTL is extended. The LRU session
  1019. // cache entry may be discarded once the cache is full.
  1020. func (s *ResponderSessions) touchSession(sessionID ID, session *session) {
  1021. s.mutex.Lock()
  1022. defer s.mutex.Unlock()
  1023. if !session.hasUnexpectedInitiatorPublicKey() {
  1024. // In this case, SetKnownInitiatorPublicKeys was called concurrent to
  1025. // HandlePacket, after HandlePacket's getSession, and now the known
  1026. // initiator public key for this session is no longer valid; don't
  1027. // cache or extend the session, as that could revert a session flush
  1028. // performed in SetKnownInitiatorPublicKeys.
  1029. //
  1030. // Limitation: this won't interrupt a handshake in progress, which may
  1031. // complete, but then ultimately fail.
  1032. return
  1033. }
  1034. TTL := lrucache.DefaultExpiration
  1035. if !s.applyTTL {
  1036. TTL = lrucache.NoExpiration
  1037. }
  1038. s.sessions.Set(string(sessionID[:]), session, TTL)
  1039. }
  1040. // getSession returns an existing session for the specified session ID, or
  1041. // creates a new session, and places it in the cache, if not found.
  1042. func (s *ResponderSessions) getSession(sessionID ID) (*session, error) {
  1043. // Concurrency: profiling indicates that holding the mutex lock here when
  1044. // calling newSession leads to high contention and blocking. Instead,
  1045. // release the lock after checking for an existing session, and then
  1046. // recheck -- using lrucache.Add, which fails if an entry exists -- when
  1047. // inserting.
  1048. //
  1049. // A read-only lock is obtained on the initial check, allowing for
  1050. // concurrent checks; however, note that lrucache has its own RWMutex and
  1051. // obtains a write lock in Get when LRU ejection may need to be performed.
  1052. //
  1053. // A semaphore is used to enforce a sanity check maximum number of
  1054. // concurrent newSession calls.
  1055. //
  1056. // TODO: add a timeout or stop signal to Acquire?
  1057. strSessionID := string(sessionID[:])
  1058. s.mutex.RLock()
  1059. entry, ok := s.sessions.Get(strSessionID)
  1060. s.mutex.RUnlock()
  1061. if ok {
  1062. return entry.(*session), nil
  1063. }
  1064. err := s.concurrentNewSessions.Acquire(context.Background(), 1)
  1065. if err != nil {
  1066. return nil, errors.Trace(err)
  1067. }
  1068. session, err := newSession(
  1069. false, // !isInitiator
  1070. s.privateKey,
  1071. s.sendObfuscationSecret,
  1072. s.receiveObfuscationSecret,
  1073. s.obfuscationReplayHistory,
  1074. nil,
  1075. nil,
  1076. &sessionID,
  1077. s.expectedInitiatorPublicKeys)
  1078. s.concurrentNewSessions.Release(1)
  1079. if err != nil {
  1080. return nil, errors.Trace(err)
  1081. }
  1082. s.mutex.Lock()
  1083. err = s.sessions.Add(
  1084. strSessionID, session, lrucache.DefaultExpiration)
  1085. s.mutex.Unlock()
  1086. if err != nil {
  1087. return nil, errors.Trace(err)
  1088. }
  1089. return session, nil
  1090. }
  1091. // removeSession removes any existing session for the specified session ID.
  1092. func (s *ResponderSessions) removeSession(sessionID ID) {
  1093. s.mutex.Lock()
  1094. defer s.mutex.Unlock()
  1095. s.sessions.Delete(string(sessionID[:]))
  1096. }
  1097. // makeResetSessionToken creates a secure reset session token.
  1098. //
  1099. // This token is used for a responder to signal to an initiator that a session
  1100. // has expired, or is no longer valid and that a new session should be
  1101. // established. Securing this signal is particularly important for the
  1102. // broker/server sessions relayed by untrusted clients, as it prevents a
  1103. // malicious client from injecting invalid reset tokens and
  1104. // interrupting/degrading session performance.
  1105. //
  1106. // A reset token is signed by the responder's Ed25519 public key. The signature covers:
  1107. // - The last packet received from the initiator, mitigating replay attacks
  1108. // - A context name, resetSessionTokenName, and nonce which mitigates against
  1109. // directly signing arbitrary data in the untrusted last packet received
  1110. // from the initiator
  1111. //
  1112. // Reset session tokens are not part of the Noise protocol, but are sent as
  1113. // session packets.
  1114. func makeResetSessionToken(
  1115. privateKey SessionPrivateKey,
  1116. receivedPacket []byte) []byte {
  1117. var token bytes.Buffer
  1118. token.Write(prng.Bytes(resetSessionTokenNonceSize))
  1119. h := sha256.New()
  1120. h.Write([]byte(resetSessionTokenName))
  1121. h.Write(token.Bytes()[:resetSessionTokenNonceSize])
  1122. h.Write(receivedPacket)
  1123. token.Write(ed25519.Sign(privateKey[:], h.Sum(nil)))
  1124. return token.Bytes()
  1125. }
  1126. // isValidResetSessionToken checks if a reset session token is valid, given
  1127. // the specified responder public key and last packet sent to the responder.
  1128. func isValidResetSessionToken(
  1129. publicKey SessionPublicKey,
  1130. lastSentPacket []byte,
  1131. token []byte) bool {
  1132. if len(token) <= resetSessionTokenNonceSize {
  1133. return false
  1134. }
  1135. h := sha256.New()
  1136. h.Write([]byte(resetSessionTokenName))
  1137. h.Write(token[:resetSessionTokenNonceSize])
  1138. h.Write(lastSentPacket)
  1139. return ed25519.Verify(publicKey[:], h.Sum(nil), token[resetSessionTokenNonceSize:])
  1140. }
  1141. // sessionPublicKeyLookup implements set membership lookup for session public
  1142. // keys, and is used to lookup expected public keys for optional responder
  1143. // access control. The sessionPublicKeyLookup is initialized with a list of
  1144. // Ed25519 session public keys, the canonical representation, while the
  1145. // lookup is done with Curve25519 public keys, the representation that is
  1146. // received via the Noise protocol.
  1147. type sessionPublicKeyLookup struct {
  1148. mutex sync.Mutex
  1149. lookupMap map[SessionPublicKeyCurve25519]struct{}
  1150. }
  1151. func newSessionPublicKeyLookup(publicKeys []SessionPublicKey) (*sessionPublicKeyLookup, error) {
  1152. s := &sessionPublicKeyLookup{
  1153. lookupMap: make(map[SessionPublicKeyCurve25519]struct{}),
  1154. }
  1155. _, err := s.set(publicKeys)
  1156. if err != nil {
  1157. return nil, errors.Trace(err)
  1158. }
  1159. return s, nil
  1160. }
  1161. // set modifies the lookup set of session public keys and returns true if the
  1162. // set has changed.
  1163. func (s *sessionPublicKeyLookup) set(publicKeys []SessionPublicKey) (bool, error) {
  1164. s.mutex.Lock()
  1165. defer s.mutex.Unlock()
  1166. // Convert the Ed25519 public key to its Curve25519 representation, which
  1167. // is what's looked up. SessionPublicKeyCurve25519 is a fixed-size array
  1168. // which can be used as a map key.
  1169. var curve25519PublicKeys []SessionPublicKeyCurve25519
  1170. for _, publicKey := range publicKeys {
  1171. k, err := publicKey.ToCurve25519()
  1172. if err != nil {
  1173. return false, errors.Trace(err)
  1174. }
  1175. curve25519PublicKeys = append(curve25519PublicKeys, k)
  1176. }
  1177. // Check if the set of public keys has changed. This check and return
  1178. // value is used by ResponderSessions.SetKnownInitiatorPublicKeys to skip
  1179. // checking for sessions to be revoked in the case of an overall tactics
  1180. // reload in which configured expected public keys did not change.
  1181. if len(curve25519PublicKeys) == len(s.lookupMap) {
  1182. allFound := true
  1183. for _, k := range curve25519PublicKeys {
  1184. if _, ok := s.lookupMap[k]; !ok {
  1185. allFound = false
  1186. break
  1187. }
  1188. }
  1189. if allFound {
  1190. return false, nil
  1191. }
  1192. }
  1193. lookupMap := make(map[SessionPublicKeyCurve25519]struct{})
  1194. for _, k := range curve25519PublicKeys {
  1195. lookupMap[k] = struct{}{}
  1196. }
  1197. s.lookupMap = lookupMap
  1198. return true, nil
  1199. }
  1200. func (s *sessionPublicKeyLookup) lookup(k SessionPublicKeyCurve25519) bool {
  1201. s.mutex.Lock()
  1202. defer s.mutex.Unlock()
  1203. _, ok := s.lookupMap[k]
  1204. return ok
  1205. }
  1206. type sessionState int
  1207. const (
  1208. /*
  1209. XK:
  1210. <- s
  1211. ...
  1212. -> e, es
  1213. <- e, ee
  1214. -> s, se [+ first payload]
  1215. */
  1216. sessionStateInitiator_XK_send_e_es = iota
  1217. sessionStateInitiator_XK_recv_e_ee_send_s_se_payload
  1218. sessionStateInitiator_XK_established
  1219. sessionStateInitiator_failed
  1220. sessionStateResponder_XK_recv_e_es_send_e_ee
  1221. sessionStateResponder_XK_recv_s_se_payload
  1222. sessionStateResponder_XK_established
  1223. )
  1224. // session represents a Noise protocol session, including its initial
  1225. // handshake state.
  1226. //
  1227. // The XK pattern is used:
  1228. // - Initiators may have short-lived static keys (clients), or long-lived
  1229. // static keys (proxies and brokers). The initiator key is securely
  1230. // transmitted to the responder while hiding its value.
  1231. // - The responder static key is always known (K) and exchanged out of
  1232. // band.
  1233. // - Provides forward secrecy.
  1234. // - The round trip request can be appended to the initiators final
  1235. // handshake message, eliminating an extra round trip.
  1236. //
  1237. // For in-proxy, any client or proxy can connect to a broker. Only allowed
  1238. // brokers can connect to a server.
  1239. //
  1240. // To limit access to allowed brokers, expectedInitiatorPublicKeys is an allow
  1241. // list of broker public keys. XK is still used for this case, instead of
  1242. // KK:
  1243. // - With KK, the broker identity would have to be known before the Noise
  1244. // handshake begins
  1245. // - With XK, the broker proves possession of a private key corresponding to
  1246. // a broker public key on the allow list.
  1247. // - While KK will abort sooner than XK when an invalid broker key is used,
  1248. // completing the handshake and decrypting the first payload does not
  1249. // leak any information.
  1250. //
  1251. // The is no "close" operation for sessions. Responders will maintain a cache
  1252. // of established sessions and discard the state for expired sessions or in
  1253. // an LRU fashion. Initiators will reuse sessions until they are rejected by
  1254. // a responder.
  1255. //
  1256. // There is no state for the obfuscation layer; each packet is obfuscated
  1257. // independently since session packets may arrive at a peer out-of-order.
  1258. //
  1259. // There are independent replay defenses at both the obfuscation layer
  1260. // (to mitigate active probing replays) and at the Noise protocol layer
  1261. // (to defend against replay of Noise protocol packets). The obfuscation
  1262. // anti-replay covers all obfuscated packet nonce values, and the Noise
  1263. // anti-replay filter covers post-handshake packet message sequence number
  1264. // nonces. The Noise layer anti-replay filter uses a sliding window of size
  1265. // ~8000, allowing for approximately that degree of out-of-order packets as
  1266. // could happen with concurrent requests in a shared session.
  1267. //
  1268. // Future enhancement: use a single anti-replay mechanism for both use cases?
  1269. type session struct {
  1270. isInitiator bool
  1271. sessionID ID
  1272. sendObfuscationSecret ObfuscationSecret
  1273. receiveObfuscationSecret ObfuscationSecret
  1274. replayHistory *obfuscationReplayHistory
  1275. expectedInitiatorPublicKeys *sessionPublicKeyLookup
  1276. mutex sync.Mutex
  1277. state sessionState
  1278. signalAwaitingReady []chan struct{}
  1279. handshake *noise.HandshakeState
  1280. firstPayload []byte
  1281. peerPublicKey []byte
  1282. send *noise.CipherState
  1283. receive *noise.CipherState
  1284. nonceReplay replay.Filter
  1285. }
  1286. func newSession(
  1287. isInitiator bool,
  1288. privateKey SessionPrivateKey,
  1289. sendObfuscationSecret ObfuscationSecret,
  1290. receiveObfuscationSecret ObfuscationSecret,
  1291. replayHistory *obfuscationReplayHistory,
  1292. // Initiator
  1293. expectedResponderPublicKey *SessionPublicKey,
  1294. firstPayload []byte,
  1295. // Responder
  1296. peerSessionID *ID,
  1297. expectedInitiatorPublicKeys *sessionPublicKeyLookup) (*session, error) {
  1298. if isInitiator {
  1299. if peerSessionID != nil ||
  1300. expectedResponderPublicKey == nil ||
  1301. expectedInitiatorPublicKeys != nil ||
  1302. firstPayload == nil {
  1303. return nil, errors.TraceNew("unexpected initiator parameters")
  1304. }
  1305. } else {
  1306. if peerSessionID == nil ||
  1307. expectedResponderPublicKey != nil ||
  1308. firstPayload != nil {
  1309. return nil, errors.TraceNew("unexpected responder parameters")
  1310. }
  1311. }
  1312. sessionID := peerSessionID
  1313. if sessionID == nil {
  1314. ID, err := MakeID()
  1315. if err != nil {
  1316. return nil, errors.Trace(err)
  1317. }
  1318. sessionID = &ID
  1319. }
  1320. // The prologue binds the session ID and other meta data to the session.
  1321. prologue, err := protocol.CBOREncoding.Marshal(SessionPrologue{
  1322. SessionProtocolName: SessionProtocolName,
  1323. SessionProtocolVersion: SessionProtocolVersion1,
  1324. SessionID: *sessionID,
  1325. })
  1326. if err != nil {
  1327. return nil, errors.Trace(err)
  1328. }
  1329. publicKey, err := privateKey.GetPublicKey()
  1330. if err != nil {
  1331. return nil, errors.Trace(err)
  1332. }
  1333. privateKeyCurve25519 := privateKey.ToCurve25519()
  1334. publicKeyCurve25519, err := publicKey.ToCurve25519()
  1335. if err != nil {
  1336. return nil, errors.Trace(err)
  1337. }
  1338. // SessionProtocolVersion1 implies this ciphersuite
  1339. config := noise.Config{
  1340. CipherSuite: noise.NewCipherSuite(noise.DH25519, noise.CipherChaChaPoly, noise.HashBLAKE2b),
  1341. Pattern: noise.HandshakeXK,
  1342. Initiator: isInitiator,
  1343. Prologue: prologue,
  1344. StaticKeypair: noise.DHKey{
  1345. Public: publicKeyCurve25519[:],
  1346. Private: privateKeyCurve25519},
  1347. }
  1348. if expectedResponderPublicKey != nil {
  1349. k, err := (*expectedResponderPublicKey).ToCurve25519()
  1350. if err != nil {
  1351. return nil, errors.Trace(err)
  1352. }
  1353. config.PeerStatic = k[:]
  1354. }
  1355. handshake, err := noise.NewHandshakeState(config)
  1356. if err != nil {
  1357. return nil, errors.Trace(err)
  1358. }
  1359. var state sessionState
  1360. if isInitiator {
  1361. state = sessionStateInitiator_XK_send_e_es
  1362. } else {
  1363. state = sessionStateResponder_XK_recv_e_es_send_e_ee
  1364. }
  1365. return &session{
  1366. isInitiator: isInitiator,
  1367. sessionID: *sessionID,
  1368. sendObfuscationSecret: sendObfuscationSecret,
  1369. receiveObfuscationSecret: receiveObfuscationSecret,
  1370. replayHistory: replayHistory,
  1371. expectedInitiatorPublicKeys: expectedInitiatorPublicKeys,
  1372. state: state,
  1373. signalAwaitingReady: make([]chan struct{}, 0), // must be non-nil
  1374. handshake: handshake,
  1375. firstPayload: firstPayload,
  1376. }, nil
  1377. }
  1378. // isEstablished indicates that the session handshake is complete.
  1379. //
  1380. // A session may not be ready to share when isEstablished is true.
  1381. func (s *session) isEstablished() bool {
  1382. s.mutex.Lock()
  1383. defer s.mutex.Unlock()
  1384. return s.handshake == nil
  1385. }
  1386. // isReadyToShare indicates that the session handshake is complete _and_ that
  1387. // the peer is known to have received and processed the final handshake
  1388. // message.
  1389. //
  1390. // When isReadyToShare is true, multiple round trips can use a session
  1391. // concurrently. Requests from different round trips can arrive at the peer
  1392. // out-of-order.
  1393. //
  1394. // Session sharing is performed by initiators, and in the XK handshake the
  1395. // last step is the initiator sends a final message to the responder. While
  1396. // the initiator session becomes "established" after that last message is
  1397. // output, we need to delay other round trips from sharing the session and
  1398. // sending session-encrypted packets to the responder before the responder
  1399. // actually receives that final handshake message.
  1400. //
  1401. // isReadyToShare becomes true once the round trip performing the handshake
  1402. // receives its round trip response, which demonstrates that the responder
  1403. // received the final message.
  1404. //
  1405. // When a signal channel is specified, it is registered and signaled once the
  1406. // session becomes ready to share _or_ the session fails to become ready due
  1407. // to a transport failure. When signaled, the caller must call isReadyToShare
  1408. // once again to distinguish between these two outcomes.
  1409. func (s *session) isReadyToShare(signal chan struct{}) bool {
  1410. s.mutex.Lock()
  1411. defer s.mutex.Unlock()
  1412. if !s.isInitiator || s.state == sessionStateInitiator_failed {
  1413. // Signal immediately if transportFailed was already called.
  1414. if signal != nil {
  1415. close(signal)
  1416. }
  1417. return false
  1418. }
  1419. if s.handshake == nil && s.signalAwaitingReady == nil {
  1420. return true
  1421. }
  1422. if signal != nil {
  1423. s.signalAwaitingReady = append(
  1424. s.signalAwaitingReady, signal)
  1425. }
  1426. return false
  1427. }
  1428. // transportFailed marks the session as failed and signals any initiators
  1429. // waiting to share the session.
  1430. //
  1431. // transportFailed is ignored if the session is already ready to share, as any
  1432. // transport failures past that point affect only one application-level round
  1433. // trip and not the session.
  1434. func (s *session) transportFailed() {
  1435. s.mutex.Lock()
  1436. defer s.mutex.Unlock()
  1437. if !s.isInitiator {
  1438. return
  1439. }
  1440. // Already ready to share, so ignore the transport failure.
  1441. if s.handshake == nil && s.signalAwaitingReady == nil {
  1442. return
  1443. }
  1444. if s.state == sessionStateInitiator_failed {
  1445. return
  1446. }
  1447. // In the sessionStateInitiator_failed state, nextHandshakePacket will
  1448. // always fail.
  1449. s.state = sessionStateInitiator_failed
  1450. for _, signal := range s.signalAwaitingReady {
  1451. close(signal)
  1452. }
  1453. s.signalAwaitingReady = nil
  1454. }
  1455. // getPeerID returns the peer's public key, in the form of an ID. A given peer
  1456. // identifier can only be provided by the peer with the corresponding private
  1457. // key.
  1458. func (s *session) getPeerID() (ID, error) {
  1459. s.mutex.Lock()
  1460. defer s.mutex.Unlock()
  1461. if s.handshake != nil {
  1462. return ID{}, errors.TraceNew("not established")
  1463. }
  1464. return ID(s.peerPublicKey), nil
  1465. }
  1466. // hasUnexpectedInitiatorPublicKey indicates whether the session is
  1467. // established (and so has obtained a peer public key),
  1468. // expectedInitiatorPublicKeys is configured, and the session initiator's
  1469. // public key is not in/no longer in expectedInitiatorPublicKeys.
  1470. func (s *session) hasUnexpectedInitiatorPublicKey() bool {
  1471. s.mutex.Lock()
  1472. defer s.mutex.Unlock()
  1473. if s.expectedInitiatorPublicKeys == nil {
  1474. // Not expecting specific initiator public keys
  1475. return false
  1476. }
  1477. if s.handshake != nil {
  1478. // Peer public key not known yet
  1479. return false
  1480. }
  1481. var k SessionPublicKeyCurve25519
  1482. copy(k[:], s.peerPublicKey)
  1483. return !s.expectedInitiatorPublicKeys.lookup(k)
  1484. }
  1485. // sendPacket prepares a session packet to be sent to the peer, containing the
  1486. // specified round trip payload. The packet is secured by the established
  1487. // session.
  1488. func (s *session) sendPacket(payload []byte) ([]byte, error) {
  1489. s.mutex.Lock()
  1490. defer s.mutex.Unlock()
  1491. if s.handshake != nil {
  1492. return nil, errors.TraceNew("not established")
  1493. }
  1494. if s.send == nil {
  1495. return nil, errors.Trace(s.unexpectedStateError())
  1496. }
  1497. nonce := s.send.Nonce()
  1498. // Unlike tunnels, for example, sessions are not for bulk data transfer
  1499. // and we don't aim for zero allocation or extensive buffer reuse.
  1500. encryptedPayload, err := s.send.Encrypt(nil, nil, payload)
  1501. if err != nil {
  1502. return nil, errors.Trace(err)
  1503. }
  1504. sessionPacket, err := s.wrapPacket(
  1505. &SessionPacket{
  1506. SessionID: s.sessionID,
  1507. Nonce: nonce,
  1508. Payload: encryptedPayload,
  1509. })
  1510. if err != nil {
  1511. return nil, errors.Trace(err)
  1512. }
  1513. return sessionPacket, nil
  1514. }
  1515. // receivePacket opens a session packet received from the peer, using the
  1516. // established session, and returns the round trip payload.
  1517. //
  1518. // As responders need to inspect the packet and use its session ID to route
  1519. // packets to the correct session, responders will call
  1520. // receiveUnmarshaledPacket instead.
  1521. func (s *session) receivePacket(packet []byte) ([]byte, error) {
  1522. sessionPacket, err := s.unwrapPacket(packet)
  1523. if err != nil {
  1524. return nil, errors.Trace(err)
  1525. }
  1526. payload, err := s.receiveUnmarshaledPacket(sessionPacket)
  1527. if err != nil {
  1528. return nil, errors.Trace(err)
  1529. }
  1530. return payload, nil
  1531. }
  1532. func (s *session) receiveUnmarshaledPacket(
  1533. sessionPacket *SessionPacket) ([]byte, error) {
  1534. s.mutex.Lock()
  1535. defer s.mutex.Unlock()
  1536. if s.receive == nil {
  1537. return nil, errors.Trace(s.unexpectedStateError())
  1538. }
  1539. if sessionPacket.SessionID != s.sessionID {
  1540. return nil, errors.Tracef("unexpected sessionID")
  1541. }
  1542. s.receive.SetNonce(sessionPacket.Nonce)
  1543. payload, err := s.receive.Decrypt(nil, nil, sessionPacket.Payload)
  1544. if err != nil {
  1545. return nil, errors.Trace(err)
  1546. }
  1547. if !s.nonceReplay.ValidateCounter(sessionPacket.Nonce, math.MaxUint64) {
  1548. return nil, errors.TraceNew("replay detected")
  1549. }
  1550. // The session is ready to share once it's received a post-handshake
  1551. // response from the peer.
  1552. s.readyToShare()
  1553. return payload, nil
  1554. }
  1555. // nextHandshakePacket advances the session handshake. nextHandshakePacket
  1556. // takes the next handshake packet received from the peer and returns the
  1557. // next handshake packet to send to the peer. Start by passing nil for
  1558. // inPacket. The handshake is complete when outPacket is nil.
  1559. //
  1560. // XK bundles the first initiator request payload along with a handshake
  1561. // message, and nextHandshakePacket output that payload to the responder when
  1562. // the handshake is complete.
  1563. //
  1564. // Once the handshake is complete, further round trips are exchanged using
  1565. // sendPacket and receivePacket.
  1566. //
  1567. // As responders need to inspect the packet and use its session ID to route
  1568. // packets to the correct session, responders will call
  1569. // nextUnmarshaledHandshakePacket instead.
  1570. func (s *session) nextHandshakePacket(inPacket []byte) (
  1571. isEstablished bool, outPacket []byte, payload []byte, err error) {
  1572. var sessionPacket *SessionPacket
  1573. if inPacket != nil {
  1574. sessionPacket, err = s.unwrapPacket(inPacket)
  1575. if err != nil {
  1576. return false, nil, nil, errors.Trace(err)
  1577. }
  1578. }
  1579. isEstablished, outPacket, payload, err =
  1580. s.nextUnmarshaledHandshakePacket(sessionPacket)
  1581. if err != nil {
  1582. return false, nil, nil, errors.Trace(err)
  1583. }
  1584. return isEstablished, outPacket, payload, nil
  1585. }
  1586. // potentialExpiredSessionError is packet error that indicates a potential
  1587. // expired session condition which should be handled with a reset session
  1588. // token. This includes the responder expecting a handshake packet for a new
  1589. // session, but receiving a non-handshake packet.
  1590. // Non-potentialExpiredSessionError errors include
  1591. // "unexpected initiator public key".
  1592. type potentialExpiredSessionError struct {
  1593. error
  1594. }
  1595. func (s *session) nextUnmarshaledHandshakePacket(sessionPacket *SessionPacket) (
  1596. isEstablished bool, outPacket []byte, payload []byte, err error) {
  1597. s.mutex.Lock()
  1598. defer s.mutex.Unlock()
  1599. var in []byte
  1600. if sessionPacket != nil {
  1601. if sessionPacket.SessionID != s.sessionID {
  1602. return false, nil, nil, errors.Tracef("unexpected sessionID")
  1603. }
  1604. if sessionPacket.Nonce != 0 {
  1605. // A handshake message was expected, but this packet contains a
  1606. // post-handshake nonce, Flag this as a potential expired session
  1607. // case. See comment below for limitation.
  1608. return false, nil, nil,
  1609. potentialExpiredSessionError{errors.TraceNew("unexpected nonce")}
  1610. }
  1611. in = sessionPacket.Payload
  1612. }
  1613. // Handle handshake state transitions.
  1614. switch s.state {
  1615. // Initiator
  1616. case sessionStateInitiator_XK_send_e_es:
  1617. out, _, _, err := s.handshake.WriteMessage(nil, nil)
  1618. if err != nil {
  1619. return false, nil, nil, errors.Trace(err)
  1620. }
  1621. outPacket, err := s.wrapPacket(
  1622. &SessionPacket{SessionID: s.sessionID, Payload: out})
  1623. if err != nil {
  1624. return false, nil, nil, errors.Trace(err)
  1625. }
  1626. s.state = sessionStateInitiator_XK_recv_e_ee_send_s_se_payload
  1627. return false, outPacket, nil, nil
  1628. case sessionStateInitiator_XK_recv_e_ee_send_s_se_payload:
  1629. _, _, _, err := s.handshake.ReadMessage(nil, in)
  1630. if err != nil {
  1631. return false, nil, nil, errors.Trace(err)
  1632. }
  1633. out, send, receive, err := s.handshake.WriteMessage(nil, s.firstPayload)
  1634. if err != nil {
  1635. return false, nil, nil, errors.Trace(err)
  1636. }
  1637. outPacket, err := s.wrapPacket(
  1638. &SessionPacket{SessionID: s.sessionID, Payload: out})
  1639. if err != nil {
  1640. return false, nil, nil, errors.Trace(err)
  1641. }
  1642. s.state = sessionStateInitiator_XK_established
  1643. s.established(send, receive)
  1644. return true, outPacket, nil, nil
  1645. // Responder
  1646. case sessionStateResponder_XK_recv_e_es_send_e_ee:
  1647. _, _, _, err := s.handshake.ReadMessage(nil, in)
  1648. if err != nil {
  1649. // A handshake message was expected, but and invalid message type
  1650. // was received. Flag this as a potential expired session case, a
  1651. // candidate for a reset session token. Limitation: there's no
  1652. // check that the invalid message was, in fact, a valid message
  1653. // for an expired session; this may not be possible given the
  1654. // established-session Noise protocol message is encrypted/random.
  1655. return false, nil, nil, potentialExpiredSessionError{errors.Trace(err)}
  1656. }
  1657. out, _, _, err := s.handshake.WriteMessage(nil, nil)
  1658. if err != nil {
  1659. return false, nil, nil, errors.Trace(err)
  1660. }
  1661. outPacket, err := s.wrapPacket(
  1662. &SessionPacket{SessionID: s.sessionID, Payload: out})
  1663. if err != nil {
  1664. return false, nil, nil, errors.Trace(err)
  1665. }
  1666. s.state = sessionStateResponder_XK_recv_s_se_payload
  1667. return false, outPacket, nil, nil
  1668. case sessionStateResponder_XK_recv_s_se_payload:
  1669. firstPayload, receive, send, err := s.handshake.ReadMessage(nil, in)
  1670. if err != nil {
  1671. return false, nil, nil, errors.Trace(err)
  1672. }
  1673. // Check if the initiator's public key in on the allow list.
  1674. //
  1675. // Limitation: unlike with the KK pattern, the handshake completes and
  1676. // the initial payload is decrypted even when the initiator public
  1677. // key is not on the allow list.
  1678. err = s.checkExpectedInitiatorPublicKeys(s.handshake.PeerStatic())
  1679. if err != nil {
  1680. return false, nil, nil, errors.Trace(err)
  1681. }
  1682. s.state = sessionStateResponder_XK_established
  1683. s.established(send, receive)
  1684. return true, nil, firstPayload, nil
  1685. }
  1686. return false, nil, nil, errors.Trace(s.unexpectedStateError())
  1687. }
  1688. func (s *session) checkExpectedInitiatorPublicKeys(peerPublicKey []byte) error {
  1689. if s.expectedInitiatorPublicKeys == nil {
  1690. return nil
  1691. }
  1692. var k SessionPublicKeyCurve25519
  1693. copy(k[:], peerPublicKey)
  1694. ok := s.expectedInitiatorPublicKeys.lookup(k)
  1695. if !ok {
  1696. return errors.TraceNew("unexpected initiator public key")
  1697. }
  1698. return nil
  1699. }
  1700. // Set the session as established.
  1701. func (s *session) established(
  1702. send *noise.CipherState,
  1703. receive *noise.CipherState) {
  1704. // Assumes s.mutex lock is held.
  1705. s.peerPublicKey = s.handshake.PeerStatic()
  1706. s.handshake = nil
  1707. s.firstPayload = nil
  1708. s.send = send
  1709. s.receive = receive
  1710. }
  1711. // Set the session as ready to share.
  1712. func (s *session) readyToShare() {
  1713. // Assumes s.mutex lock is held.
  1714. if !s.isInitiator {
  1715. return
  1716. }
  1717. if s.signalAwaitingReady == nil {
  1718. return
  1719. }
  1720. for _, signal := range s.signalAwaitingReady {
  1721. close(signal)
  1722. }
  1723. s.signalAwaitingReady = nil
  1724. }
  1725. // Marshal and obfuscate a SessionPacket.
  1726. func (s *session) wrapPacket(sessionPacket *SessionPacket) ([]byte, error) {
  1727. // No lock. References only static session fields.
  1728. obfuscatedPacket, err := wrapSessionPacket(
  1729. s.sendObfuscationSecret,
  1730. s.isInitiator,
  1731. sessionPacket)
  1732. if err != nil {
  1733. return nil, errors.Trace(err)
  1734. }
  1735. return obfuscatedPacket, nil
  1736. }
  1737. // Marshal and obfuscated a SessionPacket. wrapSessionPacket is used by
  1738. // responders to wrap reset session token packets.
  1739. func wrapSessionPacket(
  1740. sendObfuscationSecret ObfuscationSecret,
  1741. isInitiator bool,
  1742. sessionPacket *SessionPacket) ([]byte, error) {
  1743. marshaledPacket, err := marshalRecord(
  1744. sessionPacket, recordTypeSessionPacket)
  1745. if err != nil {
  1746. return nil, errors.Trace(err)
  1747. }
  1748. obfuscatedPacket, err := obfuscateSessionPacket(
  1749. sendObfuscationSecret,
  1750. isInitiator,
  1751. marshaledPacket,
  1752. sessionObfuscationPaddingMinSize,
  1753. sessionObfuscationPaddingMaxSize)
  1754. if err != nil {
  1755. return nil, errors.Trace(err)
  1756. }
  1757. return obfuscatedPacket, nil
  1758. }
  1759. // Deobfuscate and unmarshal a SessionPacket.
  1760. func (s *session) unwrapPacket(obfuscatedPacket []byte) (*SessionPacket, error) {
  1761. // No lock. References only static session fields.
  1762. sessionPacket, err := unwrapSessionPacket(
  1763. s.receiveObfuscationSecret,
  1764. s.isInitiator,
  1765. s.replayHistory,
  1766. obfuscatedPacket)
  1767. if err != nil {
  1768. return nil, errors.Trace(err)
  1769. }
  1770. return sessionPacket, nil
  1771. }
  1772. // Deobfuscate and unmarshal SessionPacket. unwrapSessionPacket is used by
  1773. // responders, which must peak at the SessionPacket and get the session ID to
  1774. // route packets to the correct session.
  1775. func unwrapSessionPacket(
  1776. receiveObfuscationSecret ObfuscationSecret,
  1777. isInitiator bool,
  1778. replayHistory *obfuscationReplayHistory,
  1779. obfuscatedPacket []byte) (*SessionPacket, error) {
  1780. packet, err := deobfuscateSessionPacket(
  1781. receiveObfuscationSecret,
  1782. isInitiator,
  1783. replayHistory,
  1784. obfuscatedPacket)
  1785. if err != nil {
  1786. return nil, errors.Trace(err)
  1787. }
  1788. var sessionPacket *SessionPacket
  1789. err = unmarshalRecord(recordTypeSessionPacket, packet, &sessionPacket)
  1790. if err != nil {
  1791. return nil, errors.Trace(err)
  1792. }
  1793. return sessionPacket, nil
  1794. }
  1795. // Create an error that includes the current handshake state.
  1796. func (s *session) unexpectedStateError() error {
  1797. s.mutex.Lock()
  1798. defer s.mutex.Unlock()
  1799. return errors.Tracef("unexpected state: %v", s.state)
  1800. }