| 1 | dkimpy - DKIM (DomainKeys Identified Mail) |
| 2 | https://launchpad.net/dkimpy/ |
| 3 | |
| 4 | Friendly fork of: |
| 5 | http://hewgill.com/pydkim/ |
| 6 | |
| 7 | # INTRODUCTION |
| 8 | |
| 9 | dkimpy is a library that implements DKIM (DomainKeys Identified Mail) email |
| 10 | signing and verification. Basic DKIM requirements are defined in RFC 6376: |
| 11 | |
| 12 | https://tools.ietf.org/html/rfc6376 |
| 13 | |
| 14 | # VERSION |
| 15 | |
| 16 | This is dkimpy 1.1.8. |
| 17 | |
| 18 | # REQUIREMENTS |
| 19 | |
| 20 | Dependencies will be automatically included for normal DKIM usage. The |
| 21 | extras_requires feature 'ed25519' will add the dependencies needed for signing |
| 22 | and verifying using the new DCRUP ed25519-sha256 algorithm. The |
| 23 | extras_requires feature 'ARC' will add the extra dependencies needed for ARC. |
| 24 | Similarly, extras_requires feature 'asyncio' will add the extra dependencies |
| 25 | needed for asyncio. |
| 26 | |
| 27 | - Python 3.x >= 3.5. Recent versions have not been tested on python3 < 3.4, |
| 28 | but may still work on earlier python3 versions. |
| 29 | - dnspython or py3dns. dnspython is preferred if both are present and |
| 30 | installed to satisfy the DNS module requirement if neither are installed. |
| 31 | - authres. Needed for ARC. |
| 32 | - PyNaCl. Needed for use of ed25519 capability. |
| 33 | - aiodns. Needed for asycnio (Requires python3.5 or later) |
| 34 | |
| 35 | # INSTALLATION |
| 36 | |
| 37 | This package includes a scripts and man pages. For those to be installed when |
| 38 | installing using setup.py, the following incantation is required because |
| 39 | setuptools developers decided not being able to do this by default is a |
| 40 | feature: |
| 41 | |
| 42 | ```python3 setup.py install --single-version-externally-managed --record=/dev/null``` |
| 43 | |
| 44 | # DOCUMENTATION |
| 45 | |
| 46 | An online version of the package documentation for the most recent release can |
| 47 | be found at: |
| 48 | |
| 49 | https://pymilter.org/pydkim/ |
| 50 | |
| 51 | # TESTING |
| 52 | |
| 53 | To run dkimpy's test suite: |
| 54 | |
| 55 | ```PYTHONPATH=. python3 dkim``` |
| 56 | |
| 57 | or |
| 58 | |
| 59 | ```python3 test.py``` |
| 60 | |
| 61 | or |
| 62 | |
| 63 | ```PYTHONPATH=. python3 -m unittest dkim.tests.test_suite``` |
| 64 | |
| 65 | |
| 66 | Alternatively, if you have testrepository installed: |
| 67 | |
| 68 | ```testr init``` |
| 69 | |
| 70 | ```testr run``` |
| 71 | |
| 72 | You should install all optional dependencies required for the test suite, e.g. |
| 73 | by creating a virtualenv and using: |
| 74 | |
| 75 | ```pip install -e '.[testing]'``` |
| 76 | |
| 77 | The included ARC tests are very limited. The primary testing method for ARC |
| 78 | is using the ARC test suite: https://github.com/ValiMail/arc_test_suite |
| 79 | |
| 80 | As of 0.6.0, all tests pass for both python2.7 and python3. The test suite |
| 81 | ships with test runners for dkimpy. After downloading the test suite, you |
| 82 | can run the signing and validation tests like this: |
| 83 | |
| 84 | ```python3 ./testarc.py sign runners/arcsigntest.py``` |
| 85 | ```python3 ./testarc.py validate runners/arcverifytest.py``` |
| 86 | |
| 87 | As ov version 1.1.0, python2.7 is no longer supported. |
| 88 | |
| 89 | # USAGE |
| 90 | |
| 91 | The dkimpy library offers one module called dkim. The sign() function takes an |
| 92 | RFC822 formatted message, along with some signing options, and returns a |
| 93 | DKIM-Signature header line that can be prepended to the message. The verify() |
| 94 | function takes an RFC822 formatted message, and returns True or False depending |
| 95 | on whether the signature verifies correctly. There is also a DKIM class which |
| 96 | can be used to perform these functions in a more modern way. |
| 97 | |
| 98 | In version 0.9.0, the default set of header fields that are oversigned was |
| 99 | changed from 'from', 'subject', 'date' to 'from' to reduce fragility of |
| 100 | signatures. To restore the previous behavior, you can add them back after |
| 101 | instantiating your DKIM class using the add_frozen function as shown in the |
| 102 | following example: |
| 103 | |
| 104 | ```python |
| 105 | >>> dkim = DKIM() |
| 106 | >>> dkim.add_frozen((b'date',b'subject')) |
| 107 | >>> [text(x) for x in sorted(dkim.frozen_sign)] |
| 108 | ['date', 'from', 'subject'] |
| 109 | ``` |
| 110 | |
| 111 | ## DKIM RSA MODERNIZATION (RFC 8301) |
| 112 | |
| 113 | RFC8301 updated DKIM requirements in two ways: |
| 114 | |
| 115 | 1. It set the minimum valid RSA key size to 1024 bits. |
| 116 | 2. It removed use of rsa-sha1. |
| 117 | |
| 118 | As of version 0.7, the dkimpy defaults largely support these requirements. |
| 119 | |
| 120 | It is possible to override the minimum key size to a lower value, but this is |
| 121 | strongly discouraged. As of 2018, keys much smaller than the minimum are not |
| 122 | difficult to factor. |
| 123 | |
| 124 | The code for rsa-sha1 signing and verification is retained, but not used for |
| 125 | signing by default. Future releases will raise warnings and then errors when |
| 126 | verifying rsa-sha1 signatures. There are still some significant users of |
| 127 | rsa-sha1 signatures, so operationally it's premature to disable verification |
| 128 | of rsa-sha1. |
| 129 | |
| 130 | ## ED25519 (RFC 8463) SUPPORT |
| 131 | |
| 132 | As of version 0.7, experimental signing and verifying of DKIM Ed25519 |
| 133 | signatures is supported as described in draft-ietf-dcrup-dkim-crypto: |
| 134 | |
| 135 | https://datatracker.ietf.org/doc/draft-ietf-dcrup-dkim-crypto/ |
| 136 | |
| 137 | The RFC that documents ed25519 DKIM signatures, RFC 8463, has been released |
| 138 | and dkimpy 0.7 and later are aligned to its requirements. As of 0.8, ed25519 |
| 139 | need not be considered experimental. The dkimpy implementation has |
| 140 | successfully interoperated with three other implementations and the technical |
| 141 | parameters for ed25519-sha256 are defined and stable. |
| 142 | |
| 143 | To install from pypi with the required optional depenencies, use the ed25519 |
| 144 | option: |
| 145 | |
| 146 | ```pip install -e '.[ed25519]'``` |
| 147 | |
| 148 | ## DKIM SCRIPTS |
| 149 | |
| 150 | Three helper programs are also supplied: dknewkey, dkimsign and |
| 151 | dkimverify |
| 152 | |
| 153 | dknewkey is s script that produces private and public key pairs suitable |
| 154 | for use with DKIM. Note that the private key file format used for ed25519 is |
| 155 | not standardized (there is no standard) and is unique to dkimpy. Creation of |
| 156 | keys should be done in a secure environment. If an unauthorized entity gains |
| 157 | access to current private keys they can generate signed email that will pass |
| 158 | DKIM checkes and will be difficult to repudiate. |
| 159 | |
| 160 | dkimsign is a filter that reads an RFC822 message on standard input, and |
| 161 | writes the same message on standard output with a DKIM-Signature line |
| 162 | prepended. The signing options are specified on the command line: |
| 163 | |
| 164 | dkimsign selector domain privatekeyfile [identity] |
| 165 | |
| 166 | The identity is optional and defaults to "@domain". |
| 167 | |
| 168 | dkimverify reads an RFC822 message on standard input, and returns with exit |
| 169 | code 0 if the signature verifies successfully. Otherwise, it returns with exit |
| 170 | code 1. |
| 171 | |
| 172 | ## ARC (Authenticated Receive Chain) |
| 173 | |
| 174 | As of version 0.6.0, dkimpy provides experimental support for ARC (Authenticated |
| 175 | Received Chain). See RFC 8617 for the current version of ARC: |
| 176 | |
| 177 | https://tools.ietf.org/html/rfc8617 |
| 178 | |
| 179 | In addition to arcsign and arcverify, the dkim module now provides |
| 180 | arc_sign and arc_verify functions as well as an ARC class. |
| 181 | |
| 182 | If an invalid authentication results header field is included in the set for |
| 183 | ARC, it is ignored and no error is raised. |
| 184 | |
| 185 | Both DKIM ed25519 and ARC are now considered stable (no longer experimantal). |
| 186 | |
| 187 | ## ASYNC SUPPORT |
| 188 | |
| 189 | As of version 1.0, an alternative to dkim.verify for use in an async |
| 190 | environment is provied. It requires aiodns, https://pypi.org/project/aiodns/. |
| 191 | Here is a simple example of dkim.verify_async usage: |
| 192 | |
| 193 | ```python |
| 194 | >>> sys.stdin = sys.stdin.detach() |
| 195 | >>> |
| 196 | >>> async def main(): |
| 197 | >>> res = await dkim.verify_async(message) |
| 198 | >>> return res |
| 199 | >>> |
| 200 | >>> if __name__ == "__main__": |
| 201 | >>> res = asyncio.run(main()) |
| 202 | ``` |
| 203 | |
| 204 | This feature requires python3.5 or newer. |
| 205 | |
| 206 | If aiodns is available, the async functions will be used. To avoide async |
| 207 | when aiodns is availale, set dkim.USE_ASYNC = False. |
| 208 | |
| 209 | ## TLSRPT (TLS Report) |
| 210 | |
| 211 | As of version 1.0, the RFC 8460 tlsrpt service type is supported: |
| 212 | |
| 213 | https://tools.ietf.org/html/rfc8460 |
| 214 | |
| 215 | A non-tlsrpt signed with a key record with s=tlsrpt won't verify. Since the |
| 216 | service type (s=) is optional in the DKIM public key record, it is not |
| 217 | required by RFC 8460. When checking for a tlsrpt signature, set the tlsrpt= |
| 218 | flag when verifying the signature: |
| 219 | |
| 220 | ```python |
| 221 | >>> res = dkim.verify(smessage, tlsrpt='strict') |
| 222 | ``` |
| 223 | |
| 224 | If tlsrpt='strict', only public key records with s=tlsrpt will be considered |
| 225 | valid. If set to tlsrpt=True, the service type is not required, but other |
| 226 | RFC 8460 requirements are applied. |
| 227 | |
| 228 | # LIMITATIONS |
| 229 | |
| 230 | Dkimpy will correctly sign/verify messages with ASCII or UTF-8 content. |
| 231 | Messages that contain other types of content will not verify correctly. It |
| 232 | does not yet implement RFC 8616, Email Authentication for Internationalized |
| 233 | Mail. |
| 234 | |
| 235 | # FEEDBACK |
| 236 | |
| 237 | Bug reports may be submitted to the bug tracker for the dkimpy project on |
| 238 | launchpad. |