diff --git a/src/keymanagement.c b/src/keymanagement.c index a2f47fe0..dd5a227c 100644 --- a/src/keymanagement.c +++ b/src/keymanagement.c @@ -30,7 +30,7 @@ DYNAMIC_API PEP_STATUS update_identity( assert(session); assert(identity); - assert(identity->address); + assert(!EMPTY(identity->address)); status = get_identity(session, identity->address, &stored_identity); assert(status != PEP_OUT_OF_MEMORY); @@ -78,6 +78,78 @@ DYNAMIC_API PEP_STATUS update_identity( identity->comm_type = PEP_ct_unknown; } + if (identity->lang[0] == 0) { + identity->lang[0] = stored_identity->lang[0]; + identity->lang[1] = stored_identity->lang[1]; + identity->lang[2] = 0; + } + } + else /* stored_identity == NULL */ { + if (!EMPTY(identity->fpr)) { + PEP_comm_type _comm_type_key; + + status = get_key_rating(session, identity->fpr, &_comm_type_key); + assert(status != PEP_OUT_OF_MEMORY); + if (status == PEP_OUT_OF_MEMORY) + return PEP_OUT_OF_MEMORY; + + identity->comm_type = _comm_type_key; + } + else /* EMPTY(identity->fpr) */ { + PEP_STATUS status; + stringlist_t *keylist; + char *_fpr = NULL; + identity->comm_type = PEP_ct_unknown; + + status = find_keys(session, identity->address, &keylist); + assert(status != PEP_OUT_OF_MEMORY); + if (status == PEP_OUT_OF_MEMORY) + return PEP_OUT_OF_MEMORY; + + stringlist_t *_keylist; + for (_keylist = keylist; _keylist && _keylist->value; _keylist = _keylist->next) { + PEP_comm_type _comm_type_key; + + status = get_key_rating(session, _keylist->value, &_comm_type_key); + assert(status != PEP_OUT_OF_MEMORY); + if (status == PEP_OUT_OF_MEMORY) { + free_stringlist(keylist); + return PEP_OUT_OF_MEMORY; + } + + if (identity->comm_type == PEP_ct_unknown) { + if (_comm_type_key != PEP_ct_compromized && _comm_type_key != PEP_ct_unknown) { + identity->comm_type = _comm_type_key; + _fpr = _keylist->value; + } + } + else { + if (_comm_type_key != PEP_ct_compromized && _comm_type_key != PEP_ct_unknown) { + if (_comm_type_key > identity->comm_type) { + identity->comm_type = _comm_type_key; + _fpr = _keylist->value; + } + } + } + } + + if (_fpr) { + free(identity->fpr); + + identity->fpr = strdup(_fpr); + if (identity->fpr == NULL) { + free_stringlist(keylist); + return PEP_OUT_OF_MEMORY; + } + identity->fpr_size = strlen(identity->fpr); + } + free_stringlist(keylist); + } + } + + status = PEP_STATUS_OK; + + if (identity->comm_type != PEP_ct_unknown && !EMPTY(identity->user_id)) { if (EMPTY(identity->username)) { free(identity->username); identity->username = strdup(stored_identity->username); @@ -87,28 +159,6 @@ DYNAMIC_API PEP_STATUS update_identity( identity->username_size = stored_identity->username_size; } - if (EMPTY(identity->user_id)) { - free(identity->user_id); - identity->user_id = strdup(stored_identity->user_id); - assert(identity->user_id); - if (identity->user_id == NULL) - return PEP_OUT_OF_MEMORY; - identity->user_id_size = stored_identity->user_id_size; - } - - if (identity->lang[0] == 0) { - identity->lang[0] = stored_identity->lang[0]; - identity->lang[1] = stored_identity->lang[1]; - identity->lang[2] = 0; - } - } - else /* stored_identity == NULL */ { - identity->comm_type = PEP_ct_unknown; - } - - status = PEP_STATUS_OK; - - if (identity->comm_type != PEP_ct_unknown) { status = set_identity(session, identity); assert(status == PEP_STATUS_OK); } diff --git a/src/keymanagement.h b/src/keymanagement.h index edc23b84..ea07dfe7 100644 --- a/src/keymanagement.h +++ b/src/keymanagement.h @@ -13,7 +13,7 @@ extern "C" { // identity->comm_type, the caller must insert the identity into the // asynchronous management implementation, so retrieve_next_identity() // will return this identity later -// at least identity->address must be a valid UTF-8 string as input +// at least identity->address must be a non-empty UTF-8 string as input DYNAMIC_API PEP_STATUS update_identity( PEP_SESSION session, pEp_identity * identity