Skip to content

Commit

Permalink
mobile: Add namespace to jni_interface.(h|cc) (envoyproxy#30697)
Browse files Browse the repository at this point in the history
There is no reason why `jni_interface.(h|cc)` cannot have a namespace
since it's just a utility and does not to use `extern "C"`.

Signed-off-by: Fredy Wijaya <[email protected]>
  • Loading branch information
fredyw authored Nov 2, 2023
1 parent f9bd228 commit e1139da
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 137 deletions.
2 changes: 1 addition & 1 deletion mobile/library/common/jni/android_jni_interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ extern "C" JNIEXPORT jint JNICALL
Java_io_envoyproxy_envoymobile_engine_AndroidJniLibrary_initialize(JNIEnv* env,
jclass, // class
jobject class_loader) {
set_class_loader(env->NewGlobalRef(class_loader));
Envoy::JNI::set_class_loader(env->NewGlobalRef(class_loader));
return ENVOY_SUCCESS;
}
10 changes: 5 additions & 5 deletions mobile/library/common/jni/android_jni_utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
bool is_cleartext_permitted(absl::string_view hostname) {
#if defined(__ANDROID_API__)
envoy_data host = Envoy::Data::Utility::copyToBridgeData(hostname);
JNIEnv* env = get_env();
jstring java_host = native_data_to_string(env, host);
JNIEnv* env = Envoy::JNI::get_env();
jstring java_host = Envoy::JNI::native_data_to_string(env, host);
jclass jcls_AndroidNetworkLibrary =
find_class("io.envoyproxy.envoymobile.utilities.AndroidNetworkLibrary");
Envoy::JNI::find_class("io.envoyproxy.envoymobile.utilities.AndroidNetworkLibrary");
jmethodID jmid_isCleartextTrafficPermitted = env->GetStaticMethodID(
jcls_AndroidNetworkLibrary, "isCleartextTrafficPermitted", "(Ljava/lang/String;)Z");
jboolean result = env->CallStaticBooleanMethod(jcls_AndroidNetworkLibrary,
Expand All @@ -36,9 +36,9 @@ bool is_cleartext_permitted(absl::string_view hostname) {

void tag_socket(int ifd, int uid, int tag) {
#if defined(__ANDROID_API__)
JNIEnv* env = get_env();
JNIEnv* env = Envoy::JNI::get_env();
jclass jcls_AndroidNetworkLibrary =
find_class("io.envoyproxy.envoymobile.utilities.AndroidNetworkLibrary");
Envoy::JNI::find_class("io.envoyproxy.envoymobile.utilities.AndroidNetworkLibrary");
jmethodID jmid_tagSocket =
env->GetStaticMethodID(jcls_AndroidNetworkLibrary, "tagSocket", "(III)V");
env->CallStaticVoidMethod(jcls_AndroidNetworkLibrary, jmid_tagSocket, ifd, uid, tag);
Expand Down
23 changes: 12 additions & 11 deletions mobile/library/common/jni/android_network_utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

bool jvm_cert_is_issued_by_known_root(JNIEnv* env, jobject result) {
jclass jcls_AndroidCertVerifyResult =
find_class("io.envoyproxy.envoymobile.utilities.AndroidCertVerifyResult");
Envoy::JNI::find_class("io.envoyproxy.envoymobile.utilities.AndroidCertVerifyResult");
jmethodID jmid_isIssuedByKnownRoot =
env->GetMethodID(jcls_AndroidCertVerifyResult, "isIssuedByKnownRoot", "()Z");
Envoy::JNI::Exception::checkAndClear("jvm_cert_is_issued_by_known_root:GetMethodID");
Expand All @@ -27,7 +27,7 @@ bool jvm_cert_is_issued_by_known_root(JNIEnv* env, jobject result) {

envoy_cert_verify_status_t jvm_cert_get_status(JNIEnv* env, jobject j_result) {
jclass jcls_AndroidCertVerifyResult =
find_class("io.envoyproxy.envoymobile.utilities.AndroidCertVerifyResult");
Envoy::JNI::find_class("io.envoyproxy.envoymobile.utilities.AndroidCertVerifyResult");
jmethodID jmid_getStatus = env->GetMethodID(jcls_AndroidCertVerifyResult, "getStatus", "()I");
Envoy::JNI::Exception::checkAndClear("jvm_cert_get_status:GetMethodID");
ASSERT(jmid_getStatus);
Expand All @@ -41,7 +41,7 @@ envoy_cert_verify_status_t jvm_cert_get_status(JNIEnv* env, jobject j_result) {

jobjectArray jvm_cert_get_certificate_chain_encoded(JNIEnv* env, jobject result) {
jclass jcls_AndroidCertVerifyResult =
find_class("io.envoyproxy.envoymobile.utilities.AndroidCertVerifyResult");
Envoy::JNI::find_class("io.envoyproxy.envoymobile.utilities.AndroidCertVerifyResult");
jmethodID jmid_getCertificateChainEncoded =
env->GetMethodID(jcls_AndroidCertVerifyResult, "getCertificateChainEncoded", "()[[B");
Envoy::JNI::Exception::checkAndClear("jvm_cert_get_certificate_chain_encoded:GetMethodID");
Expand All @@ -60,7 +60,7 @@ static void ExtractCertVerifyResult(JNIEnv* env, jobject result, envoy_cert_veri
*is_issued_by_known_root = jvm_cert_is_issued_by_known_root(env, result);
jobjectArray chain_byte_array = jvm_cert_get_certificate_chain_encoded(env, result);
if (chain_byte_array != nullptr) {
JavaArrayOfByteArrayToStringVector(env, chain_byte_array, verified_chain);
Envoy::JNI::JavaArrayOfByteArrayToStringVector(env, chain_byte_array, verified_chain);
}
}
}
Expand All @@ -70,15 +70,15 @@ jobject call_jvm_verify_x509_cert_chain(JNIEnv* env, const std::vector<std::stri
std::string auth_type, absl::string_view hostname) {
jni_log("[Envoy]", "jvm_verify_x509_cert_chain");
jclass jcls_AndroidNetworkLibrary =
find_class("io.envoyproxy.envoymobile.utilities.AndroidNetworkLibrary");
Envoy::JNI::find_class("io.envoyproxy.envoymobile.utilities.AndroidNetworkLibrary");
jmethodID jmid_verifyServerCertificates = env->GetStaticMethodID(
jcls_AndroidNetworkLibrary, "verifyServerCertificates",
"([[B[B[B)Lio/envoyproxy/envoymobile/utilities/AndroidCertVerifyResult;");
Envoy::JNI::Exception::checkAndClear("call_jvm_verify_x509_cert_chain:GetStaticMethodID");
jobjectArray chain_byte_array = ToJavaArrayOfByteArray(env, cert_chain);
jbyteArray auth_string = ToJavaByteArray(env, auth_type);
jbyteArray host_string =
ToJavaByteArray(env, reinterpret_cast<const uint8_t*>(hostname.data()), hostname.length());
jobjectArray chain_byte_array = Envoy::JNI::ToJavaArrayOfByteArray(env, cert_chain);
jbyteArray auth_string = Envoy::JNI::ToJavaByteArray(env, auth_type);
jbyteArray host_string = Envoy::JNI::ToJavaByteArray(
env, reinterpret_cast<const uint8_t*>(hostname.data()), hostname.length());
jobject result =
env->CallStaticObjectMethod(jcls_AndroidNetworkLibrary, jmid_verifyServerCertificates,
chain_byte_array, auth_string, host_string);
Expand All @@ -96,12 +96,13 @@ static void jvm_verify_x509_cert_chain(const std::vector<std::string>& cert_chai
envoy_cert_verify_status_t* status,
bool* is_issued_by_known_root,
std::vector<std::string>* verified_chain) {
JNIEnv* env = get_env();
JNIEnv* env = Envoy::JNI::get_env();
jobject result = call_jvm_verify_x509_cert_chain(env, cert_chain, auth_type, hostname);
if (Envoy::JNI::Exception::checkAndClear()) {
*status = CERT_VERIFY_STATUS_NOT_YET_VALID;
} else {
ExtractCertVerifyResult(get_env(), result, status, is_issued_by_known_root, verified_chain);
ExtractCertVerifyResult(Envoy::JNI::get_env(), result, status, is_issued_by_known_root,
verified_chain);
if (Envoy::JNI::Exception::checkAndClear()) {
*status = CERT_VERIFY_STATUS_FAILED;
}
Expand Down
2 changes: 2 additions & 0 deletions mobile/library/common/jni/jni_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Envoy {
namespace JNI {

JNIEnv* JniHelper::getEnv() { return env_; }

jmethodID JniHelper::getMethodId(jclass clazz, const char* name, const char* signature) {
jmethodID method_id = env_->GetMethodID(clazz, name, signature);
rethrowException();
Expand Down
3 changes: 3 additions & 0 deletions mobile/library/common/jni/jni_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ class JniHelper {
public:
explicit JniHelper(JNIEnv* env) : env_(env) {}

/** Gets the underlying `JNIEnv`. */
JNIEnv* getEnv();

/**
* Gets the object method with the given signature.
*
Expand Down
Loading

0 comments on commit e1139da

Please sign in to comment.