diff --git a/quest/a1/android-verifier-init.md b/quest/a1/android-verifier-init.md deleted file mode 100644 index 3d58ce31..00000000 --- a/quest/a1/android-verifier-init.md +++ /dev/null @@ -1,9 +0,0 @@ -# [S] Share one Android platform-verifier initializer - -## Goal - -`rs/web-transport-ffi/src/android.rs`, `rs/web-transport-quinn/src/android.rs` and `rs/web-transport-noq/src/android.rs` each find the process `JavaVM` and the current `Application` to initialize rustls-platform-verifier. The lookup lives in one place, so a fix to it reaches every Android client. - -## Plan - -Each crate keeps its own policy: the quinn and noq builders return an error when initialization fails, while the FFI retries and falls back to bundled roots until it succeeds. Only the JNI lookup and initialization call are shared. Recommended home: `web-transport-quinn`'s helper, exposed only on Android and only to the FFI (which already depends on it). `web-transport-noq` cannot depend on quinn, so if no shared crate fits without adding one just for this, keep noq's copy and say why in the PR. Verify with clippy for `aarch64-linux-android` on all three crates, as [#512](https://github.com/moq-dev/web-transport/pull/512) did. diff --git a/rs/web-transport-ffi/src/android.rs b/rs/web-transport-ffi/src/android.rs index 6d1eda35..97d1cc3e 100644 --- a/rs/web-transport-ffi/src/android.rs +++ b/rs/web-transport-ffi/src/android.rs @@ -7,23 +7,24 @@ //! 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 } @@ -31,13 +32,11 @@ pub extern "system" fn JNI_OnLoad(vm: *mut sys::JavaVM, _reserved: *mut c_void) pub(crate) fn verifier( provider: Arc, ) -> Result, 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(), }; @@ -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) -} diff --git a/rs/web-transport-noq/src/android.rs b/rs/web-transport-noq/src/android.rs index faa9b019..d26fd46a 100644 --- a/rs/web-transport-noq/src/android.rs +++ b/rs/web-transport-noq/src/android.rs @@ -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}; diff --git a/rs/web-transport-quinn/src/android.rs b/rs/web-transport-quinn/src/android.rs index faa9b019..ba70fe52 100644 --- a/rs/web-transport-quinn/src/android.rs +++ b/rs/web-transport-quinn/src/android.rs @@ -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(()); } diff --git a/rs/web-transport-quinn/src/lib.rs b/rs/web-transport-quinn/src/lib.rs index 2d57f19f..6b58b474 100644 --- a/rs/web-transport-quinn/src/lib.rs +++ b/rs/web-transport-quinn/src/lib.rs @@ -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;