text
stringlengths
0
357
{
dc_hash_clear(helper->signatures);
free(helper->signatures);
helper->signatures = NULL;
}
}
/*******************************************************************************
* Decrypt
******************************************************************************/
static int has_decrypted_pgp_armor(const char* str__, int str_bytes)
{
const unsigned char* str_end = (const unsigned char*)str__+str_bytes;
const unsigned char* p=(const unsigned char*)str__;
while (p < str_end) {
if (*p > ' ') {
break;
}
p++;
str_bytes--;
}
if (str_bytes>27 && strncmp((const char*)p, "-----BEGIN PGP MESSAGE-----", 27)==0) {
return 1;
}
return 0;
}
static int decrypt_part(dc_context_t* context,
struct mailmime* mime,
const dc_keyring_t* private_keyring,
const dc_keyring_t* public_keyring_for_validate, /*may be NULL*/
dc_hash_t* ret_valid_signatures,
struct mailmime** ret_decrypted_mime)
{
struct mailmime_data* mime_data = NULL;
int mime_transfer_encoding = MAILMIME_MECHANISM_BINARY;
char* transfer_decoding_buffer = NULL; /* mmap_string_unref()'d if set */
const char* decoded_data = NULL; /* must not be free()'d */
size_t decoded_data_bytes = 0;
void* plain_buf = NULL;
size_t plain_bytes = 0;
int sth_decrypted = 0;
*ret_decrypted_mime = NULL;
/* get data pointer from `mime` */
mime_data = mime->mm_data.mm_single;
if (mime_data->dt_type!=MAILMIME_DATA_TEXT /* MAILMIME_DATA_FILE indicates, the data is in a file; AFAIK this is not used on parsing */
|| mime_data->dt_data.dt_text.dt_data==NULL
|| mime_data->dt_data.dt_text.dt_length <= 0) {
goto cleanup;
}
/* check headers in `mime` */
if (mime->mm_mime_fields!=NULL) {
clistiter* cur;
for (cur = clist_begin(mime->mm_mime_fields->fld_list); cur!=NULL; cur = clist_next(cur)) {
struct mailmime_field* field = (struct mailmime_field*)clist_content(cur);
if (field) {
if (field->fld_type==MAILMIME_FIELD_TRANSFER_ENCODING && field->fld_data.fld_encoding) {
mime_transfer_encoding = field->fld_data.fld_encoding->enc_type;
}
}
}
}
/* regard `Content-Transfer-Encoding:` */
if (mime_transfer_encoding==MAILMIME_MECHANISM_7BIT
|| mime_transfer_encoding==MAILMIME_MECHANISM_8BIT
|| mime_transfer_encoding==MAILMIME_MECHANISM_BINARY)
{
decoded_data = mime_data->dt_data.dt_text.dt_data;
decoded_data_bytes = mime_data->dt_data.dt_text.dt_length;
if (decoded_data==NULL || decoded_data_bytes <= 0) {
goto cleanup; /* no error - but no data */
}
}
else
{
int r;
size_t current_index = 0;
r = mailmime_part_parse(mime_data->dt_data.dt_text.dt_data, mime_data->dt_data.dt_text.dt_length,
&current_index, mime_transfer_encoding,
&transfer_decoding_buffer, &decoded_data_bytes);
if (r!=MAILIMF_NO_ERROR || transfer_decoding_buffer==NULL || decoded_data_bytes <= 0) {
goto cleanup;
}
decoded_data = transfer_decoding_buffer;
}
/* encrypted, decoded data in decoded_data now ... */
if (!has_decrypted_pgp_armor(decoded_data, decoded_data_bytes)) {
goto cleanup;
}
dc_hash_t* add_signatures = dc_hash_cnt(ret_valid_signatures)<=0?