add more tests, refactor sign and encrypt

Signed-off-by: Florian Brandes <florian.brandes@posteo.de>
This commit is contained in:
2024-07-06 20:14:49 +02:00
parent cb5590b2b5
commit 81fa631d16
6 changed files with 145 additions and 11 deletions

View File

@@ -147,7 +147,7 @@ class Config:
class SMTPClient(SMTP):
"""Client part of library
This will connect to an upstream SMTP server to deliver the mal
This will connect to an upstream SMTP server to deliver the mail
"""
@@ -206,7 +206,7 @@ class SMTPClient(SMTP):
message (Message): message object
Returns:
bytes: The signed message including the From and To Header
bytes: The signed message
"""
with open(Path(self._config.smime_cert_private), "rb") as key_data:
key = load_pem_private_key(key_data.read(), password=None)
@@ -220,12 +220,20 @@ class SMTPClient(SMTP):
.add_signer(cert, key, hashes.SHA512(), rsa_padding=padding.PKCS1v15())
.sign(Encoding.SMIME, [pkcs7.PKCS7Options.DetachedSignature])
)
# Add correct headers
# new = b"From: " + self._config.sender.encode() + b"\r\n" + output
# new = b"To: " + ", ".join(self._config.recipients).encode() + b"\r\n" + new
# new = b"Subject: " + message.get("Subject", "").encode() + b"\r\n" + new
new = self._encrypt(output, message.get("Subject", ""))
return new
return output
def _encrypt_and_sign(self, message: Message) -> bytes:
"""Sign and encrypt the message
Args:
message (Message): message object
Returns:
bytes: The signed and encrypted message including the From and To Header
"""
signed = self._sign(message)
encrypt = self._encrypt(signed, message.get("Subject", ""))
return encrypt
async def _send_message(
self, message: bytes, sender: str, recipients: List[str]
@@ -275,7 +283,7 @@ class SMTPClient(SMTP):
f"'{message.get('Subject', '')}'"
)
if self._config.smime_cert:
message = self._sign(message)
message = self._encrypt_and_sign(message)
async with (
self._lock
): # TODO: consumer task from spool queue, reusing connections