netpgp : pgp_find_keys + some small fixes
authorEdouard Tisserant
Thu, 07 May 2015 18:54:13 +0200
changeset 254710ff44d3cf0
parent 253 162d1a585cf8
child 255 813b795312b1
netpgp : pgp_find_keys + some small fixes
src/pgp_netpgp.c
     1.1 --- a/src/pgp_netpgp.c	Wed May 06 12:06:26 2015 +0200
     1.2 +++ b/src/pgp_netpgp.c	Thu May 07 18:54:13 2015 +0200
     1.3 @@ -20,13 +20,8 @@
     1.4  #define PEP_NETPGP_DEBUG
     1.5  
     1.6  static netpgp_t netpgp;
     1.7 -static pthread_mutex_t netpgp_mutex = PTHREAD_MUTEX_INITIALIZER;
     1.8 -static pthread_mutex_t curl_mutex = PTHREAD_MUTEX_INITIALIZER;
     1.9 -
    1.10 -
    1.11 -
    1.12 -PEP_STATUS enter_curl(PEP_SESSION session, CURL **curl){
    1.13 -}
    1.14 +static pthread_mutex_t netpgp_mutex;
    1.15 +static pthread_mutex_t curl_mutex;
    1.16  
    1.17  PEP_STATUS pgp_init(PEP_SESSION session, bool in_first)
    1.18  {
    1.19 @@ -661,7 +656,6 @@
    1.20      char newid[1024];
    1.21      const char *hashalg;
    1.22      const char *cipher;
    1.23 -    char *fprstr = NULL;
    1.24  
    1.25      assert(session);
    1.26      assert(identity);
    1.27 @@ -698,25 +692,24 @@
    1.28  
    1.29      // TODO "Expire-Date: 1y\n";
    1.30  
    1.31 -    fpr_to_str(&fprstr,
    1.32 -               newkey.sigfingerprint.fingerprint,
    1.33 -               newkey.sigfingerprint.length);
    1.34 -    if (fprstr == NULL) {
    1.35 -        result = PEP_OUT_OF_MEMORY;
    1.36 -        goto free_newkey;
    1.37 -    } 
    1.38  
    1.39      result = import_key_or_keypair(&netpgp, &newkey);
    1.40  
    1.41      if (result == PEP_STATUS_OK) {
    1.42 +        char *fprstr = NULL;
    1.43 +        fpr_to_str(&fprstr,
    1.44 +                   newkey.sigfingerprint.fingerprint,
    1.45 +                   newkey.sigfingerprint.length);
    1.46 +        if (fprstr == NULL) {
    1.47 +            result = PEP_OUT_OF_MEMORY;
    1.48 +            goto free_newkey;
    1.49 +        } 
    1.50          identity->fpr = fprstr;
    1.51          /* free nothing, everything transfered */
    1.52          result = PEP_STATUS_OK;
    1.53          goto unlock_netpgp;
    1.54      }
    1.55  
    1.56 -free_fprstr:
    1.57 -    free(fprstr);
    1.58  free_newkey:
    1.59      pgp_key_free(&newkey);
    1.60  unlock_netpgp:
    1.61 @@ -729,7 +722,6 @@
    1.62  {
    1.63      uint8_t fpr[PGP_FINGERPRINT_SIZE];
    1.64      size_t length;
    1.65 -    unsigned res;
    1.66  
    1.67      PEP_STATUS result;
    1.68  
    1.69 @@ -859,10 +851,9 @@
    1.70      }
    1.71  
    1.72      if (str_to_fpr(fprstr, fpr, &fprlen)) {
    1.73 -        unsigned from = 0;
    1.74          if ((key = (pgp_key_t *)pgp_getkeybyfpr(netpgp.io, netpgp.pubring, 
    1.75                                                  fpr, fprlen,
    1.76 -                                                &from, NULL)) == NULL) {
    1.77 +                                                NULL)) == NULL) {
    1.78              result = PEP_KEY_NOT_FOUND;
    1.79              goto unlock_netpgp;
    1.80          }
    1.81 @@ -932,39 +923,106 @@
    1.82      return PEP_STATUS_OK;
    1.83  }
    1.84  
    1.85 +PEP_STATUS add_key_fpr_to_stringlist(stringlist_t **keylist, pgp_key_t *key)
    1.86 +{
    1.87 +    char *newfprstr = NULL;
    1.88 +
    1.89 +    fpr_to_str(&newfprstr,
    1.90 +               key->sigfingerprint.fingerprint,
    1.91 +               key->sigfingerprint.length);
    1.92 +
    1.93 +    if (newfprstr == NULL) {
    1.94 +        return PEP_OUT_OF_MEMORY;
    1.95 +    } else { 
    1.96 +
    1.97 +        *keylist = stringlist_add(*keylist, newfprstr);
    1.98 +        if (*keylist == NULL) {
    1.99 +            free(newfprstr);
   1.100 +            return PEP_OUT_OF_MEMORY;
   1.101 +        }
   1.102 +    }
   1.103 +    return PEP_STATUS_OK;
   1.104 +}
   1.105 +
   1.106  PEP_STATUS pgp_find_keys(
   1.107      PEP_SESSION session, const char *pattern, stringlist_t **keylist
   1.108      )
   1.109  {
   1.110 -    stringlist_t *_keylist;
   1.111 -    char *fpr;
   1.112 +    stringlist_t *_keylist, *_k;
   1.113 +    uint8_t fpr[PGP_FINGERPRINT_SIZE];
   1.114 +    size_t length;
   1.115 +    pgp_key_t *key;
   1.116 +    char *newfprstr = NULL;
   1.117 +
   1.118 +    PEP_STATUS result;
   1.119  
   1.120      assert(session);
   1.121      assert(pattern);
   1.122      assert(keylist);
   1.123  
   1.124 -    *keylist = NULL;
   1.125 +    if (!session || !pattern || !keylist )
   1.126 +        return PEP_UNKNOWN_ERROR;
   1.127  
   1.128 -    /* Ask for key */
   1.129 +    if(pthread_mutex_lock(&netpgp_mutex)){;
   1.130          return PEP_UNKNOWN_ERROR;
   1.131 -        return PEP_GET_KEY_FAILED;
   1.132 +    }
   1.133 +
   1.134 +    *keylist = NULL;
   1.135 +    _keylist = new_stringlist(NULL);
   1.136 +    if (_k == NULL) {
   1.137 +        result = PEP_OUT_OF_MEMORY;
   1.138 +        goto unlock_netpgp;
   1.139 +    }
   1.140 +    _k = _keylist;
   1.141 +
   1.142 +    result = PEP_STATUS_OK;
   1.143  
   1.144 -    _keylist = new_stringlist(NULL);
   1.145 -    stringlist_t *_k = _keylist;
   1.146 +    // Try find a fingerprint in pattern
   1.147 +    if (str_to_fpr(pattern, fpr, &length)) {
   1.148 +
   1.149 +        // Only one fingerprint can match
   1.150 +        if ((key = (pgp_key_t *)pgp_getkeybyfpr(
   1.151 +                        netpgp.io,
   1.152 +                        (pgp_keyring_t *)netpgp.pubring, 
   1.153 +                        (const uint8_t *)fpr, length,
   1.154 +                        NULL)) == NULL) {
   1.155 +
   1.156 +            result = PEP_KEY_NOT_FOUND;
   1.157 +            goto unlock_netpgp;
   1.158 +        }
   1.159  
   1.160 -    do {
   1.161 -            fpr = "TODO key->subkeys->fpr";
   1.162 -            assert(fpr);
   1.163 -            _k = stringlist_add(_k, fpr);
   1.164 -            assert(_k);
   1.165 -            if (_k == NULL){
   1.166 -                free_stringlist(_keylist);
   1.167 -                return PEP_OUT_OF_MEMORY;
   1.168 -            }
   1.169 -    } while (0);
   1.170 +        result = add_key_fpr_to_stringlist(&_k, key);
   1.171 +
   1.172 +    } else {
   1.173 +        // Search by name for pattern. Can match many.
   1.174 +        unsigned from = 0;
   1.175 +        while((key = (pgp_key_t *)pgp_getnextkeybyname(
   1.176 +                        netpgp.io,
   1.177 +                        (pgp_keyring_t *)netpgp.pubring, 
   1.178 +			            (const char *)pattern,
   1.179 +                        &from)) != NULL) {
   1.180 +
   1.181 +            result = add_key_fpr_to_stringlist(&_k, key);
   1.182 +            if (result != PEP_STATUS_OK)
   1.183 +                goto free_keylist;
   1.184  
   1.185 -    *keylist = _keylist;
   1.186 -    return PEP_STATUS_OK;
   1.187 +            from++;
   1.188 +        }
   1.189 +    }
   1.190 +
   1.191 +    if (result == PEP_STATUS_OK) {
   1.192 +        *keylist = _keylist;
   1.193 +        // Transfer ownership, no free
   1.194 +        goto unlock_netpgp;
   1.195 +    }
   1.196 +
   1.197 +free_keylist:
   1.198 +    free_stringlist(_keylist);
   1.199 +
   1.200 +unlock_netpgp:
   1.201 +    pthread_mutex_unlock(&netpgp_mutex);
   1.202 +
   1.203 +    return result;
   1.204  }
   1.205  
   1.206  PEP_STATUS pgp_send_key(PEP_SESSION session, const char *pattern)