diff --git a/Cargo.lock b/Cargo.lock index 6097d75..499080a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -31,9 +31,9 @@ dependencies = [ [[package]] name = "autocfg" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" [[package]] name = "base64" @@ -49,15 +49,18 @@ checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" [[package]] name = "bytes" -version = "1.7.1" +version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8318a53db07bb3f8dca91a600466bdb3f2eaadeedfdbcf02e1accbad9271ba50" +checksum = "9ac0150caa2ae65ca5bd83f25c7de183dea78d4d366469f148435e2acfbad0da" [[package]] name = "cc" -version = "1.1.7" +version = "1.1.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26a5c3fd7bfa1ce3897a3a3501d362b2d87b7f2583ebcb4a949ec25911025cbc" +checksum = "40545c26d092346d8a8dab71ee48e7685a7a9cba76e634790c215b41a4a7b4cf" +dependencies = [ + "shlex", +] [[package]] name = "cesu8" @@ -93,9 +96,9 @@ dependencies = [ [[package]] name = "core-foundation-sys" -version = "0.8.6" +version = "0.8.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" [[package]] name = "env_logger" @@ -120,16 +123,18 @@ dependencies = [ [[package]] name = "jni" -version = "0.19.0" +version = "0.21.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c6df18c2e3db7e453d3c6ac5b3e9d5182664d28788126d39b91f2d1e22b017ec" +checksum = "1a87aa2bb7d2af34197c04845522473242e1aa17c12f4935d5856491a7fb8c97" dependencies = [ "cesu8", + "cfg-if", "combine", "jni-sys", "log", "thiserror", "walkdir", + "windows-sys 0.45.0", ] [[package]] @@ -140,9 +145,9 @@ checksum = "8eaf4bc02d17cbdd7ff4c7438cafcdf7fb9a4613313ad11b4f8fefe7d3fa0130" [[package]] name = "libc" -version = "0.2.155" +version = "0.2.162" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" +checksum = "18d287de67fe55fd7e1581fe933d965a5a9477b38e949cfa9f8574ef01506398" [[package]] name = "log" @@ -186,9 +191,9 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.19.0" +version = "1.20.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" +checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" [[package]] name = "openssl-probe" @@ -204,27 +209,27 @@ checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" [[package]] name = "proc-macro2" -version = "1.0.86" +version = "1.0.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +checksum = "f139b0662de085916d1fb67d2b4169d1addddda1919e696f3252b740b629986e" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.36" +version = "1.0.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" dependencies = [ "proc-macro2", ] [[package]] name = "regex" -version = "1.10.6" +version = "1.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4219d74c6b67a3654a9fbebc4b419e22126d13d2f3c4a07ee0cb61ff79a79619" +checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" dependencies = [ "aho-corasick", "memchr", @@ -234,9 +239,9 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.4.7" +version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" +checksum = "368758f23274712b504848e9d5a6f010445cc8b87a7cdb4d7cbee666c1288da3" dependencies = [ "aho-corasick", "memchr", @@ -245,9 +250,9 @@ dependencies = [ [[package]] name = "regex-syntax" -version = "0.8.4" +version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" [[package]] name = "ring" @@ -261,7 +266,7 @@ dependencies = [ "libc", "spin", "untrusted", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -280,9 +285,9 @@ dependencies = [ [[package]] name = "rustls-native-certs" -version = "0.7.1" +version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a88d6d420651b496bdd98684116959239430022a115c1240e6c3993be0b15fba" +checksum = "e5bfb394eeed242e909609f56089eecfe5fda225042e8b171791b9c95f5931e5" dependencies = [ "openssl-probe", "rustls-pemfile", @@ -293,11 +298,10 @@ dependencies = [ [[package]] name = "rustls-pemfile" -version = "2.1.3" +version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "196fe16b00e106300d3e45ecfcb764fa292a535d7326a29a5875c579c7417425" +checksum = "dce314e5fee3f39953d46bb63bb8a46d40c2f8fb7cc5a3b6cab2bde9721d6e50" dependencies = [ - "base64", "rustls-pki-types", ] @@ -326,7 +330,7 @@ dependencies = [ "security-framework", "security-framework-sys", "webpki-root-certs", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -355,11 +359,11 @@ dependencies = [ [[package]] name = "schannel" -version = "0.1.23" +version = "0.1.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fbc91545643bcf3a0bbb6569265615222618bdf33ce4ffbbd13c4bbd4c093534" +checksum = "01227be5826fa0690321a2ba6c5cd57a19cf3f6a09e76973b58e61de6ab9d1c1" dependencies = [ - "windows-sys", + "windows-sys 0.59.0", ] [[package]] @@ -378,14 +382,20 @@ dependencies = [ [[package]] name = "security-framework-sys" -version = "2.11.1" +version = "2.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75da29fe9b9b08fe9d6b22b5b4bcbc75d8db3aa31e639aa56bb62e9d46bfceaf" +checksum = "fa39c7303dc58b5543c94d22c1766b0d31f2ee58306363ea622b10bbc075eaa2" dependencies = [ "core-foundation-sys", "libc", ] +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + [[package]] name = "spin" version = "0.9.8" @@ -400,9 +410,9 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" [[package]] name = "syn" -version = "2.0.72" +version = "2.0.87" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc4b9b9bf2add8093d3f2c0204471e951b2285580335de42f9d2534f3ae7a8af" +checksum = "25aa4ce346d03a6dcd68dd8b4010bcb74e54e62c90c573f394c46eae99aba32d" dependencies = [ "proc-macro2", "quote", @@ -411,18 +421,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.63" +version = "1.0.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0342370b38b6a11b6cc11d6a805569958d54cfa061a29969c3b5ce2ea405724" +checksum = "02dd99dc800bbb97186339685293e1cc5d9df1f8fae2d0aecd9ff1c77efea892" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.63" +version = "1.0.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261" +checksum = "a7c61ec9a6f64d2793d8a45faba21efbe3ced62a886d44c36a009b2b519b4c7e" dependencies = [ "proc-macro2", "quote", @@ -431,9 +441,9 @@ dependencies = [ [[package]] name = "unicode-ident" -version = "1.0.12" +version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" [[package]] name = "untrusted" @@ -459,9 +469,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "webpki-root-certs" -version = "0.26.4" +version = "0.26.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7a7077dce97d094d1a558b1dad6a3baf1c5c3ba5b65ecb18c493ae00b955f9e" +checksum = "e8c6dfa3ac045bc517de14c7b1384298de1dbd229d38e08e169d9ae8c170937c" dependencies = [ "rustls-pki-types", ] @@ -472,7 +482,16 @@ version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" dependencies = [ - "windows-sys", + "windows-sys 0.59.0", +] + +[[package]] +name = "windows-sys" +version = "0.45.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" +dependencies = [ + "windows-targets 0.42.2", ] [[package]] @@ -481,7 +500,31 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-targets" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071" +dependencies = [ + "windows_aarch64_gnullvm 0.42.2", + "windows_aarch64_msvc 0.42.2", + "windows_i686_gnu 0.42.2", + "windows_i686_msvc 0.42.2", + "windows_x86_64_gnu 0.42.2", + "windows_x86_64_gnullvm 0.42.2", + "windows_x86_64_msvc 0.42.2", ] [[package]] @@ -490,28 +533,46 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", "windows_i686_gnullvm", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", ] +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" + [[package]] name = "windows_aarch64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" +[[package]] +name = "windows_aarch64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" + [[package]] name = "windows_aarch64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" +[[package]] +name = "windows_i686_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" + [[package]] name = "windows_i686_gnu" version = "0.52.6" @@ -524,24 +585,48 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" +[[package]] +name = "windows_i686_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" + [[package]] name = "windows_i686_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" +[[package]] +name = "windows_x86_64_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" + [[package]] name = "windows_x86_64_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" + [[package]] name = "windows_x86_64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" +[[package]] +name = "windows_x86_64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" + [[package]] name = "windows_x86_64_msvc" version = "0.52.6" diff --git a/rustls-platform-verifier/Cargo.toml b/rustls-platform-verifier/Cargo.toml index be57904..39ff2d3 100644 --- a/rustls-platform-verifier/Cargo.toml +++ b/rustls-platform-verifier/Cargo.toml @@ -32,7 +32,7 @@ docsrs = ["jni"] rustls = { version = "0.23.16", default-features = false, features = ["std"] } log = { version = "0.4" } base64 = { version = "0.22", optional = true } # Only used when the `cert-logging` feature is enabled. -jni = { version = "0.19", default-features = false, optional = true } # Only used during doc generation +jni = { version = "0.21", default-features = false, optional = true } # Only used during doc generation once_cell = "1.9" paste = { version = "1.0", default-features = false, optional = true } # Only used when `ffi-testing` feature is enabled @@ -42,7 +42,7 @@ webpki = { package = "rustls-webpki", version = "0.102", default-features = fals [target.'cfg(target_os = "android")'.dependencies] rustls-platform-verifier-android = { path = "../android-release-support", version = "0.1.0" } -jni = { version = "0.19", default-features = false } +jni = { version = "0.21", default-features = false } webpki = { package = "rustls-webpki", version = "0.102", default-features = false } android_logger = { version = "0.13", optional = true } # Only used during testing. diff --git a/rustls-platform-verifier/src/android.rs b/rustls-platform-verifier/src/android.rs index 334ff4c..21a6a69 100644 --- a/rustls-platform-verifier/src/android.rs +++ b/rustls-platform-verifier/src/android.rs @@ -55,9 +55,9 @@ impl Global { }; Ok(Context { + context: env.new_global_ref(context)?, + loader: env.new_global_ref(loader)?, env, - context: JObject::from(context), - loader: JObject::from(loader), }) } } @@ -74,10 +74,10 @@ fn global() -> &'static Global { /// nothing else in your application needs access the Android runtime. /// /// Initialization must be done before any verification is attempted. -pub fn init_hosted(env: &JNIEnv, context: JObject) -> Result<(), JNIError> { +pub fn init_hosted(env: &mut JNIEnv, context: JObject) -> Result<(), JNIError> { GLOBAL.get_or_try_init(|| -> Result<_, JNIError> { let loader = - env.call_method(context, "getClassLoader", "()Ljava/lang/ClassLoader;", &[])?; + env.call_method(&context, "getClassLoader", "()Ljava/lang/ClassLoader;", &[])?; let global = Global::Internal { java_vm: env.get_java_vm()?, context: env.new_global_ref(context)?, @@ -126,14 +126,14 @@ impl From for Error { pub(super) struct Context<'a> { env: JNIEnv<'a>, - context: JObject<'a>, - loader: JObject<'a>, + context: GlobalRef, + loader: GlobalRef, } impl<'a> Context<'a> { /// Borrow a reference to the JNI Environment executing the Android application - pub(super) fn env(&self) -> &JNIEnv<'a> { - &self.env + pub(super) fn env(&mut self) -> &mut JNIEnv<'a> { + &mut self.env } /// Borrow the `applicationContext` from the Android application @@ -146,14 +146,13 @@ impl<'a> Context<'a> { /// /// This should be used instead of `JNIEnv::find_class` to ensure all classes /// in the application can be found. - pub(super) fn load_class(&self, name: &str) -> Result, Error> { - let env = self.env(); - let name = env.new_string(name)?; - let class = env.call_method( - self.loader, + pub(super) fn load_class(&mut self, name: &str) -> Result, Error> { + let name = self.env.new_string(name)?; + let class = self.env.call_method( + &self.loader, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;", - &[JValue::from(name)], + &[JValue::from(&name)], )?; Ok(JObject::try_from(class)?.into()) @@ -165,19 +164,14 @@ impl<'a> Context<'a> { /// are cleared. pub(super) fn with_context(f: F) -> Result where - F: FnOnce(&Context) -> Result, + F: FnOnce(&mut Context, &mut JNIEnv) -> Result, { - let context = global().context()?; - let env = context.env(); + let mut context = global().context()?; + let mut binding = global().context()?; + let env = binding.env(); // 16 is the default capacity in the JVM, we can make this configurable if necessary - env.push_local_frame(16)?; - - let res = f(&context); - - env.pop_local_frame(JObject::null())?; - - res + env.with_local_frame(16, |env| f(&mut context, env)) } /// Loads and caches a class on first use @@ -196,13 +190,13 @@ impl CachedClass { } /// Gets the cached class reference, loaded on first use - pub(super) fn get<'a: 'b, 'b>(&'a self, cx: &Context<'b>) -> Result, Error> { + pub(super) fn get<'a: 'b, 'b>(&'a self, cx: &mut Context<'b>) -> Result<&JClass<'b>, Error> { let class = self.class.get_or_try_init(|| -> Result<_, Error> { let class = cx.load_class(self.name)?; Ok(cx.env().new_global_ref(class)?) })?; - Ok(JClass::from(class.as_obj())) + Ok(class.as_obj().into()) } } diff --git a/rustls-platform-verifier/src/tests/ffi.rs b/rustls-platform-verifier/src/tests/ffi.rs index f66b49d..9030cc3 100644 --- a/rustls-platform-verifier/src/tests/ffi.rs +++ b/rustls-platform-verifier/src/tests/ffi.rs @@ -29,7 +29,7 @@ mod android { const SUCCESS_MARKER: &str = "success"; fn run_android_test<'a>( - env: &'a JNIEnv, + env: &'a mut JNIEnv, cx: JObject, suite_name: &'static str, test_cases: &'static [fn()], @@ -86,53 +86,53 @@ mod android { #[export_name = "Java_org_rustls_platformverifier_CertificateVerifierTests_mockTests"] pub extern "C" fn rustls_platform_verifier_mock_test_suite( - env: JNIEnv, + mut env: JNIEnv, _class: JClass, cx: JObject, ) -> jstring { log::info!("running mock test suite..."); run_android_test( - &env, + &mut env, cx, "mock tests", tests::verification_mock::ALL_TEST_CASES, ) - .into_inner() + .into_raw() } #[export_name = "Java_org_rustls_platformverifier_CertificateVerifierTests_verifyMockRootUsage"] pub extern "C" fn rustls_platform_verifier_verify_mock_root_usage( - env: JNIEnv, + mut env: JNIEnv, _class: JClass, cx: JObject, ) -> jstring { log::info!("verifying mock roots are not used by default..."); run_android_test( - &env, + &mut env, cx, "mock root verification", &[tests::verification_mock::verification_without_mock_root], ) - .into_inner() + .into_raw() } #[export_name = "Java_org_rustls_platformverifier_CertificateVerifierTests_realWorldTests"] pub extern "C" fn rustls_platform_verifier_real_world_test_suite( - env: JNIEnv, + mut env: JNIEnv, _class: JClass, cx: JObject, ) -> jstring { log::info!("running real world suite..."); run_android_test( - &env, + &mut env, cx, "real world", tests::verification_real_world::ALL_TEST_CASES, ) - .into_inner() + .into_raw() } } diff --git a/rustls-platform-verifier/src/verification/android.rs b/rustls-platform-verifier/src/verification/android.rs index bcf9772..c16e8cf 100644 --- a/rustls-platform-verifier/src/verification/android.rs +++ b/rustls-platform-verifier/src/verification/android.rs @@ -59,8 +59,7 @@ impl Default for Verifier { #[cfg(any(test, feature = "ffi-testing"))] impl Drop for Verifier { fn drop(&mut self) { - with_context::<_, ()>(|cx| { - let env = cx.env(); + with_context::<_, ()>(|cx, env| { env.call_static_method(CERT_VERIFIER_CLASS.get(cx)?, "clearMockRoots", "()V", &[])? .v()?; Ok(()) @@ -113,8 +112,7 @@ impl Verifier { .try_into() .map_err(|_| TlsError::FailedToGetCurrentTime)?; - let verification_result = with_context(|cx| { - let env = cx.env(); + let verification_result = with_context(|cx, env| { // We don't provide an initial element so that the array filling can be cleaner. // It's valid to provide a `null` value. Ref: https://docs.oracle.com/en/java/javase/13/docs/specs/jni/functions.html -> NewObjectArray let cert_list = { @@ -127,7 +125,7 @@ impl Verifier { for (idx, cert) in certificate_chain { let idx = idx.try_into().unwrap(); let cert_buffer = env.byte_array_from_slice(cert)?; - env.set_object_array_element(array, idx, cert_buffer)? + env.set_object_array_element(&array, idx, cert_buffer)? } array @@ -143,17 +141,16 @@ impl Verifier { for (idx, eku) in ALLOWED_EKUS.iter().enumerate() { let idx = idx.try_into().unwrap(); let eku = env.new_string(eku)?; - env.set_object_array_element(array, idx, eku)?; + env.set_object_array_element(&array, idx, eku)?; } array }; - let ocsp_response = ocsp_response - .map(|b| env.byte_array_from_slice(b)) - .transpose()? - .map(JObject::from) - .unwrap_or_else(JObject::null); + let ocsp_response = match ocsp_response { + Some(b) => env.byte_array_from_slice(b)?, + None => JObject::null().into(), + }; #[cfg(any(test, feature = "ffi-testing"))] { @@ -163,7 +160,7 @@ impl Verifier { CERT_VERIFIER_CLASS.get(cx)?, "addMockRoot", "([B)V", - &[JValue::from(mock_root)], + &[JValue::from(&mock_root)], )? .v() .expect("failed to add test root") @@ -189,13 +186,13 @@ impl Verifier { "verifyCertificateChain", VERIFIER_CALL, &[ - JValue::from(*cx.application_context()), - JValue::from(env.new_string(server_name.to_str())?), - JValue::from(env.new_string(AUTH_TYPE)?), - JValue::from(JObject::from(allowed_ekus)), - JValue::from(ocsp_response), + JValue::from(cx.application_context()), + JValue::from(&env.new_string(server_name.to_str())?), + JValue::from(&env.new_string(AUTH_TYPE)?), + JValue::from(&JObject::from(allowed_ekus)), + JValue::from(&ocsp_response), JValue::Long(now), - JValue::from(JObject::from(cert_list)), + JValue::from(&JObject::from(cert_list)), ], )? .l()?; @@ -245,9 +242,12 @@ impl Verifier { } } -fn extract_result_info(env: &JNIEnv<'_>, result: JObject<'_>) -> (VerifierStatus, Option) { +fn extract_result_info( + env: &mut JNIEnv<'_>, + result: JObject<'_>, +) -> (VerifierStatus, Option) { let status_code = env - .get_field(result, "code", "I") + .get_field(&result, "code", "I") .and_then(|code| code.i()) .unwrap(); @@ -267,10 +267,9 @@ fn extract_result_info(env: &JNIEnv<'_>, result: JObject<'_>) -> (VerifierStatus .get_field(result, "message", "Ljava/lang/String;") .and_then(|m| m.l()) .map(|o| (!o.is_null()).then_some(o)) - .and_then(|s| s.map(|s| JavaStr::from_env(env, s.into())).transpose()) + .and_then(|s| Ok(JavaStr::from_env(env, &s.unwrap().into()).ok().map(String::from))) .unwrap(); - - (status, msg.map(String::from)) + (status, msg) } impl ServerCertVerifier for Verifier {