|
| 1 | +#include <jni.h> |
| 2 | +#include <stdint.h> |
| 3 | +#include <stdlib.h> |
| 4 | +#include <string.h> |
| 5 | + |
| 6 | +void *box_system_certificates_der(uintptr_t vmPtr, int *out_length) { |
| 7 | + *out_length = 0; |
| 8 | + |
| 9 | + JavaVM *vm = (JavaVM *) vmPtr; |
| 10 | + JNIEnv *env = NULL; |
| 11 | + int attached = 0; |
| 12 | + jint getEnvResult = (*vm)->GetEnv(vm, (void **) &env, JNI_VERSION_1_6); |
| 13 | + if (getEnvResult == JNI_EDETACHED) { |
| 14 | + if ((*vm)->AttachCurrentThread(vm, &env, NULL) != JNI_OK) { |
| 15 | + return NULL; |
| 16 | + } |
| 17 | + attached = 1; |
| 18 | + } else if (getEnvResult != JNI_OK) { |
| 19 | + return NULL; |
| 20 | + } |
| 21 | + |
| 22 | + unsigned char *result = NULL; |
| 23 | + int resultLength = 0; |
| 24 | + |
| 25 | + jclass keyStoreClass = (*env)->FindClass(env, "java/security/KeyStore"); |
| 26 | + jmethodID getInstance = (*env)->GetStaticMethodID(env, keyStoreClass, "getInstance", "(Ljava/lang/String;)Ljava/security/KeyStore;"); |
| 27 | + jstring storeName = (*env)->NewStringUTF(env, "AndroidCAStore"); |
| 28 | + jobject keyStore = (*env)->CallStaticObjectMethod(env, keyStoreClass, getInstance, storeName); |
| 29 | + if ((*env)->ExceptionCheck(env) || keyStore == NULL) { |
| 30 | + goto done; |
| 31 | + } |
| 32 | + |
| 33 | + jmethodID load = (*env)->GetMethodID(env, keyStoreClass, "load", "(Ljava/io/InputStream;[C)V"); |
| 34 | + (*env)->CallVoidMethod(env, keyStore, load, NULL, NULL); |
| 35 | + if ((*env)->ExceptionCheck(env)) { |
| 36 | + goto done; |
| 37 | + } |
| 38 | + |
| 39 | + jmethodID aliasesMethod = (*env)->GetMethodID(env, keyStoreClass, "aliases", "()Ljava/util/Enumeration;"); |
| 40 | + jmethodID getCertificate = (*env)->GetMethodID(env, keyStoreClass, "getCertificate", "(Ljava/lang/String;)Ljava/security/cert/Certificate;"); |
| 41 | + jobject aliases = (*env)->CallObjectMethod(env, keyStore, aliasesMethod); |
| 42 | + if ((*env)->ExceptionCheck(env) || aliases == NULL) { |
| 43 | + goto done; |
| 44 | + } |
| 45 | + |
| 46 | + jclass enumerationClass = (*env)->FindClass(env, "java/util/Enumeration"); |
| 47 | + jmethodID hasMoreElements = (*env)->GetMethodID(env, enumerationClass, "hasMoreElements", "()Z"); |
| 48 | + jmethodID nextElement = (*env)->GetMethodID(env, enumerationClass, "nextElement", "()Ljava/lang/Object;"); |
| 49 | + |
| 50 | + jclass certificateClass = (*env)->FindClass(env, "java/security/cert/Certificate"); |
| 51 | + jmethodID getEncoded = (*env)->GetMethodID(env, certificateClass, "getEncoded", "()[B"); |
| 52 | + |
| 53 | + while ((*env)->CallBooleanMethod(env, aliases, hasMoreElements)) { |
| 54 | + jstring alias = (jstring) (*env)->CallObjectMethod(env, aliases, nextElement); |
| 55 | + jobject certificate = (*env)->CallObjectMethod(env, keyStore, getCertificate, alias); |
| 56 | + (*env)->DeleteLocalRef(env, alias); |
| 57 | + if ((*env)->ExceptionCheck(env) || certificate == NULL) { |
| 58 | + (*env)->ExceptionClear(env); |
| 59 | + continue; |
| 60 | + } |
| 61 | + jbyteArray encoded = (jbyteArray) (*env)->CallObjectMethod(env, certificate, getEncoded); |
| 62 | + (*env)->DeleteLocalRef(env, certificate); |
| 63 | + if ((*env)->ExceptionCheck(env) || encoded == NULL) { |
| 64 | + (*env)->ExceptionClear(env); |
| 65 | + continue; |
| 66 | + } |
| 67 | + jsize encodedLength = (*env)->GetArrayLength(env, encoded); |
| 68 | + unsigned char *grown = realloc(result, resultLength + encodedLength); |
| 69 | + if (grown == NULL) { |
| 70 | + (*env)->DeleteLocalRef(env, encoded); |
| 71 | + free(result); |
| 72 | + result = NULL; |
| 73 | + resultLength = 0; |
| 74 | + goto done; |
| 75 | + } |
| 76 | + result = grown; |
| 77 | + (*env)->GetByteArrayRegion(env, encoded, 0, encodedLength, (jbyte *) (result + resultLength)); |
| 78 | + resultLength += encodedLength; |
| 79 | + (*env)->DeleteLocalRef(env, encoded); |
| 80 | + } |
| 81 | + |
| 82 | +done: |
| 83 | + if ((*env)->ExceptionCheck(env)) { |
| 84 | + (*env)->ExceptionClear(env); |
| 85 | + } |
| 86 | + if (attached) { |
| 87 | + (*vm)->DetachCurrentThread(vm); |
| 88 | + } |
| 89 | + *out_length = resultLength; |
| 90 | + return result; |
| 91 | +} |
0 commit comments