src/message_api.c
author Volker Birk <vb@pep-project.org>
Wed, 02 Mar 2016 09:26:45 +0100
changeset 485 05643db87f77
parent 480 dc8c26e06eb0
child 486 82210e6ef7f7
permissions -rw-r--r--
implementing mistrust vs. under_attack
     1 #include "pEp_internal.h"
     2 #include "message_api.h"
     3 
     4 #include "platform.h"
     5 #include "mime.h"
     6 
     7 #include <assert.h>
     8 #include <string.h>
     9 #include <stdlib.h>
    10 
    11 #ifndef MIN
    12 #define MIN(A, B) ((B) > (A) ? (A) : (B))
    13 #endif
    14 #ifndef MAX
    15 #define MAX(A, B) ((B) > (A) ? (B) : (A))
    16 #endif
    17 
    18 
    19 static bool string_equality(const char *s1, const char *s2)
    20 {
    21     if (s1 == NULL || s2 == NULL)
    22         return false;
    23 
    24     assert(s1 && s2);
    25 
    26     return strcmp(s1, s2) == 0;
    27 }
    28 
    29 static bool is_mime_type(const bloblist_t *bl, const char *mt)
    30 {
    31     assert(mt);
    32 
    33     return bl && string_equality(bl->mime_type, mt);
    34 }
    35 
    36 static bool is_fileending(const bloblist_t *bl, const char *fe)
    37 {
    38     assert(fe);
    39 
    40     if (bl == NULL || bl->filename == NULL)
    41         return false;
    42 
    43     assert(bl && bl->filename);
    44 
    45     size_t fe_len = strlen(fe);
    46     size_t fn_len = strlen(bl->filename);
    47 
    48     if (fn_len <= fe_len)
    49         return false;
    50 
    51     assert(fn_len > fe_len);
    52 
    53     return strcmp(bl->filename + (fn_len - fe_len), fe) == 0;
    54 }
    55 
    56 static void add_opt_field(message *msg, const char *name, const char *value)
    57 {
    58     assert(msg);
    59 
    60     if (msg && name && value) {
    61         stringpair_t *pair = new_stringpair(name, value);
    62         if (pair == NULL)
    63             return;
    64 
    65         stringpair_list_t *field = stringpair_list_add(msg->opt_fields, pair);
    66         if (field == NULL)
    67             return;
    68 
    69         if (msg->opt_fields == NULL)
    70             msg->opt_fields = field;
    71     }
    72 }
    73 
    74 static char * combine_short_and_long(const char *shortmsg, const char *longmsg)
    75 {
    76     char * ptext;
    77 
    78     assert(shortmsg);
    79     assert(strcmp(shortmsg, "pEp") != 0);
    80 
    81     if (longmsg == NULL)
    82         longmsg = "";
    83 
    84     ptext = calloc(1, strlen(shortmsg) + strlen(longmsg) + 12);
    85     assert(ptext);
    86     if (ptext == NULL)
    87         return NULL;
    88 
    89     strcpy(ptext, "Subject: ");
    90     strcat(ptext, shortmsg);
    91     strcat(ptext, "\n\n");
    92     strcat(ptext, longmsg);
    93 
    94     return ptext;
    95 }
    96 
    97 static int seperate_short_and_long(const char *src, char **shortmsg, char **longmsg)
    98 {
    99     char *_shortmsg = NULL;
   100     char *_longmsg = NULL;
   101 
   102     assert(src);
   103     assert(shortmsg);
   104     assert(longmsg);
   105 
   106     *shortmsg = NULL;
   107     *longmsg = NULL;
   108 
   109     if (strncasecmp(src, "subject: ", 9) == 0) {
   110         char *line_end = strchr(src, '\n');
   111 
   112         if (line_end == NULL) {
   113             _shortmsg = strdup(src + 9);
   114             assert(_shortmsg);
   115             if (_shortmsg == NULL)
   116                 goto enomem;
   117             // _longmsg = NULL;
   118         }
   119         else {
   120             size_t n = line_end - src;
   121 
   122             if (*(line_end - 1) == '\r')
   123                 _shortmsg = strndup(src + 9, n - 10);
   124             else
   125                 _shortmsg = strndup(src + 9, n - 9);
   126             assert(_shortmsg);
   127             if (_shortmsg == NULL)
   128                 goto enomem;
   129 
   130             while (*(src + n) && (*(src + n) == '\n' || *(src + n) == '\r'))
   131                 ++n;
   132 
   133             if (*(src + n)) {
   134                 _longmsg = strdup(src + n);
   135                 assert(_longmsg);
   136                 if (_longmsg == NULL)
   137                     goto enomem;
   138             }
   139         }
   140     }
   141     else {
   142         _shortmsg = strdup("");
   143         assert(_shortmsg);
   144         if (_shortmsg == NULL)
   145             goto enomem;
   146         _longmsg = strdup(src);
   147         assert(_longmsg);
   148         if (_longmsg == NULL)
   149             goto enomem;
   150     }
   151 
   152     *shortmsg = _shortmsg;
   153     *longmsg = _longmsg;
   154 
   155     return 0;
   156 
   157 enomem:
   158     free(_shortmsg);
   159     free(_longmsg);
   160 
   161     return -1;
   162 }
   163 
   164 static PEP_STATUS copy_fields(message *dst, const message *src)
   165 {
   166     assert(dst);
   167     assert(src);
   168 
   169     free_timestamp(dst->sent);
   170     dst->sent = NULL;
   171     if (src->sent) {
   172         dst->sent = timestamp_dup(src->sent);
   173         if (dst->sent == NULL)
   174             return PEP_OUT_OF_MEMORY;
   175     }
   176 
   177     free_timestamp(dst->recv);
   178     dst->recv = NULL;
   179     if (src->recv) {
   180         dst->recv = timestamp_dup(src->recv);
   181         if (dst->recv == NULL)
   182             return PEP_OUT_OF_MEMORY;
   183     }
   184 
   185     free_identity(dst->from);
   186     dst->from = NULL;
   187     if (src->from) {
   188         dst->from = identity_dup(src->from);
   189         if (dst->from == NULL)
   190             return PEP_OUT_OF_MEMORY;
   191     }
   192 
   193     free_identity_list(dst->to);
   194     dst->to = NULL;
   195     if (src->to && src->to->ident) {
   196         dst->to = identity_list_dup(src->to);
   197         if (dst->to == NULL)
   198             return PEP_OUT_OF_MEMORY;
   199     }
   200 
   201     free_identity(dst->recv_by);
   202     dst->recv_by = NULL;
   203     if (src->recv_by) {
   204         dst->recv_by = identity_dup(src->recv_by);
   205         if (dst->recv_by == NULL)
   206             return PEP_OUT_OF_MEMORY;
   207     }
   208 
   209     free_identity_list(dst->cc);
   210     dst->cc = NULL;
   211     if (src->cc && src->cc->ident) {
   212         dst->cc = identity_list_dup(src->cc);
   213         if (dst->cc == NULL)
   214             return PEP_OUT_OF_MEMORY;
   215     }
   216 
   217     free_identity_list(dst->bcc);
   218     dst->bcc = NULL;
   219     if (src->bcc && src->bcc->ident) {
   220         dst->bcc = identity_list_dup(src->bcc);
   221         if (dst->bcc == NULL)
   222             return PEP_OUT_OF_MEMORY;
   223     }
   224 
   225     free_identity_list(dst->reply_to);
   226     dst->reply_to = NULL;
   227     if (src->reply_to && src->reply_to->ident) {
   228         dst->reply_to = identity_list_dup(src->reply_to);
   229         if (dst->reply_to == NULL)
   230             return PEP_OUT_OF_MEMORY;
   231     }
   232 
   233     free_stringlist(dst->in_reply_to);
   234     dst->in_reply_to = NULL;
   235     if (src->in_reply_to && src->in_reply_to->value) {
   236         dst->in_reply_to = stringlist_dup(src->in_reply_to);
   237         if (dst->in_reply_to == NULL)
   238             return PEP_OUT_OF_MEMORY;
   239     }
   240 
   241     free_stringlist(dst->references);
   242     dst->references = NULL;
   243     if (src->references) {
   244         dst->references = stringlist_dup(src->references);
   245         if (dst->references == NULL)
   246             return PEP_OUT_OF_MEMORY;
   247     }
   248 
   249     free_stringlist(dst->keywords);
   250     dst->keywords = NULL;
   251     if (src->keywords && src->keywords->value) {
   252         dst->keywords = stringlist_dup(src->keywords);
   253         if (dst->keywords == NULL)
   254             return PEP_OUT_OF_MEMORY;
   255     }
   256 
   257     free(dst->comments);
   258     dst->comments = NULL;
   259     if (src->comments) {
   260         dst->comments = strdup(src->comments);
   261         assert(dst->comments);
   262         if (dst->comments == NULL)
   263             return PEP_OUT_OF_MEMORY;
   264     }
   265 
   266     return PEP_STATUS_OK;
   267 }
   268 
   269 static message * clone_to_empty_message(const message * src)
   270 {
   271     PEP_STATUS status;
   272     message * msg = NULL;
   273 
   274     assert(src);
   275 
   276     msg = calloc(1, sizeof(message));
   277     assert(msg);
   278     if (msg == NULL)
   279         goto enomem;
   280 
   281     msg->dir = src->dir;
   282 
   283     status = copy_fields(msg, src);
   284     if (status != PEP_STATUS_OK)
   285         goto enomem;
   286 
   287     return msg;
   288 
   289 enomem:
   290     free_message(msg);
   291     return NULL;
   292 }
   293 
   294 static PEP_STATUS encrypt_PGP_MIME(
   295     PEP_SESSION session,
   296     const message *src,
   297     stringlist_t *keys,
   298     message *dst
   299     )
   300 {
   301     PEP_STATUS status = PEP_STATUS_OK;
   302     bool free_ptext = false;
   303     char *ptext = NULL;
   304     char *ctext;
   305     char *_ctext = NULL;
   306     char *mimetext = NULL;
   307     size_t csize;
   308     assert(dst->longmsg == NULL);
   309     dst->enc_format = PEP_enc_PGP_MIME;
   310 
   311     if (src->shortmsg && strcmp(src->shortmsg, "pEp") != 0) {
   312         if (session->unencrypted_subject) {
   313             dst->shortmsg = strdup(src->shortmsg);
   314             assert(dst->shortmsg);
   315             if (dst->shortmsg == NULL)
   316                 goto enomem;
   317             ptext = src->longmsg;
   318         }
   319         else {
   320             ptext = combine_short_and_long(src->shortmsg, src->longmsg);
   321             if (ptext == NULL)
   322                 goto enomem;
   323             free_ptext = true;
   324         }
   325     }
   326     else if (src->longmsg) {
   327         ptext = src->longmsg;
   328     }
   329     else {
   330         ptext = "pEp";
   331     }
   332 
   333     message *_src = calloc(1, sizeof(message));
   334     assert(_src);
   335     if (_src == NULL)
   336         goto enomem;
   337     _src->longmsg = ptext;
   338     _src->longmsg_formatted = src->longmsg_formatted;
   339     _src->attachments = src->attachments;
   340     _src->enc_format = PEP_enc_none;
   341     status = mime_encode_message(_src, true, &mimetext);
   342     assert(status == PEP_STATUS_OK);
   343     if (free_ptext){
   344         free(ptext);
   345         free_ptext=0;
   346     }
   347     free(_src);
   348     assert(mimetext);
   349     if (mimetext == NULL)
   350         goto pep_error;
   351 
   352     status = encrypt_and_sign(session, keys, mimetext, strlen(mimetext),
   353         &ctext, &csize);
   354     free(mimetext);
   355     if (ctext == NULL)
   356         goto pep_error;
   357 
   358     dst->longmsg = strdup("this message was encrypted with p≡p "
   359         "http://pEp-project.org");
   360     assert(dst->longmsg);
   361     if (dst->longmsg == NULL)
   362         goto enomem;
   363 
   364     char *v = strdup("Version: 1");
   365     assert(v);
   366     if (v == NULL)
   367         goto enomem;
   368 
   369     bloblist_t *_a = new_bloblist(v, 11, "application/pgp-encrypted", NULL);
   370     if (_a == NULL)
   371         goto enomem;
   372     dst->attachments = _a;
   373 
   374     _ctext = malloc(csize);
   375     assert(_ctext);
   376     if (_ctext == NULL)
   377         goto enomem;
   378     memcpy(_ctext, ctext, csize);
   379 
   380     _a = bloblist_add(_a, _ctext, csize, "application/octet-stream",
   381         "msg.asc");
   382     if (_a == NULL)
   383         goto enomem;
   384 
   385     return PEP_STATUS_OK;
   386 
   387 enomem:
   388     status = PEP_OUT_OF_MEMORY;
   389 
   390 pep_error:
   391     if (free_ptext)
   392         free(ptext);
   393     free(_ctext);
   394     return status;
   395 }
   396 
   397 static PEP_STATUS encrypt_PGP_in_pieces(
   398     PEP_SESSION session,
   399     const message *src,
   400     stringlist_t *keys,
   401     message *dst
   402     )
   403 {
   404     PEP_STATUS status = PEP_STATUS_OK;
   405     char *ctext;
   406     size_t csize;
   407     char *ptext = NULL;
   408     bool free_ptext = false;
   409 
   410     assert(dst->longmsg == NULL);
   411     assert(dst->attachments == NULL);
   412 
   413     dst->enc_format = PEP_enc_pieces;
   414 
   415     if (src->shortmsg && src->shortmsg[0] && strcmp(src->shortmsg, "pEp") != 0) {
   416         if (session->unencrypted_subject) {
   417             dst->shortmsg = strdup(src->shortmsg);
   418             assert(dst->shortmsg);
   419             if (dst->shortmsg == NULL)
   420                 goto enomem;
   421             ptext = src->longmsg;
   422         }
   423         else {
   424             ptext = combine_short_and_long(src->shortmsg, src->longmsg);
   425             if (ptext == NULL)
   426                 goto enomem;
   427             free_ptext = true;
   428         }
   429 
   430         status = encrypt_and_sign(session, keys, ptext, strlen(ptext), &ctext,
   431             &csize);
   432         if (free_ptext)
   433             free(ptext);
   434         free_ptext = false;
   435         if (ctext) {
   436             dst->longmsg = strndup(ctext, csize);
   437             assert(dst->longmsg);
   438             if (dst->longmsg == NULL)
   439                 goto enomem;
   440         }
   441         else {
   442             goto pep_error;
   443         }
   444     }
   445     else if (src->longmsg && src->longmsg[0]) {
   446         ptext = src->longmsg;
   447         status = encrypt_and_sign(session, keys, ptext, strlen(ptext), &ctext,
   448             &csize);
   449         if (ctext) {
   450             dst->longmsg = strndup(ctext, csize);
   451             assert(dst->longmsg);
   452             if (dst->longmsg == NULL)
   453                 goto enomem;
   454         }
   455         else {
   456             goto pep_error;
   457         }
   458     }
   459     else {
   460         dst->longmsg = strdup("");
   461         assert(dst->longmsg);
   462         if (dst->longmsg == NULL)
   463             goto enomem;
   464     }
   465 
   466     if (src->longmsg_formatted && src->longmsg_formatted[0]) {
   467         ptext = src->longmsg_formatted;
   468         status = encrypt_and_sign(session, keys, ptext, strlen(ptext), &ctext,
   469             &csize);
   470         if (ctext) {
   471             char *_ctext = malloc(csize);
   472             assert(_ctext);
   473             if (_ctext == NULL)
   474                 goto enomem;
   475             memcpy(_ctext, ctext, csize);
   476 
   477             bloblist_t *_a = bloblist_add(dst->attachments, _ctext, csize,
   478                 "application/octet-stream", "PGPexch.htm.pgp");
   479             if (_a == NULL)
   480                 goto enomem;
   481             if (dst->attachments == NULL)
   482                 dst->attachments = _a;
   483         }
   484         else {
   485             goto pep_error;
   486         }
   487     }
   488 
   489     if (src->attachments) {
   490         if (dst->attachments == NULL) {
   491             dst->attachments = new_bloblist(NULL, 0, NULL, NULL);
   492             if (dst->attachments == NULL)
   493                 goto enomem;
   494         }
   495 
   496         bloblist_t *_s = src->attachments;
   497         bloblist_t *_d = dst->attachments;
   498 
   499         for (int n = 0; _s && _s->value; _s = _s->next) {
   500             size_t psize = _s->size;
   501             ptext = _s->value;
   502             status = encrypt_and_sign(session, keys, ptext, psize, &ctext,
   503                 &csize);
   504             if (ctext) {
   505                 char *filename = NULL;
   506 
   507                 if (_s->filename) {
   508                     size_t len = strlen(_s->filename);
   509                     filename = calloc(1, len + 5);
   510                     if (filename == NULL)
   511                         goto enomem;
   512 
   513                     strcpy(filename, _s->filename);
   514                     strcpy(filename + len, ".pgp");
   515                 }
   516                 else {
   517                     filename = calloc(1, 20);
   518                     if (filename == NULL)
   519                         goto enomem;
   520 
   521                     ++n;
   522                     n &= 0xffff;
   523                     snprintf(filename, 20, "Attachment%d.pgp", n);
   524                 }
   525 
   526                 char *_ctext = malloc(csize);
   527                 assert(_ctext);
   528                 if (_ctext == NULL)
   529                     goto enomem;
   530                 memcpy(_ctext, ctext, csize);
   531 
   532                 _d = bloblist_add(_d, _ctext, csize, "application/octet-stream",
   533                     filename);
   534                 if (_d == NULL)
   535                     goto enomem;
   536             }
   537             else {
   538                 goto pep_error;
   539             }
   540         }
   541     }
   542 
   543     return PEP_STATUS_OK;
   544 
   545 enomem:
   546     status = PEP_OUT_OF_MEMORY;
   547 
   548 pep_error:
   549     if (free_ptext)
   550         free(ptext);
   551     return status;
   552 }
   553 
   554 static char * keylist_to_string(const stringlist_t *keylist)
   555 {
   556     if (keylist) {
   557         size_t size = stringlist_length(keylist);
   558 
   559         const stringlist_t *_kl;
   560         for (_kl = keylist; _kl && _kl->value; _kl = _kl->next) {
   561             size += strlen(_kl->value);
   562         }
   563 
   564         char *result = calloc(1, size);
   565         if (result == NULL)
   566             return NULL;
   567 
   568         char *_r = result;
   569         for (_kl = keylist; _kl && _kl->value; _kl = _kl->next) {
   570             _r = stpcpy(_r, _kl->value);
   571             if (_kl->next && _kl->next->value)
   572                 _r = stpcpy(_r, ",");
   573         }
   574 
   575         return result;
   576     }
   577     else {
   578         return NULL;
   579     }
   580 }
   581 
   582 static const char * color_to_string(PEP_color color)
   583 {
   584     switch (color) {
   585     case PEP_rating_cannot_decrypt:
   586         return "cannot_decrypt";
   587     case PEP_rating_have_no_key:
   588         return "have_no_key";
   589     case PEP_rating_unencrypted:
   590         return "unencrypted";
   591     case PEP_rating_unreliable:
   592         return "unreliable";
   593     case PEP_rating_reliable:
   594         return "reliable";
   595     case PEP_rating_trusted:
   596         return "trusted";
   597     case PEP_rating_trusted_and_anonymized:
   598         return "trusted_and_anonymized";
   599     case PEP_rating_fully_anonymous:
   600         return "fully_anonymous";
   601     case PEP_rating_mistrust:
   602         return "mistrust";
   603     case PEP_rating_b0rken:
   604         return "b0rken";
   605     case PEP_rating_under_attack:
   606         return "unter_attack";
   607     default:
   608         return "undefined";
   609     }
   610 }
   611 
   612 static void decorate_message(
   613     message *msg,
   614     PEP_color color,
   615     stringlist_t *keylist
   616     )
   617 {
   618     assert(msg);
   619 
   620     add_opt_field(msg, "X-pEp-Version", "1.0");
   621     
   622     if (color != PEP_rating_undefined)
   623         add_opt_field(msg, "X-EncStatus", color_to_string(color));
   624 
   625     if (keylist) {
   626         char *_keylist = keylist_to_string(keylist);
   627         add_opt_field(msg, "X-KeyList", _keylist);
   628         free(_keylist);
   629     }
   630 }
   631 
   632 static PEP_color _rating(PEP_comm_type ct)
   633 {
   634     if (ct == PEP_ct_unknown)
   635         return PEP_rating_undefined;
   636 
   637     else if (ct == PEP_ct_compromized)
   638         return PEP_rating_under_attack;
   639 
   640     else if (ct == PEP_ct_mistrusted)
   641         return PEP_rating_mistrust;
   642     
   643     else if (ct >= PEP_ct_confirmed_enc_anon)
   644         return PEP_rating_trusted_and_anonymized;
   645 
   646     else if (ct >= PEP_ct_strong_encryption)
   647         return PEP_rating_trusted;
   648 
   649     else if (ct >= PEP_ct_strong_but_unconfirmed && ct < PEP_ct_confirmed)
   650         return PEP_rating_reliable;
   651 
   652     else if (ct == PEP_ct_no_encryption || ct == PEP_ct_no_encrypted_channel ||
   653             ct == PEP_ct_my_key_not_included)
   654         return PEP_rating_unencrypted;
   655 
   656     else
   657         return PEP_rating_unreliable;
   658 }
   659 
   660 static bool is_encrypted_attachment(const bloblist_t *blob)
   661 {
   662     char *ext;
   663 
   664     assert(blob);
   665 
   666     if (blob->filename == NULL)
   667         return false;
   668 
   669     ext = strrchr(blob->filename, '.');
   670     if (ext == NULL)
   671         return false;
   672 
   673     if (strcmp(blob->mime_type, "application/octet-stream") == 0) {
   674         if (strcmp(ext, ".pgp") == 0 || strcmp(ext, ".gpg") == 0 ||
   675             strcmp(ext, ".asc") == 0)
   676             return true;
   677     }
   678     else if (strcmp(blob->mime_type, "text/plain") == 0) {
   679         if (strcmp(ext, ".asc") == 0)
   680             return true;
   681     }
   682 
   683     return false;
   684 }
   685 
   686 static bool is_encrypted_html_attachment(const bloblist_t *blob)
   687 {
   688     assert(blob);
   689     assert(blob->filename);
   690 
   691     if (strncmp(blob->filename, "PGPexch.htm.", 12) == 0) {
   692         if (strcmp(blob->filename + 11, ".pgp") == 0 ||
   693             strcmp(blob->filename + 11, ".asc") == 0)
   694             return true;
   695     }
   696 
   697     return false;
   698 }
   699 
   700 static char * without_double_ending(const char *filename)
   701 {
   702     char *ext;
   703 
   704     assert(filename);
   705 
   706     ext = strrchr(filename, '.');
   707     if (ext == NULL)
   708         return NULL;
   709 
   710     char *result = strndup(filename, ext - filename);
   711     assert(result);
   712     return result;
   713 }
   714 
   715 static PEP_color decrypt_color(PEP_STATUS status)
   716 {
   717     switch (status) {
   718     case PEP_UNENCRYPTED:
   719     case PEP_VERIFIED:
   720     case PEP_VERIFY_NO_KEY:
   721     case PEP_VERIFIED_AND_TRUSTED:
   722         return PEP_rating_unencrypted;
   723 
   724     case PEP_DECRYPTED:
   725         return PEP_rating_unreliable;
   726 
   727     case PEP_DECRYPTED_AND_VERIFIED:
   728         return PEP_rating_reliable;
   729 
   730     case PEP_DECRYPT_NO_KEY:
   731         return PEP_rating_have_no_key;
   732 
   733     case PEP_DECRYPT_WRONG_FORMAT:
   734     case PEP_CANNOT_DECRYPT_UNKNOWN:
   735         return PEP_rating_cannot_decrypt;
   736 
   737     default:
   738         return PEP_rating_undefined;
   739     }
   740 }
   741 
   742 static PEP_color key_color(PEP_SESSION session, const char *fpr)
   743 {
   744     PEP_comm_type comm_type = PEP_ct_unknown;
   745 
   746     assert(session);
   747     assert(fpr);
   748 
   749     PEP_STATUS status = get_key_rating(session, fpr, &comm_type);
   750     if (status != PEP_STATUS_OK)
   751         return PEP_rating_undefined;
   752 
   753     return _rating(comm_type);
   754 }
   755 
   756 static PEP_color keylist_color(PEP_SESSION session, stringlist_t *keylist)
   757 {
   758     PEP_color color = PEP_rating_reliable;
   759 
   760     assert(keylist && keylist->value);
   761     if (keylist == NULL || keylist->value == NULL)
   762         return PEP_rating_unencrypted;
   763 
   764     stringlist_t *_kl;
   765     for (_kl = keylist; _kl && _kl->value; _kl = _kl->next) {
   766         PEP_comm_type ct;
   767         PEP_STATUS status;
   768 
   769         color = key_color(session, _kl->value);
   770         if (color <= PEP_rating_mistrust)
   771             return color;
   772 
   773         if (color >= PEP_rating_reliable) {
   774             status = least_trust(session, _kl->value, &ct);
   775             if (status != PEP_STATUS_OK)
   776                 return PEP_rating_undefined;
   777             if (ct == PEP_ct_unknown)
   778                 color = PEP_rating_unreliable;
   779             else
   780                 color = _rating(ct);
   781         }
   782     }
   783 
   784     return color;
   785 }
   786 
   787 static PEP_comm_type _get_comm_type(
   788     PEP_SESSION session,
   789     PEP_comm_type max_comm_type,
   790     pEp_identity *ident
   791     )
   792 {
   793     PEP_STATUS status = update_identity(session, ident);
   794 
   795     if (max_comm_type == PEP_ct_compromized)
   796         return PEP_ct_compromized;
   797 
   798     if (status == PEP_STATUS_OK) {
   799         if (ident->comm_type == PEP_ct_compromized)
   800             return PEP_ct_compromized;
   801         else
   802             return MIN(max_comm_type, ident->comm_type);
   803     }
   804     else {
   805         return PEP_ct_unknown;
   806     }
   807 }
   808 
   809 void import_attached_keys(PEP_SESSION session, const message *msg)
   810 {
   811     assert(session);
   812     assert(msg);
   813 
   814     bloblist_t *bl;
   815     for (bl = msg->attachments; bl && bl->value; bl = bl->next) {
   816         assert(bl && bl->value && bl->size);
   817 
   818         // workaround for Apple Mail bugs
   819         if (is_mime_type(bl, "application/x-apple-msg-attachment")) {
   820             if (is_fileending(bl, ".asc")) {
   821                 if (strlen(bl->filename) == 14 &&
   822                         bl->filename[0] == '0' && bl->filename[1] == 'x')
   823                     import_key(session, bl->value, bl->size);
   824                 else if (strlen(bl->filename) == 12)
   825                     import_key(session, bl->value, bl->size);
   826             }
   827         }
   828         else if (bl->mime_type == NULL ||
   829                     is_mime_type(bl, "application/octet-stream")) {
   830             if (is_fileending(bl, ".pgp") || is_fileending(bl, ".gpg") ||
   831                     is_fileending(bl, ".key") || is_fileending(bl, ".asc"))
   832                 import_key(session, bl->value, bl->size);
   833         }
   834         else if (is_mime_type(bl, "application/pgp-keys")) {
   835             import_key(session, bl->value, bl->size);
   836         }
   837         else if (is_mime_type(bl, "text/plain")) {
   838             if (is_fileending(bl, ".pgp") || is_fileending(bl, ".gpg") ||
   839                     is_fileending(bl, ".key") || is_fileending(bl, ".asc"))
   840                 import_key(session, bl->value, bl->size);
   841         }
   842     }
   843     if(msg->from && msg->from->user_id && msg->from->address)
   844         update_identity(session, msg->from);
   845 }
   846 
   847 void attach_own_key(PEP_SESSION session, message *msg)
   848 {
   849     char *keydata;
   850     size_t size;
   851     bloblist_t *bl;
   852 
   853     assert(session);
   854     assert(msg);
   855 
   856     if (msg->dir == PEP_dir_incoming)
   857         return;
   858 
   859     assert(msg->from && msg->from->fpr);
   860     if (msg->from == NULL || msg->from->fpr == NULL)
   861         return;
   862 
   863     PEP_STATUS status = export_key(session, msg->from->fpr, &keydata, &size);
   864     assert(status == PEP_STATUS_OK);
   865     if (status != PEP_STATUS_OK)
   866         return;
   867     assert(size);
   868 
   869     bl = bloblist_add(msg->attachments, keydata, size, "application/pgp-keys",
   870             "pEpkey.asc");
   871     if (msg->attachments == NULL && bl)
   872         msg->attachments = bl;
   873 }
   874 
   875 PEP_cryptotech determine_encryption_format(message *msg)
   876 {
   877     assert(msg);
   878 
   879     if (is_PGP_message_text(msg->longmsg)) {
   880         msg->enc_format = PEP_enc_pieces;
   881         return PEP_crypt_OpenPGP;
   882     }
   883     else if (msg->attachments && msg->attachments->next &&
   884             is_mime_type(msg->attachments, "application/pgp-encrypted") &&
   885             is_PGP_message_text(msg->attachments->next->value)
   886         ) {
   887         msg->enc_format = PEP_enc_PGP_MIME;
   888         return PEP_crypt_OpenPGP;
   889     }
   890     else {
   891         msg->enc_format = PEP_enc_none;
   892         return PEP_crypt_none;
   893     }
   894 }
   895 
   896 DYNAMIC_API PEP_STATUS encrypt_message(
   897         PEP_SESSION session,
   898         message *src,
   899         stringlist_t * extra,
   900         message **dst,
   901         PEP_enc_format enc_format
   902     )
   903 {
   904     PEP_STATUS status = PEP_STATUS_OK;
   905     message * msg = NULL;
   906     stringlist_t * keys = NULL;
   907 
   908     assert(session);
   909     assert(src);
   910     assert(dst);
   911     assert(enc_format != PEP_enc_none);
   912 
   913     if (!(session && src && dst && enc_format != PEP_enc_none))
   914         return PEP_ILLEGAL_VALUE;
   915 
   916     determine_encryption_format(src);
   917     if (src->enc_format != PEP_enc_none)
   918         return PEP_ILLEGAL_VALUE;
   919 
   920     *dst = NULL;
   921 
   922     status = myself(session, src->from);
   923     if (status != PEP_STATUS_OK)
   924         goto pep_error;
   925 
   926     keys = new_stringlist(src->from->fpr);
   927     if (keys == NULL)
   928         goto enomem;
   929 
   930     stringlist_t *_k = keys;
   931 
   932     if (extra) {
   933         _k = stringlist_append(_k, extra);
   934         if (_k == NULL)
   935             goto enomem;
   936     }
   937 
   938     bool dest_keys_found = true;
   939 
   940     identity_list * _il;
   941     for (_il = src->to; _il && _il->ident; _il = _il->next) {
   942         PEP_STATUS _status = update_identity(session, _il->ident);
   943         if (_status != PEP_STATUS_OK) {
   944             status = _status;
   945             goto pep_error;
   946         }
   947 
   948         if (_il->ident->fpr && _il->ident->fpr[0]) {
   949             _k = stringlist_add(_k, _il->ident->fpr);
   950             if (_k == NULL)
   951                 goto enomem;
   952         }
   953         else {
   954             dest_keys_found = false;
   955             status = PEP_KEY_NOT_FOUND;
   956         }
   957     }
   958 
   959     for (_il = src->cc; _il && _il->ident; _il = _il->next) {
   960         PEP_STATUS _status = update_identity(session, _il->ident);
   961         if (_status != PEP_STATUS_OK)
   962         {
   963             status = _status;
   964             goto pep_error;
   965         }
   966 
   967         if (_il->ident->fpr && _il->ident->fpr[0]) {
   968             _k = stringlist_add(_k, _il->ident->fpr);
   969             if (_k == NULL)
   970                 goto enomem;
   971         }
   972         else {
   973             dest_keys_found = false;
   974             status = PEP_KEY_NOT_FOUND;
   975         }
   976     }
   977 
   978     if (!dest_keys_found) {
   979         free_stringlist(keys);
   980         if (!session->passive_mode)
   981             attach_own_key(session, src);
   982         return PEP_UNENCRYPTED;
   983     }
   984     else {
   985         msg = clone_to_empty_message(src);
   986         if (msg == NULL)
   987             goto enomem;
   988 
   989         attach_own_key(session, src);
   990 
   991         switch (enc_format) {
   992         case PEP_enc_PGP_MIME:
   993         case PEP_enc_PEP: // BUG: should be implemented extra
   994             status = encrypt_PGP_MIME(session, src, keys, msg);
   995             break;
   996 
   997         case PEP_enc_pieces:
   998             status = encrypt_PGP_in_pieces(session, src, keys, msg);
   999             break;
  1000 
  1001         /* case PEP_enc_PEP:
  1002             // TODO: implement
  1003             NOT_IMPLEMENTED */
  1004 
  1005         default:
  1006             assert(0);
  1007             status = PEP_ILLEGAL_VALUE;
  1008             goto pep_error;
  1009         }
  1010         
  1011         if (status == PEP_OUT_OF_MEMORY)
  1012             goto enomem;
  1013         
  1014         if (status != PEP_STATUS_OK)
  1015             goto pep_error;
  1016     }
  1017 
  1018     free_stringlist(keys);
  1019 
  1020     if (msg && msg->shortmsg == NULL) {
  1021         msg->shortmsg = strdup("pEp");
  1022         assert(msg->shortmsg);
  1023     }
  1024 
  1025     if (msg)
  1026         decorate_message(msg, PEP_rating_undefined, NULL);
  1027 
  1028     *dst = msg;
  1029     return status;
  1030 
  1031 enomem:
  1032     status = PEP_OUT_OF_MEMORY;
  1033 
  1034 pep_error:
  1035     free_stringlist(keys);
  1036     free_message(msg);
  1037 
  1038     return status;
  1039 }
  1040 
  1041 DYNAMIC_API PEP_STATUS decrypt_message(
  1042         PEP_SESSION session,
  1043         message *src,
  1044         message **dst,
  1045         stringlist_t **keylist,
  1046         PEP_color *color
  1047     )
  1048 {
  1049     PEP_STATUS status = PEP_STATUS_OK;
  1050     PEP_STATUS decrypt_status = PEP_CANNOT_DECRYPT_UNKNOWN;
  1051     message *msg = NULL;
  1052     char *ctext;
  1053     size_t csize;
  1054     char *ptext = NULL;
  1055     size_t psize;
  1056     stringlist_t *_keylist = NULL;
  1057 
  1058     assert(session);
  1059     assert(src);
  1060     assert(dst);
  1061     assert(keylist);
  1062     assert(color);
  1063 
  1064     if (!(session && src && dst && keylist && color))
  1065         return PEP_ILLEGAL_VALUE;
  1066 
  1067     import_attached_keys(session, src);
  1068     PEP_cryptotech crypto = determine_encryption_format(src);
  1069 
  1070     *dst = NULL;
  1071     *keylist = NULL;
  1072     *color = PEP_rating_undefined;
  1073  
  1074     switch (src->enc_format) {
  1075         case PEP_enc_none:
  1076             *color = PEP_rating_unencrypted;
  1077             return PEP_UNENCRYPTED;
  1078 
  1079         case PEP_enc_PGP_MIME:
  1080             ctext = src->attachments->next->value;
  1081             csize = src->attachments->next->size;
  1082             break;
  1083 
  1084         case PEP_enc_pieces:
  1085             ctext = src->longmsg;
  1086             csize = strlen(ctext);
  1087             break;
  1088 
  1089         default:
  1090             NOT_IMPLEMENTED
  1091     }
  1092     status = cryptotech[crypto].decrypt_and_verify(session, ctext,
  1093                                                    csize, &ptext, &psize, &_keylist);
  1094     if (status > PEP_CANNOT_DECRYPT_UNKNOWN)
  1095         goto pep_error;
  1096 
  1097     decrypt_status = status;
  1098 
  1099     if (ptext) {
  1100         switch (src->enc_format) {
  1101             case PEP_enc_PGP_MIME:
  1102                 status = mime_decode_message(ptext, psize, &msg);
  1103                 if (status != PEP_STATUS_OK)
  1104                     goto pep_error;
  1105                 break;
  1106 
  1107             case PEP_enc_pieces:
  1108                 msg = clone_to_empty_message(src);
  1109                 if (msg == NULL)
  1110                     goto enomem;
  1111 
  1112                 msg->longmsg = strdup(ptext);
  1113                 assert(msg->longmsg);
  1114                 if (msg->longmsg == NULL)
  1115                     goto enomem;
  1116 
  1117                 bloblist_t *_m = msg->attachments;
  1118                 if (_m == NULL && src->attachments && src->attachments->value) {
  1119                     msg->attachments = new_bloblist(NULL, 0, NULL, NULL);
  1120                     _m = msg->attachments;
  1121                 }
  1122 
  1123                 bloblist_t *_s;
  1124                 for (_s = src->attachments; _s && _s->value; _s = _s->next) {
  1125                     if (is_encrypted_attachment(_s)) {
  1126                         stringlist_t *_keylist = NULL;
  1127                         char *attctext;
  1128                         size_t attcsize;
  1129 
  1130                         attctext = _s->value;
  1131                         attcsize = _s->size;
  1132 
  1133                         status = decrypt_and_verify(session, attctext, attcsize,
  1134                                 &ptext, &psize, &_keylist);
  1135                         free_stringlist(_keylist);
  1136 
  1137                         if (ptext) {
  1138                             if (is_encrypted_html_attachment(_s)) {
  1139                                 msg->longmsg_formatted = strdup(ptext);
  1140                                 assert(msg->longmsg_formatted);
  1141                                 if (msg->longmsg_formatted == NULL)
  1142                                     goto pep_error;
  1143                             }
  1144                             else {
  1145                                 char * mime_type = "application/octet-stream";
  1146                                 char * filename =
  1147                                     without_double_ending(_s->filename);
  1148                                 if (filename == NULL)
  1149                                     goto enomem;
  1150 
  1151                                 char *_ptext = malloc(psize);
  1152                                 assert(_ptext);
  1153                                 if (_ptext == NULL)
  1154                                     goto enomem;
  1155                                 memcpy(_ptext, ptext, psize);
  1156 
  1157                                 _m = bloblist_add(_m, _ptext, psize, mime_type,
  1158                                     filename);
  1159                                 if (_m == NULL)
  1160                                     goto enomem;
  1161 
  1162                                 if (msg->attachments == NULL)
  1163                                     msg->attachments = _m;
  1164                             }
  1165                         }
  1166                         else {
  1167                             char *copy = malloc(_s->size);
  1168                             assert(copy);
  1169                             if (copy == NULL)
  1170                                 goto enomem;
  1171                             memcpy(copy, _s->value, _s->size);
  1172                             _m = bloblist_add(_m, copy, _s->size, _s->mime_type, _s->filename);
  1173                             if (_m == NULL)
  1174                                 goto enomem;
  1175                         }
  1176                     }
  1177                     else {
  1178                         char *copy = malloc(_s->size);
  1179                         assert(copy);
  1180                         if (copy == NULL)
  1181                             goto enomem;
  1182                         memcpy(copy, _s->value, _s->size);
  1183                         _m = bloblist_add(_m, copy, _s->size, _s->mime_type, _s->filename);
  1184                         if (_m == NULL)
  1185                             goto enomem;
  1186                     }
  1187                 }
  1188 
  1189                 break;
  1190 
  1191             default:
  1192                 // BUG: must implement more
  1193                 NOT_IMPLEMENTED
  1194         }
  1195         
  1196         switch (src->enc_format) {
  1197             case PEP_enc_PGP_MIME:
  1198             case PEP_enc_pieces:
  1199                 status = copy_fields(msg, src);
  1200                 if (status != PEP_STATUS_OK)
  1201                     goto pep_error;
  1202 
  1203                 if (src->shortmsg == NULL || strcmp(src->shortmsg, "pEp") == 0)
  1204                 {
  1205                     char * shortmsg;
  1206                     char * longmsg;
  1207 
  1208                     int r = seperate_short_and_long(msg->longmsg, &shortmsg,
  1209                             &longmsg);
  1210                     if (r == -1)
  1211                         goto enomem;
  1212 
  1213                     free(msg->shortmsg);
  1214                     free(msg->longmsg);
  1215 
  1216                     msg->shortmsg = shortmsg;
  1217                     msg->longmsg = longmsg;
  1218                 }
  1219                 else {
  1220                     msg->shortmsg = strdup(src->shortmsg);
  1221                     assert(msg->shortmsg);
  1222                     if (msg->shortmsg == NULL)
  1223                         goto enomem;
  1224                 }
  1225                 break;
  1226 
  1227             default:
  1228                 // BUG: must implement more
  1229                 NOT_IMPLEMENTED
  1230         }
  1231         
  1232         import_attached_keys(session, msg);
  1233         
  1234         if(decrypt_status == PEP_DECRYPTED){
  1235             
  1236             // In case message did decrypt, but no valid signature could be found
  1237             // then retry decrypt+verify after importing key.
  1238             // TODO optimize if import_attached_keys didn't import any key
  1239             
  1240             char *re_ptext = NULL;
  1241             size_t re_psize;
  1242             
  1243             free_stringlist(_keylist);
  1244             _keylist = NULL;
  1245 
  1246             status = cryptotech[crypto].decrypt_and_verify(session, ctext,
  1247                 csize, &re_ptext, &re_psize, &_keylist);
  1248             
  1249             if(re_ptext)
  1250                 free(re_ptext);
  1251             
  1252             if (status > PEP_CANNOT_DECRYPT_UNKNOWN)
  1253                 goto pep_error;
  1254             
  1255             decrypt_status = status;
  1256         }
  1257         
  1258         *color = decrypt_color(decrypt_status);
  1259         
  1260         if (*color > PEP_rating_mistrust) {
  1261             PEP_color kl_color = PEP_rating_undefined;
  1262             
  1263             if (_keylist)
  1264                 kl_color = keylist_color(session, _keylist);
  1265             
  1266             if (kl_color <= PEP_rating_mistrust) {
  1267                 *color = kl_color;
  1268             }
  1269             else if (*color >= PEP_rating_reliable &&
  1270                      kl_color < PEP_rating_reliable) {
  1271                 *color = PEP_rating_unreliable;
  1272             }
  1273             else if (*color >= PEP_rating_reliable &&
  1274                      kl_color >= PEP_rating_reliable) {
  1275                 if (!(src->from && src->from->user_id && src->from->user_id[0])) {
  1276                     *color = PEP_rating_unreliable;
  1277                 }
  1278                 else {
  1279                     char *fpr = _keylist->value;
  1280                     pEp_identity *_from = new_identity(src->from->address, fpr,
  1281                                                        src->from->user_id, src->from->username);
  1282                     if (_from == NULL)
  1283                         goto enomem;
  1284                     status = update_identity(session, _from);
  1285                     if (_from->comm_type != PEP_ct_unknown)
  1286                         *color = _rating(_from->comm_type);
  1287                     free_identity(_from);
  1288                     if (status != PEP_STATUS_OK)
  1289                         goto pep_error;
  1290                 }
  1291             }
  1292         }
  1293     }
  1294 
  1295     if (msg)
  1296         decorate_message(msg, *color, _keylist);
  1297 
  1298     *dst = msg;
  1299     *keylist = _keylist;
  1300 
  1301     return PEP_STATUS_OK;
  1302 
  1303 enomem:
  1304     status = PEP_OUT_OF_MEMORY;
  1305 
  1306 pep_error:
  1307     free_message(msg);
  1308     free_stringlist(_keylist);
  1309 
  1310     return status;
  1311 }
  1312 
  1313 DYNAMIC_API PEP_STATUS outgoing_message_color(
  1314         PEP_SESSION session,
  1315         message *msg,
  1316         PEP_color *color
  1317     )
  1318 {
  1319     PEP_STATUS status = PEP_STATUS_OK;
  1320     PEP_comm_type max_comm_type = PEP_ct_pEp;
  1321     bool comm_type_determined = false;
  1322     identity_list * il;
  1323 
  1324     assert(session);
  1325     assert(msg);
  1326     assert(msg->from);
  1327     assert(msg->dir == PEP_dir_outgoing);
  1328     assert(color);
  1329 
  1330     if (!(session && msg && color))
  1331         return PEP_ILLEGAL_VALUE;
  1332 
  1333     if (msg->from == NULL || msg->dir != PEP_dir_outgoing)
  1334         return PEP_ILLEGAL_VALUE;
  1335 
  1336     *color = PEP_rating_undefined;
  1337 
  1338     status = myself(session, msg->from);
  1339     if (status != PEP_STATUS_OK)
  1340         return status;
  1341 
  1342     for (il = msg->to; il != NULL; il = il->next) {
  1343         if (il->ident) {
  1344             update_identity(session, il->ident);
  1345             max_comm_type = _get_comm_type(session, max_comm_type,
  1346                     il->ident);
  1347             comm_type_determined = true;
  1348         }
  1349     }
  1350 
  1351     for (il = msg->cc; il != NULL; il = il->next) {
  1352         if (il->ident) {
  1353             update_identity(session, il->ident);
  1354             max_comm_type = _get_comm_type(session, max_comm_type,
  1355                     il->ident);
  1356             comm_type_determined = true;
  1357         }
  1358     }
  1359 
  1360     if (comm_type_determined == false)
  1361         *color = PEP_rating_undefined;
  1362     else
  1363         *color = MAX(_rating(max_comm_type), PEP_rating_unencrypted);
  1364 
  1365     return PEP_STATUS_OK;
  1366 }
  1367 
  1368 DYNAMIC_API PEP_STATUS identity_color(
  1369         PEP_SESSION session,
  1370         pEp_identity *ident,
  1371         PEP_color *color
  1372     )
  1373 {
  1374     PEP_STATUS status = PEP_STATUS_OK;
  1375 
  1376     assert(session);
  1377     assert(ident);
  1378     assert(color);
  1379 
  1380     if (!(session && ident && color))
  1381         return PEP_ILLEGAL_VALUE;
  1382 
  1383     if (ident->me)
  1384         status = myself(session, ident);
  1385     else
  1386         status = update_identity(session, ident);
  1387 
  1388     if (status == PEP_STATUS_OK)
  1389         *color = _rating(ident->comm_type);
  1390 
  1391     return status;
  1392 }
  1393