text
stringlengths
0
357
dc_key_unref(private_key);
dc_key_unref(public_key);
return success;
}
/**
* Continue the Autocrypt Key Transfer on another device.
*
* If you have started the key transfer on another device using dc_initiate_key_transfer()
* and you've detected a setup message with dc_msg_is_setupmessage(), you should prompt the
* user for the setup code and call this function then.
*
* You can use dc_msg_get_setupcodebegin() to give the user a hint about the code (useful if the user
* has created several messages and should not enter the wrong code).
*
* @memberof dc_context_t
* @param context The context object.
* @param msg_id ID of the setup message to decrypt.
* @param setup_code Setup code entered by the user. This is the same setup code as returned from
* dc_initiate_key_transfer() on the other device.
* There is no need to format the string correctly, the function will remove all spaces and other characters and
* insert the `-` characters at the correct places.
* @return 1=key successfully decrypted and imported; both devices will use the same key now;
* 0=key transfer failed eg. due to a bad setup code.
*/
int dc_continue_key_transfer(dc_context_t* context, uint32_t msg_id, const char* setup_code)
{
int success = 0;
dc_msg_t* msg = NULL;
char* filename = NULL;
char* filecontent = NULL;
size_t filebytes = 0;
char* armored_key = NULL;
char* norm_sc = NULL;
if (context==NULL || context->magic!=DC_CONTEXT_MAGIC || msg_id <= DC_MSG_ID_LAST_SPECIAL || setup_code==NULL) {
goto cleanup;
}
if ((msg=dc_get_msg(context, msg_id))==NULL || !dc_msg_is_setupmessage(msg)
|| (filename=dc_msg_get_file(msg))==NULL || filename[0]==0) {
dc_log_error(context, 0, "Message is no Autocrypt Setup Message.");
goto cleanup;
}
if (!dc_read_file(context, filename, (void**)&filecontent, &filebytes) || filecontent==NULL || filebytes <= 0) {
dc_log_error(context, 0, "Cannot read Autocrypt Setup Message file.");
goto cleanup;
}
if ((norm_sc = dc_normalize_setup_code(context, setup_code))==NULL) {
dc_log_warning(context, 0, "Cannot normalize Setup Code.");
goto cleanup;
}
if ((armored_key=dc_decrypt_setup_file(context, norm_sc, filecontent))==NULL) {
dc_log_warning(context, 0, "Cannot decrypt Autocrypt Setup Message."); /* do not log as error - this is quite normal after entering the bad setup code */
goto cleanup;
}
if (!set_self_key(context, armored_key, 1/*set default*/)) {
goto cleanup; /* error already logged */
}
success = 1;
cleanup:
free(armored_key);
free(filecontent);
free(filename);
dc_msg_unref(msg);
free(norm_sc);
return success;
}
/*******************************************************************************
* Classic key export
******************************************************************************/
static int export_key_to_asc_file(dc_context_t* context, const char* dir, int id, const dc_key_t* key, int is_default)
{
int success = 0;
char* file_name = NULL;
if (is_default) {
file_name = dc_mprintf("%s/%s-key-default.asc", dir, key->type==DC_KEY_PUBLIC? "public" : "private");
}
else {
file_name = dc_mprintf("%s/%s-key-%i.asc", dir, key->type==DC_KEY_PUBLIC? "public" : "private", id);
}
dc_log_info(context, 0, "Exporting key %s", file_name);
dc_delete_file(context, file_name);
if (!dc_key_render_asc_to_file(key, file_name, context)) {
dc_log_error(context, 0, "Cannot write key to %s", file_name);
goto cleanup;
}