bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2015-2018 Dovecot authors, see the included COPYING file */
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomistatic const char *mcp_old_user_key = "1\t716\t0\t048FD04FD3612B22D32790C592CF21CEF417EFD2EA34AE5F688FA5B51BED29E05A308B68DA78E16E90B47A11E133BD9A208A2894FD01B0BEE865CE339EA3FB17AC\td0cfaca5d335f9edc41c84bb47465184cb0e2ec3931bebfcea4dd433615e77a0";
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomistatic const char *mcp_old_user_key_id = "d0cfaca5d335f9edc41c84bb47465184cb0e2ec3931bebfcea4dd433615e77a0";
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomistatic const char *mcp_old_box_key = "1\t716\t1\t0567e6bf9579813ae967314423b0fceb14bda24749303923de9a9bb9370e0026f995901a57e63113eeb2baf0c940e978d00686cbb52bd5014bc318563375876255\t0300E46DA2125427BE968EB3B649910CDC4C405E5FFDE18D433A97CABFEE28CEEFAE9EE356C792004FFB80981D67E741B8CC036A34235A8D2E1F98D1658CFC963D07EB\td0cfaca5d335f9edc41c84bb47465184cb0e2ec3931bebfcea4dd433615e77a0\t7c9a1039ea2e4fed73e81dd3ffc3fa22ea4a28352939adde7bf8ea858b00fa4f";
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomistatic const char *mcp_old_box_key_id = "7c9a1039ea2e4fed73e81dd3ffc3fa22ea4a28352939adde7bf8ea858b00fa4f";
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomistatic struct mail_storage_service_ctx *mail_storage_service = NULL;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomistatic struct mail_storage_service_user *test_service_user = NULL;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomistruct mail_crypt_user *mail_crypt_get_mail_crypt_user(struct mail_user *user ATTR_UNUSED)
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomiint test_mail_attribute_get(struct mailbox *box, bool user_key, bool shared,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi const char *pubid, const char **value_r, const char **error_r)
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi attr_name = user_key ? USER_CRYPT_PREFIX ACTIVE_KEY_NAME :
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi attr_type = shared ? MAIL_ATTRIBUTE_TYPE_SHARED : MAIL_ATTRIBUTE_TYPE_PRIVATE;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi *error_r = t_strdup_printf("mailbox_attribute_get(%s, %s) failed: %s",
bf7dc750b95039981c0e9d728f313d50cf38a156Martti Rannanjärvi mailbox_get_last_internal_error(box, NULL));
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomitest_mail_attribute_set(struct mailbox_transaction_context *t,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi attr_name = user_key ? USER_CRYPT_PREFIX ACTIVE_KEY_NAME :
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi attr_type = shared ? MAIL_ATTRIBUTE_TYPE_SHARED : MAIL_ATTRIBUTE_TYPE_PRIVATE;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi *error_r = t_strdup_printf("mailbox_attribute_set(%s, %s) failed: %s",
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi mailbox_get_vname(mailbox_transaction_get_mailbox(t)),
bf7dc750b95039981c0e9d728f313d50cf38a156Martti Rannanjärvi mailbox_get_last_internal_error(mailbox_transaction_get_mailbox(t), NULL));
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi mail_home = p_strdup_printf(test_pool, "%s/mcp_user/", path_buf);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi .userdb_fields = (const char*const[]){
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi mail_storage_service = mail_storage_service_init(master_service, NULL,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi if (mail_storage_service_lookup(mail_storage_service, &input,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi mail_storage_service_user_get_settings_parser(test_service_user);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi t_strdup_printf("mail_attribute_dict=file:%s/dovecot-attributes",
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi if (mail_storage_service_next(mail_storage_service, test_service_user,
eb318ea05532d2e54ed3bfc89bc15dcf1adae838Timo Sirainen mail_storage_service_user_unref(&test_service_user);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi mail_storage_service_deinit(&mail_storage_service);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi if (unlink_directory(mail_home, UNLINK_DIRECTORY_FLAG_RMDIR,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi i_error("unlink_directory(%s) failed: %s", mail_home, error);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi /* try to generate a keypair for user */
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi if (mail_crypt_user_generate_keypair(test_mail_user, &pair,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi /* keys ought to be in cache or somewhere...*/
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi if (mail_crypt_user_get_private_key(test_mail_user, NULL, &pair.priv, &error) <= 0)
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi i_error("Cannot get user private key: %s", error);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi if (mail_crypt_user_get_public_key(test_mail_user, &user_key,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi i_error("Cannot get user private key: %s", error);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi mail_namespace_find_inbox(test_mail_user->namespaces);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi struct mailbox *box = mailbox_alloc(ns->list, "INBOX",
bf7dc750b95039981c0e9d728f313d50cf38a156Martti Rannanjärvi mailbox_get_last_internal_error(box, NULL));
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi if (mail_crypt_box_generate_keypair(box, &pair, user_key, &pubid,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi struct mail_crypt_user *muser = mail_crypt_get_mail_crypt_user(test_mail_user);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(mail_crypt_user_get_private_key(test_mail_user, NULL,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi i_error("mail_crypt_user_get_private_key() failed: %s", error);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(mail_crypt_user_get_public_key(test_mail_user,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi i_error("mail_crypt_user_get_public_key() failed: %s", error);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi struct dcrypt_private_key *privkey = NULL, *user_key = NULL;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi mail_namespace_find_inbox(test_mail_user->namespaces);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi struct mailbox *box = mailbox_alloc(ns->list, "INBOX",
bf7dc750b95039981c0e9d728f313d50cf38a156Martti Rannanjärvi mailbox_get_last_internal_error(box, NULL));
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi /* verify links */
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi /* user's public key */
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(test_mail_attribute_get(box, TRUE, TRUE, ACTIVE_KEY_NAME,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(strcmp(value, test_user_key_id) == 0);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(test_mail_attribute_get(box, TRUE, TRUE, value, &value,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi /* load key */
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(dcrypt_key_load_public(&pubkey, value, &error) == TRUE);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi /* see if it matches */
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(mail_crypt_public_key_id_match(pubkey, test_user_key_id,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi /* user's private key */
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(test_mail_attribute_get(box, TRUE, FALSE, ACTIVE_KEY_NAME,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(strcmp(value, test_user_key_id) == 0);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(test_mail_attribute_get(box, TRUE, FALSE, value, &value,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi /* load key */
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(dcrypt_key_load_private(&user_key, value, NULL, NULL,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi /* see if it matches */
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(mail_crypt_private_key_id_match(user_key, test_user_key_id,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi /* inbox's public key */
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(test_mail_attribute_get(box, FALSE, TRUE, ACTIVE_KEY_NAME,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(test_mail_attribute_get(box, FALSE, TRUE, value, &value,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi /* load key */
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(dcrypt_key_load_public(&pubkey, value, &error) == TRUE);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi /* see if it matches */
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(mail_crypt_public_key_id_match(pubkey, test_box_key_id,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi /* user's private key */
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(test_mail_attribute_get(box, FALSE, FALSE, ACTIVE_KEY_NAME,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(test_mail_attribute_get(box, FALSE, FALSE, value, &value,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(dcrypt_key_string_get_info(value, NULL, NULL, NULL,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(enc_type == DCRYPT_KEY_ENCRYPTION_TYPE_KEY);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(strcmp(enc_id, test_user_key_id) == 0);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi /* load key */
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(dcrypt_key_load_private(&privkey, value, NULL, user_key,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi /* see if it matches */
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(mail_crypt_private_key_id_match(privkey, test_box_key_id,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi mail_namespace_find_inbox(test_mail_user->namespaces);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi struct mailbox *box = mailbox_alloc(ns->list, "INBOX",
bf7dc750b95039981c0e9d728f313d50cf38a156Martti Rannanjärvi mailbox_get_last_internal_error(box, NULL));
0dab9cb35a976c49b28a11e28d5570f5191f1a7aMartti Rannanjärvi mailbox_transaction_begin(box, 0, __func__);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_mail_attribute_set(t, TRUE, FALSE, mcp_old_user_key_id,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_mail_attribute_set(t, FALSE, FALSE, mcp_old_box_key_id,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi /* try to load old key */
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(mail_crypt_get_private_key(box, mcp_old_box_key_id, FALSE, FALSE,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi i_error("mail_crypt_get_private_key(%s) failed: %s",
b261fc7c4fa222748c8c4e401d925d32e0d86041Aki Tuomi test_assert(dcrypt_key_id_private_old(privkey, key_id, &error));
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(strcmp(binary_to_hex(key_id->data, key_id->used), mcp_old_box_key_id) == 0);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi .module_dir = top_builddir "/src/lib-dcrypt/.libs"
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_pool = pool_alloconly_create(MEMPOOL_GROWING "mcp test pool", 128);
beec8b0a6a3ece557c9acec524e82542a9641662Aki Tuomi i_info("No functional dcrypt backend found - skipping tests");
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi /* allocate a user */
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi mail_crypt_key_register_mailbox_internal_attributes();
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi struct mail_crypt_user *muser = mail_crypt_get_mail_crypt_user(test_mail_user);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi void (*tests[])(void) = {
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi master_service = master_service_init("test-mail-key",