|
|
|
// This file is under GNU General Public License 3.0
|
|
|
|
// see LICENSE.txt
|
|
|
|
|
|
|
|
#include "platform.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include "pEp_internal.h"
|
|
|
|
#include "keymanagement.h"
|
|
|
|
|
|
|
|
#include "sync_fsm.h"
|
|
|
|
#include "blacklist.h"
|
|
|
|
|
|
|
|
#define KEY_EXPIRE_DELTA (60 * 60 * 24 * 365)
|
|
|
|
|
|
|
|
static bool key_matches_address(PEP_SESSION session, const char* address,
|
|
|
|
const char* fpr) {
|
|
|
|
if (!session || !address || !fpr)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool retval = false;
|
|
|
|
stringlist_t *keylist = NULL;
|
|
|
|
PEP_STATUS status = find_keys(session, address, &keylist);
|
|
|
|
if (status == PEP_STATUS_OK && keylist) {
|
|
|
|
stringlist_t* curr = keylist;
|
|
|
|
while (curr) {
|
|
|
|
if (curr->value) {
|
|
|
|
if (strcasecmp(curr->value, fpr)) {
|
|
|
|
retval = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
curr = curr->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free_stringlist(keylist);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
PEP_STATUS elect_pubkey(
|
|
|
|
PEP_SESSION session, pEp_identity * identity
|
|
|
|
)
|
|
|
|
{
|
|
|
|
PEP_STATUS status;
|
|
|
|
stringlist_t *keylist = NULL;
|
|
|
|
char *_fpr = "";
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (!keylist || !keylist->value)
|
|
|
|
identity->comm_type = PEP_ct_key_not_found;
|
|
|
|
else {
|
|
|
|
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 (_comm_type_key != PEP_ct_compromized &&
|
|
|
|
_comm_type_key != PEP_ct_unknown)
|
|
|
|
{
|
|
|
|
if (identity->comm_type == PEP_ct_unknown ||
|
|
|
|
_comm_type_key > identity->comm_type)
|
|
|
|
{
|
|
|
|
bool blacklisted;
|
|
|
|
bool mistrusted;
|
|
|
|
status = is_mistrusted_key(session, _keylist->value, &mistrusted);
|
|
|
|
if (status == PEP_STATUS_OK)
|
|
|
|
status = blacklist_is_listed(session, _keylist->value, &blacklisted);
|
|
|
|
if (status == PEP_STATUS_OK && !mistrusted && !blacklisted) {
|
|
|
|
identity->comm_type = _comm_type_key;
|
|
|
|
_fpr = _keylist->value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(identity->fpr);
|
|
|
|
|
|
|
|
if (!_fpr || _fpr[0] == '\0')
|
|
|
|
identity->fpr = NULL;
|
|
|
|
else {
|
|
|
|
identity->fpr = strdup(_fpr);
|
|
|
|
if (identity->fpr == NULL) {
|
|
|
|
free_stringlist(keylist);
|
|
|
|
return PEP_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free_stringlist(keylist);
|
|
|
|
return PEP_STATUS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PEP_STATUS validate_fpr(PEP_SESSION session,
|
|
|
|
pEp_identity* ident) {
|
|
|
|
|
|
|
|
PEP_STATUS status = PEP_STATUS_OK;
|
|
|
|
|
|
|
|
if (!session || !ident || !ident->fpr || !ident->fpr[0])
|
|
|
|
return PEP_ILLEGAL_VALUE;
|
|
|
|
|
|
|
|
char* fpr = ident->fpr;
|
|
|
|
|
|
|
|
bool has_private = false;
|
|
|
|
|
|
|
|
if (ident->me) {
|
|
|
|
status = contains_priv_key(session, fpr, &has_private);
|
|
|
|
if (status != PEP_STATUS_OK || !has_private)
|
|
|
|
return PEP_KEY_UNSUITABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = get_trust(session, ident);
|
|
|
|
if (status != PEP_STATUS_OK)
|
|
|
|
ident->comm_type = PEP_ct_unknown;
|
|
|
|
|
|
|
|
PEP_comm_type ct = ident->comm_type;
|
|
|
|
|
|
|
|
if (ct == PEP_ct_unknown) {
|
|
|
|
// If status is bad, it's ok, we get the rating
|
|
|
|
// we should use then (PEP_ct_unknown)
|
|
|
|
get_key_rating(session, fpr, &ct);
|
|
|
|
ident->comm_type = ct;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool revoked, expired;
|
|
|
|
bool blacklisted = false;
|
|
|
|
|
|
|
|
status = key_revoked(session, fpr, &revoked);
|
|
|
|
|
|
|
|
if (status != PEP_STATUS_OK) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!revoked) {
|
|
|
|
time_t exp_time = (ident->me ?
|
|
|
|
time(NULL) + (7*24*3600) : time(NULL));
|
|
|
|
|
|
|
|
status = key_expired(session, fpr,
|
|
|
|
exp_time,
|
|
|
|
&expired);
|
|
|
|
|
|
|
|
assert(status == PEP_STATUS_OK);
|
|
|
|
if (status != PEP_STATUS_OK)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
if ((ct | PEP_ct_confirmed) == PEP_ct_OpenPGP &&
|
|
|
|
!ident->me) {
|
|
|
|
status = blacklist_is_listed(session,
|
|
|
|
fpr,
|
|
|
|
&blacklisted);
|
|
|
|
|
|
|
|
if (status != PEP_STATUS_OK)
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ident->me && (ct >= PEP_ct_strong_but_unconfirmed) && !revoked && expired) {
|
|
|
|
// extend key
|
|
|
|
timestamp *ts = new_timestamp(time(NULL) + KEY_EXPIRE_DELTA);
|
|
|
|
status = renew_key(session, fpr, ts);
|
|
|
|
free_timestamp(ts);
|
|
|
|
|
|
|
|
if (status == PEP_STATUS_OK) {
|
|
|
|
// if key is valid (second check because pEp key might be extended above)
|
|
|
|
// Return fpr
|
|
|
|
status = key_expired(session, fpr, time(NULL), &expired);
|
|
|
|
if (status != PEP_STATUS_OK) {
|
|
|
|
ident->comm_type = PEP_ct_key_expired;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
// communicate key(?)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (revoked)
|
|
|
|
ct = PEP_ct_key_revoked;
|
|
|
|
else if (expired)
|
|
|
|
ct = PEP_ct_key_expired;
|
|
|
|
else if (blacklisted) { // never true for .me
|
|
|
|
ident->comm_type = ct = PEP_ct_key_not_found;
|
|
|
|
free(ident->fpr);
|
|
|
|
ident->fpr = strdup("");
|
|
|
|
status = PEP_KEY_BLACKLISTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (ct) {
|
|
|
|
case PEP_ct_key_expired:
|
|
|
|
case PEP_ct_key_revoked:
|
|
|
|
case PEP_ct_key_b0rken:
|
|
|
|
// delete key from being default key for all users/identities
|
|
|
|
status = remove_fpr_as_default(session, fpr);
|
|
|
|
status = update_trust_for_fpr(session,
|
|
|
|
fpr,
|
|
|
|
ct);
|
|
|
|
case PEP_ct_mistrusted:
|
|
|
|
free(ident->fpr);
|
|
|
|
ident->fpr = NULL;
|
|
|
|
ident->comm_type = ct;
|
|
|
|
status = PEP_KEY_UNSUITABLE;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
PEP_STATUS get_user_default_key(PEP_SESSION session, const char* user_id,
|
|
|
|
char** default_key) {
|
|
|
|
assert(session);
|
|
|
|
assert(user_id);
|
|
|
|
|
|
|
|
if (!session || !user_id)
|
|
|
|
return PEP_ILLEGAL_VALUE;
|
|
|
|
|
|
|
|
PEP_STATUS status = PEP_STATUS_OK;
|
|
|
|
|
|
|
|
// try to get default key for user_data
|
|
|
|
sqlite3_reset(session->get_user_default_key);
|
|
|
|
sqlite3_bind_text(session->get_user_default_key, 1, user_id,
|
|
|
|
-1, SQLITE_STATIC);
|
|
|
|
|
|
|
|
const int result = sqlite3_step(session->get_user_default_key);
|
|
|
|
char* user_fpr = NULL;
|
|
|
|
if (result == SQLITE_ROW) {
|
|
|
|
const char* u_fpr =
|
|
|
|
(char *) sqlite3_column_text(session->get_user_default_key, 0);
|
|
|
|
if (u_fpr)
|
|
|
|
user_fpr = strdup(u_fpr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
status = PEP_GET_KEY_FAILED;
|
|
|
|
|
|
|
|
sqlite3_reset(session->get_user_default_key);
|
|
|
|
|
|
|
|
*default_key = user_fpr;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only call on retrieval of previously stored identity!
|
|
|
|
// Also, we presume that if the stored_identity was sent in
|
|
|
|
// without an fpr, there wasn't one in the trust DB for this
|
|
|
|
// identity.
|
|
|
|
PEP_STATUS get_valid_pubkey(PEP_SESSION session,
|
|
|
|
pEp_identity* stored_identity,
|
|
|
|
bool* is_identity_default,
|
|
|
|
bool* is_user_default,
|
|
|
|
bool* is_address_default) {
|
|
|
|
|
|
|
|
PEP_STATUS status = PEP_STATUS_OK;
|
|
|
|
|
|
|
|
if (!stored_identity || EMPTYSTR(stored_identity->user_id)
|
|
|
|
|| !is_identity_default || !is_user_default || !is_address_default)
|
|
|
|
return PEP_ILLEGAL_VALUE;
|
|
|
|
|
|
|
|
*is_identity_default = *is_user_default = *is_address_default = false;
|
|
|
|
|
|
|
|
PEP_comm_type first_reject_comm_type = PEP_ct_key_not_found;
|
|
|
|
PEP_STATUS first_reject_status = PEP_KEY_NOT_FOUND;
|
|
|
|
|
|
|
|
char* stored_fpr = stored_identity->fpr;
|
|
|
|
// Input: stored identity retrieved from database
|
|
|
|
// if stored identity contains a default key
|
|
|
|
if (!EMPTYSTR(stored_fpr)) {
|
|
|
|
status = validate_fpr(session, stored_identity);
|
|
|
|
if (status == PEP_STATUS_OK && !EMPTYSTR(stored_identity->fpr)) {
|
|
|
|
*is_identity_default = *is_address_default = true;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
else if (status != PEP_KEY_NOT_FOUND) {
|
|
|
|
first_reject_status = status;
|
|
|
|
first_reject_comm_type = stored_identity->comm_type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if no valid default stored identity key found
|
|
|
|
free(stored_identity->fpr);
|
|
|
|
stored_identity->fpr = NULL;
|
|
|
|
|
|
|
|
char* user_fpr = NULL;
|
|
|
|
status = get_user_default_key(session, stored_identity->user_id, &user_fpr);
|
|
|
|
|
|
|
|
if (!EMPTYSTR(user_fpr)) {
|
|
|
|
// There exists a default key for user, so validate
|
|
|
|
stored_identity->fpr = user_fpr;
|
|
|
|
status = validate_fpr(session, stored_identity);
|
|
|
|
if (status == PEP_STATUS_OK && stored_identity->fpr) {
|
|
|
|
*is_user_default = true;
|
|
|
|
*is_address_default = key_matches_address(session,
|
|
|
|
stored_identity->address,
|
|
|
|
stored_identity->fpr);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
else if (status != PEP_KEY_NOT_FOUND && first_reject_status != PEP_KEY_NOT_FOUND) {
|
|
|
|
first_reject_status = status;
|
|
|
|
first_reject_comm_type = stored_identity->comm_type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
status = elect_pubkey(session, stored_identity);
|
|
|
|
if (status == PEP_STATUS_OK) {
|
|
|
|
if (!EMPTYSTR(stored_identity->fpr))
|
|
|
|
validate_fpr(session, stored_identity);
|
|
|
|
}
|
|
|
|
else if (status != PEP_KEY_NOT_FOUND && first_reject_status != PEP_KEY_NOT_FOUND) {
|
|
|
|
first_reject_status = status;
|
|
|
|
first_reject_comm_type = stored_identity->comm_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (stored_identity->comm_type) {
|
|
|
|
case PEP_ct_key_revoked:
|
|
|
|
case PEP_ct_key_b0rken:
|
|
|
|
case PEP_ct_key_expired:
|
|
|
|
case PEP_ct_compromized:
|
|
|
|
case PEP_ct_mistrusted:
|
|
|
|
// this only happens when it's all there is
|
|
|
|
status = first_reject_status;
|
|
|
|
free(stored_identity->fpr);
|
|
|
|
stored_identity->fpr = NULL;
|
|
|
|
stored_identity->comm_type = first_reject_comm_type;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void transfer_ident_lang_and_flags(pEp_identity* new_ident,
|
|
|
|
pEp_identity* stored_ident) {
|
|
|
|
if (new_ident->lang[0] == 0) {
|
|
|
|
new_ident->lang[0] = stored_ident->lang[0];
|
|
|
|
new_ident->lang[1] = stored_ident->lang[1];
|
|
|
|
new_ident->lang[2] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_ident->flags = stored_ident->flags;
|
|
|
|
new_ident->me = new_ident->me || stored_ident->me;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PEP_STATUS prepare_updated_identity(PEP_SESSION session,
|
|
|
|
pEp_identity* return_id,
|
|
|
|
pEp_identity* stored_ident,
|
|
|
|
bool store) {
|
|
|
|
|
|
|
|
if (!session || !return_id || !stored_ident)
|
|
|
|
return PEP_ILLEGAL_VALUE;
|
|
|
|
|
|
|
|
PEP_STATUS status;
|
|
|
|
|
|
|
|
bool is_identity_default, is_user_default, is_address_default;
|
|
|
|
status = get_valid_pubkey(session, stored_ident,
|
|
|
|
&is_identity_default,
|
|
|
|
&is_user_default,
|
|
|
|
&is_address_default);
|
|
|
|
|
|
|
|
if (status == PEP_STATUS_OK && stored_ident->fpr && *(stored_ident->fpr) != '\0') {
|
|
|
|
// set identity comm_type from trust db (user_id, FPR)
|
|
|
|
status = get_trust(session, stored_ident);
|
|
|
|
if (status == PEP_CANNOT_FIND_IDENTITY || stored_ident->comm_type == PEP_ct_unknown) {
|
|
|
|
// This is OK - there is no trust DB entry, but we
|
|
|
|
// found a key. We won't store this, but we'll
|
|
|
|
// use it.
|
|
|
|
PEP_comm_type ct = PEP_ct_unknown;
|
|
|
|
status = get_key_rating(session, stored_ident->fpr, &ct);
|
|
|
|
stored_ident->comm_type = ct;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (stored_ident->comm_type == PEP_ct_unknown)
|
|
|
|
stored_ident->comm_type = PEP_ct_key_not_found;
|
|
|
|
}
|
|
|
|
free(return_id->fpr);
|
|
|
|
return_id->fpr = NULL;
|
|
|
|
if (status == PEP_STATUS_OK && !EMPTYSTR(stored_ident->fpr))
|
|
|
|
return_id->fpr = strdup(stored_ident->fpr);
|
|
|
|
|
|
|
|
return_id->comm_type = stored_ident->comm_type;
|
|
|
|
|
|
|
|
// We patch the DB with the input username, but if we didn't have
|
|
|
|
// one, we pull it out of storage if available.
|
|
|
|
// (also, if the input username is "anonymous" and there exists
|
|
|
|
// a DB username, we replace)
|
|
|
|
if (!EMPTYSTR(stored_ident->username)) {
|
|
|
|
if (!EMPTYSTR(return_id->username) &&
|
|
|
|
(strcasecmp(return_id->username, return_id->address) == 0)) {
|
|
|
|
free(return_id->username);
|
|
|
|
return_id->username = NULL;
|
|
|
|
}
|
|
|
|
if (EMPTYSTR(return_id->username)) {
|
|
|
|
free(return_id->username);
|
|
|
|
return_id->username = strdup(stored_ident->username);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (EMPTYSTR(return_id->username))
|
|
|
|
return_id->username = strdup(return_id->address);
|
|
|
|
}
|
|
|
|
|
|
|
|
return_id->me = stored_ident->me;
|
|
|
|
|
|
|
|
// FIXME: Do we ALWAYS do this? We probably should...
|
|
|
|
if (EMPTYSTR(return_id->user_id)) {
|
|
|
|
free(return_id->user_id);
|
|
|
|
return_id->user_id = strdup(stored_ident->user_id);
|
|
|
|
}
|
|
|
|
// Call set_identity() to store
|
|
|
|
if ((is_identity_default || is_user_default) &&
|
|
|
|
is_address_default) {
|
|
|
|
// if we got an fpr which is default for either user
|
|
|
|
// or identity AND is valid for this address, set in DB
|
|
|
|
// as default
|
|
|
|
status = set_identity(session, return_id);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Store without default fpr/ct, but return the fpr and ct
|
|
|
|
// for current use
|
|
|
|
char* save_fpr = return_id->fpr;
|
|
|
|
PEP_comm_type save_ct = return_id->comm_type;
|
|
|
|
return_id->fpr = NULL;
|
|
|
|
return_id->comm_type = PEP_ct_unknown;
|
|
|
|
PEP_STATUS save_status = status;
|
|
|
|
status = set_identity(session, return_id);
|
|
|
|
if (save_status != PEP_STATUS_OK)
|
|
|
|
status = save_status;
|
|
|
|
return_id->fpr = save_fpr;
|
|
|
|
return_id->comm_type = save_ct;
|
|
|
|
}
|
|
|
|
|
|
|
|
transfer_ident_lang_and_flags(return_id, stored_ident);
|
|
|
|
|
|
|
|
if (return_id->comm_type == PEP_ct_unknown)
|
|
|
|
return_id->comm_type = PEP_ct_key_not_found;
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DYNAMIC_API PEP_STATUS update_identity(
|
|
|
|
PEP_SESSION session, pEp_identity * identity
|
|
|
|
)
|
|
|
|
{
|
|
|
|
PEP_STATUS status;
|
|
|
|
|
|
|
|
assert(session);
|
|
|
|
assert(identity);
|
|
|
|
assert(!EMPTYSTR(identity->address));
|
|
|
|
|
|
|
|
if (!(session && identity && !EMPTYSTR(identity->address)))
|
|
|
|
return PEP_ILLEGAL_VALUE;
|
|
|
|
|
|
|
|
char* default_own_id = NULL;
|
|
|
|
status = get_default_own_userid(session, &default_own_id);
|
|
|
|
|
|
|
|
// Is this me, temporary or not? If so, BAIL.
|
|
|
|
if (identity->me ||
|
|
|
|
(default_own_id && identity->user_id && (strcmp(default_own_id, identity->user_id) == 0)))
|
|
|
|
{
|
|
|
|
return PEP_ILLEGAL_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have, at least, an address.
|
|
|
|
// Retrieve stored identity information!
|
|
|
|
pEp_identity* stored_ident = NULL;
|
|
|
|
|
|
|
|
if (!EMPTYSTR(identity->user_id)) {
|
|
|
|
// (we're gonna update the trust/fpr anyway, so we use the no-fpr-from-trust-db variant)
|
|
|
|
// * do get_identity() to retrieve stored identity information
|
|
|
|
status = get_identity_without_trust_check(session, identity->address, identity->user_id, &stored_ident);
|
|
|
|
|
|
|
|
// Before we start - if there was no stored identity, we should check to make sure we don't
|
|
|
|
// have a stored identity with a temporary user_id that differs from the input user_id. This
|
|
|
|
// happens in multithreaded environments sometimes.
|
|
|
|
if (!stored_ident) {
|
|
|
|
identity_list* id_list = NULL;
|
|
|
|
status = get_identities_by_address(session, identity->address, &id_list);
|
|
|
|
|
|
|
|
if (id_list) {
|
|
|
|
identity_list* id_curr = id_list;
|
|
|
|
while (id_curr) {
|
|
|
|
pEp_identity* this_id = id_curr->ident;
|
|
|
|
if (this_id) {
|
|
|
|
char* this_uid = this_id->user_id;
|
|
|
|
if (this_uid && (strstr(this_uid, "TOFU_") == this_uid)) {
|
|
|
|
// FIXME: should we also be fixing pEp_own_userId in this
|
|
|
|
// function here?
|
|
|
|
|
|
|
|
// if usernames match, we replace the userid. Or if the temp username
|
|
|
|
// is anonymous.
|
|
|
|
// FIXME: do we need to create an address match function which
|
|
|
|
// matches the whole dot-and-case rigamarole from
|
|
|
|
if (EMPTYSTR(this_id->username) ||
|
|
|
|
strcasecmp(this_id->username, this_id->address) == 0 ||
|
|
|
|
(identity->username &&
|
|
|
|
strcasecmp(identity->username,
|
|
|
|
this_id->username) == 0)) {
|
|
|
|
|
|
|
|
// Ok, we have a temp ID. We have to replace this
|
|
|
|
// with the real ID.
|
|
|
|
status = replace_userid(session,
|
|
|
|
this_uid,
|
|
|
|
identity->user_id);
|
|
|
|
if (status != PEP_STATUS_OK) {
|
|
|
|
free_identity_list(id_list);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(this_uid);
|
|
|
|
this_uid = NULL;
|
|
|
|
|
|
|
|
// Reflect the change we just made to the DB
|
|
|
|
this_id->user_id = strdup(identity->user_id);
|
|
|
|
stored_ident = this_id;
|
|
|
|
// FIXME: free list.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
id_curr = id_curr->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status == PEP_STATUS_OK && stored_ident) {
|
|
|
|
// * if identity available
|
|
|
|
// * patch it with username
|
|
|
|
// (note: this will happen when
|
|
|
|
// setting automatically below...)
|
|
|
|
// * elect valid key for identity
|
|
|
|
// * if valid key exists
|
|
|
|
// * set return value's fpr
|
|
|
|
status = prepare_updated_identity(session,
|
|
|
|
identity,
|
|
|
|
stored_ident, true);
|
|
|
|
}
|
|
|
|
// * else (identity unavailable)
|
|
|
|
else {
|
|
|
|
status = PEP_STATUS_OK;
|
|
|
|
|
|
|
|
// FIXME: We may need to roll this back.
|
|
|
|
// FIXME: change docs if we don't
|
|
|
|
// if we only have user_id and address and identity not available
|
|
|
|
// * return error status (identity not found)
|
|
|
|
if (EMPTYSTR(identity->username)) {
|
|
|
|
free(identity->username);
|
|
|
|
identity->username = strdup(identity->address);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, if we had user_id, address, and username:
|
|
|
|
// * create identity with user_id, address, username
|
|
|
|
// (this is the input id without the fpr + comm type!)
|
|
|
|
|
|
|
|
if (status == PEP_STATUS_OK) {
|
|
|
|
elect_pubkey(session, identity);
|
|
|
|
}
|
|
|
|
|
|
|
|
// * We've already checked and retrieved
|
|
|
|
// any applicable temporary identities above. If we're
|
|
|
|
// here, none of them fit.
|
|
|
|
// * call set_identity() to store
|
|
|
|
if (status == PEP_STATUS_OK)
|
|
|
|
// FIXME: Do we set if we had to copy in the address?
|
|
|
|
status = set_identity(session, identity);
|
|
|
|
// * Return: created identity
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!EMPTYSTR(identity->username)) {
|
|
|
|
/*
|
|
|
|
* Temporary identity information with username supplied
|
|
|
|
* Input: address, username (no others)
|
|
|
|
*/
|
|
|
|
|
|
|
|
// * See if there is an own identity that uses this address. If so, we'll
|
|
|
|
// prefer that
|
|
|
|
stored_ident = NULL;
|
|
|
|
|
|
|
|
if (default_own_id) {
|
|
|
|
status = get_identity(session,
|
|
|
|
default_own_id,
|
|
|
|
identity->address,
|
|
|
|
&stored_ident);
|
|
|
|
}
|
|
|
|
// If there isn't an own identity, search for a non-temp stored ident
|
|
|
|
// with this address.
|
|
|
|
if (status == PEP_CANNOT_FIND_IDENTITY || !stored_ident) {
|
|
|
|
|
|
|
|
identity_list* id_list = NULL;
|
|
|
|
status = get_identities_by_address(session, identity->address, &id_list);
|
|
|
|
|
|
|
|
if (id_list) {
|
|
|
|
identity_list* id_curr = id_list;
|
|
|
|
while (id_curr) {
|
|
|
|
pEp_identity* this_id = id_curr->ident;
|
|
|
|
if (this_id) {
|
|
|
|
char* this_uid = this_id->user_id;
|
|
|
|
if (this_uid && (strstr(this_uid, "TOFU_") != this_uid)) {
|
|
|
|
// if usernames match, we replace the userid.
|
|
|
|
if (identity->username &&
|
|
|
|
strcasecmp(identity->username,
|
|
|
|
this_id->username) == 0) {
|
|
|
|
|
|
|
|
// Ok, we have a real ID. Copy it!
|
|
|
|
identity->user_id = strdup(this_uid);
|
|
|
|
|
|
|
|
if (!identity->user_id)
|
|
|
|
status = PEP_OUT_OF_MEMORY;
|
|
|
|
stored_ident = this_id;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
id_curr = id_curr->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stored_ident) {
|
|
|
|
status = prepare_updated_identity(session,
|
|
|
|
identity,
|
|
|
|
stored_ident, true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// create temporary identity, store it, and Return this
|
|
|
|
// This means TOFU_ user_id
|
|
|
|
identity->user_id = calloc(1, strlen(identity->address) + 6);
|
|
|
|
if (!identity->user_id)
|
|
|
|
return PEP_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
snprintf(identity->user_id, strlen(identity->address) + 6,
|
|
|
|
"TOFU_%s", identity->address);
|
|
|
|
|
|
|
|
// * We've already checked and retrieved
|
|
|
|
// any applicable temporary identities above. If we're
|
|
|
|
// here, none of them fit.
|
|
|
|
|
|
|
|
status = elect_pubkey(session, identity);
|
|
|
|
|
|
|
|
// * call set_identity() to store
|
|
|
|
if (identity->fpr)
|
|
|
|
status = get_key_rating(session, identity->fpr, &identity->comm_type);
|
|
|
|
|
|
|
|
// * call set_identity() to store
|
|
|
|
status = set_identity(session, identity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/*
|
|
|
|
* Input: address (no others)
|
|
|
|
* Temporary identity information without username suplied
|
|
|
|
*/
|
|
|
|
|
|
|
|
// * Again, see if there is an own identity that uses this address. If so, we'll
|
|
|
|
// prefer that
|
|
|
|
stored_ident = NULL;
|
|
|
|
|
|
|
|
if (default_own_id) {
|
|
|
|
status = get_identity(session,
|
|
|
|
default_own_id,
|
|
|
|
identity->address,
|
|
|
|
&stored_ident);
|
|
|
|
}
|
|
|
|
// If there isn't an own identity, search for a non-temp stored ident
|
|
|
|
// with this address.
|
|
|
|
if (status == PEP_CANNOT_FIND_IDENTITY || !stored_ident) {
|
|
|
|
|
|
|
|
identity_list* id_list = NULL;
|
|
|
|
// * Search for identity with this address
|
|
|
|
status = get_identities_by_address(session, identity->address, &id_list);
|
|
|
|
|
|
|
|
// Results are ordered by timestamp descending, so this covers
|
|
|
|
// both the one-result and multi-result cases
|
|
|
|
if (id_list) {
|
|
|
|
if (stored_ident) // unlikely
|
|
|
|
free_identity(stored_ident);
|
|
|
|
stored_ident = id_list->ident;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (stored_ident)
|
|
|
|
status = prepare_updated_identity(session, identity,
|
|
|
|
stored_ident, false);
|
|
|
|
else {
|
|
|
|
// too little info. BUT. We see if we can find a key; if so, we create a
|
|
|
|
// temp identity, look for a key, and store.
|
|
|
|
|
|
|
|
// create temporary identity, store it, and Return this
|
|
|
|
// This means TOFU_ user_id
|
|
|
|
identity->user_id = calloc(1, strlen(identity->address) + 6);
|
|
|
|
if (!identity->user_id)
|
|
|
|
return PEP_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
snprintf(identity->user_id, strlen(identity->address) + 6,
|
|
|
|
"TOFU_%s", identity->address);
|
|
|
|
|
|
|
|
identity->username = strdup(identity->address);
|
|
|
|
if (!identity->address)
|
|
|
|
return PEP_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
free(identity->fpr);
|
|
|
|
identity->fpr = NULL;
|
|
|
|
identity->comm_type = PEP_ct_unknown;
|
|
|
|
|
|
|
|
status = elect_pubkey(session, identity);
|
|
|
|
|
|
|
|
if (identity->fpr)
|
|
|
|
status = get_key_rating(session, identity->fpr, &identity->comm_type);
|
|
|
|
|
|
|
|
// * call set_identity() to store
|
|
|
|
status = set_identity(session, identity);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: This is legacy. I presume it's a notification for the caller...
|
|
|
|
// Revisit once I can talk to Volker
|
|
|
|
if (identity->comm_type != PEP_ct_compromized &&
|
|
|
|
identity->comm_type < PEP_ct_strong_but_unconfirmed)
|
|
|
|
if (session->examine_identity)
|
|
|
|
session->examine_identity(identity, session->examine_management);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
PEP_STATUS elect_ownkey(
|
|
|
|
PEP_SESSION session, pEp_identity * identity
|
|
|
|
)
|
|
|
|
{
|
|
|
|
PEP_STATUS status;
|
|
|
|
stringlist_t *keylist = NULL;
|
|
|
|
|
|
|
|
free(identity->fpr);
|
|
|
|
identity->fpr = NULL;
|
|
|
|
|
|
|
|
status = find_private_keys(session, identity->address, &keylist);
|
|
|
|
assert(status != PEP_OUT_OF_MEMORY);
|
|
|
|
if (status == PEP_OUT_OF_MEMORY)
|
|
|
|
return PEP_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
if (keylist != NULL && keylist->value != NULL)
|
|
|
|
{
|
|
|
|
char *_fpr = NULL;
|
|
|
|
identity->comm_type = PEP_ct_unknown;
|
|
|
|
|
|
|
|
stringlist_t *_keylist;
|
|
|
|
for (_keylist = keylist; _keylist && _keylist->value; _keylist = _keylist->next) {
|
|
|
|
bool is_own = false;
|
|
|
|
|
|
|
|
status = own_key_is_listed(session, _keylist->value, &is_own);
|
|
|
|
assert(status == PEP_STATUS_OK);
|
|
|
|
if (status != PEP_STATUS_OK) {
|
|
|
|
free_stringlist(keylist);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_own)
|
|
|
|
{
|
|
|
|
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 (_comm_type_key != PEP_ct_compromized &&
|
|
|
|
_comm_type_key != PEP_ct_unknown)
|
|
|
|
{
|
|
|
|
if (identity->comm_type == PEP_ct_unknown ||
|
|
|
|
_comm_type_key > identity->comm_type)
|
|
|
|
{
|
|
|
|
identity->comm_type = _comm_type_key;
|
|
|
|
_fpr = _keylist->value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_fpr)
|
|
|
|
{
|
|
|
|
identity->fpr = strdup(_fpr);
|
|
|
|
assert(identity->fpr);
|
|
|
|
if (identity->fpr == NULL)
|
|
|
|
{
|
|
|
|
free_stringlist(keylist);
|
|
|
|
return PEP_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free_stringlist(keylist);
|
|
|
|
}
|
|
|
|
return PEP_STATUS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
PEP_STATUS _has_usable_priv_key(PEP_SESSION session, char* fpr,
|
|
|
|
bool* is_usable) {
|
|
|
|
|
|
|
|
bool dont_use_fpr = true;
|
|
|
|
|
|
|
|
PEP_STATUS status = blacklist_is_listed(session, fpr, &dont_use_fpr);
|
|
|
|
if (status == PEP_STATUS_OK && !dont_use_fpr) {
|
|
|
|
// Make sure there is a *private* key associated with this fpr
|
|
|
|
bool has_private = false;
|
|
|
|
status = contains_priv_key(session, fpr, &has_private);
|
|
|
|
|
|
|
|
if (status == PEP_STATUS_OK)
|
|
|
|
dont_use_fpr = !has_private;
|
|
|
|
}
|
|
|
|
|
|
|
|
*is_usable = !dont_use_fpr;
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
PEP_STATUS _myself(PEP_SESSION session, pEp_identity * identity, bool do_keygen, bool ignore_flags)
|
|
|
|
{
|
|
|
|
|
|
|
|
PEP_STATUS status;
|
|
|
|
|
|
|
|
assert(session);
|
|
|
|
assert(identity);
|
|
|
|
assert(!EMPTYSTR(identity->address));
|
|
|
|
assert(!EMPTYSTR(identity->user_id));
|
|
|
|
|
|
|
|
if (!session || !identity || EMPTYSTR(identity->address) ||
|
|
|
|
EMPTYSTR(identity->user_id))
|
|
|
|
return PEP_ILLEGAL_VALUE;
|
|
|
|
|
|
|
|
pEp_identity *stored_identity = NULL;
|
|
|
|
char* revoked_fpr = NULL;
|
|
|
|
|
|
|
|
char* default_own_id = NULL;
|
|
|
|
status = get_default_own_userid(session |