text
stringlengths
0
357
char passphrase_begin[8];
char* encr_string = NULL;
char* ret_setupfilecontent = NULL;
if (context==NULL || context->magic!=DC_CONTEXT_MAGIC || passphrase==NULL
|| strlen(passphrase)<2 || curr_private_key==NULL) {
goto cleanup;
}
strncpy(passphrase_begin, passphrase, 2);
passphrase_begin[2] = 0;
/* create the payload */
if (!dc_ensure_secret_key_exists(context)) {
goto cleanup;
}
{
self_addr = dc_sqlite3_get_config(context->sql, "configured_addr", NULL);
dc_key_load_self_private(curr_private_key, self_addr, context->sql);
int e2ee_enabled = dc_sqlite3_get_config_int(context->sql, "e2ee_enabled", DC_E2EE_DEFAULT_ENABLED);
char* payload_key_asc = dc_key_render_asc(curr_private_key, e2ee_enabled? "Autocrypt-Prefer-Encrypt: mutual\r\n" : NULL);
if (payload_key_asc==NULL) {
goto cleanup;
}
if(!dc_pgp_symm_encrypt(context, passphrase, payload_key_asc, strlen(payload_key_asc), &encr_string)) {
goto cleanup;
}
free(payload_key_asc);
}
/* add additional header to armored block */
#define LINEEND "\r\n" /* use the same lineends as the PGP armored data */
{
char* replacement = dc_mprintf("-----BEGIN PGP MESSAGE-----" LINEEND
"Passphrase-Format: numeric9x4" LINEEND
"Passphrase-Begin: %s", passphrase_begin);
dc_str_replace(&encr_string, "-----BEGIN PGP MESSAGE-----", replacement);
free(replacement);
}
/* wrap HTML-commands with instructions around the encrypted payload */
{
char* setup_message_title = dc_stock_str(context, DC_STR_AC_SETUP_MSG_SUBJECT);
char* setup_message_body = dc_stock_str(context, DC_STR_AC_SETUP_MSG_BODY);
dc_str_replace(&setup_message_body, "\r", NULL);
dc_str_replace(&setup_message_body, "\n", "<br>");
ret_setupfilecontent = dc_mprintf(
"<!DOCTYPE html>" LINEEND
"<html>" LINEEND
"<head>" LINEEND
"<title>%s</title>" LINEEND
"</head>" LINEEND
"<body>" LINEEND
"<h1>%s</h1>" LINEEND
"<p>%s</p>" LINEEND
"<pre>" LINEEND
"%s" LINEEND
"</pre>" LINEEND
"</body>" LINEEND
"</html>" LINEEND,
setup_message_title,
setup_message_title,
setup_message_body,
encr_string);
free(setup_message_title);
free(setup_message_body);
}
cleanup:
sqlite3_finalize(stmt);
dc_key_unref(curr_private_key);
free(encr_string);
free(self_addr);
return ret_setupfilecontent;
}
/**
* Parse the given file content and extract the private key.
*
* @private @memberof dc_context_t
* @param context The context object
* @param passphrase The setup code that shall be used to decrypt the message.
* May be created by dc_create_setup_code() on another device or by
* a completely different app as Thunderbird/Enigmail or K-9.
* @param filecontent The file content of the setup message, may be HTML.