Skip to content

Commit a27cd09

Browse files
committed
Use rustls_platform_verifier certificates.
Fix to always getting ` Websocket(Io(Custom { kind: InvalidData, error: InvalidCertificate(UnknownIssuer) }))` in IOS. As discussed in rustls/rustls-native-certs#3, ios and android support is broken by default. in this PR, I use `rustls_platform_verifier` on all platforms, but we could `cfg` it to only use `rustls_platform_verifier` on ios and android. Thoughts? For what its worth using `native-tls` instead of `rustls` also works. See 30b4db6 for an implementation using that. The advantage is that it requires no code changes, but I think it does pull in the whole native-tls stack due to feature unification.
1 parent b0dde30 commit a27cd09

File tree

4 files changed

+135
-10
lines changed

4 files changed

+135
-10
lines changed

Diff for: crates/Cargo.lock

+115-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: crates/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ jsonwebtoken = "9.2.0"
2828
pem = { version = "3.0.3", default-features = false }
2929
pin-project = "1.1.0"
3030
rand = "0.8.5"
31+
rustls-platform-verifier = "0.2.0"
32+
rustls = "0.22.4"
3133
serde = { version = "1.0.187", features = ["derive"] }
3234
serde_json = "1.0.97"
3335
thiserror = "1.0.40"

Diff for: crates/portal-client/Cargo.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ thiserror.workspace = true
1919
tracing.workspace = true
2020
tokio = { workspace = true, features = ["sync", "time", "macros", "rt"] }
2121
tokio-tungstenite.workspace = true
22-
tokio-util = {workspace = true, features = ["io"] }
22+
tokio-util = { workspace = true, features = ["io"] }
23+
rustls-platform-verifier.workspace = true
24+
rustls.workspace = true
2325
url.workspace = true
2426

2527
[dev-dependencies]

Diff for: crates/portal-client/src/lib.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,22 @@
33
use std::fmt::Debug;
44
use std::io::{self, ErrorKind};
55
use std::pin::Pin;
6-
use std::sync::OnceLock;
6+
use std::sync::{Arc, OnceLock};
77
use std::task::{Context, Poll};
88
use std::time::{Duration, Instant};
99

1010
use futures_util::{Sink, Stream, StreamExt as _};
1111
use matic_portal_types::{ControlMessage, Nexus};
1212
use pin_project::pin_project;
13+
use rustls::ClientConfig;
14+
use rustls_platform_verifier::Verifier;
1315
use tokio::net::TcpStream;
1416
use tokio_tungstenite::tungstenite::client::IntoClientRequest;
1517
use tokio_tungstenite::tungstenite::protocol::Message;
1618
use tokio_tungstenite::tungstenite::Error as WsError;
17-
use tokio_tungstenite::{connect_async, MaybeTlsStream, WebSocketStream};
19+
use tokio_tungstenite::{
20+
connect_async_tls_with_config, Connector, MaybeTlsStream, WebSocketStream,
21+
};
1822
use url::Url;
1923

2024
mod tunnel_io;
@@ -337,7 +341,15 @@ async fn websocket_connect(url: &str, token: &str) -> Result<TcpWebSocket, WsErr
337341
"authorization",
338342
format!("Bearer {}", token).parse().unwrap(),
339343
);
340-
let (websocket, http_response) = connect_async(request).await?;
344+
let config = Arc::new(
345+
ClientConfig::builder()
346+
.dangerous() // The `Verifier` we're using is actually safe
347+
.with_custom_certificate_verifier(Arc::new(Verifier::new()))
348+
.with_no_client_auth(),
349+
);
350+
let (websocket, http_response) =
351+
connect_async_tls_with_config(request, None, false, Some(Connector::Rustls(config)))
352+
.await?;
341353
tracing::debug!("got http response: {http_response:?}");
342354
Ok(websocket)
343355
}

0 commit comments

Comments
 (0)