Merge async work into master for 1.0

This commit is contained in:
Scott Kitterman
2019-12-09 09:18:25 -05:00
6 changed files with 248 additions and 38 deletions
+2
View File
@@ -1,5 +1,7 @@
Version 1.0.0 Version 1.0.0
- Add support for RFC 8460 tlsrpt DKIM signature processing (LP: #1847020) - Add support for RFC 8460 tlsrpt DKIM signature processing (LP: #1847020)
- Add async support with aiodns for DKIM verification (ARC not supported)
(LP: #1847002)
- Add new timeout parameter to enable DNS lookup timeouts to be adjusted - Add new timeout parameter to enable DNS lookup timeouts to be adjusted
- Add new DKIM.present function to allow applications to test if a DKIM - Add new DKIM.present function to allow applications to test if a DKIM
signature is present without doing validation (LP: #1851141) signature is present without doing validation (LP: #1851141)
+3
View File
@@ -19,6 +19,8 @@ Dependencies will be automatically included for normal DKIM usage. The
extras_requires feature 'ed25519' will add the dependencies needed for signing extras_requires feature 'ed25519' will add the dependencies needed for signing
and verifying using the new DCRUP ed25519-sha256 algorithm. The and verifying using the new DCRUP ed25519-sha256 algorithm. The
extras_requires feature 'ARC' will add the extra dependencies needed for ARC. extras_requires feature 'ARC' will add the extra dependencies needed for ARC.
Similarly, extras_requires feature 'asyncio' will add the extra dependencies
needed for asyncio.
- Python 2.x >= 2.7, or Python 3.x >= 3.5. Recent versions have not been - Python 2.x >= 2.7, or Python 3.x >= 3.5. Recent versions have not been
tested on python < 2.7 or python3 < 3.5, but may still work on python2.6 tested on python < 2.7 or python3 < 3.5, but may still work on python2.6
@@ -28,6 +30,7 @@ extras_requires feature 'ARC' will add the extra dependencies needed for ARC.
- argparse. Standard library in python2.7 and later. - argparse. Standard library in python2.7 and later.
- authres. Needed for ARC. - authres. Needed for ARC.
- PyNaCl. Needed for use of ed25519 capability. - PyNaCl. Needed for use of ed25519 capability.
- aiodns. Needed for asycnio (Required python3.5 or later)
INSTALLATION INSTALLATION
+68 -35
View File
@@ -36,6 +36,7 @@ import base64
import hashlib import hashlib
import logging import logging
import re import re
import sys
import time import time
import binascii import binascii
@@ -71,6 +72,11 @@ from dkim.crypto import (
try: try:
from dkim.dnsplug import get_txt from dkim.dnsplug import get_txt
except ImportError: except ImportError:
try:
import aiodns
from dkim.asyncsupport import get_txt_async as get_txt
except:
# Only true if not using async
def get_txt(s,timeout=5): def get_txt(s,timeout=5):
raise RuntimeError("DKIM.verify requires DNS or dnspython module") raise RuntimeError("DKIM.verify requires DNS or dnspython module")
from dkim.util import ( from dkim.util import (
@@ -420,8 +426,7 @@ def fold(header, namelen=0, linesep=b'\r\n'):
return pre + header return pre + header
def load_pk_from_dns(name, dnsfunc=get_txt, timeout=5): def evaluate_pk(name, s):
s = dnsfunc(name, timeout=timeout)
if not s: if not s:
raise KeyFormatError("missing public key: %s"%name) raise KeyFormatError("missing public key: %s"%name)
try: try:
@@ -470,6 +475,12 @@ def load_pk_from_dns(name, dnsfunc=get_txt, timeout=5):
return pk, keysize, ktag, seqtlsrpt return pk, keysize, ktag, seqtlsrpt
def load_pk_from_dns(name, dnsfunc=get_txt, timeout=5):
s = dnsfunc(name, timeout=timeout)
pk, keysize, ktag, seqtlsrpt = evaluate_pk(name, s)
return pk, keysize, ktag, seqtlsrpt
#: Abstract base class for holding messages and options during DKIM/ARC signing and verification. #: Abstract base class for holding messages and options during DKIM/ARC signing and verification.
class DomainSigner(object): class DomainSigner(object):
# NOTE - the first 2 indentation levels are 2 instead of 4 # NOTE - the first 2 indentation levels are 2 instead of 4
@@ -676,27 +687,12 @@ class DomainSigner(object):
return header_value return header_value
# Abstract helper method to verify a signed header def verify_sig_process(self, sig, include_headers, sig_header, dnsfunc):
#: @param sig: List of (key, value) tuples containing tag=values of the header """Non-async sensitive verify_sig elements. Separated to avoid async code
#: @param include_headers: headers to validate b= signature against duplication."""
#: @param sig_header: (header_name, header_value)
#: @param dnsfunc: interface to dns
def verify_sig(self, sig, include_headers, sig_header, dnsfunc):
name = sig[b's'] + b"._domainkey." + sig[b'd'] + b"."
try:
pk, self.keysize, ktag, self.seqtlsrpt = load_pk_from_dns(name, dnsfunc,
timeout=self.timeout)
except KeyFormatError as e:
self.logger.error("%s" % e)
return False
except binascii.Error as e:
self.logger.error('KeyFormatError: {0}'.format(e))
return False
# RFC 8460 MAY ignore signatures without tlsrpt Service Type # RFC 8460 MAY ignore signatures without tlsrpt Service Type
if self.tlsrpt == 'strict' and not self.seqtlsrpt: if self.tlsrpt == 'strict' and not self.seqtlsrpt:
raise ValidationError("Message is tlsrpt and Service Type is not tlsrpt") raise ValidationError("Message is tlsrpt and Service Type is not tlsrpt")
# Inferred requirement from both RFC 8460 and RFC 6376 # Inferred requirement from both RFC 8460 and RFC 6376
if not self.tlsrpt and self.seqtlsrpt: if not self.tlsrpt and self.seqtlsrpt:
raise ValidationError("Message is not tlsrpt and Service Type is tlsrpt") raise ValidationError("Message is not tlsrpt and Service Type is tlsrpt")
@@ -744,24 +740,43 @@ class DomainSigner(object):
if self.debug_content: if self.debug_content:
self.logger.debug("signed for %s: %r" % (sig_header[0], h.hashed())) self.logger.debug("signed for %s: %r" % (sig_header[0], h.hashed()))
signature = base64.b64decode(re.sub(br"\s+", b"", sig[b'b'])) signature = base64.b64decode(re.sub(br"\s+", b"", sig[b'b']))
if ktag == b'rsa': if self.ktag == b'rsa':
try: try:
res = RSASSA_PKCS1_v1_5_verify(h, signature, pk) res = RSASSA_PKCS1_v1_5_verify(h, signature, self.pk)
self.logger.debug("%s valid: %s" % (sig_header[0], res)) self.logger.debug("%s valid: %s" % (sig_header[0], res))
if res and self.keysize < self.minkey: if res and self.keysize < self.minkey:
raise KeyFormatError("public key too small: %d" % self.keysize) raise KeyFormatError("public key too small: %d" % self.keysize)
return res return res
except (TypeError,DigestTooLargeError) as e: except (TypeError,DigestTooLargeError) as e:
raise KeyFormatError("digest too large for modulus: %s"%e) raise KeyFormatError("digest too large for modulus: %s"%e)
elif ktag == b'ed25519': elif self.ktag == b'ed25519':
try: try:
pk.verify(h.digest(), signature) self.pk.verify(h.digest(), signature)
self.logger.debug("%s valid" % (sig_header[0])) self.logger.debug("%s valid" % (sig_header[0]))
return True return True
except (nacl.exceptions.BadSignatureError) as e: except (nacl.exceptions.BadSignatureError) as e:
return False return False
else: else:
raise UnknownKeyTypeError(ktag) raise UnknownKeyTypeError(self.ktag)
# Abstract helper method to verify a signed header
#: @param sig: List of (key, value) tuples containing tag=values of the header
#: @param include_headers: headers to validate b= signature against
#: @param sig_header: (header_name, header_value)
#: @param dnsfunc: interface to dns
def verify_sig(self, sig, include_headers, sig_header, dnsfunc):
name = sig[b's'] + b"._domainkey." + sig[b'd'] + b"."
try:
self.pk, self.keysize, self.ktag, self.seqtlsrpt = load_pk_from_dns(name,
dnsfunc, timeout=self.timeout)
except KeyFormatError as e:
self.logger.error("%s" % e)
return False
except binascii.Error as e:
self.logger.error('KeyFormatError: {0}'.format(e))
return False
return self.verify_sig_process(sig, include_headers, sig_header, dnsfunc)
#: Hold messages and options during DKIM signing and verification. #: Hold messages and options during DKIM signing and verification.
@@ -881,15 +896,9 @@ class DKIM(DomainSigner):
def present(self): def present(self):
return (len([(x,y) for x,y in self.headers if x.lower() == b"dkim-signature"]) > 0) return (len([(x,y) for x,y in self.headers if x.lower() == b"dkim-signature"]) > 0)
#: Verify a DKIM signature. def verify_headerprep(self, idx=0):
#: @type idx: int """Non-DNS verify parts to minimize asyncio code duplication."""
#: @param idx: which signature to verify. The first (topmost) signature is 0.
#: @type dnsfunc: callable
#: @param dnsfunc: an option function to lookup TXT resource records
#: for a DNS domain. The default uses dnspython or pydns.
#: @return: True if signature verifies or False otherwise
#: @raise DKIMException: when the message, signature, or key are badly formed
def verify(self,idx=0,dnsfunc=get_txt):
sigheaders = [(x,y) for x,y in self.headers if x.lower() == b"dkim-signature"] sigheaders = [(x,y) for x,y in self.headers if x.lower() == b"dkim-signature"]
if len(sigheaders) <= idx: if len(sigheaders) <= idx:
return False return False
@@ -909,7 +918,18 @@ class DKIM(DomainSigner):
include_headers = [x.lower() for x in re.split(br"\s*:\s*", sig[b'h'])] include_headers = [x.lower() for x in re.split(br"\s*:\s*", sig[b'h'])]
self.include_headers = tuple(include_headers) self.include_headers = tuple(include_headers)
return sig, include_headers, sigheaders
#: Verify a DKIM signature.
#: @type idx: int
#: @param idx: which signature to verify. The first (topmost) signature is 0.
#: @type dnsfunc: callable
#: @param dnsfunc: an option function to lookup TXT resource records
#: for a DNS domain. The default uses dnspython or pydns.
#: @return: True if signature verifies or False otherwise
#: @raise DKIMException: when the message, signature, or key are badly formed
def verify(self,idx=0,dnsfunc=get_txt):
sig, include_headers, sigheaders = self.verify_headerprep(idx=0)
return self.verify_sig(sig, include_headers, sigheaders[idx], dnsfunc) return self.verify_sig(sig, include_headers, sigheaders[idx], dnsfunc)
@@ -1307,7 +1327,8 @@ def sign(message, selector, domain, privkey, identity=None,
return d.sign(selector, domain, privkey, identity=identity, canonicalize=canonicalize, include_headers=include_headers, length=length) return d.sign(selector, domain, privkey, identity=identity, canonicalize=canonicalize, include_headers=include_headers, length=length)
def verify(message, logger=None, dnsfunc=get_txt, minkey=1024, timeout=5, tlsrpt=False): def verify(message, logger=None, dnsfunc=get_txt, minkey=1024,
timeout=5, tlsrpt=False):
"""Verify the first (topmost) DKIM signature on an RFC822 formatted message. """Verify the first (topmost) DKIM signature on an RFC822 formatted message.
@param message: an RFC822 formatted message (with either \\n or \\r\\n line endings) @param message: an RFC822 formatted message (with either \\n or \\r\\n line endings)
@param logger: a logger to which debug info will be written (default None) @param logger: a logger to which debug info will be written (default None)
@@ -1326,6 +1347,18 @@ def verify(message, logger=None, dnsfunc=get_txt, minkey=1024, timeout=5, tlsrpt
logger.error("%s" % x) logger.error("%s" % x)
return False return False
# aiodns requires Python 3.5+, so no async before that
if sys.version_info >= (3, 5):
try:
import aiodns
from dkim.asyncsupport import verify_async
dkim_verify_async = verify_async
except ImportError:
# If aiodns is not installed, then async verification is not available
pass
# For consistency with ARC # For consistency with ARC
dkim_sign = sign dkim_sign = sign
dkim_verify = verify dkim_verify = verify
+123
View File
@@ -0,0 +1,123 @@
# This software is provided 'as-is', without any express or implied
# warranty. In no event will the author be held liable for any damages
# arising from the use of this software.
#
# Permission is granted to anyone to use this software for any purpose,
# including commercial applications, and to alter it and redistribute it
# freely, subject to the following restrictions:
#
# 1. The origin of this software must not be misrepresented; you must not
# claim that you wrote the original software. If you use this software
# in a product, an acknowledgment in the product documentation would be
# appreciated but is not required.
# 2. Altered source versions must be plainly marked as such, and must not be
# misrepresented as being the original software.
# 3. This notice may not be removed or altered from any source distribution.
#
# Copyright (c) 2008 Greg Hewgill http://hewgill.com
#
# This has been modified from the original software.
# Copyright (c) 2011 William Grant <me@williamgrant.id.au>
#
# This has been modified from the original software.
# Copyright (c) 2016 Google, Inc.
# Contact: Brandon Long <blong@google.com>
#
# This has been modified from the original software.
# Copyright (c) 2016, 2017, 2018, 2019 Scott Kitterman <scott@kitterman.com>
#
# This has been modified from the original software.
# Copyright (c) 2017 Valimail Inc
# Contact: Gene Shuman <gene@valimail.com>
import asyncio
import aiodns
import base64
import dkim
import re
__all__ = [
'get_txt_async',
'load_pk_from_dns_async',
'verify_async'
]
async def get_txt_async(name, timeout=5):
"""Return a TXT record associated with a DNS name in an asnyc loop. For
DKIM we can assume there is only one."""
# Note: This will use the existing loop or create one if needed
loop = asyncio.get_event_loop()
resolver = aiodns.DNSResolver(loop=loop, timeout=timeout)
async def query(name, qtype):
return await resolver.query(name, qtype)
#q = query(name, 'TXT')
try:
result = await query(name, 'TXT')
except aiodns.error.DNSError:
result = None
if result:
return result[0].text
else:
return None
async def load_pk_from_dns_async(name, dnsfunc, timeout=5):
s = await dnsfunc(name, timeout=timeout)
pk, keysize, ktag, seqtlsrpt = dkim.evaluate_pk(name, s)
return pk, keysize, ktag, seqtlsrpt
class DKIM(dkim.DKIM):
#: Sign an RFC822 message and return the DKIM-Signature header line.
#:
#: Identical to dkim.DKIM, except uses aiodns and can be awaited in an
#: ascyncio context. See dkim.DKIM for details.
# Abstract helper method to verify a signed header
#: @param sig: List of (key, value) tuples containing tag=values of the header
#: @param include_headers: headers to validate b= signature against
#: @param sig_header: (header_name, header_value)
#: @param dnsfunc: interface to dns
async def verify_sig(self, sig, include_headers, sig_header, dnsfunc):
name = sig[b's'] + b"._domainkey." + sig[b'd'] + b"."
try:
self.pk, self.keysize, self.ktag, self.seqtlsrpt = await load_pk_from_dns_async(name,
dnsfunc, timeout=self.timeout)
except dkim.KeyFormatError as e:
self.logger.error("%s" % e)
return False
return self.verify_sig_process(sig, include_headers, sig_header, dnsfunc)
async def verify(self,idx=0,dnsfunc=get_txt_async):
sig, include_headers, sigheaders = self.verify_headerprep(idx=0)
return await self.verify_sig(sig, include_headers, sigheaders[idx], dnsfunc)
async def verify_async(message, logger=None, dnsfunc=None, minkey=1024,
timeout=5, tlsrpt=False):
"""Verify the first (topmost) DKIM signature on an RFC822 formatted message in an asyncio contxt.
@param message: an RFC822 formatted message (with either \\n or \\r\\n line endings)
@param logger: a logger to which debug info will be written (default None)
@param timeout: number of seconds for DNS lookup timeout (default = 5)
@param tlsrpt: message is an RFC 8460 TLS report (default False)
False: Not a tlsrpt, True: Is a tlsrpt, 'strict': tlsrpt, invalid if
service type is missing. For signing, if True, length is never used.
@return: True if signature verifies or False otherwise
"""
# type: (bytes, any, function, int) -> bool
# Note: This will use the existing loop or create one if needed
loop = asyncio.get_event_loop()
if not dnsfunc:
dnsfunc=get_txt_async
d = DKIM(message,logger=logger,minkey=minkey,timeout=timeout,tlsrpt=tlsrpt)
try:
return await d.verify(dnsfunc=dnsfunc)
except dkim.DKIMException as x:
if logger is not None:
logger.error("%s" % x)
return False
+48
View File
@@ -0,0 +1,48 @@
#!/usr/bin/env python
# This software is provided 'as-is', without any express or implied
# warranty. In no event will the author be held liable for any damages
# arising from the use of this software.
#
# Permission is granted to anyone to use this software for any purpose,
# including commercial applications, and to alter it and redistribute it
# freely, subject to the following restrictions:
#
# 1. The origin of this software must not be misrepresented; you must not
# claim that you wrote the original software. If you use this software
# in a product, an acknowledgment in the product documentation would be
# appreciated but is not required.
# 2. Altered source versions must be plainly marked as such, and must not be
# misrepresented as being the original software.
# 3. This notice may not be removed or altered from any source distribution.
#
# Copyright (c) 2008 Greg Hewgill http://hewgill.com
#
# This has been modified from the original software.
# Copyright (c) 2011 William Grant <me@williamgrant.id.au>
from __future__ import print_function
import sys
import asyncio
import dkim
if sys.version_info[0] >= 3:
# Make sys.stdin a binary stream.
sys.stdin = sys.stdin.detach()
message = sys.stdin.read()
async def main():
res = await dkim.verify_async(message)
return res
if __name__ == "__main__":
res = asyncio.run(main())
if not res:
print("signature verification failed")
sys.exit(1)
print("signature ok")
+2 -1
View File
@@ -86,7 +86,8 @@ verification.""",
'pynacl', 'pynacl',
], ],
'ed25519': ['pynacl'], 'ed25519': ['pynacl'],
'ARC': ['authres'] 'ARC': ['authres'],
'asyncio': ['aiodns']
}, },
**kw **kw
) )