p≡p for Java
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

461 lines
13 KiB

3 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
2 years ago
2 years ago
7 years ago
  1. #include <pEp/keymanagement.h>
  2. #include <pEp/Adapter.hh>
  3. #include <pEp/pEpLog.hh>
  4. #ifndef ANDROID
  5. #include <string.h>
  6. #endif
  7. #include "throw_pEp_exception.hh"
  8. #include "jniutils.hh"
  9. #include "passphrase_callback.hh"
  10. extern "C" {
  11. using namespace pEp::JNIAdapter;
  12. using pEp::Adapter::session;
  13. using pEp::passphrase_cache;
  14. JNIEXPORT jbyteArray JNICALL Java_foundation_pEp_jniadapter_Engine__1trustwords(JNIEnv *env,
  15. jobject obj,
  16. jobject ident)
  17. {
  18. std::mutex *mutex_local = nullptr;
  19. {
  20. std::lock_guard<std::mutex> l(global_mutex);
  21. pEpLog("called with lock_guard");
  22. mutex_local = get_engine_java_object_mutex(env, obj);
  23. }
  24. std::lock_guard<std::mutex> l(*mutex_local);
  25. pEp_identity *_ident = to_identity(env, ident);
  26. char *words;
  27. size_t wsize;
  28. PEP_STATUS status = PEP_STATUS_OK;
  29. if (_ident->fpr == NULL || _ident->fpr[0] == 0) {
  30. if (_ident->me) {
  31. status = passphraseWrap(::myself, session(), _ident);
  32. } else {
  33. status = passphraseWrap(::update_identity, session(), _ident);
  34. }
  35. }
  36. if (status != PEP_STATUS_OK) {
  37. throw_pEp_Exception(env, status);
  38. return NULL;
  39. }
  40. if (_ident->fpr == NULL || _ident->fpr[0] == 0) {
  41. throw_pEp_Exception(env, PEP_CANNOT_FIND_IDENTITY);
  42. return NULL;
  43. }
  44. const char *lang;
  45. if (_ident->lang[0]) {
  46. lang = _ident->lang;
  47. } else {
  48. lang = "en";
  49. }
  50. status = passphraseWrap(::trustwords, session(), static_cast<const char *>(_ident->fpr), lang, &words, &wsize, 10);
  51. if (status != PEP_STATUS_OK) {
  52. throw_pEp_Exception(env, status);
  53. return NULL;
  54. }
  55. return from_string(env, words);
  56. }
  57. JNIEXPORT jobject JNICALL Java_foundation_pEp_jniadapter_Engine__1myself(JNIEnv *env,
  58. jobject obj,
  59. jobject ident)
  60. {
  61. std::mutex *mutex_local = nullptr;
  62. {
  63. std::lock_guard<std::mutex> l(global_mutex);
  64. pEpLog("called with lock_guard");
  65. mutex_local = get_engine_java_object_mutex(env, obj);
  66. }
  67. std::lock_guard<std::mutex> l(*mutex_local);
  68. pEp_identity *_ident = to_identity(env, ident);
  69. PEP_STATUS status = passphraseWrap(::myself, session(), _ident);
  70. if (status != PEP_STATUS_OK) {
  71. LOGD("Failed Myself: 0x%04x\\n", status);
  72. throw_pEp_Exception(env, status);
  73. return NULL;
  74. }
  75. return from_identity(env, _ident);
  76. }
  77. JNIEXPORT jobject JNICALL Java_foundation_pEp_jniadapter_Engine__1updateIdentity(JNIEnv *env,
  78. jobject obj,
  79. jobject ident)
  80. {
  81. std::mutex *mutex_local = nullptr;
  82. {
  83. std::lock_guard<std::mutex> l(global_mutex);
  84. pEpLog("called with lock_guard");
  85. mutex_local = get_engine_java_object_mutex(env, obj);
  86. }
  87. std::lock_guard<std::mutex> l(*mutex_local);
  88. pEp_identity *_ident = to_identity(env, ident);
  89. passphraseWrap(::update_identity, session(), _ident);
  90. return from_identity(env, _ident);
  91. }
  92. JNIEXPORT jobject JNICALL Java_foundation_pEp_jniadapter_Engine__1setOwnKey(JNIEnv *env,
  93. jobject obj,
  94. jobject ident,
  95. jbyteArray fpr)
  96. {
  97. std::mutex *mutex_local = nullptr;
  98. {
  99. std::lock_guard<std::mutex> l(global_mutex);
  100. pEpLog("called with lock_guard");
  101. mutex_local = get_engine_java_object_mutex(env, obj);
  102. }
  103. std::lock_guard<std::mutex> l(*mutex_local);
  104. pEp_identity *_ident = to_identity(env, ident);
  105. const char *_fpr = to_string(env, fpr);
  106. PEP_STATUS status = passphraseWrap(::set_own_key, session(), _ident, _fpr);
  107. if (status != PEP_STATUS_OK) {
  108. LOGD("Failed setOwnKey: 0x%04x\\n", status);
  109. throw_pEp_Exception(env, status);
  110. return NULL;
  111. }
  112. return from_identity(env, _ident);
  113. }
  114. JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1keyMistrusted(JNIEnv *env,
  115. jobject obj,
  116. jobject ident)
  117. {
  118. std::mutex *mutex_local = nullptr;
  119. {
  120. std::lock_guard<std::mutex> l(global_mutex);
  121. pEpLog("called with lock_guard");
  122. mutex_local = get_engine_java_object_mutex(env, obj);
  123. }
  124. std::lock_guard<std::mutex> l(*mutex_local);
  125. pEp_identity *_ident = to_identity(env, ident);
  126. PEP_STATUS status = PEP_STATUS_OK;
  127. if (_ident->fpr == NULL || _ident->fpr[0] == 0) {
  128. if (_ident->me)
  129. status = passphraseWrap(::myself, session(), _ident);
  130. else
  131. status = passphraseWrap(::update_identity, session(), _ident);
  132. }
  133. if (status != PEP_STATUS_OK) {
  134. throw_pEp_Exception(env, status);
  135. return;
  136. }
  137. if (_ident->fpr == NULL || _ident->fpr[0] == 0) {
  138. throw_pEp_Exception(env, PEP_CANNOT_FIND_IDENTITY);
  139. return;
  140. }
  141. passphraseWrap(::key_mistrusted, session(), _ident);
  142. }
  143. JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1keyResetTrust(JNIEnv *env,
  144. jobject obj,
  145. jobject ident)
  146. {
  147. std::mutex *mutex_local = nullptr;
  148. {
  149. std::lock_guard<std::mutex> l(global_mutex);
  150. pEpLog("called with lock_guard");
  151. mutex_local = get_engine_java_object_mutex(env, obj);
  152. }
  153. std::lock_guard<std::mutex> l(*mutex_local);
  154. pEp_identity *_ident = to_identity(env, ident);
  155. PEP_STATUS status = PEP_STATUS_OK;
  156. if (_ident->fpr == NULL || _ident->fpr[0] == 0) {
  157. if (_ident->me) {
  158. status = passphraseWrap(::myself, session(), _ident);
  159. } else {
  160. status = passphraseWrap(::update_identity, session(), _ident);
  161. }
  162. }
  163. if (status != PEP_STATUS_OK) {
  164. throw_pEp_Exception(env, status);
  165. return;
  166. }
  167. if (_ident->fpr == NULL || _ident->fpr[0] == 0) {
  168. throw_pEp_Exception(env, PEP_CANNOT_FIND_IDENTITY);
  169. return;
  170. }
  171. passphraseWrap(::key_reset_trust, session(), _ident);
  172. }
  173. JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1trustPersonalKey(JNIEnv *env,
  174. jobject obj,
  175. jobject ident)
  176. {
  177. std::mutex *mutex_local = nullptr;
  178. {
  179. std::lock_guard<std::mutex> l(global_mutex);
  180. pEpLog("called with lock_guard");
  181. mutex_local = get_engine_java_object_mutex(env, obj);
  182. }
  183. std::lock_guard<std::mutex> l(*mutex_local);
  184. pEp_identity *_ident = to_identity(env, ident);
  185. PEP_STATUS status = PEP_STATUS_OK;
  186. if (_ident->fpr == NULL || _ident->fpr[0] == 0) {
  187. if (_ident->me) {
  188. status = passphraseWrap(::myself, session(), _ident);
  189. } else {
  190. status = passphraseWrap(::update_identity, session(), _ident);
  191. }
  192. }
  193. if (status != PEP_STATUS_OK) {
  194. throw_pEp_Exception(env, status);
  195. return;
  196. }
  197. if (_ident->fpr == NULL || _ident->fpr[0] == 0) {
  198. throw_pEp_Exception(env, PEP_CANNOT_FIND_IDENTITY);
  199. return;
  200. }
  201. passphraseWrap(::trust_personal_key, session(), _ident);
  202. }
  203. JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1trustOwnKey(JNIEnv *env,
  204. jobject obj,
  205. jobject ident)
  206. {
  207. std::mutex *mutex_local = nullptr;
  208. {
  209. std::lock_guard<std::mutex> l(global_mutex);
  210. pEpLog("called with lock_guard");
  211. mutex_local = get_engine_java_object_mutex(env, obj);
  212. }
  213. std::lock_guard<std::mutex> l(*mutex_local);
  214. pEp_identity *_ident = to_identity(env, ident);
  215. if (_ident->fpr == NULL || _ident->fpr[0] == 0) {
  216. throw_pEp_Exception(env, PEP_CANNOT_FIND_IDENTITY);
  217. return;
  218. }
  219. passphraseWrap(::trust_own_key, session(), _ident);
  220. }
  221. JNIEXPORT jobject JNICALL Java_foundation_pEp_jniadapter_Engine__1importKey(JNIEnv *env,
  222. jobject obj,
  223. jbyteArray key)
  224. {
  225. std::mutex *mutex_local = nullptr;
  226. {
  227. std::lock_guard<std::mutex> l(global_mutex);
  228. pEpLog("called with lock_guard");
  229. mutex_local = get_engine_java_object_mutex(env, obj);
  230. }
  231. std::lock_guard<std::mutex> l(*mutex_local);
  232. size_t _size = static_cast<size_t>(env->GetArrayLength(key));
  233. jbyte* _key = env->GetByteArrayElements(key, NULL);
  234. if(_key == NULL) {
  235. throw_pEp_Exception(env, PEP_OUT_OF_MEMORY);
  236. return NULL;
  237. }
  238. identity_list *_identities;
  239. PEP_STATUS status = passphraseWrap(::import_key, session(), reinterpret_cast<const char*>(_key), _size, &_identities);
  240. if (status != PEP_STATUS_OK && status != PEP_KEY_IMPORTED) {
  241. throw_pEp_Exception(env, status);
  242. return NULL;
  243. }
  244. jobject identities_ = NULL;
  245. if (_identities) {
  246. identities_ = from_identitylist(env, _identities);
  247. }
  248. env->ReleaseByteArrayElements(key, _key, JNI_ABORT);
  249. return identities_;
  250. }
  251. JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1config_1passive_1mode(JNIEnv *env,
  252. jobject obj,
  253. jboolean enable)
  254. {
  255. std::mutex *mutex_local = nullptr;
  256. {
  257. std::lock_guard<std::mutex> l(global_mutex);
  258. pEpLog("called with lock_guard");
  259. mutex_local = get_engine_java_object_mutex(env, obj);
  260. }
  261. std::lock_guard<std::mutex> l(*mutex_local);
  262. ::config_passive_mode(session(), static_cast<bool>(enable));
  263. }
  264. JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1config_1unencrypted_1subject(JNIEnv *env,
  265. jobject obj,
  266. jboolean enable)
  267. {
  268. std::mutex *mutex_local = nullptr;
  269. {
  270. std::lock_guard<std::mutex> l(global_mutex);
  271. pEpLog("called with lock_guard");
  272. mutex_local = get_engine_java_object_mutex(env, obj);
  273. }
  274. std::lock_guard<std::mutex> l(*mutex_local);
  275. ::config_unencrypted_subject(session(), static_cast<bool>(enable));
  276. }
  277. JNIEXPORT jbyteArray JNICALL Java_foundation_pEp_jniadapter_Engine__1getCrashdumpLog(JNIEnv *env,
  278. jobject obj,
  279. jint dummy,
  280. jint maxlines)
  281. {
  282. std::mutex *mutex_local = nullptr;
  283. {
  284. std::lock_guard<std::mutex> l(global_mutex);
  285. pEpLog("called with lock_guard");
  286. mutex_local = get_engine_java_object_mutex(env, obj);
  287. }
  288. std::lock_guard<std::mutex> l(*mutex_local);
  289. int _maxlines = static_cast<int>(maxlines);
  290. char *_logdata;
  291. PEP_STATUS status = passphraseWrap(::get_crashdump_log, session(), _maxlines, &_logdata);
  292. if ((status > PEP_STATUS_OK && status < PEP_UNENCRYPTED) ||
  293. status < PEP_STATUS_OK ||
  294. status >= PEP_TRUSTWORD_NOT_FOUND) {
  295. throw_pEp_Exception(env, status);
  296. return NULL;
  297. }
  298. return from_string(env, _logdata);
  299. }
  300. JNIEXPORT jbyteArray JNICALL Java_foundation_pEp_jniadapter_Engine__1getUserDirectory(JNIEnv *env,
  301. jobject obj)
  302. {
  303. pEpLog("called");
  304. return from_string(env, ::per_user_directory());
  305. }
  306. JNIEXPORT jbyteArray JNICALL Java_foundation_pEp_jniadapter_Engine__1getMachineDirectory(JNIEnv *env,
  307. jobject obj)
  308. {
  309. pEpLog("called");
  310. return from_string(env, ::per_machine_directory());
  311. }
  312. JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1config_1passphrase(JNIEnv * env,
  313. jobject obj,
  314. jbyteArray passphrase)
  315. {
  316. std::mutex *mutex_local = nullptr;
  317. {
  318. std::lock_guard<std::mutex> l(global_mutex);
  319. pEpLog("called with lock_guard");
  320. mutex_local = get_engine_java_object_mutex(env, obj);
  321. }
  322. std::lock_guard<std::mutex> l(*mutex_local);
  323. char* _passphrase = to_string(env, passphrase);
  324. PEP_STATUS status = ::config_passphrase(session(),passphrase_cache.add(_passphrase));
  325. if (status != 0) {
  326. throw_pEp_Exception(env, status);
  327. return;
  328. }
  329. }
  330. JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1config_1passphrase_1for_1new_1keys(JNIEnv *env,
  331. jobject obj,
  332. jboolean enable,
  333. jbyteArray passphrase)
  334. {
  335. std::mutex *mutex_local = nullptr;
  336. {
  337. std::lock_guard<std::mutex> l(global_mutex);
  338. pEpLog("called with lock_guard");
  339. mutex_local = get_engine_java_object_mutex(env, obj);
  340. }
  341. std::lock_guard<std::mutex> l(*mutex_local);
  342. bool _enable = static_cast<bool>(enable);
  343. const char *_passphrase = to_string(env, passphrase);
  344. PEP_STATUS status = ::config_passphrase_for_new_keys(session(),_enable,passphrase_cache.add_stored(_passphrase));
  345. if ((status > PEP_STATUS_OK && status < PEP_UNENCRYPTED) ||
  346. status < PEP_STATUS_OK ||
  347. status >= PEP_TRUSTWORD_NOT_FOUND) {
  348. throw_pEp_Exception(env, status);
  349. return ;
  350. }
  351. }
  352. JNIEXPORT jbyteArray JNICALL Java_foundation_pEp_jniadapter_Engine__1export_1key (JNIEnv *env,
  353. jobject obj,
  354. jbyteArray fpr)
  355. {
  356. std::mutex *mutex_local = nullptr;
  357. {
  358. std::lock_guard<std::mutex> l(global_mutex);
  359. pEpLog("called with lock_guard");
  360. mutex_local = get_engine_java_object_mutex(env, obj);
  361. }
  362. std::lock_guard<std::mutex> l(*mutex_local);
  363. const char *_fpr = to_string(env, fpr);
  364. char *buff = nullptr;
  365. size_t size = 0;
  366. PEP_STATUS status = passphraseWrap(::export_key, session(), _fpr, &buff, &size);
  367. if (status != PEP_STATUS_OK) {
  368. throw_pEp_Exception(env, status);
  369. return NULL;
  370. }
  371. return from_string(env, buff);
  372. }
  373. } // extern "C"