...
authorvb
Sun, 15 Mar 2015 21:07:12 +0100
changeset 117d2ff99ccdf30
parent 116 e4d286e7b300
child 118 217e8ebe524a
...
src/message.h
src/message_api.c
src/message_api.h
src/mime.c
src/mime.h
     1.1 --- a/src/message.h	Sun Mar 15 19:39:07 2015 +0100
     1.2 +++ b/src/message.h	Sun Mar 15 21:07:12 2015 +0100
     1.3 @@ -108,13 +108,13 @@
     1.4  // free_message() - free message struct
     1.5  //
     1.6  //  parameters:
     1.7 -//      src (in)        message struct to free
     1.8 +//      msg (in)        message struct to free
     1.9  //
    1.10  //  caveat:
    1.11  //      raw data as well as referenced other messages aren't freed and remain
    1.12  //      in the ownership of the caller
    1.13  
    1.14 -DYNAMIC_API void free_message(message *src);
    1.15 +DYNAMIC_API void free_message(message *msg);
    1.16  
    1.17  
    1.18  // message_dup - duplicate message (deep copy)
     2.1 --- a/src/message_api.c	Sun Mar 15 19:39:07 2015 +0100
     2.2 +++ b/src/message_api.c	Sun Mar 15 21:07:12 2015 +0100
     2.3 @@ -554,24 +554,6 @@
     2.4  
     2.5      *dst = NULL;
     2.6   
     2.7 -    if (src->enc_format < PEP_enc_pieces) {
     2.8 -        assert(0); // message is not encrypted
     2.9 -        if (mime == src->mime) {
    2.10 -            msg = message_dup(src);
    2.11 -            if (msg == NULL)
    2.12 -                goto enomem;
    2.13 -            *dst = msg;
    2.14 -            return PEP_STATUS_OK;
    2.15 -        }
    2.16 -        else {
    2.17 -            // TODO: we don't re-encode yet
    2.18 -            NOT_IMPLEMENTED
    2.19 -        }
    2.20 -    }
    2.21 -
    2.22 -    // src message is encrypted
    2.23 -    assert(src->enc_format >= PEP_enc_pieces);
    2.24 -
    2.25      if (src->mime == PEP_MIME_fields_omitted || src->mime == PEP_MIME) {
    2.26          message *_src = NULL;
    2.27          status = mime_decode_message(src->longmsg, &_src);
    2.28 @@ -590,9 +572,14 @@
    2.29          free_src = true;
    2.30      }
    2.31  
    2.32 -    // src message is not MIME encoded but still encrypted
    2.33 +    // src message is not MIME encoded
    2.34      assert(src->mime == PEP_MIME_none);
    2.35  
    2.36 +    if (!is_PGP_message_text(src->longmsg)) {
    2.37 +        status = PEP_UNENCRYPTED;
    2.38 +        goto pep_error;
    2.39 +    }
    2.40 +
    2.41      ctext = src->longmsg;
    2.42      csize = strlen(src->longmsg);
    2.43  
     3.1 --- a/src/message_api.h	Sun Mar 15 19:39:07 2015 +0100
     3.2 +++ b/src/message_api.h	Sun Mar 15 21:07:12 2015 +0100
     3.3 @@ -29,6 +29,8 @@
     3.4  //
     3.5  //	caveat:
     3.6  //	    the ownership of the new message goes to the caller
     3.7 +//	    if src is unencrypted this function returns PEP_UNENCRYPTED and sets
     3.8 +//	    dst to NULL
     3.9  
    3.10  DYNAMIC_API PEP_STATUS encrypt_message(
    3.11          PEP_SESSION session,
     4.1 --- a/src/mime.c	Sun Mar 15 19:39:07 2015 +0100
     4.2 +++ b/src/mime.c	Sun Mar 15 21:07:12 2015 +0100
     4.3 @@ -11,6 +11,15 @@
     4.4  
     4.5  #define NOT_IMPLEMENTED assert(0);
     4.6  
     4.7 +DYNAMIC_API bool is_PGP_message_text(const char *text)
     4.8 +{
     4.9 +    assert(text);
    4.10 +    if (text == NULL)
    4.11 +        return false;
    4.12 +
    4.13 +    return strncmp(text, "-----BEGIN PGP MESSAGE-----", 27) == 0;
    4.14 +}
    4.15 +
    4.16  static PEP_STATUS render_mime(struct mailmime *mime, char **mimetext)
    4.17  {
    4.18      PEP_STATUS status = PEP_STATUS_OK;
    4.19 @@ -585,15 +594,6 @@
    4.20      return status;
    4.21  }
    4.22  
    4.23 -bool is_PGP_message(const char *text)
    4.24 -{
    4.25 -    assert(text);
    4.26 -    if (text == NULL)
    4.27 -        return false;
    4.28 -
    4.29 -    return strncmp(text, "-----BEGIN PGP MESSAGE-----", 27) == 0;
    4.30 -}
    4.31 -
    4.32  static PEP_STATUS mime_encode_message_plain(
    4.33          const message *msg,
    4.34          bool omit_fields,
    4.35 @@ -621,7 +621,7 @@
    4.36              goto pep_error;
    4.37      }
    4.38      else {
    4.39 -        if (is_PGP_message(plaintext))
    4.40 +        if (is_PGP_message_text(plaintext))
    4.41              mime = get_text_part("msg.asc", "application/octet-stream", plaintext,
    4.42                      strlen(plaintext), MAILMIME_MECHANISM_7BIT);
    4.43          else
    4.44 @@ -1185,30 +1185,45 @@
    4.45      return status;
    4.46  }
    4.47  
    4.48 -static PEP_STATUS interpret_PGP_MIME(struct mailmime *mime, message **msg)
    4.49 +static PEP_STATUS interpret_PGP_MIME(struct mailmime *mime, message *msg)
    4.50  {
    4.51 -    PEP_STATUS status = PEP_STATUS_OK;
    4.52 -    message *_msg = NULL;
    4.53 -
    4.54      assert(mime);
    4.55      assert(msg);
    4.56  
    4.57 -    *msg = NULL;
    4.58 +    clist *partlist = mime->mm_data.mm_multipart.mm_mp_list;
    4.59 +    if (partlist == NULL)
    4.60 +        return PEP_ILLEGAL_VALUE;
    4.61  
    4.62 -    _msg = calloc(1, sizeof(message));
    4.63 -    assert(_msg);
    4.64 -    if (_msg == NULL)
    4.65 -        goto enomem;
    4.66 +    clistiter *cur = clist_begin(partlist);
    4.67 +    if (cur == NULL)
    4.68 +        return PEP_ILLEGAL_VALUE;
    4.69  
    4.70 -    *msg = _msg;
    4.71 +    cur = clist_next(cur);
    4.72 +    if (cur == NULL)
    4.73 +        return PEP_ILLEGAL_VALUE;
    4.74 +
    4.75 +    struct mailmime *second = clist_content(cur);
    4.76 +    if (second == NULL)
    4.77 +        return PEP_ILLEGAL_VALUE;
    4.78 +
    4.79 +    if (second->mm_body == NULL)
    4.80 +        return PEP_ILLEGAL_VALUE;
    4.81 +
    4.82 +    const char *text = second->mm_body->dt_data.dt_text.dt_data;
    4.83 +    if (text == NULL)
    4.84 +        return PEP_ILLEGAL_VALUE;
    4.85 +
    4.86 +    char *_text = strdup(text);
    4.87 +    if (_text == NULL)
    4.88 +        return PEP_OUT_OF_MEMORY;
    4.89 +
    4.90 +    msg->mime = PEP_MIME_fields_omitted;
    4.91 +    msg->enc_format = PEP_enc_PGP_MIME;
    4.92 +
    4.93 +    free(msg->longmsg);
    4.94 +    msg->longmsg = _text;
    4.95 +
    4.96      return PEP_STATUS_OK;
    4.97 -
    4.98 -enomem:
    4.99 -    status = PEP_OUT_OF_MEMORY;
   4.100 -
   4.101 -pep_error:
   4.102 -    free_message(msg);
   4.103 -    return status;
   4.104  }
   4.105  
   4.106  static bool parameter_has_value(
   4.107 @@ -1219,10 +1234,15 @@
   4.108  {
   4.109      clistiter *cur;
   4.110  
   4.111 +    assert(list);
   4.112 +    assert(name);
   4.113 +    assert(value);
   4.114 +
   4.115      for (cur = clist_begin(list); cur != NULL ; cur = clist_next(cur)) {
   4.116          struct mailmime_parameter * param = clist_content(cur);
   4.117 -        if (strcmp(name, param->pa_name) == 0 &&
   4.118 -                strcmp(value, param->pa_value) == 0)
   4.119 +        if (param &&
   4.120 +                param->pa_name && strcmp(name, param->pa_name) == 0 &&
   4.121 +                param->pa_value && strcmp(value, param->pa_value) == 0)
   4.122              return true;
   4.123      }
   4.124  
   4.125 @@ -1256,31 +1276,43 @@
   4.126              goto err_mime;
   4.127      }
   4.128  
   4.129 -    struct mailmime_content *content =
   4.130 -            mime->mm_data.mm_message.mm_msg_mime->mm_content_type;
   4.131 +    _msg = calloc(1, sizeof(message));
   4.132 +    assert(_msg);
   4.133 +    if (_msg == NULL)
   4.134 +        goto enomem;
   4.135  
   4.136 -    if (content->ct_type->tp_type == MAILMIME_TYPE_COMPOSITE_TYPE
   4.137 -            && content->ct_type->tp_data.tp_composite_type->ct_type ==
   4.138 -                    MAILMIME_COMPOSITE_TYPE_MULTIPART
   4.139 -            && strcmp(content->ct_subtype, "encrypted") == 0
   4.140 -            && parameter_has_value(content->ct_parameters, "protocol",
   4.141 -                    "application/pgp-encrypted")) {
   4.142 -
   4.143 -        status = interpret_PGP_MIME(mime, &_msg);
   4.144 +    if (mime->mm_data.mm_message.mm_fields
   4.145 +            && mime->mm_data.mm_message.mm_fields->fld_list) {
   4.146 +        clist * _fieldlist = mime->mm_data.mm_message.mm_fields->fld_list;
   4.147 +        status = read_fields(_msg, _fieldlist);
   4.148          if (status != PEP_STATUS_OK)
   4.149              goto pep_error;
   4.150      }
   4.151 -    else {
   4.152  
   4.153 +    if (mime->mm_data.mm_message.mm_msg_mime) {
   4.154 +        struct mailmime_content *content =
   4.155 +                mime->mm_data.mm_message.mm_msg_mime->mm_content_type;
   4.156 +
   4.157 +        if (content->ct_type &&
   4.158 +                content->ct_type->tp_type == MAILMIME_TYPE_COMPOSITE_TYPE
   4.159 +                && content->ct_type->tp_data.tp_composite_type
   4.160 +                && content->ct_type->tp_data.tp_composite_type->ct_type ==
   4.161 +                        MAILMIME_COMPOSITE_TYPE_MULTIPART
   4.162 +                && content->ct_subtype
   4.163 +                && strcmp(content->ct_subtype, "encrypted") == 0
   4.164 +                && content->ct_parameters
   4.165 +                && parameter_has_value(content->ct_parameters, "protocol",
   4.166 +                        "application/pgp-encrypted")) {
   4.167 +
   4.168 +            status = interpret_PGP_MIME(mime->mm_data.mm_message.mm_msg_mime,
   4.169 +                    _msg);
   4.170 +            if (status != PEP_STATUS_OK)
   4.171 +                goto pep_error;
   4.172 +        }
   4.173 +        else {
   4.174 +        }
   4.175      }
   4.176  
   4.177 -    assert(_msg);
   4.178 -
   4.179 -    clist * _fieldlist = mime->mm_data.mm_message.mm_fields->fld_list;
   4.180 -    status = read_fields(_msg, _fieldlist);
   4.181 -    if (status != PEP_STATUS_OK)
   4.182 -        goto pep_error;
   4.183 -
   4.184      mailmime_free(mime);
   4.185      *msg = _msg;
   4.186  
     5.1 --- a/src/mime.h	Sun Mar 15 19:39:07 2015 +0100
     5.2 +++ b/src/mime.h	Sun Mar 15 21:07:12 2015 +0100
     5.3 @@ -7,6 +7,17 @@
     5.4  #endif
     5.5  
     5.6  
     5.7 +// is_PGP_message_text() - determine if text encodes a PGP message
     5.8 +//
     5.9 +//  parameters:
    5.10 +//      text (in)               text to examine
    5.11 +//
    5.12 +//  return value:
    5.13 +//      true if text is a PGP message, false otherwise
    5.14 +
    5.15 +DYNAMIC_API bool is_PGP_message_text(const char *text);
    5.16 +
    5.17 +
    5.18  // mime_encode_message() - encode a MIME message
    5.19  //
    5.20  //  parameters: