text
stringlengths
0
357
context->cb(context, DC_EVENT_IMEX_FILE_WRITTEN, (uintptr_t)file_name, 0);
success = 1;
cleanup:
free(file_name);
return success;
}
static int export_self_keys(dc_context_t* context, const char* dir)
{
int success = 0;
int export_errors = 0;
sqlite3_stmt* stmt = NULL;
int id = 0;
int is_default = 0;
dc_key_t* public_key = dc_key_new();
dc_key_t* private_key = dc_key_new();
if ((stmt=dc_sqlite3_prepare(context->sql, "SELECT id, public_key, private_key, is_default FROM keypairs;"))==NULL) {
goto cleanup;
}
while (sqlite3_step(stmt)==SQLITE_ROW) {
id = sqlite3_column_int( stmt, 0 );
dc_key_set_from_stmt(public_key, stmt, 1, DC_KEY_PUBLIC);
dc_key_set_from_stmt(private_key, stmt, 2, DC_KEY_PRIVATE);
is_default = sqlite3_column_int( stmt, 3 );
if(!export_key_to_asc_file(context, dir, id, public_key, is_default)) {
export_errors++;
}
if (!export_key_to_asc_file(context, dir, id, private_key, is_default)) {
export_errors++;
}
}
if (export_errors==0) {
success = 1;
}
cleanup:
sqlite3_finalize(stmt);
dc_key_unref(public_key);
dc_key_unref(private_key);
return success;
}
/*******************************************************************************
* Classic key import
******************************************************************************/
static int import_self_keys(dc_context_t* context, const char* dir_name)
{
/* hint: even if we switch to import Autocrypt Setup Files, we should leave the possibility to import
plain ASC keys, at least keys without a password, if we do not want to implement a password entry function.
Importing ASC keys is useful to use keys in Delta Chat used by any other non-Autocrypt-PGP implementation.
Maybe we should make the "default" key handlong also a little bit smarter
(currently, the last imported key is the standard key unless it contains the string "legacy" in its name) */
int imported_cnt = 0;
DIR* dir_handle = NULL;
struct dirent* dir_entry = NULL;
char* suffix = NULL;
char* path_plus_name = NULL;
int set_default = 0;
char* buf = NULL;
size_t buf_bytes = 0;
const char* private_key = NULL; // a pointer inside buf, MUST NOT be free()'d
char* buf2 = NULL;
const char* buf2_headerline = NULL; // a pointer inside buf2, MUST NOT be free()'d
if (context==NULL || context->magic!=DC_CONTEXT_MAGIC || dir_name==NULL) {
goto cleanup;
}
if ((dir_handle=opendir(dir_name))==NULL) {
dc_log_error(context, 0, "Import: Cannot open directory \"%s\".", dir_name);
goto cleanup;
}
while ((dir_entry=readdir(dir_handle))!=NULL)
{
free(suffix);
suffix = dc_get_filesuffix_lc(dir_entry->d_name);
if (suffix==NULL || strcmp(suffix, "asc")!=0) {
continue;
}
free(path_plus_name);
path_plus_name = dc_mprintf("%s/%s", dir_name, dir_entry->d_name/* name without path; may also be `.` or `..` */);
dc_log_info(context, 0, "Checking: %s", path_plus_name);
free(buf);
buf = NULL;
if (!dc_read_file(context, path_plus_name, (void**)&buf, &buf_bytes)
|| buf_bytes < 50) {
continue;