Skip to content

Commit 9575c76

Browse files
authored
Merge pull request #21 from jacob-pro/main
Add OpenSSL support
2 parents 9e80a12 + 2ab0d17 commit 9575c76

File tree

8 files changed

+77
-11
lines changed

8 files changed

+77
-11
lines changed

.github/workflows/ci.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ jobs:
3939
- tokio-net
4040
- rustls
4141
- native-tls
42+
- openssl
4243
- rt
4344
- hyper-h1
4445
- hyper-h2
45-
- rustls,native-tls
46+
- rustls,native-tls,openssl
4647
- tokio-net,rt,rustls
4748
- tokio-net,native-tls
4849
steps:

CHANGELOG.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
## Upcoming 0.5.1
44

5+
### Added
6+
7+
- Support for [`openssl`](https://github.com/sfackler/rust-openssl)
8+
59
### Fixed
610

711
- Fixed compilation on non-unix environments, where tokio-net doesn't include unix sockets
812
- `SpawningHandshakes` will abort the tasks for pending connections when the linked futures are dropped. This should allow timeouts to cause the connectionto be closed.
913

10-
1114
## 0.5.0 - 2022-03-20
1215

1316
### Added
@@ -29,7 +32,7 @@
2932

3033
### Added
3134

32-
- Added [TlsListener::replace_acceptor()] function to allow replacing the listener certificate at runtime.
35+
- Added `TlsListener::replace_acceptor()` function to allow replacing the listener certificate at runtime.
3336

3437
## 0.4.1 - 2022-03-09
3538

Cargo.toml

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "tls-listener"
33
description = "wrap incoming Stream of connections in TLS"
4-
version = "0.5.0"
4+
version = "0.5.1"
55
authors = ["Thayne McCombs <[email protected]>"]
66
repository = "https://github.com/tmccombs/tls-listener"
77
edition = "2018"
@@ -11,6 +11,7 @@ license = "Apache-2.0"
1111
default = ["tokio-net"]
1212
rustls = ["tokio-rustls"]
1313
native-tls = ["tokio-native-tls"]
14+
openssl = ["tokio-openssl", "openssl_impl"]
1415
rt = ["tokio/rt"]
1516

1617
tokio-net = ["tokio/net"]
@@ -25,6 +26,8 @@ thiserror = "1.0.30"
2526
tokio = { version = "1.0", features = ["time"] }
2627
tokio-native-tls = { version = "0.3.0", optional = true }
2728
tokio-rustls = { version = "0.23.0", optional = true }
29+
tokio-openssl = { version = "0.6.3", optional = true }
30+
openssl_impl = { package = "openssl", version = "0.10.32", optional = true }
2831

2932
[dev-dependencies]
3033
hyper = { version = "0.14.1", features = ["http1", "stream"] }
@@ -56,5 +59,5 @@ path = "examples/http-low-level.rs"
5659
required-features = ["hyper-h1"]
5760

5861
[package.metadata.docs.rs]
59-
features = ["rustls", "native-tls", "hyper-h1", "hyper-h2"]
62+
features = ["rustls", "native-tls", "openssl", "hyper-h1", "hyper-h2", "rt"]
6063
rustdoc-args = ["--cfg", "docsrs"]

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ one of the `hyper-h1` or `hyper-h2` features).
1414

1515
See examples for examples of usage.
1616

17-
You must enable either one of the `rustls` or `native-tls` features depending on which implementation you would
18-
like to use.
17+
You must enable either one of the `rustls`, `native-tls`, or `openssl` features depending on which implementation you
18+
would like to use.

examples/echo.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,17 @@ use tls_listener::{AsyncAccept, TlsListener};
44
use tokio::io::{copy, split};
55
use tokio::net::{TcpListener, TcpStream};
66
use tokio::signal::ctrl_c;
7-
#[cfg(all(feature = "native-tls", not(feature = "rustls")))]
7+
8+
#[cfg(all(
9+
feature = "native-tls",
10+
not(any(feature = "rustls", feature = "openssl"))
11+
))]
812
use tokio_native_tls::TlsStream;
13+
#[cfg(all(
14+
feature = "openssl",
15+
not(any(feature = "rustls", feature = "native-tls"))
16+
))]
17+
use tokio_openssl::SslStream as TlsStream;
918
#[cfg(feature = "rustls")]
1019
use tokio_rustls::server::TlsStream;
1120

@@ -22,7 +31,7 @@ async fn handle_stream(stream: TlsStream<TcpStream>) {
2231
}
2332

2433
/// For example try opening and closing a connection with:
25-
/// `echo "Q" | openssl s_client -connect host:port`
34+
/// `echo "Q" | openssl s_client -connect localhost:3000`
2635
#[tokio::main(flavor = "current_thread")]
2736
async fn main() -> Result<(), Box<dyn std::error::Error>> {
2837
let addr: SocketAddr = ([127, 0, 0, 1], 3000).into();

examples/http-low-level.rs

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use std::convert::Infallible;
1111
mod tls_config;
1212
use tls_config::tls_acceptor;
1313

14+
/// For example try:
15+
/// `curl https://localhost:3000 -k`
1416
#[tokio::main(flavor = "current_thread")]
1517
async fn main() {
1618
let addr = ([127, 0, 0, 1], 3000).into();

examples/tls_config/mod.rs

+21-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ mod cert {
33
pub const CERT: &[u8] = include_bytes!("local.cert");
44
pub const PKEY: &[u8] = include_bytes!("local.key");
55
}
6-
#[cfg(feature = "native-tls")]
6+
#[cfg(all(feature = "native-tls", not(feature = "rustls")))]
77
const PFX: &[u8] = include_bytes!("local.pfx");
88

99
#[cfg(feature = "rustls")]
@@ -24,10 +24,29 @@ pub fn tls_acceptor() -> tokio_rustls::TlsAcceptor {
2424
.into()
2525
}
2626

27-
#[cfg(all(feature = "native-tls", not(feature = "rustls")))]
27+
#[cfg(all(
28+
feature = "native-tls",
29+
not(any(feature = "rustls", feature = "openssl"))
30+
))]
2831
pub fn tls_acceptor() -> tokio_native_tls::TlsAcceptor {
2932
use tokio_native_tls::native_tls::{Identity, TlsAcceptor};
3033

3134
let identity = Identity::from_pkcs12(PFX, "").unwrap();
3235
TlsAcceptor::builder(identity).build().unwrap().into()
3336
}
37+
38+
#[cfg(all(
39+
feature = "openssl",
40+
not(any(feature = "rustls", feature = "native-tls"))
41+
))]
42+
pub fn tls_acceptor() -> openssl_impl::ssl::SslContext {
43+
use openssl_impl::ssl::{SslContext, SslFiletype, SslMethod};
44+
let mut builder = SslContext::builder(SslMethod::tls_server()).unwrap();
45+
builder
46+
.set_certificate_file("./examples/tls_config/local.cert", SslFiletype::ASN1)
47+
.unwrap();
48+
builder
49+
.set_private_key_file("./examples/tls_config/local.key", SslFiletype::ASN1)
50+
.unwrap();
51+
builder.build()
52+
}

src/lib.rs

+29
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,35 @@ where
253253
}
254254
}
255255

256+
#[cfg(feature = "openssl")]
257+
impl<C> AsyncTls<C> for openssl_impl::ssl::SslContext
258+
where
259+
C: AsyncRead + AsyncWrite + Unpin + Send + 'static,
260+
{
261+
type Stream = tokio_openssl::SslStream<C>;
262+
type Error = openssl_impl::ssl::Error;
263+
type AcceptFuture = Pin<Box<dyn Future<Output = Result<Self::Stream, Self::Error>> + Send>>;
264+
265+
fn accept(&self, conn: C) -> Self::AcceptFuture {
266+
let ssl = match openssl_impl::ssl::Ssl::new(self) {
267+
Ok(s) => s,
268+
Err(e) => {
269+
return Box::pin(futures_util::future::err(e.into()));
270+
}
271+
};
272+
let mut stream = match tokio_openssl::SslStream::new(ssl, conn) {
273+
Ok(s) => s,
274+
Err(e) => {
275+
return Box::pin(futures_util::future::err(e.into()));
276+
}
277+
};
278+
Box::pin(async move {
279+
Pin::new(&mut stream).accept().await?;
280+
Ok(stream)
281+
})
282+
}
283+
}
284+
256285
impl<T> Builder<T> {
257286
/// Set the maximum number of concurrent handshakes.
258287
///

0 commit comments

Comments
 (0)