mattosaurus / PgpCore

.NET Core class library for using PGP
MIT License
245 stars 98 forks source link

DecryptFileAsync: Message is not a simple encrypted file. #214

Closed martinstenhoff closed 1 year ago

martinstenhoff commented 1 year ago

Seems like this issues has been reported before in other places of PgpCore.

In our case it is triggered in the private async Task DecryptAsync(Stream inputStream, Stream outputStream)

If we change if (message is PgpOnePassSignatureList) in PGP.cs L5030 to if (message is PgpOnePassSignatureList || message is PgpSignatureList) then the file we have received can successfully be decrypted.

I don't know how the file has been generated other than it seems to be done by BouncyCastle Java?:

-----BEGIN PGP MESSAGE-----
Version: BCPG v1.61

I had a quick look at private async Task DecryptAndVerifyAsync(Stream inputStream, Stream outputStream) as well and the following should perhaps be added:

else if (message is PgpSignatureList pgpSignatureList)
{
    var keyIdToVerify = pgpSignatureList[0].KeyId;
    var verified = Utilities.FindPublicKey(keyIdToVerify, EncryptionKeys.VerificationKeys,
                            out PgpPublicKey _);
    if (verified == false)
         throw new PgpException("Failed to verify file.");

    message = plainFact.NextPgpObject();
}

But I can't verify that at the moment cause I only have the public key, that I believe was used to sign, in a scanned document and I don't want to transcribe it. I'll update this when I receive it in proper format.

mattosaurus commented 1 year ago

Hi, yes we pretty much just have a big if statement here for each type so we occasionally get ones I haven't thought of yet which cause the same error.

I think this could definitely need some improvement so if you're happy to put in a PR to fix it for use case or to add the else in then that would be great.