rpgp: Can't decrypt GPG encrypted file
Hi!
I’m trying to use a gpg generated key pair to encrypt/decrypt a file, but I consistently get PGP error: "unexpected packet data: Message(\"Partial body length is not allowed for packet type Other(20)\")". Do you have any pointers on what I’m doing wrong?
This is how I generate the keys, the keys are rsa2048, option 0. gpg version is 2.4.4, as well as encrypt:
$ gpg --full-generate-key --openpgp
$ gpg --export-secret-key -a "TEST@TEST" > local-test-privkey.asc
$ gpg --export -a "TEST@TEST" > local-test-pubkey.asc
$ gpg --encrypt --armor --recipient TEST@TEST my_test_file.xml
$ gpg --decrypt my_test_file.xml.asc
Decryption using gpg works. Ingesting the keys in rPGP seems to work too.
This is the decryption code:
/// Decrypts a message using the primary secret key, with a fallback to the old secret key if decryption fails.
pub async fn decrypt_message(&self, encrypted_message: &[u8]) -> Result<Vec<u8>, Error> {
let encrypted_message = encrypted_message.to_vec();
let secret_key = self.secret_key.clone();
let old_secret_key = self.old_secret_key.clone();
task::spawn_blocking({
move || {
let encrypted_str = String::from_utf8(encrypted_message)?;
let (message, _) = Message::from_string(&encrypted_str)?;
// Attempt decryption with the primary secret key
if let Ok(decrypted) =
message.decrypt(|| secret_key.passphrase.clone(), &[&secret_key.key])
{
return Ok(decrypted.0.get_content()?.ok_or(Error::EmptyMessage));
}
// If decryption with the primary key fails, attempt with the old secret key if available
if let Some(old_key) = old_secret_key {
if let Ok(decrypted) =
message.decrypt(|| old_key.passphrase.clone(), &[&old_key.key])
{
return Ok(decrypted.0.get_content()?.ok_or(Error::EmptyMessage));
}
}
// If both decryption attempts fail, return an error
Err(Error::DecryptionFailed)
}
})
.await??
}
I’ve tried different key generation parameters (with/without --openpgp), different ciphers, as well as different ways to encrypt the message, but nothing seems to work!
Would appreciate any pointers regarding what I’m doing wrong! Feels like there must be something super obvious I’m missing!
Thanks a lot for your work on the library!
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 20 (14 by maintainers)
That sounds like a good direction!
Please be aware that validity of component keys in OpenPGP is a relatively complex issue, with quite a lot of potential tripwires (which is why I am trying to implement a simple, but also hopefully correct and complete, version of those semantics in rpgpie).
You definitely want to make sure that you encrypt to whatever component key is valid for encryption.
If you only ever use your code with keys that are generated under your control, where you know what “shape” to expect, then that can be handled with relatively simple code. Like, you might know that all of your recipient keys only ever have one subkey, which is for encryption. and that those never expire, and never get revoked.
However, if you handle keys from “in the wild”, those can have a pretty wide range of properties.
As I wrote above, implementing the “higher level OpenPGP semantics” such as “which component key is valid to encrypt to” is what my rpgpie project does, because rPGP itself is a lower level abstraction (I think is a very sensible design to have two layers like that … but it means that the low level API isn’t always convenient - or safe - for casual use 😅).
So, I generated the key, edited with
--edit-key, and now my code works with them the way I want to. So I feel confident sending the pubkey over to people who’ll produce the messages!I think this can be closed! It’s not really a
rpgpissue, but also, you helped me fix it for me too!Thanks a lot!
Took me some time to recall where my own battle with disabling GnuPG’s non-standard “OCB” encryption mode took place. This is what I did: https://codeberg.org/heiko/openpgp-piv/src/branch/main/cli/ci/gpgsm/disable-aead.exp
This edits the preferences that the key shows - and removes the preference that signals support for GnuPG’s mode. The preferences are a signal to the encrypting party, telling it what kind of encryption the recipient supports.
Note that I am not sure if this approach will still work with current GnuPG 2.4.x, but if you find out I’d love to know!
If you generate keys with another implementation, e.g. by using one of the many Stateless OpenPGP CLI tools, e.g. rsop which is based on rPGP, then that key does not advertise support for the “GnuPG packet type 20” feature.
When using GnuPG to encrypt to such a key, I’d hope that it will not emit its proprietary encryption format.