diff --git a/Cargo.toml b/Cargo.toml index ba98aebdd6..a506ca2818 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -105,7 +105,7 @@ bdk_electrum = { version = "0.24.0", default-features = false, features = ["use- bdk_wallet = { version = "3.2.0", default-features = false, features = ["std", "keys-bip39"]} bitreq = { version = "0.3", default-features = false, features = ["async-https", "json-using-serde"] } -rustls = { version = "0.23", default-features = false } +rustls = { version = "0.23", default-features = false, features = ["ring"] } # rusqlite 0.40.0 and 0.40.1 do not compile on our MSRV. rusqlite = { version = ">=0.31.0, <0.40.0", features = ["bundled"], optional = true } bitcoin = "0.32.7" diff --git a/src/builder.rs b/src/builder.rs index 037bb88737..d0d0646289 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -2657,7 +2657,7 @@ fn build_with_store_internal( }) } -fn optionally_install_rustls_cryptoprovider() { +pub(crate) fn optionally_install_rustls_cryptoprovider() { // Acquire a global Mutex, ensuring that only one process at a time install the provider. This // is mostly required for running tests concurrently. static INIT_CRYPTO: Once = Once::new(); diff --git a/src/io/vss_store.rs b/src/io/vss_store.rs index df8ba60762..8f4f9093f6 100644 --- a/src/io/vss_store.rs +++ b/src/io/vss_store.rs @@ -44,6 +44,7 @@ use vss_client::util::retry::{ }; use vss_client::util::storable_builder::{EntropySource, StorableBuilder}; +use crate::builder::optionally_install_rustls_cryptoprovider; use crate::entropy::NodeEntropy; use crate::io::utils::check_namespace_key_validity; use crate::lnurl_auth::LNURL_AUTH_HARDENED_CHILD_INDEX; @@ -99,6 +100,10 @@ impl VssStore { base_url: String, store_id: String, vss_seed: [u8; 32], header_provider: Arc, ) -> io::Result { + // Setting up the store already issues requests to the VSS server, which requires a + // `CryptoProvider` to be available for TLS connections. + optionally_install_rustls_cryptoprovider(); + let next_version = AtomicU64::new(1); let internal_runtime = Arc::new(StoreRuntime::new("ldk-node-vss-runtime", INTERNAL_RUNTIME_WORKERS, "VSS")?); diff --git a/tests/vss_crypto_provider_tests.rs b/tests/vss_crypto_provider_tests.rs new file mode 100644 index 0000000000..a91ec85418 --- /dev/null +++ b/tests/vss_crypto_provider_tests.rs @@ -0,0 +1,58 @@ +// This file is Copyright its original authors, visible in version control history. +// +// This file is licensed under the Apache License, Version 2.0 or the MIT license , at your option. You may not use this file except in +// accordance with one or both of these licenses. + +#![cfg(feature = "storage-vss")] + +use std::collections::HashMap; +use std::io::{Read, Write}; +use std::net::TcpListener; + +use ldk_node::entropy::NodeEntropy; +use ldk_node::Builder; + +// This needs to remain the only test in this file: the `CryptoProvider` default is process-global, +// so any other test building a `Node` in the same process would install it for us. +#[test] +fn crypto_provider_is_installed_before_vss_store_setup() { + assert!(rustls::crypto::CryptoProvider::get_default().is_none()); + + // Serve a single response with an unsupported protocol version, which makes the VSS store + // setup fail without retrying. + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let vss_url = format!("http://{}", listener.local_addr().unwrap()); + let server = std::thread::spawn(move || { + let (mut stream, _) = listener.accept().unwrap(); + let mut request = Vec::new(); + while !request.windows(4).any(|w| w == b"\r\n\r\n") { + let mut buffer = [0; 1024]; + let bytes_read = stream.read(&mut buffer).unwrap(); + assert!(bytes_read > 0, "HTTP request ended before its headers"); + request.extend_from_slice(&buffer[..bytes_read]); + } + let response = + "HTTP/1.1 200 OK\r\ncontent-length: 0\r\nvss-protocol-version: 999\r\nconnection: close\r\n\r\n"; + stream.write_all(response.as_bytes()).unwrap(); + }); + + #[cfg(not(feature = "uniffi"))] + let node_entropy = NodeEntropy::from_seed_bytes([42u8; 64]); + #[cfg(feature = "uniffi")] + let node_entropy = std::sync::Arc::new(NodeEntropy::from_seed_bytes(vec![42u8; 64]).unwrap()); + let res = Builder::new().build_with_vss_store_and_fixed_headers( + node_entropy, + vss_url, + "store_id".to_owned(), + HashMap::new(), + ); + assert!(res.is_err()); + server.join().unwrap(); + + assert!( + rustls::crypto::CryptoProvider::get_default().is_some(), + "The CryptoProvider must be installed before the VSS store makes its first request" + ); +}