bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2016-2018 Dovecot authors, see the included COPYING file */
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomiint mail_crypt_load_global_public_key(const char *set_key, const char *key_data,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi const char **error_r)
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi if (!dcrypt_key_string_get_info(key_data, &format, NULL,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi if (!dcrypt_key_string_get_info(key_data, &format, NULL,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi *error_r = t_strdup_printf("%s: Couldn't parse public key: %s",
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi *error_r = t_strdup_printf("%s: key is not public", set_key);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi if (!dcrypt_key_load_public(&global_keys->public_key, key_data, &error)) {
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi *error_r = t_strdup_printf("%s: Couldn't load public key: %s",
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomimail_crypt_key_get_ids(struct dcrypt_private_key *key,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi const char **error_r)
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi /* new key ID */
c147bff818798a979d93537f72f5c1f68f5d5ba8Aki Tuomi key_id = t_buffer_create(MAIL_CRYPT_HASH_BUF_SIZE);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi if (!dcrypt_key_id_private(key, MAIL_CRYPT_KEY_ID_ALGORITHM, key_id, &error)) {
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi *error_r = t_strdup_printf("Failed to get private key ID: %s", error);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi *key_id_r = binary_to_hex(key_id->data, key_id->used);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi /* old key ID */
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi if (dcrypt_key_type_private(key) != DCRYPT_KEY_EC)
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi if (!dcrypt_key_id_private_old(key, key_id, &error)) {
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi *error_r = t_strdup_printf("Failed to get private key old ID: %s",
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi *key_id_old_r = binary_to_hex(key_id->data, key_id->used);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomiint mail_crypt_load_global_private_key(const char *set_key, const char *key_data,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi const char **error_r)
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi if (!dcrypt_key_string_get_info(key_data, &format, NULL, &kind,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi if (!dcrypt_key_string_get_info(key_data, &format, NULL, &kind,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi *error_r = t_strdup_printf("%s: Couldn't parse private"
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi *error_r = t_strdup_printf("%s: key is not private", set_key);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi if (enc_type == DCRYPT_KEY_ENCRYPTION_TYPE_PASSWORD) {
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi /* Fail here if password is not set since openssl will prompt
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi * for it otherwise */
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi "password to decrypt the key",
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi if (!dcrypt_key_load_private(&key, key_data, key_password, NULL, &error)) {
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi *error_r = t_strdup_printf("%s: Couldn't load private key: %s",
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi if (mail_crypt_key_get_ids(key, &key_id, &key_id_old, error_r) < 0) {
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomivoid mail_crypt_global_keys_init(struct mail_crypt_global_keys *global_keys_r)
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomivoid mail_crypt_global_keys_free(struct mail_crypt_global_keys *global_keys)
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi dcrypt_key_unref_public(&global_keys->public_key);
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi if (!array_is_created(&global_keys->private_keys))
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi array_foreach_modifiable(&global_keys->private_keys, priv_key) {
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomimail_crypt_global_key_find(struct mail_crypt_global_keys *global_keys,
4c78d9e646c4a1158d7167806937c02d86cdfc25Aki Tuomi const struct mail_crypt_global_private_key *priv_key;
e41e5d5456fb6c78693a385239ef5b0cbbd57408Aki Tuomi if (!array_is_created(&global_keys->private_keys))