Skip to content

Commit

Permalink
Optional legacy client support
Browse files Browse the repository at this point in the history
  • Loading branch information
sfackler committed Nov 18, 2023
1 parent cb44f81 commit 731b5ee
Show file tree
Hide file tree
Showing 6 changed files with 637 additions and 19 deletions.
27 changes: 26 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,36 @@ license = "MIT/Apache-2.0"
repository = "https://github.com/sfackler/hyper-openssl"
readme = "README.md"

[features]
default = []

client-legacy = [
"dep:http",
"dep:hyper-util",
"dep:linked_hash_set",
"dep:once_cell",
"dep:parking_lot",
"dep:pin-project",
"dep:tower-layer",
"dep:tower-service",
"hyper-util?/client-legacy",
]


[dependencies]
http = { version = "1.0.0", optional = true }
hyper = "1.0.1"
hyper-util = { version = "0.1", optional = true }
linked_hash_set = { version = "0.1", optional = true }
once_cell = { version = "1", optional = true }
openssl = "0.10.32"
openssl-sys = "0.9.26"
parking_lot = { version = "0.12", optional = true }
pin-project = { version = "1.1.3", optional = true }
tower-layer = { version = "0.3", optional = true }
tower-service = { version = "0.3", optional = true }

[dev-dependencies]
hyper = { version = "1", features = ["full"] }
hyper-util = { version = "0.1", features = ["tokio"] }
hyper-util = { version = "0.1", features = ["full"] }
tokio = { version = "1", features = ["full"] }
15 changes: 15 additions & 0 deletions build.rs
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");
}
}
}
95 changes: 95 additions & 0 deletions src/cache.rs
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();
}
}
}
}
Loading

0 comments on commit 731b5ee

Please sign in to comment.