Skip to content

Commit

Permalink
pelikan-net: multiple TLS/SSL implementations
Browse files Browse the repository at this point in the history
Refactors the pelikan-net crate to allow building without TLS/SSL,
or with BoringSSL and/or OpenSSL and TLS/SSL providers.
  • Loading branch information
brayniac committed Apr 4, 2024
1 parent f2f936b commit c0ea97d
Show file tree
Hide file tree
Showing 13 changed files with 1,412 additions and 458 deletions.
560 changes: 329 additions & 231 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ clap = "4.4.6"
clocksource = "0.8.1"
crossbeam-channel = "0.5.8"
datatier = { path = "./src/storage/datatier", version = "0.1.0"}
foreign-types-shared = "0.3.1"
httparse = "1.8.0"
libc = "0.2.149"
log = "0.4.20"
Expand All @@ -60,8 +59,10 @@ metriken = "0.3.3"
metrohash = "1.0.6"
mio = "0.8.8"
nom = "7.1.3"
openssl = "0.10.64"
openssl-sys = "0.9.102"
parking_lot = "0.12.1"
pelikan-net = { path = "./src/net", version = "0.1.0" }
pelikan-net = { path = "./src/net", version = "0.2.0" }
phf = "0.11.2"
proc-macro2 = "1.0.69"
quote = "1.0.33"
Expand Down
2 changes: 1 addition & 1 deletion src/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ license = { workspace = true }
boring = { workspace = true }
clocksource = { workspace = true }
metriken = { workspace = true }
pelikan-net = { workspace = true }
pelikan-net = { workspace = true, features = ["boringssl"] }
ringlog = { workspace = true }
serde = { workspace = true, features = ["derive"] }
2 changes: 1 addition & 1 deletion src/common/src/ssl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub trait TlsConfig {
/// `TlsTcpAcceptor` wrapped in an option, where the `None` variant indicates
/// that TLS should not be used.
pub fn tls_acceptor(config: &dyn TlsConfig) -> Result<Option<TlsTcpAcceptor>, IoError> {
let mut builder = TlsTcpAcceptor::mozilla_intermediate_v5()?;
let mut builder = TlsTcpAcceptor::builder();

// we use xor here to check if we have an under-specified tls configuration
if config.private_key().is_some()
Expand Down
16 changes: 12 additions & 4 deletions src/net/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@
name = "pelikan-net"
description = "Pelikan project's networking abstractions for non-blocking event loops"
authors = ["Brian Martin <[email protected]>"]
version = "0.1.0"
version = "0.2.0"

edition = { workspace = true }
homepage = { workspace = true }
repository = { workspace = true }
license = { workspace = true }

[dependencies]
boring = { workspace = true }
boring-sys = { workspace = true }
foreign-types-shared = { workspace = true }
boring = { workspace = true, optional = true }
boring-sys = { workspace = true, optional = true }
foreign-types-shared_03 = { package = "foreign-types-shared", version = "0.3.1" }
foreign-types-shared_01 = { package = "foreign-types-shared", version = "0.1.1" }
libc = { workspace = true }
metriken = { workspace = true }
mio = { workspace = true, features = ["os-poll", "net"] }
openssl = { workspace = true, optional = true }
openssl-sys = { workspace = true, optional = true }

[features]
default = ["boringssl"]
boringssl = ["boring", "boring-sys"]
openssl = ["dep:openssl", "openssl-sys", "openssl/vendored"]
3 changes: 3 additions & 0 deletions src/net/src/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub struct Connector {

enum ConnectorType {
Tcp(TcpConnector),
#[cfg(any(feature = "boringssl", feature = "openssl"))]
TlsTcp(TlsTcpConnector),
}

Expand All @@ -18,6 +19,7 @@ impl Connector {
pub fn connect<A: ToSocketAddrs>(&self, addr: A) -> Result<Stream> {
match &self.inner {
ConnectorType::Tcp(connector) => Ok(Stream::from(connector.connect(addr)?)),
#[cfg(any(feature = "boringssl", feature = "openssl"))]
ConnectorType::TlsTcp(connector) => Ok(Stream::from(connector.connect(addr)?)),
}
}
Expand All @@ -31,6 +33,7 @@ impl From<TcpConnector> for Connector {
}
}

#[cfg(any(feature = "boringssl", feature = "openssl"))]
impl From<TlsTcpConnector> for Connector {
fn from(other: TlsTcpConnector) -> Self {
Self {
Expand Down
6 changes: 4 additions & 2 deletions src/net/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ mod connector;
mod listener;
mod stream;
mod tcp;

#[cfg(any(feature = "boringssl", feature = "openssl"))]
mod tls_tcp;

pub use connector::*;
pub use listener::*;
pub use stream::*;
pub use tcp::*;

#[cfg(any(feature = "boringssl", feature = "openssl"))]
pub use tls_tcp::*;

pub mod event {
Expand All @@ -24,9 +28,7 @@ use core::fmt::Debug;
use core::ops::Deref;
use std::io::{Error, ErrorKind, Read, Write};
use std::net::{SocketAddr, ToSocketAddrs};
use std::path::{Path, PathBuf};

use foreign_types_shared::{ForeignType, ForeignTypeRef};
use metriken::*;

type Result<T> = std::io::Result<T>;
Expand Down
7 changes: 7 additions & 0 deletions src/net/src/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub struct Listener {

enum ListenerType {
Plain(TcpListener),
#[cfg(any(feature = "boringssl", feature = "openssl"))]
Tls((TcpListener, TlsTcpAcceptor)),
}

Expand All @@ -21,6 +22,7 @@ impl From<TcpListener> for Listener {
}
}

#[cfg(any(feature = "boringssl", feature = "openssl"))]
impl From<(TcpListener, TlsTcpAcceptor)> for Listener {
fn from(other: (TcpListener, TlsTcpAcceptor)) -> Self {
Self {
Expand Down Expand Up @@ -51,6 +53,7 @@ impl Listener {
let (stream, _addr) = listener.accept()?;
Ok(Stream::from(stream))
}
#[cfg(any(feature = "boringssl", feature = "openssl"))]
ListenerType::Tls((listener, acceptor)) => {
let (stream, _addr) = listener.accept()?;
let stream = acceptor.accept(stream)?;
Expand All @@ -62,6 +65,7 @@ impl Listener {
pub fn local_addr(&self) -> Result<SocketAddr> {
match &self.inner {
ListenerType::Plain(listener) => listener.local_addr(),
#[cfg(any(feature = "boringssl", feature = "openssl"))]
ListenerType::Tls((listener, _acceptor)) => listener.local_addr(),
}
}
Expand All @@ -76,6 +80,7 @@ impl event::Source for Listener {
) -> Result<()> {
match &mut self.inner {
ListenerType::Plain(listener) => listener.register(registry, token, interests),
#[cfg(any(feature = "boringssl", feature = "openssl"))]
ListenerType::Tls((listener, _acceptor)) => {
listener.register(registry, token, interests)
}
Expand All @@ -90,6 +95,7 @@ impl event::Source for Listener {
) -> Result<()> {
match &mut self.inner {
ListenerType::Plain(listener) => listener.reregister(registry, token, interests),
#[cfg(any(feature = "boringssl", feature = "openssl"))]
ListenerType::Tls((listener, _acceptor)) => {
listener.reregister(registry, token, interests)
}
Expand All @@ -99,6 +105,7 @@ impl event::Source for Listener {
fn deregister(&mut self, registry: &mio::Registry) -> Result<()> {
match &mut self.inner {
ListenerType::Plain(listener) => listener.deregister(registry),
#[cfg(any(feature = "boringssl", feature = "openssl"))]
ListenerType::Tls((listener, _acceptor)) => listener.deregister(registry),
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/net/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ impl AsRawFd for Stream {
fn as_raw_fd(&self) -> i32 {
match &self.inner {
StreamType::Tcp(s) => s.as_raw_fd(),

#[cfg(any(feature = "boringssl", feature = "openssl"))]
StreamType::TlsTcp(s) => s.as_raw_fd(),
}
}
Expand All @@ -35,41 +37,47 @@ impl Stream {
Interest::READABLE
}
}
#[cfg(any(feature = "boringssl", feature = "openssl"))]
StreamType::TlsTcp(s) => s.interest(),
}
}

pub fn is_established(&mut self) -> bool {
match &mut self.inner {
StreamType::Tcp(s) => s.is_established(),
#[cfg(any(feature = "boringssl", feature = "openssl"))]
StreamType::TlsTcp(s) => !s.is_handshaking(),
}
}

pub fn is_handshaking(&self) -> bool {
match &self.inner {
StreamType::Tcp(_) => false,
#[cfg(any(feature = "boringssl", feature = "openssl"))]
StreamType::TlsTcp(s) => s.is_handshaking(),
}
}

pub fn do_handshake(&mut self) -> Result<()> {
match &mut self.inner {
StreamType::Tcp(_) => Ok(()),
#[cfg(any(feature = "boringssl", feature = "openssl"))]
StreamType::TlsTcp(s) => s.do_handshake(),
}
}

pub fn set_nodelay(&mut self, nodelay: bool) -> Result<()> {
match &mut self.inner {
StreamType::Tcp(s) => s.set_nodelay(nodelay),
#[cfg(any(feature = "boringssl", feature = "openssl"))]
StreamType::TlsTcp(s) => s.set_nodelay(nodelay),
}
}

pub fn shutdown(&mut self) -> Result<bool> {
let result = match &mut self.inner {
StreamType::Tcp(s) => s.shutdown(Shutdown::Both).map(|_| true),
#[cfg(any(feature = "boringssl", feature = "openssl"))]
StreamType::TlsTcp(s) => s.shutdown().map(|v| v == ShutdownResult::Received),
};

Expand All @@ -92,6 +100,7 @@ impl Debug for Stream {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
match &self.inner {
StreamType::Tcp(s) => write!(f, "{s:?}"),
#[cfg(any(feature = "boringssl", feature = "openssl"))]
StreamType::TlsTcp(s) => write!(f, "{s:?}"),
}
}
Expand All @@ -105,6 +114,7 @@ impl From<TcpStream> for Stream {
}
}

#[cfg(any(feature = "boringssl", feature = "openssl"))]
impl From<TlsTcpStream> for Stream {
fn from(other: TlsTcpStream) -> Self {
Self {
Expand All @@ -117,6 +127,7 @@ impl Read for Stream {
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
match &mut self.inner {
StreamType::Tcp(s) => s.read(buf),
#[cfg(any(feature = "boringssl", feature = "openssl"))]
StreamType::TlsTcp(s) => s.read(buf),
}
}
Expand All @@ -126,13 +137,15 @@ impl Write for Stream {
fn write(&mut self, buf: &[u8]) -> Result<usize> {
match &mut self.inner {
StreamType::Tcp(s) => s.write(buf),
#[cfg(any(feature = "boringssl", feature = "openssl"))]
StreamType::TlsTcp(s) => s.write(buf),
}
}

fn flush(&mut self) -> Result<()> {
match &mut self.inner {
StreamType::Tcp(s) => s.flush(),
#[cfg(any(feature = "boringssl", feature = "openssl"))]
StreamType::TlsTcp(s) => s.flush(),
}
}
Expand All @@ -142,6 +155,7 @@ impl event::Source for Stream {
fn register(&mut self, registry: &Registry, token: Token, interest: Interest) -> Result<()> {
match &mut self.inner {
StreamType::Tcp(s) => s.register(registry, token, interest),
#[cfg(any(feature = "boringssl", feature = "openssl"))]
StreamType::TlsTcp(s) => s.register(registry, token, interest),
}
}
Expand All @@ -154,13 +168,15 @@ impl event::Source for Stream {
) -> Result<()> {
match &mut self.inner {
StreamType::Tcp(s) => s.reregister(registry, token, interest),
#[cfg(any(feature = "boringssl", feature = "openssl"))]
StreamType::TlsTcp(s) => s.reregister(registry, token, interest),
}
}

fn deregister(&mut self, registry: &mio::Registry) -> Result<()> {
match &mut self.inner {
StreamType::Tcp(s) => s.deregister(registry),
#[cfg(any(feature = "boringssl", feature = "openssl"))]
StreamType::TlsTcp(s) => s.deregister(registry),
}
}
Expand All @@ -171,5 +187,6 @@ impl event::Source for Stream {
/// efficient than using a trait for dynamic dispatch.
enum StreamType {
Tcp(TcpStream),
#[cfg(any(feature = "boringssl", feature = "openssl"))]
TlsTcp(TlsTcpStream),
}
2 changes: 0 additions & 2 deletions src/net/src/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
use crate::*;
use std::os::unix::prelude::FromRawFd;

pub use std::net::Shutdown;

#[derive(PartialEq)]
enum State {
Connecting,
Expand Down
Loading

0 comments on commit c0ea97d

Please sign in to comment.