Skip to content

Commit 3450259

Browse files
committed
Ensure we always startup with a rustls CryptoProvider
The `rustls` library recently introduced this weird behavior where they expect users to, apart from configuring the respective feature, also explictly call `CryptoProvider::install_default`. Otherwise they'd simply panic at runtime whenever the first network call requiring TLS would be made. While we already made a change upstream at `rust-electrum-client`, we also make sure here that we definitely, always, absolutley are sure that we have a `CryptoProvider` set on startup.
1 parent 54e1953 commit 3450259

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ bdk_electrum = { version = "0.23.0", default-features = false, features = ["use-
6767
bdk_wallet = { version = "2.0.0", default-features = false, features = ["std", "keys-bip39"]}
6868

6969
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"] }
70+
rustls = { version = "0.23", default-features = false }
7071
rusqlite = { version = "0.31.0", features = ["bundled"] }
7172
bitcoin = "0.32.4"
7273
bip39 = "2.0.0"

src/builder.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ use std::fmt;
7474
use std::fs;
7575
use std::path::PathBuf;
7676
use std::sync::atomic::AtomicBool;
77-
use std::sync::{Arc, Mutex, RwLock};
77+
use std::sync::{Arc, Mutex, Once, RwLock};
7878
use std::time::SystemTime;
7979
use vss_client::headers::{FixedHeaders, LnurlAuthToJwtProvider, VssHeaderProvider};
8080

@@ -936,6 +936,8 @@ fn build_with_store_internal(
936936
liquidity_source_config: Option<&LiquiditySourceConfig>, seed_bytes: [u8; 64],
937937
logger: Arc<Logger>, kv_store: Arc<DynStore>,
938938
) -> Result<Node, BuildError> {
939+
optionally_install_rustls_cryptoprovider();
940+
939941
if let Err(err) = may_announce_channel(&config) {
940942
if config.announcement_addresses.is_some() {
941943
log_error!(logger, "Announcement addresses were set but some required configuration options for node announcement are missing: {}", err);
@@ -1525,6 +1527,27 @@ fn build_with_store_internal(
15251527
})
15261528
}
15271529

1530+
fn optionally_install_rustls_cryptoprovider() {
1531+
// Acquire a global Mutex, ensuring that only one process at a time install the provider. This
1532+
// is mostly required for running tests concurrently.
1533+
static INIT_CRYPTO: Once = Once::new();
1534+
1535+
INIT_CRYPTO.call_once(|| {
1536+
// Ensure we always install a `CryptoProvider` for `rustls` if it was somehow not previously installed by now.
1537+
if rustls::crypto::CryptoProvider::get_default().is_none() {
1538+
let _ = rustls::crypto::aws_lc_rs::default_provider()
1539+
.install_default()
1540+
.or(rustls::crypto::ring::default_provider().install_default());
1541+
}
1542+
1543+
// Refuse to startup without TLS support. Better to catch it now than even later at runtime.
1544+
assert!(
1545+
rustls::crypto::CryptoProvider::get_default().is_some(),
1546+
"We need to have a CryptoProvider"
1547+
);
1548+
});
1549+
}
1550+
15281551
/// Sets up the node logger.
15291552
fn setup_logger(
15301553
log_writer_config: &Option<LogWriterConfig>, config: &Config,

0 commit comments

Comments
 (0)