简体   繁体   中英

Bringing SSL certificate bye[] array from jni to flutter

I'm performing certificate pinning in flutter by securely storing the certificate in JNI and fetching it during run time. But I get BAD_PKCS12_DATA(pkcs8_x509.c:626), errno = 0) when I fetch the data from JNI. The pinning works if I set it directly in flutter code though like List<int> _crt = <int>[45, 45, 45, 45, 45, 66, 69, 71, 73, 78, ...]

Here is the JNI method:

extern "C" JNIEXPORT jintArray JNICALL Java_com_package_android_MainActivity_getCert
        (JNIEnv *env, jobject This)
{
    int a[] ={45,45,45,45,45,...};
    jintArray ret = env->NewIntArray(sizeof(a));
    env->SetIntArrayRegion(ret, 0, 6, a);
    return ret;
}

MainActivity.kt:

external fun getCert(): IntArray
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
            call, result ->
            if (call.method == "cert") {
                result.success(getCert())
            }
        }
    }

Flutter code:

      List<int> _crt;
      _crt = await _platform.invokeMethod("cert");
      //print("CRT: " + _crt.length);
      SecurityContext context = SecurityContext(withTrustedRoots: true);
      context.setTrustedCertificatesBytes(_crt);
      httpClient = new HttpClient(context: context);

I'm confused why the returned int array from JNI doesnt work but would have no problem if I set it directly in flutter?

sizeof(a) doesn't give you the number of elements in a . It gives you total size of all the elements, in bytes.

What you want is sizeof(a) / sizeof(int) or sizeof(a) / sizeof(a[0]) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM