src/pEpEngine.c
changeset 9 41e66a54f03e
parent 8 26cc9f0228f4
child 10 ead888e73384
     1.1 --- a/src/pEpEngine.c	Fri Jul 11 17:43:11 2014 +0200
     1.2 +++ b/src/pEpEngine.c	Wed Jul 30 16:02:49 2014 +0200
     1.3 @@ -1921,4 +1921,101 @@
     1.4  
     1.5      sqlite3_reset(_session->get_trust);
     1.6      return status;
     1.7 +}
     1.8 +
     1.9 +DYNAMIC_API PEP_STATUS get_key_rating(
    1.10 +    PEP_SESSION session,
    1.11 +    const char *fpr,
    1.12 +    PEP_comm_type *comm_type
    1.13 +    )
    1.14 +{
    1.15 +    pEpSession *_session = (pEpSession *) session;
    1.16 +    PEP_STATUS status = PEP_STATUS_OK;
    1.17 +    gpgme_error_t gpgme_error;
    1.18 +    gpgme_key_t key;
    1.19 +
    1.20 +    assert(session);
    1.21 +    assert(fpr);
    1.22 +    assert(comm_type);
    1.23 +    
    1.24 +    *comm_type = PEP_ct_unknown;
    1.25 +
    1.26 +    gpgme_error = _session->gpgme_op_keylist_start(_session->ctx, fpr, 0);
    1.27 +    switch (gpgme_error) {
    1.28 +    case GPG_ERR_NO_ERROR:
    1.29 +        break;
    1.30 +    case GPG_ERR_INV_VALUE:
    1.31 +        assert(0);
    1.32 +        return PEP_UNKNOWN_ERROR;
    1.33 +    default:
    1.34 +        return PEP_GET_KEY_FAILED;
    1.35 +    };
    1.36 +
    1.37 +    gpgme_error = _session->gpgme_op_keylist_next(_session->ctx, &key);
    1.38 +    assert(gpgme_error != GPG_ERR_INV_VALUE);
    1.39 +
    1.40 +    switch (key->protocol) {
    1.41 +    case GPGME_PROTOCOL_OpenPGP:
    1.42 +    case GPGME_PROTOCOL_DEFAULT:
    1.43 +        *comm_type = PEP_ct_OpenPGP_unconfirmed;
    1.44 +        break;
    1.45 +    case GPGME_PROTOCOL_CMS:
    1.46 +        *comm_type = PEP_ct_CMS_unconfirmed;
    1.47 +        break;
    1.48 +    default:
    1.49 +        *comm_type = PEP_ct_unknown;
    1.50 +        _session->gpgme_op_keylist_end(_session->ctx);
    1.51 +        return PEP_STATUS_OK;
    1.52 +    }
    1.53 +
    1.54 +    switch (gpgme_error) {
    1.55 +    case GPG_ERR_EOF:
    1.56 +        break;
    1.57 +    case GPG_ERR_NO_ERROR:
    1.58 +        assert(key);
    1.59 +        assert(key->subkeys);
    1.60 +        for (gpgme_subkey_t sk = key->subkeys; sk != NULL; sk = sk->next) {
    1.61 +            if (sk->length < 1024)
    1.62 +                *comm_type = PEP_ct_key_too_short;
    1.63 +            else if (
    1.64 +                (
    1.65 +                       (sk->pubkey_algo == GPGME_PK_RSA)
    1.66 +                    || (sk->pubkey_algo == GPGME_PK_RSA_E)
    1.67 +                    || (sk->pubkey_algo == GPGME_PK_RSA_S)
    1.68 +                )
    1.69 +                && sk->length == 1024
    1.70 +            )
    1.71 +                *comm_type = PEP_ct_OpenPGP_1024_RSA_unconfirmed;
    1.72 +
    1.73 +            if (sk->invalid) {
    1.74 +                *comm_type = PEP_ct_key_b0rken;
    1.75 +                break;
    1.76 +            }
    1.77 +            if (sk->expired) {
    1.78 +                *comm_type = PEP_ct_key_expired;
    1.79 +                break;
    1.80 +            }
    1.81 +            if (sk->revoked) {
    1.82 +                *comm_type = PEP_ct_key_revoked;
    1.83 +                break;
    1.84 +            }
    1.85 +        }
    1.86 +        break;
    1.87 +    case GPG_ERR_ENOMEM:
    1.88 +        _session->gpgme_op_keylist_end(_session->ctx);
    1.89 +        return PEP_OUT_OF_MEMORY;
    1.90 +    default:
    1.91 +        // BUG: GPGME returns an illegal value instead of GPG_ERR_EOF after
    1.92 +        // reading first key
    1.93 +#ifndef NDEBUG
    1.94 +        fprintf(stderr, "warning: unknown result 0x%x of"
    1.95 +            " gpgme_op_keylist_next()\n", gpgme_error);
    1.96 +#endif
    1.97 +        gpgme_error = GPG_ERR_EOF;
    1.98 +        break;
    1.99 +    };
   1.100 +
   1.101 +    _session->gpgme_op_keylist_end(_session->ctx);
   1.102 +
   1.103 +    return status;
   1.104  }
   1.105 \ No newline at end of file