-
-
Notifications
You must be signed in to change notification settings - Fork 453
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support s/mime encryption #1946
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2566,6 +2566,12 @@ class Mailbox(Base, ModelMixin): | |
sa.Boolean, default=False, nullable=False, server_default="0" | ||
) | ||
|
||
# smime | ||
smime_public_key = sa.Column(sa.Text, nullable=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you move this into a different model like |
||
disable_smime = sa.Column( | ||
sa.Boolean, default=True, nullable=False, server_default="0" | ||
) | ||
|
||
# incremented when a check is failed on the mailbox | ||
# alert when the number exceeds a threshold | ||
# used in sanity_check() | ||
|
@@ -2588,6 +2594,12 @@ def pgp_enabled(self) -> bool: | |
|
||
return False | ||
|
||
def smime_enabled(self) -> bool: | ||
if self.smime_public_key and not self.disable_smime: | ||
return True | ||
|
||
return False | ||
|
||
def nb_alias(self): | ||
return ( | ||
AliasMailbox.filter_by(mailbox_id=self.id).count() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,8 +32,10 @@ | |
""" | ||
import argparse | ||
import email | ||
import smail | ||
import time | ||
import uuid | ||
from asn1crypto import pem, x509 | ||
from email import encoders | ||
from email.encoders import encode_noop | ||
from email.message import Message | ||
|
@@ -535,6 +537,21 @@ def prepare_pgp_message( | |
return msg | ||
|
||
|
||
def prepare_smime_message(orig_msg: Message, public_key: str) -> Message: | ||
# clone orig message to avoid modifying it | ||
clone_msg = copy(orig_msg) | ||
|
||
# create certificate object using public key | ||
_, _, der_bytes = pem.unarmor(public_key.encode()) | ||
cert = x509.Certificate.load(der_bytes) | ||
|
||
# encrypt the message | ||
clone_msg = smail.encrypt_message(clone_msg, [cert]) | ||
|
||
# return the message | ||
return clone_msg | ||
|
||
|
||
def sign_msg(msg: Message) -> Message: | ||
container = MIMEMultipart( | ||
"signed", protocol="application/pgp-signature", micalg="pgp-sha256" | ||
|
@@ -908,6 +925,26 @@ def forward_email_to_mailbox( | |
f"""PGP encryption fails with {mailbox.email}'s PGP key""", | ||
) | ||
|
||
# create SMIME email if needed | ||
if mailbox.smime_enabled() and user.is_premium(): | ||
LOG.d("Encrypt message using S/MIME for mailbox %s", mailbox) | ||
|
||
try: | ||
msg = prepare_smime_message(msg, mailbox.smime_public_key) | ||
except Exception as exceptasdf: | ||
LOG.w( | ||
"Cannot S/MIME encrypt message %s -> %s. %s %s", | ||
contact, | ||
alias, | ||
mailbox, | ||
user, | ||
) | ||
LOG.w(exceptasdf) | ||
Comment on lines
+934
to
+942
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, sorry; I can clean up this variable name and logging There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
msg = add_header( | ||
msg, | ||
f"""S/MIME encryption fails with {mailbox.email}'s S/MIME key""", | ||
) | ||
|
||
# add custom header | ||
add_or_replace_header(msg, headers.SL_DIRECTION, "Forward") | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"""empty message | ||
|
||
Revision ID: fb0ab73c1825 | ||
Revises: 4bc54632d9aa | ||
Create Date: 2023-11-15 21:50:40.424160 | ||
|
||
""" | ||
import sqlalchemy_utils | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = 'fb0ab73c1825' | ||
down_revision = '4bc54632d9aa' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.add_column('mailbox', sa.Column('disable_smime', sa.Boolean(), server_default='0', nullable=False)) | ||
op.add_column('mailbox', sa.Column('smime_public_key', sa.Text(), nullable=True)) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_column('mailbox', 'smime_public_key') | ||
op.drop_column('mailbox', 'disable_smime') | ||
# ### end Alembic commands ### |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you: