DeltaChat fails to import keys

Delta Chat version

1.36.5

Expected behavior

Some time ago, I exported keys from DeltaChat running on an Android device and copied them over to my PC using adb pull. Using adb push, I now copied them over to a new Android device into the directory suggested by DetaChat’s “Import secret key” dialog and verified their presence as well as file permissions using adb shell:

payton:/storage/emulated/0/Download $ ls -l
total 8
-rw-rw---- 1 root everybody 3498 2020-01-12 09:43 private-key-1.asc
-rw-rw---- 1 root everybody 1733 2020-01-12 09:43 public-key-1.asc

When in the “Import secret key” dialog I tap [OK], I expect the key files to get successfully imported.

Actual behavior

After tapping [OK], I get a message saying “IMEX failed to compete: No private keys found in ‘/storage/emulated/0/Download’.”

Steps to reproduce the problem

Described above.

Debug logs

05-29 19:43:57.690 I/DeltaChat(16335): src/scheduler.rs:82: stopping IO
05-29 19:43:57.695 I/DeltaChat(16335): src/scheduler.rs:345: shutting down inbox loop
05-29 19:43:57.695 I/DeltaChat(16335): src/scheduler.rs:552: shutting down simple loop
05-29 19:43:57.695 I/DeltaChat(16335): src/scheduler.rs:633: shutting down smtp loop
05-29 19:43:57.701 I/DeltaChat(16335): src/scheduler.rs:82: stopping IO
05-29 19:43:57.701 I/DeltaChat(16335): src/imex.rs:379: Import/export dir: /storage/emulated/0/Download
05-29 19:43:57.713 E/DeltaChat(16335): IMEX failed to complete: No private keys found in "/storage/emulated/0/Download".
05-29 19:43:57.713 W/DeltaChat(16335): deltachat-ffi/src/lib.rs:2192: IMEX failed: No private keys found in "/storage/emulated/0
/Download".
05-29 19:43:57.754 I/DeltaChat(16335): src/scheduler.rs:60: starting IO
05-29 19:43:57.754 I/DeltaChat(16335): src/scheduler.rs:252: starting inbox loop
05-29 19:43:57.754 I/DeltaChat(16335): src/job.rs:264: Loading job.
05-29 19:43:57.754 I/DeltaChat(16335): src/scheduler.rs:529: starting simple loop for Mvbox
05-29 19:43:57.754 I/DeltaChat(16335): src/imap.rs:309: Connecting to IMAP server
05-29 19:43:57.754 I/DeltaChat(16335): src/scheduler.rs:561: starting smtp loop
05-29 19:43:57.757 I/DeltaChat(16335): src/ephemeral.rs:565: Ephemeral loop waiting for deletion in 24h 0m 0s or interrupt
05-29 19:43:57.759 I/DeltaChat(16335): src/imap.rs:309: Connecting to IMAP server
05-29 19:43:57.760 I/DeltaChat(16335): src/scheduler.rs:724: scheduler is running
05-29 19:43:57.760 I/DeltaChat(16335): src/location.rs:654: Location loop is waiting for 24h 0m 0s or interrupt
05-29 19:43:57.762 I/DeltaChat(16335): src/smtp.rs:754: Sending MDNs
05-29 19:43:57.762 I/DeltaChat(16335): src/contact.rs:1609: Recently seen loop waiting for 24h 0m 0s or interrupt
05-29 19:43:57.764 I/DeltaChat(16335): src/scheduler.rs:600: smtp fake idle - started
05-29 19:43:57.764 I/DeltaChat(16335): src/scheduler.rs:622: smtp has no messages to retry, waiting for interrupt
05-29 19:43:57.774 I/DeltaChat(16335): src/net.rs:70: Resolved <some.mail.server>:993 into [2a01:488:42:1000:b24d:51f1:
c9:16d9]:993.
05-29 19:43:57.774 I/DeltaChat(16335): src/net.rs:70: Resolved <some.mail.server>:993 into 178.77.81.241:993.
05-29 19:43:57.777 I/DeltaChat(16335): src/net.rs:70: Resolved <some.mail.server>:993 into [2a01:488:42:1000:b24d:51f1:
c9:16d9]:993.
05-29 19:43:57.778 I/DeltaChat(16335): src/net.rs:70: Resolved <some.mail.server>:993 into 178.77.81.241:993.
05-29 19:43:57.926 I/DeltaChat(16335): src/imap.rs:378: Logging into IMAP server with LOGIN
05-29 19:43:57.968 I/DeltaChat(16335): src/imap.rs:378: Logging into IMAP server with LOGIN
05-29 19:43:57.988 I/DeltaChat(16335): src/imap.rs:394: Successfully logged into IMAP server
05-29 19:43:58.008 I/DeltaChat(16335): src/imap.rs:720: No new emails in folder DeltaChat
05-29 19:43:58.025 I/DeltaChat(16335): src/scheduler.rs:493: IMAP session supports IDLE, using it.
05-29 19:43:58.027 I/DeltaChat(16335): src/imap.rs:394: Successfully logged into IMAP server
05-29 19:43:58.042 I/DeltaChat(16335): src/imap/idle.rs:59: DeltaChat: Idle entering wait-on-remote state
05-29 19:43:58.046 I/DeltaChat(16335): src/imap.rs:720: No new emails in folder INBOX
05-29 19:43:58.067 I/DeltaChat(16335): src/scheduler.rs:493: IMAP session supports IDLE, using it.
05-29 19:43:58.082 I/DeltaChat(16335): src/imap/idle.rs:59: INBOX: Idle entering wait-on-remote state

This looks like a permission problem to me. The code which looks through the directory is here, and it should at least output the “considering key file” log:

Could you check if the application has the permission to read the storage? Maybe you denied it when it was requested?

Hm, I can’t remember being asked for any permissions when installing DeltaChat on the new device (which runs LineageOS 20). When checking DeltaChat permissions in the “Apps” category of the system settings, I find permissions for reading audio, video and image files, but no general permission of reading the storage. Or is there any other permissions settings page somewhere hidden in the system settings which I have overlooked?

BTW, DeltaChat is capable of exporting the keys in to the mentioned location, so write permissions seem to be granted.

Hi, maybe it is of interest what I figured out as a workaround here. I had the same situation, that DeltaChat fails to import keys. I am on Android 11 (LineageOS 18.1) and it looks like a very odd permission problem…

Maybe we should export both private and public key to a single file. Selecting a file on Android and similarly sandboxed platforms like Flatpak seems to be much less error-prone than selecting a folder, there are no failures like “granted access to the folder but not the files inside of it” possible.

gpg itself does not allow to use both --export and --export-secret-keys though, so having such file with both keys concatenated is probably not common.

Maybe it is possible to drop the public key and reconstruct it from the private key, then there is no need to import/export the public key at all, or only allow to export it for publishing somewhere?

EDIT: Information Security Stack Exchange question suggests that private key export contains the public key as well. So exporting public key should not be necessary to transfer
And the import function in the core only needs the secret key file, because it extracts the public key from the secret key:

EDIT2: I made a PR to stop using public keys in the tests: refactor: ignore public key argument in dc_preconfigure_keypair() by link2xt · Pull Request #4701 · deltachat/deltachat-core-rust · GitHub
If this works out, we can do the same for import/export and import/export a single “secret” file, thus not dealing with the directories anymore.

Note: there is a similar issue in the desktop repository, the requirement to select a directory rather than a file is confusing:

Going to make a fix for the core to be able to open a file with a secret key directly, this may not only fix the Android permission problem but be a better UI for the desktop.

android fix:

desktop fix: