Moved myself() own key election to a separate function
authorEdouard Tisserant
Wed, 29 Jun 2016 17:31:50 +0200
changeset 774bc90d5bf74d5
parent 773 26b63b76518e
child 775 af16c9faedf2
Moved myself() own key election to a separate function
src/keymanagement.c
     1.1 --- a/src/keymanagement.c	Wed Jun 29 17:23:09 2016 +0200
     1.2 +++ b/src/keymanagement.c	Wed Jun 29 17:31:50 2016 +0200
     1.3 @@ -56,7 +56,7 @@
     1.4      return ai == fpras && bi == fprbs;
     1.5  }
     1.6  
     1.7 -PEP_STATUS elect_key(
     1.8 +PEP_STATUS elect_pubkey(
     1.9          PEP_SESSION session, pEp_identity * identity
    1.10      )
    1.11  {
    1.12 @@ -165,7 +165,7 @@
    1.13              if (identity->fpr == NULL)
    1.14                  return PEP_OUT_OF_MEMORY;
    1.15              if (_comm_type_key < PEP_ct_unconfirmed_encryption) {
    1.16 -                PEP_STATUS status = elect_key(session, identity);
    1.17 +                PEP_STATUS status = elect_pubkey(session, identity);
    1.18                  if (status != PEP_STATUS_OK)
    1.19                      return status;
    1.20              }
    1.21 @@ -214,7 +214,7 @@
    1.22              identity->comm_type = _comm_type_key;
    1.23          }
    1.24          else /* EMPTYSTR(identity->fpr) */ {
    1.25 -            PEP_STATUS status = elect_key(session, identity);
    1.26 +            PEP_STATUS status = elect_pubkey(session, identity);
    1.27              if (status != PEP_STATUS_OK)
    1.28                  return status;
    1.29          }
    1.30 @@ -252,6 +252,81 @@
    1.31      return status;
    1.32  }
    1.33  
    1.34 +PEP_STATUS elect_ownkey(
    1.35 +        PEP_SESSION session, pEp_identity * identity
    1.36 +    )
    1.37 +{
    1.38 +    PEP_STATUS status;
    1.39 +    stringlist_t *keylist = NULL;
    1.40 +
    1.41 +    free(identity->fpr);
    1.42 +    identity->fpr = NULL;
    1.43 +
    1.44 +    status = find_keys(session, identity->address, &keylist);
    1.45 +    assert(status != PEP_OUT_OF_MEMORY);
    1.46 +    if (status == PEP_OUT_OF_MEMORY)
    1.47 +        return PEP_OUT_OF_MEMORY;
    1.48 +    
    1.49 +    if (keylist != NULL && keylist->value != NULL)
    1.50 +    {
    1.51 +        char *_fpr = NULL;
    1.52 +        identity->comm_type = PEP_ct_unknown;
    1.53 +
    1.54 +        stringlist_t *_keylist;
    1.55 +        for (_keylist = keylist; _keylist && _keylist->value; _keylist = _keylist->next) {
    1.56 +            bool is_own = false;
    1.57 +            
    1.58 +            if (session->use_only_own_private_keys)
    1.59 +            {
    1.60 +                status = own_key_is_listed(session, _keylist->value, &is_own);
    1.61 +                assert(status == PEP_STATUS_OK);
    1.62 +                if (status != PEP_STATUS_OK) {
    1.63 +                    free_stringlist(keylist);
    1.64 +                    return status;
    1.65 +                }
    1.66 +            }
    1.67 +
    1.68 +            // TODO : also accept synchronized device group keys ?
    1.69 +            
    1.70 +            if (!session->use_only_own_private_keys || is_own)
    1.71 +            {
    1.72 +                PEP_comm_type _comm_type_key;
    1.73 +                
    1.74 +                status = get_key_rating(session, _keylist->value, &_comm_type_key);
    1.75 +                assert(status != PEP_OUT_OF_MEMORY);
    1.76 +                if (status == PEP_OUT_OF_MEMORY) {
    1.77 +                    free_stringlist(keylist);
    1.78 +                    return PEP_OUT_OF_MEMORY;
    1.79 +                }
    1.80 +                
    1.81 +                if (_comm_type_key != PEP_ct_compromized &&
    1.82 +                    _comm_type_key != PEP_ct_unknown)
    1.83 +                {
    1.84 +                    if (identity->comm_type == PEP_ct_unknown ||
    1.85 +                        _comm_type_key > identity->comm_type)
    1.86 +                    {
    1.87 +                        identity->comm_type = _comm_type_key;
    1.88 +                        _fpr = _keylist->value;
    1.89 +                    }
    1.90 +                }
    1.91 +            }
    1.92 +        }
    1.93 +        
    1.94 +        if (_fpr)
    1.95 +        {
    1.96 +            identity->fpr = strdup(_fpr);
    1.97 +            assert(identity->fpr);
    1.98 +            if (identity->fpr == NULL)
    1.99 +            {
   1.100 +                free_stringlist(keylist);
   1.101 +                return PEP_OUT_OF_MEMORY;
   1.102 +            }
   1.103 +        }
   1.104 +        free_stringlist(keylist);
   1.105 +    }
   1.106 +    return PEP_STATUS_OK;
   1.107 +}
   1.108 +
   1.109  DYNAMIC_API PEP_STATUS myself(PEP_SESSION session, pEp_identity * identity)
   1.110  {
   1.111      pEp_identity *stored_identity;
   1.112 @@ -314,72 +389,10 @@
   1.113      }
   1.114      else
   1.115      {
   1.116 -        stringlist_t *keylist = NULL;
   1.117 -
   1.118 -        free(identity->fpr);
   1.119 -        identity->fpr = NULL;
   1.120 -
   1.121 -        status = find_keys(session, identity->address, &keylist);
   1.122 -        assert(status != PEP_OUT_OF_MEMORY);
   1.123 -        if (status == PEP_OUT_OF_MEMORY)
   1.124 -            return PEP_OUT_OF_MEMORY;
   1.125 -        
   1.126 -        if (keylist != NULL && keylist->value != NULL)
   1.127 -        {
   1.128 -            char *_fpr = NULL;
   1.129 -            identity->comm_type = PEP_ct_unknown;
   1.130 -
   1.131 -            stringlist_t *_keylist;
   1.132 -            for (_keylist = keylist; _keylist && _keylist->value; _keylist = _keylist->next) {
   1.133 -                bool is_own = false;
   1.134 -                
   1.135 -                if (session->use_only_own_private_keys)
   1.136 -                {
   1.137 -                    status = own_key_is_listed(session, _keylist->value, &is_own);
   1.138 -                    assert(status == PEP_STATUS_OK);
   1.139 -                    if (status != PEP_STATUS_OK) {
   1.140 -                        free_stringlist(keylist);
   1.141 -                        return status;
   1.142 -                    }
   1.143 -                }
   1.144 -
   1.145 -                // TODO : also accept synchronized device group keys ?
   1.146 -                
   1.147 -                if (!session->use_only_own_private_keys || is_own)
   1.148 -                {
   1.149 -                    PEP_comm_type _comm_type_key;
   1.150 -                    
   1.151 -                    status = get_key_rating(session, _keylist->value, &_comm_type_key);
   1.152 -                    assert(status != PEP_OUT_OF_MEMORY);
   1.153 -                    if (status == PEP_OUT_OF_MEMORY) {
   1.154 -                        free_stringlist(keylist);
   1.155 -                        return PEP_OUT_OF_MEMORY;
   1.156 -                    }
   1.157 -                    
   1.158 -                    if (_comm_type_key != PEP_ct_compromized &&
   1.159 -                        _comm_type_key != PEP_ct_unknown)
   1.160 -                    {
   1.161 -                        if (identity->comm_type == PEP_ct_unknown ||
   1.162 -                            _comm_type_key > identity->comm_type)
   1.163 -                        {
   1.164 -                            identity->comm_type = _comm_type_key;
   1.165 -                            _fpr = _keylist->value;
   1.166 -                        }
   1.167 -                    }
   1.168 -                }
   1.169 -            }
   1.170 -            
   1.171 -            if (_fpr)
   1.172 -            {
   1.173 -                identity->fpr = strdup(_fpr);
   1.174 -                assert(identity->fpr);
   1.175 -                if (identity->fpr == NULL)
   1.176 -                {
   1.177 -                    free_stringlist(keylist);
   1.178 -                    return PEP_OUT_OF_MEMORY;
   1.179 -                }
   1.180 -            }
   1.181 -            free_stringlist(keylist);
   1.182 +        status = elect_ownkey(session, identity);
   1.183 +        assert(status == PEP_STATUS_OK);
   1.184 +        if (status != PEP_STATUS_OK) {
   1.185 +            return status;
   1.186          }
   1.187      }
   1.188