src/keymanagement.c
branchENGINE-289
changeset 2388 40cadfd425bb
parent 2386 0ae02e926c6b
child 2394 88b9027db1bf
     1.1 --- a/src/keymanagement.c	Mon Jan 15 17:13:25 2018 +0100
     1.2 +++ b/src/keymanagement.c	Tue Jan 16 01:45:58 2018 +0100
     1.3 @@ -60,35 +60,37 @@
     1.4      assert(status != PEP_OUT_OF_MEMORY);
     1.5      if (status == PEP_OUT_OF_MEMORY)
     1.6          return PEP_OUT_OF_MEMORY;
     1.7 +    
     1.8 +    if (!keylist || !keylist->value)
     1.9 +        identity->comm_type = PEP_ct_key_not_found;    
    1.10 +    else {
    1.11 +        stringlist_t *_keylist;
    1.12 +        for (_keylist = keylist; _keylist && _keylist->value; _keylist = _keylist->next) {
    1.13 +            PEP_comm_type _comm_type_key;
    1.14  
    1.15 -    stringlist_t *_keylist;
    1.16 -    for (_keylist = keylist; _keylist && _keylist->value; _keylist = _keylist->next) {
    1.17 -        PEP_comm_type _comm_type_key;
    1.18 +            status = get_key_rating(session, _keylist->value, &_comm_type_key);
    1.19 +            assert(status != PEP_OUT_OF_MEMORY);
    1.20 +            if (status == PEP_OUT_OF_MEMORY) {
    1.21 +                free_stringlist(keylist);
    1.22 +                return PEP_OUT_OF_MEMORY;
    1.23 +            }
    1.24  
    1.25 -        status = get_key_rating(session, _keylist->value, &_comm_type_key);
    1.26 -        assert(status != PEP_OUT_OF_MEMORY);
    1.27 -        if (status == PEP_OUT_OF_MEMORY) {
    1.28 -            free_stringlist(keylist);
    1.29 -            return PEP_OUT_OF_MEMORY;
    1.30 -        }
    1.31 -
    1.32 -        if (_comm_type_key != PEP_ct_compromized &&
    1.33 -            _comm_type_key != PEP_ct_unknown)
    1.34 -        {
    1.35 -            if (identity->comm_type == PEP_ct_unknown ||
    1.36 -                _comm_type_key > identity->comm_type)
    1.37 +            if (_comm_type_key != PEP_ct_compromized &&
    1.38 +                _comm_type_key != PEP_ct_unknown)
    1.39              {
    1.40 -                bool blacklisted;
    1.41 -                status = blacklist_is_listed(session, _keylist->value, &blacklisted);
    1.42 -                if (status == PEP_STATUS_OK && !blacklisted) {
    1.43 -                    identity->comm_type = _comm_type_key;
    1.44 -                    _fpr = _keylist->value;
    1.45 +                if (identity->comm_type == PEP_ct_unknown ||
    1.46 +                    _comm_type_key > identity->comm_type)
    1.47 +                {
    1.48 +                    bool blacklisted;
    1.49 +                    status = blacklist_is_listed(session, _keylist->value, &blacklisted);
    1.50 +                    if (status == PEP_STATUS_OK && !blacklisted) {
    1.51 +                        identity->comm_type = _comm_type_key;
    1.52 +                        _fpr = _keylist->value;
    1.53 +                    }
    1.54                  }
    1.55              }
    1.56          }
    1.57      }
    1.58 -    
    1.59 -//    if (_fpr) {
    1.60      free(identity->fpr);
    1.61  
    1.62      identity->fpr = strdup(_fpr);
    1.63 @@ -96,7 +98,7 @@
    1.64          free_stringlist(keylist);
    1.65          return PEP_OUT_OF_MEMORY;
    1.66      }
    1.67 -//    }
    1.68 +    
    1.69      free_stringlist(keylist);
    1.70      return PEP_STATUS_OK;
    1.71  }
    1.72 @@ -106,7 +108,7 @@
    1.73      
    1.74      PEP_STATUS status = PEP_STATUS_OK;
    1.75      
    1.76 -    if (!session || !ident || !ident->fpr)
    1.77 +    if (!session || !ident || !ident->fpr || !ident->fpr[0])
    1.78          return PEP_ILLEGAL_VALUE;    
    1.79          
    1.80      char* fpr = ident->fpr;
    1.81 @@ -294,8 +296,10 @@
    1.82      }
    1.83      
    1.84      status = elect_pubkey(session, stored_identity);
    1.85 -    if (status == PEP_STATUS_OK)
    1.86 -        validate_fpr(session, stored_identity);    
    1.87 +    if (status == PEP_STATUS_OK) {
    1.88 +        if (stored_identity->fpr)
    1.89 +            validate_fpr(session, stored_identity);
    1.90 +    }    
    1.91      
    1.92      switch (stored_identity->comm_type) {
    1.93          case PEP_ct_key_revoked:
    1.94 @@ -357,7 +361,8 @@
    1.95          if (status != PEP_STATUS_OK) {
    1.96              return status; // FIXME - free mem
    1.97          }
    1.98 -        free (return_id->fpr);
    1.99 +        free(return_id->fpr);
   1.100 +        return_id->fpr = NULL;
   1.101          return_id->fpr = strdup(stored_ident->fpr);
   1.102          return_id->comm_type = stored_ident->comm_type;            
   1.103      }
   1.104 @@ -381,6 +386,12 @@
   1.105          if (!return_id->username)
   1.106              return_id->username = strdup(stored_ident->username);
   1.107      }
   1.108 +    
   1.109 +    return_id->me = stored_ident->me;
   1.110 +    
   1.111 +    // FIXME: Do we ALWAYS do this? We probably should...
   1.112 +    if (!return_id->user_id)
   1.113 +        return_id->user_id = strdup(stored_ident->user_id);
   1.114          
   1.115      // Call set_identity() to store
   1.116      if ((is_identity_default || is_user_default) &&
   1.117 @@ -476,6 +487,7 @@
   1.118                                  }
   1.119                                      
   1.120                                  free(this_uid);
   1.121 +                                this_uid = NULL;
   1.122                                  
   1.123                                  // Reflect the change we just made to the DB
   1.124                                  this_id->user_id = strdup(identity->user_id);
   1.125 @@ -841,6 +853,8 @@
   1.126          else {
   1.127              DEBUG_LOG("Generating key pair", "debug", identity->address);
   1.128  
   1.129 +            free(identity->fpr);
   1.130 +            identity->fpr = NULL;
   1.131              status = generate_keypair(session, identity);
   1.132              assert(status != PEP_OUT_OF_MEMORY);
   1.133