second test for gnupg
This commit is contained in:
parent
9b98db2a9f
commit
a3126a453e
|
@ -25,171 +25,170 @@ try:
|
||||||
import GnuPGInterface
|
import GnuPGInterface
|
||||||
except:
|
except:
|
||||||
USE_GPG = 0
|
USE_GPG = 0
|
||||||
return
|
else:
|
||||||
|
class GnuPG(GnuPGInterface.GnuPG):
|
||||||
|
def __init__(self):
|
||||||
|
GnuPGInterface.GnuPG.__init__(self)
|
||||||
|
self._setup_my_options()
|
||||||
|
|
||||||
class GnuPG(GnuPGInterface.GnuPG):
|
def _setup_my_options(self):
|
||||||
def __init__(self):
|
self.options.armor = 1
|
||||||
GnuPGInterface.GnuPG.__init__(self)
|
self.options.meta_interactive = 0
|
||||||
self._setup_my_options()
|
self.options.extra_args.append('--no-secmem-warning')
|
||||||
|
# Nolith's patch - prevent crashs on non fully-trusted keys
|
||||||
|
self.options.extra_args.append('--always-trust')
|
||||||
|
|
||||||
def _setup_my_options(self):
|
def _read_response(self, child_stdout):
|
||||||
self.options.armor = 1
|
# Internal method: reads all the output from GPG, taking notice
|
||||||
self.options.meta_interactive = 0
|
# only of lines that begin with the magic [GNUPG:] prefix.
|
||||||
self.options.extra_args.append('--no-secmem-warning')
|
# (See doc/DETAILS in the GPG distribution for info on GPG's
|
||||||
# Nolith's patch - prevent crashs on non fully-trusted keys
|
# output when --status-fd is specified.)
|
||||||
self.options.extra_args.append('--always-trust')
|
#
|
||||||
|
# Returns a dictionary, mapping GPG's keywords to the arguments
|
||||||
|
# for that keyword.
|
||||||
|
|
||||||
def _read_response(self, child_stdout):
|
resp = {}
|
||||||
# Internal method: reads all the output from GPG, taking notice
|
while 1:
|
||||||
# only of lines that begin with the magic [GNUPG:] prefix.
|
line = child_stdout.readline()
|
||||||
# (See doc/DETAILS in the GPG distribution for info on GPG's
|
if line == "": break
|
||||||
# output when --status-fd is specified.)
|
line = line.rstrip()
|
||||||
#
|
if line[0:9] == '[GNUPG:] ':
|
||||||
# Returns a dictionary, mapping GPG's keywords to the arguments
|
# Chop off the prefix
|
||||||
# for that keyword.
|
line = line[9:]
|
||||||
|
L = line.split(None, 1)
|
||||||
|
keyword = L[0]
|
||||||
|
if len(L) > 1:
|
||||||
|
resp[ keyword ] = L[1]
|
||||||
|
else:
|
||||||
|
resp[ keyword ] = ""
|
||||||
|
return resp
|
||||||
|
|
||||||
resp = {}
|
def encrypt(self, str, recipients):
|
||||||
while 1:
|
if not USE_GPG:
|
||||||
line = child_stdout.readline()
|
return str
|
||||||
if line == "": break
|
self.options.recipients = recipients # a list!
|
||||||
line = line.rstrip()
|
|
||||||
if line[0:9] == '[GNUPG:] ':
|
|
||||||
# Chop off the prefix
|
|
||||||
line = line[9:]
|
|
||||||
L = line.split(None, 1)
|
|
||||||
keyword = L[0]
|
|
||||||
if len(L) > 1:
|
|
||||||
resp[ keyword ] = L[1]
|
|
||||||
else:
|
|
||||||
resp[ keyword ] = ""
|
|
||||||
return resp
|
|
||||||
|
|
||||||
def encrypt(self, str, recipients):
|
proc = self.run(['--encrypt'], create_fhs=['stdin', 'stdout'])
|
||||||
if not USE_GPG:
|
proc.handles['stdin'].write(str)
|
||||||
return str
|
proc.handles['stdin'].close()
|
||||||
self.options.recipients = recipients # a list!
|
|
||||||
|
|
||||||
proc = self.run(['--encrypt'], create_fhs=['stdin', 'stdout'])
|
output = proc.handles['stdout'].read()
|
||||||
proc.handles['stdin'].write(str)
|
proc.handles['stdout'].close()
|
||||||
proc.handles['stdin'].close()
|
|
||||||
|
|
||||||
output = proc.handles['stdout'].read()
|
try: proc.wait()
|
||||||
proc.handles['stdout'].close()
|
except IOError: pass
|
||||||
|
|
||||||
try: proc.wait()
|
|
||||||
except IOError: pass
|
|
||||||
return self._stripHeaderFooter(output)
|
|
||||||
|
|
||||||
def decrypt(self, str, keyID):
|
|
||||||
if not USE_GPG:
|
|
||||||
return str
|
|
||||||
proc = self.run(['--decrypt', '-q', '-u %s'%keyID], create_fhs=['stdin', 'stdout', 'status'])
|
|
||||||
enc = self._addHeaderFooter(str, 'MESSAGE')
|
|
||||||
proc.handles['stdin'].write(enc)
|
|
||||||
proc.handles['stdin'].close()
|
|
||||||
|
|
||||||
output = proc.handles['stdout'].read()
|
|
||||||
proc.handles['stdout'].close()
|
|
||||||
|
|
||||||
resp = proc.handles['status'].read()
|
|
||||||
proc.handles['status'].close()
|
|
||||||
|
|
||||||
try: proc.wait()
|
|
||||||
except IOError: pass
|
|
||||||
return output
|
|
||||||
|
|
||||||
def sign(self, str, keyID):
|
|
||||||
if not USE_GPG:
|
|
||||||
return str
|
|
||||||
proc = self.run(['-b', '-u %s'%keyID], create_fhs=['stdin', 'stdout', 'status', 'stderr'])
|
|
||||||
proc.handles['stdin'].write(str)
|
|
||||||
proc.handles['stdin'].close()
|
|
||||||
|
|
||||||
output = proc.handles['stdout'].read()
|
|
||||||
proc.handles['stdout'].close()
|
|
||||||
proc.handles['stderr'].close()
|
|
||||||
|
|
||||||
stat = proc.handles['status']
|
|
||||||
resp = self._read_response(stat)
|
|
||||||
proc.handles['status'].close()
|
|
||||||
|
|
||||||
try: proc.wait()
|
|
||||||
except IOError: pass
|
|
||||||
if resp.has_key('BAD_PASSPHRASE'):
|
|
||||||
return 'BAD_PASSPHRASE'
|
|
||||||
elif resp.has_key('GOOD_PASSPHRASE'):
|
|
||||||
return self._stripHeaderFooter(output)
|
return self._stripHeaderFooter(output)
|
||||||
|
|
||||||
def verify(self, str, sign):
|
def decrypt(self, str, keyID):
|
||||||
if not USE_GPG:
|
if not USE_GPG:
|
||||||
return str
|
return str
|
||||||
if not str:
|
proc = self.run(['--decrypt', '-q', '-u %s'%keyID], create_fhs=['stdin', 'stdout', 'status'])
|
||||||
return ''
|
enc = self._addHeaderFooter(str, 'MESSAGE')
|
||||||
file = TemporaryFile(prefix='gajim')
|
proc.handles['stdin'].write(enc)
|
||||||
fd = file.fileno()
|
proc.handles['stdin'].close()
|
||||||
file.write(str)
|
|
||||||
file.seek(0)
|
|
||||||
|
|
||||||
proc = self.run(['--verify', '--enable-special-filenames', '-', '-&%s'%fd], create_fhs=['stdin', 'status', 'stderr'])
|
output = proc.handles['stdout'].read()
|
||||||
|
proc.handles['stdout'].close()
|
||||||
|
|
||||||
file.close()
|
resp = proc.handles['status'].read()
|
||||||
sign = self._addHeaderFooter(sign, 'SIGNATURE')
|
proc.handles['status'].close()
|
||||||
proc.handles['stdin'].write(sign)
|
|
||||||
proc.handles['stdin'].close()
|
|
||||||
proc.handles['stderr'].close()
|
|
||||||
|
|
||||||
stat = proc.handles['status']
|
try: proc.wait()
|
||||||
resp = self._read_response(stat)
|
except IOError: pass
|
||||||
proc.handles['status'].close()
|
return output
|
||||||
|
|
||||||
try: proc.wait()
|
def sign(self, str, keyID):
|
||||||
except IOError: pass
|
if not USE_GPG:
|
||||||
|
return str
|
||||||
|
proc = self.run(['-b', '-u %s'%keyID], create_fhs=['stdin', 'stdout', 'status', 'stderr'])
|
||||||
|
proc.handles['stdin'].write(str)
|
||||||
|
proc.handles['stdin'].close()
|
||||||
|
|
||||||
keyid = ''
|
output = proc.handles['stdout'].read()
|
||||||
if resp.has_key('GOODSIG'):
|
proc.handles['stdout'].close()
|
||||||
keyid = resp['GOODSIG'].split()[0]
|
proc.handles['stderr'].close()
|
||||||
elif resp.has_key('BADSIG'):
|
|
||||||
keyid = resp['BADSIG'].split()[0]
|
|
||||||
return keyid
|
|
||||||
|
|
||||||
def get_secret_keys(self):
|
stat = proc.handles['status']
|
||||||
if not USE_GPG:
|
resp = self._read_response(stat)
|
||||||
return
|
proc.handles['status'].close()
|
||||||
proc = self.run(['--with-colons', '--list-secret-keys'], \
|
|
||||||
create_fhs=['stdout'])
|
|
||||||
output = proc.handles['stdout'].read()
|
|
||||||
proc.handles['stdout'].close()
|
|
||||||
|
|
||||||
keys = {}
|
try: proc.wait()
|
||||||
lines = output.split('\n')
|
except IOError: pass
|
||||||
for line in lines:
|
if resp.has_key('BAD_PASSPHRASE'):
|
||||||
sline = line.split(':')
|
return 'BAD_PASSPHRASE'
|
||||||
if sline[0] == 'sec':
|
elif resp.has_key('GOOD_PASSPHRASE'):
|
||||||
keys[sline[4][8:]] = sline[9]
|
return self._stripHeaderFooter(output)
|
||||||
return keys
|
|
||||||
try: proc.wait()
|
|
||||||
except IOError: pass
|
|
||||||
|
|
||||||
def _stripHeaderFooter(self, data):
|
def verify(self, str, sign):
|
||||||
"""Remove header and footer from data"""
|
if not USE_GPG:
|
||||||
lines = data.split('\n')
|
return str
|
||||||
while lines[0] != '':
|
if not str:
|
||||||
lines.remove(lines[0])
|
return ''
|
||||||
while lines[0] == '':
|
file = TemporaryFile(prefix='gajim')
|
||||||
lines.remove(lines[0])
|
fd = file.fileno()
|
||||||
i = 0
|
file.write(str)
|
||||||
for line in lines:
|
file.seek(0)
|
||||||
if line:
|
|
||||||
if line[0] == '-': break
|
|
||||||
i = i+1
|
|
||||||
line = '\n'.join(lines[0:i])
|
|
||||||
return line
|
|
||||||
|
|
||||||
def _addHeaderFooter(self, data, type):
|
proc = self.run(['--verify', '--enable-special-filenames', '-', '-&%s'%fd], create_fhs=['stdin', 'status', 'stderr'])
|
||||||
"""Add header and footer from data"""
|
|
||||||
out = "-----BEGIN PGP %s-----\n" % type
|
file.close()
|
||||||
out = out + "Version: PGP\n"
|
sign = self._addHeaderFooter(sign, 'SIGNATURE')
|
||||||
out = out + "\n"
|
proc.handles['stdin'].write(sign)
|
||||||
out = out + data + "\n"
|
proc.handles['stdin'].close()
|
||||||
out = out + "-----END PGP %s-----\n" % type
|
proc.handles['stderr'].close()
|
||||||
return out
|
|
||||||
|
stat = proc.handles['status']
|
||||||
|
resp = self._read_response(stat)
|
||||||
|
proc.handles['status'].close()
|
||||||
|
|
||||||
|
try: proc.wait()
|
||||||
|
except IOError: pass
|
||||||
|
|
||||||
|
keyid = ''
|
||||||
|
if resp.has_key('GOODSIG'):
|
||||||
|
keyid = resp['GOODSIG'].split()[0]
|
||||||
|
elif resp.has_key('BADSIG'):
|
||||||
|
keyid = resp['BADSIG'].split()[0]
|
||||||
|
return keyid
|
||||||
|
|
||||||
|
def get_secret_keys(self):
|
||||||
|
if not USE_GPG:
|
||||||
|
return
|
||||||
|
proc = self.run(['--with-colons', '--list-secret-keys'], \
|
||||||
|
create_fhs=['stdout'])
|
||||||
|
output = proc.handles['stdout'].read()
|
||||||
|
proc.handles['stdout'].close()
|
||||||
|
|
||||||
|
keys = {}
|
||||||
|
lines = output.split('\n')
|
||||||
|
for line in lines:
|
||||||
|
sline = line.split(':')
|
||||||
|
if sline[0] == 'sec':
|
||||||
|
keys[sline[4][8:]] = sline[9]
|
||||||
|
return keys
|
||||||
|
try: proc.wait()
|
||||||
|
except IOError: pass
|
||||||
|
|
||||||
|
def _stripHeaderFooter(self, data):
|
||||||
|
"""Remove header and footer from data"""
|
||||||
|
lines = data.split('\n')
|
||||||
|
while lines[0] != '':
|
||||||
|
lines.remove(lines[0])
|
||||||
|
while lines[0] == '':
|
||||||
|
lines.remove(lines[0])
|
||||||
|
i = 0
|
||||||
|
for line in lines:
|
||||||
|
if line:
|
||||||
|
if line[0] == '-': break
|
||||||
|
i = i+1
|
||||||
|
line = '\n'.join(lines[0:i])
|
||||||
|
return line
|
||||||
|
|
||||||
|
def _addHeaderFooter(self, data, type):
|
||||||
|
"""Add header and footer from data"""
|
||||||
|
out = "-----BEGIN PGP %s-----\n" % type
|
||||||
|
out = out + "Version: PGP\n"
|
||||||
|
out = out + "\n"
|
||||||
|
out = out + data + "\n"
|
||||||
|
out = out + "-----END PGP %s-----\n" % type
|
||||||
|
return out
|
||||||
|
|
Loading…
Reference in New Issue