...
authorvb
Tue, 05 Aug 2014 20:33:20 +0200
changeset 14c99db0b8fc0f
parent 10 ead888e73384
child 15 98a6c1cc9160
...
src/keymanagement.c
src/keymanagement.h
src/pEpEngine.c
src/pEpEngine.h
test/pEpEngineTest.cc
     1.1 --- a/src/keymanagement.c	Thu Jul 31 10:15:09 2014 +0200
     1.2 +++ b/src/keymanagement.c	Tue Aug 05 20:33:20 2014 +0200
     1.3 @@ -37,30 +37,32 @@
     1.4      if (status == PEP_OUT_OF_MEMORY)
     1.5          return PEP_OUT_OF_MEMORY;
     1.6  
     1.7 -    PEP_comm_type _comm_type_key;
     1.8 -    status = get_key_rating(session, stored_identity->fpr, &_comm_type_key);
     1.9 -    assert(status != PEP_OUT_OF_MEMORY);
    1.10 -    if (status == PEP_OUT_OF_MEMORY)
    1.11 -        return PEP_OUT_OF_MEMORY;
    1.12 +    if (stored_identity) {
    1.13 +        PEP_comm_type _comm_type_key;
    1.14 +        status = get_key_rating(session, stored_identity->fpr, &_comm_type_key);
    1.15 +        assert(status != PEP_OUT_OF_MEMORY);
    1.16 +        if (status == PEP_OUT_OF_MEMORY)
    1.17 +            return PEP_OUT_OF_MEMORY;
    1.18  
    1.19 -    if (stored_identity) {
    1.20          if (EMPTY(identity->fpr)) {
    1.21              identity->fpr = strdup(stored_identity->fpr);
    1.22              assert(identity->fpr);
    1.23              if (identity->fpr == NULL)
    1.24                  return PEP_OUT_OF_MEMORY;
    1.25              identity->fpr_size = stored_identity->address_size;
    1.26 +            if (_comm_type_key < PEP_ct_unconfirmed_encryption) {
    1.27 +                identity->comm_type = _comm_type_key;
    1.28 +            }
    1.29 +            else {
    1.30 +                identity->comm_type = stored_identity->comm_type;
    1.31 +            }
    1.32          }
    1.33          else /* !EMPTY(identity->fpr) */ {
    1.34 -            stringlist_t *keylist;
    1.35 -
    1.36 -            status = find_keys(session, identity->fpr, &keylist);
    1.37 -            assert(status != PEP_OUT_OF_MEMORY);
    1.38 -            if (status == PEP_OUT_OF_MEMORY)
    1.39 -                return PEP_OUT_OF_MEMORY;
    1.40 -
    1.41 -            if (keylist && keylist->value) {
    1.42 -                if (identity->comm_type == PEP_ct_unknown) {
    1.43 +            if (_comm_type_key != PEP_ct_unknown) {
    1.44 +                if (_comm_type_key < PEP_ct_unconfirmed_encryption) {
    1.45 +                    identity->comm_type = _comm_type_key;
    1.46 +                }
    1.47 +                else if (identity->comm_type == PEP_ct_unknown) {
    1.48                      if (strcmp(identity->fpr, stored_identity->fpr) == 0) {
    1.49                          identity->comm_type = stored_identity->comm_type;
    1.50                      }
    1.51 @@ -74,8 +76,6 @@
    1.52              }
    1.53              else
    1.54                  identity->comm_type = PEP_ct_unknown;
    1.55 -
    1.56 -            free_stringlist(keylist);
    1.57          }
    1.58  
    1.59          if (EMPTY(identity->username)) {
    1.60 @@ -103,21 +103,7 @@
    1.61          }
    1.62      }
    1.63      else /* stored_identity == NULL */ {
    1.64 -        if (identity->fpr && identity->user_id) {
    1.65 -            if (identity->comm_type == PEP_ct_unknown) {
    1.66 -                status = get_trust(session, identity);
    1.67 -                assert(status != PEP_OUT_OF_MEMORY);
    1.68 -                if (status == PEP_OUT_OF_MEMORY)
    1.69 -                    return PEP_OUT_OF_MEMORY;
    1.70 -            }
    1.71 -            if (identity->comm_type != PEP_ct_unknown && EMPTY(identity->username)) {
    1.72 -                free(identity->username);
    1.73 -                identity->username = strdup("anonymous");
    1.74 -                identity->username_size = 10;
    1.75 -            }
    1.76 -        }
    1.77 -        else
    1.78 -            identity->comm_type = PEP_ct_unknown;
    1.79 +        identity->comm_type = PEP_ct_unknown;
    1.80      }
    1.81  
    1.82      status = PEP_STATUS_OK;
    1.83 @@ -130,63 +116,6 @@
    1.84      return status;
    1.85  }
    1.86  
    1.87 -DYNAMIC_API PEP_STATUS outgoing_comm_type(
    1.88 -        PEP_SESSION session,
    1.89 -        const stringlist_t *addresses,
    1.90 -        PEP_comm_type *comm_type
    1.91 -    )
    1.92 -{
    1.93 -    const stringlist_t *l;
    1.94 -
    1.95 -    assert(session);
    1.96 -    assert(addresses);
    1.97 -    assert(addresses->value);
    1.98 -    assert(comm_type);
    1.99 -
   1.100 -    *comm_type = PEP_ct_unknown;
   1.101 -
   1.102 -    for (l=addresses; l && l->value; l = l->next) {
   1.103 -        PEP_STATUS _status;
   1.104 -        pEp_identity *identity;
   1.105 -
   1.106 -        _status = get_identity(session, l->value, &identity);
   1.107 -        assert(_status != PEP_OUT_OF_MEMORY);
   1.108 -
   1.109 -        if (identity == NULL) {
   1.110 -            *comm_type = PEP_ct_no_encryption;
   1.111 -            return PEP_STATUS_OK;
   1.112 -        }
   1.113 -        else if (identity->comm_type == PEP_ct_unknown) {
   1.114 -            *comm_type = PEP_ct_no_encryption;
   1.115 -            free_identity(identity);
   1.116 -            return PEP_STATUS_OK;
   1.117 -        }
   1.118 -        else if (*comm_type == PEP_ct_unknown) {
   1.119 -            *comm_type = identity->comm_type;
   1.120 -        }
   1.121 -        else if (*comm_type != identity->comm_type) {
   1.122 -            PEP_comm_type min = MIN(*comm_type, identity->comm_type);
   1.123 -            if (min < PEP_ct_unconfirmed_encryption) {
   1.124 -                *comm_type = PEP_ct_no_encryption;
   1.125 -                free_identity(identity);
   1.126 -                return PEP_STATUS_OK;
   1.127 -            }
   1.128 -            else if (min < PEP_ct_unconfirmed_enc_anon)
   1.129 -                *comm_type = PEP_ct_unconfirmed_encryption;
   1.130 -            else if (min < PEP_ct_confirmed_encryption)
   1.131 -                *comm_type = PEP_ct_unconfirmed_enc_anon;
   1.132 -            else if (min < PEP_ct_confirmed_enc_anon)
   1.133 -                *comm_type = PEP_ct_confirmed_encryption;
   1.134 -            else
   1.135 -                *comm_type = PEP_ct_confirmed_enc_anon;
   1.136 -        }
   1.137 -
   1.138 -        free_identity(identity);
   1.139 -    }
   1.140 -
   1.141 -    return PEP_STATUS_OK;
   1.142 -}
   1.143 -
   1.144  DYNAMIC_API PEP_STATUS myself(PEP_SESSION session, pEp_identity * identity)
   1.145  {
   1.146      PEP_STATUS status;
     2.1 --- a/src/keymanagement.h	Thu Jul 31 10:15:09 2014 +0200
     2.2 +++ b/src/keymanagement.h	Tue Aug 05 20:33:20 2014 +0200
     2.3 @@ -20,21 +20,6 @@
     2.4      );
     2.5  
     2.6  
     2.7 -// outgoing_comm_type() - minimum comm_type for a list of addresses
     2.8 -//
     2.9 -//  parameters:
    2.10 -//      session (in)        session to use
    2.11 -//      addresses (in)      list of addresses
    2.12 -//      comm_type (out)     comm_type which can be used to communicate with
    2.13 -//                          addresses
    2.14 -
    2.15 -DYNAMIC_API PEP_STATUS outgoing_comm_type(
    2.16 -        PEP_SESSION session,
    2.17 -        const stringlist_t *addresses,
    2.18 -        PEP_comm_type *comm_type
    2.19 -    );
    2.20 -
    2.21 -
    2.22  // myself() - ensures that the own identity is being complete
    2.23  //
    2.24  //  parameters:
     3.1 --- a/src/pEpEngine.c	Thu Jul 31 10:15:09 2014 +0200
     3.2 +++ b/src/pEpEngine.c	Tue Aug 05 20:33:20 2014 +0200
     3.3 @@ -749,7 +749,7 @@
     3.4  		switch (gpgme_error) {
     3.5  		case GPG_ERR_NO_ERROR:
     3.6  			{
     3.7 -				gpgme_verify_result_t gpgme_verify_result;
     3.8 +                gpgme_verify_result_t gpgme_verify_result;
     3.9                  char *_buffer = NULL;
    3.10  				size_t reading;
    3.11                  size_t length = _session->gpgme_data_seek(plain, 0, SEEK_END);
    3.12 @@ -837,8 +837,34 @@
    3.13  			break;
    3.14  		case GPG_ERR_BAD_PASSPHRASE:
    3.15  			NOT_IMPLEMENTED;
    3.16 -		default:
    3.17 -			result = PEP_CANNOT_DECRYPT_UNKNOWN;
    3.18 +        default:
    3.19 +            {
    3.20 +                gpgme_decrypt_result_t gpgme_decrypt_result = _session->gpgme_op_decrypt_result(_session->ctx);
    3.21 +                result = PEP_CANNOT_DECRYPT_UNKNOWN;
    3.22 +
    3.23 +                if (gpgme_decrypt_result != NULL) {
    3.24 +                    *keylist = new_stringlist(gpgme_decrypt_result->unsupported_algorithm);
    3.25 +                    assert(*keylist);
    3.26 +                    if (*keylist == NULL) {
    3.27 +                        result = PEP_OUT_OF_MEMORY;
    3.28 +                        break;
    3.29 +                    }
    3.30 +                    
    3.31 +                    stringlist_t *_keylist = *keylist;
    3.32 +                    for (gpgme_recipient_t r = gpgme_decrypt_result->recipients; r != NULL; r = r->next) {
    3.33 +                        _keylist = stringlist_add(_keylist, r->keyid);
    3.34 +                        assert(_keylist);
    3.35 +                        if (_keylist == NULL) {
    3.36 +                            free_stringlist(*keylist);
    3.37 +                            *keylist = NULL;
    3.38 +                            result = PEP_OUT_OF_MEMORY;
    3.39 +                            break;
    3.40 +                        }
    3.41 +                    }
    3.42 +                    if (result == PEP_OUT_OF_MEMORY)
    3.43 +                        break;
    3.44 +                }
    3.45 +            }
    3.46  		}
    3.47  		break;
    3.48  
    3.49 @@ -1954,6 +1980,11 @@
    3.50      gpgme_error = _session->gpgme_op_keylist_next(_session->ctx, &key);
    3.51      assert(gpgme_error != GPG_ERR_INV_VALUE);
    3.52  
    3.53 +    if (key == NULL) {
    3.54 +        _session->gpgme_op_keylist_end(_session->ctx);
    3.55 +        return PEP_KEY_NOT_FOUND;
    3.56 +    }
    3.57 +
    3.58      switch (key->protocol) {
    3.59      case GPGME_PROTOCOL_OpenPGP:
    3.60      case GPGME_PROTOCOL_DEFAULT:
    3.61 @@ -2019,4 +2050,4 @@
    3.62      _session->gpgme_op_keylist_end(_session->ctx);
    3.63  
    3.64      return status;
    3.65 -}
    3.66 \ No newline at end of file
    3.67 +}
     4.1 --- a/src/pEpEngine.h	Thu Jul 31 10:15:09 2014 +0200
     4.2 +++ b/src/pEpEngine.h	Tue Aug 05 20:33:20 2014 +0200
     4.3 @@ -599,9 +599,10 @@
     4.4  //      identity (inout)        user_id and fpr to check as UTF-8 strings (in)
     4.5  //                              user_id and comm_type as result (out)
     4.6  //
     4.7 -//  this function modifies the given identity struct; the struct remains in the
     4.8 -//  ownership of the caller
     4.9 -//  if the trust level cannot be determined identity->comm_type is set to PEP_ct_unknown
    4.10 +//  this function modifies the given identity struct; the struct remains in
    4.11 +// the ownership of the caller
    4.12 +//  if the trust level cannot be determined identity->comm_type is set
    4.13 +//  to PEP_ct_unknown
    4.14  
    4.15  DYNAMIC_API PEP_STATUS get_trust(PEP_SESSION session, pEp_identity *identity);
    4.16  
    4.17 @@ -613,13 +614,13 @@
    4.18  //      fpr (in)                unique identifyer for key as UTF-8 string
    4.19  //      comm_type (out)         key rating
    4.20  //
    4.21 -//  if an error occurs, *comm_type is set to PEP_ct_unknown and an error status
    4.22 +//  if an error occurs, *comm_type is set to PEP_ct_unknown and an error
    4.23  //  is returned
    4.24  
    4.25  DYNAMIC_API PEP_STATUS get_key_rating(
    4.26 -    PEP_SESSION session,
    4.27 -    const char *fpr,
    4.28 -    PEP_comm_type *comm_type
    4.29 +        PEP_SESSION session,
    4.30 +        const char *fpr,
    4.31 +        PEP_comm_type *comm_type
    4.32      );
    4.33  
    4.34  
     5.1 --- a/test/pEpEngineTest.cc	Thu Jul 31 10:15:09 2014 +0200
     5.2 +++ b/test/pEpEngineTest.cc	Tue Aug 05 20:33:20 2014 +0200
     5.3 @@ -184,25 +184,6 @@
     5.4  
     5.5      free_identity(identity);
     5.6  
     5.7 -    // testing key management
     5.8 -
     5.9 -    stringlist_t *addresses = new_stringlist("leon.schumacher@digitalekho.com");
    5.10 -    PEP_comm_type comm_type;
    5.11 -    cout << "\nretrieving communication type for leon.schumacher@digitalekho.com\n";
    5.12 -    PEP_STATUS oct_result = outgoing_comm_type(session, addresses, &comm_type);
    5.13 -    cout << "communication type is " << comm_type << "\n";
    5.14 -    free_stringlist(addresses);
    5.15 -    assert(oct_result == PEP_STATUS_OK && comm_type == PEP_ct_pEp);
    5.16 -
    5.17 -    stringlist_t *addresses2 = new_stringlist("leon.schumacher@digitalekho.com");
    5.18 -    stringlist_add(addresses2, "this.email@is.invalid");
    5.19 -    cout << "\nretrieving communication type for an unknown address\n";
    5.20 -    oct_result = outgoing_comm_type(session, addresses2, &comm_type);
    5.21 -    cout << "communication type is " << comm_type << "\n";
    5.22 -    cout << "status is " << oct_result << "\n";
    5.23 -    free_stringlist(addresses2);
    5.24 -    assert(oct_result == PEP_STATUS_OK && comm_type == PEP_ct_no_encryption);
    5.25 -
    5.26      cout << "\ngenerating key for testuser\n";
    5.27      identity = new_identity(
    5.28              "testuser@pibit.ch",