2010-11-30 11:41:14 +01:00
|
|
|
## src/common/gpg.py
|
2005-04-12 13:46:20 +02:00
|
|
|
##
|
2014-01-02 09:33:54 +01:00
|
|
|
## Copyright (C) 2003-2014 Yann Leboulanger <asterix AT lagaule.org>
|
2008-08-15 05:20:23 +02:00
|
|
|
## Copyright (C) 2005 Alex Mauer <hawke AT hawkesnest.net>
|
|
|
|
## Copyright (C) 2005-2006 Nikos Kouremenos <kourem AT gmail.com>
|
|
|
|
## Copyright (C) 2007 Stephan Erb <steve-e AT h3c.de>
|
|
|
|
## Copyright (C) 2008 Jean-Marie Traissard <jim AT lapin.org>
|
|
|
|
## Jonathan Schleifer <js-gajim AT webkeks.org>
|
2005-04-12 13:46:20 +02:00
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## This file is part of Gajim.
|
|
|
|
##
|
|
|
|
## Gajim is free software; you can redistribute it and/or modify
|
2005-04-12 13:46:20 +02:00
|
|
|
## it under the terms of the GNU General Public License as published
|
2007-10-22 13:13:13 +02:00
|
|
|
## by the Free Software Foundation; version 3 only.
|
2005-04-12 13:46:20 +02:00
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## Gajim is distributed in the hope that it will be useful,
|
2005-04-12 13:46:20 +02:00
|
|
|
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2008-08-15 05:20:23 +02:00
|
|
|
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2005-04-12 13:46:20 +02:00
|
|
|
## GNU General Public License for more details.
|
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## You should have received a copy of the GNU General Public License
|
2008-08-15 05:20:23 +02:00
|
|
|
## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
2007-10-22 13:13:13 +02:00
|
|
|
##
|
2005-04-12 13:46:20 +02:00
|
|
|
|
2013-01-02 13:54:02 +01:00
|
|
|
from common.gajim import HAVE_GPG
|
2010-11-29 18:44:22 +01:00
|
|
|
import os
|
2005-04-12 13:46:20 +02:00
|
|
|
|
2010-11-30 11:41:14 +01:00
|
|
|
if HAVE_GPG:
|
2013-01-01 21:06:16 +01:00
|
|
|
from common import gnupg
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2010-11-29 18:44:22 +01:00
|
|
|
class GnuPG(gnupg.GPG):
|
|
|
|
def __init__(self, use_agent=False):
|
|
|
|
gnupg.GPG.__init__(self)
|
2015-03-17 22:06:36 +01:00
|
|
|
self.encoding = 'utf-8'
|
2013-07-17 12:50:34 +02:00
|
|
|
self.decode_errors = 'replace'
|
2010-11-29 18:44:22 +01:00
|
|
|
self.passphrase = None
|
2010-02-08 15:08:40 +01:00
|
|
|
self.use_agent = use_agent
|
2013-12-01 10:06:38 +01:00
|
|
|
self.always_trust = [] # list of keyID to always trust
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def _setup_my_options(self):
|
|
|
|
self.options.armor = 1
|
|
|
|
self.options.meta_interactive = 0
|
|
|
|
self.options.extra_args.append('--no-secmem-warning')
|
|
|
|
# disable photo viewer when verifying keys
|
|
|
|
self.options.extra_args.append('--verify-options')
|
|
|
|
self.options.extra_args.append('no-show-photo')
|
|
|
|
if self.use_agent:
|
|
|
|
self.options.extra_args.append('--use-agent')
|
|
|
|
|
|
|
|
def encrypt(self, str_, recipients, always_trust=False):
|
2013-12-01 10:06:38 +01:00
|
|
|
trust = always_trust
|
|
|
|
if not trust:
|
2015-11-01 16:30:27 +01:00
|
|
|
# check if we trust all keys
|
2013-12-01 10:06:38 +01:00
|
|
|
trust = True
|
|
|
|
for key in recipients:
|
|
|
|
if key not in self.always_trust:
|
|
|
|
trust = False
|
2015-07-29 22:32:02 +02:00
|
|
|
if not trust:
|
|
|
|
# check that we'll be able to encrypt
|
2015-08-12 22:13:29 +02:00
|
|
|
result = super(GnuPG, self).list_keys(keys=recipients)
|
2015-07-29 22:32:02 +02:00
|
|
|
for key in result:
|
|
|
|
if key['trust'] not in ('f', 'u'):
|
2015-11-01 16:30:27 +01:00
|
|
|
if key['keyid'][-8:] not in self.always_trust:
|
|
|
|
return '', 'NOT_TRUSTED ' + key['keyid'][-8:]
|
|
|
|
else:
|
|
|
|
trust = True
|
2010-11-29 18:44:22 +01:00
|
|
|
result = super(GnuPG, self).encrypt(str_, recipients,
|
2013-12-01 10:06:38 +01:00
|
|
|
always_trust=trust, passphrase=self.passphrase)
|
2010-11-29 18:44:22 +01:00
|
|
|
|
|
|
|
if result.ok:
|
2010-02-08 15:08:40 +01:00
|
|
|
error = ''
|
2010-11-29 18:44:22 +01:00
|
|
|
else:
|
|
|
|
error = result.status
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2010-11-29 18:44:22 +01:00
|
|
|
return self._stripHeaderFooter(str(result)), error
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2010-11-29 18:44:22 +01:00
|
|
|
def decrypt(self, str_, keyID):
|
|
|
|
data = self._addHeaderFooter(str_, 'MESSAGE')
|
|
|
|
result = super(GnuPG, self).decrypt(data,
|
|
|
|
passphrase=self.passphrase)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2010-11-29 18:44:22 +01:00
|
|
|
return str(result)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def sign(self, str_, keyID):
|
2010-11-29 18:44:22 +01:00
|
|
|
result = super(GnuPG, self).sign(str_, keyid=keyID, detach=True,
|
|
|
|
passphrase=self.passphrase)
|
|
|
|
|
|
|
|
if result.fingerprint:
|
|
|
|
return self._stripHeaderFooter(str(result))
|
2012-05-18 16:58:51 +02:00
|
|
|
if result.status == 'key expired':
|
|
|
|
return 'KEYEXPIRED'
|
2010-02-08 15:08:40 +01:00
|
|
|
return 'BAD_PASSPHRASE'
|
|
|
|
|
|
|
|
def verify(self, str_, sign):
|
|
|
|
if str_ is None:
|
|
|
|
return ''
|
2013-11-30 10:10:39 +01:00
|
|
|
# Hash algorithm is not transfered in the signed presence stanza so try
|
|
|
|
# all algorithms. Text name for hash algorithms from RFC 4880 - section 9.4
|
|
|
|
hash_algorithms = ['SHA512', 'SHA384', 'SHA256', 'SHA224', 'SHA1', 'RIPEMD160']
|
|
|
|
for algo in hash_algorithms:
|
|
|
|
data = os.linesep.join(
|
|
|
|
['-----BEGIN PGP SIGNED MESSAGE-----',
|
|
|
|
'Hash: ' + algo,
|
|
|
|
'',
|
|
|
|
str_,
|
|
|
|
self._addHeaderFooter(sign, 'SIGNATURE')]
|
|
|
|
)
|
|
|
|
result = super(GnuPG, self).verify(data)
|
|
|
|
if result.valid:
|
|
|
|
return result.key_id
|
|
|
|
|
2010-11-29 18:44:22 +01:00
|
|
|
return ''
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2010-11-29 18:44:22 +01:00
|
|
|
def get_keys(self, secret=False):
|
2010-02-08 15:08:40 +01:00
|
|
|
keys = {}
|
2010-11-29 18:44:22 +01:00
|
|
|
result = super(GnuPG, self).list_keys(secret=secret)
|
|
|
|
for key in result:
|
|
|
|
# Take first not empty uid
|
|
|
|
keys[key['keyid'][8:]] = [uid for uid in key['uids'] if uid][0]
|
2010-02-08 15:08:40 +01:00
|
|
|
return keys
|
|
|
|
|
|
|
|
def get_secret_keys(self):
|
|
|
|
return self.get_keys(True)
|
|
|
|
|
|
|
|
def _stripHeaderFooter(self, data):
|
|
|
|
"""
|
|
|
|
Remove header and footer from data
|
|
|
|
"""
|
|
|
|
if not data: return ''
|
2010-11-29 18:44:22 +01:00
|
|
|
lines = data.splitlines()
|
2010-02-08 15:08:40 +01:00
|
|
|
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
|
|
|
|
"""
|
2010-11-29 18:44:22 +01:00
|
|
|
out = "-----BEGIN PGP %s-----" % type_ + os.linesep
|
|
|
|
out = out + "Version: PGP" + os.linesep
|
|
|
|
out = out + os.linesep
|
|
|
|
out = out + data + os.linesep
|
|
|
|
out = out + "-----END PGP %s-----" % type_ + os.linesep
|
2010-02-08 15:08:40 +01:00
|
|
|
return out
|