xref: /aosp_15_r20/external/libwebsockets/READMEs/README.tls-sessions.md (revision 1c60b9aca93fdbc9b5f19b2d2194c91294b22281)
1*1c60b9acSAndroid Build Coastguard Worker# Using TLS Session resumption
2*1c60b9acSAndroid Build Coastguard Worker
3*1c60b9acSAndroid Build Coastguard WorkerLws supports clientside session caching and session resumption on both mbedtls
4*1c60b9acSAndroid Build Coastguard Workerand openssl-type tls library backends, to accellerate connection re-
5*1c60b9acSAndroid Build Coastguard Workerestablishment.
6*1c60b9acSAndroid Build Coastguard Worker
7*1c60b9acSAndroid Build Coastguard Worker## Background
8*1c60b9acSAndroid Build Coastguard Worker
9*1c60b9acSAndroid Build Coastguard WorkerTLS specifies logical "sessions" that get "established" on both sides when the
10*1c60b9acSAndroid Build Coastguard Workertls tunnel is negotiated... these are the object that gets validated by the
11*1c60b9acSAndroid Build Coastguard Workercertificate PKI.  They each have a server-unique "Session ID" of up to 32 bytes
12*1c60b9acSAndroid Build Coastguard Workereach.
13*1c60b9acSAndroid Build Coastguard Worker
14*1c60b9acSAndroid Build Coastguard WorkerNormally the default is that there is a new session negotiated per connection,
15*1c60b9acSAndroid Build Coastguard Workerso multiple connections to the same endpoint each negotiate fresh sessions from
16*1c60b9acSAndroid Build Coastguard Workerscratch.
17*1c60b9acSAndroid Build Coastguard Worker
18*1c60b9acSAndroid Build Coastguard WorkerHowever tls servers typically maintain a cache of recent sessions, and where
19*1c60b9acSAndroid Build Coastguard Workerboth the server and client still have a copy of a previously-negotiated session
20*1c60b9acSAndroid Build Coastguard Workeraround, support the client explicitly requesting additional connections binding
21*1c60b9acSAndroid Build Coastguard Workerto the old session by asking for it by its Session ID at negotiation time.
22*1c60b9acSAndroid Build Coastguard Worker
23*1c60b9acSAndroid Build Coastguard Worker### Re-use of validated sessions
24*1c60b9acSAndroid Build Coastguard Worker
25*1c60b9acSAndroid Build Coastguard WorkerThe advantage is that the timeconsuming key exchange part of the negotiation can
26*1c60b9acSAndroid Build Coastguard Workerbe skipped, and a connection-specific AES key agreed at both sides just by
27*1c60b9acSAndroid Build Coastguard Workerhashing on the secret held in the session object at each side.  This allows new
28*1c60b9acSAndroid Build Coastguard Workertunnels to be established much faster after the first, while the session from
29*1c60b9acSAndroid Build Coastguard Workerthe first is still valid and available at both sides.
30*1c60b9acSAndroid Build Coastguard Worker
31*1c60b9acSAndroid Build Coastguard WorkerBoth the server and client may apply their own lifetime restriction to their
32*1c60b9acSAndroid Build Coastguard Workercopy of the session, the first side to expire it will cause a new session to be
33*1c60b9acSAndroid Build Coastguard Workerforced at the next reuse attempt.  Lifetimes above 24h are not recommended by
34*1c60b9acSAndroid Build Coastguard WorkerRFC5246.
35*1c60b9acSAndroid Build Coastguard Worker
36*1c60b9acSAndroid Build Coastguard Worker### Multiple concurrent use of validated sessions
37*1c60b9acSAndroid Build Coastguard Worker
38*1c60b9acSAndroid Build Coastguard WorkerIn addition, the session's scope is any connection to the server that knows the
39*1c60b9acSAndroid Build Coastguard Workeroriginal session ID, because individual new AES keys are hashed from the session
40*1c60b9acSAndroid Build Coastguard Workersecret, multiple connections to the same endpoint can take advantage of a single
41*1c60b9acSAndroid Build Coastguard Workervalid session object.
42*1c60b9acSAndroid Build Coastguard Worker
43*1c60b9acSAndroid Build Coastguard Worker### Difference from Session Tickets
44*1c60b9acSAndroid Build Coastguard Worker
45*1c60b9acSAndroid Build Coastguard WorkerTLS also supports sessions as bearer tokens, but these are generally considered
46*1c60b9acSAndroid Build Coastguard Workeras degrading security.  Lws doesn't support Session Tickets, just reuse by
47*1c60b9acSAndroid Build Coastguard WorkerSession IDs.
48*1c60b9acSAndroid Build Coastguard Worker
49*1c60b9acSAndroid Build Coastguard Worker## Support in lws
50*1c60b9acSAndroid Build Coastguard Worker
51*1c60b9acSAndroid Build Coastguard WorkerServer-side TLS generally has session caching enabled by default.  For client
52*1c60b9acSAndroid Build Coastguard Workerside, lws now enables `LWS_WITH_TLS_SESSIONS` at cmake by default, which adds
53*1c60b9acSAndroid Build Coastguard Workera configurable tls session cache that is automatically kept updated with a
54*1c60b9acSAndroid Build Coastguard WorkerMRU-sorted list of established sessions.
55*1c60b9acSAndroid Build Coastguard Worker
56*1c60b9acSAndroid Build Coastguard WorkerIt's also possible to serialize sessions and save and load them, but this has to
57*1c60b9acSAndroid Build Coastguard Workerbe treated with caution.
58*1c60b9acSAndroid Build Coastguard Worker
59*1c60b9acSAndroid Build Coastguard WorkerFilling, expiring and consulting the session cache for client connections is
60*1c60b9acSAndroid Build Coastguard Workerperformed automatically.
61*1c60b9acSAndroid Build Coastguard Worker
62*1c60b9acSAndroid Build Coastguard Worker### tls library differences
63*1c60b9acSAndroid Build Coastguard Worker
64*1c60b9acSAndroid Build Coastguard WorkerMbedtls supports clientside session caching in lws, but it does not have a
65*1c60b9acSAndroid Build Coastguard Workersession message arrival callback to synchronize updating the client session
66*1c60b9acSAndroid Build Coastguard Workercache like openssl does.
67*1c60b9acSAndroid Build Coastguard Worker
68*1c60b9acSAndroid Build Coastguard WorkerSeparately, the session cb in boringssl is reportedly nonfunctional at the
69*1c60b9acSAndroid Build Coastguard Workermoment.
70*1c60b9acSAndroid Build Coastguard Worker
71*1c60b9acSAndroid Build Coastguard WorkerTo solve both cases, lws will schedule a check for the session at +500ms after
72*1c60b9acSAndroid Build Coastguard Workerthe tls negotiation completed, and for the case the connection doesn't last
73*1c60b9acSAndroid Build Coastguard Worker500ms or the server is slow issuing the message, also attempt to update the
74*1c60b9acSAndroid Build Coastguard Workercache at the time the tls connection object is closing.
75*1c60b9acSAndroid Build Coastguard Worker
76*1c60b9acSAndroid Build Coastguard Worker### Session namespacing in lws
77*1c60b9acSAndroid Build Coastguard Worker
78*1c60b9acSAndroid Build Coastguard WorkerInternally sessions are referred to by a vhostname.hostname.port tuple.
79*1c60b9acSAndroid Build Coastguard Worker
80*1c60b9acSAndroid Build Coastguard Worker### Configuring the clientside cache
81*1c60b9acSAndroid Build Coastguard Worker
82*1c60b9acSAndroid Build Coastguard WorkerSession caches in lws exist in and are bound to the vhost.  Different vhosts may
83*1c60b9acSAndroid Build Coastguard Workerprovide different authentication (eg, client certs) to the same endpoint that
84*1c60b9acSAndroid Build Coastguard Workeranother connection should not be able to take advantage of.
85*1c60b9acSAndroid Build Coastguard Worker
86*1c60b9acSAndroid Build Coastguard WorkerThe max size of this cache can be set at `.tls_session_cache_max` in the vhost
87*1c60b9acSAndroid Build Coastguard Workercreation info struct, if left at 0 then a default of 10 is applied.
88*1c60b9acSAndroid Build Coastguard Worker
89*1c60b9acSAndroid Build Coastguard WorkerThe Time-To-Live policy for sessions at the client can be set in seconds at
90*1c60b9acSAndroid Build Coastguard Worker`.tls_session_timeout`, by default whatever the tls library thinks it should be,
91*1c60b9acSAndroid Build Coastguard Workerperhaps 300s.
92*1c60b9acSAndroid Build Coastguard Worker
93*1c60b9acSAndroid Build Coastguard WorkerYou can disable session caching for a particular vhost by adding the vhost
94*1c60b9acSAndroid Build Coastguard Workeroption flag `LWS_SERVER_OPTION_DISABLE_TLS_SESSION_CACHE` to `.options` at
95*1c60b9acSAndroid Build Coastguard Workervhost creation time.
96*1c60b9acSAndroid Build Coastguard Worker
97*1c60b9acSAndroid Build Coastguard Worker### Session saving and loading
98*1c60b9acSAndroid Build Coastguard Worker
99*1c60b9acSAndroid Build Coastguard WorkerTrying to make sessions really persistent is supported but requires extra
100*1c60b9acSAndroid Build Coastguard Workercaution.  RFC5246 says
101*1c60b9acSAndroid Build Coastguard Worker
102*1c60b9acSAndroid Build Coastguard Worker   Applications that may be run in relatively insecure environments should not
103*1c60b9acSAndroid Build Coastguard Worker   write session IDs to stable storage.
104*1c60b9acSAndroid Build Coastguard Worker
105*1c60b9acSAndroid Build Coastguard WorkerThe issue is that while in process memory the session object is relatively
106*1c60b9acSAndroid Build Coastguard Workersecure compared to sensitive secrets and tls library data already in process
107*1c60b9acSAndroid Build Coastguard Workermemory.
108*1c60b9acSAndroid Build Coastguard Worker
109*1c60b9acSAndroid Build Coastguard WorkerBut when serialized to, eg, some external, unencrypted medium, the accessibility
110*1c60b9acSAndroid Build Coastguard Workerof what is basically a secret able to decrypt tls connections can become a
111*1c60b9acSAndroid Build Coastguard Workersecurity hazard.  It's left to the user to take any necessary steps to secure
112*1c60b9acSAndroid Build Coastguard Workersessions stored that way.
113*1c60b9acSAndroid Build Coastguard Worker
114*1c60b9acSAndroid Build Coastguard WorkerFor openssl, Public APIs are provided in `libwebsockets/lws-tls-sessions.h` to
115*1c60b9acSAndroid Build Coastguard Workerserialize any session in the cache associated with a vhost/host/port tuple, and
116*1c60b9acSAndroid Build Coastguard Workerto preload any available session into a vhost session cache by describing the
117*1c60b9acSAndroid Build Coastguard Workerendpoint hostname and port.
118*1c60b9acSAndroid Build Coastguard Worker
119*1c60b9acSAndroid Build Coastguard WorkerThe session saving and loading apis aren't supported for mbedtls yet.
120