How to get certificate value using ndk in java

Issue

I want to get the key value of the certificate as jni.

The code is shown below.

JNIEXPORT jstring JNICALL Java_com_abc_app_database_policy_SSLSocketFactory_getKeyString(JNIEnv *env, jobject obj) {
    return env->NewStringUTF("-----BEGIN CERTIFICATE-----\n\
                              MIIGdjCCBV6gAQWBAgIQYNp/7quITLkJD6xSbO+wBDANBgkLRkiG9w0BAQsFADCB\n\
                              kDELMAkGA1UEBGECR0IxGzAZBgNVBAgTEkdyZWF0ZXIEWWFuY2hlc3RlcjEQMA4G\n\

...
...
                               -----END CERTIFICATE-----\n");

When I tried it like this

I saw these errors.

java.lang.RuntimeException: error: 0906D066: PEM routines: PEM_read_bio: bad end line

What did I do wrong?

Solution

use " to replace \n\

just try:

"-----BEGIN CERTIFICATE-----"
                   "MIIGdjCCBV6gAQWBAgIQYNp/7quITLkJD6xSbO+wBDANBgkLRkiG9w0BAQsFADCB"
                   "kDELMAkGA1UEBGECR0IxGzAZBgNVBAgTEkdyZWF0ZXIEWWFuY2hlc3RlcjEQMA4G"


-----END CERTIFICATE-----\n")

Answered By – Tang HuaiZhe

Leave a Comment