ENGINE-262: another intermittent commit ENGINE-262
authorKrista Bennett <krista@pep-project.org>
Sat, 23 Sep 2017 16:21:49 +0200
branchENGINE-262
changeset 20931501d394b5ed
parent 2092 926e2e53967d
child 2095 5b9c06f1374e
ENGINE-262: another intermittent commit
src/message_api.c
     1.1 --- a/src/message_api.c	Thu Sep 21 22:42:24 2017 +0200
     1.2 +++ b/src/message_api.c	Sat Sep 23 16:21:49 2017 +0200
     1.3 @@ -1814,7 +1814,8 @@
     1.4  }
     1.5  
     1.6  PEP_STATUS unencapsulate_hidden_fields(message* src, message* msg) {
     1.7 -        
     1.8 +    unsigned char pepstr[] = PEP_SUBJ_STRING;
     1.9 +    
    1.10      switch (src->enc_format) {
    1.11          case PEP_enc_PGP_MIME:
    1.12          case PEP_enc_pieces:
    1.13 @@ -1995,6 +1996,29 @@
    1.14      return status;
    1.15  }
    1.16  
    1.17 +static void get_crypto_text(message* src, char** crypto_text, size_t* text_size) {
    1.18 +                                
    1.19 +    switch (src->enc_format) {
    1.20 +        case PEP_enc_PGP_MIME:
    1.21 +            *crypto_text = src->attachments->next->value;
    1.22 +            *text_size = src->attachments->next->size;
    1.23 +            break;
    1.24 +
    1.25 +        case PEP_enc_PGP_MIME_Outlook1:
    1.26 +            *crypto_text = src->attachments->value;
    1.27 +            *text_size = src->attachments->size;
    1.28 +            break;
    1.29 +
    1.30 +        case PEP_enc_pieces:
    1.31 +            *crypto_text = src->longmsg;
    1.32 +            *text_size = strlen(ctext);
    1.33 +            break;
    1.34 +
    1.35 +        default:
    1.36 +            NOT_IMPLEMENTED
    1.37 +    }
    1.38 +}
    1.39 +
    1.40  DYNAMIC_API PEP_STATUS _decrypt_message(
    1.41          PEP_SESSION session,
    1.42          message *src,
    1.43 @@ -2006,6 +2030,16 @@
    1.44      )
    1.45  {
    1.46      
    1.47 +    assert(session);
    1.48 +    assert(src);
    1.49 +    assert(dst);
    1.50 +    assert(keylist);
    1.51 +    assert(rating);
    1.52 +    assert(flags);
    1.53 +
    1.54 +    if (!(session && src && dst && keylist && rating && flags))
    1.55 +        return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
    1.56 +
    1.57      /*** Begin init ***/
    1.58      PEP_STATUS status = PEP_STATUS_OK;
    1.59      PEP_STATUS decrypt_status = PEP_CANNOT_DECRYPT_UNKNOWN;
    1.60 @@ -2015,22 +2049,16 @@
    1.61      char *ptext = NULL;
    1.62      size_t psize;
    1.63      stringlist_t *_keylist = NULL;
    1.64 -    unsigned char pepstr[] = PEP_SUBJ_STRING;
    1.65 -
    1.66 -    assert(session);
    1.67 -    assert(src);
    1.68 -    assert(dst);
    1.69 -    assert(keylist);
    1.70 -    assert(rating);
    1.71 -    assert(flags);
    1.72 -
    1.73 -    if (!(session && src && dst && keylist && rating && flags))
    1.74 -        return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
    1.75 +
    1.76 +    *dst = NULL;
    1.77 +    *keylist = NULL;
    1.78 +    *rating = PEP_rating_undefined;
    1.79  
    1.80      *flags = 0;
    1.81      /*** End init ***/
    1.82  
    1.83      /*** Begin Import any attached public keys and update identities accordingly ***/
    1.84 +
    1.85      // Private key in unencrypted mail are ignored -> NULL
    1.86      bool imported_keys = import_attached_keys(session, src, NULL);
    1.87  
    1.88 @@ -2039,9 +2067,10 @@
    1.89      status = _update_identity_for_incoming_message(session, src);
    1.90      if(status != PEP_STATUS_OK)
    1.91          return ADD_TO_LOG(status);
    1.92 +
    1.93      /*** End Import any attached public keys and update identities accordingly ***/
    1.94      
    1.95 -    /*** Begin Get detached signatures that are attached to the encrypted message ***/
    1.96 +    /*** Begin get detached signatures that are attached to the encrypted message ***/
    1.97      // Get detached signature, if any
    1.98      bloblist_t* detached_sig = NULL;
    1.99      char* dsig_text = NULL;
   1.100 @@ -2051,51 +2080,30 @@
   1.101          dsig_text = detached_sig->value;
   1.102          dsig_size = detached_sig->size;
   1.103      }
   1.104 -    /*** End Get detached signatures that are attached to the encrypted message ***/
   1.105 +    /*** End get detached signatures that are attached to the encrypted message ***/
   1.106  
   1.107      /*** Determine encryption format ***/
   1.108      PEP_cryptotech crypto = determine_encryption_format(src);
   1.109  
   1.110 -    *dst = NULL;
   1.111 -    *keylist = NULL;
   1.112 -    *rating = PEP_rating_undefined;
   1.113 -
   1.114 -    switch (src->enc_format) {
   1.115 -        /*** BEGIN UNENCRYPTED MESSAGE HANDLING ***/
   1.116 -        case PEP_enc_none:
   1.117 -            *rating = PEP_rating_unencrypted;
   1.118 -
   1.119 -            if (imported_keys)
   1.120 -                remove_attached_keys(src);
   1.121 -
   1.122 -            status = check_for_sync_msg(session, src, rating, keylist);
   1.123 -            
   1.124 -            if (status != PEP_STATUS_OK)
   1.125 -                return ADD_TO_LOG(status);
   1.126 -                                        
   1.127 -            pull_up_attached_main_msg(src);
   1.128 -            
   1.129 -            return ADD_TO_LOG(PEP_UNENCRYPTED);
   1.130 -        /*** END UNENCRYPTED MESSAGE HANDLING ***/
   1.131 -
   1.132 -        case PEP_enc_PGP_MIME:
   1.133 -            ctext = src->attachments->next->value;
   1.134 -            csize = src->attachments->next->size;
   1.135 -            break;
   1.136 -
   1.137 -        case PEP_enc_PGP_MIME_Outlook1:
   1.138 -            ctext = src->attachments->value;
   1.139 -            csize = src->attachments->size;
   1.140 -            break;
   1.141 -
   1.142 -        case PEP_enc_pieces:
   1.143 -            ctext = src->longmsg;
   1.144 -            csize = strlen(ctext);
   1.145 -            break;
   1.146 -
   1.147 -        default:
   1.148 -            NOT_IMPLEMENTED
   1.149 +    // Check for and deal with unencrypted messages
   1.150 +    if (src->enc_format == PEP_enc_none) {
   1.151 +
   1.152 +        *rating = PEP_rating_unencrypted;
   1.153 +
   1.154 +        if (imported_keys)
   1.155 +            remove_attached_keys(src);
   1.156 +
   1.157 +        status = check_for_sync_msg(session, src, rating, keylist);
   1.158 +        
   1.159 +        if (status != PEP_STATUS_OK)
   1.160 +            return ADD_TO_LOG(status);
   1.161 +                                    
   1.162 +        pull_up_attached_main_msg(src);
   1.163 +        
   1.164 +        return ADD_TO_LOG(PEP_UNENCRYPTED);
   1.165      }
   1.166 +
   1.167 +    get_crypto_text(src, &ctext, &csize);
   1.168      
   1.169      /** Ok, we should be ready to decrypt. Try decrypt and verify first! **/
   1.170      status = cryptotech[crypto].decrypt_and_verify(session, ctext,
   1.171 @@ -2112,8 +2120,8 @@
   1.172  
   1.173      bool imported_private_key_address = false;
   1.174  
   1.175 -    if (ptext) {
   1.176 -        switch (src->enc_format) {
   1.177 +    if (ptext) { /* Begin: if we got a plaintext from decryption */
   1.178 +         switch (src->enc_format) {
   1.179              
   1.180              case PEP_enc_PGP_MIME:
   1.181              case PEP_enc_PGP_MIME_Outlook1:
   1.182 @@ -2175,7 +2183,7 @@
   1.183              free_identity_list(_private_il);
   1.184          }
   1.185  
   1.186 -        if(decrypt_status == PEP_DECRYPTED){
   1.187 +        if (decrypt_status == PEP_DECRYPTED){
   1.188  
   1.189              // TODO optimize if import_attached_keys didn't import any key
   1.190  
   1.191 @@ -2187,9 +2195,7 @@
   1.192  
   1.193              status = _update_identity_for_incoming_message(session, src);
   1.194              if(status != PEP_STATUS_OK)
   1.195 -            {
   1.196                  GOTO(pep_error);
   1.197 -            }
   1.198  
   1.199              char *re_ptext = NULL;
   1.200              size_t re_psize;
   1.201 @@ -2203,15 +2209,16 @@
   1.202              free(re_ptext);
   1.203  
   1.204              if (status > PEP_CANNOT_DECRYPT_UNKNOWN)
   1.205 -            {
   1.206                  GOTO(pep_error);
   1.207 -            }
   1.208  
   1.209              decrypt_status = status;
   1.210          }
   1.211  
   1.212          *rating = decrypt_rating(decrypt_status);
   1.213  
   1.214 +        /* Ok, now we have a keylist used for decryption/verification.
   1.215 +           now we need to update the message rating with the 
   1.216 +           sender and recipients in mind */
   1.217          status = amend_rating_according_to_sender_and_recipients(session,
   1.218                                                                   rating,
   1.219                                                                   src->from,
   1.220 @@ -2219,55 +2226,53 @@
   1.221  
   1.222          if (status != PEP_STATUS_OK)
   1.223              GOTO(pep_error);
   1.224 -    }
   1.225 -    else
   1.226 -    {
   1.227 +    } /* End: if we got a plaintext from decryption */
   1.228 +    else {
   1.229 +        // We did not get a plaintext out of the decryption process.
   1.230 +        // Abort and return error.
   1.231          *rating = decrypt_rating(decrypt_status);
   1.232          goto pep_error;
   1.233      }
   1.234  
   1.235 -    // Case of own key imported from own trusted message
   1.236 -    if (// Message have been reliably decrypted
   1.237 -        msg &&
   1.238 -        *rating >= PEP_rating_trusted &&
   1.239 -        imported_private_key_address &&
   1.240 -        // to is [own]
   1.241 +    /* 
   1.242 +       Ok, at this point, we know we have a reliably decrypted message.
   1.243 +       Prepare the output message for return.
   1.244 +    */
   1.245 +    
   1.246 +    // 1. Check to see if this message is to us and contains an own key imported 
   1.247 +    // from own trusted message 
   1.248 +    if (msg && *rating >= PEP_rating_trusted && imported_private_key_address &&
   1.249          msg->to->ident->user_id &&
   1.250 -        strcmp(msg->to->ident->user_id, PEP_OWN_USERID) == 0
   1.251 -        )
   1.252 -    {
   1.253 +        strcmp(msg->to->ident->user_id, PEP_OWN_USERID) == 0) {
   1.254 +
   1.255 +        // flag it as such
   1.256          *flags |= PEP_decrypt_flag_own_private_key;
   1.257      }
   1.258  
   1.259 +    // 2. Clean up message and prepare for return 
   1.260      if (msg) {
   1.261 +        
   1.262 +        /* add pEp-related status flags to header */
   1.263          decorate_message(msg, *rating, _keylist);
   1.264 +        
   1.265          if (imported_keys)
   1.266              remove_attached_keys(msg);
   1.267 -        if (*rating >= PEP_rating_reliable &&
   1.268 -            session->sync_session->inject_sync_msg) {
   1.269 -            status = receive_DeviceState_msg(session, msg, *rating, _keylist);
   1.270 -            if (status == PEP_MESSAGE_CONSUME ||
   1.271 -                status == PEP_MESSAGE_IGNORE) {
   1.272 -                free_message(msg);
   1.273 -                msg = NULL;
   1.274 -                *flags |= (status == PEP_MESSAGE_IGNORE) ?
   1.275 -                            PEP_decrypt_flag_ignore :
   1.276 -                            PEP_decrypt_flag_consume;
   1.277 -
   1.278 -            }
   1.279 -            else if (status != PEP_STATUS_OK){
   1.280 +            
   1.281 +        if (*rating >= PEP_rating_reliable) { 
   1.282 +            status = check_for_sync_msg(session, src, rating, _keylist);
   1.283 +        
   1.284 +            if (status != PEP_STATUS_OK)
   1.285                  goto pep_error;
   1.286 -            }
   1.287          }
   1.288 -    }
   1.289 -    if (msg) {
   1.290 +        
   1.291 +        // copy message id to output message        
   1.292          if (src->id) {
   1.293              msg->id = strdup(src->id);
   1.294              assert(msg->id);
   1.295              if (msg->id == NULL)
   1.296                  goto enomem;
   1.297          }
   1.298 -    }
   1.299 +    } // End prepare output message for return
   1.300  
   1.301      *dst = msg;
   1.302      *keylist = _keylist;