netpgp : pgp_get_key_rating
authorEdouard Tisserant
Fri, 26 Jun 2015 11:33:37 +0200
changeset 324c34ff8d62156
parent 323 f2d4f0173818
child 325 4055dfe3f987
netpgp : pgp_get_key_rating
src/pgp_netpgp.c
test/pEpEngineTest.cc
     1.1 --- a/src/pgp_netpgp.c	Thu Jun 25 14:30:19 2015 +0200
     1.2 +++ b/src/pgp_netpgp.c	Fri Jun 26 11:33:37 2015 +0200
     1.3 @@ -1362,65 +1362,69 @@
     1.4  
     1.5  PEP_STATUS pgp_get_key_rating(
     1.6      PEP_SESSION session,
     1.7 -    const char *fpr,
     1.8 +    const char *keyidstr,
     1.9      PEP_comm_type *comm_type
    1.10      )
    1.11  {
    1.12 +    const pgp_key_t *key;
    1.13 +    uint8_t keyid[PGP_KEY_ID_SIZE];
    1.14 +    unsigned from = 0;
    1.15 +
    1.16      PEP_STATUS status = PEP_STATUS_OK;
    1.17  
    1.18      assert(session);
    1.19 -    assert(fpr);
    1.20 +    assert(keyidstr);
    1.21      assert(comm_type);
    1.22  
    1.23 -    *comm_type = PEP_ct_unknown;
    1.24 +    if (!session || !keyidstr || !comm_type )
    1.25 +        return PEP_UNKNOWN_ERROR;
    1.26  
    1.27 -    /* TODO get key from fpr */
    1.28 -    return PEP_UNKNOWN_ERROR;
    1.29 -    return PEP_GET_KEY_FAILED;
    1.30 +    if(pthread_mutex_lock(&netpgp_mutex)){
    1.31 +        return PEP_UNKNOWN_ERROR;
    1.32 +    }
    1.33  
    1.34 -    switch (/*TODO key->protocol*/ 4) {
    1.35 -    case /* TODO  OpenPGP */0:
    1.36 -    case /* TODO DEFAULT */1:
    1.37 +    if(!str_to_id(keyid, keyidstr))
    1.38 +    {
    1.39 +        status = PEP_ILLEGAL_VALUE;
    1.40 +        goto unlock_netpgp;
    1.41 +    }
    1.42 +
    1.43 +    key = pgp_getkeybyid(netpgp.io, netpgp.pubring, 
    1.44 +             keyid, &from, NULL, NULL, 
    1.45 +             0, 0); /* accept revoked and expired */
    1.46 +
    1.47 +    if(key == NULL)
    1.48 +    {
    1.49 +        status = PEP_KEY_NOT_FOUND;
    1.50 +        goto unlock_netpgp;
    1.51 +    }
    1.52 +
    1.53 +    switch(pgp_key_get_rating(key)){
    1.54 +	case PGP_VALID:
    1.55          *comm_type = PEP_ct_OpenPGP_unconfirmed;
    1.56          break;
    1.57 -    case /* TODO CMS */2:
    1.58 -        *comm_type = PEP_ct_CMS_unconfirmed;
    1.59 +    case PGP_WEAK:
    1.60 +        *comm_type = PEP_ct_OpenPGP_weak_unconfirmed;
    1.61 +        break;
    1.62 +    case PGP_TOOSHORT:
    1.63 +        *comm_type = PEP_ct_key_too_short;
    1.64 +        break;
    1.65 +	case PGP_INVALID:
    1.66 +        *comm_type = PEP_ct_key_b0rken;
    1.67 +        break;
    1.68 +	case PGP_EXPIRED:
    1.69 +        *comm_type = PEP_ct_key_expired;
    1.70 +        break;
    1.71 +    case PGP_REVOKED:
    1.72 +        *comm_type = PEP_ct_key_revoked;
    1.73          break;
    1.74      default:
    1.75          *comm_type = PEP_ct_unknown;
    1.76 -        return PEP_STATUS_OK;
    1.77 +        break;
    1.78      }
    1.79  
    1.80 -        for (; 1 == 0; /* Each subkeys */ ) {
    1.81 -            if (/* TODO length */0 < 1024)
    1.82 -                *comm_type = PEP_ct_key_too_short;
    1.83 -            else if (
    1.84 -                (
    1.85 -                (   /* TODO pubkey_algo == RSA  */ 0)
    1.86 -                || (/* TODO pubkey_algo == RSA_E*/ 0)
    1.87 -                || (/* TODO pubkey_algo == RSA_S*/ 0)
    1.88 -                )
    1.89 -                && /* sk->length */0 == 1024
    1.90 -                )
    1.91 -                *comm_type = PEP_ct_OpenPGP_weak_unconfirmed;
    1.92 -
    1.93 -            if (/* TODO invalid */ 1) {
    1.94 -                *comm_type = PEP_ct_key_b0rken;
    1.95 -                break;
    1.96 -            }
    1.97 -            if (/* TODO expired */ 1) {
    1.98 -                *comm_type = PEP_ct_key_expired;
    1.99 -                break;
   1.100 -            }
   1.101 -            if (/* TODO revoked*/ 1) {
   1.102 -                *comm_type = PEP_ct_key_revoked;
   1.103 -                break;
   1.104 -            }
   1.105 -        }
   1.106 -        *comm_type = PEP_ct_unknown;
   1.107 -        return PEP_OUT_OF_MEMORY;
   1.108 -        return PEP_UNKNOWN_ERROR;
   1.109 -
   1.110 +unlock_netpgp:
   1.111 +    pthread_mutex_unlock(&netpgp_mutex);
   1.112  
   1.113      return status;
   1.114  }
     2.1 --- a/test/pEpEngineTest.cc	Thu Jun 25 14:30:19 2015 +0200
     2.2 +++ b/test/pEpEngineTest.cc	Fri Jun 26 11:33:37 2015 +0200
     2.3 @@ -275,7 +275,8 @@
     2.4      assert(send_key_status == PEP_STATUS_OK);
     2.5  
     2.6      PEP_comm_type tcomm_type;
     2.7 -    PEP_STATUS tstatus = get_key_rating(session, "DD55BF29DF9B1541", &tcomm_type);
     2.8 +    PEP_STATUS tstatus = get_key_rating(session, "59BFF488C9C2EE39", &tcomm_type);
     2.9 +    cout << "get_key_rating() exits with " << tstatus << "\n";
    2.10      assert(tstatus == PEP_STATUS_OK);
    2.11      assert(tcomm_type == PEP_ct_OpenPGP_unconfirmed);
    2.12