| 1 | #include <atomic> |
| 2 | |
| 3 | #include <gtest/gtest.h> |
| 4 | |
| 5 | #include <nlohmann/json.hpp> |
| 6 | |
| 7 | #include "mtxclient/crypto/client.hpp" |
| 8 | #include "mtxclient/crypto/types.hpp" |
| 9 | #include "mtxclient/http/client.hpp" |
| 10 | |
| 11 | #include "mtx/requests.hpp" |
| 12 | #include "mtx/responses.hpp" |
| 13 | |
| 14 | #include "test_helpers.hpp" |
| 15 | |
| 16 | using namespace mtx::client; |
| 17 | using namespace mtx::http; |
| 18 | using namespace mtx::crypto; |
| 19 | |
| 20 | using namespace mtx::identifiers; |
| 21 | using namespace mtx::events; |
| 22 | using namespace mtx::events::msg; |
| 23 | using namespace mtx::events::collections; |
| 24 | using namespace mtx::responses; |
| 25 | |
| 26 | using namespace std; |
| 27 | |
| 28 | using namespace nlohmann; |
| 29 | |
| 30 | struct OlmMessage |
| 31 | { |
| 32 | std::string sender_key; |
| 33 | std::string sender; |
| 34 | |
| 35 | using RecipientKey = std::string; |
| 36 | std::map<RecipientKey, OlmCipherContent> ciphertext; |
| 37 | }; |
| 38 | |
| 39 | constexpr auto OLM_ALGO = "m.olm.v1.curve25519-aes-sha2"; |
| 40 | |
| 41 | inline void |
| 42 | from_json(const nlohmann::json &obj, OlmMessage &msg) |
| 43 | { |
| 44 | if (obj.at("type") != "m.room.encrypted") { |
| 45 | throw std::invalid_argument("invalid type for olm message"); |
| 46 | } |
| 47 | |
| 48 | if (obj.at("content").at("algorithm") != OLM_ALGO) |
| 49 | throw std::invalid_argument("invalid algorithm for olm message"); |
| 50 | |
| 51 | msg.sender = obj.at("sender").get<std::string>(); |
| 52 | msg.sender_key = obj.at("content").at("sender_key").get<std::string>(); |
| 53 | msg.ciphertext = |
| 54 | obj.at("content").at("ciphertext").get<std::map<std::string, OlmCipherContent>>(); |
| 55 | } |
| 56 | |
| 57 | mtx::requests::UploadKeys |
| 58 | generate_keys(std::shared_ptr<mtx::crypto::OlmClient> account) |
| 59 | { |
| 60 | auto idks = account->identity_keys(); |
| 61 | account->generate_one_time_keys(1); |
| 62 | auto otks = account->one_time_keys(); |
| 63 | |
| 64 | return account->create_upload_keys_request(otks, {}); |
| 65 | } |
| 66 | |
| 67 | TEST(Encryption, UploadIdentityKeys) |
| 68 | { |
| 69 | auto alice = make_test_client(); |
| 70 | auto olm_account = std::make_shared<mtx::crypto::OlmClient>(); |
| 71 | |
| 72 | EXPECT_THROW(olm_account->identity_keys(), olm_exception); |
| 73 | |
| 74 | olm_account->create_new_account(); |
| 75 | |
| 76 | alice->login( |
| 77 | "alice", "secret", [](const mtx::responses::Login &, RequestErr err) { check_error(err); }); |
| 78 | |
| 79 | while (alice->access_token().empty()) |
| 80 | sleep(); |
| 81 | |
| 82 | olm_account->set_user_id(alice->user_id().to_string()); |
| 83 | olm_account->set_device_id(alice->device_id()); |
| 84 | |
| 85 | auto id_keys = olm_account->identity_keys(); |
| 86 | |
| 87 | ASSERT_TRUE(id_keys.curve25519.size() > 10); |
| 88 | ASSERT_TRUE(id_keys.curve25519.size() > 10); |
| 89 | |
| 90 | mtx::crypto::OneTimeKeys unused; |
| 91 | auto request = olm_account->create_upload_keys_request(unused, {}); |
| 92 | |
| 93 | // Make the request with the signed identity keys. |
| 94 | alice->upload_keys(request, [](const mtx::responses::UploadKeys &res, RequestErr err) { |
| 95 | check_error(err); |
| 96 | for (const auto &e : res.one_time_key_counts) |
| 97 | EXPECT_EQ(e.second, 0); |
| 98 | }); |
| 99 | |
| 100 | alice->close(); |
| 101 | } |
| 102 | |
| 103 | TEST(Encryption, UploadOneTimeKeys) |
| 104 | { |
| 105 | auto alice = make_test_client(); |
| 106 | auto olm_account = std::make_shared<mtx::crypto::OlmClient>(); |
| 107 | olm_account->create_new_account(); |
| 108 | |
| 109 | alice->login( |
| 110 | "alice", "secret", [](const mtx::responses::Login &, RequestErr err) { check_error(err); }); |
| 111 | |
| 112 | while (alice->access_token().empty()) |
| 113 | sleep(); |
| 114 | |
| 115 | olm_account->set_user_id(alice->user_id().to_string()); |
| 116 | olm_account->set_device_id(alice->device_id()); |
| 117 | |
| 118 | auto nkeys = olm_account->generate_one_time_keys(5, true); |
| 119 | EXPECT_EQ(nkeys, 5); |
| 120 | |
| 121 | mtx::requests::UploadKeys req = olm_account->create_upload_keys_request(); |
| 122 | |
| 123 | alice->upload_keys(req, [](const mtx::responses::UploadKeys &res, RequestErr err) { |
| 124 | check_error(err); |
| 125 | ASSERT_TRUE(res.one_time_key_counts.find("signed_curve25519") != |
| 126 | res.one_time_key_counts.end()); |
| 127 | EXPECT_EQ(res.one_time_key_counts.at("signed_curve25519"), 5); |
| 128 | }); |
| 129 | |
| 130 | alice->close(); |
| 131 | } |
| 132 | |
| 133 | TEST(Encryption, UploadSignedOneTimeKeys) |
| 134 | { |
| 135 | auto alice = make_test_client(); |
| 136 | auto olm_account = std::make_shared<mtx::crypto::OlmClient>(); |
| 137 | olm_account->create_new_account(); |
| 138 | |
| 139 | alice->login( |
| 140 | "alice", "secret", [](const mtx::responses::Login &, RequestErr err) { check_error(err); }); |
| 141 | |
| 142 | while (alice->access_token().empty()) |
| 143 | sleep(); |
| 144 | |
| 145 | olm_account->set_user_id(alice->user_id().to_string()); |
| 146 | olm_account->set_device_id(alice->device_id()); |
| 147 | |
| 148 | auto nkeys = olm_account->generate_one_time_keys(5); |
| 149 | EXPECT_EQ(nkeys, 5); |
| 150 | |
| 151 | auto one_time_keys = olm_account->one_time_keys(); |
| 152 | |
| 153 | mtx::requests::UploadKeys req; |
| 154 | for (const auto &[key_id, key] : olm_account->sign_one_time_keys(one_time_keys)) |
| 155 | req.one_time_keys[key_id] = key; |
| 156 | |
| 157 | alice->upload_keys(req, [nkeys](const mtx::responses::UploadKeys &res, RequestErr err) { |
| 158 | check_error(err); |
| 159 | EXPECT_EQ(res.one_time_key_counts.size(), 1); |
| 160 | EXPECT_EQ(res.one_time_key_counts.at("signed_curve25519"), nkeys); |
| 161 | }); |
| 162 | |
| 163 | alice->close(); |
| 164 | } |
| 165 | |
| 166 | TEST(Encryption, UploadKeys) |
| 167 | { |
| 168 | auto alice = make_test_client(); |
| 169 | auto olm_account = std::make_shared<mtx::crypto::OlmClient>(); |
| 170 | olm_account->create_new_account(); |
| 171 | |
| 172 | alice->login( |
| 173 | "alice", "secret", [](const mtx::responses::Login &, RequestErr err) { check_error(err); }); |
| 174 | |
| 175 | while (alice->access_token().empty()) |
| 176 | sleep(); |
| 177 | |
| 178 | olm_account->set_user_id(alice->user_id().to_string()); |
| 179 | olm_account->set_device_id(alice->device_id()); |
| 180 | |
| 181 | auto req = generate_keys(olm_account); |
| 182 | |
| 183 | alice->upload_keys(req, [](const mtx::responses::UploadKeys &res, RequestErr err) { |
| 184 | check_error(err); |
| 185 | EXPECT_EQ(res.one_time_key_counts.size(), 1); |
| 186 | EXPECT_EQ(res.one_time_key_counts.at("signed_curve25519"), 1); |
| 187 | }); |
| 188 | |
| 189 | alice->close(); |
| 190 | } |
| 191 | |
| 192 | TEST(Encryption, QueryKeys) |
| 193 | { |
| 194 | auto alice = make_test_client(); |
| 195 | auto alice_olm = std::make_shared<mtx::crypto::OlmClient>(); |
| 196 | |
| 197 | auto bob = make_test_client(); |
| 198 | auto bob_olm = std::make_shared<mtx::crypto::OlmClient>(); |
| 199 | |
| 200 | alice_olm->create_new_account(); |
| 201 | bob_olm->create_new_account(); |
| 202 | |
| 203 | alice->login( |
| 204 | "alice", "secret", [](const mtx::responses::Login &, RequestErr err) { check_error(err); }); |
| 205 | |
| 206 | bob->login( |
| 207 | "bob", "secret", [](const mtx::responses::Login &, RequestErr err) { check_error(err); }); |
| 208 | |
| 209 | while (alice->access_token().empty() || bob->access_token().empty()) |
| 210 | sleep(); |
| 211 | |
| 212 | alice_olm->set_user_id(alice->user_id().to_string()); |
| 213 | alice_olm->set_device_id(alice->device_id()); |
| 214 | |
| 215 | bob_olm->set_user_id(bob->user_id().to_string()); |
| 216 | bob_olm->set_device_id(bob->device_id()); |
| 217 | |
| 218 | // Create and upload keys for both users. |
| 219 | auto alice_req = ::generate_keys(alice_olm); |
| 220 | auto bob_req = ::generate_keys(bob_olm); |
| 221 | |
| 222 | // Validates that both upload requests are finished. |
| 223 | atomic_int uploads(0); |
| 224 | |
| 225 | alice->upload_keys(alice_req, |
| 226 | [&uploads](const mtx::responses::UploadKeys &res, RequestErr err) { |
| 227 | check_error(err); |
| 228 | EXPECT_EQ(res.one_time_key_counts.size(), 1); |
| 229 | EXPECT_EQ(res.one_time_key_counts.at("signed_curve25519"), 1); |
| 230 | |
| 231 | uploads += 1; |
| 232 | }); |
| 233 | |
| 234 | bob->upload_keys(bob_req, [&uploads](const mtx::responses::UploadKeys &res, RequestErr err) { |
| 235 | check_error(err); |
| 236 | EXPECT_EQ(res.one_time_key_counts.size(), 1); |
| 237 | EXPECT_EQ(res.one_time_key_counts.at("signed_curve25519"), 1); |
| 238 | |
| 239 | uploads += 1; |
| 240 | }); |
| 241 | |
| 242 | while (uploads != 2) |
| 243 | sleep(); |
| 244 | |
| 245 | atomic_int responses(0); |
| 246 | |
| 247 | // Each user is requests each other's keys. |
| 248 | mtx::requests::QueryKeys alice_rk; |
| 249 | alice_rk.device_keys[bob->user_id().to_string()] = {}; |
| 250 | alice->query_keys( |
| 251 | alice_rk, [&responses, bob, bob_req](const mtx::responses::QueryKeys &res, RequestErr err) { |
| 252 | check_error(err); |
| 253 | |
| 254 | ASSERT_TRUE(res.failures.size() == 0); |
| 255 | |
| 256 | auto bob_devices = res.device_keys.at(bob->user_id().to_string()); |
| 257 | ASSERT_TRUE(bob_devices.size() > 0); |
| 258 | |
| 259 | auto dev_keys = bob_devices.at(bob->device_id()); |
| 260 | EXPECT_EQ(dev_keys.user_id, bob->user_id().to_string()); |
| 261 | EXPECT_EQ(dev_keys.device_id, bob->device_id()); |
| 262 | EXPECT_EQ(dev_keys.keys, bob_req.device_keys.keys); |
| 263 | EXPECT_EQ(dev_keys.signatures, bob_req.device_keys.signatures); |
| 264 | |
| 265 | responses += 1; |
| 266 | }); |
| 267 | |
| 268 | mtx::requests::QueryKeys bob_rk; |
| 269 | bob_rk.device_keys[alice->user_id().to_string()] = {}; |
| 270 | bob->query_keys( |
| 271 | bob_rk, [&responses, alice, alice_req](const mtx::responses::QueryKeys &res, RequestErr err) { |
| 272 | check_error(err); |
| 273 | |
| 274 | ASSERT_TRUE(res.failures.size() == 0); |
| 275 | |
| 276 | auto bob_devices = res.device_keys.at(alice->user_id().to_string()); |
| 277 | ASSERT_TRUE(bob_devices.size() > 0); |
| 278 | |
| 279 | auto dev_keys = bob_devices.at(alice->device_id()); |
| 280 | EXPECT_EQ(dev_keys.user_id, alice->user_id().to_string()); |
| 281 | EXPECT_EQ(dev_keys.device_id, alice->device_id()); |
| 282 | EXPECT_EQ(dev_keys.keys, alice_req.device_keys.keys); |
| 283 | EXPECT_EQ(dev_keys.signatures, alice_req.device_keys.signatures); |
| 284 | |
| 285 | responses += 1; |
| 286 | }); |
| 287 | |
| 288 | while (responses != 2) |
| 289 | sleep(); |
| 290 | |
| 291 | alice->close(); |
| 292 | bob->close(); |
| 293 | } |
| 294 | |
| 295 | TEST(Encryption, ClaimKeys) |
| 296 | { |
| 297 | using namespace mtx::crypto; |
| 298 | |
| 299 | auto alice = make_test_client(); |
| 300 | auto alice_olm = std::make_shared<OlmClient>(); |
| 301 | |
| 302 | alice_olm->create_new_account(); |
| 303 | alice->login("alice", "secret", check_login); |
| 304 | |
| 305 | auto bob = make_test_client(); |
| 306 | auto bob_olm = std::make_shared<OlmClient>(); |
| 307 | |
| 308 | bob_olm->create_new_account(); |
| 309 | bob->login("bob", "secret", check_login); |
| 310 | |
| 311 | while (alice->access_token().empty() || bob->access_token().empty()) |
| 312 | sleep(); |
| 313 | |
| 314 | alice_olm->set_user_id(alice->user_id().to_string()); |
| 315 | alice_olm->set_device_id(alice->device_id()); |
| 316 | |
| 317 | bob_olm->set_user_id(bob->user_id().to_string()); |
| 318 | bob_olm->set_device_id(bob->device_id()); |
| 319 | |
| 320 | atomic_bool uploaded(false); |
| 321 | |
| 322 | // Bob uploads his keys. |
| 323 | bob_olm->generate_one_time_keys(1); |
| 324 | bob->upload_keys(bob_olm->create_upload_keys_request(), |
| 325 | [&uploaded](const mtx::responses::UploadKeys &res, RequestErr err) { |
| 326 | check_error(err); |
| 327 | EXPECT_EQ(res.one_time_key_counts.size(), 1); |
| 328 | EXPECT_EQ(res.one_time_key_counts.at("signed_curve25519"), 1); |
| 329 | uploaded = true; |
| 330 | }); |
| 331 | |
| 332 | // Waiting for the upload to finish. |
| 333 | while (!uploaded) |
| 334 | sleep(); |
| 335 | |
| 336 | // Alice retrieves bob's keys & claims one signed one-time key. |
| 337 | mtx::requests::QueryKeys alice_rk; |
| 338 | alice_rk.device_keys[bob->user_id().to_string()] = {}; |
| 339 | alice->query_keys( |
| 340 | alice_rk, [alice_olm, alice, bob](const mtx::responses::QueryKeys &res, RequestErr err) { |
| 341 | check_error(err); |
| 342 | |
| 343 | auto bob_devices = res.device_keys.at(bob->user_id().to_string()); |
| 344 | ASSERT_TRUE(bob_devices.size() > 0); |
| 345 | |
| 346 | auto devices = {bob->device_id()}; |
| 347 | |
| 348 | // Retrieve the identity key for the current device. |
| 349 | auto bob_ed25519 = |
| 350 | bob_devices.at(bob->device_id()).keys.at("ed25519:" + bob->device_id()); |
| 351 | |
| 352 | const auto current_device = bob_devices.at(bob->device_id()); |
| 353 | |
| 354 | // Verify signature. |
| 355 | ASSERT_TRUE(verify_identity_signature(json(current_device).get<mtx::crypto::DeviceKeys>(), |
| 356 | DeviceId(bob->device_id()), |
| 357 | UserId(bob->user_id().to_string()))); |
| 358 | |
| 359 | mtx::requests::ClaimKeys claim_keys; |
| 360 | for (const auto &d : devices) |
| 361 | claim_keys.one_time_keys[bob->user_id().to_string()][d] = SIGNED_CURVE25519; |
| 362 | alice->claim_keys( |
| 363 | claim_keys, |
| 364 | [alice_olm, bob, bob_ed25519](const mtx::responses::ClaimKeys &res, RequestErr err) { |
| 365 | check_error(err); |
| 366 | |
| 367 | const auto user_id = bob->user_id().to_string(); |
| 368 | const auto device_id = bob->device_id(); |
| 369 | |
| 370 | // The device exists. |
| 371 | EXPECT_EQ(res.one_time_keys.size(), 1); |
| 372 | EXPECT_EQ(res.one_time_keys.at(user_id).size(), 1); |
| 373 | |
| 374 | // The key is the one bob sent. |
| 375 | auto one_time_key = res.one_time_keys.at(user_id).at(device_id); |
| 376 | ASSERT_TRUE(one_time_key.is_object()); |
| 377 | |
| 378 | auto algo = one_time_key.begin().key(); |
| 379 | auto contents = one_time_key.begin().value(); |
| 380 | }); |
| 381 | }); |
| 382 | |
| 383 | alice->close(); |
| 384 | bob->close(); |
| 385 | } |
| 386 | |
| 387 | TEST(Encryption, ClaimMultipleDeviceKeys) |
| 388 | { |
| 389 | using namespace mtx::crypto; |
| 390 | |
| 391 | // Login with alice multiple times |
| 392 | auto alice1 = make_test_client(); |
| 393 | auto alice2 = make_test_client(); |
| 394 | auto alice3 = make_test_client(); |
| 395 | alice1->login("alice", "secret", check_login); |
| 396 | alice2->login("alice", "secret", check_login); |
| 397 | alice3->login("alice", "secret", check_login); |
| 398 | |
| 399 | WAIT_UNTIL(!alice1->access_token().empty() && !alice2->access_token().empty() && |
| 400 | !alice3->access_token().empty()) |
| 401 | |
| 402 | atomic_int uploads(0); |
| 403 | auto upload_cb = [&uploads](const mtx::responses::UploadKeys &res, RequestErr err) { |
| 404 | check_error(err); |
| 405 | EXPECT_EQ(res.one_time_key_counts.size(), 1); |
| 406 | EXPECT_EQ(res.one_time_key_counts.at("signed_curve25519"), 10); |
| 407 | uploads += 1; |
| 408 | }; |
| 409 | |
| 410 | auto olm1 = std::make_shared<OlmClient>(); |
| 411 | olm1->create_new_account(); |
| 412 | olm1->generate_one_time_keys(10); |
| 413 | olm1->set_user_id(alice1->user_id().to_string()); |
| 414 | olm1->set_device_id(alice1->device_id()); |
| 415 | |
| 416 | auto olm2 = std::make_shared<OlmClient>(); |
| 417 | olm2->create_new_account(); |
| 418 | olm2->generate_one_time_keys(10); |
| 419 | olm2->set_user_id(alice2->user_id().to_string()); |
| 420 | olm2->set_device_id(alice2->device_id()); |
| 421 | |
| 422 | auto olm3 = std::make_shared<OlmClient>(); |
| 423 | olm3->create_new_account(); |
| 424 | olm3->generate_one_time_keys(10); |
| 425 | olm3->set_user_id(alice3->user_id().to_string()); |
| 426 | olm3->set_device_id(alice3->device_id()); |
| 427 | |
| 428 | alice1->upload_keys(olm1->create_upload_keys_request(), upload_cb); |
| 429 | alice2->upload_keys(olm2->create_upload_keys_request(), upload_cb); |
| 430 | alice3->upload_keys(olm3->create_upload_keys_request(), upload_cb); |
| 431 | |
| 432 | WAIT_UNTIL(uploads == 3); |
| 433 | |
| 434 | // Bob will claim all keys from alice |
| 435 | auto bob = make_test_client(); |
| 436 | bob->login("bob", "secret", check_login); |
| 437 | |
| 438 | WAIT_UNTIL(!bob->access_token().empty()) |
| 439 | |
| 440 | std::vector<std::string> devices_; |
| 441 | devices_.push_back(alice1->device_id()); |
| 442 | devices_.push_back(alice2->device_id()); |
| 443 | devices_.push_back(alice3->device_id()); |
| 444 | |
| 445 | mtx::requests::ClaimKeys claim_keys; |
| 446 | for (const auto &d : devices_) |
| 447 | claim_keys.one_time_keys[alice1->user_id().to_string()][d] = SIGNED_CURVE25519; |
| 448 | |
| 449 | bob->claim_keys(claim_keys, |
| 450 | [user_id = alice1->user_id().to_string()](const mtx::responses::ClaimKeys &res, |
| 451 | RequestErr err) { |
| 452 | check_error(err); |
| 453 | auto retrieved_devices = res.one_time_keys.at(user_id); |
| 454 | EXPECT_EQ(retrieved_devices.size(), 3); |
| 455 | }); |
| 456 | |
| 457 | bob->close(); |
| 458 | |
| 459 | alice1->close(); |
| 460 | alice2->close(); |
| 461 | alice3->close(); |
| 462 | } |
| 463 | |
| 464 | TEST(Encryption, UploadCrossSigningKeys) |
| 465 | { |
| 466 | auto alice = make_test_client(); |
| 467 | auto olm_account = std::make_shared<mtx::crypto::OlmClient>(); |
| 468 | |
| 469 | EXPECT_THROW(olm_account->identity_keys(), olm_exception); |
| 470 | |
| 471 | olm_account->create_new_account(); |
| 472 | |
| 473 | alice->login( |
| 474 | "alice", "secret", [](const mtx::responses::Login &, RequestErr err) { check_error(err); }); |
| 475 | |
| 476 | while (alice->access_token().empty()) |
| 477 | sleep(); |
| 478 | |
| 479 | olm_account->set_user_id(alice->user_id().to_string()); |
| 480 | olm_account->set_device_id(alice->device_id()); |
| 481 | |
| 482 | auto id_keys = olm_account->identity_keys(); |
| 483 | |
| 484 | ASSERT_TRUE(id_keys.curve25519.size() > 10); |
| 485 | ASSERT_TRUE(id_keys.curve25519.size() > 10); |
| 486 | |
| 487 | mtx::crypto::OneTimeKeys unused; |
| 488 | auto request = olm_account->create_upload_keys_request(unused, {}); |
| 489 | |
| 490 | // Make the request with the signed identity keys. |
| 491 | alice->upload_keys( |
| 492 | request, [](const mtx::responses::UploadKeys &, RequestErr err) { check_error(err); }); |
| 493 | |
| 494 | auto xsign_keys = olm_account->create_crosssigning_keys(); |
| 495 | ASSERT_TRUE(xsign_keys.has_value()); |
| 496 | mtx::requests::DeviceSigningUpload u; |
| 497 | u.master_key = xsign_keys->master_key; |
| 498 | u.user_signing_key = xsign_keys->user_signing_key; |
| 499 | u.self_signing_key = xsign_keys->self_signing_key; |
| 500 | alice->device_signing_upload( |
| 501 | u, |
| 502 | mtx::http::UIAHandler([](const mtx::http::UIAHandler &h, |
| 503 | const mtx::user_interactive::Unauthorized &unauthorized) { |
| 504 | ASSERT_EQ(unauthorized.flows.size(), 1); |
| 505 | ASSERT_EQ(unauthorized.flows[0].stages.size(), 1); |
| 506 | ASSERT_EQ(unauthorized.flows[0].stages[0], mtx::user_interactive::auth_types::password); |
| 507 | |
| 508 | mtx::user_interactive::Auth auth; |
| 509 | auth.session = unauthorized.session; |
| 510 | mtx::user_interactive::auth::Password pass{}; |
| 511 | pass.password = "secret"; |
| 512 | pass.identifier_user = "alice"; |
| 513 | pass.identifier_type = mtx::user_interactive::auth::Password::IdType::UserId; |
| 514 | auth.content = pass; |
| 515 | h.next(auth); |
| 516 | }), |
| 517 | [](RequestErr e) { check_error(e); }); |
| 518 | |
| 519 | alice->close(); |
| 520 | } |
| 521 | |
| 522 | TEST(Encryption, UploadOnlineBackup) |
| 523 | { |
| 524 | auto alice = make_test_client(); |
| 525 | auto olm_account = std::make_shared<mtx::crypto::OlmClient>(); |
| 526 | |
| 527 | EXPECT_THROW(olm_account->identity_keys(), olm_exception); |
| 528 | |
| 529 | olm_account->create_new_account(); |
| 530 | |
| 531 | alice->login( |
| 532 | "alice", "secret", [](const mtx::responses::Login &, RequestErr err) { check_error(err); }); |
| 533 | |
| 534 | while (alice->access_token().empty()) |
| 535 | sleep(); |
| 536 | |
| 537 | olm_account->set_user_id(alice->user_id().to_string()); |
| 538 | olm_account->set_device_id(alice->device_id()); |
| 539 | |
| 540 | auto id_keys = olm_account->identity_keys(); |
| 541 | |
| 542 | ASSERT_TRUE(id_keys.curve25519.size() > 10); |
| 543 | ASSERT_TRUE(id_keys.curve25519.size() > 10); |
| 544 | |
| 545 | mtx::crypto::OneTimeKeys unused; |
| 546 | auto request = olm_account->create_upload_keys_request(unused, {}); |
| 547 | |
| 548 | // Make the request with the signed identity keys. |
| 549 | alice->upload_keys( |
| 550 | request, [](const mtx::responses::UploadKeys &, RequestErr err) { check_error(err); }); |
| 551 | |
| 552 | auto xsign_keys = olm_account->create_crosssigning_keys(); |
| 553 | ASSERT_TRUE(xsign_keys.has_value()); |
| 554 | mtx::requests::DeviceSigningUpload u; |
| 555 | u.master_key = xsign_keys->master_key; |
| 556 | u.user_signing_key = xsign_keys->user_signing_key; |
| 557 | u.self_signing_key = xsign_keys->self_signing_key; |
| 558 | alice->device_signing_upload( |
| 559 | u, |
| 560 | mtx::http::UIAHandler([](const mtx::http::UIAHandler &h, |
| 561 | const mtx::user_interactive::Unauthorized &unauthorized) { |
| 562 | ASSERT_EQ(unauthorized.flows.size(), 1); |
| 563 | ASSERT_EQ(unauthorized.flows[0].stages.size(), 1); |
| 564 | ASSERT_EQ(unauthorized.flows[0].stages[0], mtx::user_interactive::auth_types::password); |
| 565 | |
| 566 | mtx::user_interactive::Auth auth; |
| 567 | auth.session = unauthorized.session; |
| 568 | mtx::user_interactive::auth::Password pass{}; |
| 569 | pass.password = "secret"; |
| 570 | pass.identifier_user = "alice"; |
| 571 | pass.identifier_type = mtx::user_interactive::auth::Password::IdType::UserId; |
| 572 | auth.content = pass; |
| 573 | h.next(auth); |
| 574 | }), |
| 575 | [xsign_keys, olm_account, alice](RequestErr e) { |
| 576 | check_error(e); |
| 577 | |
| 578 | auto bk = olm_account->create_online_key_backup(xsign_keys->private_master_key); |
| 579 | alice->post_backup_version(bk->backupVersion.algorithm, |
| 580 | bk->backupVersion.auth_data, |
| 581 | [](const mtx::responses::Version &v, RequestErr e) { |
| 582 | check_error(e); |
| 583 | |
| 584 | EXPECT_FALSE(v.version.empty()); |
| 585 | }); |
| 586 | }); |
| 587 | |
| 588 | alice->close(); |
| 589 | } |
| 590 | |
| 591 | TEST(Encryption, KeyChanges) |
| 592 | { |
| 593 | auto carl = make_test_client(); |
| 594 | auto carl_olm = std::make_shared<mtx::crypto::OlmClient>(); |
| 595 | carl_olm->create_new_account(); |
| 596 | |
| 597 | carl->login( |
| 598 | "carl", "secret", [](const mtx::responses::Login &, RequestErr err) { check_error(err); }); |
| 599 | |
| 600 | while (carl->access_token().empty()) |
| 601 | sleep(); |
| 602 | |
| 603 | carl_olm->set_device_id(carl->device_id()); |
| 604 | carl_olm->set_user_id(carl->user_id().to_string()); |
| 605 | |
| 606 | mtx::requests::CreateRoom req; |
| 607 | carl->create_room(req, [carl, carl_olm](const mtx::responses::CreateRoom &, RequestErr err) { |
| 608 | check_error(err); |
| 609 | |
| 610 | // Carl syncs to get the first next_batch token. |
| 611 | SyncOpts opts; |
| 612 | opts.timeout = 0; |
| 613 | carl->sync(opts, [carl, carl_olm](const mtx::responses::Sync &res, RequestErr err) { |
| 614 | check_error(err); |
| 615 | const auto next_batch_token = res.next_batch; |
| 616 | |
| 617 | auto key_req = ::generate_keys(carl_olm); |
| 618 | |
| 619 | // Changes his keys. |
| 620 | carl->upload_keys( |
| 621 | key_req, |
| 622 | [carl, next_batch_token](const mtx::responses::UploadKeys &, RequestErr err) { |
| 623 | check_error(err); |
| 624 | |
| 625 | // The key changes should contain his username |
| 626 | // because of the key uploading. |
| 627 | carl->key_changes(next_batch_token, |
| 628 | "", |
| 629 | [carl](const mtx::responses::KeyChanges &res, RequestErr err) { |
| 630 | check_error(err); |
| 631 | |
| 632 | EXPECT_EQ(res.changed.size(), 1); |
| 633 | EXPECT_EQ(res.left.size(), 0); |
| 634 | |
| 635 | EXPECT_EQ(res.changed.at(0), carl->user_id().to_string()); |
| 636 | }); |
| 637 | }); |
| 638 | }); |
| 639 | }); |
| 640 | |
| 641 | carl->close(); |
| 642 | } |
| 643 | |
| 644 | TEST(Encryption, EnableEncryption) |
| 645 | { |
| 646 | auto bob = make_test_client(); |
| 647 | auto carl = make_test_client(); |
| 648 | |
| 649 | bob->login("bob", "secret", [](const Login &, RequestErr err) { check_error(err); }); |
| 650 | carl->login("carl", "secret", [](const Login &, RequestErr err) { check_error(err); }); |
| 651 | |
| 652 | while (bob->access_token().empty() || carl->access_token().empty()) |
| 653 | sleep(); |
| 654 | |
| 655 | atomic_int responses(0); |
| 656 | mtx::identifiers::Room joined_room; |
| 657 | |
| 658 | mtx::requests::CreateRoom req; |
| 659 | req.invite = {"@carl:" + server_name()}; |
| 660 | bob->create_room( |
| 661 | req, |
| 662 | [bob, carl, &responses, &joined_room](const mtx::responses::CreateRoom &res, RequestErr err) { |
| 663 | check_error(err); |
| 664 | joined_room = res.room_id; |
| 665 | |
| 666 | bob->enable_encryption(res.room_id.to_string(), |
| 667 | [&responses](const mtx::responses::EventId &, RequestErr err) { |
| 668 | check_error(err); |
| 669 | responses += 1; |
| 670 | }); |
| 671 | |
| 672 | carl->join_room(res.room_id.to_string(), |
| 673 | [&responses](const mtx::responses::RoomId &, RequestErr err) { |
| 674 | check_error(err); |
| 675 | responses += 1; |
| 676 | }); |
| 677 | }); |
| 678 | |
| 679 | while (responses != 2) |
| 680 | sleep(); |
| 681 | |
| 682 | SyncOpts opts; |
| 683 | opts.timeout = 0; |
| 684 | carl->sync(opts, [&joined_room](const Sync &res, RequestErr err) { |
| 685 | check_error(err); |
| 686 | |
| 687 | auto events = res.rooms.join.at(joined_room.to_string()).timeline.events; |
| 688 | |
| 689 | using namespace mtx::events; |
| 690 | |
| 691 | int has_encryption = 0; |
| 692 | for (const auto &e : events) { |
| 693 | if (std::holds_alternative<StateEvent<state::Encryption>>(e)) |
| 694 | has_encryption = 1; |
| 695 | } |
| 696 | |
| 697 | ASSERT_TRUE(has_encryption == 1); |
| 698 | }); |
| 699 | |
| 700 | bob->close(); |
| 701 | carl->close(); |
| 702 | } |
| 703 | |
| 704 | TEST(Encryption, CreateOutboundGroupSession) |
| 705 | { |
| 706 | auto alice = make_shared<mtx::crypto::OlmClient>(); |
| 707 | auto bob = make_shared<mtx::crypto::OlmClient>(); |
| 708 | |
| 709 | alice->create_new_account(); |
| 710 | bob->create_new_account(); |
| 711 | |
| 712 | bob->generate_one_time_keys(1); |
| 713 | alice->generate_one_time_keys(1); |
| 714 | |
| 715 | auto outbound_session = alice->init_outbound_group_session(); |
| 716 | |
| 717 | auto session_id = mtx::crypto::session_id(outbound_session.get()); |
| 718 | ASSERT_FALSE(session_id.empty()); |
| 719 | auto session_key = mtx::crypto::session_key(outbound_session.get()); |
| 720 | ASSERT_FALSE(session_key.empty()); |
| 721 | } |
| 722 | |
| 723 | TEST(Encryption, OlmSessions) |
| 724 | { |
| 725 | using namespace mtx::crypto; |
| 726 | |
| 727 | auto alice = std::make_shared<OlmClient>(); |
| 728 | alice->create_new_account(); |
| 729 | alice->generate_one_time_keys(1); |
| 730 | |
| 731 | auto bob = std::make_shared<OlmClient>(); |
| 732 | bob->create_new_account(); |
| 733 | bob->generate_one_time_keys(1); |
| 734 | |
| 735 | std::string alice_key = alice->identity_keys().curve25519; |
| 736 | std::string alice_one_time_key = alice->one_time_keys().curve25519.begin()->second; |
| 737 | |
| 738 | std::string bob_key = bob->identity_keys().curve25519; |
| 739 | std::string bob_one_time_key = bob->one_time_keys().curve25519.begin()->second; |
| 740 | |
| 741 | // Alice is preparing to send a pre-shared message to Bob by opening |
| 742 | // a new 1-1 outbound session. |
| 743 | auto alice_outbound_session = alice->create_outbound_session(bob_key, bob_one_time_key); |
| 744 | |
| 745 | // Alice encrypts the message using the current session. |
| 746 | auto plaintext = "Hello, Bob!"; |
| 747 | size_t msgtype = olm_encrypt_message_type(alice_outbound_session.get()); |
| 748 | auto ciphertext = alice->encrypt_message(alice_outbound_session.get(), plaintext); |
| 749 | auto ciphertext_str = std::string((char *)ciphertext.data(), ciphertext.size()); |
| 750 | |
| 751 | EXPECT_EQ(msgtype, 0); |
| 752 | |
| 753 | // Bob creates an inbound session to receive Alice's message. |
| 754 | auto bob_inbound_session = bob->create_inbound_session(ciphertext_str); |
| 755 | |
| 756 | // Bob validates that the message was meant for him. |
| 757 | ASSERT_EQ(1, matches_inbound_session(bob_inbound_session.get(), ciphertext_str)); |
| 758 | |
| 759 | // Bob validates that the message was sent from Alice. |
| 760 | ASSERT_EQ(1, |
| 761 | matches_inbound_session_from(bob_inbound_session.get(), alice_key, ciphertext_str)); |
| 762 | |
| 763 | // Bob validates that the message wasn't sent by someone else. |
| 764 | ASSERT_EQ(0, matches_inbound_session_from(bob_inbound_session.get(), bob_key, ciphertext_str)); |
| 765 | |
| 766 | // Bob decrypts the message |
| 767 | auto decrypted = bob->decrypt_message(bob_inbound_session.get(), msgtype, ciphertext_str); |
| 768 | |
| 769 | auto body_str = std::string((char *)decrypted.data(), decrypted.size()); |
| 770 | ASSERT_EQ(body_str, plaintext); |
| 771 | } |
| 772 | |
| 773 | TEST(Encryption, MegolmSessions) |
| 774 | { |
| 775 | auto alice = std::make_shared<OlmClient>(); |
| 776 | alice->create_new_account(); |
| 777 | alice->generate_one_time_keys(1); |
| 778 | |
| 779 | auto bob = std::make_shared<OlmClient>(); |
| 780 | bob->create_new_account(); |
| 781 | bob->generate_one_time_keys(1); |
| 782 | |
| 783 | // Alice wants to send an encrypted megolm message to Bob. |
| 784 | const std::string secret_message = "Hey, Bob!"; |
| 785 | |
| 786 | // Alice creates an outbound megolm session that will be used by both parties. |
| 787 | auto outbound_megolm_session = alice->init_outbound_group_session(); |
| 788 | auto msg_index = olm_outbound_group_session_message_index(outbound_megolm_session.get()); |
| 789 | ASSERT_EQ(msg_index, 0); |
| 790 | |
| 791 | // Alice extracts the session id & session key so she can share them with Bob. |
| 792 | const auto session_id = mtx::crypto::session_id(outbound_megolm_session.get()); |
| 793 | const auto session_key = mtx::crypto::session_key(outbound_megolm_session.get()); |
| 794 | |
| 795 | // Encrypt the message using megolm. |
| 796 | auto encrypted_secret_message = |
| 797 | alice->encrypt_group_message(outbound_megolm_session.get(), secret_message); |
| 798 | |
| 799 | msg_index = olm_outbound_group_session_message_index(outbound_megolm_session.get()); |
| 800 | ASSERT_EQ(msg_index, 1); |
| 801 | |
| 802 | // First she will create an outbound olm session so she can share the session data. |
| 803 | // Alice will need Bob's curve25519 key and one claimed one time key. |
| 804 | auto outbound_olm_session = alice->create_outbound_session( |
| 805 | bob->identity_keys().curve25519, bob->one_time_keys().curve25519.begin()->second); |
| 806 | const auto msg_type = olm_encrypt_message_type(outbound_olm_session.get()); |
| 807 | |
| 808 | // Plaintext version of the session data to be shared. |
| 809 | const auto session_data = json{{"session_id", session_id}, {"session_key", session_key}}; |
| 810 | |
| 811 | // Alice encrypts the session data using olm. |
| 812 | const auto encrypted_session_data = |
| 813 | alice->encrypt_message(outbound_olm_session.get(), session_data.dump()); |
| 814 | const auto encrypted_session_data_str = |
| 815 | std::string((char *)encrypted_session_data.data(), encrypted_session_data.size()); |
| 816 | |
| 817 | // |
| 818 | // Alice sends the olm & megolm messages to Bob ... |
| 819 | // |
| 820 | |
| 821 | // Bob creates an inbound olm session to receive the session data. |
| 822 | auto inbound_olm_session = bob->create_inbound_session(encrypted_session_data); |
| 823 | |
| 824 | // and validates that the message was indeed from Alice. |
| 825 | ASSERT_EQ(1, |
| 826 | matches_inbound_session_from(inbound_olm_session.get(), |
| 827 | alice->identity_keys().curve25519, |
| 828 | encrypted_session_data_str)); |
| 829 | |
| 830 | // Bob decrypts the encrypted olm message. |
| 831 | auto plaintext_session_data = |
| 832 | bob->decrypt_message(inbound_olm_session.get(), msg_type, encrypted_session_data_str); |
| 833 | auto session_str_data = json::parse( |
| 834 | std::string((char *)plaintext_session_data.data(), plaintext_session_data.size())); |
| 835 | |
| 836 | // Validate that the output matches the input. |
| 837 | ASSERT_EQ(session_id, session_str_data.at("session_id").get<std::string>()); |
| 838 | ASSERT_EQ(session_key, session_str_data.at("session_key").get<std::string>()); |
| 839 | |
| 840 | // Bob will use the session_key to create an inbound megolm session. |
| 841 | // The session_id will be used to map future messages to this session. |
| 842 | auto inbound_megolm_session = bob->init_inbound_group_session(session_key); |
| 843 | |
| 844 | // Bob can finally decrypt Alice's original message. |
| 845 | auto ciphertext = |
| 846 | std::string((char *)encrypted_secret_message.data(), encrypted_secret_message.size()); |
| 847 | auto bob_plaintext = bob->decrypt_group_message(inbound_megolm_session.get(), ciphertext); |
| 848 | |
| 849 | auto output_str = std::string((char *)bob_plaintext.data.data(), bob_plaintext.data.size()); |
| 850 | ASSERT_EQ(output_str, secret_message); |
| 851 | } |
| 852 | |
| 853 | TEST(Encryption, OlmRoomKeyEncryption) |
| 854 | { |
| 855 | // Alice wants to use olm to send data to Bob. |
| 856 | auto alice_olm = std::make_shared<OlmClient>(); |
| 857 | auto alice_http = make_test_client(); |
| 858 | alice_olm->create_new_account(); |
| 859 | alice_olm->generate_one_time_keys(10); |
| 860 | |
| 861 | auto bob_olm = std::make_shared<OlmClient>(); |
| 862 | auto bob_http = make_test_client(); |
| 863 | bob_olm->create_new_account(); |
| 864 | bob_olm->generate_one_time_keys(10); |
| 865 | |
| 866 | alice_http->login("alice", "secret", &check_login); |
| 867 | bob_http->login("bob", "secret", &check_login); |
| 868 | |
| 869 | WAIT_UNTIL(!bob_http->access_token().empty() && !alice_http->access_token().empty()) |
| 870 | |
| 871 | bob_olm->set_user_id(bob_http->user_id().to_string()); |
| 872 | bob_olm->set_device_id(bob_http->device_id()); |
| 873 | alice_olm->set_user_id(alice_http->user_id().to_string()); |
| 874 | alice_olm->set_device_id(alice_http->device_id()); |
| 875 | |
| 876 | // Both users upload their identity & one time keys |
| 877 | atomic_int uploads(0); |
| 878 | auto upload_cb = [&uploads](const mtx::responses::UploadKeys &res, RequestErr err) { |
| 879 | check_error(err); |
| 880 | EXPECT_EQ(res.one_time_key_counts.size(), 1); |
| 881 | EXPECT_EQ(res.one_time_key_counts.at("signed_curve25519"), 10); |
| 882 | uploads += 1; |
| 883 | }; |
| 884 | |
| 885 | alice_http->upload_keys(alice_olm->create_upload_keys_request(), upload_cb); |
| 886 | bob_http->upload_keys(bob_olm->create_upload_keys_request(), upload_cb); |
| 887 | |
| 888 | WAIT_UNTIL(uploads == 2) |
| 889 | |
| 890 | atomic_bool request_finished(false); |
| 891 | std::string bob_ed25519, bob_curve25519, bob_otk; |
| 892 | |
| 893 | // Alice needs Bob's ed25519 device key. |
| 894 | mtx::requests::QueryKeys query; |
| 895 | query.device_keys[bob_http->user_id().to_string()] = {}; |
| 896 | alice_http->query_keys(query, |
| 897 | [&request_finished, &bob_ed25519, &bob_curve25519, bob = bob_http]( |
| 898 | const mtx::responses::QueryKeys &res, RequestErr err) { |
| 899 | check_error(err); |
| 900 | |
| 901 | const auto device_id = bob->device_id(); |
| 902 | const auto user_id = bob->user_id().to_string(); |
| 903 | const auto devices = res.device_keys.at(user_id); |
| 904 | |
| 905 | assert(devices.find(device_id) != devices.end()); |
| 906 | |
| 907 | bob_ed25519 = devices.at(device_id).keys.at("ed25519:" + device_id); |
| 908 | bob_curve25519 = |
| 909 | devices.at(device_id).keys.at("curve25519:" + device_id); |
| 910 | |
| 911 | request_finished = true; |
| 912 | }); |
| 913 | |
| 914 | WAIT_UNTIL(request_finished); |
| 915 | |
| 916 | // Alice needs one of Bob's one time keys. |
| 917 | request_finished = false; |
| 918 | |
| 919 | mtx::requests::ClaimKeys claim_keys; |
| 920 | claim_keys.one_time_keys[bob_http->user_id().to_string()][bob_http->device_id()] = |
| 921 | SIGNED_CURVE25519; |
| 922 | |
| 923 | alice_http->claim_keys(claim_keys, |
| 924 | [&bob_otk, bob = bob_http, &request_finished]( |
| 925 | const mtx::responses::ClaimKeys &res, RequestErr err) { |
| 926 | check_error(err); |
| 927 | |
| 928 | const auto user_id = bob->user_id().to_string(); |
| 929 | const auto device_id = bob->device_id(); |
| 930 | |
| 931 | auto retrieved_devices = res.one_time_keys.at(user_id); |
| 932 | for (const auto &device : retrieved_devices) { |
| 933 | if (device.first == device_id) { |
| 934 | bob_otk = |
| 935 | device.second.begin()->at("key").get<std::string>(); |
| 936 | break; |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | request_finished = true; |
| 941 | }); |
| 942 | |
| 943 | WAIT_UNTIL(request_finished); |
| 944 | |
| 945 | EXPECT_EQ(bob_ed25519, bob_olm->identity_keys().ed25519); |
| 946 | EXPECT_EQ(bob_curve25519, bob_olm->identity_keys().curve25519); |
| 947 | EXPECT_EQ(bob_otk, bob_olm->one_time_keys().curve25519.begin()->second); |
| 948 | |
| 949 | constexpr auto SECRET_TEXT = "Hello Bob!"; |
| 950 | |
| 951 | // Alice create m.room.key request |
| 952 | json payload = |
| 953 | json{{"content", {{"secret", SECRET_TEXT}}}, {"type", "im.nheko.custom_test_event"}}; |
| 954 | |
| 955 | // Alice creates an outbound session. |
| 956 | auto out_session = alice_olm->create_outbound_session(bob_curve25519, bob_otk); |
| 957 | auto device_msg = alice_olm->create_olm_encrypted_content(out_session.get(), |
| 958 | payload, |
| 959 | UserId("@bob:" + server_name()), |
| 960 | bob_olm->identity_keys().ed25519, |
| 961 | bob_curve25519); |
| 962 | |
| 963 | // Finally sends the olm encrypted message to Bob's device. |
| 964 | atomic_bool is_sent(false); |
| 965 | json body{ |
| 966 | {"messages", {{bob_http->user_id().to_string(), {{bob_http->device_id(), device_msg}}}}}}; |
| 967 | alice_http->send_to_device("m.room.encrypted", body, [&is_sent](RequestErr err) { |
| 968 | check_error(err); |
| 969 | is_sent = true; |
| 970 | }); |
| 971 | |
| 972 | WAIT_UNTIL(is_sent) |
| 973 | |
| 974 | SyncOpts opts; |
| 975 | opts.timeout = 0; |
| 976 | bob_http->sync( |
| 977 | opts, [bob = bob_olm, SECRET_TEXT](const mtx::responses::Sync &res, RequestErr err) { |
| 978 | check_error(err); |
| 979 | |
| 980 | EXPECT_EQ(res.to_device.events.size(), 1); |
| 981 | |
| 982 | auto olm_msg = |
| 983 | std::get<DeviceEvent<mtx::events::msg::OlmEncrypted>>(res.to_device.events[0]).content; |
| 984 | auto cipher = olm_msg.ciphertext.begin(); |
| 985 | |
| 986 | EXPECT_EQ(cipher->first, bob->identity_keys().curve25519); |
| 987 | |
| 988 | const auto msg_body = cipher->second.body; |
| 989 | const auto msg_type = cipher->second.type; |
| 990 | |
| 991 | assert(msg_type == 0); |
| 992 | |
| 993 | auto inbound_session = bob->create_inbound_session(msg_body); |
| 994 | ASSERT_TRUE( |
| 995 | matches_inbound_session_from(inbound_session.get(), olm_msg.sender_key, msg_body)); |
| 996 | |
| 997 | auto output = bob->decrypt_message(inbound_session.get(), msg_type, msg_body); |
| 998 | |
| 999 | // Parsing the original plaintext json object. |
| 1000 | auto plaintext = json::parse(std::string((char *)output.data(), output.size())); |
| 1001 | std::string secret = plaintext.at("content").at("secret").get<std::string>(); |
| 1002 | |
| 1003 | ASSERT_EQ(secret, SECRET_TEXT); |
| 1004 | }); |
| 1005 | |
| 1006 | alice_http->close(); |
| 1007 | bob_http->close(); |
| 1008 | } |
| 1009 | |
| 1010 | TEST(Encryption, ShareSecret) |
| 1011 | { |
| 1012 | // Alice wants to use olm to send data to Bob. |
| 1013 | auto alice_olm = std::make_shared<OlmClient>(); |
| 1014 | auto alice_http = make_test_client(); |
| 1015 | alice_olm->create_new_account(); |
| 1016 | alice_olm->generate_one_time_keys(10); |
| 1017 | |
| 1018 | auto bob_olm = std::make_shared<OlmClient>(); |
| 1019 | auto bob_http = make_test_client(); |
| 1020 | bob_olm->create_new_account(); |
| 1021 | bob_olm->generate_one_time_keys(10); |
| 1022 | |
| 1023 | alice_http->login("alice", "secret", &check_login); |
| 1024 | bob_http->login("bob", "secret", &check_login); |
| 1025 | |
| 1026 | WAIT_UNTIL(!bob_http->access_token().empty() && !alice_http->access_token().empty()) |
| 1027 | |
| 1028 | bob_olm->set_user_id(bob_http->user_id().to_string()); |
| 1029 | bob_olm->set_device_id(bob_http->device_id()); |
| 1030 | alice_olm->set_user_id(alice_http->user_id().to_string()); |
| 1031 | alice_olm->set_device_id(alice_http->device_id()); |
| 1032 | |
| 1033 | // Both users upload their identity & one time keys |
| 1034 | atomic_int uploads(0); |
| 1035 | auto upload_cb = [&uploads](const mtx::responses::UploadKeys &res, RequestErr err) { |
| 1036 | check_error(err); |
| 1037 | EXPECT_EQ(res.one_time_key_counts.size(), 1); |
| 1038 | EXPECT_EQ(res.one_time_key_counts.at("signed_curve25519"), 10); |
| 1039 | uploads += 1; |
| 1040 | }; |
| 1041 | |
| 1042 | alice_http->upload_keys(alice_olm->create_upload_keys_request(), upload_cb); |
| 1043 | bob_http->upload_keys(bob_olm->create_upload_keys_request(), upload_cb); |
| 1044 | |
| 1045 | WAIT_UNTIL(uploads == 2) |
| 1046 | |
| 1047 | atomic_bool request_finished(false); |
| 1048 | std::string bob_ed25519, bob_curve25519, bob_otk; |
| 1049 | |
| 1050 | // Alice needs Bob's ed25519 device key. |
| 1051 | mtx::requests::QueryKeys query; |
| 1052 | query.device_keys[bob_http->user_id().to_string()] = {}; |
| 1053 | alice_http->query_keys(query, |
| 1054 | [&request_finished, &bob_ed25519, &bob_curve25519, bob = bob_http]( |
| 1055 | const mtx::responses::QueryKeys &res, RequestErr err) { |
| 1056 | check_error(err); |
| 1057 | |
| 1058 | const auto device_id = bob->device_id(); |
| 1059 | const auto user_id = bob->user_id().to_string(); |
| 1060 | const auto devices = res.device_keys.at(user_id); |
| 1061 | |
| 1062 | assert(devices.find(device_id) != devices.end()); |
| 1063 | |
| 1064 | bob_ed25519 = devices.at(device_id).keys.at("ed25519:" + device_id); |
| 1065 | bob_curve25519 = |
| 1066 | devices.at(device_id).keys.at("curve25519:" + device_id); |
| 1067 | |
| 1068 | request_finished = true; |
| 1069 | }); |
| 1070 | |
| 1071 | WAIT_UNTIL(request_finished); |
| 1072 | |
| 1073 | // Alice needs one of Bob's one time keys. |
| 1074 | request_finished = false; |
| 1075 | |
| 1076 | mtx::requests::ClaimKeys claim_keys; |
| 1077 | claim_keys.one_time_keys[bob_http->user_id().to_string()][bob_http->device_id()] = |
| 1078 | SIGNED_CURVE25519; |
| 1079 | |
| 1080 | alice_http->claim_keys(claim_keys, |
| 1081 | [&bob_otk, bob = bob_http, &request_finished]( |
| 1082 | const mtx::responses::ClaimKeys &res, RequestErr err) { |
| 1083 | check_error(err); |
| 1084 | |
| 1085 | const auto user_id = bob->user_id().to_string(); |
| 1086 | const auto device_id = bob->device_id(); |
| 1087 | |
| 1088 | auto retrieved_devices = res.one_time_keys.at(user_id); |
| 1089 | for (const auto &device : retrieved_devices) { |
| 1090 | if (device.first == device_id) { |
| 1091 | bob_otk = |
| 1092 | device.second.begin()->at("key").get<std::string>(); |
| 1093 | break; |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | request_finished = true; |
| 1098 | }); |
| 1099 | |
| 1100 | WAIT_UNTIL(request_finished); |
| 1101 | |
| 1102 | EXPECT_EQ(bob_ed25519, bob_olm->identity_keys().ed25519); |
| 1103 | EXPECT_EQ(bob_curve25519, bob_olm->identity_keys().curve25519); |
| 1104 | EXPECT_EQ(bob_otk, bob_olm->one_time_keys().curve25519.begin()->second); |
| 1105 | |
| 1106 | constexpr auto SECRET_TEXT = "Hello Bob!"; |
| 1107 | constexpr auto REQUEST_ID = "askjfdgsdfgfdg"; |
| 1108 | |
| 1109 | // Alice create m.room.key request |
| 1110 | mtx::events::msg::SecretRequest secretRequest{}; |
| 1111 | secretRequest.request_id = REQUEST_ID; |
| 1112 | secretRequest.name = "abcdefg"; |
| 1113 | secretRequest.requesting_device_id = bob_http->device_id(); |
| 1114 | secretRequest.action = RequestAction::Request; |
| 1115 | |
| 1116 | SyncOpts opts; |
| 1117 | opts.timeout = 0; |
| 1118 | |
| 1119 | // Finally sends the olm encrypted message to Bob's device. |
| 1120 | atomic_bool is_sent(false); |
| 1121 | bob_http->send_to_device<SecretRequest>( |
| 1122 | bob_http->generate_txn_id(), |
| 1123 | {{alice_http->user_id(), {{alice_http->device_id(), secretRequest}}}}, |
| 1124 | [&](RequestErr err) { |
| 1125 | check_error(err); |
| 1126 | |
| 1127 | alice_http->sync(opts, [&](const mtx::responses::Sync &res, RequestErr err) { |
| 1128 | check_error(err); |
| 1129 | |
| 1130 | mtx::events::DeviceEvent<mtx::events::msg::SecretSend> secretSend{}; |
| 1131 | secretSend.content.secret = SECRET_TEXT; |
| 1132 | secretSend.content.request_id = |
| 1133 | std::get<mtx::events::DeviceEvent<mtx::events::msg::SecretRequest>>( |
| 1134 | res.to_device.events.at(0)) |
| 1135 | .content.request_id; |
| 1136 | secretSend.type = mtx::events::EventType::SecretSend; |
| 1137 | |
| 1138 | json payload = secretSend; |
| 1139 | |
| 1140 | // Alice creates an outbound session. |
| 1141 | auto out_session = alice_olm->create_outbound_session(bob_curve25519, bob_otk); |
| 1142 | auto device_msg = |
| 1143 | alice_olm->create_olm_encrypted_content(out_session.get(), |
| 1144 | payload, |
| 1145 | UserId("@bob:" + server_name()), |
| 1146 | bob_olm->identity_keys().ed25519, |
| 1147 | bob_curve25519); |
| 1148 | |
| 1149 | std::map<mtx::identifiers::User, |
| 1150 | std::map<std::string, mtx::events::msg::OlmEncrypted>> |
| 1151 | body{{bob_http->user_id(), |
| 1152 | {{bob_http->device_id(), device_msg.get<mtx::events::msg::OlmEncrypted>()}}}}; |
| 1153 | alice_http->send_to_device<OlmEncrypted>( |
| 1154 | alice_http->generate_txn_id(), body, [&is_sent](RequestErr err) { |
| 1155 | check_error(err); |
| 1156 | is_sent = true; |
| 1157 | }); |
| 1158 | }); |
| 1159 | }); |
| 1160 | |
| 1161 | WAIT_UNTIL(is_sent) |
| 1162 | |
| 1163 | bob_http->sync( |
| 1164 | opts, |
| 1165 | [bob = bob_olm, SECRET_TEXT, REQUEST_ID](const mtx::responses::Sync &res, RequestErr err) { |
| 1166 | check_error(err); |
| 1167 | |
| 1168 | EXPECT_EQ(res.to_device.events.size(), 1); |
| 1169 | |
| 1170 | auto olm_msg = |
| 1171 | std::get<DeviceEvent<mtx::events::msg::OlmEncrypted>>(res.to_device.events[0]).content; |
| 1172 | auto cipher = olm_msg.ciphertext.begin(); |
| 1173 | |
| 1174 | EXPECT_EQ(cipher->first, bob->identity_keys().curve25519); |
| 1175 | |
| 1176 | const auto msg_body = cipher->second.body; |
| 1177 | const auto msg_type = cipher->second.type; |
| 1178 | |
| 1179 | assert(msg_type == 0); |
| 1180 | |
| 1181 | auto inbound_session = bob->create_inbound_session(msg_body); |
| 1182 | ASSERT_TRUE( |
| 1183 | matches_inbound_session_from(inbound_session.get(), olm_msg.sender_key, msg_body)); |
| 1184 | |
| 1185 | auto output = bob->decrypt_message(inbound_session.get(), msg_type, msg_body); |
| 1186 | |
| 1187 | // Parsing the original plaintext json object. |
| 1188 | auto ev = json::parse(std::string_view((char *)output.data(), output.size())) |
| 1189 | .get<mtx::events::DeviceEvent<mtx::events::msg::SecretSend>>(); |
| 1190 | |
| 1191 | ASSERT_EQ(ev.content.secret, SECRET_TEXT); |
| 1192 | ASSERT_EQ(ev.content.request_id, REQUEST_ID); |
| 1193 | }); |
| 1194 | |
| 1195 | alice_http->close(); |
| 1196 | bob_http->close(); |
| 1197 | } |
| 1198 | |
| 1199 | TEST(Encryption, PickleAccount) |
| 1200 | { |
| 1201 | auto alice = std::make_shared<OlmClient>(); |
| 1202 | alice->create_new_account(); |
| 1203 | alice->generate_one_time_keys(10); |
| 1204 | |
| 1205 | auto alice_pickled = pickle<AccountObject>(alice->account(), "secret"); |
| 1206 | |
| 1207 | auto bob = std::make_shared<OlmClient>(); |
| 1208 | bob->restore_account(alice_pickled, "secret"); |
| 1209 | |
| 1210 | EXPECT_EQ(json(bob->identity_keys()).dump(), json(alice->identity_keys()).dump()); |
| 1211 | EXPECT_EQ(json(bob->one_time_keys()).dump(), json(alice->one_time_keys()).dump()); |
| 1212 | |
| 1213 | auto carl = std::make_shared<OlmClient>(); |
| 1214 | |
| 1215 | // BAD_ACCOUNT_KEY |
| 1216 | EXPECT_THROW(carl->restore_account(alice_pickled, "another_secret"), olm_exception); |
| 1217 | } |
| 1218 | |
| 1219 | TEST(Encryption, PickleOlmSessions) |
| 1220 | { |
| 1221 | auto alice = std::make_shared<OlmClient>(); |
| 1222 | alice->create_new_account(); |
| 1223 | |
| 1224 | auto bob = std::make_shared<OlmClient>(); |
| 1225 | bob->create_new_account(); |
| 1226 | bob->generate_one_time_keys(1); |
| 1227 | |
| 1228 | std::string bob_key = bob->identity_keys().curve25519; |
| 1229 | std::string bob_one_time_key = bob->one_time_keys().curve25519.begin()->second; |
| 1230 | |
| 1231 | auto outbound_session = alice->create_outbound_session(bob_key, bob_one_time_key); |
| 1232 | auto initial_session_id = session_id(outbound_session.get()); |
| 1233 | |
| 1234 | auto plaintext = "Hello, Bob!"; |
| 1235 | size_t msgtype = olm_encrypt_message_type(outbound_session.get()); |
| 1236 | auto ciphertext = alice->encrypt_message(outbound_session.get(), plaintext); |
| 1237 | auto ciphertext_str = std::string((char *)ciphertext.data(), ciphertext.size()); |
| 1238 | |
| 1239 | EXPECT_EQ(msgtype, 0); |
| 1240 | |
| 1241 | auto saved_outbound_session = pickle<SessionObject>(outbound_session.get(), "wat"); |
| 1242 | auto restored_outbound_session = unpickle<SessionObject>(saved_outbound_session, "wat"); |
| 1243 | |
| 1244 | EXPECT_EQ(session_id(restored_outbound_session.get()), initial_session_id); |
| 1245 | |
| 1246 | EXPECT_THROW(unpickle<SessionObject>(saved_outbound_session, "another_secret"), olm_exception); |
| 1247 | |
| 1248 | msgtype = olm_encrypt_message_type(restored_outbound_session.get()); |
| 1249 | EXPECT_EQ(msgtype, 0); |
| 1250 | |
| 1251 | auto restored_ciphertext = alice->encrypt_message(restored_outbound_session.get(), plaintext); |
| 1252 | auto restored_ciphertext_str = |
| 1253 | std::string((char *)restored_ciphertext.data(), restored_ciphertext.size()); |
| 1254 | |
| 1255 | auto inbound_session = bob->create_inbound_session(ciphertext_str); |
| 1256 | auto saved_inbound_session = pickle<SessionObject>(inbound_session.get(), "woot"); |
| 1257 | auto restored_inbound_session = unpickle<SessionObject>(saved_inbound_session, "woot"); |
| 1258 | |
| 1259 | EXPECT_THROW(unpickle<SessionObject>(saved_inbound_session, "another_secret"), olm_exception); |
| 1260 | |
| 1261 | ASSERT_EQ(1, matches_inbound_session(inbound_session.get(), ciphertext_str)); |
| 1262 | ASSERT_EQ(1, matches_inbound_session(inbound_session.get(), restored_ciphertext_str)); |
| 1263 | ASSERT_EQ(1, matches_inbound_session(restored_inbound_session.get(), restored_ciphertext_str)); |
| 1264 | ASSERT_EQ(1, matches_inbound_session(restored_inbound_session.get(), ciphertext_str)); |
| 1265 | |
| 1266 | auto d1 = bob->decrypt_message(inbound_session.get(), msgtype, ciphertext_str); |
| 1267 | auto d2 = bob->decrypt_message(restored_inbound_session.get(), msgtype, ciphertext_str); |
| 1268 | auto d3 = bob->decrypt_message(inbound_session.get(), msgtype, restored_ciphertext_str); |
| 1269 | auto d4 = |
| 1270 | bob->decrypt_message(restored_inbound_session.get(), msgtype, restored_ciphertext_str); |
| 1271 | |
| 1272 | EXPECT_EQ(d1, d2); |
| 1273 | EXPECT_EQ(d2, d3); |
| 1274 | EXPECT_EQ(d3, d4); |
| 1275 | EXPECT_EQ(d1, d4); |
| 1276 | EXPECT_EQ(d2, d4); |
| 1277 | |
| 1278 | EXPECT_EQ(std::string((char *)d1.data(), d1.size()), "Hello, Bob!"); |
| 1279 | } |
| 1280 | |
| 1281 | TEST(Encryption, PickleMegolmSessions) |
| 1282 | { |
| 1283 | // Outbound Session |
| 1284 | auto alice = make_shared<mtx::crypto::OlmClient>(); |
| 1285 | alice->create_new_account(); |
| 1286 | |
| 1287 | auto outbound_session = alice->init_outbound_group_session(); |
| 1288 | |
| 1289 | const auto original_session_id = mtx::crypto::session_id(outbound_session.get()); |
| 1290 | const auto original_session_key = mtx::crypto::session_key(outbound_session.get()); |
| 1291 | |
| 1292 | auto saved_session = pickle<OutboundSessionObject>(outbound_session.get(), "secret"); |
| 1293 | auto restored_outbound_session = unpickle<OutboundSessionObject>(saved_session, "secret"); |
| 1294 | |
| 1295 | const auto restored_session_id = mtx::crypto::session_id(restored_outbound_session.get()); |
| 1296 | const auto restored_session_key = mtx::crypto::session_key(restored_outbound_session.get()); |
| 1297 | |
| 1298 | EXPECT_EQ(original_session_id, restored_session_id); |
| 1299 | EXPECT_EQ(original_session_key, restored_session_key); |
| 1300 | |
| 1301 | // BAD_ACCOUNT_KEY |
| 1302 | EXPECT_THROW(unpickle<OutboundSessionObject>(saved_session, "another_secret"), olm_exception); |
| 1303 | |
| 1304 | const auto SECRET = "Hello World!"; |
| 1305 | |
| 1306 | auto encrypted = alice->encrypt_group_message(outbound_session.get(), SECRET); |
| 1307 | auto ciphertext = std::string((char *)encrypted.data(), encrypted.size()); |
| 1308 | |
| 1309 | // Inbound Session |
| 1310 | auto inbound_session = alice->init_inbound_group_session(original_session_key); |
| 1311 | auto plaintext = alice->decrypt_group_message(inbound_session.get(), ciphertext); |
| 1312 | |
| 1313 | saved_session = pickle<InboundSessionObject>(inbound_session.get(), "secret"); |
| 1314 | |
| 1315 | auto restored_inbound_session = unpickle<InboundSessionObject>(saved_session, "secret"); |
| 1316 | auto restored_plaintext = |
| 1317 | alice->decrypt_group_message(restored_inbound_session.get(), ciphertext); |
| 1318 | |
| 1319 | EXPECT_EQ(std::string((char *)plaintext.data.data(), plaintext.data.size()), |
| 1320 | std::string((char *)restored_plaintext.data.data(), restored_plaintext.data.size())); |
| 1321 | |
| 1322 | EXPECT_EQ(std::string((char *)plaintext.data.data(), plaintext.data.size()), SECRET); |
| 1323 | |
| 1324 | EXPECT_THROW(alice->decrypt_group_message(inbound_session.get(), ""), olm_exception); |
| 1325 | EXPECT_THROW(alice->decrypt_group_message(nullptr, ""), olm_exception); |
| 1326 | EXPECT_THROW(alice->decrypt_group_message(nullptr, ciphertext), olm_exception); |
| 1327 | } |
| 1328 | |
| 1329 | TEST(ExportSessions, InboundMegolmSessions) |
| 1330 | { |
| 1331 | auto alice = std::make_shared<OlmClient>(); |
| 1332 | alice->create_new_account(); |
| 1333 | alice->generate_one_time_keys(1); |
| 1334 | |
| 1335 | auto bob = std::make_shared<OlmClient>(); |
| 1336 | bob->create_new_account(); |
| 1337 | bob->generate_one_time_keys(1); |
| 1338 | |
| 1339 | // ==================== SESSION SETUP =================== // |
| 1340 | |
| 1341 | // Alice wants to send an encrypted megolm message to Bob. |
| 1342 | const std::string secret_message = "Hey, Bob!"; |
| 1343 | |
| 1344 | // Alice creates an outbound megolm session that will be used by both parties. |
| 1345 | auto outbound_megolm_session = alice->init_outbound_group_session(); |
| 1346 | auto msg_index = olm_outbound_group_session_message_index(outbound_megolm_session.get()); |
| 1347 | ASSERT_EQ(msg_index, 0); |
| 1348 | |
| 1349 | // Alice extracts the session id & session key so she can share them with Bob. |
| 1350 | const auto session_id = mtx::crypto::session_id(outbound_megolm_session.get()); |
| 1351 | const auto session_key = mtx::crypto::session_key(outbound_megolm_session.get()); |
| 1352 | |
| 1353 | // Encrypt the message using megolm. |
| 1354 | auto encrypted_secret_message = |
| 1355 | alice->encrypt_group_message(outbound_megolm_session.get(), secret_message); |
| 1356 | |
| 1357 | msg_index = olm_outbound_group_session_message_index(outbound_megolm_session.get()); |
| 1358 | ASSERT_EQ(msg_index, 1); |
| 1359 | |
| 1360 | // Bob will use the session_key to create an inbound megolm session. |
| 1361 | // The session_id will be used to map future messages to this session. |
| 1362 | auto inbound_megolm_session = bob->init_inbound_group_session(session_key); |
| 1363 | |
| 1364 | // Bob can finally decrypt Alice's original message. |
| 1365 | auto ciphertext = |
| 1366 | std::string((char *)encrypted_secret_message.data(), encrypted_secret_message.size()); |
| 1367 | auto bob_plaintext = bob->decrypt_group_message(inbound_megolm_session.get(), ciphertext); |
| 1368 | |
| 1369 | auto output_str = std::string((char *)bob_plaintext.data.data(), bob_plaintext.data.size()); |
| 1370 | ASSERT_EQ(output_str, secret_message); |
| 1371 | |
| 1372 | // ==================== SESSION IMPORT/EXPORT =================== // |
| 1373 | |
| 1374 | auto exported_session_key = export_session(inbound_megolm_session.get(), -1); |
| 1375 | auto restored_inbound_session = import_session(exported_session_key); |
| 1376 | |
| 1377 | // Decrypt message again. |
| 1378 | auto restored_ciphertext = |
| 1379 | std::string((char *)encrypted_secret_message.data(), encrypted_secret_message.size()); |
| 1380 | auto restored_plaintext = |
| 1381 | bob->decrypt_group_message(restored_inbound_session.get(), restored_ciphertext); |
| 1382 | |
| 1383 | auto restored_output_str = |
| 1384 | std::string((char *)restored_plaintext.data.data(), restored_plaintext.data.size()); |
| 1385 | ASSERT_EQ(restored_output_str, secret_message); |
| 1386 | } |
| 1387 | |
| 1388 | TEST(Encryption, EncryptedFile) |
| 1389 | { |
| 1390 | { |
| 1391 | auto buffer = mtx::crypto::create_buffer(16); |
| 1392 | ASSERT_EQ(buffer.size(), 16); |
| 1393 | auto buf_str = mtx::crypto::to_string(buffer); |
| 1394 | ASSERT_EQ(buf_str.size(), 16); |
| 1395 | } |
| 1396 | |
| 1397 | std::string plaintext = "This is some plain text payload"; |
| 1398 | auto encryption_data = mtx::crypto::encrypt_file(plaintext); |
| 1399 | ASSERT_NE(plaintext, mtx::crypto::to_string(encryption_data.first)); |
| 1400 | ASSERT_EQ(plaintext, |
| 1401 | mtx::crypto::to_string(mtx::crypto::decrypt_file( |
| 1402 | mtx::crypto::to_string(encryption_data.first), encryption_data.second))); |
| 1403 | // key needs to be 32 bytes/256 bits |
| 1404 | ASSERT_EQ(32, mtx::crypto::base642bin_urlsafe_unpadded(encryption_data.second.key.k).size()); |
| 1405 | // IV needs to be 16 bytes/128 bits |
| 1406 | ASSERT_EQ(16, mtx::crypto::base642bin_unpadded(encryption_data.second.iv).size()); |
| 1407 | |
| 1408 | json j = R"({ |
| 1409 | "type": "m.room.message", |
| 1410 | "content": { |
| 1411 | "body": "test.txt", |
| 1412 | "info": { |
| 1413 | "size": 8, |
| 1414 | "mimetype": "text/plain" |
| 1415 | }, |
| 1416 | "msgtype": "m.file", |
| 1417 | "file": { |
| 1418 | "v": "v2", |
| 1419 | "key": { |
| 1420 | "alg": "A256CTR", |
| 1421 | "ext": true, |
| 1422 | "k": "6osKLzUKV1YZ06WEX0b77D784Te8oAj5eNU-gAgkjs4", |
| 1423 | "key_ops": [ |
| 1424 | "encrypt", |
| 1425 | "decrypt" |
| 1426 | ], |
| 1427 | "kty": "oct" |
| 1428 | }, |
| 1429 | "iv": "7zRP/t89YWcAAAAAAAAAAA", |
| 1430 | "hashes": { |
| 1431 | "sha256": "5g41hn7n10sCw3+2j7CQ9SJl6R/v5EBT4MshdFgHhzo" |
| 1432 | }, |
| 1433 | "url": "mxc://neko.dev/WPKoOAPfPlcHiZZTEoaIoZhN", |
| 1434 | "mimetype": "text/plain" |
| 1435 | } |
| 1436 | }, |
| 1437 | "event_id": "$1575320135447DEPky:neko.dev", |
| 1438 | "origin_server_ts": 1575320135324, |
| 1439 | "sender": "@test:neko.dev", |
| 1440 | "unsigned": { |
| 1441 | "age": 1081, |
| 1442 | "transaction_id": "m1575320142400.8" |
| 1443 | }, |
| 1444 | "room_id": "!YnUlhwgbBaGcAFsJOJ:neko.dev" |
| 1445 | })"_json; |
| 1446 | mtx::events::RoomEvent<mtx::events::msg::File> ev = |
| 1447 | j.get<mtx::events::RoomEvent<mtx::events::msg::File>>(); |
| 1448 | |
| 1449 | ASSERT_EQ("abcdefg\n", |
| 1450 | mtx::crypto::to_string( |
| 1451 | mtx::crypto::decrypt_file("=\xFDX\xAB\xCA\xEB\x8F\xFF", ev.content.file.value()))); |
| 1452 | } |
| 1453 | |
| 1454 | TEST(Encryption, SAS) |
| 1455 | { |
| 1456 | auto alice = std::make_shared<OlmClient>(); |
| 1457 | alice->create_new_account(); |
| 1458 | auto bob = std::make_shared<OlmClient>(); |
| 1459 | bob->create_new_account(); |
| 1460 | |
| 1461 | auto alice_sas = alice->sas_init(); |
| 1462 | auto bob_sas = bob->sas_init(); |
| 1463 | |
| 1464 | ASSERT_EQ(alice_sas->public_key().length(), 43); |
| 1465 | ASSERT_EQ(bob_sas->public_key().length(), 43); |
| 1466 | |
| 1467 | alice_sas->set_their_key(bob_sas->public_key()); |
| 1468 | bob_sas->set_their_key(alice_sas->public_key()); |
| 1469 | |
| 1470 | std::string info = "test_info"; |
| 1471 | |
| 1472 | std::vector<int> alice_decimal = alice_sas->generate_bytes_decimal(info); |
| 1473 | std::vector<int> bob_decimal = bob_sas->generate_bytes_decimal(info); |
| 1474 | |
| 1475 | ASSERT_EQ(alice_decimal.size(), 3); |
| 1476 | ASSERT_EQ(bob_decimal.size(), 3); |
| 1477 | |
| 1478 | for (int i = 0; i < 3; ++i) { |
| 1479 | ASSERT_TRUE((alice_decimal[i] >= 0) && (alice_decimal[i] <= 8191)); |
| 1480 | ASSERT_TRUE((bob_decimal[i] >= 0) && (bob_decimal[i] <= 8191)); |
| 1481 | ASSERT_EQ(alice_decimal[i], bob_decimal[i]); |
| 1482 | } |
| 1483 | |
| 1484 | std::vector<int> alice_emoji = alice_sas->generate_bytes_emoji(info); |
| 1485 | std::vector<int> bob_emoji = bob_sas->generate_bytes_emoji(info); |
| 1486 | |
| 1487 | ASSERT_EQ(alice_emoji.size(), 7); |
| 1488 | ASSERT_EQ(bob_emoji.size(), 7); |
| 1489 | |
| 1490 | for (int i = 0; i < 7; ++i) { |
| 1491 | ASSERT_TRUE((alice_emoji[i] >= 0) && (alice_emoji[i] <= 8191)); |
| 1492 | ASSERT_TRUE((bob_emoji[i] >= 0) && (bob_emoji[i] <= 8191)); |
| 1493 | ASSERT_EQ(alice_emoji[i], bob_emoji[i]); |
| 1494 | } |
| 1495 | } |
| 1496 | |
| 1497 | TEST(Encryption, DISABLED_HandleRoomKeyEvent) {} |
| 1498 | TEST(Encryption, DISABLED_HandleRoomKeyRequestEvent) {} |
| 1499 | TEST(Encryption, DISABLED_HandleNewDevices) {} |
| 1500 | TEST(Encryption, DISABLED_HandleLeftDevices) {} |
| 1501 | |
| 1502 | TEST(Encryption, DISABLED_SendEncryptedMessageWithMegolm) {} |
| 1503 | TEST(Encryption, DISABLED_RotateMegolmSession) {} |