I'm trying to authenticate as client on a SMTP server, that requires certificate.
The problem is i'm only given a self-sigend certificate (.pem)
Note: I'm trying to implement the solution in tls 1.2
The code i expected to use is below, but since they gave me only one self-signed certificate for the authentication is pretty much useless(?)
Do you have any suggestion?
def create_smtp_server(smtp_server, port, username, password, certificate):
"""
Create a SMTP server instance to send the Email
:param: smtp_server:
:param: port:
:param: username:
:param: password:
:return: SMTP server instance
"""
try:
client_cert = certificate
server_cert = certificate
client_key = certificate
server = smtplib.SMTP(smtp_server, port)
server.connect(smtp_server, port)
server.ehlo()
context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH, cafile=server_cert)
context.load_cert_chain(certfile=client_cert, keyfile=client_key)
server.starttls(context=context)
server.login(username, password)
log(log_type="SERVER_CONNECTED", msg="SMTP server created", detail=None)
except Exception as e:
log(log_type="SERVER_CONNECTION_ERROR", msg="Can't create SMTP server", detail=e)
server = None
return server
I also tried with the code blow but did not work
...
context = ssl.create_default_context(certificate)
server.starttls(context=context)
...
question from:
https://stackoverflow.com/questions/65935962/smtp-authentication-with-self-signed-pem-certificate-python 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…