text
stringlengths
0
357
char* ctext = NULL;
size_t ctext_bytes = 0;
dc_array_t* peerstates = dc_array_new(NULL, 10);
if (helper) { memset(helper, 0, sizeof(dc_e2ee_helper_t)); }
if (context==NULL || context->magic!=DC_CONTEXT_MAGIC || recipients_addr==NULL || in_out_message==NULL
|| in_out_message->mm_parent /* libEtPan's pgp_encrypt_mime() takes the parent as the new root. We just expect the root as being given to this function. */
|| autocryptheader==NULL || keyring==NULL || sign_key==NULL || plain==NULL || helper==NULL) {
goto cleanup;
}
/* init autocrypt header from db */
autocryptheader->prefer_encrypt = DC_PE_NOPREFERENCE;
if (dc_sqlite3_get_config_int(context->sql, "e2ee_enabled", DC_E2EE_DEFAULT_ENABLED)) {
autocryptheader->prefer_encrypt = DC_PE_MUTUAL;
}
autocryptheader->addr = dc_sqlite3_get_config(context->sql, "configured_addr", NULL);
if (autocryptheader->addr==NULL) {
goto cleanup;
}
if (!load_or_generate_self_public_key(context, autocryptheader->public_key, autocryptheader->addr, in_out_message/*only for random-seed*/)) {
goto cleanup;
}
/* load peerstate information etc. */
if (autocryptheader->prefer_encrypt==DC_PE_MUTUAL || e2ee_guaranteed)
{
do_encrypt = 1;
clistiter* iter1;
for (iter1 = clist_begin(recipients_addr); iter1!=NULL ; iter1=clist_next(iter1)) {
const char* recipient_addr = clist_content(iter1);
dc_apeerstate_t* peerstate = dc_apeerstate_new(context);
dc_key_t* key_to_use = NULL;
if (strcasecmp(recipient_addr, autocryptheader->addr)==0)
{
; // encrypt to SELF, this key is added below
}
else if (dc_apeerstate_load_by_addr(peerstate, context->sql, recipient_addr)
&& (key_to_use=dc_apeerstate_peek_key(peerstate, min_verified))!=NULL
&& (peerstate->prefer_encrypt==DC_PE_MUTUAL || e2ee_guaranteed))
{
dc_keyring_add(keyring, key_to_use); /* we always add all recipients (even on IMAP upload) as otherwise forwarding may fail */
dc_array_add_ptr(peerstates, peerstate);
}
else
{
dc_apeerstate_unref(peerstate);
do_encrypt = 0;
break; /* if we cannot encrypt to a single recipient, we cannot encrypt the message at all */
}
}
}
if (do_encrypt) {
dc_keyring_add(keyring, autocryptheader->public_key); /* we always add ourself as otherwise forwarded messages are not readable */
if (!dc_key_load_self_private(sign_key, autocryptheader->addr, context->sql)) {
do_encrypt = 0;
}
}
if (force_unencrypted) {
do_encrypt = 0;
}
if ((imffields_unprotected=mailmime_find_mailimf_fields(in_out_message))==NULL) {
goto cleanup;
}
/* encrypt message, if possible */
if (do_encrypt)
{
/* prepare part to encrypt */
mailprivacy_prepare_mime(in_out_message); /* encode quoted printable all text parts */
struct mailmime* part_to_encrypt = in_out_message->mm_data.mm_message.mm_msg_mime;
part_to_encrypt->mm_parent = NULL;
struct mailimf_fields* imffields_encrypted = mailimf_fields_new_empty();
struct mailmime* message_to_encrypt = mailmime_new(MAILMIME_MESSAGE, NULL, 0, mailmime_fields_new_empty(), /* mailmime_new_message_data() calls mailmime_fields_new_with_version() which would add the unwanted MIME-Version:-header */
mailmime_get_content_message(), NULL, NULL, NULL, NULL, imffields_encrypted, part_to_encrypt);
/* gossip keys */
if (do_gossip) {
int iCnt = dc_array_get_cnt(peerstates);
if (iCnt > 1) {
for (int i = 0; i < iCnt; i++) {
char* p = dc_apeerstate_render_gossip_header((dc_apeerstate_t*)dc_array_get_ptr(peerstates, i), min_verified);
if (p) {
mailimf_fields_add(imffields_encrypted, mailimf_field_new_custom(strdup("Autocrypt-Gossip"), p/*takes ownership*/));
}
}
}
}
/* memoryhole headers */
clistiter* cur = clist_begin(imffields_unprotected->fld_list);
while (cur!=NULL) {
int move_to_encrypted = 0;