2019-12-11 04:32:07 -05:00
|
|
|
import copy
|
|
|
|
import io
|
2019-12-19 14:04:34 -05:00
|
|
|
import logging
|
2019-12-11 04:32:07 -05:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import tempfile
|
|
|
|
##
|
|
|
|
import gpg
|
|
|
|
import gpg.errors
|
|
|
|
|
|
|
|
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2019-12-17 03:40:08 -05:00
|
|
|
class KeyEditor(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.trusted = False
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.info('Key editor instantiated.')
|
2019-12-17 03:40:08 -05:00
|
|
|
|
|
|
|
def truster(self, kw, arg, *args, **kwargs):
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.debug('Key trust editor invoked:')
|
|
|
|
_logger.debug('Command: {0}'.format(kw))
|
|
|
|
_logger.debug('Argument: {0}'.format(arg))
|
|
|
|
if args:
|
|
|
|
_logger.debug('args: {0}'.format(','.join(args)))
|
|
|
|
if kwargs:
|
|
|
|
_logger.debug('kwargs: {0}'.format(kwargs))
|
2019-12-17 03:40:08 -05:00
|
|
|
if kw == 'GET_LINE':
|
|
|
|
if arg == 'keyedit.prompt':
|
|
|
|
if not self.trusted:
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.debug('Returning: "trust"')
|
2019-12-17 03:40:08 -05:00
|
|
|
return('trust')
|
|
|
|
else:
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.debug('Returning: "save"')
|
2019-12-17 03:40:08 -05:00
|
|
|
return('save')
|
|
|
|
elif arg == 'edit_ownertrust.value' and not self.trusted:
|
|
|
|
self.trusted = True
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.debug('Status changed to trusted')
|
|
|
|
_logger.debug('Returning: "4"')
|
2019-12-17 03:40:08 -05:00
|
|
|
return('4') # "Full"
|
|
|
|
else:
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.debug('Returning: "save"')
|
2019-12-17 03:40:08 -05:00
|
|
|
return('save')
|
|
|
|
return(None)
|
|
|
|
|
|
|
|
|
2019-12-11 04:32:07 -05:00
|
|
|
class GPG(object):
|
2019-12-17 03:40:08 -05:00
|
|
|
def __init__(self, homedir = None, primary_key = None, *args, **kwargs):
|
2019-12-11 04:32:07 -05:00
|
|
|
self.homedir = homedir
|
|
|
|
self.primary_key = primary_key
|
|
|
|
self.temporary = None
|
2019-12-17 03:40:08 -05:00
|
|
|
self.ctx = None
|
2019-12-11 04:32:07 -05:00
|
|
|
self._imported_keys = []
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.debug('Homedir: {0}'.format(self.homedir))
|
|
|
|
_logger.debug('Primary key: {0}'.format(self.primary_key))
|
|
|
|
if args:
|
|
|
|
_logger.debug('args: {0}'.format(','.join(args)))
|
|
|
|
if kwargs:
|
|
|
|
_logger.debug('kwargs: {0}'.format(kwargs))
|
|
|
|
_logger.info('Instantiated GPG class.')
|
2019-12-17 03:40:08 -05:00
|
|
|
self._initContext()
|
2019-12-11 04:32:07 -05:00
|
|
|
|
|
|
|
def _initContext(self):
|
|
|
|
if not self.homedir:
|
2019-12-19 14:04:34 -05:00
|
|
|
self.homedir = tempfile.mkdtemp(prefix = '.aif.', suffix = '.gpg')
|
2019-12-17 03:40:08 -05:00
|
|
|
self.temporary = True
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.debug('Set as temporary homedir.')
|
2019-12-11 04:32:07 -05:00
|
|
|
self.homedir = os.path.abspath(os.path.expanduser(self.homedir))
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.debug('Homedir finalized: {0}'.format(self.homedir))
|
2019-12-11 04:32:07 -05:00
|
|
|
if not os.path.isdir(self.homedir):
|
|
|
|
os.makedirs(self.homedir, exist_ok = True)
|
|
|
|
os.chmod(self.homedir, 0o0700)
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.info('Created {0}'.format(self.homedir))
|
2019-12-17 03:40:08 -05:00
|
|
|
self.ctx = gpg.Context(home_dir = self.homedir)
|
2019-12-11 04:32:07 -05:00
|
|
|
if self.temporary:
|
2019-12-19 14:04:34 -05:00
|
|
|
self.primary_key = self.createKey('AIF-NG File Verification Key',
|
|
|
|
sign = True,
|
|
|
|
force = True,
|
|
|
|
certify = True).fpr
|
2019-12-17 03:40:08 -05:00
|
|
|
self.primary_key = self.findKeyByID(self.primary_key, source = 'secret')
|
2019-12-19 14:04:34 -05:00
|
|
|
if self.primary_key:
|
|
|
|
_logger.debug('Found primary key in secret keyring: {0}'.format(self.primary_key.fpr))
|
|
|
|
else:
|
|
|
|
_logger.error('Could not find primary key in secret keyring: {0}'.format(self.primary_key))
|
|
|
|
raise RuntimeError('Primary key not found in secret keyring')
|
2019-12-17 03:40:08 -05:00
|
|
|
self.ctx.signers = [self.primary_key]
|
2019-12-19 14:04:34 -05:00
|
|
|
if self.ctx.signers:
|
|
|
|
_logger.debug('Signers set to: {0}'.format(','.join([k.fpr for k in self.ctx.signers])))
|
|
|
|
else:
|
|
|
|
raise _logger.error('Could not assign signing keys; signing set empty')
|
2019-12-11 04:33:15 -05:00
|
|
|
return(None)
|
2019-12-11 04:32:07 -05:00
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
# This is mostly just to cleanup the stuff we did before.
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.info('Cleaning GPG homedir.')
|
2019-12-11 04:32:07 -05:00
|
|
|
self.primary_key = self.primary_key.fpr
|
|
|
|
if self.temporary:
|
|
|
|
self.primary_key = None
|
|
|
|
shutil.rmtree(self.homedir)
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.info('Deleted temporary GPG homedir: {0}'.format(self.homedir))
|
2019-12-17 03:40:08 -05:00
|
|
|
self.ctx = None
|
2019-12-11 04:33:15 -05:00
|
|
|
return(None)
|
2019-12-11 04:32:07 -05:00
|
|
|
|
|
|
|
def createKey(self, userid, *args, **kwargs):
|
|
|
|
# algorithm=None, expires_in=0, expires=True, sign=False, encrypt=False, certify=False,
|
|
|
|
# authenticate=False, passphrase=None, force=False
|
|
|
|
keyinfo = {'userid': userid,
|
|
|
|
'algorithm': kwargs.get('algorithm', 'rsa4096'),
|
|
|
|
'expires_in': kwargs.get('expires_in'),
|
|
|
|
'sign': kwargs.get('sign', True),
|
|
|
|
'encrypt': kwargs.get('encrypt', False),
|
|
|
|
'certify': kwargs.get('certify', False),
|
|
|
|
'authenticate': kwargs.get('authenticate', False),
|
|
|
|
'passphrase': kwargs.get('passphrase'),
|
|
|
|
'force': kwargs.get('force')}
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.debug('Key creation parameters: {0}'.format(keyinfo))
|
|
|
|
if args:
|
|
|
|
_logger.debug('args: {0}'.format(','.join(args)))
|
|
|
|
if kwargs:
|
|
|
|
_logger.debug('kwargs: {0}'.format(kwargs))
|
2019-12-11 04:32:07 -05:00
|
|
|
if not keyinfo['expires_in']:
|
|
|
|
del(keyinfo['expires_in'])
|
|
|
|
keyinfo['expires'] = False
|
2019-12-17 03:40:08 -05:00
|
|
|
k = self.ctx.create_key(**keyinfo)
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.info('Created key: {0}'.format(k.fpr))
|
|
|
|
_logger.debug('Key info: {0}'.format(k))
|
2019-12-17 03:40:08 -05:00
|
|
|
return(k)
|
2019-12-11 04:32:07 -05:00
|
|
|
|
|
|
|
def findKey(self, searchstr, secret = False, local = True, remote = True,
|
2019-12-17 03:40:08 -05:00
|
|
|
secret_only = False, keyring_import = False, *args, **kwargs):
|
2019-12-11 04:32:07 -05:00
|
|
|
fltr = 0
|
|
|
|
if secret:
|
|
|
|
fltr = fltr | gpg.constants.KEYLIST_MODE_WITH_SECRET
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.debug('Added "secret" to filter; new filter value: {0}'.format(fltr))
|
2019-12-11 04:32:07 -05:00
|
|
|
if local:
|
|
|
|
fltr = fltr | gpg.constants.KEYLIST_MODE_LOCAL
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.debug('Added "local" to filter; new filter value: {0}'.format(fltr))
|
2019-12-11 04:32:07 -05:00
|
|
|
if remote:
|
|
|
|
fltr = fltr | gpg.constants.KEYLIST_MODE_EXTERN
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.debug('Added "remote" to filter; new filter value: {0}'.format(fltr))
|
|
|
|
if args:
|
|
|
|
_logger.debug('args: {0}'.format(','.join(args)))
|
|
|
|
if kwargs:
|
|
|
|
_logger.debug('kwargs: {0}'.format(kwargs))
|
2019-12-17 03:40:08 -05:00
|
|
|
keys = [k for k in self.ctx.keylist(pattern = searchstr, secret = secret_only, mode = fltr)]
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.info('Found {0} keys'.format(len(keys)))
|
|
|
|
if keys:
|
|
|
|
_logger.debug('Found keys: {0}'.format(keys))
|
|
|
|
else:
|
|
|
|
_logger.warn('Found no keys.')
|
2019-12-11 04:32:07 -05:00
|
|
|
if keyring_import:
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.debug('Importing enabled; importing found keys.')
|
2019-12-11 04:32:07 -05:00
|
|
|
self.importKeys(keys, native = True)
|
|
|
|
return(keys)
|
|
|
|
|
2019-12-17 03:40:08 -05:00
|
|
|
def findKeyByID(self, key_id, source = 'remote', keyring_import = False, *args, **kwargs):
|
|
|
|
# So .get_key() CAN get a remote key from a keyserver... but you can't have ANY other keylist modes defined.
|
|
|
|
# Ugh.
|
|
|
|
sources = {'remote': gpg.constants.KEYLIST_MODE_EXTERN,
|
|
|
|
'local': gpg.constants.KEYLIST_MODE_LOCAL,
|
|
|
|
'secret': gpg.constants.KEYLIST_MODE_WITH_SECRET}
|
|
|
|
if source not in sources.keys():
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.error('Invalid source parameter ({0}); must be one of: {1}'.format(source, sources.keys()))
|
|
|
|
raise ValueError('Invalid source parameter')
|
|
|
|
if args:
|
|
|
|
_logger.debug('args: {0}'.format(','.join(args)))
|
|
|
|
if kwargs:
|
|
|
|
_logger.debug('kwargs: {0}'.format(kwargs))
|
2019-12-17 03:40:08 -05:00
|
|
|
orig_mode = self.ctx.get_keylist_mode()
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.debug('Original keylist mode: {0}'.format(orig_mode))
|
2019-12-17 03:40:08 -05:00
|
|
|
self.ctx.set_keylist_mode(sources[source])
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.info('Set keylist mode: {0} ({1})'.format(source, sources[source]))
|
|
|
|
_logger.debug('Searching for key ID: {0}'.format(key_id))
|
2019-12-17 03:40:08 -05:00
|
|
|
try:
|
|
|
|
key = self.ctx.get_key(key_id, secret = (True if source == 'secret' else False))
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.info('Found key object for {0}'.format(key_id))
|
|
|
|
_logger.debug('Found key: {0}'.format(key))
|
2019-12-17 03:40:08 -05:00
|
|
|
except gpg.errors.KeyNotFound:
|
|
|
|
key = None
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.warning('Found no keys.')
|
2019-12-17 03:40:08 -05:00
|
|
|
self.ctx.set_keylist_mode(orig_mode)
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.info('Restored keylist mode ({0})'.format(orig_mode))
|
2019-12-17 03:40:08 -05:00
|
|
|
if keyring_import and key:
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.debug('Importing enabled; importing found keys.')
|
2019-12-17 03:40:08 -05:00
|
|
|
self.importKeys(key, native = True)
|
|
|
|
return(key)
|
|
|
|
|
|
|
|
def getKey(self, key_id, secret = False, strict = False, *args, **kwargs):
|
2019-12-19 14:04:34 -05:00
|
|
|
key = None
|
|
|
|
if args:
|
|
|
|
_logger.debug('args: {0}'.format(','.join(args)))
|
|
|
|
if kwargs:
|
|
|
|
_logger.debug('kwargs: {0}'.format(kwargs))
|
2019-12-11 04:32:07 -05:00
|
|
|
try:
|
|
|
|
getattr(key_id, 'fpr')
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.info('Key specified is already a native key object.')
|
|
|
|
_logger.debug('Key: {0}'.format(key_id))
|
2019-12-11 04:32:07 -05:00
|
|
|
return(key_id)
|
|
|
|
except AttributeError:
|
|
|
|
if not strict:
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.debug('Strict mode disabled; attempting import of {0} first.'.format(key_id))
|
2019-12-17 03:40:08 -05:00
|
|
|
self.findKeyByID(key_id, keyring_import = True, **kwargs)
|
2019-12-11 04:32:07 -05:00
|
|
|
try:
|
2019-12-17 03:40:08 -05:00
|
|
|
key = self.ctx.get_key(key_id, secret = secret)
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.info('Found {0}.'.format(key_id))
|
|
|
|
_logger.debug('Key: {0}'.format(key))
|
2019-12-11 04:32:07 -05:00
|
|
|
except gpg.errors.KeyNotFound:
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.warning('Could not locate {0} in keyring'.format(key_id))
|
|
|
|
return(key)
|
2019-12-11 04:32:07 -05:00
|
|
|
|
2019-12-17 03:40:08 -05:00
|
|
|
def getKeyData(self, keydata, keyring_import = False, *args, **kwargs):
|
2019-12-11 04:32:07 -05:00
|
|
|
orig_keydata = keydata
|
2019-12-19 14:04:34 -05:00
|
|
|
if args:
|
|
|
|
_logger.debug('args: {0}'.format(','.join(args)))
|
|
|
|
if kwargs:
|
|
|
|
_logger.debug('kwargs: {0}'.format(kwargs))
|
2019-12-11 04:32:07 -05:00
|
|
|
if isinstance(keydata, str):
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.debug('String passed as keydata; converting to bytes.')
|
2019-12-11 04:32:07 -05:00
|
|
|
keydata = keydata.encode('utf-8')
|
|
|
|
buf = io.BytesIO(keydata)
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.info('Parsed {0} bytes; looking for key(s).'.format(buf.getbuffer().nbytes))
|
2019-12-17 03:40:08 -05:00
|
|
|
keys = [k for k in self.ctx.keylist(source = buf)]
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.info('Found {0} key(s) in data.'.format(len(keys)))
|
|
|
|
if keys:
|
|
|
|
_logger.debug('Keys found: {0}'.format(keys))
|
|
|
|
else:
|
|
|
|
_logger.warning('No keys found in data.')
|
2019-12-11 04:32:07 -05:00
|
|
|
buf.close()
|
|
|
|
if keyring_import:
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.debug('Importing enabled; importing found keys.')
|
2019-12-11 04:32:07 -05:00
|
|
|
self.importKeys(keys, native = True)
|
|
|
|
return((keys, orig_keydata))
|
|
|
|
|
2019-12-19 14:04:34 -05:00
|
|
|
def getKeyFile(self, keyfile, keyring_import = False, *args, **kwargs):
|
|
|
|
if args:
|
|
|
|
_logger.debug('args: {0}'.format(','.join(args)))
|
|
|
|
if kwargs:
|
|
|
|
_logger.debug('kwargs: {0}'.format(kwargs))
|
|
|
|
orig_keyfile = keyfile
|
|
|
|
keyfile = os.path.abspath(os.path.expanduser(keyfile))
|
|
|
|
_logger.info('Parsed absolute keyfile path: {0} => {1}'.format(orig_keyfile, keyfile))
|
|
|
|
with open(keyfile, 'rb') as fh:
|
|
|
|
rawkey_data = fh.read()
|
|
|
|
fh.seek(0, 0)
|
|
|
|
_logger.debug('Parsed {0} bytes; looking for key(s).'.format(len(rawkey_data)))
|
|
|
|
keys = [k for k in self.ctx.keylist(source = fh)]
|
|
|
|
_logger.info('Found {0} key(s) in data.'.format(len(keys)))
|
|
|
|
if keys:
|
|
|
|
_logger.debug('Keys found: {0}'.format(keys))
|
|
|
|
else:
|
|
|
|
_logger.warning('No keys found in data.')
|
|
|
|
if keyring_import:
|
|
|
|
_logger.debug('Importing enabled; importing found keys.')
|
|
|
|
self.importKeys(keys, native = True)
|
|
|
|
return((keys, rawkey_data))
|
|
|
|
|
2019-12-17 03:40:08 -05:00
|
|
|
def importKeys(self, keydata, native = False, local = True, remote = True, *args, **kwargs):
|
|
|
|
fltr = 0
|
|
|
|
orig_km = None
|
|
|
|
keys = []
|
2019-12-19 14:04:34 -05:00
|
|
|
if args:
|
|
|
|
_logger.debug('args: {0}'.format(','.join(args)))
|
|
|
|
if kwargs:
|
|
|
|
_logger.debug('kwargs: {0}'.format(kwargs))
|
2019-12-17 03:40:08 -05:00
|
|
|
if local:
|
|
|
|
fltr = fltr | gpg.constants.KEYLIST_MODE_LOCAL
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.debug('Added "local" to filter; new filter value: {0}'.format(fltr))
|
2019-12-17 03:40:08 -05:00
|
|
|
if remote:
|
|
|
|
fltr = fltr | gpg.constants.KEYLIST_MODE_EXTERN
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.debug('Added "remote" to filter; new filter value: {0}'.format(fltr))
|
2019-12-17 03:40:08 -05:00
|
|
|
if self.ctx.get_keylist_mode() != fltr:
|
|
|
|
orig_km = self.ctx.get_keylist_mode()
|
|
|
|
self.ctx.set_keylist_mode(fltr)
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.info(('Current keylist mode ({0}) doesn\'t match filter ({1}); '
|
|
|
|
'set to new mode.').format(orig_km, fltr))
|
2019-12-17 03:40:08 -05:00
|
|
|
if not native: # It's raw key data (.gpg, .asc, etc.).
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.info('Non-native keydata specified; parsing.')
|
2019-12-17 03:40:08 -05:00
|
|
|
formatted_keys = b''
|
|
|
|
if isinstance(keydata, str):
|
|
|
|
formatted_keys += keydata.encode('utf-8')
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.debug('Specified keydata was a string; converted to bytes.')
|
2019-12-17 03:40:08 -05:00
|
|
|
elif isinstance(keydata, list):
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.debug('Specified keydata was a list/list-like; iterating.')
|
|
|
|
for idx, k in enumerate(keydata):
|
|
|
|
_logger.debug('Parsing entry {0} of {1} entries.'.format((idx + 1), len(keydata)))
|
2019-12-17 03:40:08 -05:00
|
|
|
if isinstance(k, str):
|
|
|
|
formatted_keys += k.encode('utf-8')
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.debug('Keydata ({0}) was a string; converted to bytes.'.format((idx + 1)))
|
2019-12-17 03:40:08 -05:00
|
|
|
else:
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.debug('Keydata ({0}) was already in bytes.'.format((idx + 1)))
|
2019-12-17 03:40:08 -05:00
|
|
|
formatted_keys += k
|
|
|
|
else:
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.warning('Could not identify keydata reliably; unpredictable results ahead.')
|
|
|
|
formatted_keys = keydata
|
|
|
|
rslt = self.ctx.key_import(formatted_keys).imports
|
|
|
|
_logger.debug('Imported keys: {0}'.format(rslt))
|
|
|
|
for r in rslt:
|
|
|
|
k = self.ctx.get_key(r.fpr)
|
|
|
|
if k:
|
|
|
|
_logger.debug('Adding key to keylist: {0}'.format(k))
|
|
|
|
else:
|
|
|
|
_logger.warning('Could not find key ID {0}.'.format(r.fpr))
|
|
|
|
keys.append(k)
|
2019-12-17 03:40:08 -05:00
|
|
|
else: # It's a native Key() object (or a list of them).
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.info('Native keydata specified; parsing.')
|
2019-12-11 04:32:07 -05:00
|
|
|
if not isinstance(keydata, list):
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.debug('Specified keydata was not a list/list-like; fixing.')
|
2019-12-11 04:32:07 -05:00
|
|
|
keydata = [keydata]
|
2019-12-17 03:40:08 -05:00
|
|
|
keys = keydata
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.debug('Importing keys: {0}'.format(keys))
|
2019-12-17 03:40:08 -05:00
|
|
|
self.ctx.op_import_keys(keydata)
|
|
|
|
if orig_km:
|
|
|
|
self.ctx.set_keylist_mode(orig_km)
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.info('Restored keylist mode to {0}'.format(orig_km))
|
2019-12-17 03:40:08 -05:00
|
|
|
for k in keys:
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.info('Signing {0} with a local signature.'.format(k.fpr))
|
2019-12-17 03:40:08 -05:00
|
|
|
self.ctx.key_sign(k, local = True)
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.debug('Adding trust for {0}.'.format(k.fpr))
|
2019-12-17 03:40:08 -05:00
|
|
|
trusteditor = KeyEditor()
|
|
|
|
self.ctx.interact(k, trusteditor.truster)
|
2019-12-11 04:33:15 -05:00
|
|
|
return(None)
|
2019-12-11 04:32:07 -05:00
|
|
|
|
|
|
|
def verifyData(self, data, keys = None, strict = False, detached = None, *args, **kwargs):
|
|
|
|
results = {}
|
2019-12-19 14:04:34 -05:00
|
|
|
if args:
|
|
|
|
_logger.debug('args: {0}'.format(','.join(args)))
|
|
|
|
if kwargs:
|
|
|
|
_logger.debug('kwargs: {0}'.format(kwargs))
|
2019-12-11 04:32:07 -05:00
|
|
|
if keys:
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.info('Keys were specified.')
|
2019-12-11 04:32:07 -05:00
|
|
|
if not isinstance(keys, list):
|
2019-12-17 03:40:08 -05:00
|
|
|
keys = [self.getKey(keys, source = 'local')]
|
2019-12-11 04:32:07 -05:00
|
|
|
else:
|
2019-12-17 03:40:08 -05:00
|
|
|
keys = [self.getKey(k, source = 'local') for k in keys]
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.debug('Verifying against keys: {0}'.format(keys))
|
2019-12-11 04:32:07 -05:00
|
|
|
if isinstance(data, str):
|
|
|
|
data = data.encode('utf-8')
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.debug('Specified data was a string; converted to bytes.')
|
|
|
|
_logger.info('Verifying {0} bytes of data.'.format(len(data)))
|
2019-12-17 03:40:08 -05:00
|
|
|
fnargs = {'signed_data': data}
|
2019-12-11 04:32:07 -05:00
|
|
|
if detached:
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.info('Specified a detached signature.')
|
2019-12-11 04:32:07 -05:00
|
|
|
if isinstance(detached, str):
|
|
|
|
detached = detached.encode('utf-8')
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.debug('Specified signature was a string; converted to bytes.')
|
2019-12-17 03:40:08 -05:00
|
|
|
if not isinstance(detached, bytes) and not hasattr(detached, 'read'):
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.error('Detached signature was neither bytes nor a buffer-like object.')
|
|
|
|
raise TypeError('detached must be bytes or buffer-like object')
|
|
|
|
if isinstance(detached, bytes):
|
|
|
|
_logger.info('Signature length: {0} bytes'.format(len(detached)))
|
|
|
|
else:
|
|
|
|
_logger.info('Signature length: {0} bytes'.format(detached.getbuffer().nbytes))
|
2019-12-17 03:40:08 -05:00
|
|
|
fnargs['signature'] = detached
|
2019-12-11 04:32:07 -05:00
|
|
|
if strict:
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.debug('Strict mode enabled; data must be signed by ALL specified keys.')
|
2019-12-17 03:40:08 -05:00
|
|
|
fnargs['verify'] = keys
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.debug('Verifying with args: {0}'.format(fnargs))
|
2019-12-17 03:40:08 -05:00
|
|
|
results[None] = self.ctx.verify(**fnargs)
|
2019-12-11 04:32:07 -05:00
|
|
|
else:
|
|
|
|
if keys:
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.debug('Keys were specified but running in non-strict; iterating over all.')
|
2019-12-11 04:32:07 -05:00
|
|
|
for k in keys:
|
2019-12-17 03:40:08 -05:00
|
|
|
_fnargs = copy.deepcopy(fnargs)
|
|
|
|
_fnargs['verify'] = [k]
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.info('Verifying against key {0}'.format(k.fpr))
|
2019-12-17 03:40:08 -05:00
|
|
|
try:
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.debug(('Verifying with args (data-stripped): '
|
|
|
|
'{0}').format({k: (v if k not in ('signed_data',
|
|
|
|
'signature')
|
|
|
|
else '(stripped)') for k, v in _fnargs.items()}))
|
2019-12-17 03:40:08 -05:00
|
|
|
sigchk = self.ctx.verify(**_fnargs)
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.info('Key {0} verification results: {1}'.format(k.fpr, sigchk))
|
|
|
|
results[k.fpr] = (True, sigchk[1], None)
|
2019-12-17 03:40:08 -05:00
|
|
|
except gpg.errors.MissingSignatures as e:
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.warning('Key {0}: missing signature'.format(k.fpr))
|
|
|
|
_logger.debug('Key {0} results: {1}'.format(k.fpr, e.results))
|
2019-12-17 03:40:08 -05:00
|
|
|
results[k.fpr] = (False, e.results, 'Missing Signature')
|
|
|
|
except gpg.errors.BadSignatures as e:
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.warning('Key {0}: bad signature'.format(k.fpr))
|
|
|
|
_logger.debug('Key {0} results: {1}'.format(k.fpr, e.results))
|
2019-12-17 03:40:08 -05:00
|
|
|
results[k.fpr] = (False, e.results, 'Bad Signature')
|
2019-12-11 04:32:07 -05:00
|
|
|
else:
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.debug('No keys specified but running in non-strict; accepting any signatures.')
|
|
|
|
_logger.debug(('Verifying with args (data-stripped): '
|
|
|
|
'{0}').format({k: (v if k not in ('signed_data',
|
|
|
|
'signature')
|
|
|
|
else '(stripped)') for k, v in fnargs.items()}))
|
2019-12-17 03:40:08 -05:00
|
|
|
results[None] = self.ctx.verify(**fnargs)
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.debug('Results for any/all signatures: {0}'.format(results[None]))
|
2019-12-11 04:32:07 -05:00
|
|
|
return(results)
|
|
|
|
|
|
|
|
def verifyFile(self, filepath, *args, **kwargs):
|
2019-12-19 14:04:34 -05:00
|
|
|
orig_filepath = filepath
|
2019-12-11 04:32:07 -05:00
|
|
|
filepath = os.path.abspath(os.path.expanduser(filepath))
|
2019-12-19 14:04:34 -05:00
|
|
|
_logger.debug('File verification invoked. Transformed filepath: {0} => {1}'.format(orig_filepath, filepath))
|
|
|
|
if args:
|
|
|
|
_logger.debug('args: {0}'.format(','.join(args)))
|
|
|
|
if kwargs:
|
|
|
|
_logger.debug('kwargs: {0}'.format(kwargs))
|
2019-12-11 04:32:07 -05:00
|
|
|
with open(filepath, 'rb') as fh:
|
|
|
|
results = self.verifyData(fh.read(), **kwargs)
|
|
|
|
return(results)
|