2016-06-06 12:08:59 +00:00
|
|
|
# Copyright (C) 2016 g10 Code GmbH
|
2015-05-05 17:09:44 +00:00
|
|
|
# Copyright (C) 2004,2008 Igor Belyi <belyi@users.sourceforge.net>
|
|
|
|
# Copyright (C) 2002 John Goerzen <jgoerzen@complete.org>
|
|
|
|
#
|
|
|
|
# This library is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2.1 of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this library; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
2016-06-06 12:08:59 +00:00
|
|
|
"""Core functionality
|
|
|
|
|
|
|
|
Core functionality of GPGME wrapped in a object-oriented fashion.
|
|
|
|
Provides the 'Context' class for performing cryptographic operations,
|
|
|
|
and the 'Data' class describing buffers of data.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2016-09-14 09:39:00 +00:00
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
|
|
|
del absolute_import, print_function, unicode_literals
|
|
|
|
|
2016-06-14 11:28:37 +00:00
|
|
|
import re
|
2016-06-08 16:58:57 +00:00
|
|
|
import os
|
2016-09-16 12:56:29 +00:00
|
|
|
import warnings
|
2016-05-27 10:25:59 +00:00
|
|
|
import weakref
|
2016-07-28 13:44:38 +00:00
|
|
|
from . import gpgme
|
2016-05-17 12:14:25 +00:00
|
|
|
from .errors import errorcheck, GPGMEError
|
2016-06-10 11:00:33 +00:00
|
|
|
from . import constants
|
2015-05-05 17:09:44 +00:00
|
|
|
from . import errors
|
2016-07-28 10:40:54 +00:00
|
|
|
from . import util
|
2016-05-24 14:45:39 +00:00
|
|
|
|
|
|
|
class GpgmeWrapper(object):
|
2016-06-06 12:08:59 +00:00
|
|
|
"""Base wrapper class
|
|
|
|
|
|
|
|
Not to be instantiated directly.
|
|
|
|
|
|
|
|
"""
|
2016-05-24 14:45:39 +00:00
|
|
|
|
|
|
|
def __init__(self, wrapped):
|
|
|
|
self._callback_excinfo = None
|
|
|
|
self.wrapped = wrapped
|
|
|
|
|
|
|
|
def __repr__(self):
|
2016-09-12 15:21:34 +00:00
|
|
|
return '<{}/{!r}>'.format(super(GpgmeWrapper, self).__repr__(),
|
|
|
|
self.wrapped)
|
2016-05-24 14:45:39 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2016-06-10 11:00:33 +00:00
|
|
|
acc = ['{}.{}'.format(__name__, self.__class__.__name__)]
|
|
|
|
flags = [f for f in self._boolean_properties if getattr(self, f)]
|
|
|
|
if flags:
|
|
|
|
acc.append('({})'.format(' '.join(flags)))
|
|
|
|
|
|
|
|
return '<{}>'.format(' '.join(acc))
|
2016-05-24 14:45:39 +00:00
|
|
|
|
|
|
|
def __hash__(self):
|
|
|
|
return hash(repr(self.wrapped))
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
if other == None:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return repr(self.wrapped) == repr(other.wrapped)
|
|
|
|
|
2016-06-14 10:37:26 +00:00
|
|
|
@property
|
|
|
|
def _ctype(self):
|
|
|
|
"""The name of the c type wrapped by this class
|
|
|
|
|
|
|
|
Must be set by child classes.
|
2016-05-24 14:45:39 +00:00
|
|
|
|
2016-06-14 10:37:26 +00:00
|
|
|
"""
|
2016-05-24 14:45:39 +00:00
|
|
|
raise NotImplementedError()
|
|
|
|
|
2016-06-14 10:37:26 +00:00
|
|
|
@property
|
|
|
|
def _cprefix(self):
|
|
|
|
"""The common prefix of c functions wrapped by this class
|
|
|
|
|
|
|
|
Must be set by child classes.
|
2016-05-24 14:45:39 +00:00
|
|
|
|
2016-06-14 10:37:26 +00:00
|
|
|
"""
|
2016-05-24 14:45:39 +00:00
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def _errorcheck(self, name):
|
|
|
|
"""Must be implemented by child classes.
|
|
|
|
|
|
|
|
This function must return a trueish value for all c functions
|
|
|
|
returning gpgme_error_t."""
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2016-06-10 11:00:33 +00:00
|
|
|
"""The set of all boolean properties"""
|
|
|
|
_boolean_properties = set()
|
|
|
|
|
|
|
|
def __wrap_boolean_property(self, key, do_set=False, value=None):
|
2016-07-28 13:44:38 +00:00
|
|
|
get_func = getattr(gpgme,
|
2016-06-14 10:37:26 +00:00
|
|
|
"{}get_{}".format(self._cprefix, key))
|
2016-07-28 13:44:38 +00:00
|
|
|
set_func = getattr(gpgme,
|
2016-06-14 10:37:26 +00:00
|
|
|
"{}set_{}".format(self._cprefix, key))
|
2016-06-10 11:00:33 +00:00
|
|
|
def get(slf):
|
|
|
|
return bool(get_func(slf.wrapped))
|
|
|
|
def set_(slf, value):
|
|
|
|
set_func(slf.wrapped, bool(value))
|
|
|
|
|
|
|
|
p = property(get, set_, doc="{} flag".format(key))
|
|
|
|
setattr(self.__class__, key, p)
|
|
|
|
|
|
|
|
if do_set:
|
|
|
|
set_(self, bool(value))
|
|
|
|
else:
|
|
|
|
return get(self)
|
|
|
|
|
2016-06-14 11:28:37 +00:00
|
|
|
_munge_docstring = re.compile(r'gpgme_([^(]*)\(([^,]*), (.*\) -> .*)')
|
2016-05-24 14:45:39 +00:00
|
|
|
def __getattr__(self, key):
|
2016-06-10 11:00:33 +00:00
|
|
|
"""On-the-fly generation of wrapper methods and properties"""
|
2016-06-14 10:37:26 +00:00
|
|
|
if key[0] == '_' or self._cprefix == None:
|
2016-05-24 14:45:39 +00:00
|
|
|
return None
|
2016-06-10 11:00:33 +00:00
|
|
|
|
|
|
|
if key in self._boolean_properties:
|
|
|
|
return self.__wrap_boolean_property(key)
|
|
|
|
|
2016-06-14 10:37:26 +00:00
|
|
|
name = self._cprefix + key
|
2016-07-28 13:44:38 +00:00
|
|
|
func = getattr(gpgme, name)
|
2016-05-24 14:45:39 +00:00
|
|
|
|
|
|
|
if self._errorcheck(name):
|
2016-06-14 11:28:37 +00:00
|
|
|
def _funcwrap(slf, *args):
|
|
|
|
result = func(slf.wrapped, *args)
|
2016-05-24 14:45:39 +00:00
|
|
|
if slf._callback_excinfo:
|
2016-10-28 20:45:49 +00:00
|
|
|
gpgme.gpg_raise_callback_exception(slf)
|
2016-05-24 14:45:39 +00:00
|
|
|
return errorcheck(result, "Invocation of " + name)
|
|
|
|
else:
|
2016-06-14 11:28:37 +00:00
|
|
|
def _funcwrap(slf, *args):
|
|
|
|
result = func(slf.wrapped, *args)
|
2016-05-24 14:45:39 +00:00
|
|
|
if slf._callback_excinfo:
|
2016-10-28 20:45:49 +00:00
|
|
|
gpgme.gpg_raise_callback_exception(slf)
|
2016-05-24 14:45:39 +00:00
|
|
|
return result
|
|
|
|
|
2016-06-14 11:28:37 +00:00
|
|
|
doc = self._munge_docstring.sub(r'\2.\1(\3', getattr(func, "__doc__"))
|
|
|
|
_funcwrap.__doc__ = doc
|
2016-05-24 14:45:39 +00:00
|
|
|
|
|
|
|
# Monkey-patch the class.
|
|
|
|
setattr(self.__class__, key, _funcwrap)
|
|
|
|
|
|
|
|
# Bind the method to 'self'.
|
2016-06-14 11:28:37 +00:00
|
|
|
def wrapper(*args):
|
|
|
|
return _funcwrap(self, *args)
|
|
|
|
wrapper.__doc__ = doc
|
2016-05-24 14:45:39 +00:00
|
|
|
|
|
|
|
return wrapper
|
2015-05-05 17:09:44 +00:00
|
|
|
|
2016-06-10 11:00:33 +00:00
|
|
|
def __setattr__(self, key, value):
|
|
|
|
"""On-the-fly generation of properties"""
|
|
|
|
if key in self._boolean_properties:
|
|
|
|
self.__wrap_boolean_property(key, True, value)
|
|
|
|
else:
|
2016-09-12 15:21:34 +00:00
|
|
|
super(GpgmeWrapper, self).__setattr__(key, value)
|
2016-06-10 11:00:33 +00:00
|
|
|
|
2015-05-05 17:09:44 +00:00
|
|
|
class Context(GpgmeWrapper):
|
2016-06-06 12:08:59 +00:00
|
|
|
"""Context for cryptographic operations
|
|
|
|
|
|
|
|
All cryptographic operations in GPGME are performed within a
|
|
|
|
context, which contains the internal state of the operation as
|
|
|
|
well as configuration parameters. By using several contexts you
|
|
|
|
can run several cryptographic operations in parallel, with
|
|
|
|
different configuration.
|
2015-05-05 17:09:44 +00:00
|
|
|
|
2016-06-06 12:08:59 +00:00
|
|
|
Access to a context must be synchronized.
|
2015-05-05 17:09:44 +00:00
|
|
|
|
2016-06-06 12:08:59 +00:00
|
|
|
"""
|
2015-05-05 17:09:44 +00:00
|
|
|
|
2016-06-08 16:58:57 +00:00
|
|
|
def __init__(self, armor=False, textmode=False, offline=False,
|
|
|
|
signers=[], pinentry_mode=constants.PINENTRY_MODE_DEFAULT,
|
2016-07-28 08:59:46 +00:00
|
|
|
protocol=constants.PROTOCOL_OpenPGP,
|
2017-02-14 12:43:01 +00:00
|
|
|
wrapped=None, home_dir=None):
|
2016-06-08 16:58:57 +00:00
|
|
|
"""Construct a context object
|
|
|
|
|
|
|
|
Keyword arguments:
|
|
|
|
armor -- enable ASCII armoring (default False)
|
|
|
|
textmode -- enable canonical text mode (default False)
|
|
|
|
offline -- do not contact external key sources (default False)
|
|
|
|
signers -- list of keys used for signing (default [])
|
|
|
|
pinentry_mode -- pinentry mode (default PINENTRY_MODE_DEFAULT)
|
2016-07-28 08:59:46 +00:00
|
|
|
protocol -- protocol to use (default PROTOCOL_OpenPGP)
|
2017-02-14 12:43:01 +00:00
|
|
|
home_dir -- state directory (default is the engine default)
|
2016-06-08 16:58:57 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
if wrapped:
|
|
|
|
self.own = False
|
|
|
|
else:
|
2016-07-28 13:44:38 +00:00
|
|
|
tmp = gpgme.new_gpgme_ctx_t_p()
|
|
|
|
errorcheck(gpgme.gpgme_new(tmp))
|
|
|
|
wrapped = gpgme.gpgme_ctx_t_p_value(tmp)
|
|
|
|
gpgme.delete_gpgme_ctx_t_p(tmp)
|
2016-06-08 16:58:57 +00:00
|
|
|
self.own = True
|
2016-09-12 15:21:34 +00:00
|
|
|
super(Context, self).__init__(wrapped)
|
2016-06-08 16:58:57 +00:00
|
|
|
self.armor = armor
|
|
|
|
self.textmode = textmode
|
|
|
|
self.offline = offline
|
|
|
|
self.signers = signers
|
|
|
|
self.pinentry_mode = pinentry_mode
|
2016-07-28 08:59:46 +00:00
|
|
|
self.protocol = protocol
|
2017-02-14 12:43:01 +00:00
|
|
|
self.home_dir = home_dir
|
2016-06-08 16:58:57 +00:00
|
|
|
|
2016-11-29 22:26:20 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return (
|
|
|
|
"Context(armor={0.armor}, "
|
|
|
|
"textmode={0.textmode}, offline={0.offline}, "
|
|
|
|
"signers={0.signers}, pinentry_mode={0.pinentry_mode}, "
|
2017-02-14 12:43:01 +00:00
|
|
|
"protocol={0.protocol}, home_dir={0.home_dir}"
|
2016-11-29 22:26:20 +00:00
|
|
|
")").format(self)
|
|
|
|
|
2016-06-08 16:58:57 +00:00
|
|
|
def encrypt(self, plaintext, recipients=[], sign=True, sink=None,
|
|
|
|
passphrase=None, always_trust=False, add_encrypt_to=False,
|
|
|
|
prepare=False, expect_sign=False, compress=True):
|
|
|
|
"""Encrypt data
|
|
|
|
|
|
|
|
Encrypt the given plaintext for the given recipients. If the
|
|
|
|
list of recipients is empty, the data is encrypted
|
|
|
|
symmetrically with a passphrase.
|
|
|
|
|
|
|
|
The passphrase can be given as parameter, using a callback
|
|
|
|
registered at the context, or out-of-band via pinentry.
|
|
|
|
|
|
|
|
Keyword arguments:
|
|
|
|
recipients -- list of keys to encrypt to
|
|
|
|
sign -- sign plaintext (default True)
|
|
|
|
sink -- write result to sink instead of returning it
|
|
|
|
passphrase -- for symmetric encryption
|
|
|
|
always_trust -- always trust the keys (default False)
|
|
|
|
add_encrypt_to -- encrypt to configured additional keys (default False)
|
|
|
|
prepare -- (ui) prepare for encryption (default False)
|
|
|
|
expect_sign -- (ui) prepare for signing (default False)
|
|
|
|
compress -- compress plaintext (default True)
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
ciphertext -- the encrypted data (or None if sink is given)
|
|
|
|
result -- additional information about the encryption
|
|
|
|
sign_result -- additional information about the signature(s)
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
InvalidRecipients -- if encryption using a particular key failed
|
|
|
|
InvalidSigners -- if signing using a particular key failed
|
|
|
|
GPGMEError -- as signaled by the underlying library
|
|
|
|
|
|
|
|
"""
|
|
|
|
ciphertext = sink if sink else Data()
|
|
|
|
flags = 0
|
|
|
|
flags |= always_trust * constants.ENCRYPT_ALWAYS_TRUST
|
|
|
|
flags |= (not add_encrypt_to) * constants.ENCRYPT_NO_ENCRYPT_TO
|
|
|
|
flags |= prepare * constants.ENCRYPT_PREPARE
|
|
|
|
flags |= expect_sign * constants.ENCRYPT_EXPECT_SIGN
|
|
|
|
flags |= (not compress) * constants.ENCRYPT_NO_COMPRESS
|
|
|
|
|
|
|
|
if passphrase != None:
|
|
|
|
old_pinentry_mode = self.pinentry_mode
|
|
|
|
old_passphrase_cb = getattr(self, '_passphrase_cb', None)
|
|
|
|
self.pinentry_mode = constants.PINENTRY_MODE_LOOPBACK
|
|
|
|
def passphrase_cb(hint, desc, prev_bad, hook=None):
|
|
|
|
return passphrase
|
|
|
|
self.set_passphrase_cb(passphrase_cb)
|
|
|
|
|
|
|
|
try:
|
|
|
|
if sign:
|
|
|
|
self.op_encrypt_sign(recipients, flags, plaintext, ciphertext)
|
|
|
|
else:
|
|
|
|
self.op_encrypt(recipients, flags, plaintext, ciphertext)
|
|
|
|
except errors.GPGMEError as e:
|
|
|
|
if e.getcode() == errors.UNUSABLE_PUBKEY:
|
|
|
|
result = self.op_encrypt_result()
|
|
|
|
if result.invalid_recipients:
|
|
|
|
raise errors.InvalidRecipients(result.invalid_recipients)
|
|
|
|
if e.getcode() == errors.UNUSABLE_SECKEY:
|
|
|
|
sig_result = self.op_sign_result()
|
|
|
|
if sig_result.invalid_signers:
|
|
|
|
raise errors.InvalidSigners(sig_result.invalid_signers)
|
|
|
|
raise
|
|
|
|
finally:
|
|
|
|
if passphrase != None:
|
|
|
|
self.pinentry_mode = old_pinentry_mode
|
|
|
|
if old_passphrase_cb:
|
|
|
|
self.set_passphrase_cb(*old_passphrase_cb[1:])
|
|
|
|
|
|
|
|
result = self.op_encrypt_result()
|
|
|
|
assert not result.invalid_recipients
|
|
|
|
sig_result = self.op_sign_result() if sign else None
|
|
|
|
assert not sig_result or not sig_result.invalid_signers
|
|
|
|
|
|
|
|
cipherbytes = None
|
|
|
|
if not sink:
|
|
|
|
ciphertext.seek(0, os.SEEK_SET)
|
|
|
|
cipherbytes = ciphertext.read()
|
|
|
|
return cipherbytes, result, sig_result
|
|
|
|
|
|
|
|
def decrypt(self, ciphertext, sink=None, passphrase=None, verify=True):
|
|
|
|
"""Decrypt data
|
|
|
|
|
|
|
|
Decrypt the given ciphertext and verify any signatures. If
|
|
|
|
VERIFY is an iterable of keys, the ciphertext must be signed
|
|
|
|
by all those keys, otherwise an error is raised.
|
|
|
|
|
|
|
|
If the ciphertext is symmetrically encrypted using a
|
|
|
|
passphrase, that passphrase can be given as parameter, using a
|
|
|
|
callback registered at the context, or out-of-band via
|
|
|
|
pinentry.
|
|
|
|
|
|
|
|
Keyword arguments:
|
|
|
|
sink -- write result to sink instead of returning it
|
|
|
|
passphrase -- for symmetric decryption
|
|
|
|
verify -- check signatures (default True)
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
plaintext -- the decrypted data (or None if sink is given)
|
|
|
|
result -- additional information about the decryption
|
|
|
|
verify_result -- additional information about the signature(s)
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
UnsupportedAlgorithm -- if an unsupported algorithm was used
|
|
|
|
BadSignatures -- if a bad signature is encountered
|
|
|
|
MissingSignatures -- if expected signatures are missing or bad
|
|
|
|
GPGMEError -- as signaled by the underlying library
|
|
|
|
|
|
|
|
"""
|
|
|
|
plaintext = sink if sink else Data()
|
|
|
|
|
|
|
|
if passphrase != None:
|
|
|
|
old_pinentry_mode = self.pinentry_mode
|
|
|
|
old_passphrase_cb = getattr(self, '_passphrase_cb', None)
|
|
|
|
self.pinentry_mode = constants.PINENTRY_MODE_LOOPBACK
|
|
|
|
def passphrase_cb(hint, desc, prev_bad, hook=None):
|
|
|
|
return passphrase
|
|
|
|
self.set_passphrase_cb(passphrase_cb)
|
|
|
|
|
|
|
|
try:
|
|
|
|
if verify:
|
|
|
|
self.op_decrypt_verify(ciphertext, plaintext)
|
|
|
|
else:
|
|
|
|
self.op_decrypt(ciphertext, plaintext)
|
|
|
|
finally:
|
|
|
|
if passphrase != None:
|
|
|
|
self.pinentry_mode = old_pinentry_mode
|
|
|
|
if old_passphrase_cb:
|
|
|
|
self.set_passphrase_cb(*old_passphrase_cb[1:])
|
|
|
|
|
|
|
|
result = self.op_decrypt_result()
|
|
|
|
verify_result = self.op_verify_result() if verify else None
|
|
|
|
if result.unsupported_algorithm:
|
|
|
|
raise errors.UnsupportedAlgorithm(result.unsupported_algorithm)
|
|
|
|
|
|
|
|
if verify:
|
|
|
|
if any(s.status != errors.NO_ERROR
|
|
|
|
for s in verify_result.signatures):
|
|
|
|
raise errors.BadSignatures(verify_result)
|
|
|
|
|
|
|
|
if verify and verify != True:
|
|
|
|
missing = list()
|
|
|
|
for key in verify:
|
|
|
|
ok = False
|
|
|
|
for subkey in key.subkeys:
|
|
|
|
for sig in verify_result.signatures:
|
|
|
|
if sig.summary & constants.SIGSUM_VALID == 0:
|
|
|
|
continue
|
|
|
|
if subkey.can_sign and subkey.fpr == sig.fpr:
|
|
|
|
ok = True
|
|
|
|
break
|
|
|
|
if ok:
|
|
|
|
break
|
|
|
|
if not ok:
|
|
|
|
missing.append(key)
|
|
|
|
if missing:
|
|
|
|
raise errors.MissingSignatures(verify_result, missing)
|
|
|
|
|
|
|
|
plainbytes = None
|
|
|
|
if not sink:
|
|
|
|
plaintext.seek(0, os.SEEK_SET)
|
|
|
|
plainbytes = plaintext.read()
|
|
|
|
return plainbytes, result, verify_result
|
|
|
|
|
|
|
|
def sign(self, data, sink=None, mode=constants.SIG_MODE_NORMAL):
|
|
|
|
"""Sign data
|
|
|
|
|
|
|
|
Sign the given data with either the configured default local
|
|
|
|
key, or the 'signers' keys of this context.
|
|
|
|
|
|
|
|
Keyword arguments:
|
|
|
|
mode -- signature mode (default: normal, see below)
|
|
|
|
sink -- write result to sink instead of returning it
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
either
|
|
|
|
signed_data -- encoded data and signature (normal mode)
|
|
|
|
signature -- only the signature data (detached mode)
|
|
|
|
cleartext -- data and signature as text (cleartext mode)
|
|
|
|
(or None if sink is given)
|
|
|
|
result -- additional information about the signature(s)
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
InvalidSigners -- if signing using a particular key failed
|
|
|
|
GPGMEError -- as signaled by the underlying library
|
|
|
|
|
|
|
|
"""
|
|
|
|
signeddata = sink if sink else Data()
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.op_sign(data, signeddata, mode)
|
|
|
|
except errors.GPGMEError as e:
|
|
|
|
if e.getcode() == errors.UNUSABLE_SECKEY:
|
|
|
|
result = self.op_sign_result()
|
|
|
|
if result.invalid_signers:
|
|
|
|
raise errors.InvalidSigners(result.invalid_signers)
|
|
|
|
raise
|
|
|
|
|
|
|
|
result = self.op_sign_result()
|
|
|
|
assert not result.invalid_signers
|
|
|
|
|
|
|
|
signedbytes = None
|
|
|
|
if not sink:
|
|
|
|
signeddata.seek(0, os.SEEK_SET)
|
|
|
|
signedbytes = signeddata.read()
|
|
|
|
return signedbytes, result
|
|
|
|
|
|
|
|
def verify(self, signed_data, signature=None, sink=None, verify=[]):
|
|
|
|
"""Verify signatures
|
|
|
|
|
|
|
|
Verify signatures over data. If VERIFY is an iterable of
|
|
|
|
keys, the ciphertext must be signed by all those keys,
|
|
|
|
otherwise an error is raised.
|
|
|
|
|
|
|
|
Keyword arguments:
|
|
|
|
signature -- detached signature data
|
|
|
|
sink -- write result to sink instead of returning it
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
data -- the plain data
|
|
|
|
(or None if sink is given, or we verified a detached signature)
|
|
|
|
result -- additional information about the signature(s)
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
BadSignatures -- if a bad signature is encountered
|
|
|
|
MissingSignatures -- if expected signatures are missing or bad
|
|
|
|
GPGMEError -- as signaled by the underlying library
|
|
|
|
|
|
|
|
"""
|
|
|
|
if signature:
|
|
|
|
# Detached signature, we don't return the plain text.
|
|
|
|
data = None
|
|
|
|
else:
|
|
|
|
data = sink if sink else Data()
|
|
|
|
|
|
|
|
if signature:
|
|
|
|
self.op_verify(signature, signed_data, None)
|
|
|
|
else:
|
|
|
|
self.op_verify(signed_data, None, data)
|
|
|
|
|
|
|
|
result = self.op_verify_result()
|
|
|
|
if any(s.status != errors.NO_ERROR for s in result.signatures):
|
|
|
|
raise errors.BadSignatures(result)
|
|
|
|
|
|
|
|
missing = list()
|
|
|
|
for key in verify:
|
|
|
|
ok = False
|
|
|
|
for subkey in key.subkeys:
|
|
|
|
for sig in result.signatures:
|
|
|
|
if sig.summary & constants.SIGSUM_VALID == 0:
|
|
|
|
continue
|
|
|
|
if subkey.can_sign and subkey.fpr == sig.fpr:
|
|
|
|
ok = True
|
|
|
|
break
|
|
|
|
if ok:
|
|
|
|
break
|
|
|
|
if not ok:
|
|
|
|
missing.append(key)
|
|
|
|
if missing:
|
|
|
|
raise errors.MissingSignatures(result, missing)
|
|
|
|
|
|
|
|
plainbytes = None
|
|
|
|
if data and not sink:
|
|
|
|
data.seek(0, os.SEEK_SET)
|
|
|
|
plainbytes = data.read()
|
|
|
|
return plainbytes, result
|
|
|
|
|
2016-08-03 14:32:30 +00:00
|
|
|
def keylist(self, pattern=None, secret=False):
|
|
|
|
"""List keys
|
|
|
|
|
|
|
|
Keyword arguments:
|
|
|
|
pattern -- return keys matching pattern (default: all keys)
|
|
|
|
secret -- return only secret keys
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
-- an iterator returning key objects
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
GPGMEError -- as signaled by the underlying library
|
|
|
|
"""
|
|
|
|
return self.op_keylist_all(pattern, secret)
|
|
|
|
|
2016-07-28 10:40:54 +00:00
|
|
|
def assuan_transact(self, command,
|
|
|
|
data_cb=None, inquire_cb=None, status_cb=None):
|
|
|
|
"""Issue a raw assuan command
|
|
|
|
|
|
|
|
This function can be used to issue a raw assuan command to the
|
|
|
|
engine.
|
|
|
|
|
|
|
|
If command is a string or bytes, it will be used as-is. If it
|
|
|
|
is an iterable of strings, it will be properly escaped and
|
|
|
|
joined into an well-formed assuan command.
|
|
|
|
|
|
|
|
Keyword arguments:
|
|
|
|
data_cb -- a callback receiving data lines
|
|
|
|
inquire_cb -- a callback providing more information
|
|
|
|
status_cb -- a callback receiving status lines
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
result -- the result of command as GPGMEError
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
GPGMEError -- as signaled by the underlying library
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
if isinstance(command, (str, bytes)):
|
|
|
|
cmd = command
|
|
|
|
else:
|
|
|
|
cmd = " ".join(util.percent_escape(f) for f in command)
|
|
|
|
|
2016-07-28 13:44:38 +00:00
|
|
|
errptr = gpgme.new_gpgme_error_t_p()
|
2016-07-28 10:40:54 +00:00
|
|
|
|
2016-07-28 13:44:38 +00:00
|
|
|
err = gpgme.gpgme_op_assuan_transact_ext(
|
2016-07-28 10:40:54 +00:00
|
|
|
self.wrapped,
|
|
|
|
cmd,
|
|
|
|
(weakref.ref(self), data_cb) if data_cb else None,
|
|
|
|
(weakref.ref(self), inquire_cb) if inquire_cb else None,
|
|
|
|
(weakref.ref(self), status_cb) if status_cb else None,
|
|
|
|
errptr)
|
|
|
|
|
|
|
|
if self._callback_excinfo:
|
2016-10-28 20:45:49 +00:00
|
|
|
gpgme.gpg_raise_callback_exception(self)
|
2016-07-28 10:40:54 +00:00
|
|
|
|
|
|
|
errorcheck(err)
|
|
|
|
|
2016-07-28 13:44:38 +00:00
|
|
|
status = gpgme.gpgme_error_t_p_value(errptr)
|
|
|
|
gpgme.delete_gpgme_error_t_p(errptr)
|
2016-07-28 10:40:54 +00:00
|
|
|
|
|
|
|
return GPGMEError(status) if status != 0 else None
|
|
|
|
|
2016-09-16 12:56:29 +00:00
|
|
|
def interact(self, key, func, sink=None, flags=0, fnc_value=None):
|
|
|
|
"""Interact with the engine
|
|
|
|
|
|
|
|
This method can be used to edit keys and cards interactively.
|
|
|
|
KEY is the key to edit, FUNC is called repeatedly with two
|
|
|
|
unicode arguments, 'keyword' and 'args'. See the GPGME manual
|
|
|
|
for details.
|
|
|
|
|
|
|
|
Keyword arguments:
|
|
|
|
sink -- if given, additional output is written here
|
|
|
|
flags -- use constants.INTERACT_CARD to edit a card
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
GPGMEError -- as signaled by the underlying library
|
|
|
|
|
|
|
|
"""
|
|
|
|
if key == None:
|
|
|
|
raise ValueError("First argument cannot be None")
|
|
|
|
|
|
|
|
if sink == None:
|
|
|
|
sink = Data()
|
|
|
|
|
|
|
|
if fnc_value:
|
|
|
|
opaquedata = (weakref.ref(self), func, fnc_value)
|
|
|
|
else:
|
|
|
|
opaquedata = (weakref.ref(self), func)
|
|
|
|
|
|
|
|
result = gpgme.gpgme_op_interact(self.wrapped, key, flags,
|
|
|
|
opaquedata, sink)
|
|
|
|
if self._callback_excinfo:
|
2016-10-28 20:45:49 +00:00
|
|
|
gpgme.gpg_raise_callback_exception(self)
|
2016-09-16 12:56:29 +00:00
|
|
|
errorcheck(result)
|
|
|
|
|
2016-06-10 11:00:33 +00:00
|
|
|
@property
|
|
|
|
def signers(self):
|
|
|
|
"""Keys used for signing"""
|
|
|
|
return [self.signers_enum(i) for i in range(self.signers_count())]
|
|
|
|
@signers.setter
|
|
|
|
def signers(self, signers):
|
|
|
|
old = self.signers
|
|
|
|
self.signers_clear()
|
|
|
|
try:
|
|
|
|
for key in signers:
|
|
|
|
self.signers_add(key)
|
|
|
|
except:
|
|
|
|
self.signers = old
|
|
|
|
raise
|
|
|
|
|
|
|
|
@property
|
|
|
|
def pinentry_mode(self):
|
|
|
|
"""Pinentry mode"""
|
|
|
|
return self.get_pinentry_mode()
|
|
|
|
@pinentry_mode.setter
|
|
|
|
def pinentry_mode(self, value):
|
|
|
|
self.set_pinentry_mode(value)
|
|
|
|
|
2016-07-28 08:59:46 +00:00
|
|
|
@property
|
|
|
|
def protocol(self):
|
|
|
|
"""Protocol to use"""
|
|
|
|
return self.get_protocol()
|
|
|
|
@protocol.setter
|
|
|
|
def protocol(self, value):
|
2016-07-28 15:44:18 +00:00
|
|
|
errorcheck(gpgme.gpgme_engine_check_version(value))
|
2016-07-28 08:59:46 +00:00
|
|
|
self.set_protocol(value)
|
|
|
|
|
2017-02-14 12:43:01 +00:00
|
|
|
@property
|
|
|
|
def home_dir(self):
|
|
|
|
"""Engine's home directory"""
|
|
|
|
return self.engine_info.home_dir
|
|
|
|
@home_dir.setter
|
|
|
|
def home_dir(self, value):
|
|
|
|
self.set_engine_info(self.protocol, home_dir=value)
|
|
|
|
|
2016-06-14 10:37:26 +00:00
|
|
|
_ctype = 'gpgme_ctx_t'
|
|
|
|
_cprefix = 'gpgme_'
|
2015-05-05 17:09:44 +00:00
|
|
|
|
|
|
|
def _errorcheck(self, name):
|
|
|
|
"""This function should list all functions returning gpgme_error_t"""
|
2016-07-28 15:44:18 +00:00
|
|
|
return ((name.startswith('gpgme_op_')
|
|
|
|
and not name.endswith('_result'))
|
|
|
|
or name in {
|
|
|
|
'gpgme_set_ctx_flag',
|
|
|
|
'gpgme_set_protocol',
|
|
|
|
'gpgme_set_sub_protocol',
|
|
|
|
'gpgme_set_keylist_mode',
|
|
|
|
'gpgme_set_pinentry_mode',
|
|
|
|
'gpgme_set_locale',
|
|
|
|
'gpgme_set_engine_info',
|
|
|
|
'gpgme_signers_add',
|
|
|
|
'gpgme_get_sig_key',
|
|
|
|
'gpgme_sig_notation_add',
|
|
|
|
'gpgme_cancel',
|
|
|
|
'gpgme_cancel_async',
|
|
|
|
'gpgme_cancel_get_key',
|
|
|
|
})
|
2015-05-05 17:09:44 +00:00
|
|
|
|
2016-06-10 11:00:33 +00:00
|
|
|
_boolean_properties = {'armor', 'textmode', 'offline'}
|
2015-05-05 17:09:44 +00:00
|
|
|
|
|
|
|
def __del__(self):
|
2016-07-28 13:44:38 +00:00
|
|
|
if not gpgme:
|
|
|
|
# At interpreter shutdown, gpgme is set to NONE.
|
2016-05-12 09:53:43 +00:00
|
|
|
return
|
|
|
|
|
2015-05-05 17:09:44 +00:00
|
|
|
self._free_passcb()
|
|
|
|
self._free_progresscb()
|
2016-05-27 10:25:59 +00:00
|
|
|
self._free_statuscb()
|
2016-07-28 13:44:38 +00:00
|
|
|
if self.own and self.wrapped and gpgme.gpgme_release:
|
|
|
|
gpgme.gpgme_release(self.wrapped)
|
2016-06-07 17:31:10 +00:00
|
|
|
self.wrapped = None
|
|
|
|
|
|
|
|
# Implement the context manager protocol.
|
|
|
|
def __enter__(self):
|
|
|
|
return self
|
|
|
|
def __exit__(self, type, value, tb):
|
|
|
|
self.__del__()
|
2015-05-05 17:09:44 +00:00
|
|
|
|
|
|
|
def op_keylist_all(self, *args, **kwargs):
|
|
|
|
self.op_keylist_start(*args, **kwargs)
|
|
|
|
key = self.op_keylist_next()
|
|
|
|
while key:
|
|
|
|
yield key
|
|
|
|
key = self.op_keylist_next()
|
2016-06-06 10:49:11 +00:00
|
|
|
self.op_keylist_end()
|
2015-05-05 17:09:44 +00:00
|
|
|
|
|
|
|
def op_keylist_next(self):
|
|
|
|
"""Returns the next key in the list created
|
|
|
|
by a call to op_keylist_start(). The object returned
|
|
|
|
is of type Key."""
|
2016-07-28 13:44:38 +00:00
|
|
|
ptr = gpgme.new_gpgme_key_t_p()
|
2015-05-05 17:09:44 +00:00
|
|
|
try:
|
2016-07-28 13:44:38 +00:00
|
|
|
errorcheck(gpgme.gpgme_op_keylist_next(self.wrapped, ptr))
|
|
|
|
key = gpgme.gpgme_key_t_p_value(ptr)
|
2015-05-05 17:09:44 +00:00
|
|
|
except errors.GPGMEError as excp:
|
|
|
|
key = None
|
|
|
|
if excp.getcode() != errors.EOF:
|
|
|
|
raise excp
|
2016-07-28 13:44:38 +00:00
|
|
|
gpgme.delete_gpgme_key_t_p(ptr)
|
2015-05-05 17:09:44 +00:00
|
|
|
if key:
|
2016-07-28 13:44:38 +00:00
|
|
|
key.__del__ = lambda self: gpgme.gpgme_key_unref(self)
|
2015-05-05 17:09:44 +00:00
|
|
|
return key
|
2016-05-10 11:30:30 +00:00
|
|
|
|
2016-10-13 10:05:59 +00:00
|
|
|
def get_key(self, fpr, secret=False):
|
|
|
|
"""Get a key given a fingerprint
|
|
|
|
|
|
|
|
Keyword arguments:
|
|
|
|
secret -- to request a secret key
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
-- the matching key
|
|
|
|
|
|
|
|
Raises:
|
2016-10-13 11:13:23 +00:00
|
|
|
KeyError -- if the key was not found
|
2016-10-13 10:05:59 +00:00
|
|
|
GPGMEError -- as signaled by the underlying library
|
|
|
|
|
|
|
|
"""
|
2016-07-28 13:44:38 +00:00
|
|
|
ptr = gpgme.new_gpgme_key_t_p()
|
2016-10-13 11:13:23 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
errorcheck(gpgme.gpgme_get_key(self.wrapped, fpr, ptr, secret))
|
|
|
|
except errors.GPGMEError as e:
|
|
|
|
if e.getcode() == errors.EOF:
|
|
|
|
raise errors.KeyNotFound(fpr)
|
|
|
|
raise e
|
|
|
|
|
2016-07-28 13:44:38 +00:00
|
|
|
key = gpgme.gpgme_key_t_p_value(ptr)
|
|
|
|
gpgme.delete_gpgme_key_t_p(ptr)
|
2016-10-13 10:05:59 +00:00
|
|
|
assert key
|
|
|
|
key.__del__ = lambda self: gpgme.gpgme_key_unref(self)
|
|
|
|
return key
|
2015-05-05 17:09:44 +00:00
|
|
|
|
|
|
|
def op_trustlist_all(self, *args, **kwargs):
|
|
|
|
self.op_trustlist_start(*args, **kwargs)
|
2016-06-06 10:49:11 +00:00
|
|
|
trust = self.op_trustlist_next()
|
2015-05-05 17:09:44 +00:00
|
|
|
while trust:
|
|
|
|
yield trust
|
2016-06-06 10:49:11 +00:00
|
|
|
trust = self.op_trustlist_next()
|
|
|
|
self.op_trustlist_end()
|
2015-05-05 17:09:44 +00:00
|
|
|
|
|
|
|
def op_trustlist_next(self):
|
|
|
|
"""Returns the next trust item in the list created
|
|
|
|
by a call to op_trustlist_start(). The object returned
|
|
|
|
is of type TrustItem."""
|
2016-07-28 13:44:38 +00:00
|
|
|
ptr = gpgme.new_gpgme_trust_item_t_p()
|
2015-05-05 17:09:44 +00:00
|
|
|
try:
|
2016-07-28 13:44:38 +00:00
|
|
|
errorcheck(gpgme.gpgme_op_trustlist_next(self.wrapped, ptr))
|
|
|
|
trust = gpgme.gpgme_trust_item_t_p_value(ptr)
|
2015-05-05 17:09:44 +00:00
|
|
|
except errors.GPGMEError as excp:
|
|
|
|
trust = None
|
|
|
|
if excp.getcode() != errors.EOF:
|
|
|
|
raise
|
2016-07-28 13:44:38 +00:00
|
|
|
gpgme.delete_gpgme_trust_item_t_p(ptr)
|
2015-05-05 17:09:44 +00:00
|
|
|
return trust
|
|
|
|
|
|
|
|
def set_passphrase_cb(self, func, hook=None):
|
|
|
|
"""Sets the passphrase callback to the function specified by func.
|
|
|
|
|
|
|
|
When the system needs a passphrase, it will call func with three args:
|
|
|
|
hint, a string describing the key it needs the passphrase for;
|
|
|
|
desc, a string describing the passphrase it needs;
|
|
|
|
prev_bad, a boolean equal True if this is a call made after
|
|
|
|
unsuccessful previous attempt.
|
2016-05-10 11:30:30 +00:00
|
|
|
|
2015-05-05 17:09:44 +00:00
|
|
|
If hook has a value other than None it will be passed into the func
|
|
|
|
as a forth argument.
|
|
|
|
|
|
|
|
Please see the GPGME manual for more information.
|
|
|
|
"""
|
|
|
|
if func == None:
|
|
|
|
hookdata = None
|
|
|
|
else:
|
|
|
|
if hook == None:
|
2016-05-27 10:25:59 +00:00
|
|
|
hookdata = (weakref.ref(self), func)
|
2015-05-05 17:09:44 +00:00
|
|
|
else:
|
2016-05-27 10:25:59 +00:00
|
|
|
hookdata = (weakref.ref(self), func, hook)
|
2016-10-28 20:45:49 +00:00
|
|
|
gpgme.gpg_set_passphrase_cb(self, hookdata)
|
2016-06-13 17:16:30 +00:00
|
|
|
|
|
|
|
def _free_passcb(self):
|
2016-10-28 20:45:49 +00:00
|
|
|
if gpgme.gpg_set_passphrase_cb:
|
2016-06-13 17:16:30 +00:00
|
|
|
self.set_passphrase_cb(None)
|
2015-05-05 17:09:44 +00:00
|
|
|
|
|
|
|
def set_progress_cb(self, func, hook=None):
|
2016-05-24 13:12:40 +00:00
|
|
|
"""Sets the progress meter callback to the function specified by FUNC.
|
|
|
|
If FUNC is None, the callback will be cleared.
|
2015-05-05 17:09:44 +00:00
|
|
|
|
2016-05-24 13:12:40 +00:00
|
|
|
This function will be called to provide an interactive update
|
|
|
|
of the system's progress. The function will be called with
|
|
|
|
three arguments, type, total, and current. If HOOK is not
|
|
|
|
None, it will be supplied as fourth argument.
|
2015-05-05 17:09:44 +00:00
|
|
|
|
2016-05-24 13:12:40 +00:00
|
|
|
Please see the GPGME manual for more information.
|
|
|
|
|
|
|
|
"""
|
2015-05-05 17:09:44 +00:00
|
|
|
if func == None:
|
|
|
|
hookdata = None
|
|
|
|
else:
|
|
|
|
if hook == None:
|
2016-05-27 10:25:59 +00:00
|
|
|
hookdata = (weakref.ref(self), func)
|
2015-05-05 17:09:44 +00:00
|
|
|
else:
|
2016-05-27 10:25:59 +00:00
|
|
|
hookdata = (weakref.ref(self), func, hook)
|
2016-10-28 20:45:49 +00:00
|
|
|
gpgme.gpg_set_progress_cb(self, hookdata)
|
2016-06-13 17:16:30 +00:00
|
|
|
|
|
|
|
def _free_progresscb(self):
|
2016-10-28 20:45:49 +00:00
|
|
|
if gpgme.gpg_set_progress_cb:
|
2016-06-13 17:16:30 +00:00
|
|
|
self.set_progress_cb(None)
|
2015-05-05 17:09:44 +00:00
|
|
|
|
2016-05-24 13:14:53 +00:00
|
|
|
def set_status_cb(self, func, hook=None):
|
|
|
|
"""Sets the status callback to the function specified by FUNC. If
|
|
|
|
FUNC is None, the callback will be cleared.
|
|
|
|
|
|
|
|
The function will be called with two arguments, keyword and
|
|
|
|
args. If HOOK is not None, it will be supplied as third
|
|
|
|
argument.
|
|
|
|
|
|
|
|
Please see the GPGME manual for more information.
|
|
|
|
|
|
|
|
"""
|
|
|
|
if func == None:
|
|
|
|
hookdata = None
|
|
|
|
else:
|
|
|
|
if hook == None:
|
2016-05-27 10:25:59 +00:00
|
|
|
hookdata = (weakref.ref(self), func)
|
2016-05-24 13:14:53 +00:00
|
|
|
else:
|
2016-05-27 10:25:59 +00:00
|
|
|
hookdata = (weakref.ref(self), func, hook)
|
2016-10-28 20:45:49 +00:00
|
|
|
gpgme.gpg_set_status_cb(self, hookdata)
|
2016-06-13 17:16:30 +00:00
|
|
|
|
|
|
|
def _free_statuscb(self):
|
2016-10-28 20:45:49 +00:00
|
|
|
if gpgme.gpg_set_status_cb:
|
2016-06-13 17:16:30 +00:00
|
|
|
self.set_status_cb(None)
|
2016-05-24 13:14:53 +00:00
|
|
|
|
2016-07-28 09:16:35 +00:00
|
|
|
@property
|
|
|
|
def engine_info(self):
|
|
|
|
"""Configuration of the engine currently in use"""
|
|
|
|
p = self.protocol
|
|
|
|
infos = [i for i in self.get_engine_info() if i.protocol == p]
|
|
|
|
assert len(infos) == 1
|
|
|
|
return infos[0]
|
|
|
|
|
2015-05-05 17:09:44 +00:00
|
|
|
def get_engine_info(self):
|
2016-07-28 09:16:35 +00:00
|
|
|
"""Get engine configuration
|
|
|
|
|
|
|
|
Returns information about all configured and installed
|
|
|
|
engines.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
infos -- a list of engine infos
|
|
|
|
|
|
|
|
"""
|
2016-07-28 13:44:38 +00:00
|
|
|
return gpgme.gpgme_ctx_get_engine_info(self.wrapped)
|
2015-05-05 17:09:44 +00:00
|
|
|
|
2016-07-28 09:16:35 +00:00
|
|
|
def set_engine_info(self, proto, file_name=None, home_dir=None):
|
|
|
|
"""Change engine configuration
|
|
|
|
|
|
|
|
Changes the configuration of the crypto engine implementing
|
|
|
|
the protocol 'proto' for the context.
|
|
|
|
|
|
|
|
Keyword arguments:
|
|
|
|
file_name -- engine program file name (unchanged if None)
|
|
|
|
home_dir -- configuration directory (unchanged if None)
|
|
|
|
|
|
|
|
"""
|
2016-07-28 13:44:38 +00:00
|
|
|
errorcheck(gpgme.gpgme_ctx_set_engine_info(
|
2016-07-28 09:16:35 +00:00
|
|
|
self.wrapped, proto, file_name, home_dir))
|
2015-05-05 17:09:44 +00:00
|
|
|
|
|
|
|
def wait(self, hang):
|
2016-05-24 15:57:10 +00:00
|
|
|
"""Wait for asynchronous call to finish. Wait forever if hang is True.
|
|
|
|
Raises an exception on errors.
|
2015-05-05 17:09:44 +00:00
|
|
|
|
2016-05-24 15:57:10 +00:00
|
|
|
Please read the GPGME manual for more information.
|
2015-05-05 17:09:44 +00:00
|
|
|
|
2016-05-24 15:57:10 +00:00
|
|
|
"""
|
2016-07-28 13:44:38 +00:00
|
|
|
ptr = gpgme.new_gpgme_error_t_p()
|
|
|
|
gpgme.gpgme_wait(self.wrapped, ptr, hang)
|
|
|
|
status = gpgme.gpgme_error_t_p_value(ptr)
|
|
|
|
gpgme.delete_gpgme_error_t_p(ptr)
|
2016-05-24 15:57:10 +00:00
|
|
|
errorcheck(status)
|
2015-05-05 17:09:44 +00:00
|
|
|
|
|
|
|
def op_edit(self, key, func, fnc_value, out):
|
2016-09-16 12:56:29 +00:00
|
|
|
"""Start key editing using supplied callback function
|
|
|
|
|
|
|
|
Note: This interface is deprecated and will be removed with
|
|
|
|
GPGME 1.8. Please use .interact instead. Furthermore, we
|
|
|
|
implement this using gpgme_op_interact, so callbacks will get
|
|
|
|
called with string keywords instead of numeric status
|
|
|
|
messages. Code that is using constants.STATUS_X or
|
|
|
|
constants.status.X will continue to work, whereas code using
|
|
|
|
magic numbers will break as a result.
|
|
|
|
|
|
|
|
"""
|
|
|
|
warnings.warn("Call to deprecated method op_edit.",
|
|
|
|
category=DeprecationWarning)
|
|
|
|
return self.interact(key, func, sink=out, fnc_value=fnc_value)
|
2016-05-24 10:29:32 +00:00
|
|
|
|
2016-05-10 11:30:30 +00:00
|
|
|
|
2015-05-05 17:09:44 +00:00
|
|
|
class Data(GpgmeWrapper):
|
2016-06-06 12:08:59 +00:00
|
|
|
"""Data buffer
|
2015-05-05 17:09:44 +00:00
|
|
|
|
2016-06-06 12:08:59 +00:00
|
|
|
A lot of data has to be exchanged between the user and the crypto
|
|
|
|
engine, like plaintext messages, ciphertext, signatures and
|
|
|
|
information about the keys. The technical details about
|
|
|
|
exchanging the data information are completely abstracted by
|
|
|
|
GPGME. The user provides and receives the data via `gpgme_data_t'
|
|
|
|
objects, regardless of the communication protocol between GPGME
|
|
|
|
and the crypto engine in use.
|
2015-05-05 17:09:44 +00:00
|
|
|
|
2016-06-06 12:08:59 +00:00
|
|
|
This Data class is the implementation of the GpgmeData objects.
|
2015-05-05 17:09:44 +00:00
|
|
|
|
2016-06-06 12:08:59 +00:00
|
|
|
Please see the information about __init__ for instantiation.
|
|
|
|
|
|
|
|
"""
|
2015-05-05 17:09:44 +00:00
|
|
|
|
2016-06-14 10:37:26 +00:00
|
|
|
_ctype = 'gpgme_data_t'
|
|
|
|
_cprefix = 'gpgme_data_'
|
2015-05-05 17:09:44 +00:00
|
|
|
|
|
|
|
def _errorcheck(self, name):
|
|
|
|
"""This function should list all functions returning gpgme_error_t"""
|
2016-05-27 13:58:23 +00:00
|
|
|
return name not in {
|
|
|
|
'gpgme_data_release_and_get_mem',
|
|
|
|
'gpgme_data_get_encoding',
|
|
|
|
'gpgme_data_seek',
|
|
|
|
'gpgme_data_get_file_name',
|
|
|
|
}
|
2016-05-10 11:30:30 +00:00
|
|
|
|
2016-05-25 10:47:28 +00:00
|
|
|
def __init__(self, string=None, file=None, offset=None,
|
|
|
|
length=None, cbs=None, copy=True):
|
2015-05-05 17:09:44 +00:00
|
|
|
"""Initialize a new gpgme_data_t object.
|
|
|
|
|
|
|
|
If no args are specified, make it an empty object.
|
|
|
|
|
|
|
|
If string alone is specified, initialize it with the data
|
|
|
|
contained there.
|
|
|
|
|
|
|
|
If file, offset, and length are all specified, file must
|
|
|
|
be either a filename or a file-like object, and the object
|
|
|
|
will be initialized by reading the specified chunk from the file.
|
|
|
|
|
|
|
|
If cbs is specified, it MUST be a tuple of the form:
|
|
|
|
|
2016-05-27 12:04:28 +00:00
|
|
|
(read_cb, write_cb, seek_cb, release_cb[, hook])
|
2015-05-05 17:09:44 +00:00
|
|
|
|
2016-06-06 12:08:59 +00:00
|
|
|
where the first four items are functions implementing reading,
|
|
|
|
writing, seeking the data, and releasing any resources once
|
|
|
|
the data object is deallocated. The functions must match the
|
|
|
|
following prototypes:
|
|
|
|
|
|
|
|
def read(amount, hook=None):
|
|
|
|
return <a b"bytes" object>
|
|
|
|
|
|
|
|
def write(data, hook=None):
|
|
|
|
return <the number of bytes written>
|
|
|
|
|
|
|
|
def seek(offset, whence, hook=None):
|
|
|
|
return <the new file position>
|
|
|
|
|
|
|
|
def release(hook=None):
|
|
|
|
<return value and exceptions are ignored>
|
|
|
|
|
|
|
|
The functions may be bound methods. In that case, you can
|
|
|
|
simply use the 'self' reference instead of using a hook.
|
2015-05-05 17:09:44 +00:00
|
|
|
|
|
|
|
If file is specified without any other arguments, then
|
|
|
|
it must be a filename, and the object will be initialized from
|
|
|
|
that file.
|
|
|
|
|
2016-06-06 12:08:59 +00:00
|
|
|
"""
|
2016-09-12 15:21:34 +00:00
|
|
|
super(Data, self).__init__(None)
|
2016-05-27 12:04:28 +00:00
|
|
|
self.data_cbs = None
|
2015-05-05 17:09:44 +00:00
|
|
|
|
|
|
|
if cbs != None:
|
|
|
|
self.new_from_cbs(*cbs)
|
|
|
|
elif string != None:
|
2016-05-25 10:47:28 +00:00
|
|
|
self.new_from_mem(string, copy)
|
2015-05-05 17:09:44 +00:00
|
|
|
elif file != None and offset != None and length != None:
|
|
|
|
self.new_from_filepart(file, offset, length)
|
|
|
|
elif file != None:
|
2016-09-13 08:44:14 +00:00
|
|
|
if util.is_a_string(file):
|
2016-05-25 10:47:28 +00:00
|
|
|
self.new_from_file(file, copy)
|
2015-05-05 17:09:44 +00:00
|
|
|
else:
|
|
|
|
self.new_from_fd(file)
|
|
|
|
else:
|
|
|
|
self.new()
|
|
|
|
|
|
|
|
def __del__(self):
|
2016-07-28 13:44:38 +00:00
|
|
|
if not gpgme:
|
|
|
|
# At interpreter shutdown, gpgme is set to NONE.
|
2016-05-12 09:53:43 +00:00
|
|
|
return
|
|
|
|
|
2016-07-28 13:44:38 +00:00
|
|
|
if self.wrapped != None and gpgme.gpgme_data_release:
|
|
|
|
gpgme.gpgme_data_release(self.wrapped)
|
2016-05-27 12:04:28 +00:00
|
|
|
if self._callback_excinfo:
|
2016-10-28 20:45:49 +00:00
|
|
|
gpgme.gpg_raise_callback_exception(self)
|
2016-06-07 17:31:10 +00:00
|
|
|
self.wrapped = None
|
2016-05-27 12:04:28 +00:00
|
|
|
self._free_datacbs()
|
2015-05-05 17:09:44 +00:00
|
|
|
|
2016-06-07 17:31:10 +00:00
|
|
|
# Implement the context manager protocol.
|
|
|
|
def __enter__(self):
|
|
|
|
return self
|
|
|
|
def __exit__(self, type, value, tb):
|
|
|
|
self.__del__()
|
|
|
|
|
2016-05-27 12:04:28 +00:00
|
|
|
def _free_datacbs(self):
|
2016-06-13 17:16:30 +00:00
|
|
|
self._data_cbs = None
|
2015-05-05 17:09:44 +00:00
|
|
|
|
|
|
|
def new(self):
|
2016-07-28 13:44:38 +00:00
|
|
|
tmp = gpgme.new_gpgme_data_t_p()
|
|
|
|
errorcheck(gpgme.gpgme_data_new(tmp))
|
|
|
|
self.wrapped = gpgme.gpgme_data_t_p_value(tmp)
|
|
|
|
gpgme.delete_gpgme_data_t_p(tmp)
|
2015-05-05 17:09:44 +00:00
|
|
|
|
2016-05-25 10:47:28 +00:00
|
|
|
def new_from_mem(self, string, copy=True):
|
2016-07-28 13:44:38 +00:00
|
|
|
tmp = gpgme.new_gpgme_data_t_p()
|
|
|
|
errorcheck(gpgme.gpgme_data_new_from_mem(tmp,string,len(string),copy))
|
|
|
|
self.wrapped = gpgme.gpgme_data_t_p_value(tmp)
|
|
|
|
gpgme.delete_gpgme_data_t_p(tmp)
|
2015-05-05 17:09:44 +00:00
|
|
|
|
2016-05-25 10:47:28 +00:00
|
|
|
def new_from_file(self, filename, copy=True):
|
2016-07-28 13:44:38 +00:00
|
|
|
tmp = gpgme.new_gpgme_data_t_p()
|
2016-05-25 10:47:28 +00:00
|
|
|
try:
|
2016-07-28 13:44:38 +00:00
|
|
|
errorcheck(gpgme.gpgme_data_new_from_file(tmp, filename, copy))
|
2016-05-25 10:47:28 +00:00
|
|
|
except errors.GPGMEError as e:
|
|
|
|
if e.getcode() == errors.INV_VALUE and not copy:
|
|
|
|
raise ValueError("delayed reads are not yet supported")
|
|
|
|
else:
|
|
|
|
raise e
|
2016-07-28 13:44:38 +00:00
|
|
|
self.wrapped = gpgme.gpgme_data_t_p_value(tmp)
|
|
|
|
gpgme.delete_gpgme_data_t_p(tmp)
|
2015-05-05 17:09:44 +00:00
|
|
|
|
2016-05-27 12:04:28 +00:00
|
|
|
def new_from_cbs(self, read_cb, write_cb, seek_cb, release_cb, hook=None):
|
2016-07-28 13:44:38 +00:00
|
|
|
tmp = gpgme.new_gpgme_data_t_p()
|
2016-05-27 12:04:28 +00:00
|
|
|
if hook != None:
|
|
|
|
hookdata = (weakref.ref(self),
|
|
|
|
read_cb, write_cb, seek_cb, release_cb, hook)
|
|
|
|
else:
|
|
|
|
hookdata = (weakref.ref(self),
|
|
|
|
read_cb, write_cb, seek_cb, release_cb)
|
2016-10-28 20:45:49 +00:00
|
|
|
gpgme.gpg_data_new_from_cbs(self, hookdata, tmp)
|
2016-07-28 13:44:38 +00:00
|
|
|
self.wrapped = gpgme.gpgme_data_t_p_value(tmp)
|
|
|
|
gpgme.delete_gpgme_data_t_p(tmp)
|
2015-05-05 17:09:44 +00:00
|
|
|
|
|
|
|
def new_from_filepart(self, file, offset, length):
|
|
|
|
"""This wraps the GPGME gpgme_data_new_from_filepart() function.
|
|
|
|
The argument "file" may be:
|
|
|
|
|
2016-06-06 12:08:59 +00:00
|
|
|
* a string specifying a file name, or
|
|
|
|
* a file-like object supporting the fileno() and the mode attribute.
|
|
|
|
|
|
|
|
"""
|
2015-05-05 17:09:44 +00:00
|
|
|
|
2016-07-28 13:44:38 +00:00
|
|
|
tmp = gpgme.new_gpgme_data_t_p()
|
2015-05-05 17:09:44 +00:00
|
|
|
filename = None
|
|
|
|
fp = None
|
|
|
|
|
2016-09-13 08:44:14 +00:00
|
|
|
if util.is_a_string(file):
|
2015-05-05 17:09:44 +00:00
|
|
|
filename = file
|
|
|
|
else:
|
2016-07-28 13:44:38 +00:00
|
|
|
fp = gpgme.fdopen(file.fileno(), file.mode)
|
2015-05-05 17:09:44 +00:00
|
|
|
if fp == None:
|
|
|
|
raise ValueError("Failed to open file from %s arg %s" % \
|
|
|
|
(str(type(file)), str(file)))
|
|
|
|
|
2016-07-28 13:44:38 +00:00
|
|
|
errorcheck(gpgme.gpgme_data_new_from_filepart(tmp, filename, fp,
|
2015-05-05 17:09:44 +00:00
|
|
|
offset, length))
|
2016-07-28 13:44:38 +00:00
|
|
|
self.wrapped = gpgme.gpgme_data_t_p_value(tmp)
|
|
|
|
gpgme.delete_gpgme_data_t_p(tmp)
|
2015-05-05 17:09:44 +00:00
|
|
|
|
|
|
|
def new_from_fd(self, file):
|
2016-05-25 10:47:28 +00:00
|
|
|
"""This wraps the GPGME gpgme_data_new_from_fd() function. The
|
|
|
|
argument "file" must be a file-like object, supporting the
|
|
|
|
fileno() method.
|
2016-05-10 11:30:30 +00:00
|
|
|
|
2016-05-25 10:47:28 +00:00
|
|
|
"""
|
2016-07-28 13:44:38 +00:00
|
|
|
tmp = gpgme.new_gpgme_data_t_p()
|
|
|
|
errorcheck(gpgme.gpgme_data_new_from_fd(tmp, file.fileno()))
|
|
|
|
self.wrapped = gpgme.gpgme_data_t_p_value(tmp)
|
|
|
|
gpgme.delete_gpgme_data_t_p(tmp)
|
2015-05-05 17:09:44 +00:00
|
|
|
|
|
|
|
def new_from_stream(self, file):
|
|
|
|
"""This wrap around gpgme_data_new_from_stream is an alias for
|
|
|
|
new_from_fd() method since in python there's not difference
|
|
|
|
between file stream and file descriptor"""
|
|
|
|
self.new_from_fd(file)
|
2016-05-10 11:30:30 +00:00
|
|
|
|
2015-05-05 17:09:44 +00:00
|
|
|
def write(self, buffer):
|
2016-05-12 15:44:54 +00:00
|
|
|
"""Write buffer given as string or bytes.
|
|
|
|
|
|
|
|
If a string is given, it is implicitly encoded using UTF-8."""
|
2016-07-28 13:44:38 +00:00
|
|
|
written = gpgme.gpgme_data_write(self.wrapped, buffer)
|
2016-05-12 16:00:16 +00:00
|
|
|
if written < 0:
|
2016-05-27 12:04:28 +00:00
|
|
|
if self._callback_excinfo:
|
2016-10-28 20:45:49 +00:00
|
|
|
gpgme.gpg_raise_callback_exception(self)
|
2016-05-27 12:04:28 +00:00
|
|
|
else:
|
|
|
|
raise GPGMEError.fromSyserror()
|
2016-05-12 16:00:16 +00:00
|
|
|
return written
|
2015-05-05 17:09:44 +00:00
|
|
|
|
|
|
|
def read(self, size = -1):
|
2016-05-12 09:21:58 +00:00
|
|
|
"""Read at most size bytes, returned as bytes.
|
2016-05-10 11:30:30 +00:00
|
|
|
|
2015-05-05 17:09:44 +00:00
|
|
|
If the size argument is negative or omitted, read until EOF is reached.
|
|
|
|
|
|
|
|
Returns the data read, or the empty string if there was no data
|
|
|
|
to read before EOF was reached."""
|
2016-05-10 11:30:30 +00:00
|
|
|
|
2015-05-05 17:09:44 +00:00
|
|
|
if size == 0:
|
|
|
|
return ''
|
|
|
|
|
|
|
|
if size > 0:
|
2016-05-27 12:04:28 +00:00
|
|
|
try:
|
2016-07-28 13:44:38 +00:00
|
|
|
result = gpgme.gpgme_data_read(self.wrapped, size)
|
2016-05-27 12:04:28 +00:00
|
|
|
except:
|
|
|
|
if self._callback_excinfo:
|
2016-10-28 20:45:49 +00:00
|
|
|
gpgme.gpg_raise_callback_exception(self)
|
2016-05-27 12:04:28 +00:00
|
|
|
else:
|
|
|
|
raise
|
|
|
|
return result
|
2015-05-05 17:09:44 +00:00
|
|
|
else:
|
2016-05-12 09:21:58 +00:00
|
|
|
chunks = []
|
2016-05-27 12:04:28 +00:00
|
|
|
while True:
|
|
|
|
try:
|
2016-07-28 13:44:38 +00:00
|
|
|
result = gpgme.gpgme_data_read(self.wrapped, 4096)
|
2016-05-27 12:04:28 +00:00
|
|
|
except:
|
|
|
|
if self._callback_excinfo:
|
2016-10-28 20:45:49 +00:00
|
|
|
gpgme.gpg_raise_callback_exception(self)
|
2016-05-27 12:04:28 +00:00
|
|
|
else:
|
|
|
|
raise
|
2015-05-05 17:09:44 +00:00
|
|
|
if len(result) == 0:
|
|
|
|
break
|
2016-05-12 09:21:58 +00:00
|
|
|
chunks.append(result)
|
|
|
|
return b''.join(chunks)
|
2015-05-05 17:09:44 +00:00
|
|
|
|
|
|
|
def pubkey_algo_name(algo):
|
2016-07-28 13:44:38 +00:00
|
|
|
return gpgme.gpgme_pubkey_algo_name(algo)
|
2015-05-05 17:09:44 +00:00
|
|
|
|
|
|
|
def hash_algo_name(algo):
|
2016-07-28 13:44:38 +00:00
|
|
|
return gpgme.gpgme_hash_algo_name(algo)
|
2015-05-05 17:09:44 +00:00
|
|
|
|
|
|
|
def get_protocol_name(proto):
|
2016-07-28 13:44:38 +00:00
|
|
|
return gpgme.gpgme_get_protocol_name(proto)
|
2015-05-05 17:09:44 +00:00
|
|
|
|
|
|
|
def check_version(version=None):
|
2016-07-28 13:44:38 +00:00
|
|
|
return gpgme.gpgme_check_version(version)
|
2015-05-05 17:09:44 +00:00
|
|
|
|
2016-06-02 13:18:40 +00:00
|
|
|
# check_version also makes sure that several subsystems are properly
|
|
|
|
# initialized, and it must be run at least once before invoking any
|
|
|
|
# other function. We do it here so that the user does not have to do
|
|
|
|
# it unless she really wants to check for a certain version.
|
|
|
|
check_version()
|
|
|
|
|
2015-05-05 17:09:44 +00:00
|
|
|
def engine_check_version (proto):
|
|
|
|
try:
|
2016-07-28 13:44:38 +00:00
|
|
|
errorcheck(gpgme.gpgme_engine_check_version(proto))
|
2015-05-05 17:09:44 +00:00
|
|
|
return True
|
|
|
|
except errors.GPGMEError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def get_engine_info():
|
2016-07-28 13:44:38 +00:00
|
|
|
ptr = gpgme.new_gpgme_engine_info_t_p()
|
2015-05-05 17:09:44 +00:00
|
|
|
try:
|
2016-07-28 13:44:38 +00:00
|
|
|
errorcheck(gpgme.gpgme_get_engine_info(ptr))
|
|
|
|
info = gpgme.gpgme_engine_info_t_p_value(ptr)
|
2015-05-05 17:09:44 +00:00
|
|
|
except errors.GPGMEError:
|
|
|
|
info = None
|
2016-07-28 13:44:38 +00:00
|
|
|
gpgme.delete_gpgme_engine_info_t_p(ptr)
|
2015-05-05 17:09:44 +00:00
|
|
|
return info
|
|
|
|
|
|
|
|
def set_engine_info(proto, file_name, home_dir=None):
|
|
|
|
"""Changes the default configuration of the crypto engine implementing
|
|
|
|
the protocol 'proto'. 'file_name' is the file name of
|
|
|
|
the executable program implementing this protocol. 'home_dir' is the
|
|
|
|
directory name of the configuration directory (engine's default is
|
|
|
|
used if omitted)."""
|
2016-07-28 13:44:38 +00:00
|
|
|
errorcheck(gpgme.gpgme_set_engine_info(proto, file_name, home_dir))
|
2015-05-05 17:09:44 +00:00
|
|
|
|
|
|
|
def set_locale(category, value):
|
|
|
|
"""Sets the default locale used by contexts"""
|
2016-07-28 13:44:38 +00:00
|
|
|
errorcheck(gpgme.gpgme_set_locale(None, category, value))
|
2015-05-05 17:09:44 +00:00
|
|
|
|
|
|
|
def wait(hang):
|
|
|
|
"""Wait for asynchronous call on any Context to finish.
|
|
|
|
Wait forever if hang is True.
|
2016-05-10 11:30:30 +00:00
|
|
|
|
2015-05-05 17:09:44 +00:00
|
|
|
For finished anynch calls it returns a tuple (status, context):
|
|
|
|
status - status return by asnynchronous call.
|
|
|
|
context - context which caused this call to return.
|
2016-05-10 11:30:30 +00:00
|
|
|
|
2015-05-05 17:09:44 +00:00
|
|
|
Please read the GPGME manual of more information."""
|
2016-07-28 13:44:38 +00:00
|
|
|
ptr = gpgme.new_gpgme_error_t_p()
|
|
|
|
context = gpgme.gpgme_wait(None, ptr, hang)
|
|
|
|
status = gpgme.gpgme_error_t_p_value(ptr)
|
|
|
|
gpgme.delete_gpgme_error_t_p(ptr)
|
2015-05-05 17:09:44 +00:00
|
|
|
if context == None:
|
|
|
|
errorcheck(status)
|
|
|
|
else:
|
|
|
|
context = Context(context)
|
|
|
|
return (status, context)
|