1 // This file is under GNU General Public License 3.0
4 #include "pEp_internal.h"
5 #include "dynamic_api.h"
6 #include "message_api.h"
11 PEP_STATUS has_key_reset_been_sent(
14 const char* revoked_fpr,
21 assert(!EMPTYSTR(user_id));
23 if (!session || !contacted || EMPTYSTR(revoked_fpr) || EMPTYSTR(user_id))
24 return PEP_ILLEGAL_VALUE;
28 char* alias_default = NULL;
30 PEP_STATUS status = get_userid_alias_default(session, user_id, &alias_default);
32 if (status == PEP_CANNOT_FIND_ALIAS || EMPTYSTR(alias_default)) {
34 alias_default = strdup(user_id);
37 sqlite3_reset(session->was_id_for_revoke_contacted);
38 sqlite3_bind_text(session->was_id_for_revoke_contacted, 1, revoked_fpr, -1,
40 sqlite3_bind_text(session->was_id_for_revoke_contacted, 2, user_id, -1,
42 int result = sqlite3_step(session->was_id_for_revoke_contacted);
45 *contacted = (sqlite3_column_int(session->was_id_for_revoke_contacted, 0) != 0);
49 sqlite3_reset(session->was_id_for_revoke_contacted);
51 return PEP_UNKNOWN_DB_ERROR;
54 sqlite3_reset(session->was_id_for_revoke_contacted);
58 //static const char *sql_set_revoke_contact_as_notified =
59 // "insert or replace into revocation_contact_list(fpr, contact_id) values (?1, ?2) ;";
61 PEP_STATUS set_reset_contact_notified(
63 const char* revoke_fpr,
64 const char* contact_id
67 PEP_STATUS status = PEP_STATUS_OK;
69 assert(session && !EMPTYSTR(revoke_fpr) && !EMPTYSTR(contact_id));
71 if (!session || EMPTYSTR(revoke_fpr) || EMPTYSTR(contact_id))
72 return PEP_ILLEGAL_VALUE;
74 sqlite3_reset(session->set_revoke_contact_as_notified);
75 sqlite3_bind_text(session->set_revoke_contact_as_notified, 1, revoke_fpr, -1,
77 sqlite3_bind_text(session->set_revoke_contact_as_notified, 2, contact_id, -1,
82 result = sqlite3_step(session->set_revoke_contact_as_notified);
85 status = PEP_STATUS_OK;
89 status = PEP_UNKNOWN_DB_ERROR;
92 sqlite3_reset(session->set_revoke_contact_as_notified);
97 PEP_STATUS receive_key_reset(PEP_SESSION session,
100 if (!session || !reset_msg)
101 return PEP_ILLEGAL_VALUE;
103 pEp_identity* sender_id = reset_msg->from;
106 return PEP_MALFORMED_KEY_RESET_MSG;
108 PEP_STATUS status = update_identity(session, sender_id);
109 if (!sender_id->user_id)
110 return PEP_UNKNOWN_ERROR;
112 if (is_me(session, sender_id))
113 return PEP_ILLEGAL_VALUE;
115 if (!reset_msg->longmsg || strncmp(reset_msg->longmsg, "OLD: ", 5) != 0)
116 return PEP_MALFORMED_KEY_RESET_MSG;
118 status = PEP_STATUS_OK;
119 char* old_fpr = NULL;
120 char* new_fpr = NULL;
122 stringlist_t* keylist = NULL;
123 pEp_identity* temp_ident = identity_dup(sender_id);
125 status = PEP_OUT_OF_MEMORY;
130 char* p = strtok_r(reset_msg->longmsg, "\r\n", &rest);
131 if (!EMPTYSTR(p + 5))
132 old_fpr = strdup(p + 5);
134 status = PEP_MALFORMED_KEY_RESET_MSG;
138 bool own_key = false;
139 status = is_own_key(session, old_fpr, &own_key);
142 // Nope, no one can make us our own default. If we want to do that,
143 // that's keysync, NOT key reset.
144 status = PEP_ILLEGAL_VALUE;
148 p = strtok_r(NULL, "\r\n", &rest);
149 if (strncmp(p, "NEW: ", 5) != 0 || EMPTYSTR(p + 5)) {
150 status = PEP_MALFORMED_KEY_RESET_MSG;
154 new_fpr = strdup(p + 5);
156 // Reset the original key
157 status = key_reset(session, old_fpr, temp_ident);
158 if (status != PEP_STATUS_OK)
161 status = find_keys(session, new_fpr, &keylist);
162 if (status != PEP_STATUS_OK)
166 status = PEP_KEY_NOT_FOUND;
170 // alright, we've checked as best we can. Let's set that baby.
171 sender_id->fpr = new_fpr;
173 // This only sets as the default, does NOT TRUST IN ANY WAY
174 sender_id->comm_type = sender_id->comm_type & (~PEP_ct_confirmed);
175 status = set_identity(session, sender_id);
177 sender_id->fpr = NULL; // ownership for free
179 free_stringlist(keylist);
182 free_identity(temp_ident);
186 PEP_STATUS create_standalone_key_reset_message(PEP_SESSION session,
190 const char* new_fpr) {
192 if (!dst || !recip->user_id || !recip->address)
193 return PEP_ILLEGAL_VALUE;
195 if (!old_fpr || !new_fpr)
196 return PEP_ILLEGAL_VALUE;
199 // Get own identity user has corresponded with
200 pEp_identity* own_identity = NULL;
202 PEP_STATUS status = get_own_ident_for_contact_id(session,
205 if (status != PEP_STATUS_OK)
208 message* reset_message = new_message(PEP_dir_outgoing);
209 reset_message->from = own_identity;
210 reset_message->to = new_identity_list(identity_dup(recip)); // ?
212 const char* oldtag = "OLD: ";
213 const char* newtag = "\nNEW: ";
214 const size_t taglens = 11;
215 size_t full_len = taglens + strlen(old_fpr) + strlen(new_fpr) + 2; // \n and \0
216 char* longmsg = calloc(full_len, 1);
217 strlcpy(longmsg, oldtag, full_len);
218 strlcat(longmsg, old_fpr, full_len);
219 strlcat(longmsg, newtag, full_len);
220 strlcat(longmsg, new_fpr, full_len);
221 strlcat(longmsg, "\n", full_len);
222 reset_message->longmsg = longmsg;
223 reset_message->shortmsg = strdup("Key reset");
225 message* output_msg = NULL;
227 status = encrypt_message(session, reset_message, NULL,
228 &output_msg, PEP_enc_PGP_MIME,
229 PEP_encrypt_flag_key_reset_only);
231 if (status == PEP_STATUS_OK)
234 free_message(reset_message);
238 PEP_STATUS send_key_reset_to_recents(PEP_SESSION session,
240 const char* new_fpr) {
244 assert(session->messageToSend);
246 if (!session || !old_fpr || !new_fpr)
247 return PEP_ILLEGAL_VALUE;
249 messageToSend_t send_cb = session->messageToSend;
251 return PEP_SYNC_NO_MESSAGE_SEND_CALLBACK;
253 identity_list* recent_contacts = NULL;
254 message* reset_msg = NULL;
256 PEP_STATUS status = get_last_contacted(session, &recent_contacts);
258 if (status != PEP_STATUS_OK)
261 identity_list* curr_id_ptr = recent_contacts;
263 for (curr_id_ptr = recent_contacts; curr_id_ptr; curr_id_ptr = curr_id_ptr->next) {
264 pEp_identity* curr_id = curr_id_ptr->ident;
269 const char* user_id = curr_id->user_id;
271 // Should be impossible, but?
275 // Check if it's us - if so, pointless...
276 if (is_me(session, curr_id))
279 // Check if they've already been told - this shouldn't be the case, but...
280 bool contacted = false;
281 status = has_key_reset_been_sent(session, user_id, old_fpr, &contacted);
282 if (status != PEP_STATUS_OK)
288 // if not, make em a message
291 status = create_standalone_key_reset_message(session,
297 if (status == PEP_CANNOT_FIND_IDENTITY) { // this is ok, just means we never mailed them
298 status = PEP_STATUS_OK;
302 if (status != PEP_STATUS_OK) {
308 status = send_cb(reset_msg);
310 if (status != PEP_STATUS_OK) {
315 // Put into notified DB
316 status = set_reset_contact_notified(session, old_fpr, user_id);
317 if (status != PEP_STATUS_OK)
322 free_identity_list(recent_contacts);
326 DYNAMIC_API PEP_STATUS key_reset(
333 return PEP_ILLEGAL_VALUE;
335 PEP_STATUS status = PEP_STATUS_OK;
337 char* fpr_copy = NULL;
339 char* new_key = NULL;
340 identity_list* key_idents = NULL;
341 stringlist_t* keys = NULL;
343 if (!EMPTYSTR(key_id)) {
344 fpr_copy = strdup(key_id);
346 return PEP_OUT_OF_MEMORY;
350 // Get list of own identities
351 status = get_default_own_userid(session, &own_id);
352 if (status != PEP_STATUS_OK)
355 if (EMPTYSTR(fpr_copy)) {
356 status = get_all_keys_for_user(session, own_id, &keys);
357 if (status == PEP_STATUS_OK) {
358 stringlist_t* curr_key;
359 for (curr_key = keys; curr_key && curr_key->value; curr_key = curr_key->next) {
360 status = key_reset(session, curr_key->value, NULL);
361 if (status != PEP_STATUS_OK)
366 } // otherwise, we have a specific fpr to process
368 // fpr_copy exists, so... let's go.
369 // Process own identities with this fpr
370 status = get_identities_by_main_key_id(session, fpr_copy, &key_idents);
372 if (status == PEP_STATUS_OK) {
373 // have ident list, or should
374 identity_list* curr_ident;
375 for (curr_ident = key_idents; curr_ident && curr_ident->ident;
376 curr_ident = curr_ident->next) {
377 pEp_identity* this_identity = curr_ident->ident;
378 status = key_reset(session, fpr_copy, this_identity);
379 if (status != PEP_STATUS_OK)
383 else if (status == PEP_CANNOT_FIND_IDENTITY) // not an error
384 status = PEP_STATUS_OK;
388 else { // an identity was specified.
389 if (is_me(session, ident)) {
390 // FIXME: make sure this IS our fpr?
392 // If it got sent in with an empty fpr...
393 if (EMPTYSTR(fpr_copy)) {
395 // if (!EMPTYSTR(ident->fpr))
396 // fpr_copy = strdup(ident->fpr);
397 status = _myself(session, ident, false, true, true);
398 if (status == PEP_STATUS_OK && ident->fpr)
399 fpr_copy = strdup(ident->fpr);
402 // Get list of own identities
404 status = get_default_own_userid(session, &own_id);
405 if (status == PEP_STATUS_OK)
406 status = get_user_default_key(session, own_id, &fpr_copy);
407 if (status != PEP_STATUS_OK || EMPTYSTR(fpr_copy)) {
409 return (status == PEP_STATUS_OK ? PEP_KEY_NOT_FOUND : status);
415 ident->fpr = fpr_copy;
417 status = revoke_key(session, fpr_copy, NULL);
419 if (status == PEP_STATUS_OK) {
421 status = generate_keypair(session, ident);
423 if (status == PEP_STATUS_OK) {
424 new_key = strdup(ident->fpr);
425 status = set_own_key(session, ident, new_key);
427 // mistrust fpr from trust
428 ident->fpr = fpr_copy;
430 ident->comm_type = PEP_ct_mistrusted;
431 status = set_trust(session, ident);
434 // Done with old use of ident.
435 if (status == PEP_STATUS_OK) {
436 // Update fpr for outgoing
437 status = myself(session, ident);
440 if (status == PEP_STATUS_OK)
441 // cascade that mistrust for anyone using this key
442 status = mark_as_compromised(session, fpr_copy);
443 if (status == PEP_STATUS_OK)
444 status = remove_fpr_as_default(session, fpr_copy);
445 if (status == PEP_STATUS_OK)
446 status = add_mistrusted_key(session, fpr_copy);
447 // add to revocation list
448 if (status == PEP_STATUS_OK)
449 status = set_revoked(session, fpr_copy, new_key, time(NULL));
450 // for all active communication partners:
451 // active_send revocation
452 if (status == PEP_STATUS_OK)
453 status = send_key_reset_to_recents(session, fpr_copy, new_key);
457 // TODO: Decide what this means. We have a non-own identity, we don't
458 // have an fpr. Do we reset all keys for that identity?
459 if (EMPTYSTR(fpr_copy)) {
463 // remove fpr from all identities
464 // remove fpr from all users
465 if (status == PEP_STATUS_OK)
466 status = remove_fpr_as_default(session, fpr_copy);
467 // delete key from DB
468 if (status == PEP_STATUS_OK) {
469 status = remove_key(session, fpr_copy);
477 free_identity_list(key_idents);
478 free_stringlist(keys);