Engine-97 Merge with default keysync-windows
authorMarkus Schaber <markus@pep-security.net>
Mon, 03 Oct 2016 17:16:33 +0200
branchkeysync-windows
changeset 12328ca2cf56f8af
parent 1227 633e90f0e2f2
parent 1231 fc71de0ef4a1
child 1242 e7b0ff022963
Engine-97 Merge with default
src/keymanagement.c
src/keymanagement.h
src/message_api.c
src/mime.c
src/pEpEngine.c
     1.1 --- a/src/keymanagement.c	Sun Oct 02 11:20:02 2016 +0200
     1.2 +++ b/src/keymanagement.c	Mon Oct 03 17:16:33 2016 +0200
     1.3 @@ -113,6 +113,7 @@
     1.4      )
     1.5  {
     1.6      pEp_identity *stored_identity;
     1.7 +    pEp_identity* temp_id = NULL;
     1.8      PEP_STATUS status;
     1.9  
    1.10      assert(session);
    1.11 @@ -159,100 +160,133 @@
    1.12      if (status == PEP_OUT_OF_MEMORY)
    1.13          goto exit_free;
    1.14  
    1.15 +    /* We elect a pubkey first in case there's no acceptable stored fpr */
    1.16 +    temp_id = identity_dup(identity);
    1.17 +    
    1.18 +    status = elect_pubkey(session, temp_id);
    1.19 +    if (status != PEP_STATUS_OK)
    1.20 +        goto exit_free;
    1.21 +        
    1.22      if (stored_identity) {
    1.23          PEP_comm_type _comm_type_key;
    1.24 -        status = get_key_rating(session, stored_identity->fpr, &_comm_type_key);
    1.25 -        assert(status != PEP_OUT_OF_MEMORY);
    1.26 -        if (status == PEP_OUT_OF_MEMORY)
    1.27 -            goto exit_free;
    1.28 +        
    1.29 +        bool dont_use_fpr = true;
    1.30  
    1.31 -        if (EMPTYSTR(identity->username)) {
    1.32 -            free(identity->username);
    1.33 -            identity->username = strdup(stored_identity->username);
    1.34 -            assert(identity->username);
    1.35 -            if (identity->username == NULL){
    1.36 +        /* if we have a stored_identity fpr */
    1.37 +        if (!EMPTYSTR(stored_identity->fpr)) {
    1.38 +            status = blacklist_is_listed(session, stored_identity->fpr, &dont_use_fpr);
    1.39 +            if (status != PEP_STATUS_OK)
    1.40 +                dont_use_fpr = true; 
    1.41 +        }
    1.42 +            
    1.43 +
    1.44 +        if (!dont_use_fpr) {
    1.45 +            free(temp_id->fpr);
    1.46 +            temp_id->fpr = strdup(stored_identity->fpr);
    1.47 +            assert(temp_id->fpr);
    1.48 +            if (temp_id->fpr == NULL) {
    1.49 +                status = PEP_OUT_OF_MEMORY;
    1.50 +                goto exit_free;
    1.51 +            }
    1.52 +        }
    1.53 +        else if (!EMPTYSTR(temp_id->fpr)) {
    1.54 +            status = blacklist_is_listed(session, temp_id->fpr, &dont_use_fpr);
    1.55 +            if (dont_use_fpr) {
    1.56 +                free(temp_id->fpr);
    1.57 +                temp_id->fpr = strdup("");
    1.58 +            }
    1.59 +            else {
    1.60 +                _did_elect_new_key = 1;
    1.61 +            }
    1.62 +        }
    1.63 +        else {
    1.64 +            if (temp_id->fpr == NULL)
    1.65 +                temp_id->fpr = strdup("");
    1.66 +        }
    1.67 +        
    1.68 +        /* ok, from here on out, use temp_id */
    1.69 +        
    1.70 +        
    1.71 +        /* At this point, we either have a non-blacklisted fpr we can work */
    1.72 +        /* with, or we've got nada.                                        */        
    1.73 +        if (!EMPTYSTR(temp_id->fpr)) {
    1.74 +            status = get_key_rating(session, temp_id->fpr, &_comm_type_key);
    1.75 +            assert(status != PEP_OUT_OF_MEMORY);
    1.76 +            if (status == PEP_OUT_OF_MEMORY)
    1.77 +                goto exit_free;
    1.78 +            status = get_trust(session, temp_id);
    1.79 +            if (status == PEP_OUT_OF_MEMORY)
    1.80 +                goto exit_free;
    1.81 +            if (_comm_type_key < PEP_ct_unconfirmed_encryption) {
    1.82 +                temp_id->comm_type = _comm_type_key;
    1.83 +            } else{
    1.84 +                temp_id->comm_type = stored_identity->comm_type;
    1.85 +                if (temp_id->comm_type == PEP_ct_unknown) {
    1.86 +                    temp_id->comm_type = _comm_type_key;
    1.87 +                }
    1.88 +            }
    1.89 +        }
    1.90 +            
    1.91 +        if (EMPTYSTR(temp_id->username)) {
    1.92 +            free(temp_id->username);
    1.93 +            temp_id->username = strdup(stored_identity->username);
    1.94 +            assert(temp_id->username);
    1.95 +            if (temp_id->username == NULL){
    1.96                  status = PEP_OUT_OF_MEMORY;
    1.97                  goto exit_free;
    1.98              }
    1.99          }
   1.100  
   1.101 -        if (EMPTYSTR(identity->fpr)) {
   1.102 -            identity->fpr = strdup(stored_identity->fpr);
   1.103 -            assert(identity->fpr);
   1.104 -            if (identity->fpr == NULL)
   1.105 -                return PEP_OUT_OF_MEMORY;
   1.106 -            if (_comm_type_key < PEP_ct_unconfirmed_encryption) {
   1.107 -                PEP_STATUS status = elect_pubkey(session, identity);
   1.108 -                if (status != PEP_STATUS_OK)
   1.109 -                    goto exit_free;
   1.110 +        if (temp_id->lang[0] == 0) {
   1.111 +            temp_id->lang[0] = stored_identity->lang[0];
   1.112 +            temp_id->lang[1] = stored_identity->lang[1];
   1.113 +            temp_id->lang[2] = 0;
   1.114 +        }
   1.115  
   1.116 -                _did_elect_new_key = 1;
   1.117 -            }
   1.118 -            else {
   1.119 -                identity->comm_type = stored_identity->comm_type;
   1.120 -            }
   1.121 -        }
   1.122 -        else /* !EMPTYSTR(identity->fpr) */ {
   1.123 -            if (_same_fpr(identity->fpr,
   1.124 -                          strlen(identity->fpr),
   1.125 -                          stored_identity->fpr,
   1.126 -                          strlen(stored_identity->fpr))) {
   1.127 -                if (_comm_type_key < PEP_ct_unconfirmed_encryption) {
   1.128 -                    identity->comm_type = _comm_type_key;
   1.129 -                }else{
   1.130 -                    identity->comm_type = stored_identity->comm_type;
   1.131 -                    if (identity->comm_type == PEP_ct_unknown) {
   1.132 -                        identity->comm_type = _comm_type_key;
   1.133 -                    }
   1.134 -                }
   1.135 -            } else {
   1.136 -                status = get_trust(session, identity);
   1.137 +        temp_id->flags = stored_identity->flags;
   1.138 +    }
   1.139 +    else /* stored_identity == NULL */ {
   1.140 +        temp_id->flags = 0;
   1.141 +
   1.142 +        /* Work with the elected key from above */
   1.143 +        if (!EMPTYSTR(temp_id->fpr)) {
   1.144 +            
   1.145 +            bool dont_use_fpr = true;
   1.146 +            status = blacklist_is_listed(session, temp_id->fpr, &dont_use_fpr);
   1.147 +            if (status != PEP_STATUS_OK)
   1.148 +                dont_use_fpr = true; 
   1.149 +
   1.150 +            if (!dont_use_fpr) {
   1.151 +                PEP_comm_type _comm_type_key;
   1.152 +
   1.153 +                status = get_key_rating(session, temp_id->fpr, &_comm_type_key);
   1.154                  assert(status != PEP_OUT_OF_MEMORY);
   1.155                  if (status == PEP_OUT_OF_MEMORY)
   1.156                      goto exit_free;
   1.157 -                if (identity->comm_type < stored_identity->comm_type)
   1.158 -                    identity->comm_type = PEP_ct_unknown;
   1.159 +
   1.160 +                temp_id->comm_type = _comm_type_key;
   1.161              }
   1.162 -        }
   1.163 -
   1.164 -        if (identity->lang[0] == 0) {
   1.165 -            identity->lang[0] = stored_identity->lang[0];
   1.166 -            identity->lang[1] = stored_identity->lang[1];
   1.167 -            identity->lang[2] = 0;
   1.168 -        }
   1.169 -
   1.170 -        identity->flags = stored_identity->flags;
   1.171 -    }
   1.172 -    else /* stored_identity == NULL */ {
   1.173 -        identity->flags = 0;
   1.174 -
   1.175 -        if (!EMPTYSTR(identity->fpr)) {
   1.176 -            PEP_comm_type _comm_type_key;
   1.177 -
   1.178 -            status = get_key_rating(session, identity->fpr, &_comm_type_key);
   1.179 -            assert(status != PEP_OUT_OF_MEMORY);
   1.180 -            if (status == PEP_OUT_OF_MEMORY)
   1.181 -                goto exit_free;
   1.182 -
   1.183 -            identity->comm_type = _comm_type_key;
   1.184 -        }
   1.185 -        else /* EMPTYSTR(identity->fpr) */ {
   1.186 -            PEP_STATUS status = elect_pubkey(session, identity);
   1.187 -            if (status != PEP_STATUS_OK)
   1.188 -                goto exit_free;
   1.189 +            else {
   1.190 +                free(temp_id->fpr);
   1.191 +                temp_id->fpr = strdup("");
   1.192 +            }
   1.193          }
   1.194      }
   1.195  
   1.196 +    if (temp_id->fpr == NULL)
   1.197 +        temp_id->fpr = strdup("");
   1.198 +    
   1.199 +    
   1.200      status = PEP_STATUS_OK;
   1.201  
   1.202 -    if (identity->comm_type != PEP_ct_unknown && !EMPTYSTR(identity->user_id)) {
   1.203 -        assert(!EMPTYSTR(identity->username)); // this should not happen
   1.204 +    if (temp_id->comm_type != PEP_ct_unknown && !EMPTYSTR(temp_id->user_id)) {
   1.205 +        assert(!EMPTYSTR(temp_id->username)); // this should not happen
   1.206  
   1.207 -        if (EMPTYSTR(identity->username)) { // mitigate
   1.208 -            free(identity->username);
   1.209 -            identity->username = strdup("anonymous");
   1.210 -            assert(identity->username);
   1.211 -            if (identity->username == NULL){
   1.212 +        if (EMPTYSTR(temp_id->username)) { // mitigate
   1.213 +            free(temp_id->username);
   1.214 +            temp_id->username = strdup("anonymous");
   1.215 +            assert(temp_id->username);
   1.216 +            if (temp_id->username == NULL){
   1.217                  status = PEP_OUT_OF_MEMORY;
   1.218                  goto exit_free;
   1.219              }
   1.220 @@ -262,7 +296,7 @@
   1.221          // user by address (i.e. no user id given but already stored)
   1.222          if (!(_no_user_id && stored_identity) || _did_elect_new_key)
   1.223          {
   1.224 -            status = set_identity(session, identity);
   1.225 +            status = set_identity(session, temp_id);
   1.226              assert(status == PEP_STATUS_OK);
   1.227              if (status != PEP_STATUS_OK) {
   1.228                  goto exit_free;
   1.229 @@ -270,10 +304,26 @@
   1.230          }
   1.231      }
   1.232  
   1.233 -    if (identity->comm_type != PEP_ct_compromized &&
   1.234 -            identity->comm_type < PEP_ct_strong_but_unconfirmed)
   1.235 +    if (temp_id->comm_type != PEP_ct_compromized &&
   1.236 +            temp_id->comm_type < PEP_ct_strong_but_unconfirmed)
   1.237          if (session->examine_identity)
   1.238 -            session->examine_identity(identity, session->examine_management);
   1.239 +            session->examine_identity(temp_id, session->examine_management);
   1.240 +    
   1.241 +    /* ok, we got to the end. So we can assign the output identity */
   1.242 +    free(identity->address);
   1.243 +    identity->address = strdup(temp_id->address);
   1.244 +    free(identity->fpr);
   1.245 +    identity->fpr = strdup(temp_id->fpr);
   1.246 +    free(identity->user_id);
   1.247 +    identity->user_id = strdup(temp_id->user_id);
   1.248 +    free(identity->username);
   1.249 +    identity->username = strdup(temp_id->username);
   1.250 +    identity->comm_type = temp_id->comm_type;
   1.251 +    identity->lang[0] = temp_id->lang[0];
   1.252 +    identity->lang[1] = temp_id->lang[1];
   1.253 +    identity->lang[2] = 0;
   1.254 +    identity->me = temp_id->me;
   1.255 +    identity->flags = temp_id->flags;
   1.256  
   1.257  exit_free :
   1.258      
   1.259 @@ -281,6 +331,9 @@
   1.260          free_identity(stored_identity);
   1.261      }
   1.262  
   1.263 +    if (temp_id)
   1.264 +        free_identity(temp_id);
   1.265 +    
   1.266      return status;
   1.267  }
   1.268  
     2.1 --- a/src/keymanagement.h	Sun Oct 02 11:20:02 2016 +0200
     2.2 +++ b/src/keymanagement.h	Mon Oct 03 17:16:33 2016 +0200
     2.3 @@ -11,7 +11,7 @@
     2.4  //  parameters:
     2.5  //      session (in)        session to use
     2.6  //      identity (inout)    identity information of communication partner
     2.7 -//
     2.8 +//                          (identity->fpr is OUT ONLY)
     2.9  //  caveat:
    2.10  //      if this function returns PEP_ct_unknown or PEP_ct_key_expired in
    2.11  //      identity->comm_type, the caller must insert the identity into the
    2.12 @@ -20,6 +20,7 @@
    2.13  //      at least identity->address must be a non-empty UTF-8 string as input
    2.14  //      update_identity() never writes flags; use set_identity_flags() for
    2.15  //      writing
    2.16 +//      this function NEVER reads the incoming fpr, only writes to it.
    2.17  
    2.18  DYNAMIC_API PEP_STATUS update_identity(
    2.19          PEP_SESSION session, pEp_identity * identity
     3.1 --- a/src/mime.c	Sun Oct 02 11:20:02 2016 +0200
     3.2 +++ b/src/mime.c	Mon Oct 03 17:16:33 2016 +0200
     3.3 @@ -1300,7 +1300,14 @@
     3.4              }
     3.5          }
     3.6          else {
     3.7 -            if (_is_text_part(content, NULL) && msg->longmsg == NULL) {
     3.8 +            if (_is_text_part(content, "html") &&
     3.9 +                msg->longmsg_formatted == NULL) {
    3.10 +                status = interpret_body(mime, &msg->longmsg_formatted,
    3.11 +                                        NULL);
    3.12 +                if (status)
    3.13 +                    return status;
    3.14 +            }
    3.15 +            else if (_is_text_part(content, NULL) && msg->longmsg == NULL) {
    3.16                  status = interpret_body(mime, &msg->longmsg, NULL);
    3.17                  if (status)
    3.18                      return status;
     4.1 --- a/src/pEpEngine.c	Sun Oct 02 11:20:02 2016 +0200
     4.2 +++ b/src/pEpEngine.c	Mon Oct 03 17:16:33 2016 +0200
     4.3 @@ -986,22 +986,24 @@
     4.4      assert(session);
     4.5      assert(identity);
     4.6      assert(identity->address);
     4.7 -    assert(identity->fpr);
     4.8      assert(identity->user_id);
     4.9      assert(identity->username);
    4.10  
    4.11 -    if (!(session && identity && identity->address && identity->fpr &&
    4.12 +    if (!(session && identity && identity->address &&
    4.13                  identity->user_id && identity->username))
    4.14          return PEP_ILLEGAL_VALUE;
    4.15  
    4.16      bool listed;
    4.17 -    PEP_STATUS status = blacklist_is_listed(session, identity->fpr, &listed);
    4.18 -    assert(status == PEP_STATUS_OK);
    4.19 -    if (status != PEP_STATUS_OK)
    4.20 -        return status;
    4.21 +    
    4.22 +    if (identity->fpr && identity->fpr[0] != '\0') {
    4.23 +        PEP_STATUS status = blacklist_is_listed(session, identity->fpr, &listed);
    4.24 +        assert(status == PEP_STATUS_OK);
    4.25 +        if (status != PEP_STATUS_OK)
    4.26 +            return status;
    4.27  
    4.28 -    if (listed)
    4.29 -        return PEP_KEY_BLACKLISTED;
    4.30 +        if (listed)
    4.31 +            return PEP_KEY_BLACKLISTED;
    4.32 +    }
    4.33  
    4.34      sqlite3_exec(session->db, "BEGIN ;", NULL, NULL, NULL);
    4.35