You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pEpEngine/src/keymanagement.c

1892 lines
62 KiB
C

// This file is under GNU General Public License 3.0
// see LICENSE.txt
#include "platform.h"
9 years ago
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <ctype.h>
9 years ago
8 years ago
#include "pEp_internal.h"
9 years ago
#include "keymanagement.h"
#include "blacklist.h"
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, bool check_blacklist
)
{
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_compromised &&
_comm_type_key != PEP_ct_unknown)
{
if (identity->comm_type == PEP_ct_unknown ||
_comm_type_key > identity->comm_type)
{
bool blacklisted = false;
bool mistrusted = false;
status = is_mistrusted_key(session, _keylist->value, &mistrusted);
if (status == PEP_STATUS_OK && check_blacklist)
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,
bool check_blacklist) {
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;
9 years ago
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;
}
5 years ago
bool pEp_user = false;
5 years ago
is_pEp_user(session, ident, &pEp_user);
5 years ago
if (pEp_user) {
switch (ct) {
case PEP_ct_OpenPGP:
case PEP_ct_OpenPGP_unconfirmed:
ct += 0x47; // difference between PEP and OpenPGP values;
ident->comm_type = ct;
break;
default:
break;
}
}
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;
9 years ago
if (check_blacklist && IS_PGP_CT(ct) &&
!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);
8 years ago
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(?)
}
7 years ago
}
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;
}
7 years ago
PEP_STATUS get_all_keys_for_user(PEP_SESSION session,
const char* user_id,
stringlist_t** keys) {
if (!session || EMPTYSTR(user_id) || !keys)
return PEP_ILLEGAL_VALUE;
PEP_STATUS status = PEP_STATUS_OK;
*keys = NULL;
stringlist_t* _kl = NULL;
sqlite3_reset(session->get_all_keys_for_user);
sqlite3_bind_text(session->get_all_keys_for_user, 1, user_id, -1, SQLITE_STATIC);
int result = -1;
while ((result = sqlite3_step(session->get_all_keys_for_user)) == SQLITE_ROW) {
const char* keyres = (const char *) sqlite3_column_text(session->get_all_keys_for_user, 0);
if (keyres) {
if (_kl)
stringlist_add(_kl, keyres);
else
_kl = new_stringlist(keyres);
}
}
if (!_kl)
return PEP_KEY_NOT_FOUND;
*keys = _kl;
sqlite3_reset(session->get_all_keys_for_user);
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,
bool check_blacklist) {
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, check_blacklist);
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, check_blacklist);
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, check_blacklist);
if (status == PEP_STATUS_OK) {
if (!EMPTYSTR(stored_identity->fpr))
validate_fpr(session, stored_identity, false); // blacklist already filtered of needed
}
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_compromised:
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:
if (check_blacklist && status == PEP_KEY_BLACKLISTED) {
free(stored_identity->fpr);
stored_identity->fpr = NULL;
stored_identity->comm_type = PEP_ct_key_not_found;
}
break;
}
return status;
}
9 years ago
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;
}
5 years ago
static void adjust_pEp_trust_status(PEP_SESSION session, pEp_identity* identity) {
assert(session);
assert(identity);
if (identity->comm_type < PEP_ct_strong_but_unconfirmed ||
(identity->comm_type | PEP_ct_confirmed) == PEP_ct_pEp)
return;
5 years ago
bool pEp_user;
5 years ago
is_pEp_user(session, identity, &pEp_user);
5 years ago
if (pEp_user) {
PEP_comm_type confirmation_status = identity->comm_type & PEP_ct_confirmed;
identity->comm_type = PEP_ct_pEp_unconfirmed | confirmation_status;
}
}
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,
false);
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);
}
5 years ago
adjust_pEp_trust_status(session, return_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)))
{
free(default_own_id);
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;
bool input_is_TOFU = strstr(identity->user_id, "TOFU_") == identity->user_id;
while (id_curr) {
pEp_identity* this_id = id_curr->ident;
if (this_id) {
char* this_uid = this_id->user_id;
bool curr_is_TOFU = strstr(this_uid, "TOFU_") == this_uid;
if (this_uid) {
if (curr_is_TOFU && !input_is_TOFU) {
// 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);
free(default_own_id);
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;
}
}
else if (input_is_TOFU && !curr_is_TOFU) {
// Replace ruthlessly - this is NOT supposed to happen.
// BAD APP BEHAVIOUR.
free(identity->user_id);
identity->user_id = strdup(this_id->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, false);
}
// * 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?
5 years ago
adjust_pEp_trust_status(session, identity);
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,
identity->address,
default_own_id,
&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);
assert(identity->user_id);
if (!identity->user_id)
goto enomem;
stored_ident = this_id;
break;
}
}
}
id_curr = id_curr->next;
}
}
}
if (stored_ident) {
status = prepare_updated_identity(session,
identity,
stored_ident, true);
}
else {
identity->user_id = calloc(1, strlen(identity->address) + 6);
if (!identity->user_id)
goto enomem;
9 years ago
snprintf(identity->user_id, strlen(identity->address) + 6,
"TOFU_%s", identity->address);
status = get_identity(session,
identity->address,
identity->user_id,
&stored_ident);
if (status == PEP_STATUS_OK && stored_ident) {
status = prepare_updated_identity(session,
identity,
stored_ident, true);
}
else {
// * We've already checked and retrieved
// any applicable temporary identities above. If we're
// here, none of them fit.
status = elect_pubkey(session, identity, false);
// * call set_identity() to store
if (identity->fpr)
status = get_key_rating(session, identity->fpr, &identity->comm_type);
// * call set_identity() to store
5 years ago
adjust_pEp_trust_status(session, identity);
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,
identity->address,
default_own_id,
&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;