/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to mandos

  • Committer: Teddy Hogeborn
  • Date: 2012-11-13 21:45:10 UTC
  • Revision ID: teddy@recompile.se-20121113214510-34tvoek6sg6e366i
* mandos: Don't use the GnuPGInterface module.
  (PGPEngine): Use straight subprocess.Popen() to call "gpg".
* debian/control (mandos/Depends): Remove "python-gnupginterface".
                                   Add "gnupg (<< 2)".

Show diffs side-by-side

added added

removed removed

Lines of Context:
79
79
import ctypes.util
80
80
import xml.dom.minidom
81
81
import inspect
82
 
import GnuPGInterface
83
82
 
84
83
try:
85
84
    SO_BINDTODEVICE = socket.SO_BINDTODEVICE
140
139
class PGPEngine(object):
141
140
    """A simple class for OpenPGP symmetric encryption & decryption"""
142
141
    def __init__(self):
143
 
        self.gnupg = GnuPGInterface.GnuPG()
144
142
        self.tempdir = tempfile.mkdtemp(prefix="mandos-")
145
 
        self.gnupg = GnuPGInterface.GnuPG()
146
 
        self.gnupg.options.meta_interactive = False
147
 
        self.gnupg.options.homedir = self.tempdir
148
 
        self.gnupg.options.extra_args.extend(['--force-mdc',
149
 
                                              '--quiet',
150
 
                                              '--no-use-agent'])
 
143
        self.gnupgargs = ['--batch',
 
144
                          '--home', self.tempdir,
 
145
                          '--force-mdc',
 
146
                          '--quiet',
 
147
                          '--no-use-agent']
151
148
    
152
149
    def __enter__(self):
153
150
        return self
178
175
        return b"mandos" + binascii.hexlify(password)
179
176
    
180
177
    def encrypt(self, data, password):
181
 
        self.gnupg.passphrase = self.password_encode(password)
182
 
        with open(os.devnull, "w") as devnull:
183
 
            try:
184
 
                proc = self.gnupg.run(['--symmetric'],
185
 
                                      create_fhs=['stdin', 'stdout'],
186
 
                                      attach_fhs={'stderr': devnull})
187
 
                with contextlib.closing(proc.handles['stdin']) as f:
188
 
                    f.write(data)
189
 
                with contextlib.closing(proc.handles['stdout']) as f:
190
 
                    ciphertext = f.read()
191
 
                proc.wait()
192
 
            except IOError as e:
193
 
                raise PGPError(e)
194
 
        self.gnupg.passphrase = None
 
178
        passphrase = self.password_encode(password)
 
179
        with tempfile.NamedTemporaryFile(dir=self.tempdir
 
180
                                         ) as passfile:
 
181
            passfile.write(passphrase)
 
182
            passfile.flush()
 
183
            proc = subprocess.Popen(['gpg', '--symmetric',
 
184
                                     '--passphrase-file',
 
185
                                     passfile.name]
 
186
                                    + self.gnupgargs,
 
187
                                    stdin = subprocess.PIPE,
 
188
                                    stdout = subprocess.PIPE,
 
189
                                    stderr = subprocess.PIPE)
 
190
            ciphertext, err = proc.communicate(input = data)
 
191
        if proc.returncode != 0:
 
192
            raise PGPError(err)
195
193
        return ciphertext
196
194
    
197
195
    def decrypt(self, data, password):
198
 
        self.gnupg.passphrase = self.password_encode(password)
199
 
        with open(os.devnull, "w") as devnull:
200
 
            try:
201
 
                proc = self.gnupg.run(['--decrypt'],
202
 
                                      create_fhs=['stdin', 'stdout'],
203
 
                                      attach_fhs={'stderr': devnull})
204
 
                with contextlib.closing(proc.handles['stdin']) as f:
205
 
                    f.write(data)
206
 
                with contextlib.closing(proc.handles['stdout']) as f:
207
 
                    decrypted_plaintext = f.read()
208
 
                proc.wait()
209
 
            except IOError as e:
210
 
                raise PGPError(e)
211
 
        self.gnupg.passphrase = None
 
196
        passphrase = self.password_encode(password)
 
197
        with tempfile.NamedTemporaryFile(dir = self.tempdir
 
198
                                         ) as passfile:
 
199
            passfile.write(passphrase)
 
200
            passfile.flush()
 
201
            proc = subprocess.Popen(['gpg', '--decrypt',
 
202
                                     '--passphrase-file',
 
203
                                     passfile.name]
 
204
                                    + self.gnupgargs,
 
205
                                    stdin = subprocess.PIPE,
 
206
                                    stdout = subprocess.PIPE,
 
207
                                    stderr = subprocess.PIPE)
 
208
            decrypted_plaintext, err = proc.communicate(input
 
209
                                                        = data)
 
210
        if proc.returncode != 0:
 
211
            raise PGPError(err)
212
212
        return decrypted_plaintext
213
213
 
214
214