-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This implements `SSL_CTX_sess_set_cache_size`, `SSL_CTX_sess_get_cache_size` & `SSL_CTX_set_session_id_context`. TODO: need to implement `SSL_CTX_set_session_cache_mode`
- Loading branch information
Showing
5 changed files
with
152 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use std::sync::Arc; | ||
|
||
use rustls::client::ClientSessionMemoryCache; | ||
use rustls::client::ClientSessionStore; | ||
use rustls::server::ServerSessionMemoryCache; | ||
use rustls::server::{ProducesTickets, StoresServerSessions}; | ||
|
||
/// A container for session caches that can live inside | ||
/// an `SSL_CTX` but outlive a rustls `ServerConfig`/`ClientConfig` | ||
pub struct SessionCaches { | ||
size: usize, | ||
context: Vec<u8>, | ||
client: Option<Arc<dyn ClientSessionStore + Send + Sync>>, | ||
server: Option<Arc<dyn StoresServerSessions + Send + Sync>>, | ||
ticketer: Option<Arc<dyn ProducesTickets>>, | ||
} | ||
|
||
impl SessionCaches { | ||
pub fn with_size(size: usize) -> Self { | ||
// a user who has one `SSL_CTX` for both clients and servers will end | ||
// up with twice as many sessions as this, since rustls caches | ||
// client and server sessions separately. | ||
// | ||
// the common case is to have those separate (it is, for example, | ||
// impossible to configure certs/keys separately for client and | ||
// servers in a given `SSL_CTX`) so this should be ok. | ||
Self { | ||
size, | ||
context: vec![], | ||
client: None, | ||
server: None, | ||
ticketer: None, | ||
} | ||
} | ||
|
||
pub fn get_client(&mut self) -> Arc<dyn ClientSessionStore + Send + Sync> { | ||
Arc::clone( | ||
self.client | ||
.get_or_insert_with(|| Arc::new(ClientSessionMemoryCache::new(self.size))), | ||
) | ||
} | ||
|
||
pub fn get_server(&mut self) -> Arc<dyn StoresServerSessions + Send + Sync> { | ||
Arc::clone( | ||
self.server | ||
.get_or_insert_with(|| ServerSessionMemoryCache::new(self.size)), | ||
) | ||
} | ||
|
||
pub fn get_ticketer(&mut self) -> Arc<dyn ProducesTickets> { | ||
Arc::clone( | ||
self.ticketer | ||
.get_or_insert_with(|| crate::provider::Ticketer::new().unwrap()), | ||
) | ||
} | ||
|
||
pub fn set_context(&mut self, context: &[u8]) { | ||
// This is a different behaviour to that described by `SSL_set_session_id_context()`: | ||
// the context isn't bound to an individual session but instead is an epoch for | ||
// the entire set of stored sessions & ticketer keying. | ||
if context != self.context { | ||
context.clone_into(&mut self.context); | ||
self.client = None; | ||
self.server = None; | ||
self.ticketer = None; | ||
} | ||
} | ||
|
||
pub fn size(&self) -> usize { | ||
self.size | ||
} | ||
} | ||
|
||
impl Default for SessionCaches { | ||
fn default() -> Self { | ||
// this is SSL_SESSION_CACHE_MAX_SIZE_DEFAULT | ||
Self::with_size(1024 * 20) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters