Skip to content
Merged
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
9 changes: 0 additions & 9 deletions quest/a1/android-verifier-init.md

This file was deleted.

62 changes: 11 additions & 51 deletions rs/web-transport-ffi/src/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,36 @@
//! trust the bundled Mozilla roots from webpki-roots.

use std::ffi::c_void;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;

use jni::sys::{self, jint, JNI_VERSION_1_6};
use jni::{jni_sig, jni_str, Env, JavaVM};
use jni::JavaVM;
use rustls::client::danger::ServerCertVerifier;

static INITIALIZED: AtomicBool = AtomicBool::new(false);
use web_transport_quinn::android::init_platform_verifier;

/// Called by `System.loadLibrary`; JNA loads skip it, so the per-client retry
/// also needs the `JavaVM` registered here.
/// also needs the `JavaVM` singleton registered here.
#[no_mangle]
#[allow(non_snake_case)]
pub extern "system" fn JNI_OnLoad(vm: *mut sys::JavaVM, _reserved: *mut c_void) -> jint {
// SAFETY: the JVM passes `JNI_OnLoad` a valid `JavaVM` pointer.
let vm = unsafe { JavaVM::from_raw(vm) };
init(&vm);
unsafe { JavaVM::from_raw(vm) };
// A failure leaves no pending Java exception, so loading the library never fails.
if let Err(err) = init_platform_verifier() {
tracing::warn!(%err, "failed to initialize the Android platform verifier");
}
JNI_VERSION_1_6
}

/// The verifier for default verification on Android.
pub(crate) fn verifier(
provider: Arc<rustls::crypto::CryptoProvider>,
) -> Result<Arc<dyn ServerCertVerifier>, rustls::Error> {
let ready =
INITIALIZED.load(Ordering::Acquire) || JavaVM::singleton().is_ok_and(|vm| init(&vm));
if ready {
return Ok(Arc::new(rustls_platform_verifier::Verifier::new(provider)?));
match init_platform_verifier() {
Ok(()) => return Ok(Arc::new(rustls_platform_verifier::Verifier::new(provider)?)),
Err(err) => tracing::warn!(%err, "trusting bundled webpki roots"),
}

tracing::warn!("Android platform verifier is not initialized; trusting bundled webpki roots");
let roots = rustls::RootCertStore {
roots: webpki_roots::TLS_SERVER_ROOTS.to_vec(),
};
Expand All @@ -47,42 +46,3 @@ pub(crate) fn verifier(
.map_err(|e| rustls::Error::General(e.to_string()))?;
Ok(verifier)
}

/// Initialize the platform verifier, returning whether it is ready.
///
/// A failure leaves no pending Java exception: `attach_current_thread` catches
/// and clears any thrown by the callback, so loading the library never fails.
fn init(vm: &JavaVM) -> bool {
if INITIALIZED.load(Ordering::Acquire) {
return true;
}
match vm.attach_current_thread(init_with_application) {
Ok(()) => {
INITIALIZED.store(true, Ordering::Release);
true
}
Err(err) => {
tracing::warn!(%err, "failed to initialize the Android platform verifier");
false
}
}
}

fn init_with_application(env: &mut Env) -> Result<(), jni::errors::Error> {
// Native code isn't handed the Context, so use the long-stable
// ActivityThread.currentApplication(), which is null until the Application exists.
let app = env
.call_static_method(
jni_str!("android/app/ActivityThread"),
jni_str!("currentApplication"),
jni_sig!("()Landroid/app/Application;"),
&[],
)?
.l()?;
if app.is_null() {
return Err(jni::errors::Error::NullPtr(
"ActivityThread.currentApplication()",
));
}
rustls_platform_verifier::android::init_with_env(env, app)
}
3 changes: 3 additions & 0 deletions rs/web-transport-noq/src/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
//! panics during verification if it was never given the application `Context`.
//! It does not expose whether it is initialized, so [`init_platform_verifier`]
//! initializes it idempotently and returns an error when that is impossible.
//!
//! A copy of web-transport-quinn's `android` module, which web-transport-ffi
//! shares; this crate cannot depend on web-transport-quinn.

use std::sync::atomic::{AtomicBool, Ordering};

Expand Down
2 changes: 1 addition & 1 deletion rs/web-transport-quinn/src/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ static READY: AtomicBool = AtomicBool::new(false);

/// Ensure the platform verifier is initialized, initializing it from the
/// application `Context` when the application has not.
pub(crate) fn init_platform_verifier() -> Result<(), rustls::Error> {
pub fn init_platform_verifier() -> Result<(), rustls::Error> {
if READY.load(Ordering::Acquire) {
return Ok(());
}
Expand Down
4 changes: 3 additions & 1 deletion rs/web-transport-quinn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
//! If you want to support multiple WebTransport sessions over the same QUIC connection... you should just dial a new QUIC connection instead.

// External
// Public only so web-transport-ffi shares the JNI lookup; not a supported API.
#[cfg(target_os = "android")]
mod android;
#[doc(hidden)]
pub mod android;
mod client;
mod error;
mod op;
Expand Down
Loading