isislovecruft / python-gnupg

A modified version of python-gnupg, including security patches, extensive documentation, and extra features.
Other
424 stars 172 forks source link

PY3: 2.30 decrypt_file fails with "gpg: no valid OpenPGP data found" #198

Open nixwiz opened 7 years ago

nixwiz commented 7 years ago

Yes,

this is similar to #102 , but that has languished for 2 years.

Using the following sample program:

import gnupg
homedir='/home/vagrant'
gpg = gnupg.GPG(homedir=homedir)
stream = open('testfile.gpg', "rb")
gpg_passphrase="passphrase"
outpath = "testfile.dec"
decrypted = gpg.decrypt_file(stream, passphrase=gpg_passphrase, output=outpath, always_trust=True)
print("ok: {} status: {}\nstderr: {}".format(decrypted.ok, decrypted.status, decrypted.stderr))

Works with 2.7, but fails on 3.6 with "gpg: no valid OpenPGP data found". It appears to come down to how _write_passphrase sends the passphrase into gpg. Looking at strace output comparisons between 2.7 and 3.6 shows that the passphrase is written with extra characters in 3.6 (assuming this is a result of being encoded).

2.7 strace snippet (7917 is PID of python calling gpg as PID 7921):

7917  write(5, "passphrase\n", 11)      = 11
[lines deleted]
7921  read(0, "p", 1)                   = 1
7921  read(0, "a", 1)                   = 1
7921  read(0, "s", 1)                   = 1
7921  read(0, "s", 1)                   = 1
7921  read(0, "p", 1)                   = 1
7921  read(0, "h", 1)                   = 1
7921  read(0, "r", 1)                   = 1
7921  read(0, "a", 1)                   = 1
7921  read(0, "s", 1)                   = 1
7921  read(0, "e", 1)                   = 1
7921  read(0, "\n", 1)                  = 1
7921  read(0,  <unfinished ...>

3.6 strace snippet (7908 is PID of python calling gpg as PID):

7908  write(6, "passphrase\n\357\277\275\1\f\3\357\277\275\22\0q\357\277\275\357\277\275Un\1"..., 640) = 640
[lines deleted]
7907  <... read resumed> "p", 1)        = 1
7907  read(0, "a", 1)                   = 1
7907  read(0, "s", 1)                   = 1
7907  read(0, "s", 1)                   = 1
7907  read(0, "p", 1)                   = 1
7907  read(0, "h", 1)                   = 1
7907  read(0, "r", 1)                   = 1
7907  read(0, "a", 1)                   = 1
7907  read(0, "s", 1)                   = 1
7907  read(0, "e", 1)                   = 1
7907  read(0, "\n", 1)                  = 1
7907  read(0, "\357\277\275\1\f\3\357\277\275\22\0q\357\277\275\357\277\275Un\1\7\357\277\275\35_m\r\357\277\275"..., 8192) = 629
7907  read(0,  <unfinished ...>
instantname commented 7 years ago

Have a look at #102 again. I have revived the issue with what might be a fix.

dikshantshahi commented 3 years ago

I am using Python 3.9 and getting the same issue. Also #102 didnt help. Is there any resolution for this.