text
stringlengths
0
357
CHECK_EXIT
if ((msg_id = dc_send_msg(context, chat_id, msg))==0) {
goto cleanup;
}
dc_msg_unref(msg);
msg = NULL;
/* wait until the message is really sent */
dc_log_info(context, 0, "Wait for setup message being sent ...");
while (1)
{
CHECK_EXIT
sleep(1);
msg = dc_get_msg(context, msg_id);
if (dc_msg_is_sent(msg)) {
break;
}
dc_msg_unref(msg);
msg = NULL;
}
dc_log_info(context, 0, "... setup message sent.");
success = 1;
cleanup:
if (!success) { free(setup_code); setup_code = NULL; }
free(setup_file_name);
free(setup_file_content);
dc_msg_unref(msg);
dc_free_ongoing(context);
return setup_code;
}
static int set_self_key(dc_context_t* context, const char* armored, int set_default)
{
int success = 0;
char* buf = NULL;
const char* buf_headerline = NULL; // pointer inside buf, MUST NOT be free()'d
const char* buf_preferencrypt = NULL; // - " -
const char* buf_base64 = NULL; // - " -
dc_key_t* private_key = dc_key_new();
dc_key_t* public_key = dc_key_new();
sqlite3_stmt* stmt = NULL;
char* self_addr = NULL;
buf = dc_strdup(armored);
if (!dc_split_armored_data(buf, &buf_headerline, NULL, &buf_preferencrypt, &buf_base64)
|| strcmp(buf_headerline, "-----BEGIN PGP PRIVATE KEY BLOCK-----")!=0 || buf_base64==NULL) {
dc_log_warning(context, 0, "File does not contain a private key."); /* do not log as error - this is quite normal after entering the bad setup code */
goto cleanup;
}
if (!dc_key_set_from_base64(private_key, buf_base64, DC_KEY_PRIVATE)
|| !dc_pgp_is_valid_key(context, private_key)
|| !dc_pgp_split_key(context, private_key, public_key)) {
dc_log_error(context, 0, "File does not contain a valid private key.");
goto cleanup;
}
/* add keypair; before this, delete other keypairs with the same binary key and reset defaults */
stmt = dc_sqlite3_prepare(context->sql, "DELETE FROM keypairs WHERE public_key=? OR private_key=?;");
sqlite3_bind_blob (stmt, 1, public_key->binary, public_key->bytes, SQLITE_STATIC);
sqlite3_bind_blob (stmt, 2, private_key->binary, private_key->bytes, SQLITE_STATIC);
sqlite3_step(stmt);
sqlite3_finalize(stmt);
stmt = NULL;
if (set_default) {
dc_sqlite3_execute(context->sql, "UPDATE keypairs SET is_default=0;"); /* if the new key should be the default key, all other should not */
}
self_addr = dc_sqlite3_get_config(context->sql, "configured_addr", NULL);
if (!dc_key_save_self_keypair(public_key, private_key, self_addr, set_default, context->sql)) {
dc_log_error(context, 0, "Cannot save keypair.");
goto cleanup;
}
/* if we also received an Autocrypt-Prefer-Encrypt header, handle this */
if (buf_preferencrypt) {
if (strcmp(buf_preferencrypt, "nopreference")==0) {
dc_sqlite3_set_config_int(context->sql, "e2ee_enabled", 0);
}
else if (strcmp(buf_preferencrypt, "mutual")==0) {
dc_sqlite3_set_config_int(context->sql, "e2ee_enabled", 1);
}
}
success = 1;
cleanup:
sqlite3_finalize(stmt);
free(buf);
free(self_addr);