Commit 4c1fdf1d authored by Steven Fackler's avatar Steven Fackler
Browse files

Support ALPN on libressl

Closes #690
parent 440ede3b
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -2614,14 +2614,14 @@ extern "C" {
    ) -> *mut SSL_SESSION;
    pub fn i2d_SSL_SESSION(s: *mut SSL_SESSION, pp: *mut *mut c_uchar) -> c_int;

    #[cfg(ossl102)]
    #[cfg(any(ossl102, libressl261))]
    pub fn SSL_CTX_set_alpn_protos(s: *mut SSL_CTX, data: *const c_uchar, len: c_uint) -> c_int;

    #[cfg(ossl102)]
    #[cfg(any(ossl102, libressl261))]
    pub fn SSL_set_alpn_protos(s: *mut SSL, data: *const c_uchar, len: c_uint) -> c_int;

    // FIXME should take an Option<unsafe extern "C" fn>
    #[cfg(ossl102)]
    #[cfg(any(ossl102, libressl261))]
    pub fn SSL_CTX_set_alpn_select_cb(
        ssl: *mut SSL_CTX,
        cb: extern "C" fn(
@@ -2634,7 +2634,7 @@ extern "C" {
        ) -> c_int,
        arg: *mut c_void,
    );
    #[cfg(ossl102)]
    #[cfg(any(ossl102, libressl261))]
    pub fn SSL_get0_alpn_selected(s: *const SSL, data: *mut *const c_uchar, len: *mut c_uint);

    pub fn X509_add_ext(x: *mut X509, ext: *mut X509_EXTENSION, loc: c_int) -> c_int;
+6 −3
Original line number Diff line number Diff line
use ffi;
use foreign_types::ForeignType;
use foreign_types::ForeignTypeRef;
#[cfg(not(osslconf = "OPENSSL_NO_PSK"))]
use libc::c_char;
#[cfg(ossl111)]
use libc::size_t;
use libc::{c_char, c_int, c_uchar, c_uint, c_void};
use libc::{c_int, c_uchar, c_uint, c_void};
#[cfg(not(osslconf = "OPENSSL_NO_PSK"))]
use std::ffi::CStr;
use std::mem;
use std::ptr;
@@ -17,7 +20,7 @@ use dh::Dh;
use ec::EcKey;
use error::ErrorStack;
use pkey::Params;
#[cfg(ossl102)]
#[cfg(any(ossl102, libressl261))]
use ssl::AlpnError;
#[cfg(ossl111)]
use ssl::ExtensionContext;
@@ -130,7 +133,7 @@ where
    }
}

#[cfg(any(ossl102, ossl110))]
#[cfg(any(ossl102, libressl261))]
pub extern "C" fn raw_alpn_select<F>(
    ssl: *mut ffi::SSL,
    out: *mut *const c_uchar,
+10 −9
Original line number Diff line number Diff line
@@ -85,6 +85,7 @@ use error::ErrorStack;
use ex_data::Index;
#[cfg(ossl111)]
use hash::MessageDigest;
#[cfg(ossl110)]
use nid::Nid;
use pkey::{HasPrivate, PKeyRef, Params, Private};
use ssl::bio::BioMethod;
@@ -506,12 +507,12 @@ impl SslAlert {

/// An error returned from an ALPN selection callback.
///
/// Requires OpenSSL 1.0.2 or newer.
#[cfg(any(ossl102, ossl110))]
/// Requires OpenSSL 1.0.2 or LibreSSL 2.6.1 or newer.
#[cfg(any(ossl102, libressl261))]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct AlpnError(c_int);

#[cfg(any(ossl102, ossl110))]
#[cfg(any(ossl102, libressl261))]
impl AlpnError {
    /// Terminate the handshake with a fatal alert.
    ///
@@ -1109,10 +1110,10 @@ impl SslContextBuilder {
    ///
    /// This corresponds to [`SSL_CTX_set_alpn_protos`].
    ///
    /// Requires OpenSSL 1.0.2 or newer.
    /// Requires OpenSSL 1.0.2 or LibreSSL 2.6.1 or newer.
    ///
    /// [`SSL_CTX_set_alpn_protos`]: https://www.openssl.org/docs/man1.1.0/ssl/SSL_CTX_set_alpn_protos.html
    #[cfg(any(ossl102, ossl110))]
    #[cfg(any(ossl102, libressl261))]
    pub fn set_alpn_protos(&mut self, protocols: &[u8]) -> Result<(), ErrorStack> {
        unsafe {
            assert!(protocols.len() <= c_uint::max_value() as usize);
@@ -1140,12 +1141,12 @@ impl SslContextBuilder {
    ///
    /// This corresponds to [`SSL_CTX_set_alpn_select_cb`].
    ///
    /// Requires OpenSSL 1.0.2 or newer.
    /// Requires OpenSSL 1.0.2 or LibreSSL 2.6.1 or newer.
    ///
    /// [`SslContextBuilder::set_alpn_protos`]: struct.SslContextBuilder.html#method.set_alpn_protos
    /// [`select_next_proto`]: fn.select_next_proto.html
    /// [`SSL_CTX_set_alpn_select_cb`]: https://www.openssl.org/docs/man1.1.0/ssl/SSL_CTX_set_alpn_protos.html
    #[cfg(any(ossl102, ossl110))]
    #[cfg(any(ossl102, libressl261))]
    pub fn set_alpn_select_callback<F>(&mut self, callback: F)
    where
        F: for<'a> Fn(&mut SslRef, &'a [u8]) -> Result<&'a [u8], AlpnError> + 'static + Sync + Send,
@@ -2283,12 +2284,12 @@ impl SslRef {
    /// The protocol's name is returned is an opaque sequence of bytes. It is up to the client
    /// to interpret it.
    ///
    /// Requires OpenSSL 1.0.2 or newer.
    /// Requires OpenSSL 1.0.2 or LibreSSL 2.6.1 or newer.
    ///
    /// This corresponds to [`SSL_get0_alpn_selected`].
    ///
    /// [`SSL_get0_alpn_selected`]: https://www.openssl.org/docs/manmaster/man3/SSL_get0_next_proto_negotiated.html
    #[cfg(any(ossl102, ossl110))]
    #[cfg(any(ossl102, libressl261))]
    pub fn selected_alpn_protocol(&self) -> Option<&[u8]> {
        unsafe {
            let mut data: *const c_uchar = ptr::null();
+5 −5
Original line number Diff line number Diff line
@@ -481,7 +481,7 @@ fn test_state() {
/// Tests that connecting with the client using ALPN, but the server not does not
/// break the existing connection behavior.
#[test]
#[cfg(any(ossl102, ossl110))]
#[cfg(any(ossl102, libressl261))]
fn test_connect_with_unilateral_alpn() {
    let (_s, stream) = Server::new();
    let mut ctx = SslContext::builder(SslMethod::tls()).unwrap();
@@ -503,7 +503,7 @@ fn test_connect_with_unilateral_alpn() {
/// Tests that when both the client as well as the server use ALPN and their
/// lists of supported protocols have an overlap, the correct protocol is chosen.
#[test]
#[cfg(any(ossl102, ossl110))]
#[cfg(any(ossl102, libressl261))]
fn test_connect_with_alpn_successful_multiple_matching() {
    let (_s, stream) = Server::new_alpn();
    let mut ctx = SslContext::builder(SslMethod::tls()).unwrap();
@@ -526,7 +526,7 @@ fn test_connect_with_alpn_successful_multiple_matching() {
/// lists of supported protocols have an overlap -- with only ONE protocol
/// being valid for both.
#[test]
#[cfg(any(ossl102, ossl110))]
#[cfg(any(ossl102, libressl261))]
fn test_connect_with_alpn_successful_single_match() {
    let (_s, stream) = Server::new_alpn();
    let mut ctx = SslContext::builder(SslMethod::tls()).unwrap();
@@ -548,7 +548,7 @@ fn test_connect_with_alpn_successful_single_match() {
/// Tests that when the `SslStream` is created as a server stream, the protocols
/// are correctly advertised to the client.
#[test]
#[cfg(any(ossl102, ossl110))]
#[cfg(any(ossl102, libressl261))]
fn test_alpn_server_advertise_multiple() {
    let listener = TcpListener::bind("127.0.0.1:0").unwrap();
    let localhost = listener.local_addr().unwrap();
@@ -624,7 +624,7 @@ fn test_alpn_server_select_none_fatal() {
}

#[test]
#[cfg(any(ossl102, ossl110))]
#[cfg(any(ossl102, libressl261))]
fn test_alpn_server_select_none() {
    let listener = TcpListener::bind("127.0.0.1:0").unwrap();
    let localhost = listener.local_addr().unwrap();