Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ALPN support to TlsAcceptor #229

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/imp/openssl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,29 @@ impl TlsAcceptor {
let mut acceptor = SslAcceptor::mozilla_intermediate(SslMethod::tls())?;
acceptor.set_private_key(&builder.identity.0.pkey)?;
acceptor.set_certificate(&builder.identity.0.cert)?;
#[cfg(feature = "alpn")]
if !builder.alpn.is_empty() {
use self::openssl::ssl::SslRef;
// Wire format is each alpn preceded by its length as a byte.
let mut alpn_wire_format = Vec::with_capacity(
builder
.alpn
.iter()
.map(|s| s.as_bytes().len())
.sum::<usize>()
+ builder.alpn.len(),
);
for alpn in builder.alpn.iter().map(|s| s.as_bytes()) {
alpn_wire_format.push(alpn.len() as u8);
alpn_wire_format.extend(alpn);
}
acceptor.set_alpn_protos(&alpn_wire_format)?;
// set uo ALPN selection routine - as select_next_proto
acceptor.set_alpn_select_callback(move |_: &mut SslRef, list: &[u8]| {
openssl::ssl::select_next_proto(&alpn_wire_format, list).ok_or(
openssl::ssl::AlpnError::NOACK)
});
}
for cert in builder.identity.0.chain.iter() {
// https://www.openssl.org/docs/manmaster/man3/SSL_CTX_add_extra_chain_cert.html
// specifies that "When sending a certificate chain, extra chain certificates are
Expand Down
14 changes: 14 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,8 @@ pub struct TlsAcceptorBuilder {
identity: Identity,
min_protocol: Option<Protocol>,
max_protocol: Option<Protocol>,
#[cfg(feature = "alpn")]
alpn: Vec<String>,
}

impl TlsAcceptorBuilder {
Expand All @@ -544,6 +546,16 @@ impl TlsAcceptorBuilder {
self.max_protocol = protocol;
self
}

/// Accept specific protocols through ALPN (Application-Layer Protocol Negotiation).
///
/// Defaults to empty.
#[cfg(feature = "alpn")]
#[cfg_attr(docsrs, doc(cfg(feature = "alpn")))]
pub fn accept_alpn(&mut self, protocols: &[&str]) -> &mut TlsAcceptorBuilder {
self.alpn = protocols.iter().map(|s| (*s).to_owned()).collect();
self
}

/// Creates a new `TlsAcceptor`.
pub fn build(&self) -> Result<TlsAcceptor> {
Expand Down Expand Up @@ -609,6 +621,8 @@ impl TlsAcceptor {
identity,
min_protocol: Some(Protocol::Tlsv10),
max_protocol: None,
#[cfg(feature = "alpn")]
alpn: vec![],
}
}

Expand Down