Skip to main content

Simple S/MIME e-mails with Python3

Project description

This library makes it simple to create S/MIME messages in Python. It supports signing (using RSA keys), encryption (using a public RSA key, in AES128-CBC, AES192-CBC or AES256-CBC modes) and the combination of both - where the message is first signed and the result is then encrypted (“enveloped”).

The foundation of python-smail is the oscrypto library which provides access to the C-bindings of OpenSSL and implements many high and low level functionality. Additionally asn1crypto is used which is “A fast, pure Python library for parsing and serializing ASN.1 structures.”.

Requirements

  • Python 3.5+

  • asn1crypto

  • oscrypto

Example

Encryption

The code below loads Bob’s public key in PEM format and uses it to encrypt the e-mail in S/MIME format:

import os
from email.mime.text import MIMEText
from email.utils import formatdate
from smail import encrypt_message

message = MIMEText("This a plain text body!")
message['Date'] = formatdate(localtime=True)
message['From'] = "AliceRSA@example.com"
message['To'] = "BobRSA@example.com"
message['Subject'] = "Text Message - Encrypted"

cert = os.path.join('tests', 'testdata', 'BobRSASignByCarl.pem')

encrypted_message = encrypt_message(message, [cert])
print(encrypted_message.as_string())

Output:

MIME-Version: 1.0
Content-Type: application/pkcs7-mime; smime-type=enveloped-data; name=smime.p7m
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=smime.p7m
Date: Tue, 17 Mar 2020 19:58:17 +0100
From: AliceRSA@example.com
To: BobRSA@example.com
Subject: Plain Text Message

MIIBhwYJKoZIhvcNAQcDoIIBeDCCAXQCAQAxgb4wgbsCAQAwJjASMRAwDgYDVQQD
EwdDYXJsUlNBAhBGNGvHgABWvBHTbi7NXXHQMAsGCSqGSIb3DQEBAQSBgFmMsY5d
H446eMYRwzVUREH2+Nv9VyflrA9rJlU/4yKqGGEnzt+YRPQaU+KoZ8iURaMB/GLE
ZupCnJ79VAjs+RX6kyMtKMXvcsRkzR8GGGPLHNEbqqFmDY5VQrC/jA66w6/xPjdb
hzVvf6SVWc4Aefv8xdQOMF1relsVahjvjFJrMIGtBgkqhkiG9w0BBwEwHQYJYIZI
AWUDBAEqBBArK3YkdLMydTK8DyyQvUj+gIGAaC/h3NVeGcVpdkDI9iP4OxqC4chk
BHeK+KpYJJYlelSt7D5LlRuRHjnRy3laU3bi1bxm0vxefx2ihf5rZRblpLWwnuft
duFKvXKUX0Es6Q78LpY8Ng3IxMGYNMWiCTMyqDGgC32JRI3H8twsG0/NIoZxKDXQ
1k7QedeP0a+JhVc=

Signing

The code below loads Alice’s private and public key in PEM format and uses it to sign the e-mail in S/MIME format:

import os
from email.mime.text import MIMEText
from email.utils import formatdate
from smail import sign_message

message = MIMEText("This a plain text body!")
message['Date'] = formatdate(localtime=True)
message['From'] = "AliceRSA@example.com"
message['To'] = "BobRSA@example.com"
message['Subject'] = "Text Message - Signed"

key_signer = os.path.join('tests', 'testdata', 'AlicePrivRSASign.pem')
cert_signer = os.path.join('tests', 'testdata', 'AliceRSASignByCarl.pem')

signed_message = sign_message(message, key_signer, cert_signer)
print(signed_message.as_string())

Output:

MIME-Version: 1.0
Content-Type: multipart/signed; protocol="application/x-pkcs7-signature"; micalg="sha-256"; boundary="----39D1127DF0061CD9BB50644B14CCEF86"
Date: Tue, 17 Mar 2020 20:02:11 +0100
From: AliceRSA@example.com
To: BobRSA@example.com
Subject: Plain Text Message

This is an S/MIME signed message

------39D1127DF0061CD9BB50644B14CCEF86
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

This a plain text body!
------39D1127DF0061CD9BB50644B14CCEF86
Content-Type: application/x-pkcs7-signature; name="smime.p7s"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="smime.p7s"

MIIEIwYJKoZIhvcNAQcCoIIEFDCCBBACAQExDzANBglghkgBZQMEAgEFADALBgkq
hkiG9w0BBwGgggIwMIICLDCCAZWgAwIBAgIQRjRrx4AAVrwR024uxBCzsDANBgkq
hkiG9w0BAQUFADASMRAwDgYDVQQDEwdDYXJsUlNBMB4XDTk5MDkxOTAxMDg0N1oX
DTM5MTIzMTIzNTk1OVowEzERMA8GA1UEAxMIQWxpY2VSU0EwgZ8wDQYJKoZIhvcN
A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7bmiLlDs6qeofmyRAZgUBdDKySm+yjZc
V1VLPuFuL9+BDcXarthOSnn4wbdRBhceRu7w8OnyoTtrwP58c0MiQVtyBQq1FuPZ
WBKduWYjIg==

------39D1127DF0061CD9BB50644B14CCEF86--

Sign and Encrypt

The code below loads Alice’s private and public key and also Bob’s public key in PEM format and uses it to sign and encrypt the e-mail (from Alice to Bob) in S/MIME format:

import os
from email.mime.text import MIMEText
from email.utils import formatdate
from smail import sign_and_encrypt_message

message = MIMEText("This a plain text body!")
message['Date'] = formatdate(localtime=True)
message['From'] = "AliceRSA@example.com"
message['To'] = "BobRSA@example.com"
message['Subject'] = "Text Message - Signed and Encrypted"

key_signer = os.path.join('tests', 'testdata', 'AlicePrivRSASign.pem')
cert_signer = os.path.join('tests', 'testdata', 'AliceRSASignByCarl.pem')

cert = os.path.join('tests', 'testdata', 'BobRSASignByCarl.pem')

signed_encrypted_message = sign_and_encrypt_message(message, key_signer, cert_signer, [cert])
print(signed_encrypted_message.as_string())

Output:

MIME-Version: 1.0
Content-Type: application/pkcs7-mime; smime-type=enveloped-data; name=smime.p7m
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=smime.p7m
Date: Tue, 17 Mar 2020 20:05:34 +0100
From: AliceRSA@example.com
To: BobRSA@example.com
Subject: Text Message - Signed and Encrypted

MIIIuQYJKoZIhvcNAQcDoIIIqjCCCKYCAQAxgb4wgbsCAQAwJjASMRAwDgYDVQQD
EwdDYXJsUlNBAhBGNGvHgABWvBHTbi7NXXHQMAsGCSqGSIb3DQEBAQSBgH5C7eTN
O6Yoqf/UCqMJw3Un+0ZV/Gw/LDbnrnnPCQmGx4kCMSvcvqQp3IJ1RBvvX0D9VkN1
g+5Xo+0i0nNXZ/62Be1hTMYxC9vkogq0Ec5x96X0KPs96CWJOUmGyHTt5IV/0TPN
b3mMiOCIUrMDGBMAxCxPRrHfgMoM0L483xhPMIIH3gYJKoZIhvcNAQcBMB0GCWCG
SAFlAwQBKgQQdY86v19IJTpgxF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6Kj8w2sH6WpKPBl+kuW6jXo5PlxDa
g2kJWBMrJ/5PA8s6uCwPGbRoCXpQIxCn1zaa/suNZ9JNJErxd5uLWYDcsiQlizpe
Py/nFWCfVHtxbGKKdPb56XrbD4VBdZcaz+/AVIxTCnOgGMg1b5w59ePkpbc6idD7
j7FI52vx5ArUH5U+38+xqI4s/Hfjqv7jIb0ZbLpenCyMMn+3pcWIMUqrsvNjv9uk
XxjfnFumKq7XyFM/DUGwS/22C889LpXl6EiB651pIpt6aZIMWuCiMBMASD+QKjW1
YXa+OID8K+0At6WIQSVYph5Pq4w8ldT9zR2TfLTOWUwFRm/aku0AjSraNAxaGiLO
kr+UdgYOpP4u6qAZwUHco1gmRRQQ8omNiJoQNOcKSvj6R38xzc/MrlQi1s5Tdoh7
nG76s6DJuzQQeKgYgZJbRP07jgbpZTsm5017jTnkSeQ8WnnM6eLR2HHLXJo1X4vo
e3FI2iig5N6ytDwcN2MGTzr0SuhUe+JEQys2z2A=

Acknowledgements

Python SMAIL is heavily inspired by python-smime and is actually a fork of that code base. All credits go to original Author(s).

License

This software is licensed under the Apache License 2.0. See the LICENSE file in the top distribution directory for the full license text.

Versioning

This software follows Semantic Versioning

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-smail-0.9.0.tar.gz (27.1 kB view details)

Uploaded Source

Built Distribution

python_smail-0.9.0-py2-none-any.whl (25.1 kB view details)

Uploaded Python 2

File details

Details for the file python-smail-0.9.0.tar.gz.

File metadata

  • Download URL: python-smail-0.9.0.tar.gz
  • Upload date:
  • Size: 27.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/2.7

File hashes

Hashes for python-smail-0.9.0.tar.gz
Algorithm Hash digest
SHA256 e0da2fea2189a8dece2ab1ea78a670ba2f1d025742ea118b7358e5a9ca2f052f
MD5 8576c8d21a322357c819e421d0778f88
BLAKE2b-256 62d2275e5eb345ba549c05fdfaacf62914ce5975c12f34a3f980c9e93dc98cb0

See more details on using hashes here.

File details

Details for the file python_smail-0.9.0-py2-none-any.whl.

File metadata

File hashes

Hashes for python_smail-0.9.0-py2-none-any.whl
Algorithm Hash digest
SHA256 b15e085efcf10813c37bb9fc0c1b0d6564a39b30b91c308cd443201942c718e5
MD5 ed9ddf98458851b9882c68a43a14a2a7
BLAKE2b-256 b04135e96b5e6b9473b246f9f8c18d614fccb6490341d80ffdf2ee8cde18cc8f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page