jni.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <jni.h>
  4. #include <sys/types.h>
  5. #include <inttypes.h>
  6. #include <stdlib.h>
  7. #include <openssl/aes.h>
  8. #include <openssl/evp.h>
  9. #include <unistd.h>
  10. #include <dirent.h>
  11. #include <sys/stat.h>
  12. int registerNativeTgNetFunctions(JavaVM *vm, JNIEnv *env);
  13. int videoOnJNILoad(JavaVM *vm, JNIEnv *env);
  14. int imageOnJNILoad(JavaVM *vm, JNIEnv *env);
  15. int tgvoipOnJNILoad(JavaVM *vm, JNIEnv *env);
  16. jint JNI_OnLoad(JavaVM *vm, void *reserved) {
  17. JNIEnv *env = 0;
  18. srand(time(NULL));
  19. if ((*vm)->GetEnv(vm, (void **) &env, JNI_VERSION_1_6) != JNI_OK) {
  20. return -1;
  21. }
  22. if (imageOnJNILoad(vm, env) != JNI_TRUE) {
  23. return -1;
  24. }
  25. if (videoOnJNILoad(vm, env) != JNI_TRUE) {
  26. return -1;
  27. }
  28. if (registerNativeTgNetFunctions(vm, env) != JNI_TRUE) {
  29. return -1;
  30. }
  31. tgvoipOnJNILoad(vm, env);
  32. return JNI_VERSION_1_6;
  33. }
  34. void JNI_OnUnload(JavaVM *vm, void *reserved) {
  35. }
  36. JNIEXPORT void Java_org_telegram_messenger_Utilities_aesIgeEncryption(JNIEnv *env, jclass class, jobject buffer, jbyteArray key, jbyteArray iv, jboolean encrypt, jint offset, jint length) {
  37. jbyte *what = (*env)->GetDirectBufferAddress(env, buffer) + offset;
  38. unsigned char *keyBuff = (unsigned char *)(*env)->GetByteArrayElements(env, key, NULL);
  39. unsigned char *ivBuff = (unsigned char *)(*env)->GetByteArrayElements(env, iv, NULL);
  40. AES_KEY akey;
  41. if (!encrypt) {
  42. AES_set_decrypt_key(keyBuff, 32 * 8, &akey);
  43. AES_ige_encrypt(what, what, length, &akey, ivBuff, AES_DECRYPT);
  44. } else {
  45. AES_set_encrypt_key(keyBuff, 32 * 8, &akey);
  46. AES_ige_encrypt(what, what, length, &akey, ivBuff, AES_ENCRYPT);
  47. }
  48. (*env)->ReleaseByteArrayElements(env, key, keyBuff, JNI_ABORT);
  49. (*env)->ReleaseByteArrayElements(env, iv, ivBuff, 0);
  50. }
  51. JNIEXPORT void Java_org_telegram_messenger_Utilities_aesIgeEncryptionByteArray(JNIEnv *env, jclass class, jbyteArray buffer, jbyteArray key, jbyteArray iv, jboolean encrypt, jint offset, jint length) {
  52. unsigned char *bufferBuff = (unsigned char *) (*env)->GetByteArrayElements(env, buffer, NULL);
  53. unsigned char *keyBuff = (unsigned char *) (*env)->GetByteArrayElements(env, key, NULL);
  54. unsigned char *ivBuff = (unsigned char *) (*env)->GetByteArrayElements(env, iv, NULL);
  55. AES_KEY akey;
  56. if (!encrypt) {
  57. AES_set_decrypt_key(keyBuff, 32 * 8, &akey);
  58. AES_ige_encrypt(bufferBuff, bufferBuff, length, &akey, ivBuff, AES_DECRYPT);
  59. } else {
  60. AES_set_encrypt_key(keyBuff, 32 * 8, &akey);
  61. AES_ige_encrypt(bufferBuff, bufferBuff, length, &akey, ivBuff, AES_ENCRYPT);
  62. }
  63. (*env)->ReleaseByteArrayElements(env, key, keyBuff, JNI_ABORT);
  64. (*env)->ReleaseByteArrayElements(env, iv, ivBuff, 0);
  65. (*env)->ReleaseByteArrayElements(env, buffer, bufferBuff, 0);
  66. }
  67. JNIEXPORT jint Java_org_telegram_messenger_Utilities_pbkdf2(JNIEnv *env, jclass class, jbyteArray password, jbyteArray salt, jbyteArray dst, jint iterations) {
  68. jbyte *passwordBuff = (*env)->GetByteArrayElements(env, password, NULL);
  69. size_t passwordLength = (size_t) (*env)->GetArrayLength(env, password);
  70. jbyte *saltBuff = (*env)->GetByteArrayElements(env, salt, NULL);
  71. size_t saltLength = (size_t) (*env)->GetArrayLength(env, salt);
  72. jbyte *dstBuff = (*env)->GetByteArrayElements(env, dst, NULL);
  73. size_t dstLength = (size_t) (*env)->GetArrayLength(env, dst);
  74. int result = PKCS5_PBKDF2_HMAC((char *) passwordBuff, passwordLength, (uint8_t *) saltBuff, saltLength, (unsigned int) iterations, EVP_sha512(), dstLength, (uint8_t *) dstBuff);
  75. (*env)->ReleaseByteArrayElements(env, password, passwordBuff, JNI_ABORT);
  76. (*env)->ReleaseByteArrayElements(env, salt, saltBuff, JNI_ABORT);
  77. (*env)->ReleaseByteArrayElements(env, dst, dstBuff, 0);
  78. return result;
  79. }
  80. JNIEXPORT void Java_org_telegram_messenger_Utilities_aesCtrDecryption(JNIEnv *env, jclass class, jobject buffer, jbyteArray key, jbyteArray iv, jint offset, jint length) {
  81. jbyte *what = (*env)->GetDirectBufferAddress(env, buffer) + offset;
  82. unsigned char *keyBuff = (unsigned char *)(*env)->GetByteArrayElements(env, key, NULL);
  83. unsigned char *ivBuff = (unsigned char *)(*env)->GetByteArrayElements(env, iv, NULL);
  84. AES_KEY akey;
  85. unsigned int num = 0;
  86. uint8_t count[16];
  87. memset(count, 0, 16);
  88. AES_set_encrypt_key(keyBuff, 32 * 8, &akey);
  89. AES_ctr128_encrypt(what, what, length, &akey, ivBuff, count, &num);
  90. (*env)->ReleaseByteArrayElements(env, key, keyBuff, JNI_ABORT);
  91. (*env)->ReleaseByteArrayElements(env, iv, ivBuff, JNI_ABORT);
  92. }
  93. JNIEXPORT void Java_org_telegram_messenger_Utilities_aesCtrDecryptionByteArray(JNIEnv *env, jclass class, jbyteArray buffer, jbyteArray key, jbyteArray iv, jint offset, jint length, jint fileOffset) {
  94. unsigned char *bufferBuff = (unsigned char *) (*env)->GetByteArrayElements(env, buffer, NULL);
  95. unsigned char *keyBuff = (unsigned char *) (*env)->GetByteArrayElements(env, key, NULL);
  96. unsigned char *ivBuff = (unsigned char *) (*env)->GetByteArrayElements(env, iv, NULL);
  97. AES_KEY akey;
  98. uint8_t count[16];
  99. AES_set_encrypt_key(keyBuff, 32 * 8, &akey);
  100. unsigned int num = (unsigned int) (fileOffset % 16);
  101. int o = fileOffset / 16;
  102. ivBuff[15] = (uint8_t) (o & 0xff);
  103. ivBuff[14] = (uint8_t) ((o >> 8) & 0xff);
  104. ivBuff[13] = (uint8_t) ((o >> 16) & 0xff);
  105. ivBuff[12] = (uint8_t) ((o >> 24) & 0xff);
  106. AES_encrypt(ivBuff, count, &akey);
  107. o = (fileOffset + 15) / 16;
  108. ivBuff[15] = (uint8_t) (o & 0xff);
  109. ivBuff[14] = (uint8_t) ((o >> 8) & 0xff);
  110. ivBuff[13] = (uint8_t) ((o >> 16) & 0xff);
  111. ivBuff[12] = (uint8_t) ((o >> 24) & 0xff);
  112. AES_ctr128_encrypt(bufferBuff + offset, bufferBuff + offset, length, &akey, ivBuff, count, &num);
  113. (*env)->ReleaseByteArrayElements(env, key, keyBuff, JNI_ABORT);
  114. (*env)->ReleaseByteArrayElements(env, iv, ivBuff, JNI_ABORT);
  115. (*env)->ReleaseByteArrayElements(env, buffer, bufferBuff, 0);
  116. }
  117. JNIEXPORT void Java_org_telegram_messenger_Utilities_aesCbcEncryptionByteArray(JNIEnv *env, jclass class, jbyteArray buffer, jbyteArray key, jbyteArray iv, jint offset, jint length, jint fileOffset, jint encrypt) {
  118. unsigned char *bufferBuff = (unsigned char *) (*env)->GetByteArrayElements(env, buffer, NULL);
  119. unsigned char *keyBuff = (unsigned char *) (*env)->GetByteArrayElements(env, key, NULL);
  120. unsigned char *ivBuff = (unsigned char *) (*env)->GetByteArrayElements(env, iv, NULL);
  121. AES_KEY akey;
  122. if (encrypt) {
  123. AES_set_encrypt_key(keyBuff, 32 * 8, &akey);
  124. } else {
  125. AES_set_decrypt_key(keyBuff, 32 * 8, &akey);
  126. if (fileOffset != 0) {
  127. int o = (fileOffset + 15) / 16;
  128. ivBuff[15] = (uint8_t) (o & 0xff);
  129. ivBuff[14] = (uint8_t) ((o >> 8) & 0xff);
  130. ivBuff[13] = (uint8_t) ((o >> 16) & 0xff);
  131. ivBuff[12] = (uint8_t) ((o >> 24) & 0xff);
  132. }
  133. }
  134. AES_cbc_encrypt(bufferBuff, bufferBuff, length, &akey, ivBuff, encrypt);
  135. (*env)->ReleaseByteArrayElements(env, buffer, bufferBuff, 0);
  136. (*env)->ReleaseByteArrayElements(env, key, keyBuff, JNI_ABORT);
  137. (*env)->ReleaseByteArrayElements(env, iv, ivBuff, JNI_ABORT);
  138. }
  139. JNIEXPORT void Java_org_telegram_messenger_Utilities_aesCbcEncryption(JNIEnv *env, jclass class, jobject buffer, jbyteArray key, jbyteArray iv, jint offset, jint length, jint encrypt) {
  140. unsigned char *bufferBuff = (*env)->GetDirectBufferAddress(env, buffer) + offset;
  141. unsigned char *keyBuff = (unsigned char *) (*env)->GetByteArrayElements(env, key, NULL);
  142. unsigned char *ivBuff = (unsigned char *) (*env)->GetByteArrayElements(env, iv, NULL);
  143. AES_KEY akey;
  144. if (encrypt) {
  145. AES_set_encrypt_key(keyBuff, 32 * 8, &akey);
  146. } else {
  147. AES_set_decrypt_key(keyBuff, 32 * 8, &akey);
  148. }
  149. AES_cbc_encrypt(bufferBuff + offset, bufferBuff + offset, length, &akey, ivBuff, encrypt);
  150. (*env)->ReleaseByteArrayElements(env, key, keyBuff, JNI_ABORT);
  151. (*env)->ReleaseByteArrayElements(env, iv, ivBuff, JNI_ABORT);
  152. }
  153. int64_t listdir(const char *fileName, int32_t mode, int32_t docType, int64_t time, uint8_t subdirs) {
  154. int64_t value = 0;
  155. DIR *dir;
  156. struct stat attrib;
  157. if ((dir = opendir(fileName)) != NULL) {
  158. char buff[4096];
  159. struct dirent *entry;
  160. while ((entry = readdir(dir)) != NULL) {
  161. char *name = entry->d_name;
  162. size_t len = strlen(name);
  163. if (name[0] == '.') {
  164. continue;
  165. }
  166. if ((docType == 1 || docType == 2) && len > 4) {
  167. if (name[len - 4] == '.' && (
  168. ((name[len - 3] == 'm' || name[len - 3] == 'M') && (name[len - 2] == 'p' || name[len - 2] == 'P') && (name[len - 1] == '3')) ||
  169. ((name[len - 3] == 'm' || name[len - 3] == 'M') && (name[len - 2] == '4') && (name[len - 1] == 'a' || name[len - 1] == 'A'))
  170. )) {
  171. if (docType == 1) {
  172. continue;
  173. }
  174. } else if (docType == 2) {
  175. continue;
  176. }
  177. }
  178. strncpy(buff, fileName, 4095);
  179. strncat(buff, "/", 4095);
  180. strncat(buff, entry->d_name, 4095);
  181. if (entry->d_type == DT_DIR) {
  182. if (subdirs) {
  183. value += listdir(buff, mode, docType, time, subdirs);
  184. }
  185. } else {
  186. stat(buff, &attrib);
  187. if (mode == 0) {
  188. value += 512 * attrib.st_blocks;
  189. } else if (mode == 1) {
  190. if (attrib.st_atim.tv_sec != 0) {
  191. if (attrib.st_atim.tv_sec < time) {
  192. remove(buff);
  193. }
  194. } else {
  195. if (attrib.st_mtim.tv_sec < time) {
  196. remove(buff);
  197. }
  198. }
  199. }
  200. }
  201. }
  202. closedir(dir);
  203. }
  204. return value;
  205. }
  206. JNIEXPORT jlong Java_org_telegram_messenger_Utilities_getDirSize(JNIEnv *env, jclass class, jstring path, jint docType, jboolean subdirs) {
  207. const char *fileName = (*env)->GetStringUTFChars(env, path, NULL);
  208. jlong value = listdir(fileName, 0, docType, 0, subdirs);
  209. (*env)->ReleaseStringUTFChars(env, path, fileName);
  210. return value;
  211. }
  212. JNIEXPORT void Java_org_telegram_messenger_Utilities_clearDir(JNIEnv *env, jclass class, jstring path, jint docType, jlong time, jboolean subdirs) {
  213. const char *fileName = (*env)->GetStringUTFChars(env, path, NULL);
  214. listdir(fileName, 1, docType, time, subdirs);
  215. (*env)->ReleaseStringUTFChars(env, path, fileName);
  216. }