ENGINE-73 #comment underlying netpgp and openpgp variants modified. netpgp now uses find_keys_do, because there's a convenient callback we can slip our modified functionality into. gpgme still uses a separate function, because no such callback mechanism is in there. We may want to add it to avoid duplication and clean this up at a later date. ENGINE-73
authorKrista Grothoff <krista@pep-project.org>
Thu, 18 Aug 2016 14:20:25 +0200
branchENGINE-73
changeset 1031ed718acfd6fe
parent 1030 4354451338cd
child 1032 b4004d33c2a2
ENGINE-73 #comment underlying netpgp and openpgp variants modified. netpgp now uses find_keys_do, because there's a convenient callback we can slip our modified functionality into. gpgme still uses a separate function, because no such callback mechanism is in there. We may want to add it to avoid duplication and clean this up at a later date.
src/pgp_gpg.c
src/pgp_netpgp.c
     1.1 --- a/src/pgp_gpg.c	Thu Aug 18 13:23:15 2016 +0200
     1.2 +++ b/src/pgp_gpg.c	Thu Aug 18 14:20:25 2016 +0200
     1.3 @@ -1225,17 +1225,19 @@
     1.4      return PEP_STATUS_OK;
     1.5  }
     1.6  
     1.7 -PEP_STATUS pgp_list_keys(PEP_SESSION session, identity_list** id_list) {
     1.8 +PEP_STATUS pgp_list_keyinfo(PEP_SESSION session, const char* pattern, 
     1.9 +                            stringpair_list_t** keyinfo_list)
    1.10 +{    
    1.11      gpgme_error_t gpgme_error;
    1.12      assert(session);
    1.13 -    assert(id_list);
    1.14 +    assert(keyinfo_list);
    1.15      
    1.16 -    if (!session || !id_list)
    1.17 +    if (!session || !keyinfo_list)
    1.18          return PEP_ILLEGAL_VALUE;
    1.19      
    1.20 -    *id_list = NULL;
    1.21 +    *keyinfo_list = NULL;
    1.22      
    1.23 -    gpgme_error = gpg.gpgme_op_keylist_start(session->ctx, NULL, 0);
    1.24 +    gpgme_error = gpg.gpgme_op_keylist_start(session->ctx, pattern, 0);
    1.25      gpgme_error = _GPGERR(gpgme_error);
    1.26      
    1.27      switch(gpgme_error) {
    1.28 @@ -1250,9 +1252,9 @@
    1.29      };
    1.30      
    1.31      gpgme_key_t key;
    1.32 -    identity_list* _id_list = new_identity_list(NULL);
    1.33 -    identity_list* list_curr = _id_list;
    1.34 -    pEp_identity* new_id = NULL;
    1.35 +    stringpair_list_t* _keyinfo_list = new_stringpair_list(NULL);
    1.36 +    stringpair_list_t* list_curr = _keyinfo_list;
    1.37 +    stringpair_t* pair = NULL;
    1.38          
    1.39      do {
    1.40          gpgme_error = gpg.gpgme_op_keylist_next(session->ctx, &key);
    1.41 @@ -1269,32 +1271,32 @@
    1.42                 
    1.43                  // first subkey is primary key
    1.44                  char* fpr = key->subkeys->fpr;
    1.45 -                char* primary_email = key->uids->email;
    1.46 -                char* uname = key->uids->name;
    1.47 +//                char* primary_email = key->uids->email;
    1.48 +//                char* uname = key->uids->name;
    1.49                  char* uid = key->uids->uid;
    1.50                  
    1.51                  assert(fpr);
    1.52 -                assert(primary_email);
    1.53 -                if (!fpr || !primary_email)
    1.54 +                assert(uid);
    1.55 +                if (!fpr)
    1.56                      return PEP_GET_KEY_FAILED;
    1.57                  
    1.58 -                new_id = new_identity(primary_email, fpr, uid, uname);
    1.59 +                pair = new_stringpair(fpr, uid);
    1.60  
    1.61 -                assert(new_id);
    1.62 +                assert(pair);
    1.63                  
    1.64 -                if (new_id) {
    1.65 -                    list_curr = identity_list_add(list_curr, new_id);
    1.66 -                    new_id = NULL;
    1.67 +                if (pair) {
    1.68 +                    list_curr = stringpair_list_add(list_curr, pair);
    1.69 +                    pair = NULL;
    1.70                      
    1.71                      assert(list_curr);
    1.72                      if (list_curr != NULL)
    1.73                          break;
    1.74                      else
    1.75 -                        free_identity(new_id);
    1.76 +                        free_stringpair(pair);
    1.77                  }
    1.78 -                // else fallthrough (list_curr or key_email_pair wasn't allocateable)
    1.79 +                // else fallthrough (list_curr or pair wasn't allocateable)
    1.80              case GPG_ERR_ENOMEM:
    1.81 -                free_identity_list(_id_list);
    1.82 +                free_stringpair_list(_keyinfo_list);
    1.83                  gpg.gpgme_op_keylist_end(session->ctx);
    1.84                  return PEP_OUT_OF_MEMORY;
    1.85              default:
    1.86 @@ -1303,12 +1305,12 @@
    1.87          }
    1.88      } while (gpgme_error != GPG_ERR_EOF);
    1.89      
    1.90 -    if (_id_list->ident == NULL) {
    1.91 -        free_identity_list(_id_list);
    1.92 -        _id_list = NULL;
    1.93 +    if (_keyinfo_list->ident == NULL) {
    1.94 +        free_stringpair_list(_keyinfo_list);
    1.95 +        _keyinfo_list = NULL;
    1.96      }
    1.97      
    1.98 -    *id_list = _id_list;
    1.99 +    *keyinfo_list = _keyinfo_list;
   1.100      
   1.101      return PEP_STATUS_OK;
   1.102  }
     2.1 --- a/src/pgp_netpgp.c	Thu Aug 18 13:23:15 2016 +0200
     2.2 +++ b/src/pgp_netpgp.c	Thu Aug 18 14:20:25 2016 +0200
     2.3 @@ -1178,6 +1178,27 @@
     2.4      return PEP_STATUS_OK;
     2.5  }
     2.6  
     2.7 +static PEP_STATUS add_keyinfo_to_stringpair_list(void* arg, pgp_key_t *key) {
     2.8 +    stringpair_list_t** keyinfo_list = (stringpair_list_t**)arg;
     2.9 +    stringpair_t pair = NULL;
    2.10 +    char* id_fpr = NULL;
    2.11 +    char* primary_userid = (char*)pgp_key_get_primary_userid(key);
    2.12 +     
    2.13 +    fpr_to_str(&id_fpr, key->pubkeyfpr.fingerprint,
    2.14 +                key->pubkeyfpr.length);
    2.15 +
    2.16 +    pair = new_stringpair(id_fpr, primary_userid);
    2.17 +    
    2.18 +    if (pair == NULL)
    2.19 +        return PEP_OUT_OF_MEMORY;
    2.20 +    
    2.21 +    *keyinfo_list = stringpair_list_add(*keyinfo_list, pair);
    2.22 +    free(id_fpr);
    2.23 +    if (*keyinfo_list == NULL)
    2.24 +        return PEP_OUT_OF_MEMORY;
    2.25 +    return PEP_STATUS_OK;
    2.26 +}
    2.27 +
    2.28  PEP_STATUS pgp_find_keys(
    2.29      PEP_SESSION session, const char *pattern, stringlist_t **keylist
    2.30      )
    2.31 @@ -1727,37 +1748,11 @@
    2.32      pgp_key_t *key;
    2.33  
    2.34      PEP_STATUS result;
    2.35 -
    2.36 -    result = PEP_KEY_NOT_FOUND;
    2.37      
    2.38 -    // get all available keys
    2.39 -    unsigned n = 0;
    2.40 +    result = find_keys_do(pattern, &add_keyinfo_to_stringpair_list, (void*)keyinfo_list);
    2.41      
    2.42 -    pgp_keyring_t* pubkeys = (pgp_keyring_t *)netpgp.pubring; 
    2.43 -    int keyring_end = pubkeys->keyc;
    2.44 -    
    2.45 -    if (keyring_end < 1)
    2.46 -        return result;
    2.47 -    
    2.48 -    stringpair_list_t* _retval = new_stringpair_list(NULL);
    2.49 -    
    2.50 -    for (key = pubkeys->keys; n < keyring_end; ++n, ++key) {
    2.51 -        assert(key);
    2.52 -        if (!key)
    2.53 -            continue;
    2.54 -        char* primary_userid = (char*)pgp_key_get_primary_userid(key);
    2.55 -//        parse_netpgp_uid_str(primary_userid, &username, &usermail);
    2.56 -     
    2.57 -        char* id_fpr = NULL;
    2.58 -        
    2.59 -        fpr_to_str(&id_fpr, key->pubkeyfpr.fingerprint,
    2.60 -                   key->pubkeyfpr.length);
    2.61 -
    2.62 -        stringlist_add(_retval, new_stringpair(id_fpr, primary_userid));
    2.63 -        free(id_fpr);
    2.64 -        result = PEP_STATUS_OK;
    2.65 -    }
    2.66 -    *id_list = _retval;
    2.67 +    if (!keyinfo_list)
    2.68 +        result = PEP_KEY_NOT_FOUND;
    2.69      
    2.70  unlock_netpgp:
    2.71      pthread_mutex_unlock(&netpgp_mutex);