Skip to content

Add support for linking against wolfSSL provided by wolfssl-sys #45

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

Open
wants to merge 4 commits into
base: main
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
4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
fail-fast: false
matrix:
crate: [ libcoap-sys, libcoap-rs ]
dtls_backend: [ openssl, gnutls, tinydtls, mbedtls ]
dtls_backend: [ openssl, gnutls, tinydtls, mbedtls, wolfssl ]
rust_version: [ msrv, stable, nightly ]
env:
LLVM_PROFILE_FILE: "${{ github.workspace }}/coverage-data/coverage/libcoap-rs-%p-%m.profraw"
Expand All @@ -40,10 +40,12 @@ jobs:
|| (matrix.crate == 'libcoap-rs' && matrix.dtls_backend == 'mbedtls' && 'tcp,dtls-psk,dtls-pki,dtls-mbedtls-sys')
|| (matrix.crate == 'libcoap-rs' && matrix.dtls_backend == 'openssl' && 'tcp,dtls-psk,dtls-pki,dtls-openssl-sys-vendored')
|| (matrix.crate == 'libcoap-rs' && matrix.dtls_backend == 'gnutls' && 'tcp,dtls-psk,dtls-pki,dtls-rpk')
|| (matrix.crate == 'libcoap-rs' && matrix.dtls_backend == 'wolfssl' && 'tcp,dtls-psk,dtls-pki,dtls-wolfssl-sys')
|| (matrix.crate == 'libcoap-sys' && matrix.dtls_backend == 'tinydtls' && 'dtls,dtls-tinydtls-sys-vendored')
|| (matrix.crate == 'libcoap-sys' && matrix.dtls_backend == 'mbedtls' && 'dtls,dtls-mbedtls-sys')
|| (matrix.crate == 'libcoap-sys' && matrix.dtls_backend == 'openssl' && 'dtls,dtls-openssl-sys-vendored')
|| (matrix.crate == 'libcoap-sys' && matrix.dtls_backend == 'gnutls' && 'dtls')
|| (matrix.crate == 'libcoap-sys' && matrix.dtls_backend == 'wolfssl' && 'dtls,dtls-wolfssl-sys')
|| 'vendored'
}}
steps:
Expand Down
9 changes: 7 additions & 2 deletions libcoap-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,13 @@ dtls-openssl-sys-vendored = ["dtls-openssl-sys", "openssl-sys/vendored"]
dtls-mbedtls-sys = ["dep:mbedtls-sys-auto"]
# Allows using the version of TinyDTLS provided by tinydtls-sys instead of a system-provided one.
# Note that this does not enforce the use of TinyDTLS in libcoap, see the crate-level documentation for more info.
dtls-tinydtls-sys = ["dep:tinydtls-sys", "tinydtls-sys/ecc", "tinydtls-sys/psk"]
dtls-tinydtls-sys = ["dep:tinydtls-sys"]
# Tell the tinydtls-sys version that is possibly used by libcoap-sys to use the vendored version of its library.
dtls-tinydtls-sys-vendored = ["dtls-tinydtls-sys", "tinydtls-sys/vendored"]
# Allows using the version of WolfSSL provided by wolfssl-sys instead of a system-provided one.
# Note that this does not enforce the use of WolfSSL in libcoap, see the crate-level documentation for more info.
dtls-wolfssl-sys = ["dep:wolfssl-sys"]


# Enabling this feature will allow libcoap-sys to be built with and statically linked to a vendored version of libcoap,
# This way, it is no longer required to have libcoap installed to use this crate.
Expand Down Expand Up @@ -143,7 +147,8 @@ dtls-rpk = ["dtls"]
[dependencies]
openssl-sys = { version = "^0.9.74", optional = true }
mbedtls-sys-auto = { version = "^2.26", optional = true }
tinydtls-sys = { version = "^0.2.0", default-features = false, optional = true }
wolfssl-sys = { version = "2.0.0", git = "https://github.com/namib-project/wolfssl-rs.git", branch = "add_sys_cargo_metadata", optional = true, features = ["aesccm", "hmac", "psk", "opensslall", "ex_data", "alpn", "dh"] }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll leave this comment here to make sure that we don't merge this until the required changes to wolfssl-sys have been upstreamed.

See expressvpn/wolfssl-rs#225.

tinydtls-sys = { version = "^0.2.0", default-features = false, optional = true, features = ["ecc", "psk"] }

[target.'cfg(target_os="espidf")'.dependencies]
esp-idf-sys = { version = "0.36.1" }
Expand Down
40 changes: 38 additions & 2 deletions libcoap-sys/build/build_system/vendored.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,17 @@
dtls_libraries_linked_by_other_crates |= DtlsBackend::MbedTls
}
}
#[cfg(feature = "dtls-wolfssl-sys")]
{
let (pkg_config_path, linked) = Self::configure_wolfssl_sys(build_config)?;
if let Some(pkg_config_path) = pkg_config_path {
additional_pkg_config_paths.push(pkg_config_path)
}
if linked {
dtls_libraries_linked_by_other_crates |= DtlsBackend::WolfSsl
}
}


// Add libcoap's own build directory to the PKG_CONFIG_PATH (might be used later on to
// find the generated .pc file to link against libcoap).
Expand Down Expand Up @@ -194,6 +205,8 @@
// If we do have a library already linked via a rust dependency, prefer those, but
// maintain the order also used in libcoap itself.
Some(DtlsBackend::OpenSsl)
} else if cfg!(feature = "dtls-wolfssl-sys") {
Some(DtlsBackend::WolfSsl)
} else if cfg!(feature = "dtls-mbedtls-sys") {
Some(DtlsBackend::MbedTls)
} else if cfg!(feature = "dtls-tinydtls-sys") {
Expand Down Expand Up @@ -237,14 +250,14 @@
} else {
// SAFETY: We are still single-threaded here.
unsafe { env::set_var("PKG_CONFIG_PATH", pkg_config_path_bak.unwrap_or_default()) }
println!("cargo:rustc-link-lib=static=coap-3");
println!(
"cargo:rustc-link-search={}",
libcoap_build_prefix
.join("lib")
.to_str()
.context("unable to convert OUT_DIR to a valid UTF-8 string.")?
);
println!("cargo:rustc-link-lib=static=coap-3");
Ok(Self {
out_dir,
define_info: None,
Expand Down Expand Up @@ -297,6 +310,29 @@
}
}

#[cfg(feature = "dtls-wolfssl-sys")]
fn configure_wolfssl_sys(build_config: &mut autotools::Config) -> Result<(Option<PathBuf>, bool)> {
if env::var_os("wolfSSL_CFLAGS").is_some() || env::var_os("wolfSSL_LIBS").is_some() {
// Do not use wolfssl-sys if the user manually set either the corresponding LIBS or
// CFLAGS variable.
// However, do warn the user that this might cause issues.
println!("cargo:warning=You have enabled the wolfssl-sys dependency, but have overridden either the wolfSSL_CFLAGS or wolfSSL_LIBS environment variable used by libcoap to find wolfSSL.");
println!("cargo:warning=Note that attempting to link more than one version of the same library at once may cause unexpected issues and/or cryptic compilation errors, especially if both versions are statically linked.");
Ok((None, false))
} else {
let wolfssl_root = env::var_os("DEP_WOLFSSL_ROOT")
.expect("wolfssl-sys dependency has been added, but DEP_WOLFSSL_ROOT has not been set");
let wolfssl_include = env::var_os("DEP_WOLFSSL_INCLUDE")
.expect("wolfssl-sys dependency has been added, but DEP_WOLFSSL_INCLUDE has not been set");

Check warning on line 326 in libcoap-sys/build/build_system/vendored.rs

View workflow job for this annotation

GitHub Actions / rustfmt

[rustfmt] libcoap-sys/build/build_system/vendored.rs#L326

Should be ` let wolfssl_libs = Path::new(wolfssl_root.as_os_str()).join("lib");`
Raw output
/home/runner/work/libcoap-rs/libcoap-rs/libcoap-sys/build/build_system/vendored.rs:326:0: warning: Should be `            let wolfssl_libs = Path::new(wolfssl_root.as_os_str()).join("lib");` ()
let wolfssl_libs = Path::new(wolfssl_root.as_os_str())

Check warning on line 327 in libcoap-sys/build/build_system/vendored.rs

View workflow job for this annotation

GitHub Actions / rustfmt

[rustfmt] libcoap-sys/build/build_system/vendored.rs#L327

Should be ``
Raw output
/home/runner/work/libcoap-rs/libcoap-rs/libcoap-sys/build/build_system/vendored.rs:327:0: warning: Should be `` ()
.join("lib");

// Set pkg-config path for version and library/include path determination.
Ok((Some(wolfssl_libs.join("pkgconfig")), true))
}
}


#[cfg(feature = "dtls-openssl-sys")]
fn configure_openssl_sys(_build_config: &mut autotools::Config) -> Result<(Option<PathBuf>, bool)> {
if env::var_os("OpenSSL_CFLAGS").is_some() || env::var_os("OpenSSL_LIBS").is_some() {
Expand All @@ -314,7 +350,7 @@
.context("DEP_OPENSSL_INCLUDE has no parent directory")?
.join("lib");

// Just add the OpenSSL directory to the PKG_CONFIG_PATH, that way libcoap will find it.
// Set pkg-config path for version and library/include path determination.
Ok((Some(openssl_libs.join("pkgconfig")), true))
}
}
Expand Down
2 changes: 1 addition & 1 deletion libcoap-sys/build/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn main() -> Result<()> {
println!("cargo::rustc-check-cfg=cfg(esp_idf_comp_espressif__coap_enabled)");
// Indicates the DTLS library crate that was linked against, if a library version vendored by
// another crate was used.
println!("cargo:rustc-check-cfg=cfg(used_dtls_crate, values(\"mbedtls\", \"tinydtls\", \"openssl\"))");
println!("cargo:rustc-check-cfg=cfg(used_dtls_crate, values(\"mbedtls\", \"tinydtls\", \"openssl\", \"wolfssl\"))");
// Indicates the DTLS backend used, if any.
println!("cargo:rustc-check-cfg=cfg(dtls_backend, values(\"mbedtls\", \"tinydtls\", \"openssl\", \"gnutls\", \"wolfssl\"))");
// The detected libcoap version, if any.
Expand Down
2 changes: 2 additions & 0 deletions libcoap-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,8 @@ use openssl_sys as _;
#[allow(unused_imports)]
#[cfg(used_dtls_crate = "tinydtls")]
use tinydtls_sys as _;
#[cfg(used_dtls_crate = "wolfssl")]
use wolfssl_sys as _;

// Add check whether the libcoap component is enabled when building for the ESP-IDF.
#[cfg(all(target_os = "espidf", not(esp_idf_comp_espressif__coap_enabled)))]
Expand Down
1 change: 1 addition & 0 deletions libcoap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ vendored = ["libcoap-sys/vendored"]
dtls-openssl-sys = ["libcoap-sys/dtls-openssl-sys"]
dtls-mbedtls-sys = ["libcoap-sys/dtls-mbedtls-sys"]
dtls-tinydtls-sys = ["libcoap-sys/dtls-tinydtls-sys"]
dtls-wolfssl-sys = ["libcoap-sys/dtls-wolfssl-sys"]
dtls-openssl-sys-vendored = ["libcoap-sys/dtls-openssl-sys-vendored"]
dtls-tinydtls-sys-vendored = ["libcoap-sys/dtls-tinydtls-sys-vendored"]

Expand Down
Loading