p≡p engine FORK
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.

1126 lines
42 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. // This file is under GNU General Public License 3.0
  2. // see LICENSE.txt
  3. #include <stdlib.h>
  4. #include "TestConstants.h"
  5. #include <cstring>
  6. #include <iostream>
  7. #include <fstream>
  8. #include "pEpEngine.h"
  9. #include "platform.h"
  10. #include "mime.h"
  11. #include "message_api.h"
  12. #include "keymanagement.h"
  13. #include "test_util.h"
  14. #include "Engine.h"
  15. #include <gtest/gtest.h>
  16. namespace {
  17. //The fixture for ReencryptPlusExtraKeysTest
  18. class ReencryptPlusExtraKeysTest : public ::testing::Test {
  19. public:
  20. Engine* engine;
  21. PEP_SESSION session;
  22. protected:
  23. // You can remove any or all of the following functions if its body
  24. // is empty.
  25. ReencryptPlusExtraKeysTest() {
  26. // You can do set-up work for each test here.
  27. test_suite_name = ::testing::UnitTest::GetInstance()->current_test_info()->GTEST_SUITE_SYM();
  28. test_name = ::testing::UnitTest::GetInstance()->current_test_info()->name();
  29. test_path = get_main_test_home_dir() + "/" + test_suite_name + "/" + test_name;
  30. }
  31. ~ReencryptPlusExtraKeysTest() override {
  32. // You can do clean-up work that doesn't throw exceptions here.
  33. }
  34. // If the constructor and destructor are not enough for setting up
  35. // and cleaning up each test, you can define the following methods:
  36. void SetUp() override {
  37. // Code here will be called immediately after the constructor (right
  38. // before each test).
  39. // Leave this empty if there are no files to copy to the home directory path
  40. std::vector<std::pair<std::string, std::string>> init_files = std::vector<std::pair<std::string, std::string>>();
  41. // Get a new test Engine.
  42. engine = new Engine(test_path);
  43. ASSERT_NE(engine, nullptr);
  44. // Ok, let's initialize test directories etc.
  45. engine->prep(NULL, NULL, NULL, init_files);
  46. // Ok, try to start this bugger.
  47. engine->start();
  48. ASSERT_NE(engine->session, nullptr);
  49. session = engine->session;
  50. // Engine is up. Keep on truckin'
  51. }
  52. void TearDown() override {
  53. // Code here will be called immediately after each test (right
  54. // before the destructor).
  55. engine->shut_down();
  56. delete engine;
  57. engine = NULL;
  58. session = NULL;
  59. }
  60. // Own identity keys
  61. const char* fpr_own_recip_key = "85D022E0CC9BA9F6B922CA7B638E5211B1A2BE89";
  62. const char* fpr_own_recip_2_key = "7A2EEB933E6FD99207B83E397B6D3751D6E75FFF";
  63. // Sender key
  64. const char* fpr_sender_pub_key = "95FE24B262A34FA5C6A8D0AAF90144FC3B508C8E";
  65. // Other recips
  66. const char* fpr_recip_0_pub_key = "CDF787C7C9664E02825DD416C6FBCF8D1F4A5986";
  67. const char* fpr_recip_2_pub_key = "60701073D138EF622C8F9221B6FC86831EDBE691";
  68. // Extra keys
  69. const char* fpr_pub_extra_key_0 = "33BB6C92EBFB6F29641C75B5B79D916C828AA789";
  70. const char* fpr_pub_extra_key_1 = "3DB93A746785FDD6110798AB3B193A9E8B026AEC";
  71. const char* fpr_pub_extra_key_2 = "E8AC9779A2D13A15D8D55C84B049F489BB5BCCF6";
  72. void import_reenc_test_keys() {
  73. PEP_STATUS status;
  74. // Import own identity keys
  75. const string own_recip_pub_key = slurp("test_keys/pub/reencrypt_recip_0-0xB1A2BE89_pub.asc");
  76. const string own_recip_priv_key = slurp("test_keys/priv/reencrypt_recip_0-0xB1A2BE89_priv.asc");
  77. const string own_recip_2_pub_key = slurp("test_keys/pub/reencrypt_recip_numero_deux_test_0-0xD6E75FFF_pub.asc");
  78. const string own_recip_2_priv_key = slurp("test_keys/priv/reencrypt_recip_numero_deux_test_0-0xD6E75FFF_priv.asc");
  79. status = import_key(session, own_recip_pub_key.c_str(), own_recip_pub_key.length(), NULL);
  80. ASSERT_EQ(status , PEP_TEST_KEY_IMPORT_SUCCESS);
  81. status = import_key(session, own_recip_priv_key.c_str(), own_recip_priv_key.length(), NULL);
  82. ASSERT_EQ(status , PEP_TEST_KEY_IMPORT_SUCCESS);
  83. status = import_key(session, own_recip_2_pub_key.c_str(), own_recip_2_pub_key.length(), NULL);
  84. ASSERT_EQ(status , PEP_TEST_KEY_IMPORT_SUCCESS);
  85. status = import_key(session, own_recip_2_priv_key.c_str(), own_recip_2_priv_key.length(), NULL);
  86. ASSERT_EQ(status , PEP_TEST_KEY_IMPORT_SUCCESS);
  87. // Import sender key
  88. const string sender_pub_key = slurp("test_keys/pub/reencrypt_sender_0-0x3B508C8E_pub.asc");
  89. status = import_key(session, sender_pub_key.c_str(), sender_pub_key.length(), NULL);
  90. ASSERT_EQ(status , PEP_TEST_KEY_IMPORT_SUCCESS);
  91. // Import other recips
  92. const string recip_0_pub_key = slurp("test_keys/pub/reencrypt_other_recip_0-0x1F4A5986_pub.asc");
  93. status = import_key(session, recip_0_pub_key.c_str(), recip_0_pub_key.length(), NULL);
  94. ASSERT_EQ(status , PEP_TEST_KEY_IMPORT_SUCCESS);
  95. // we're leaving recip_1 out for the Hell of it - D3886D0DF75113BE2799C9374D6B99FE0F8273D8
  96. const string recip_2_pub_key = slurp("test_keys/pub/reencrypt_other_recip_2-0x1EDBE691_pub.asc");
  97. status = import_key(session, recip_2_pub_key.c_str(), recip_2_pub_key.length(), NULL);
  98. ASSERT_EQ(status , PEP_TEST_KEY_IMPORT_SUCCESS);
  99. // Import extra keys
  100. const string pub_extra_key_0 = slurp("test_keys/pub/reencrypt_extra_keys_0-0x828AA789_pub.asc");
  101. const string pub_extra_key_1 = slurp("test_keys/pub/reencrypt_extra_keys_1-0x8B026AEC_pub.asc");
  102. status = import_key(session, pub_extra_key_0.c_str(), pub_extra_key_0.length(), NULL);
  103. ASSERT_EQ(status , PEP_TEST_KEY_IMPORT_SUCCESS);
  104. status = import_key(session, pub_extra_key_1.c_str(), pub_extra_key_1.length(), NULL);
  105. ASSERT_EQ(status , PEP_TEST_KEY_IMPORT_SUCCESS);
  106. output_stream << "Keys imported." << endl;
  107. }
  108. private:
  109. const char* test_suite_name;
  110. const char* test_name;
  111. string test_path;
  112. // Objects declared here can be used by all tests in the ReencryptPlusExtraKeysTest suite.
  113. };
  114. } // namespace
  115. TEST_F(ReencryptPlusExtraKeysTest, check_reencrypt_unencrypted_subj) {
  116. config_unencrypted_subject(session, true);
  117. pEp_identity* carol = NULL;
  118. PEP_STATUS status = set_up_preset(session, CAROL,
  119. true, true, true, true, true, &carol);
  120. ASSERT_EQ(status , PEP_STATUS_OK);
  121. ASSERT_NE(carol, nullptr);
  122. status = set_identity_flags(session, carol, PEP_idf_org_ident);
  123. ASSERT_EQ(status , PEP_STATUS_OK);
  124. status = myself(session, carol);
  125. ASSERT_OK;
  126. ASSERT_EQ(carol->flags, carol->flags & PEP_idf_org_ident);
  127. string mailfile = slurp("test_mails/From_M2_1.eml");
  128. char* decrypted_text = nullptr;
  129. // In: extra keys; Out: keys that were used to encrypt this.
  130. stringlist_t* keys = NULL;
  131. PEP_decrypt_flags_t flags = PEP_decrypt_flag_untrusted_server;
  132. PEP_rating rating;
  133. flags = PEP_decrypt_flag_untrusted_server;
  134. char* modified_src = NULL;
  135. message* decoded = NULL;
  136. status = mime_decode_message(mailfile.c_str(), mailfile.size(), &decoded, NULL);
  137. ASSERT_NE(decoded, nullptr);
  138. ASSERT_OK;
  139. message* dec_msg = NULL;
  140. decoded->recv_by = identity_dup(carol);
  141. status = decrypt_message(session, decoded, &dec_msg, &keys, &rating, &flags);
  142. ASSERT_NE(dec_msg, nullptr);
  143. ASSERT_NE(PEP_decrypt_flag_src_modified & flags, 0);
  144. message* checker = decoded;
  145. ASSERT_STREQ(checker->shortmsg, "Boom shaka laka");
  146. config_unencrypted_subject(session, false);
  147. message* src_msg = NULL;
  148. status = mime_decode_message(mailfile.c_str(), mailfile.size(), &src_msg, NULL);
  149. ASSERT_NE(src_msg, nullptr);
  150. ASSERT_STREQ(src_msg->attachments->next->value, checker->attachments->next->value);
  151. config_unencrypted_subject(session, false);
  152. }
  153. TEST_F(ReencryptPlusExtraKeysTest, check_reencrypt_unencrypted_subj_check_efficient) {
  154. config_unencrypted_subject(session, true);
  155. pEp_identity* carol = NULL;
  156. PEP_STATUS status = set_up_preset(session, CAROL,
  157. true, true, true, true, true, &carol);
  158. ASSERT_EQ(status , PEP_STATUS_OK);
  159. ASSERT_NE(carol, nullptr);
  160. status = set_identity_flags(session, carol, PEP_idf_org_ident);
  161. ASSERT_EQ(status , PEP_STATUS_OK);
  162. status = myself(session, carol);
  163. ASSERT_OK;
  164. ASSERT_EQ(carol->flags, carol->flags & PEP_idf_org_ident);
  165. string mailfile = slurp("test_mails/From_M2_1.eml");
  166. char* decrypted_text = nullptr;
  167. // In: extra keys; Out: keys that were used to encrypt this.
  168. stringlist_t* keys = NULL;
  169. PEP_decrypt_flags_t flags = PEP_decrypt_flag_untrusted_server;
  170. PEP_rating rating;
  171. flags = PEP_decrypt_flag_untrusted_server;
  172. char* modified_src = NULL;
  173. message* decoded = NULL;
  174. status = mime_decode_message(mailfile.c_str(), mailfile.size(), &decoded, NULL);
  175. ASSERT_NE(decoded, nullptr);
  176. ASSERT_OK;
  177. message* dec_msg = NULL;
  178. decoded->recv_by = identity_dup(carol);
  179. status = decrypt_message(session, decoded, &dec_msg, &keys, &rating, &flags);
  180. ASSERT_NE(status, PEP_CANNOT_REENCRYPT);
  181. ASSERT_NE(dec_msg, nullptr);
  182. ASSERT_NE(PEP_decrypt_flag_src_modified & flags, 0);
  183. message* checker = decoded;
  184. ASSERT_STREQ(checker->shortmsg, "Boom shaka laka");
  185. message* src_msg = NULL;
  186. status = mime_decode_message(mailfile.c_str(), mailfile.size(), &src_msg, NULL);
  187. ASSERT_NE(src_msg, nullptr);
  188. ASSERT_STREQ(src_msg->attachments->next->value, checker->attachments->next->value);
  189. free_message(dec_msg);
  190. dec_msg = NULL;
  191. flags = PEP_decrypt_flag_untrusted_server;
  192. free_stringlist(keys);
  193. keys = NULL; // remember, this is no extra_keys in this test
  194. status = decrypt_message(session, checker, &dec_msg, &keys, &rating, &flags);
  195. ASSERT_EQ(status , PEP_STATUS_OK);
  196. ASSERT_NE(dec_msg , nullptr);
  197. ASSERT_EQ(flags & PEP_decrypt_flag_src_modified, 0);
  198. ASSERT_NE(checker, nullptr);
  199. ASSERT_NE(dec_msg->_sender_fpr, nullptr);
  200. ASSERT_NE(keys, nullptr);
  201. ASSERT_STREQ(dec_msg->_sender_fpr, keys->value); // should be the same, since not reencrypted
  202. config_unencrypted_subject(session, false);
  203. }
  204. TEST_F(ReencryptPlusExtraKeysTest, check_reencrypt_unencrypted_subj_extra_keys) {
  205. config_unencrypted_subject(session, true);
  206. ASSERT_TRUE(slurp_and_import_key(session, "test_keys/pub/reencrypt_extra_keys_0-0x828AA789_pub.asc"));
  207. ASSERT_TRUE(slurp_and_import_key(session, "test_keys/pub/reencrypt_extra_keys_1-0x8B026AEC_pub.asc"));
  208. stringlist_t* keys = new_stringlist(fpr_pub_extra_key_0);
  209. stringlist_add(keys, fpr_pub_extra_key_1);
  210. pEp_identity* carol = NULL;
  211. PEP_STATUS status = set_up_preset(session, CAROL,
  212. true, true, true, true, true, &carol);
  213. ASSERT_EQ(status, PEP_STATUS_OK);
  214. ASSERT_NE(carol, nullptr);
  215. status = set_identity_flags(session, carol, PEP_idf_org_ident);
  216. ASSERT_EQ(status , PEP_STATUS_OK);
  217. status = myself(session, carol);
  218. ASSERT_OK;
  219. ASSERT_EQ(carol->flags, carol->flags & PEP_idf_org_ident);
  220. string mailfile = slurp("test_mails/From_M2_1.eml");
  221. char* decrypted_text = nullptr;
  222. // In: extra keys; Out: keys that were used to encrypt this.
  223. PEP_decrypt_flags_t flags = PEP_decrypt_flag_untrusted_server;
  224. PEP_rating rating;
  225. flags = PEP_decrypt_flag_untrusted_server;
  226. char* modified_src = NULL;
  227. message* decoded = NULL;
  228. status = mime_decode_message(mailfile.c_str(), mailfile.size(), &decoded, NULL);
  229. ASSERT_NE(decoded, nullptr);
  230. ASSERT_OK;
  231. message* dec_msg = NULL;
  232. decoded->recv_by = identity_dup(carol);
  233. status = decrypt_message(session, decoded, &dec_msg, &keys, &rating, &flags);
  234. ASSERT_NE(status, PEP_CANNOT_REENCRYPT);
  235. ASSERT_NE(dec_msg, nullptr);
  236. ASSERT_NE(PEP_decrypt_flag_src_modified & flags, 0);
  237. message* checker = decoded;
  238. ASSERT_STREQ(checker->shortmsg, "Boom shaka laka");
  239. message* src_msg = NULL;
  240. status = mime_decode_message(mailfile.c_str(), mailfile.size(), &src_msg, NULL);
  241. ASSERT_NE(src_msg, nullptr);
  242. ASSERT_STRNE(src_msg->attachments->next->value, checker->attachments->next->value);
  243. flags = 0;
  244. message* decryptomatic = NULL;
  245. stringlist_t* extra_keys = NULL;
  246. status = decrypt_message(session, checker, &decryptomatic, &extra_keys, &rating, &flags);
  247. bool own_key_found, extra_key_0_found, extra_key_1_found;
  248. own_key_found = extra_key_0_found = extra_key_1_found = false;
  249. int i = 0;
  250. for (stringlist_t* kl = extra_keys; kl && kl->value; kl = kl->next, i++)
  251. {
  252. if (i == 0) {
  253. output_stream << "Signed by " << (strcasecmp("", kl->value) == 0 ? "NOBODY" : kl->value) << endl;
  254. ASSERT_STRCASEEQ(carol->fpr, kl->value);
  255. }
  256. else {
  257. if (strcasecmp(carol->fpr, kl->value) == 0)
  258. own_key_found = true;
  259. else if (strcasecmp(fpr_pub_extra_key_0, kl->value) == 0)
  260. extra_key_0_found = true;
  261. else if (strcasecmp(fpr_pub_extra_key_1, kl->value) == 0)
  262. extra_key_1_found = true;
  263. else {
  264. output_stream << "FAIL: Encrypted for " << kl->value << ", which it should not be." << endl;
  265. ASSERT_TRUE(false);
  266. }
  267. }
  268. ASSERT_LT(i, 4);
  269. }
  270. ASSERT_TRUE(own_key_found && extra_key_0_found && extra_key_1_found);
  271. config_unencrypted_subject(session, false);
  272. }
  273. TEST_F(ReencryptPlusExtraKeysTest, check_reencrypt_unencrypted_subj_extra_keys_efficient_pass) {
  274. config_unencrypted_subject(session, true);
  275. ASSERT_TRUE(slurp_and_import_key(session, "test_keys/pub/reencrypt_extra_keys_0-0x828AA789_pub.asc"));
  276. ASSERT_TRUE(slurp_and_import_key(session, "test_keys/pub/reencrypt_extra_keys_1-0x8B026AEC_pub.asc"));
  277. stringlist_t* keys = new_stringlist(fpr_pub_extra_key_0);
  278. stringlist_add(keys, fpr_pub_extra_key_1);
  279. pEp_identity* carol = NULL;
  280. PEP_STATUS status = set_up_preset(session, CAROL,
  281. true, true, true, true, true, &carol);
  282. ASSERT_EQ(status, PEP_STATUS_OK);
  283. ASSERT_NE(carol, nullptr);
  284. status = set_identity_flags(session, carol, PEP_idf_org_ident);
  285. ASSERT_EQ(status , PEP_STATUS_OK);
  286. status = myself(session, carol);
  287. ASSERT_OK;
  288. ASSERT_EQ(carol->flags, carol->flags & PEP_idf_org_ident);
  289. string mailfile = slurp("test_mails/From_M2_1.eml");
  290. char* decrypted_text = nullptr;
  291. // In: extra keys; Out: keys that were used to encrypt this.
  292. PEP_decrypt_flags_t flags = PEP_decrypt_flag_untrusted_server;
  293. PEP_rating rating;
  294. flags = PEP_decrypt_flag_untrusted_server;
  295. char* modified_src = NULL;
  296. message* decoded = NULL;
  297. status = mime_decode_message(mailfile.c_str(), mailfile.size(), &decoded, NULL);
  298. ASSERT_NE(decoded, nullptr);
  299. ASSERT_OK;
  300. message* dec_msg = NULL;
  301. decoded->recv_by = identity_dup(carol);
  302. status = decrypt_message(session, decoded, &dec_msg, &keys, &rating, &flags);
  303. ASSERT_NE(status, PEP_CANNOT_REENCRYPT);
  304. ASSERT_NE(dec_msg, nullptr);
  305. ASSERT_NE(PEP_decrypt_flag_src_modified & flags, 0);
  306. message* checker = decoded;
  307. ASSERT_STREQ(checker->shortmsg, "Boom shaka laka");
  308. message* src_msg = NULL;
  309. status = mime_decode_message(mailfile.c_str(), mailfile.size(), &src_msg, NULL);
  310. ASSERT_NE(src_msg, nullptr);
  311. ASSERT_STRNE(src_msg->attachments->next->value, checker->attachments->next->value);
  312. free_stringlist(keys);
  313. keys = new_stringlist(fpr_pub_extra_key_0);
  314. stringlist_add(keys, fpr_pub_extra_key_1);
  315. flags = PEP_decrypt_flag_untrusted_server;
  316. message* decryptomatic = NULL;
  317. status = decrypt_message(session, checker, &decryptomatic, &keys, &rating, &flags);
  318. ASSERT_EQ(status , PEP_STATUS_OK);
  319. ASSERT_NE(decryptomatic, nullptr);
  320. ASSERT_EQ(flags & PEP_decrypt_flag_src_modified, 0);
  321. ASSERT_NE(checker, nullptr);
  322. ASSERT_NE(decryptomatic->_sender_fpr, nullptr);
  323. ASSERT_NE(keys, nullptr);
  324. ASSERT_STREQ(decryptomatic->_sender_fpr, "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97");
  325. ASSERT_STREQ(keys->value, "8DD4F5827B45839E9ACCA94687BDDFFB42A85A42");
  326. // ofstream outfile;
  327. // outfile.open("test_mails/From_M2_1_all_extra.eml");
  328. // outfile << modified_src;
  329. // outfile.close();
  330. config_unencrypted_subject(session, false);
  331. }
  332. TEST_F(ReencryptPlusExtraKeysTest, check_reencrypt_unencrypted_subj_extra_keys_efficient_missing) {
  333. config_unencrypted_subject(session, true);
  334. ASSERT_TRUE(slurp_and_import_key(session, "test_keys/pub/reencrypt_extra_keys_0-0x828AA789_pub.asc"));
  335. ASSERT_TRUE(slurp_and_import_key(session, "test_keys/pub/reencrypt_extra_keys_1-0x8B026AEC_pub.asc"));
  336. ASSERT_TRUE(slurp_and_import_key(session, "test_keys/pub/pep-test-dave-0xBB5BCCF6_pub.asc"));
  337. stringlist_t* keys = new_stringlist(fpr_pub_extra_key_0);
  338. stringlist_add(keys, fpr_pub_extra_key_1);
  339. pEp_identity* carol = NULL;
  340. PEP_STATUS status = set_up_preset(session, CAROL,
  341. true, true, true, true, true, &carol);
  342. ASSERT_EQ(status, PEP_STATUS_OK);
  343. ASSERT_NE(carol, nullptr);
  344. status = set_identity_flags(session, carol, PEP_idf_org_ident);
  345. ASSERT_EQ(status , PEP_STATUS_OK);
  346. status = myself(session, carol);
  347. ASSERT_OK;
  348. ASSERT_EQ(carol->flags, carol->flags & PEP_idf_org_ident);
  349. string mailfile = slurp("test_mails/From_M2_1.eml");
  350. char* decrypted_text = nullptr;
  351. // In: extra keys; Out: keys that were used to encrypt this.
  352. PEP_decrypt_flags_t flags = PEP_decrypt_flag_untrusted_server;
  353. PEP_rating rating;
  354. flags = PEP_decrypt_flag_untrusted_server;
  355. char* modified_src = NULL;
  356. message* decoded = NULL;
  357. status = mime_decode_message(mailfile.c_str(), mailfile.size(), &decoded, NULL);
  358. ASSERT_NE(decoded, nullptr);
  359. ASSERT_OK;
  360. message* dec_msg = NULL;
  361. decoded->recv_by = identity_dup(carol);
  362. status = decrypt_message(session, decoded, &dec_msg, &keys, &rating, &flags);
  363. ASSERT_NE(status, PEP_CANNOT_REENCRYPT);
  364. ASSERT_NE(dec_msg, nullptr);
  365. ASSERT_NE(PEP_decrypt_flag_src_modified & flags, 0);
  366. message* checker = decoded;
  367. ASSERT_STREQ(checker->shortmsg, "Boom shaka laka");
  368. message* src_msg = NULL;
  369. status = mime_decode_message(mailfile.c_str(), mailfile.size(), &src_msg, NULL);
  370. ASSERT_NE(src_msg, nullptr);
  371. ASSERT_STRNE(src_msg->attachments->next->value, checker->attachments->next->value);
  372. free_stringlist(keys);
  373. keys = new_stringlist(fpr_pub_extra_key_0);
  374. stringlist_add(keys, fpr_pub_extra_key_1);
  375. stringlist_add(keys, fpr_pub_extra_key_2);
  376. flags = PEP_decrypt_flag_untrusted_server;
  377. message* decryptomatic = NULL;
  378. checker->recv_by = identity_dup(carol);
  379. status = decrypt_message(session, checker, &decryptomatic, &keys, &rating, &flags);
  380. ASSERT_EQ(status , PEP_STATUS_OK);
  381. ASSERT_NE(decryptomatic, nullptr);
  382. ASSERT_EQ(flags & PEP_decrypt_flag_src_modified, PEP_decrypt_flag_src_modified);
  383. ASSERT_NE(checker, nullptr);
  384. ASSERT_NE(decryptomatic->_sender_fpr, nullptr);
  385. ASSERT_NE(keys, nullptr);
  386. ASSERT_STREQ(decryptomatic->_sender_fpr, "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97");
  387. ASSERT_STREQ(keys->value, "8DD4F5827B45839E9ACCA94687BDDFFB42A85A42");
  388. // ofstream outfile;
  389. // outfile.open("test_mails/From_M2_1_all_extra.eml");
  390. // outfile << modified_src;
  391. // outfile.close();
  392. config_unencrypted_subject(session, false);
  393. }
  394. // FIXME: Also split this one up.
  395. TEST_F(ReencryptPlusExtraKeysTest, check_reencrypt_plus_extra_keys) {
  396. PEP_STATUS status = PEP_STATUS_OK;
  397. /* import all the keys */
  398. import_reenc_test_keys();
  399. output_stream << "Keys imported." << endl;
  400. pEp_identity* me_recip_1 = new_identity("reencrypt_recip@darthmama.cool", fpr_own_recip_key, PEP_OWN_USERID, "Me Recipient");
  401. pEp_identity* me_recip_2 = new_identity("reencrypt_recip_numero_deux_test@darthmama.org", fpr_own_recip_2_key, PEP_OWN_USERID, "Me Recipient");
  402. output_stream << "Inserting own identities and keys into database." << endl;
  403. status = set_own_key(session, me_recip_2, fpr_own_recip_2_key);
  404. ASSERT_EQ(status , PEP_STATUS_OK);
  405. output_stream << "Done: inserting own identities and keys into database." << endl;
  406. status = set_identity_flags(session, me_recip_2, PEP_idf_org_ident);
  407. ASSERT_EQ(status , PEP_STATUS_OK);
  408. status = myself(session, me_recip_2);
  409. ASSERT_OK;
  410. ASSERT_EQ(me_recip_2->flags, me_recip_2->flags & PEP_idf_org_ident);
  411. const string to_reencrypt_from_enigmail = slurp("test_mails/reencrypt_sent_by_enigmail.eml");
  412. const string to_reencrypt_from_enigmail_BCC = slurp("test_mails/reencrypt_BCC_sent_by_enigmail.eml");
  413. const string to_reencrypt_from_pEp = slurp("test_mails/reencrypt_encrypted_through_pEp.eml");
  414. output_stream << endl << "Case 1a: Calling MIME_decrypt_message with reencrypt flag set on message sent from enigmail for recip 2 with no extra keys." << endl;
  415. char* decrypted_text = nullptr;
  416. // In: extra keys; Out: keys that were used to encrypt this.
  417. stringlist_t* keys = NULL;
  418. PEP_decrypt_flags_t flags = 0;
  419. PEP_rating rating;
  420. flags = PEP_decrypt_flag_untrusted_server;
  421. char* modified_src = NULL;
  422. message* decoded = NULL;
  423. status = mime_decode_message(to_reencrypt_from_enigmail.c_str(), to_reencrypt_from_enigmail.size(), &decoded, NULL);
  424. ASSERT_NE(decoded, nullptr);
  425. ASSERT_OK;
  426. message* dec_msg = NULL;
  427. decoded->recv_by = identity_dup(me_recip_2);
  428. status = decrypt_message(session, decoded, &dec_msg, &keys, &rating, &flags);
  429. ASSERT_NE(status, PEP_CANNOT_REENCRYPT);
  430. ASSERT_NE(dec_msg, nullptr);
  431. ASSERT_EQ(PEP_decrypt_flag_src_modified & flags, 0);
  432. free_message(decoded);
  433. free_message(dec_msg);
  434. output_stream << "Case 1a: PASS" << endl << endl;
  435. output_stream << "Case 1b: Calling MIME_decrypt_message with reencrypt flag set on message sent from enigmail for recip 2 extra keys." << endl;
  436. // In: extra keys; Out: keys that were used to encrypt this.
  437. free_stringlist(keys);
  438. keys = new_stringlist(fpr_pub_extra_key_0);
  439. stringlist_add(keys, fpr_pub_extra_key_1);
  440. flags = PEP_decrypt_flag_untrusted_server;
  441. decoded = NULL;
  442. status = mime_decode_message(to_reencrypt_from_enigmail.c_str(), to_reencrypt_from_enigmail.size(), &decoded, NULL);
  443. ASSERT_NE(decoded, nullptr);
  444. ASSERT_OK;
  445. dec_msg = NULL;
  446. decoded->recv_by = identity_dup(me_recip_2);
  447. status = decrypt_message(session, decoded, &dec_msg, &keys, &rating, &flags);
  448. ASSERT_NE(status, PEP_CANNOT_REENCRYPT);
  449. ASSERT_NE(dec_msg, nullptr);
  450. ASSERT_NE(PEP_decrypt_flag_src_modified & flags, 0);
  451. free_message(dec_msg);
  452. dec_msg = NULL;
  453. output_stream << "CHECK: Decrypting to see what keys it was encrypted for." << endl;
  454. flags = 0;
  455. message* throwaway = NULL;
  456. decoded->recv_by = identity_dup(me_recip_2);
  457. stringlist_t* tmp_keys = NULL;
  458. status = decrypt_message(session, decoded, &dec_msg, &tmp_keys, &rating, &flags);
  459. output_stream << "keys used:\n";
  460. bool own_key_found = false;
  461. bool extra_key_0_found = false;
  462. bool extra_key_1_found = false;
  463. int i = 0;
  464. if (tmp_keys && tmp_keys->next)
  465. dedup_stringlist(tmp_keys->next);
  466. for (stringlist_t* kl = tmp_keys; kl && kl->value; kl = kl->next, i++)
  467. {
  468. if (i == 0) {
  469. output_stream << "Signed by " << (strcasecmp("", kl->value) == 0 ? "NOBODY" : kl->value) << endl;
  470. ASSERT_EQ(strcasecmp(fpr_own_recip_2_key,kl->value) , 0);
  471. }
  472. else {
  473. if (strcasecmp(fpr_own_recip_2_key, kl->value) == 0) {
  474. output_stream << "Encrypted for us." << endl;
  475. own_key_found = true;
  476. }
  477. else if (strcasecmp(fpr_pub_extra_key_0, kl->value) == 0) {
  478. output_stream << "Encrypted for extra key 0." << endl;
  479. extra_key_0_found = true;
  480. }
  481. else if (strcasecmp(fpr_pub_extra_key_1, kl->value) == 0) {
  482. output_stream << "Encrypted for extra key 1." << endl;
  483. extra_key_1_found = true;
  484. }
  485. else {
  486. output_stream << "FAIL: Encrypted for " << kl->value << ", which it should not be." << endl;
  487. ASSERT_TRUE(false);
  488. }
  489. output_stream << "\t " << kl->value << endl;
  490. }
  491. ASSERT_LT(i , 4);
  492. }
  493. ASSERT_TRUE(own_key_found && extra_key_0_found && extra_key_1_found);
  494. output_stream << "Message was encrypted for all the keys it should be, and none it should not!" << endl;
  495. free_stringlist(tmp_keys);
  496. tmp_keys = NULL;
  497. output_stream << "Case 1b: PASS" << endl << endl;
  498. output_stream << "Case 2a: Calling MIME_decrypt_message with reencrypt flag set on message sent with recip 2 in BCC from enigmail with no extra keys." << endl;
  499. free(modified_src);
  500. modified_src = NULL;
  501. free_stringlist(keys);
  502. keys = NULL;
  503. flags = PEP_decrypt_flag_untrusted_server;
  504. decoded = NULL;
  505. status = mime_decode_message(to_reencrypt_from_enigmail_BCC.c_str(), to_reencrypt_from_enigmail_BCC.size(), &decoded, NULL);
  506. ASSERT_NE(decoded, nullptr);
  507. ASSERT_OK;
  508. dec_msg = NULL;
  509. decoded->recv_by = identity_dup(me_recip_2);
  510. status = decrypt_message(session, decoded, &dec_msg, &keys, &rating, &flags);
  511. ASSERT_NE(status, PEP_CANNOT_REENCRYPT);
  512. ASSERT_NE(dec_msg, nullptr);
  513. ASSERT_EQ(PEP_decrypt_flag_src_modified & flags, 0);
  514. free_message(dec_msg);
  515. dec_msg = NULL;
  516. output_stream << "Case 2a: PASS" << endl << endl;
  517. output_stream << "Case 2b: Calling MIME_decrypt_message with reencrypt flag set on message sent with recip 2 in BCC from enigmail with extra keys." << endl;
  518. free_stringlist(keys);
  519. keys = new_stringlist(fpr_pub_extra_key_0);
  520. stringlist_add(keys, fpr_pub_extra_key_1);
  521. flags = PEP_decrypt_flag_untrusted_server;
  522. decoded = NULL;
  523. status = mime_decode_message(to_reencrypt_from_enigmail_BCC.c_str(), to_reencrypt_from_enigmail_BCC.size(), &decoded, NULL);
  524. ASSERT_NE(decoded, nullptr);
  525. ASSERT_OK;
  526. dec_msg = NULL;
  527. decoded->recv_by = identity_dup(me_recip_2);
  528. status = decrypt_message(session, decoded, &dec_msg, &keys, &rating, &flags);
  529. ASSERT_NE(status, PEP_CANNOT_REENCRYPT);
  530. ASSERT_NE(dec_msg, nullptr);
  531. ASSERT_NE(PEP_decrypt_flag_src_modified & flags, 0);
  532. free_message(dec_msg);
  533. dec_msg = NULL;
  534. output_stream << "CHECK: Decrypting to see what keys it was encrypted for." << endl;
  535. flags = 0;
  536. throwaway = NULL;
  537. decoded->recv_by = identity_dup(me_recip_2);
  538. tmp_keys = NULL;
  539. status = decrypt_message(session, decoded, &dec_msg, &tmp_keys, &rating, &flags);
  540. output_stream << "keys used:\n";
  541. own_key_found = false;
  542. extra_key_0_found = false;
  543. extra_key_1_found = false;
  544. i = 0;
  545. if (tmp_keys && tmp_keys->next)
  546. dedup_stringlist(tmp_keys->next);
  547. for (stringlist_t* kl = tmp_keys; kl && kl->value; kl = kl->next, i++)
  548. {
  549. if (i == 0) {
  550. output_stream << "Signed by " << (strcasecmp("", kl->value) == 0 ? "NOBODY" : kl->value) << endl;
  551. // ASSERT_EQ(strcasecmp(fpr_own_recip_2_key,kl->value) , 0);
  552. }
  553. else {
  554. if (strcasecmp(fpr_own_recip_2_key, kl->value) == 0) {
  555. output_stream << "Encrypted for us." << endl;
  556. own_key_found = true;
  557. }
  558. else if (strcasecmp(fpr_pub_extra_key_0, kl->value) == 0) {
  559. output_stream << "Encrypted for extra key 0." << endl;
  560. extra_key_0_found = true;
  561. }
  562. else if (strcasecmp(fpr_pub_extra_key_1, kl->value) == 0) {
  563. output_stream << "Encrypted for extra key 1." << endl;
  564. extra_key_1_found = true;
  565. }
  566. else {
  567. output_stream << "FAIL: Encrypted for " << kl->value << ", which it should not be." << endl;
  568. // TEST_ASSERT_MSG(false, "Encrypted for someone it shouldn't have been.");
  569. }
  570. output_stream << "\t " << kl->value << endl;
  571. }
  572. ASSERT_LT(i , 4);
  573. }
  574. // TEST_ASSERT_MSG(own_key_found && extra_key_0_found && extra_key_1_found, "Not encrypted for all desired keys.");
  575. output_stream << "Message was encrypted for all the keys it should be, and none it should not!" << endl;
  576. output_stream << "Case 2b: PASS" << endl << endl;
  577. output_stream << "Case 3a: Calling MIME_decrypt_message with reencrypt flag set on message generated by pEp (message 2.0) with no extra keys." << endl;
  578. free_stringlist(keys);
  579. keys = NULL;
  580. status = set_own_key(session, me_recip_1, fpr_own_recip_key);
  581. ASSERT_EQ(status , PEP_STATUS_OK);
  582. status = set_identity_flags(session, me_recip_1, PEP_idf_org_ident);
  583. ASSERT_EQ(status , PEP_STATUS_OK);
  584. status = myself(session, me_recip_1);
  585. ASSERT_OK;
  586. ASSERT_EQ(me_recip_1->flags, me_recip_1->flags & PEP_idf_org_ident);
  587. flags = PEP_decrypt_flag_untrusted_server;
  588. decoded = NULL;
  589. status = mime_decode_message(to_reencrypt_from_pEp.c_str(), to_reencrypt_from_pEp.size(), &decoded, NULL);
  590. ASSERT_NE(decoded, nullptr);
  591. ASSERT_OK;
  592. dec_msg = NULL;
  593. decoded->recv_by = identity_dup(me_recip_1);
  594. status = decrypt_message(session, decoded, &dec_msg, &keys, &rating, &flags);
  595. ASSERT_NE(status, PEP_CANNOT_REENCRYPT);
  596. ASSERT_NE(dec_msg, nullptr);
  597. ASSERT_EQ(PEP_decrypt_flag_src_modified & flags, 0);
  598. free_message(dec_msg);
  599. dec_msg = NULL;
  600. output_stream << "Case 3a: PASS" << endl << endl;
  601. output_stream << "Case 3b: Calling MIME_decrypt_message with reencrypt flag set on message generated by pEp (message 2.0) with extra keys." << endl;
  602. free_stringlist(keys);
  603. keys = new_stringlist(fpr_pub_extra_key_0);
  604. stringlist_add(keys, fpr_pub_extra_key_1);
  605. flags = PEP_decrypt_flag_untrusted_server;
  606. decoded = NULL;
  607. status = mime_decode_message(to_reencrypt_from_pEp.c_str(), to_reencrypt_from_pEp.size(), &decoded, NULL);
  608. ASSERT_NE(decoded, nullptr);
  609. ASSERT_OK;
  610. dec_msg = NULL;
  611. decoded->recv_by = identity_dup(me_recip_1);
  612. status = decrypt_message(session, decoded, &dec_msg, &keys, &rating, &flags);
  613. ASSERT_NE(status, PEP_CANNOT_REENCRYPT);
  614. ASSERT_NE(dec_msg, nullptr);
  615. free_message(dec_msg);
  616. dec_msg = NULL;
  617. output_stream << "CHECK: Decrypting to see what keys it was encrypted for." << endl;
  618. flags = 0;
  619. throwaway = NULL;
  620. decoded->recv_by = identity_dup(me_recip_1);
  621. tmp_keys = NULL;
  622. status = decrypt_message(session, decoded, &dec_msg, &tmp_keys, &rating, &flags);
  623. output_stream << "keys used:\n";
  624. own_key_found = false;
  625. extra_key_0_found = false;
  626. extra_key_1_found = false;
  627. i = 0;
  628. if (tmp_keys && tmp_keys->next)
  629. dedup_stringlist(tmp_keys->next);
  630. for (stringlist_t* kl = tmp_keys; kl && kl->value; kl = kl->next, i++)
  631. {
  632. if (i == 0) {
  633. output_stream << "Signed by " << (strcasecmp("", kl->value) == 0 ? "NOBODY" : kl->value) << endl;
  634. // TEST_ASSERT_MSG(strcasecmp(fpr_own_recip_key,kl->value) == 0);
  635. }
  636. else {
  637. if (strcasecmp(fpr_own_recip_key, kl->value) == 0) {
  638. output_stream << "Encrypted for us." << endl;
  639. own_key_found = true;
  640. }
  641. else if (strcasecmp(fpr_pub_extra_key_0, kl->value) == 0) {
  642. output_stream << "Encrypted for extra key 0." << endl;
  643. extra_key_0_found = true;
  644. }
  645. else if (strcasecmp(fpr_pub_extra_key_1, kl->value) == 0) {
  646. output_stream << "Encrypted for extra key 1." << endl;
  647. extra_key_1_found = true;
  648. }
  649. else {
  650. output_stream << "FAIL: Encrypted for " << kl->value << ", which it should not be." << endl;
  651. // TEST_ASSERT_MSG(false);
  652. }
  653. output_stream << "\t " << kl->value << endl;
  654. }
  655. ASSERT_LT(i , 4);
  656. }
  657. // TEST_ASSERT_MSG(own_key_found && extra_key_0_found && extra_key_1_found);
  658. output_stream << "Message was encrypted for all the keys it should be, and none it should not!" << endl;
  659. output_stream << "Case 3b: PASS" << endl << endl;
  660. }
  661. TEST_F(ReencryptPlusExtraKeysTest, check_efficient_reencrypt_from_enigmail) {
  662. output_stream << "Call MIME_decrypt_message with reencrypt flag set on message sent from enigmail for recip 2 extra keys." << endl;
  663. PEP_STATUS status = PEP_STATUS_OK;
  664. /* import all the keys */
  665. import_reenc_test_keys();
  666. // Set up own identities
  667. pEp_identity* me_recip_1 = new_identity("reencrypt_recip@darthmama.cool", fpr_own_recip_key, PEP_OWN_USERID, "Me Recipient");
  668. pEp_identity* me_recip_2 = new_identity("reencrypt_recip_numero_deux_test@darthmama.org", fpr_own_recip_2_key, PEP_OWN_USERID, "Me Recipient");
  669. output_stream << "Inserting own identities and keys into database." << endl;
  670. status = set_own_key(session, me_recip_2, fpr_own_recip_2_key);
  671. ASSERT_EQ(status , PEP_STATUS_OK);
  672. output_stream << "Done: inserting own identities and keys into database." << endl;
  673. status = set_identity_flags(session, me_recip_2, PEP_idf_org_ident);
  674. ASSERT_EQ(status , PEP_STATUS_OK);
  675. status = myself(session, me_recip_2);
  676. ASSERT_OK;
  677. ASSERT_EQ(me_recip_2->flags, me_recip_2->flags & PEP_idf_org_ident);
  678. // BEGIN ACTUAL TEST
  679. // Ready to receive message for the first time
  680. const string to_reencrypt_from_enigmail = slurp("test_mails/reencrypt_sent_by_enigmail.eml");
  681. message* dec_msg = NULL;
  682. message* enc_msg = NULL;
  683. // In: extra keys; Out: keys that were used to encrypt this.
  684. stringlist_t* keys = new_stringlist(fpr_pub_extra_key_0);
  685. stringlist_add(keys, fpr_pub_extra_key_1);
  686. PEP_decrypt_flags_t flags = 0;
  687. PEP_rating rating;
  688. flags = PEP_decrypt_flag_untrusted_server;
  689. // Put the original message into a message struct
  690. status = mime_decode_message(to_reencrypt_from_enigmail.c_str(), to_reencrypt_from_enigmail.size(), &enc_msg, NULL);
  691. ASSERT_EQ(status , PEP_STATUS_OK);
  692. ASSERT_NE(enc_msg , nullptr);
  693. enc_msg->recv_by = identity_dup(me_recip_2);
  694. // First reencryption - should give us a reencrypted message
  695. status = decrypt_message(session, enc_msg, &dec_msg, &keys, &rating, &flags);
  696. ASSERT_EQ(status , PEP_STATUS_OK);
  697. ASSERT_NE(dec_msg , nullptr);
  698. ASSERT_NE(flags & PEP_decrypt_flag_src_modified, 0);
  699. ASSERT_NE(enc_msg , nullptr);
  700. // Second reencryption - should NOT give us a reencrypted message
  701. output_stream << "CHECK: Do it again, and make sure there's no modified source!" << endl;
  702. free_message(dec_msg);
  703. dec_msg = NULL;
  704. flags = PEP_decrypt_flag_untrusted_server;
  705. free_stringlist(keys);
  706. keys = new_stringlist(fpr_pub_extra_key_0);
  707. stringlist_add(keys, fpr_pub_extra_key_1);
  708. status = decrypt_message(session, enc_msg, &dec_msg, &keys, &rating, &flags);
  709. ASSERT_EQ(status , PEP_STATUS_OK);
  710. ASSERT_NE(dec_msg , nullptr);
  711. ASSERT_EQ(flags & PEP_decrypt_flag_src_modified, 0);
  712. ASSERT_NE(enc_msg , nullptr);
  713. ASSERT_NE(dec_msg->_sender_fpr, nullptr);
  714. ASSERT_NE(keys, nullptr);
  715. ASSERT_STRNE(dec_msg->_sender_fpr, keys->value);
  716. free_stringlist(keys);
  717. free_message(enc_msg);
  718. free_message(dec_msg);
  719. free_identity(me_recip_1);
  720. free_identity(me_recip_2);
  721. }
  722. TEST_F(ReencryptPlusExtraKeysTest, check_efficient_reencrypt_from_enigmail_w_own_recip_in_bcc) {
  723. output_stream << "Call MIME_decrypt_message with reencrypt flag set on message sent with recip 2 in BCC from enigmail with extra keys." << endl;
  724. PEP_STATUS status = PEP_STATUS_OK;
  725. /* import all the keys */
  726. import_reenc_test_keys();
  727. // Set up own identities
  728. pEp_identity* me_recip_1 = new_identity("reencrypt_recip@darthmama.cool", fpr_own_recip_key, PEP_OWN_USERID, "Me Recipient");
  729. pEp_identity* me_recip_2 = new_identity("reencrypt_recip_numero_deux_test@darthmama.org", fpr_own_recip_2_key, PEP_OWN_USERID, "Me Recipient");
  730. output_stream << "Inserting own identities and keys into database." << endl;
  731. status = set_own_key(session, me_recip_2, fpr_own_recip_2_key);
  732. ASSERT_EQ(status , PEP_STATUS_OK);
  733. output_stream << "Done: inserting own identities and keys into database." << endl;
  734. status = set_identity_flags(session, me_recip_2, PEP_idf_org_ident);
  735. ASSERT_EQ(status , PEP_STATUS_OK);
  736. status = myself(session, me_recip_2);
  737. ASSERT_OK;
  738. ASSERT_EQ(me_recip_2->flags, me_recip_2->flags & PEP_idf_org_ident);
  739. // BEGIN ACTUAL TEST
  740. // Ready to receive message for the first time
  741. const string to_reencrypt_from_enigmail_BCC = slurp("test_mails/reencrypt_BCC_sent_by_enigmail.eml");
  742. message* dec_msg = NULL;
  743. message* enc_msg = NULL;
  744. // In: extra keys; Out: keys that were used to encrypt this.
  745. stringlist_t* keys = new_stringlist(fpr_pub_extra_key_0);
  746. stringlist_add(keys, fpr_pub_extra_key_1);
  747. PEP_decrypt_flags_t flags = 0;
  748. PEP_rating rating;
  749. flags = PEP_decrypt_flag_untrusted_server;
  750. // Put the original message into a message struct
  751. status = mime_decode_message(to_reencrypt_from_enigmail_BCC.c_str(), to_reencrypt_from_enigmail_BCC.size(), &enc_msg, NULL);
  752. enc_msg->recv_by = identity_dup(me_recip_2);
  753. ASSERT_EQ(status , PEP_STATUS_OK);
  754. ASSERT_NE(enc_msg , nullptr);
  755. // First reencryption - should give us a reencrypted message
  756. status = decrypt_message(session, enc_msg, &dec_msg, &keys, &rating, &flags);
  757. ASSERT_EQ(status , PEP_STATUS_OK);
  758. ASSERT_NE(dec_msg , nullptr);
  759. ASSERT_NE(flags & PEP_decrypt_flag_src_modified, 0);
  760. ASSERT_NE(enc_msg , nullptr);
  761. // Second reencryption - should NOT give us a reencrypted message
  762. output_stream << "CHECK: Do it again, and make sure there's no modified source!" << endl;
  763. free_message(dec_msg);
  764. dec_msg = NULL;
  765. flags = PEP_decrypt_flag_untrusted_server;
  766. free_stringlist(keys);
  767. keys = new_stringlist(fpr_pub_extra_key_0);
  768. stringlist_add(keys, fpr_pub_extra_key_1);
  769. status = decrypt_message(session, enc_msg, &dec_msg, &keys, &rating, &flags);
  770. ASSERT_EQ(status , PEP_STATUS_OK);
  771. ASSERT_NE(dec_msg , nullptr);
  772. ASSERT_EQ(flags & PEP_decrypt_flag_src_modified, 0);
  773. ASSERT_NE(enc_msg , nullptr);
  774. ASSERT_NE(dec_msg->_sender_fpr, nullptr);
  775. ASSERT_NE(keys, nullptr);
  776. ASSERT_STRNE(dec_msg->_sender_fpr, keys->value);
  777. free_stringlist(keys);
  778. free_message(enc_msg);
  779. free_message(dec_msg);
  780. free_identity(me_recip_1);
  781. free_identity(me_recip_2);
  782. }
  783. TEST_F(ReencryptPlusExtraKeysTest, check_efficient_reencrypt_from_pEp_2_0) {
  784. output_stream << "Call MIME_decrypt_message with reencrypt flag set on message generated by pEp (message 2.0) with extra keys." << endl;
  785. PEP_STATUS status = PEP_STATUS_OK;
  786. /* import all the keys */
  787. import_reenc_test_keys();
  788. // Set up own identities
  789. pEp_identity* me_recip_1 = new_identity("reencrypt_recip@darthmama.cool", fpr_own_recip_key, PEP_OWN_USERID, "Me Recipient");
  790. pEp_identity* me_recip_2 = new_identity("reencrypt_recip_numero_deux_test@darthmama.org", fpr_own_recip_2_key, PEP_OWN_USERID, "Me Recipient");
  791. output_stream << "Inserting own identities and keys into database." << endl;
  792. status = set_own_key(session, me_recip_2, fpr_own_recip_2_key);
  793. ASSERT_EQ(status , PEP_STATUS_OK);
  794. output_stream << "Done: inserting own identities and keys into database." << endl;
  795. status = set_identity_flags(session, me_recip_2, PEP_idf_org_ident);
  796. ASSERT_EQ(status , PEP_STATUS_OK);
  797. status = myself(session, me_recip_2);
  798. ASSERT_OK;
  799. ASSERT_EQ(me_recip_2->flags, me_recip_2->flags & PEP_idf_org_ident);
  800. // BEGIN ACTUAL TEST
  801. // Ready to receive message for the first time
  802. const string to_reencrypt_from_pEp = slurp("test_mails/reencrypt_encrypted_through_pEp.eml");
  803. message* dec_msg = NULL;
  804. message* enc_msg = NULL;
  805. // In: extra keys; Out: keys that were used to encrypt this.
  806. stringlist_t* keys = new_stringlist(fpr_pub_extra_key_0);
  807. stringlist_add(keys, fpr_pub_extra_key_1);
  808. PEP_decrypt_flags_t flags = 0;
  809. PEP_rating rating;
  810. flags = PEP_decrypt_flag_untrusted_server;
  811. // Put the original message into a message struct
  812. status = mime_decode_message(to_reencrypt_from_pEp.c_str(), to_reencrypt_from_pEp.size(), &enc_msg, NULL);
  813. enc_msg->recv_by = identity_dup(me_recip_2);
  814. ASSERT_OK;
  815. ASSERT_NE(enc_msg , nullptr);
  816. // First reencryption - should give us a reencrypted message
  817. status = decrypt_message(session, enc_msg, &dec_msg, &keys, &rating, &flags);
  818. ASSERT_NE(status, PEP_CANNOT_REENCRYPT);
  819. ASSERT_EQ(status , PEP_STATUS_OK);
  820. ASSERT_NE(dec_msg , nullptr);
  821. ASSERT_NE(flags & PEP_decrypt_flag_src_modified, 0);
  822. ASSERT_NE(enc_msg , nullptr);
  823. // Second reencryption - should NOT give us a reencrypted message
  824. output_stream << "CHECK: Do it again, and make sure there's no modified source!" << endl;
  825. free_message(dec_msg);
  826. dec_msg = NULL;
  827. flags = PEP_decrypt_flag_untrusted_server;
  828. free_stringlist(keys);
  829. keys = new_stringlist(fpr_pub_extra_key_0);
  830. stringlist_add(keys, fpr_pub_extra_key_1);
  831. status = decrypt_message(session, enc_msg, &dec_msg, &keys, &rating, &flags);
  832. ASSERT_EQ(status , PEP_STATUS_OK);
  833. ASSERT_NE(dec_msg , nullptr);
  834. ASSERT_EQ(flags & PEP_decrypt_flag_src_modified, 0);
  835. ASSERT_NE(enc_msg , nullptr);
  836. ASSERT_NE(dec_msg->_sender_fpr, nullptr);
  837. ASSERT_NE(keys, nullptr);
  838. ASSERT_STRNE(dec_msg->_sender_fpr, keys->value);
  839. free_stringlist(keys);
  840. free_message(enc_msg);
  841. free_message(dec_msg);
  842. free_identity(me_recip_1);
  843. free_identity(me_recip_2);
  844. }