0

Hi I am trying to use openssl command to test my server code written in python using aiosmtpd library.

Here is the server side code I have written:

import datetime
import sys
import ssl
import aiosmtpd.controller

class SMTPServer:
      
        async def handle_DATA(self, server, session, envelope):
            # some printing of the response 
            return "250 OK"
        
if __name__ == "__main__":
    handler = SMTPServer()
    context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
    context.load_cert_chain('cert.pem', 'key.pem') 
    controller = aiosmtpd.controller.Controller(handler, hostname="localhost",port=587,ssl_context=context)
    controller.start()
    input('Running smtp Server over TLS on port 587. \n')
    controller.stop()

When I try to execute above code with command openssl s_client -debug -starttls smtp -crlf -connect localhost:587 I am getting the below error

$ openssl s_client -debug -starttls smtp -crlf -connect localhost:587
CONNECTED(00000003)
read from 0x55feaf0ba7c0 [0x55feaf0be780] (4096 bytes => 0 (0x0))
write to 0x55feaf0ba7c0 [0x55feaf0bf790] (23 bytes => 23 (0x17))
0000 - 45 48 4c 4f 20 6d 61 69-6c 2e 65 78 61 6d 70 6c   EHLO mail.exampl
0010 - 65 2e 63 6f 6d 0d 0a                              e.com..
read from 0x55feaf0ba7c0 [0x55feaf0be780] (4096 bytes => 0 (0x0))
Didn't find STARTTLS in server response, trying anyway...
write to 0x55feaf0ba7c0 [0x7ffd8edd5fb0] (10 bytes => -1 (0xFFFFFFFFFFFFFFFF))
read from 0x55feaf0ba7c0 [0x55feaf0b1f80] (8192 bytes => 0 (0x0))
write to 0x55feaf0ba7c0 [0x55feaf0ccf00] (283 bytes => -1 (0xFFFFFFFFFFFFFFFF))
write:errno=32
---
no peer certificate available
---
No client certificate CA names sent
---
SSL handshake has read 0 bytes and written 23 bytes
Verification: OK
---
New, (NONE), Cipher is (NONE)
Secure Renegotiation IS NOT supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
Early data was not sent
Verify return code: 0 (ok)
---
read from 0x55feaf0ba7c0 [0x55feaf0b1f80] (8192 bytes => 0 (0x0))

could anyone please support how to resolve this error? If I use openssl command without adding -starttls like "openssl s_client -debug -connect localhost:587" then the above server code is working fine. But I want to make it work using -starttls along with openssl command.

phuzi
  • 12,078
  • 3
  • 26
  • 50
Anna
  • 119
  • 9
  • 1
    Does this answer your question? [How do I properly support STARTTLS with aiosmtpd?](https://stackoverflow.com/questions/45447491/how-do-i-properly-support-starttls-with-aiosmtpd) – Joe Jul 11 '22 at 12:02
  • Thanks @Joe I checked that link earlier but I thing I missed this step: class ControllerTls(Controller): def factory(self): return SMTP(self.handler, require_starttls=True, tls_context=context) By adding the above code It worked. – Anna Jul 11 '22 at 12:21

0 Answers0