SEMANTIC CHANGE in myself(): .fpr is never used; to set fpr now use set_own_key()
authorVolker Birk <vb@pep.foundation>
Sat, 24 Feb 2018 16:51:38 +0100
changeset 253576edc488ccdd
parent 2532 4d46b7dd0fce
child 2536 ee7e89ef6a18
SEMANTIC CHANGE in myself(): .fpr is never used; to set fpr now use set_own_key()
src/keymanagement.c
src/keymanagement.h
test/apple_mail_test.cc
test/encrypt_for_identity_test.cc
test/new_update_id_and_myself_test.cc
test/revoke_regen_attach_test.cc
     1.1 --- a/src/keymanagement.c	Sat Feb 24 10:57:24 2018 +0100
     1.2 +++ b/src/keymanagement.c	Sat Feb 24 16:51:38 2018 +0100
     1.3 @@ -864,6 +864,7 @@
     1.4  
     1.5      pEp_identity *stored_identity = NULL;
     1.6      char* revoked_fpr = NULL; 
     1.7 +    bool valid_key_found = false;
     1.8          
     1.9      char* default_own_id = NULL;
    1.10      status = get_default_own_userid(session, &default_own_id);
    1.11 @@ -905,8 +906,10 @@
    1.12                            &stored_identity);
    1.13  
    1.14      assert(status != PEP_OUT_OF_MEMORY);
    1.15 -    if (status == PEP_OUT_OF_MEMORY)
    1.16 -        return PEP_OUT_OF_MEMORY;
    1.17 +    if (status == PEP_OUT_OF_MEMORY) {
    1.18 +        status = PEP_OUT_OF_MEMORY;
    1.19 +        goto pep_free;
    1.20 +    }
    1.21  
    1.22      // Set usernames - priority is input username > stored name > address
    1.23      // If there's an input username, we always patch the username with that
    1.24 @@ -916,50 +919,49 @@
    1.25          char* uname = (stored_uname ? stored_identity->username : identity->address);
    1.26          free(identity->username);
    1.27          identity->username = strdup(uname);
    1.28 -        if (identity->username == NULL)
    1.29 -            return PEP_OUT_OF_MEMORY;
    1.30 +        if (identity->username == NULL) {
    1.31 +            status = PEP_OUT_OF_MEMORY;
    1.32 +            goto pep_free;
    1.33 +        }
    1.34      }
    1.35  
    1.36 -    bool valid_key_found = false;
    1.37 -    
    1.38 -    // Now deal with keys.
    1.39 -    // Different from update_identity(), the input fpr here
    1.40 -    // MATTERS. 
    1.41 -    // If the input fpr is invalid, we return, giving the reason why.
    1.42 -    if (!EMPTYSTR(identity->fpr)) {
    1.43 -        status = validate_fpr(session, identity);
    1.44 -    
    1.45 -        if (status != PEP_STATUS_OK || 
    1.46 -            identity->comm_type < PEP_ct_strong_but_unconfirmed) {
    1.47 -            if (identity->comm_type != PEP_ct_key_expired)
    1.48 -                goto pep_free;
    1.49 -            // Otherwise, it was expired and key renewal failed
    1.50 -            // and we take the stored one or do keygen. 
    1.51 -        } 
    1.52 -        else
    1.53 -            valid_key_found = true;
    1.54 -    }    
    1.55 -    
    1.56 -    // Ok, if there wasn't a valid input fpr, check stored identity
    1.57 -    if (!valid_key_found && stored_identity && 
    1.58 -        (EMPTYSTR(identity->fpr) || strcmp(stored_identity->fpr, identity->fpr) != 0)) {
    1.59 -        
    1.60 +    // ignore input fpr
    1.61 +
    1.62 +    if (identity->fpr) {
    1.63 +        free(identity->fpr);
    1.64 +        identity->fpr = NULL;
    1.65 +    }
    1.66 +
    1.67 +    // check stored identity
    1.68 +    if (stored_identity && !EMPTYSTR(stored_identity->fpr)) {
    1.69          // Fall back / retrieve
    1.70          status = validate_fpr(session, stored_identity);
    1.71 -        if (status == PEP_STATUS_OK && 
    1.72 -            stored_identity->comm_type >= PEP_ct_strong_but_unconfirmed) {
    1.73 -          
    1.74 -            free(identity->fpr);
    1.75 -            identity->fpr = strdup(stored_identity->fpr);
    1.76 -            valid_key_found = true;            
    1.77 -        }
    1.78 -        else {
    1.79 -            bool revoked = false;
    1.80 -            if (!EMPTYSTR(stored_identity->fpr)) {
    1.81 +        if (status == PEP_OUT_OF_MEMORY)
    1.82 +            goto pep_free;
    1.83 +        if (status == PEP_STATUS_OK) {
    1.84 +            if (stored_identity->comm_type >= PEP_ct_strong_but_unconfirmed) {
    1.85 +                identity->fpr = strdup(stored_identity->fpr);
    1.86 +                assert(identity->fpr);
    1.87 +                if (!identity->fpr) {
    1.88 +                    status = PEP_OUT_OF_MEMORY;
    1.89 +                    goto pep_free;
    1.90 +                }
    1.91 +                valid_key_found = true;            
    1.92 +            }
    1.93 +            else {
    1.94 +                bool revoked = false;
    1.95                  status = key_revoked(session, stored_identity->fpr, &revoked);
    1.96 -                if (revoked)
    1.97 +                if (status)
    1.98 +                    goto pep_free;
    1.99 +                if (revoked) {
   1.100                      revoked_fpr = strdup(stored_identity->fpr);
   1.101 -            }        
   1.102 +                    assert(revoked_fpr);
   1.103 +                    if (!revoked_fpr) {
   1.104 +                        status = PEP_OUT_OF_MEMORY;
   1.105 +                        goto pep_free;
   1.106 +                    }
   1.107 +                }
   1.108 +            }
   1.109          }
   1.110      }
   1.111      
   1.112 @@ -1011,31 +1013,31 @@
   1.113      return status;
   1.114  }
   1.115  
   1.116 -DYNAMIC_API PEP_STATUS initialise_own_identities(PEP_SESSION session,
   1.117 -                                                 identity_list* my_idents) {
   1.118 -    PEP_STATUS status = PEP_STATUS_OK;
   1.119 -    if (!session)
   1.120 -        return PEP_ILLEGAL_VALUE;
   1.121 -        
   1.122 -    if (!my_idents)
   1.123 -        return PEP_STATUS_OK;
   1.124 -            
   1.125 -    identity_list* ident_curr = my_idents;
   1.126 -    while (ident_curr) {
   1.127 -        pEp_identity* ident = ident_curr->ident;
   1.128 -        if (!ident || !ident->address) {
   1.129 -            status = PEP_ILLEGAL_VALUE;
   1.130 -            goto pep_error;
   1.131 -        }
   1.132 -
   1.133 -        status = _myself(session, ident, false, false);
   1.134 -        
   1.135 -        ident_curr = ident_curr->next;
   1.136 -    }
   1.137 -    
   1.138 -pep_error:
   1.139 -    return status;
   1.140 -}
   1.141 +// DYNAMIC_API PEP_STATUS initialise_own_identities(PEP_SESSION session,
   1.142 +//                                                  identity_list* my_idents) {
   1.143 +//     PEP_STATUS status = PEP_STATUS_OK;
   1.144 +//     if (!session)
   1.145 +//         return PEP_ILLEGAL_VALUE;
   1.146 +//         
   1.147 +//     if (!my_idents)
   1.148 +//         return PEP_STATUS_OK;
   1.149 +//             
   1.150 +//     identity_list* ident_curr = my_idents;
   1.151 +//     while (ident_curr) {
   1.152 +//         pEp_identity* ident = ident_curr->ident;
   1.153 +//         if (!ident || !ident->address) {
   1.154 +//             status = PEP_ILLEGAL_VALUE;
   1.155 +//             goto pep_error;
   1.156 +//         }
   1.157 +// 
   1.158 +//         status = _myself(session, ident, false, false);
   1.159 +//         
   1.160 +//         ident_curr = ident_curr->next;
   1.161 +//     }
   1.162 +//     
   1.163 +// pep_error:
   1.164 +//     return status;
   1.165 +// }
   1.166  
   1.167  DYNAMIC_API PEP_STATUS myself(PEP_SESSION session, pEp_identity * identity)
   1.168  {
   1.169 @@ -1618,69 +1620,43 @@
   1.170      return _own_keys_retrieve(session, keylist, 0);
   1.171  }
   1.172  
   1.173 -// FIXME: should it be be used when sync receive old keys ? (ENGINE-145)
   1.174  DYNAMIC_API PEP_STATUS set_own_key(
   1.175         PEP_SESSION session,
   1.176 -       const char *address,
   1.177 +       pEp_identity *me,
   1.178         const char *fpr
   1.179      )
   1.180  {
   1.181      PEP_STATUS status = PEP_STATUS_OK;
   1.182      
   1.183 -    assert(session &&
   1.184 -           address &&
   1.185 -           fpr && fpr[0]
   1.186 -          );
   1.187 -    
   1.188 -    if (!(session &&
   1.189 -          address &&
   1.190 -          fpr && fpr[0]
   1.191 -         ))
   1.192 +    assert(session && me);
   1.193 +    assert(!EMPTYSTR(fpr));
   1.194 +    assert(!EMPTYSTR(me->address));
   1.195 +    assert(!EMPTYSTR(me->user_id));
   1.196 +    assert(!EMPTYSTR(me->username));
   1.197 +
   1.198 +    if (!session || !me || EMPTYSTR(fpr) || EMPTYSTR(me->address) ||
   1.199 +            EMPTYSTR(me->user_id) || EMPTYSTR(me->username))
   1.200          return PEP_ILLEGAL_VALUE;
   1.201 -                        
   1.202 -    // First see if we have it in own identities already, AND we retrieve
   1.203 -    // our own user_id
   1.204 -    char* my_user_id = NULL;
   1.205 -    status = get_default_own_userid(session, &my_user_id);
   1.206 -    if (status != PEP_STATUS_OK)
   1.207 +
   1.208 +    status = _myself(session, me, false, true);
   1.209 +    // we do not need a valid key but dislike other errors
   1.210 +    if (status != PEP_STATUS_OK && status != PEP_GET_KEY_FAILED && status != PEP_KEY_UNSUITABLE)
   1.211          return status;
   1.212 -        
   1.213 -    if (!my_user_id) {
   1.214 -        // We have no own user_id. So we cannot set it for an identity.
   1.215 -        return PEP_CANNOT_FIND_IDENTITY;
   1.216 -    }
   1.217 -    
   1.218 -    pEp_identity* my_id = NULL;
   1.219 -    
   1.220 -    status = get_identity(session, address, my_user_id, &my_id);
   1.221 +    status = PEP_STATUS_OK;
   1.222 + 
   1.223 +    if (me->fpr)
   1.224 +        free(me->fpr);
   1.225 +    me->fpr = strdup(fpr);
   1.226 +    assert(me->fpr);
   1.227 +    if (!me->fpr)
   1.228 +        return PEP_OUT_OF_MEMORY;
   1.229  
   1.230 -    if (status == PEP_STATUS_OK && my_id) {
   1.231 -        if (my_id->fpr && strcasecmp(my_id->fpr, fpr) == 0) {
   1.232 -            // We're done. It was already here.
   1.233 -            goto pep_free;
   1.234 -        }           
   1.235 -    }
   1.236 -                
   1.237 -    // If there's an id w/ user_id + address
   1.238 -    if (my_id) {
   1.239 -        free(my_id->fpr);
   1.240 -        my_id->fpr = my_user_id;
   1.241 -        my_id->comm_type = PEP_ct_pEp;
   1.242 -        my_id->me = true;
   1.243 -    }
   1.244 -    else { // Else, we need a new identity
   1.245 -        my_id = new_identity(address, fpr, my_user_id, NULL); 
   1.246 -        if (status != PEP_STATUS_OK)
   1.247 -            goto pep_free; 
   1.248 -        my_id->me = true;
   1.249 -        my_id->comm_type = PEP_ct_pEp;
   1.250 -    }
   1.251 -        
   1.252 -    status = set_identity(session, my_id);
   1.253 -    
   1.254 -pep_free:
   1.255 -    free(my_id);
   1.256 -    free(my_user_id);
   1.257 +    status = validate_fpr(session, me);
   1.258 +    if (status)
   1.259 +        return status;
   1.260 +
   1.261 +    me->comm_type = PEP_ct_pEp;
   1.262 +    status = set_identity(session, me);
   1.263      return status;
   1.264  }
   1.265  
     2.1 --- a/src/keymanagement.h	Sat Feb 24 10:57:24 2018 +0100
     2.2 +++ b/src/keymanagement.h	Sat Feb 24 16:51:38 2018 +0100
     2.3 @@ -54,6 +54,7 @@
     2.4          PEP_SESSION session, pEp_identity * identity
     2.5      );
     2.6  
     2.7 +// TODO: remove
     2.8  // initialise_own_identities () - ensures that an own identity is complete
     2.9  //
    2.10  //  parameters:
    2.11 @@ -78,8 +79,8 @@
    2.12  //      N.B. to adapter devs - this function is likely unnecessary, so please
    2.13  //      do not put work into exposing it yet. Tickets will be filed if need be.
    2.14  
    2.15 -DYNAMIC_API PEP_STATUS initialise_own_identities(PEP_SESSION session,
    2.16 -                                                 identity_list* my_idents);
    2.17 +// DYNAMIC_API PEP_STATUS initialise_own_identities(PEP_SESSION session,
    2.18 +//                                                  identity_list* my_idents);
    2.19  
    2.20  // myself() - ensures that an own identity is complete
    2.21  //
    2.22 @@ -336,9 +337,24 @@
    2.23          stringlist_t **keylist
    2.24        );
    2.25  
    2.26 +// set_own_key() - mark a key as own key
    2.27 +//
    2.28 +//  parameters:
    2.29 +//      session (in)            session to use
    2.30 +//      me (inout)              own identity this key is used for
    2.31 +//      fpr (in)                fingerprint of the key to mark as own key
    2.32 +//
    2.33 +//  caveat:
    2.34 +//      the key has to be in the key ring already
    2.35 +//      me->address, me->user_id and me->username must be set to valid data
    2.36 +//      myself() is called by set_own_key() without key generation
    2.37 +//      me->flags are ignored
    2.38 +//      me->address must not be an alias
    2.39 +//      me->fpr will be ignored and replaced by fpr
    2.40 +
    2.41  DYNAMIC_API PEP_STATUS set_own_key(
    2.42         PEP_SESSION session,
    2.43 -       const char *address,
    2.44 +       pEp_identity *me,
    2.45         const char *fpr
    2.46      );
    2.47  
     3.1 --- a/test/apple_mail_test.cc	Sat Feb 24 10:57:24 2018 +0100
     3.2 +++ b/test/apple_mail_test.cc	Sat Feb 24 16:51:38 2018 +0100
     3.3 @@ -35,7 +35,7 @@
     3.4      const string mailtext = slurp(mailfile);
     3.5      pEp_identity * me = new_identity("pep.test.recip@kgrothoff.org", "93D19F24AD6F4C4BA9134AAF84D9217908DB0AEE", PEP_OWN_USERID, "pEp Test Recipient");    
     3.6      me->me = true;    
     3.7 -    PEP_STATUS status = myself(session, me);
     3.8 +    PEP_STATUS status = set_own_key(session, me, "93D19F24AD6F4C4BA9134AAF84D9217908DB0AEE");
     3.9      
    3.10      pEp_identity * you = new_identity("pep.test.apple@pep-project.org", NULL, "pep.test.apple@pep-project.org", "pEp Apple Test");    
    3.11      you->me = false;    
     4.1 --- a/test/encrypt_for_identity_test.cc	Sat Feb 24 10:57:24 2018 +0100
     4.2 +++ b/test/encrypt_for_identity_test.cc	Sat Feb 24 16:51:38 2018 +0100
     4.3 @@ -40,7 +40,7 @@
     4.4      pEp_identity* bob = new_identity("pep.test.bob@pep-project.org", NULL, "42", "Bob Test");
     4.5      alice->me = true;
     4.6  
     4.7 -    PEP_STATUS mystatus = myself(session, alice);
     4.8 +    PEP_STATUS mystatus = set_own_key(session, alice, "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97");
     4.9      assert(mystatus == PEP_STATUS_OK);
    4.10  
    4.11      identity_list* to_list = new_identity_list(bob); // to bob
     5.1 --- a/test/new_update_id_and_myself_test.cc	Sat Feb 24 10:57:24 2018 +0100
     5.2 +++ b/test/new_update_id_and_myself_test.cc	Sat Feb 24 16:51:38 2018 +0100
     5.3 @@ -132,9 +132,7 @@
     5.4  
     5.5      char* new_fpr = strdup(new_me->fpr);
     5.6  
     5.7 -    new_me = new_identity(uniqname, new_fpr, alias_id, NULL);
     5.8 -
     5.9 -    status = myself(session, new_me);
    5.10 +    status = set_own_key(session, new_me, new_fpr);
    5.11      assert(status == PEP_STATUS_OK);
    5.12      assert(new_me->fpr);
    5.13      assert(strcmp(new_me->fpr, generated_fpr) != 0);
    5.14 @@ -153,7 +151,7 @@
    5.15      free(new_me->fpr);
    5.16      new_me->fpr = strdup(generated_fpr);
    5.17      new_me->comm_type = PEP_ct_unknown;
    5.18 -    status = myself(session, new_me);
    5.19 +    status = set_own_key(session, new_me, generated_fpr);
    5.20      assert(status == PEP_STATUS_OK);
    5.21      assert(strcmp(new_me->fpr, generated_fpr) == 0);
    5.22      assert(new_me->comm_type == PEP_ct_pEp);
    5.23 @@ -165,9 +163,9 @@
    5.24      status = revoke_key(session, generated_fpr, "Because it's fun");
    5.25      assert (status == PEP_STATUS_OK);
    5.26      
    5.27 -    new_me = new_identity(uniqname, new_fpr, alias_id, NULL);
    5.28 +    new_me = new_identity(uniqname, NULL, alias_id, start_username);
    5.29      
    5.30 -    status = myself(session, new_me);
    5.31 +    status = set_own_key(session, new_me, new_fpr);
    5.32      assert(status == PEP_STATUS_OK);
    5.33      assert(new_me->fpr);
    5.34      assert(strcmp(new_me->fpr, generated_fpr) != 0);
     6.1 --- a/test/revoke_regen_attach_test.cc	Sat Feb 24 10:57:24 2018 +0100
     6.2 +++ b/test/revoke_regen_attach_test.cc	Sat Feb 24 16:51:38 2018 +0100
     6.3 @@ -46,6 +46,7 @@
     6.4  
     6.5      cout << "re-generated fingerprint \n";
     6.6      free(me->fpr);
     6.7 +    me->fpr = NULL;
     6.8      status = myself(session, me);
     6.9      assert(status == PEP_STATUS_OK);
    6.10      cout << me->fpr << "\n";