-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
637 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use std::env; | ||
|
||
#[allow(clippy::unusual_byte_groupings)] | ||
fn main() { | ||
if let Ok(version) = env::var("DEP_OPENSSL_VERSION_NUMBER") { | ||
let version = u64::from_str_radix(&version, 16).unwrap(); | ||
|
||
if version >= 0x1_00_02_00_0 { | ||
println!("cargo:rustc-cfg=ossl102"); | ||
} | ||
if version >= 0x1_01_01_00_0 { | ||
println!("cargo:rustc-cfg=ossl111"); | ||
} | ||
} | ||
} |
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,95 @@ | ||
use linked_hash_set::LinkedHashSet; | ||
#[cfg(ossl111)] | ||
use openssl::ssl::SslVersion; | ||
use openssl::ssl::{SslSession, SslSessionRef}; | ||
use std::borrow::Borrow; | ||
use std::collections::hash_map::Entry; | ||
use std::collections::HashMap; | ||
use std::hash::{Hash, Hasher}; | ||
|
||
#[derive(Hash, PartialEq, Eq, Clone)] | ||
pub struct SessionKey { | ||
pub host: String, | ||
pub port: u16, | ||
} | ||
|
||
#[derive(Clone)] | ||
struct HashSession(SslSession); | ||
|
||
impl PartialEq for HashSession { | ||
fn eq(&self, other: &HashSession) -> bool { | ||
self.0.id() == other.0.id() | ||
} | ||
} | ||
|
||
impl Eq for HashSession {} | ||
|
||
impl Hash for HashSession { | ||
fn hash<H>(&self, state: &mut H) | ||
where | ||
H: Hasher, | ||
{ | ||
self.0.id().hash(state) | ||
} | ||
} | ||
|
||
impl Borrow<[u8]> for HashSession { | ||
fn borrow(&self) -> &[u8] { | ||
self.0.id() | ||
} | ||
} | ||
|
||
pub struct SessionCache { | ||
sessions: HashMap<SessionKey, LinkedHashSet<HashSession>>, | ||
reverse: HashMap<HashSession, SessionKey>, | ||
} | ||
|
||
impl SessionCache { | ||
pub fn new() -> SessionCache { | ||
SessionCache { | ||
sessions: HashMap::new(), | ||
reverse: HashMap::new(), | ||
} | ||
} | ||
|
||
pub fn insert(&mut self, key: SessionKey, session: SslSession) { | ||
let session = HashSession(session); | ||
|
||
self.sessions | ||
.entry(key.clone()) | ||
.or_insert_with(LinkedHashSet::new) | ||
.insert(session.clone()); | ||
self.reverse.insert(session, key); | ||
} | ||
|
||
pub fn get(&mut self, key: &SessionKey) -> Option<SslSession> { | ||
let sessions = self.sessions.get_mut(key)?; | ||
let session = sessions.front().cloned()?.0; | ||
|
||
#[cfg(ossl111)] | ||
{ | ||
// https://tools.ietf.org/html/rfc8446#appendix-C.4 | ||
// OpenSSL will remove the session from its cache after the handshake completes anyway, but this ensures | ||
// that concurrent handshakes don't end up with the same session. | ||
if session.protocol_version() == SslVersion::TLS1_3 { | ||
self.remove(&session); | ||
} | ||
} | ||
|
||
Some(session) | ||
} | ||
|
||
pub fn remove(&mut self, session: &SslSessionRef) { | ||
let key = match self.reverse.remove(session.id()) { | ||
Some(key) => key, | ||
None => return, | ||
}; | ||
|
||
if let Entry::Occupied(mut sessions) = self.sessions.entry(key) { | ||
sessions.get_mut().remove(session.id()); | ||
if sessions.get().is_empty() { | ||
sessions.remove(); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.