import attached keys automatically in Message API
authorVolker Birk <vb@pep-project.org>
Sun, 03 May 2015 16:40:28 +0200
changeset 2358b7468ca8034
parent 234 6904a5742e86
child 236 154a7077dbb7
child 243 c38813173fc7
import attached keys automatically in Message API
src/bloblist.c
src/message_api.c
src/message_api.h
     1.1 --- a/src/bloblist.c	Fri May 01 11:37:38 2015 +0200
     1.2 +++ b/src/bloblist.c	Sun May 03 16:40:28 2015 +0200
     1.3 @@ -31,6 +31,8 @@
     1.4          }
     1.5      }
     1.6  
     1.7 +    assert((blob == NULL && size == 0) || (blob && size));
     1.8 +
     1.9      bloblist->data = blob;
    1.10      bloblist->size = size;
    1.11  
    1.12 @@ -96,8 +98,12 @@
    1.13                  return NULL;
    1.14              }
    1.15          }
    1.16 +
    1.17 +        assert((blob == NULL && size == 0) || (blob && size));
    1.18 +
    1.19          bloblist->data = blob;
    1.20          bloblist->size = size;
    1.21 +
    1.22          return bloblist;
    1.23      }
    1.24  
     2.1 --- a/src/message_api.c	Fri May 01 11:37:38 2015 +0200
     2.2 +++ b/src/message_api.c	Sun May 03 16:40:28 2015 +0200
     2.3 @@ -12,6 +12,70 @@
     2.4  #define MIN(A, B) ((B) > (A) ? (A) : (B))
     2.5  #endif
     2.6  
     2.7 +
     2.8 +static bool string_equality(const char *s1, const char *s2)
     2.9 +{
    2.10 +    if (s1 == NULL || s2 == NULL)
    2.11 +        return false;
    2.12 +
    2.13 +    assert(s1 && s2);
    2.14 +
    2.15 +    return strcmp(s1, s2) == 0;
    2.16 +}
    2.17 +
    2.18 +static bool is_mime_type(const bloblist_t *bl, const char *mt)
    2.19 +{
    2.20 +    assert(mt);
    2.21 +
    2.22 +    return bl && string_equality(bl->mime_type, mt);
    2.23 +}
    2.24 +
    2.25 +static bool is_fileending(const bloblist_t *bl, const char *fe)
    2.26 +{
    2.27 +    assert(fe);
    2.28 +
    2.29 +    if (bl == NULL || bl->filename == NULL)
    2.30 +        return false;
    2.31 +
    2.32 +    assert(bl && bl->filename);
    2.33 +
    2.34 +    size_t fe_len = strlen(fe);
    2.35 +    size_t fn_len = strlen(bl->filename);
    2.36 +
    2.37 +    if (fn_len <= fe_len)
    2.38 +        return false;
    2.39 +
    2.40 +    assert(fn_len > fe_len);
    2.41 +
    2.42 +    return strcmp(bl->filename + (fn_len - fe_len), fe) == 0;
    2.43 +}
    2.44 +
    2.45 +void import_attached_keys(PEP_SESSION session, const message *msg)
    2.46 +{
    2.47 +    assert(msg);
    2.48 +
    2.49 +    bloblist_t *bl;
    2.50 +    for (bl = msg->attachments; bl && bl->data; bl = bl->next) {
    2.51 +        assert(bl && bl->data && bl->size);
    2.52 +
    2.53 +        if (bl->mime_type == NULL ||
    2.54 +                    is_mime_type(bl, "application/octet-stream")) {
    2.55 +            if (is_fileending(bl, ".pgp") || is_fileending(bl, ".gpg") ||
    2.56 +                    is_fileending(bl, ".key") ||
    2.57 +                    string_equality(bl->filename, "key.asc"))
    2.58 +                import_key(session, bl->data, bl->size);
    2.59 +        }
    2.60 +        else if (is_mime_type(bl, "application/pgp-keys")) {
    2.61 +            import_key(session, bl->data, bl->size);
    2.62 +        }
    2.63 +        else if (is_mime_type(bl, "text/plain")) {
    2.64 +            if (is_fileending(bl, ".pgp") || is_fileending(bl, ".gpg") ||
    2.65 +                    is_fileending(bl, ".key") || is_fileending(bl, ".asc"))
    2.66 +                import_key(session, bl->data, bl->size);
    2.67 +        }
    2.68 +    }
    2.69 +}
    2.70 +
    2.71  static char * combine_short_and_long(const char *shortmsg, const char *longmsg)
    2.72  {
    2.73      char * ptext;
    2.74 @@ -251,6 +315,8 @@
    2.75  
    2.76      *dst = NULL;
    2.77  
    2.78 +    import_attached_keys(session, src);
    2.79 +
    2.80      if (src->enc_format >= PEP_enc_pieces) {
    2.81          if (src->enc_format == enc_format) {
    2.82              assert(0); // the message is encrypted this way already
    2.83 @@ -479,6 +545,8 @@
    2.84      if (msg->shortmsg == NULL)
    2.85          msg->shortmsg = strdup("pEp");
    2.86  
    2.87 +    import_attached_keys(session, msg);
    2.88 +
    2.89      *dst = msg;
    2.90      return PEP_STATUS_OK;
    2.91  
    2.92 @@ -572,6 +640,8 @@
    2.93  
    2.94      *dst = NULL;
    2.95   
    2.96 +    import_attached_keys(session, src);
    2.97 +
    2.98      if (src->mime == PEP_MIME_fields_omitted || src->mime == PEP_MIME) {
    2.99          message *_src = NULL;
   2.100          status = mime_decode_message(src->longmsg, &_src);
   2.101 @@ -737,6 +807,9 @@
   2.102  
   2.103      if (free_src)
   2.104          free_message(src);
   2.105 +
   2.106 +    import_attached_keys(session, msg);
   2.107 +
   2.108      *dst = msg;
   2.109      return PEP_STATUS_OK;
   2.110  
     3.1 --- a/src/message_api.h	Fri May 01 11:37:38 2015 +0200
     3.2 +++ b/src/message_api.h	Sun May 03 16:40:28 2015 +0200
     3.3 @@ -9,6 +9,9 @@
     3.4  #endif
     3.5  
     3.6  
     3.7 +void import_attached_keys(PEP_SESSION session, const message *msg);
     3.8 +
     3.9 +
    3.10  // encrypt_message() - encrypt message in memory
    3.11  //
    3.12  //  parameters: