merging sync
authorVolker Birk <vb@pep-project.org>
Mon, 06 May 2019 15:43:53 +0200
branchsync
changeset 363400cfa9a46b3c
parent 3633 6f3310e6a219
parent 3631 f7f57f9a5e01
child 3635 137abddfa112
merging
     1.1 --- a/src/Makefile	Mon May 06 15:42:50 2019 +0200
     1.2 +++ b/src/Makefile	Mon May 06 15:43:53 2019 +0200
     1.3 @@ -103,7 +103,7 @@
     1.4  	   timestamp.h identity_list.h bloblist.h stringpair.h message.h mime.h \
     1.5  	   cryptotech.h sync_api.h blacklist.h pEp_string.h openpgp_compat.h \
     1.6  	   labeled_int_list.h key_reset.h base64.h sync_codec.h status_to_string.h\
     1.7 -	   ../asn.1/*.h $(PREFIX)/include/pEp/
     1.8 +	   aux_mime_msg.h ../asn.1/*.h $(PREFIX)/include/pEp/
     1.9  
    1.10  install: $(TARGET) install_headers
    1.11  	mkdir -p "$(PREFIX)/lib/"
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/aux_mime_msg.c	Mon May 06 15:43:53 2019 +0200
     2.3 @@ -0,0 +1,272 @@
     2.4 +// This file is under GNU General Public License 3.0
     2.5 +// see LICENSE.txt
     2.6 +
     2.7 +#ifdef ENIGMAIL_MAY_USE_THIS
     2.8 +
     2.9 +#include "pEp_internal.h"
    2.10 +#include "message_api.h"
    2.11 +#include "mime.h"
    2.12 +
    2.13 +#include <assert.h>
    2.14 +#include <string.h>
    2.15 +#include <stdlib.h>
    2.16 +
    2.17 +#include "aux_mime_msg.h"
    2.18 +
    2.19 +
    2.20 +static PEP_STATUS update_identity_recip_list(PEP_SESSION session,
    2.21 +                                             identity_list* list) {
    2.22 +
    2.23 +    PEP_STATUS status = PEP_STATUS_OK;
    2.24 +
    2.25 +    if (!session)
    2.26 +        return PEP_UNKNOWN_ERROR;
    2.27 +    
    2.28 +    identity_list* id_list_ptr = NULL;
    2.29 +        
    2.30 +    for (id_list_ptr = list; id_list_ptr; id_list_ptr = id_list_ptr->next) {
    2.31 +        pEp_identity* curr_identity = id_list_ptr->ident;
    2.32 +        if (curr_identity) {
    2.33 +            if (!is_me(session, curr_identity)) {
    2.34 +                char* name_bak = curr_identity->username;
    2.35 +                curr_identity->username = NULL;
    2.36 +                status = update_identity(session, curr_identity);
    2.37 +                if (name_bak && 
    2.38 +                    (EMPTYSTR(curr_identity->username) || strcmp(name_bak, curr_identity->username) != 0)) {
    2.39 +                    free(curr_identity->username);
    2.40 +                    curr_identity->username = name_bak;
    2.41 +                }                        
    2.42 +            }
    2.43 +            else
    2.44 +                status = _myself(session, curr_identity, false, false, true);
    2.45 +        if (status == PEP_ILLEGAL_VALUE || status == PEP_OUT_OF_MEMORY)
    2.46 +            return status;
    2.47 +        }
    2.48 +    }
    2.49 +    
    2.50 +    return PEP_STATUS_OK;                                  
    2.51 +}
    2.52 +
    2.53 +DYNAMIC_API PEP_STATUS MIME_decrypt_message(
    2.54 +    PEP_SESSION session,
    2.55 +    const char *mimetext,
    2.56 +    size_t size,
    2.57 +    char** mime_plaintext,
    2.58 +    stringlist_t **keylist,
    2.59 +    PEP_rating *rating,
    2.60 +    PEP_decrypt_flags_t *flags,
    2.61 +    char** modified_src
    2.62 +)
    2.63 +{
    2.64 +    assert(mimetext);
    2.65 +    assert(mime_plaintext);
    2.66 +    assert(keylist);
    2.67 +    assert(rating);
    2.68 +    assert(flags);
    2.69 +    assert(modified_src);
    2.70 +
    2.71 +    if (!(mimetext && mime_plaintext && keylist && rating && flags && modified_src))
    2.72 +        return PEP_ILLEGAL_VALUE;
    2.73 +        
    2.74 +    PEP_STATUS status = PEP_STATUS_OK;
    2.75 +    message* tmp_msg = NULL;
    2.76 +    message* dec_msg = NULL;
    2.77 +    *mime_plaintext = NULL;
    2.78 +
    2.79 +    status = mime_decode_message(mimetext, size, &tmp_msg);
    2.80 +    if (status != PEP_STATUS_OK)
    2.81 +        goto pEp_error;
    2.82 +
    2.83 +    tmp_msg->dir = PEP_dir_incoming;
    2.84 +    // MIME decode message delivers only addresses. We need more.
    2.85 +    if (tmp_msg->from) {
    2.86 +        if (!is_me(session, tmp_msg->from))
    2.87 +            status = update_identity(session, (tmp_msg->from));
    2.88 +        else
    2.89 +            status = _myself(session, tmp_msg->from, false, false, true);
    2.90 +
    2.91 +        if (status == PEP_ILLEGAL_VALUE || status == PEP_OUT_OF_MEMORY)
    2.92 +            goto pEp_error;
    2.93 +    }
    2.94 +
    2.95 +    status = update_identity_recip_list(session, tmp_msg->to);
    2.96 +    if (status != PEP_STATUS_OK)
    2.97 +        goto pEp_error;
    2.98 +
    2.99 +    status = update_identity_recip_list(session, tmp_msg->cc);
   2.100 +    if (status != PEP_STATUS_OK)
   2.101 +        goto pEp_error;
   2.102 +
   2.103 +    status = update_identity_recip_list(session, tmp_msg->bcc);
   2.104 +    if (status != PEP_STATUS_OK)
   2.105 +        goto pEp_error;
   2.106 +
   2.107 +    PEP_STATUS decrypt_status = decrypt_message(session,
   2.108 +                                                tmp_msg,
   2.109 +                                                &dec_msg,
   2.110 +                                                keylist,
   2.111 +                                                rating,
   2.112 +                                                flags);
   2.113 +
   2.114 +
   2.115 +    if (!dec_msg && (decrypt_status == PEP_UNENCRYPTED || decrypt_status == PEP_VERIFIED)) {
   2.116 +        dec_msg = message_dup(tmp_msg);
   2.117 +    }
   2.118 +    
   2.119 +    if (decrypt_status > PEP_CANNOT_DECRYPT_UNKNOWN || !dec_msg)
   2.120 +    {
   2.121 +        status = decrypt_status;
   2.122 +        goto pEp_error;
   2.123 +    }
   2.124 +
   2.125 +    if (*flags & PEP_decrypt_flag_src_modified) {
   2.126 +        _mime_encode_message_internal(tmp_msg, false, modified_src, true);
   2.127 +        if (!modified_src) {
   2.128 +            *flags &= (~PEP_decrypt_flag_src_modified);
   2.129 +            decrypt_status = PEP_CANNOT_REENCRYPT; // Because we couldn't return it, I guess.
   2.130 +        }
   2.131 +    }
   2.132 +
   2.133 +    // FIXME: test with att
   2.134 +    status = _mime_encode_message_internal(dec_msg, false, mime_plaintext, true);
   2.135 +
   2.136 +    if (status == PEP_STATUS_OK)
   2.137 +    {
   2.138 +        free(tmp_msg);
   2.139 +        free(dec_msg);
   2.140 +        return decrypt_status;
   2.141 +    }
   2.142 +    
   2.143 +pEp_error:
   2.144 +    free_message(tmp_msg);
   2.145 +    free_message(dec_msg);
   2.146 +
   2.147 +    return status;
   2.148 +}
   2.149 +
   2.150 +
   2.151 +DYNAMIC_API PEP_STATUS MIME_encrypt_message(
   2.152 +    PEP_SESSION session,
   2.153 +    const char *mimetext,
   2.154 +    size_t size,
   2.155 +    stringlist_t* extra,
   2.156 +    char** mime_ciphertext,
   2.157 +    PEP_enc_format enc_format,
   2.158 +    PEP_encrypt_flags_t flags
   2.159 +)
   2.160 +{
   2.161 +    PEP_STATUS status = PEP_STATUS_OK;
   2.162 +    message* tmp_msg = NULL;
   2.163 +    message* enc_msg = NULL;
   2.164 +
   2.165 +    status = mime_decode_message(mimetext, size, &tmp_msg);
   2.166 +    if (status != PEP_STATUS_OK)
   2.167 +        goto pEp_error;
   2.168 +
   2.169 +    // MIME decode message delivers only addresses. We need more.
   2.170 +    if (tmp_msg->from) {
   2.171 +        char* own_id = NULL;
   2.172 +        status = get_default_own_userid(session, &own_id);
   2.173 +        free(tmp_msg->from->user_id);
   2.174 +        
   2.175 +        if (status != PEP_STATUS_OK || !own_id) {
   2.176 +            tmp_msg->from->user_id = strdup(PEP_OWN_USERID);
   2.177 +        }
   2.178 +        else {
   2.179 +            tmp_msg->from->user_id = own_id; // ownership transfer
   2.180 +        }
   2.181 +            
   2.182 +        status = myself(session, tmp_msg->from);
   2.183 +        if (status != PEP_STATUS_OK)
   2.184 +            goto pEp_error;
   2.185 +    }
   2.186 +    
   2.187 +    // Own identities can be retrieved here where they would otherwise
   2.188 +    // fail because we lack all other information. This is ok and even
   2.189 +    // desired. FIXME: IS it?
   2.190 +    status = update_identity_recip_list(session, tmp_msg->to);
   2.191 +    if (status != PEP_STATUS_OK)
   2.192 +        goto pEp_error;
   2.193 +    
   2.194 +    status = update_identity_recip_list(session, tmp_msg->cc);
   2.195 +    if (status != PEP_STATUS_OK)
   2.196 +        goto pEp_error;
   2.197 +    
   2.198 +    status = update_identity_recip_list(session, tmp_msg->bcc);
   2.199 +    if (status != PEP_STATUS_OK)
   2.200 +        goto pEp_error;
   2.201 +    
   2.202 +    // This isn't incoming, though... so we need to reverse the direction
   2.203 +    tmp_msg->dir = PEP_dir_outgoing;
   2.204 +    status = encrypt_message(session,
   2.205 +                             tmp_msg,
   2.206 +                             extra,
   2.207 +                             &enc_msg,
   2.208 +                             enc_format,
   2.209 +                             flags);
   2.210 +                             
   2.211 +    if (status != PEP_STATUS_OK)
   2.212 +        goto pEp_error;
   2.213 +
   2.214 +
   2.215 +    if (!enc_msg) {
   2.216 +        status = PEP_UNKNOWN_ERROR;
   2.217 +        goto pEp_error;
   2.218 +    }
   2.219 +
   2.220 +    status = _mime_encode_message_internal(enc_msg, false, mime_ciphertext, false);
   2.221 +
   2.222 +pEp_error:
   2.223 +    free_message(tmp_msg);
   2.224 +    free_message(enc_msg);
   2.225 +
   2.226 +    return status;
   2.227 +
   2.228 +}
   2.229 +
   2.230 +DYNAMIC_API PEP_STATUS MIME_encrypt_message_for_self(
   2.231 +    PEP_SESSION session,
   2.232 +    pEp_identity* target_id,
   2.233 +    const char *mimetext,
   2.234 +    size_t size,
   2.235 +    stringlist_t* extra,
   2.236 +    char** mime_ciphertext,
   2.237 +    PEP_enc_format enc_format,
   2.238 +    PEP_encrypt_flags_t flags
   2.239 +)
   2.240 +{
   2.241 +    PEP_STATUS status = PEP_STATUS_OK;
   2.242 +    message* tmp_msg = NULL;
   2.243 +    message* enc_msg = NULL;
   2.244 +
   2.245 +    status = mime_decode_message(mimetext, size, &tmp_msg);
   2.246 +    if (status != PEP_STATUS_OK)
   2.247 +        goto pEp_error;
   2.248 +
   2.249 +    // This isn't incoming, though... so we need to reverse the direction
   2.250 +    tmp_msg->dir = PEP_dir_outgoing;
   2.251 +    status = encrypt_message_for_self(session,
   2.252 +                                      target_id,
   2.253 +                                      tmp_msg,
   2.254 +                                      extra,
   2.255 +                                      &enc_msg,
   2.256 +                                      enc_format,
   2.257 +                                      flags);
   2.258 +    if (status != PEP_STATUS_OK)
   2.259 +        goto pEp_error;
   2.260 + 
   2.261 +    if (!enc_msg) {
   2.262 +        status = PEP_UNKNOWN_ERROR;
   2.263 +        goto pEp_error;
   2.264 +    }
   2.265 +
   2.266 +    status = mime_encode_message(enc_msg, false, mime_ciphertext);
   2.267 +
   2.268 +pEp_error:
   2.269 +    free_message(tmp_msg);
   2.270 +    free_message(enc_msg);
   2.271 +
   2.272 +    return status;
   2.273 +}
   2.274 +
   2.275 +#endif
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/aux_mime_msg.h	Mon May 06 15:43:53 2019 +0200
     3.3 @@ -0,0 +1,150 @@
     3.4 +// This file is under GNU General Public License 3.0
     3.5 +// see LICENSE.txt
     3.6 +
     3.7 +#pragma once
     3.8 +
     3.9 +#ifdef ENIGMAIL_MAY_USE_THIS
    3.10 +
    3.11 +#include "pEpEngine.h"
    3.12 +#include "keymanagement.h"
    3.13 +#include "message.h"
    3.14 +#include "cryptotech.h"
    3.15 +
    3.16 +#ifdef __cplusplus
    3.17 +extern "C" {
    3.18 +#endif
    3.19 +
    3.20 +// MIME_encrypt_message() - encrypt a MIME message, with MIME output
    3.21 +//
    3.22 +//  parameters:
    3.23 +//      session (in)            session handle
    3.24 +//      mimetext (in)           MIME encoded text to encrypt
    3.25 +//      size (in)               size of input mime text
    3.26 +//      extra (in)              extra keys for encryption
    3.27 +//      mime_ciphertext (out)   encrypted, encoded message
    3.28 +//      enc_format (in)         encrypted format
    3.29 +//      flags (in)              flags to set special encryption features
    3.30 +//
    3.31 +//  return value:
    3.32 +//      PEP_STATUS_OK           if everything worked
    3.33 +//      PEP_BUFFER_TOO_SMALL    if encoded message size is too big to handle
    3.34 +//      PEP_CANNOT_CREATE_TEMP_FILE
    3.35 +//                              if there are issues with temp files; in
    3.36 +//                              this case errno will contain the underlying
    3.37 +//                              error
    3.38 +//      PEP_OUT_OF_MEMORY       if not enough memory could be allocated
    3.39 +//
    3.40 +//  caveat:
    3.41 +//      the encrypted, encoded mime text will go to the ownership of the caller; mimetext
    3.42 +//      will remain in the ownership of the caller
    3.43 +DYNAMIC_API PEP_STATUS MIME_encrypt_message(
    3.44 +    PEP_SESSION session,
    3.45 +    const char *mimetext,
    3.46 +    size_t size,
    3.47 +    stringlist_t* extra,
    3.48 +    char** mime_ciphertext,
    3.49 +    PEP_enc_format enc_format,
    3.50 +    PEP_encrypt_flags_t flags
    3.51 +);
    3.52 +
    3.53 +
    3.54 +// MIME_encrypt_message_for_self() - encrypt MIME message for user's identity only,
    3.55 +//                              ignoring recipients and other identities from
    3.56 +//                              the message, with MIME output
    3.57 +//  parameters:
    3.58 +//      session (in)            session handle
    3.59 +//      target_id (in)          self identity this message should be encrypted for
    3.60 +//      mimetext (in)           MIME encoded text to encrypt
    3.61 +//      size (in)               size of input mime text
    3.62 +//      extra (in)              extra keys for encryption
    3.63 +//      mime_ciphertext (out)   encrypted, encoded message
    3.64 +//      enc_format (in)         encrypted format
    3.65 +//      flags (in)              flags to set special encryption features
    3.66 +//
    3.67 +//  return value:
    3.68 +//      PEP_STATUS_OK           if everything worked
    3.69 +//      PEP_BUFFER_TOO_SMALL    if encoded message size is too big to handle
    3.70 +//      PEP_CANNOT_CREATE_TEMP_FILE
    3.71 +//                              if there are issues with temp files; in
    3.72 +//                              this case errno will contain the underlying
    3.73 +//                              error
    3.74 +//      PEP_OUT_OF_MEMORY       if not enough memory could be allocated
    3.75 +//
    3.76 +//  caveat:
    3.77 +//      the encrypted, encoded mime text will go to the ownership of the caller; mimetext
    3.78 +//      will remain in the ownership of the caller
    3.79 +DYNAMIC_API PEP_STATUS MIME_encrypt_message_for_self(
    3.80 +    PEP_SESSION session,
    3.81 +    pEp_identity* target_id,
    3.82 +    const char *mimetext,
    3.83 +    size_t size,
    3.84 +    stringlist_t* extra,
    3.85 +    char** mime_ciphertext,
    3.86 +    PEP_enc_format enc_format,
    3.87 +    PEP_encrypt_flags_t flags
    3.88 +);
    3.89 +
    3.90 +
    3.91 +
    3.92 +// MIME_decrypt_message() - decrypt a MIME message, with MIME output
    3.93 +//
    3.94 +//  parameters:
    3.95 +//      session (in)            session handle
    3.96 +//      mimetext (in)           MIME encoded text to decrypt
    3.97 +//      size (in)               size of mime text to decode (in order to decrypt)
    3.98 +//      mime_plaintext (out)    decrypted, encoded message
    3.99 +//      keylist (inout)         in: stringlist with additional keyids for reencryption if needed
   3.100 +//                                  (will be freed and replaced with output keylist)
   3.101 +//                              out: stringlist with keyids
   3.102 +//      rating (out)            rating for the message
   3.103 +//      flags (inout)           flags to signal special decryption features (see below)
   3.104 +//      modified_src (out)      modified source string, if decrypt had reason to change it
   3.105 +//
   3.106 +//  return value:
   3.107 +//      decrypt status          if everything worked with MIME encode/decode, 
   3.108 +//                              the status of the decryption is returned 
   3.109 +//                              (PEP_STATUS_OK or decryption error status)
   3.110 +//      PEP_BUFFER_TOO_SMALL    if encoded message size is too big to handle
   3.111 +//      PEP_CANNOT_CREATE_TEMP_FILE
   3.112 +//                              if there are issues with temp files; in
   3.113 +//                              this case errno will contain the underlying
   3.114 +//                              error
   3.115 +//      PEP_OUT_OF_MEMORY       if not enough memory could be allocated
   3.116 +//
   3.117 +//  flag values:
   3.118 +//      in:
   3.119 +//          PEP_decrypt_flag_untrusted_server
   3.120 +//              used to signal that decrypt function should engage in behaviour
   3.121 +//              specified for when the server storing the source is untrusted.
   3.122 +//      out:
   3.123 +//          PEP_decrypt_flag_own_private_key
   3.124 +//              private key was imported for one of our addresses (NOT trusted
   3.125 +//              or set to be used - handshake/trust is required for that)
   3.126 +//          PEP_decrypt_flag_src_modified
   3.127 +//              indicates that the modified_src field should contain a modified
   3.128 +//              version of the source, at the moment always as a result of the
   3.129 +//              input flags. 
   3.130 +//          PEP_decrypt_flag_consume
   3.131 +//              used by sync 
   3.132 +//          PEP_decrypt_flag_ignore
   3.133 +//              used by sync 
   3.134 +// 
   3.135 +//  caveat:
   3.136 +//      the decrypted, encoded mime text will go to the ownership of the caller; mimetext
   3.137 +//      will remain in the ownership of the caller
   3.138 +DYNAMIC_API PEP_STATUS MIME_decrypt_message(
   3.139 +    PEP_SESSION session,
   3.140 +    const char *mimetext,
   3.141 +    size_t size,
   3.142 +    char** mime_plaintext,
   3.143 +    stringlist_t **keylist,
   3.144 +    PEP_rating *rating,
   3.145 +    PEP_decrypt_flags_t *flags,
   3.146 +    char** modified_src
   3.147 +);
   3.148 +
   3.149 +#ifdef __cplusplus
   3.150 +}
   3.151 +#endif
   3.152 +
   3.153 +#endif
     4.1 --- a/test/include/test_util.h	Mon May 06 15:42:50 2019 +0200
     4.2 +++ b/test/include/test_util.h	Mon May 06 15:43:53 2019 +0200
     4.3 @@ -8,6 +8,7 @@
     4.4  
     4.5  #include "pEpEngine.h"
     4.6  #include "message_api.h"
     4.7 +#include "aux_mime_msg.h"
     4.8  
     4.9  void test_init();
    4.10  
    4.11 @@ -58,6 +59,8 @@
    4.12                           int ftw_info, 
    4.13                           struct FTW * ftw_struct);
    4.14                           
    4.15 +#ifndef ENIGMAIL_MAY_USE_THIS
    4.16 +
    4.17  // MIME_decrypt_message() - decrypt a MIME message, with MIME output
    4.18  //
    4.19  //  parameters:
    4.20 @@ -185,7 +188,7 @@
    4.21      PEP_encrypt_flags_t flags
    4.22  );
    4.23  
    4.24 -
    4.25 +#endif
    4.26  
    4.27  
    4.28          
     5.1 --- a/test/src/util/test_util.cc	Mon May 06 15:42:50 2019 +0200
     5.2 +++ b/test/src/util/test_util.cc	Mon May 06 15:43:53 2019 +0200
     5.3 @@ -432,6 +432,8 @@
     5.4      return retval;
     5.5  }
     5.6  
     5.7 +#ifndef ENIGMAIL_MAY_USE_THIS
     5.8 +
     5.9  static PEP_STATUS update_identity_recip_list(PEP_SESSION session,
    5.10                                        identity_list* list) {
    5.11  
    5.12 @@ -687,3 +689,5 @@
    5.13  
    5.14      return status;
    5.15  }
    5.16 +
    5.17 +#endif