for attachment in args.attachment:
body.attach(load_attachment(
filename=attachment, encoding=args.encoding))
+
+ config = _configparser.ConfigParser()
+ config.read(args.config)
+ client_params = _pgp_mime.get_client_params(config)
+
if args.sign_as:
signers = [args.sign_as]
else:
if 'encrypt' in args.mode:
recipients = [email for name,email in _pgp_mime.email_targets(header)]
if args.mode == 'sign':
- body = _pgp_mime.sign(body, signers=signers, allow_default_signer=True)
+ body = _pgp_mime.sign(
+ body, signers=signers, allow_default_signer=True, **client_params)
elif args.mode == 'encrypt':
- body = _pgp_mime.encrypt(body, recipients=recipients)
+ body = _pgp_mime.encrypt(body, recipients=recipients, **client_params)
elif args.mode == 'sign-encrypt':
body = _pgp_mime.sign_and_encrypt(
body, signers=signers, recipients=recipients,
- allow_default_signer=True)
+ allow_default_signer=True, **client_params)
elif args.mode == 'plain':
pass
else:
if args.output:
print(message.as_string())
else:
- config = _configparser.ConfigParser()
- config.read(args.config)
- params = _pgp_mime.get_smtp_params(config)
- smtp = _pgp_mime.get_smtp(*params)
+ smtp_params = _pgp_mime.get_smtp_params(config)
+ smtp = _pgp_mime.get_smtp(*smtp_params)
try:
_pgp_mime.mail(message, smtp)
finally:
LOG.addHandler(_logging.StreamHandler())
+from .crypt import get_client_params
from .pgp import sign, encrypt, sign_and_encrypt, decrypt, verify
from .email import (
header_from_text, guess_encoding, encodedMIMEText, strip_bcc, append_text,
from . import signature as _signature
-def get_client(**kwargs):
+SOCKET_PATH = _os_path.expanduser(_os_path.join('~', '.gnupg', 'S.gpgme-tool'))
+
+
+def get_client_params(config):
+ r"""Retrieve Assuan client paramters from a config file.
+
+ >>> from configparser import ConfigParser
+ >>> config = ConfigParser()
+ >>> config.read_string('\n'.join([
+ ... '[gpgme-tool]',
+ ... 'socket-path: /tmp/S.gpgme-tool',
+ ... ]))
+ >>> get_client_params(config)
+ {'socket_path': '/tmp/S.gpgme-tool'}
+ >>> config = ConfigParser()
+ >>> get_smtp_params(ConfigParser())
+ {'socket_path': None}
+ """
+ params = {'socket_path': None}
+ try:
+ params['socket_path'] = config.get('gpgme-tool', 'socket-path')
+ except _configparser.NoSectionError:
+ return params
+ except _configparser.NoOptionError:
+ pass
+ return params
+
+def get_client(socket_path=None):
+ if socket_path is None:
+ socket_path = socket_path
logger = _logging.getLogger('{}.{}'.format(_LOG.name, 'pyassuan'))
client = _client.AssuanClient(
name='pgp-mime', logger=logger, use_sublogger=False,
close_on_disconnect=True)
- client.connect(socket_path='/tmp/gpgme-tool.sock')
+ client.connect(socket_path=socket_path)
return client
def disconnect(client):
def sign_and_encrypt_bytes(data, signers=None, recipients=None,
always_trust=False, mode='detach',
- allow_default_signer=False):
+ allow_default_signer=False, **kwargs):
r"""Sign ``data`` with ``signers`` and encrypt to ``recipients``.
Just sign (with a detached signature):
"""
input_read,input_write = _os.pipe()
output_read,output_write = _os.pipe()
- client = get_client()
+ client = get_client(**kwargs)
try:
hello(client)
if signers:
_os.close(fd)
return d
-def decrypt_bytes(data):
+def decrypt_bytes(data, **kwargs):
r"""Decrypt ``data``.
>>> b = '\n'.join([
"""
input_read,input_write = _os.pipe()
output_read,output_write = _os.pipe()
- client = get_client()
+ client = get_client(**kwargs)
try:
hello(client)
client.send_fds([input_read])
_os.close(fd)
return d
-def verify_bytes(data, signature=None, always_trust=False):
+def verify_bytes(data, signature=None, always_trust=False, **kwargs):
r"""Verify a signature on ``data``, possibly decrypting first.
These tests assume you didn't trust the distributed test key.
else:
message_read = message_write = -1
output_read,output_write = _os.pipe()
- client = get_client()
+ client = get_client(**kwargs)
verified = None
signatures = []
try:
return int(self.fingerprint, 16)
-def lookup_keys(patterns=None):
+def lookup_keys(patterns=None, **kwargs):
"""Lookup keys matching any patterns listed in ``patterns``.
>>> import pprint
[..., <Key 4332B6E3>, ...]
"""
_LOG.debug('lookup key: {}'.format(patterns))
- client = _crypt.get_client()
+ client = _crypt.get_client(**kwargs)
parameters = []
if patterns:
args = [' '.join(patterns)]
from .email import strip_bcc as _strip_bcc
-def sign(message, signers=None, allow_default_signer=False):
+def sign(message, signers=None, **kwargs):
r"""Sign a ``Message``, returning the signed version.
multipart/signed
"""
body = message.as_string().encode('us-ascii').replace(b'\n', b'\r\n')
# use email.policy.SMTP once we get Python 3.3
- signature = str(_sign_and_encrypt_bytes(
- data=body, signers=signers,
- allow_default_signer=allow_default_signer), 'us-ascii')
+ signature = str(_sign_and_encrypt_bytes(data=body, **kwargs), 'us-ascii')
sig = _MIMEApplication(
_data=signature,
_subtype='pgp-signature; name="signature.asc"',
msg['Content-Disposition'] = 'inline'
return msg
-def encrypt(message, recipients=None, always_trust=True):
+def encrypt(message, recipients=None, **kwargs):
r"""Encrypt a ``Message``, returning the encrypted version.
multipart/encrypted
recipients = [email for name,email in _email_targets(message)]
_LOG.debug('extracted encryption recipients: {}'.format(recipients))
encrypted = str(_sign_and_encrypt_bytes(
- data=body, recipients=recipients,
- always_trust=always_trust), 'us-ascii')
+ data=body, recipients=recipients, **kwargs), 'us-ascii')
enc = _MIMEApplication(
_data=encrypted,
_subtype='octet-stream; name="encrypted.asc"',
msg['Content-Disposition'] = 'inline'
return msg
-def sign_and_encrypt(message, signers=None, recipients=None,
- always_trust=False, allow_default_signer=False):
+def sign_and_encrypt(message, signers=None, recipients=None, **kwargs):
r"""Sign and encrypt a ``Message``, returning the encrypted version.
multipart/encrypted
if recipients is None:
recipients = [email for name,email in _email_targets(message)]
_LOG.debug('extracted encryption recipients: {}'.format(recipients))
- encrypted = str(_sign_and_encrypt_bytes(
- data=body, signers=signers, recipients=recipients,
- always_trust=always_trust,
- allow_default_signer=allow_default_signer), 'us-ascii')
+ encrypted = str(
+ _sign_and_encrypt_bytes(
+ data=body, signers=signers, recipients=recipients, **kwargs),
+ 'us-ascii')
enc = _MIMEApplication(
_data=encrypted,
_subtype='octet-stream; name="encrypted.asc"',
raise ValueError('missing application/pgp-signature part')
return (body, signature)
-def decrypt(message):
+def decrypt(message, **kwargs):
r"""Decrypt a multipart/encrypted message.
>>> from pgp_mime.email import encodedMIMEText
encrypted = body.get_payload(decode=True)
if not isinstance(encrypted, bytes):
encrypted = encrypted.encode('us-ascii')
- decrypted,verified,result = _verify_bytes(encrypted)
+ decrypted,verified,result = _verify_bytes(encrypted, **kwargs)
return _message_from_bytes(decrypted)
-def verify(message):
+def verify(message, **kwargs):
r"""Verify a signature on ``message``, possibly decrypting first.
>>> from pgp_mime.email import encodedMIMEText
sig_data = sig_data.encode('us-ascii')
decrypted,verified,result = _verify_bytes(
body.as_string().encode('us-ascii').replace(b'\n', b'\r\n'),
- signature=sig_data)
+ signature=sig_data, **kwargs)
# use email.policy.SMTP once we get Python 3.3
return (_copy.deepcopy(body), verified, result)