bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2015-2018 Dovecot authors, see the included COPYING file */
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi#include "lib.h"
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi#include "test-common.h"
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi#include "ioloop.h"
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi#include "lib-signals.h"
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi#include "master-service.h"
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi#include "mail-storage.h"
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi#include "mail-storage-service.h"
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi#include "mailbox-list.h"
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi#include "settings-parser.h"
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi#include "mail-user.h"
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi#include "safe-mkstemp.h"
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi#include "safe-mkdir.h"
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi#include "str.h"
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi#include "unlink-directory.h"
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi#include "randgen.h"
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi#include "dcrypt.h"
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi#include "hex-binary.h"
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi#include "mail-crypt-common.h"
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi#include "mail-crypt-key.h"
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi#include "mail-crypt-plugin.h"
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
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 Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomistatic struct mail_storage_service_ctx *mail_storage_service = NULL;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomistatic struct mail_user *test_mail_user = NULL;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomistatic struct mail_storage_service_user *test_service_user = NULL;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomistatic struct ioloop *test_ioloop = NULL;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomistatic const char *mail_home;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomistatic const char *test_user_key_id;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomistatic const char *test_box_key_id;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomistatic pool_t test_pool;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomistatic struct mail_crypt_user mail_crypt_user;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomistruct mail_crypt_user *mail_crypt_get_mail_crypt_user(struct mail_user *user ATTR_UNUSED)
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi{
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi return &mail_crypt_user;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi}
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomistatic
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{
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi const char *attr_name;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi enum mail_attribute_type attr_type;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi if (strcmp(pubid, ACTIVE_KEY_NAME) == 0) {
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi attr_name = user_key ? USER_CRYPT_PREFIX ACTIVE_KEY_NAME :
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi BOX_CRYPT_PREFIX ACTIVE_KEY_NAME;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi attr_type = MAIL_ATTRIBUTE_TYPE_SHARED;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi } else {
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi attr_name = t_strdup_printf("%s%s%s",
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi user_key ? USER_CRYPT_PREFIX :
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi BOX_CRYPT_PREFIX,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi shared ? PUBKEYS_PREFIX :
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi PRIVKEYS_PREFIX,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi pubid);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi attr_type = shared ? MAIL_ATTRIBUTE_TYPE_SHARED : MAIL_ATTRIBUTE_TYPE_PRIVATE;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi }
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi struct mail_attribute_value value;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi int ret;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi if ((ret = mailbox_attribute_get(box, attr_type,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi attr_name, &value)) <= 0) {
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi if (ret < 0) {
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi *error_r = t_strdup_printf("mailbox_attribute_get(%s, %s) failed: %s",
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi mailbox_get_vname(box),
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi attr_name,
bf7dc750b95039981c0e9d728f313d50cf38a156Martti Rannanjärvi mailbox_get_last_internal_error(box, NULL));
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi }
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi } else {
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi *value_r = t_strdup(value.value);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi }
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi return ret;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi}
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomistatic int
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomitest_mail_attribute_set(struct mailbox_transaction_context *t,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi bool user_key, bool shared, const char *pubid,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi const char *value, const char **error_r)
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi{
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi const char *attr_name;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi enum mail_attribute_type attr_type;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi if (strcmp(pubid, ACTIVE_KEY_NAME) == 0) {
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi attr_name = user_key ? USER_CRYPT_PREFIX ACTIVE_KEY_NAME :
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi BOX_CRYPT_PREFIX ACTIVE_KEY_NAME;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi attr_type = MAIL_ATTRIBUTE_TYPE_SHARED;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi } else {
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi attr_name = t_strdup_printf("%s%s%s",
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi user_key ? USER_CRYPT_PREFIX :
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi BOX_CRYPT_PREFIX,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi shared ? PUBKEYS_PREFIX :
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi PRIVKEYS_PREFIX,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi pubid);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi attr_type = shared ? MAIL_ATTRIBUTE_TYPE_SHARED : MAIL_ATTRIBUTE_TYPE_PRIVATE;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi }
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi struct mail_attribute_value attr_value;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi int ret;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
efe78d3ba24fc866af1c79b9223dc0809ba26cadStephan Bosch i_zero(&attr_value);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi attr_value.value = value;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi if ((ret = mailbox_attribute_set(t, attr_type,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi attr_name, &attr_value)) <= 0) {
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi if (ret < 0) {
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi *error_r = t_strdup_printf("mailbox_attribute_set(%s, %s) failed: %s",
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi mailbox_get_vname(mailbox_transaction_get_mailbox(t)),
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi attr_name,
bf7dc750b95039981c0e9d728f313d50cf38a156Martti Rannanjärvi mailbox_get_last_internal_error(mailbox_transaction_get_mailbox(t), NULL));
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi }
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi }
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi return ret;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi}
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomistatic
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomiint init_test_mail_user(void)
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi{
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi struct setting_parser_context *set_parser;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi const char *error;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi char path_buf[4096];
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi if (getcwd(path_buf, sizeof(path_buf)) == NULL)
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi i_fatal("getcwd() failed: %m");
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi mail_home = p_strdup_printf(test_pool, "%s/mcp_user/", path_buf);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi struct mail_storage_service_input input = {
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi .userdb_fields = (const char*const[]){
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi t_strdup_printf("mail=maildir:~/"),
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi t_strdup_printf("home=%s", mail_home),
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi t_strdup_printf("mail_crypt_curve=prime256v1"),
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi NULL
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi },
e98ba66a8ead146be66ba8219038a45fbf87b62cStephan Bosch .username = "mcp_test@example.com",
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi .no_userdb_lookup = TRUE,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi .debug = TRUE,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi };
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi mail_storage_service = mail_storage_service_init(master_service, NULL,
532bb6cdc2fc62ae39ed51a694528852c2da9e07Aki Tuomi MAIL_STORAGE_SERVICE_FLAG_NO_RESTRICT_ACCESS |
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi MAIL_STORAGE_SERVICE_FLAG_NO_LOG_INIT |
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi MAIL_STORAGE_SERVICE_FLAG_NO_PLUGINS);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi if (mail_storage_service_lookup(mail_storage_service, &input,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi &test_service_user, &error) < 0)
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi {
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi i_error("Cannot lookup test user: %s", error);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi return -1;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi }
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi set_parser =
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi mail_storage_service_user_get_settings_parser(test_service_user);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi if (settings_parse_line(set_parser,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi t_strdup_printf("mail_attribute_dict=file:%s/dovecot-attributes",
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi mail_home)) < 0) {
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi i_error("Cannot set mail_attribute_dict: %s",
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi settings_parser_get_error(set_parser));
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi return -1;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi }
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi if (mail_storage_service_next(mail_storage_service, test_service_user,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi &test_mail_user, &error) < 0)
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi {
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi i_error("Cannot lookup test user: %s", error);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi return -1;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi }
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi return 0;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi}
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomistatic
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomivoid deinit_test_mail_user()
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi{
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi const char *error;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi mail_user_unref(&test_mail_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 &error) < 0)
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi i_error("unlink_directory(%s) failed: %s", mail_home, error);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi}
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomistatic void test_generate_user_key(void)
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi{
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi struct dcrypt_keypair pair;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi const char *pubid;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi const char *error = NULL;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_begin("generate user key");
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi /* try to generate a keypair for user */
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi if (mail_crypt_user_generate_keypair(test_mail_user, &pair,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi &pubid, &error) < 0) {
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi i_error("generate_keypair failed: %s", error);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_exit(1);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi }
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(pubid != NULL);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_user_key_id = p_strdup(test_pool, pubid);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi dcrypt_keypair_unref(&pair);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi error = NULL;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
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 {
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi i_error("Cannot get user private key: %s", error);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi }
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(pair.priv != NULL);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi if (pair.priv != NULL)
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi dcrypt_key_unref_private(&pair.priv);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_end();
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi}
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomistatic void test_generate_inbox_key(void)
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi{
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi struct dcrypt_public_key *user_key;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi struct dcrypt_keypair pair;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi const char *error = NULL, *pubid = NULL;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_begin("generate inbox key");
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi if (mail_crypt_user_get_public_key(test_mail_user, &user_key,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi &error) <= 0) {
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi i_error("Cannot get user private key: %s", error);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi }
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi struct mail_namespace *ns =
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi mail_namespace_find_inbox(test_mail_user->namespaces);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi struct mailbox *box = mailbox_alloc(ns->list, "INBOX",
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi MAILBOX_FLAG_READONLY);
bf2d54df4f5943ec0617aadb1900bb8f40b12150Aki Tuomi if (mailbox_open(box) < 0)
bf2d54df4f5943ec0617aadb1900bb8f40b12150Aki Tuomi i_fatal("mailbox_open(INBOX) failed: %s",
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 &error) < 0) {
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi i_error("generate_keypair failed: %s", error);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_exit(1);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi }
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi i_assert(pubid != NULL);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi dcrypt_keypair_unref(&pair);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi dcrypt_key_unref_public(&user_key);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi mailbox_free(&box);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_box_key_id = p_strdup(test_pool, pubid);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_end();
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi}
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomistatic void test_cache_reset(void)
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi{
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi struct dcrypt_keypair pair;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi const char *error = NULL;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_begin("cache reset");
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi struct mail_crypt_user *muser = mail_crypt_get_mail_crypt_user(test_mail_user);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi mail_crypt_key_cache_destroy(&muser->key_cache);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(mail_crypt_user_get_private_key(test_mail_user, NULL,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi &pair.priv, &error) > 0);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi if (error != NULL)
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi i_error("mail_crypt_user_get_private_key() failed: %s", error);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi error = NULL;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(mail_crypt_user_get_public_key(test_mail_user,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi &pair.pub, &error) > 0);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi if (error != NULL)
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi i_error("mail_crypt_user_get_public_key() failed: %s", error);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi dcrypt_keypair_unref(&pair);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_end();
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi}
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomistatic void test_verify_keys(void)
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi{
5d7fe1420add7667840a81445bc261d2cae1938cTimo Sirainen const char *value = "", *error = NULL;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi const char *enc_id;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi enum dcrypt_key_encryption_type enc_type;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_begin("verify keys");
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi struct dcrypt_private_key *privkey = NULL, *user_key = NULL;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi struct dcrypt_public_key *pubkey = NULL;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi struct mail_namespace *ns =
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi mail_namespace_find_inbox(test_mail_user->namespaces);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi struct mailbox *box = mailbox_alloc(ns->list, "INBOX",
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi MAILBOX_FLAG_READONLY);
bf2d54df4f5943ec0617aadb1900bb8f40b12150Aki Tuomi if (mailbox_open(box) < 0)
bf2d54df4f5943ec0617aadb1900bb8f40b12150Aki Tuomi i_fatal("mailbox_open(INBOX) failed: %s",
bf7dc750b95039981c0e9d728f313d50cf38a156Martti Rannanjärvi mailbox_get_last_internal_error(box, NULL));
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi /* verify links */
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi /* user's public key */
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(test_mail_attribute_get(box, TRUE, TRUE, ACTIVE_KEY_NAME,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi &value, &error) > 0);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(strcmp(value, test_user_key_id) == 0);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(test_mail_attribute_get(box, TRUE, TRUE, value, &value,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi &error) > 0);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi /* load key */
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(dcrypt_key_load_public(&pubkey, value, &error) == TRUE);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi /* see if it matches */
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(mail_crypt_public_key_id_match(pubkey, test_user_key_id,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi &error) > 0);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi dcrypt_key_unref_public(&pubkey);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi /* user's private key */
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(test_mail_attribute_get(box, TRUE, FALSE, ACTIVE_KEY_NAME,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi &value, &error) > 0);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(strcmp(value, test_user_key_id) == 0);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(test_mail_attribute_get(box, TRUE, FALSE, value, &value,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi &error) > 0);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi /* load key */
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(dcrypt_key_load_private(&user_key, value, NULL, NULL,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi &error) == TRUE);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi /* see if it matches */
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(mail_crypt_private_key_id_match(user_key, test_user_key_id,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi &error) > 0);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi /* inbox's public key */
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(test_mail_attribute_get(box, FALSE, TRUE, ACTIVE_KEY_NAME,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi &value, &error) > 0);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(strcmp(value, test_box_key_id) == 0);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(test_mail_attribute_get(box, FALSE, TRUE, value, &value,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi &error) > 0);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi /* load key */
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(dcrypt_key_load_public(&pubkey, value, &error) == TRUE);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi /* see if it matches */
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(mail_crypt_public_key_id_match(pubkey, test_box_key_id,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi &error) > 0);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi dcrypt_key_unref_public(&pubkey);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi /* user's private key */
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(test_mail_attribute_get(box, FALSE, FALSE, ACTIVE_KEY_NAME,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi &value, &error) > 0);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(strcmp(value, test_box_key_id) == 0);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(test_mail_attribute_get(box, FALSE, FALSE, value, &value,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi &error) > 0);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(dcrypt_key_string_get_info(value, NULL, NULL, NULL,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi &enc_type, &enc_id, NULL,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi &error) == TRUE);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
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
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi /* load key */
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(dcrypt_key_load_private(&privkey, value, NULL, user_key,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi &error) == TRUE);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi /* see if it matches */
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(mail_crypt_private_key_id_match(privkey, test_box_key_id,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi &error) > 0);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi dcrypt_key_unref_private(&privkey);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi dcrypt_key_unref_private(&user_key);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi mailbox_free(&box);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_end();
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi}
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomistatic void test_old_key(void)
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi{
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_begin("old keys");
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi const char *error = NULL;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi struct dcrypt_private_key *privkey = NULL;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi struct mail_namespace *ns =
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi mail_namespace_find_inbox(test_mail_user->namespaces);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi struct mailbox *box = mailbox_alloc(ns->list, "INBOX",
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi MAILBOX_FLAG_READONLY);
bf2d54df4f5943ec0617aadb1900bb8f40b12150Aki Tuomi if (mailbox_open(box) < 0)
bf2d54df4f5943ec0617aadb1900bb8f40b12150Aki Tuomi i_fatal("mailbox_open(INBOX) failed: %s",
bf7dc750b95039981c0e9d728f313d50cf38a156Martti Rannanjärvi mailbox_get_last_internal_error(box, NULL));
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
0dab9cb35a976c49b28a11e28d5570f5191f1a7aMartti Rannanjärvi struct mailbox_transaction_context *t =
0dab9cb35a976c49b28a11e28d5570f5191f1a7aMartti Rannanjärvi mailbox_transaction_begin(box, 0, __func__);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_mail_attribute_set(t, TRUE, FALSE, mcp_old_user_key_id,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi mcp_old_user_key, &error);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_mail_attribute_set(t, FALSE, FALSE, mcp_old_box_key_id,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi mcp_old_box_key, &error);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi (void)mailbox_transaction_commit(&t);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi error = NULL;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
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 &privkey, &error) > 0);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi if (error != NULL)
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi i_error("mail_crypt_get_private_key(%s) failed: %s",
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi mcp_old_box_key_id,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi error);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_assert(privkey != NULL);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi if (privkey != NULL) {
c147bff818798a979d93537f72f5c1f68f5d5ba8Aki Tuomi buffer_t *key_id = t_buffer_create(32);
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 dcrypt_key_unref_private(&privkey);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi }
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi mailbox_free(&box);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_end();
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi}
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomistatic void test_setup(void)
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi{
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi struct dcrypt_settings set = {
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi .module_dir = top_builddir "/src/lib-dcrypt/.libs"
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi };
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_pool = pool_alloconly_create(MEMPOOL_GROWING "mcp test pool", 128);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_ioloop = io_loop_create();
beec8b0a6a3ece557c9acec524e82542a9641662Aki Tuomi if (!dcrypt_initialize(NULL, &set, NULL)) {
beec8b0a6a3ece557c9acec524e82542a9641662Aki Tuomi i_info("No functional dcrypt backend found - skipping tests");
beec8b0a6a3ece557c9acec524e82542a9641662Aki Tuomi test_exit(0);
beec8b0a6a3ece557c9acec524e82542a9641662Aki Tuomi }
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi /* allocate a user */
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi if (init_test_mail_user() < 0) {
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_exit(1);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi }
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi mail_crypt_key_register_mailbox_internal_attributes();
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi}
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomistatic void test_teardown(void)
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi{
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi deinit_test_mail_user();
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi struct mail_crypt_user *muser = mail_crypt_get_mail_crypt_user(test_mail_user);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi mail_crypt_key_cache_destroy(&muser->key_cache);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi dcrypt_deinitialize();
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi io_loop_destroy(&test_ioloop);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi pool_unref(&test_pool);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi}
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomiint main(int argc, char **argv)
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi{
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi void (*tests[])(void) = {
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_setup,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_generate_user_key,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_generate_inbox_key,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_cache_reset,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_verify_keys,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_old_key,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi test_teardown,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi NULL
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi };
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi master_service = master_service_init("test-mail-key",
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi MASTER_SERVICE_FLAG_STANDALONE |
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi MASTER_SERVICE_FLAG_NO_CONFIG_SETTINGS |
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi MASTER_SERVICE_FLAG_NO_SSL_INIT |
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi MASTER_SERVICE_FLAG_NO_INIT_DATASTACK_FRAME,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi &argc, &argv, "");
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi int ret = test_run(tests);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi master_service_deinit(&master_service);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi return ret;
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi}