bluetiger9 / SmtpClient-for-Qt

An SMTP Client writen in C++ for Qt. Allows applications to send emails (MIME with text, html, attachments, inline files, etc.) via SMTP. Supports SSL and SMTP authentication.
https://github.com/bluetiger9/SmtpClient-for-Qt/wiki
GNU Lesser General Public License v2.1
456 stars 227 forks source link

SMTP TLS Support #151

Closed britus closed 11 months ago

britus commented 11 months ago

Hi, I have problem with the SMTP TLS support. The client code stops after STARTTLS offer with self signed certificates and QSslConfiguration is set to "setPeerVerifyMode(QSslSocket::VerifyNone);" in onSmtpConnected() event. Whats wrong?

2023-11-07 21:29:40.653691+0100 mailserver-test[69225:5086432] [SmtpClient] State: ConnectingState 2023-11-07 21:29:40.653737+0100 mailserver-test[69225:5086432] SMTP: state changed: 1 2023-11-07 21:29:40.655169+0100 mailserver-test[69225:5086432] [Socket] State: QAbstractSocket::HostLookupState 2023-11-07 21:29:40.659096+0100 mailserver-test[69225:5086432] [Socket] State: QAbstractSocket::ConnectingState 2023-11-07 21:29:40.691350+0100 mailserver-test[69225:5086432] [Socket] State: QAbstractSocket::ConnectedState 2023-11-07 21:29:40.691400+0100 mailserver-test[69225:5086432] [SmtpClient] State: ConnectedState 2023-11-07 21:29:40.691417+0100 mailserver-test[69225:5086432] SMTP: state changed: 2 2023-11-07 21:29:40.762322+0100 mailserver-test[69225:5086432] [Socket] IN: "220 mail.eschrich.org\r\n" 2023-11-07 21:29:40.762390+0100 mailserver-test[69225:5086432] [SmtpClient] State: _EHLO_State 2023-11-07 21:29:40.762408+0100 mailserver-test[69225:5086432] SMTP: state changed: 50 2023-11-07 21:29:40.762 428+0100 mailserver-test[69225:5086432] [Socket] OUT: "EHLO mail.google.com" 2023-11-07 21:29:40.793489+0100 mailserver-test[69225:5086432] [Socket] IN: "250-eschrich.org\r\n" 2023-11-07 21:29:40.793550+0100 mailserver-test[69225:5086432] [Socket] IN: "250-SIZE 104857600\r\n" 2023-11-07 21:29:40.793562+0100 mailserver-test[69225:5086432] [Socket] IN: "250-VRFY\r\n" 2023-11-07 21:29:40.793572+0100 mailserver-test[69225:5086432] [Socket] IN: "250-ETRN\r\n" 2023-11-07 21:29:40.793580+0100 mailserver-test[69225:5086432] [Socket] IN: "250-STARTTLS\r\n" 2023-11-07 21:29:40.793589+0100 mailserver-test[69225:5086432] [Socket] IN: "250-ENHANCEDSTATUSCODES\r\n" 2023-11-07 21:29:40.793597+0100 mailserver-test[69225:5086432] [Socket] IN: "250-8BITMIME\r\n" 2023-11-07 21:29:40.793609+0100 mailserver-test[69225:5086432] [Socket] IN: "250-DSN\r\n" 2023-11-07 21:29:40.793618+0100 mailserver-test[69225:5086432] [Socket] IN: "250 SMTPUTF8\r\n" 2023-11-07 21:29:40.793631+0100 mailserver-test[69225:5086432] [S mtpClient] State: _TLS_State 2023-11-07 21:29:40.793644+0100 mailserver-test[69225:5086432] SMTP: state changed: 51 2023-11-07 21:29:40.793652+0100 mailserver-test[69225:5086432] [SmtpClient] State: _TLS_0_STARTTLS 2023-11-07 21:29:40.793660+0100 mailserver-test[69225:5086432] SMTP: state changed: 60 2023-11-07 21:29:40.793667+0100 mailserver-test[69225:5086432] [Socket] OUT: "STARTTLS" 2023-11-07 21:29:40.824197+0100 mailserver-test[69225:5086432] [Socket] IN: "220 2.0.0 Ready to start TLS\r\n" 2023-11-07 21:29:40.824250+0100 mailserver-test[69225:5086432] [SmtpClient] State: _TLS_1_ENCRYPT 2023-11-07 21:29:40.824269+0100 mailserver-test[69225:5086432] SMTP: state changed: 61 2023-11-07 21:29:40.942521+0100 mailserver-test[69225:5086432] [Socket] ERROR: QAbstractSocket::SocketError(13) 2023-11-07 21:29:40.942574+0100 mailserver-test[69225:5086432] SMTP: client error: 6 / 220 "220 2.0.0 Ready to start TLS\r\n" 2023-11-07 21:29:40.942608+0100 mailserver-test[69225:5086432] [Socket] State: QAbstractSocket::ClosingState 2023-11-07 21:29:40.942689+0100 mailserver-test[69225:5086432] [Socket] State: QAbstractSocket::UnconnectedState 2023-11-07 21:29:40.942703+0100 mailserver-test[69225:5086432] [SmtpClient] State: UnconnectedState 2023-11-07 21:29:40.942710+0100 mailserver-test[69225:5086432] SMTP: state changed: 0 2023-11-07 21:29:40.942717+0100 mailserver-test[69225:5086432] SMTP: client disconnected. 2023-11-07 21:29:40.942963+0100 mailserver-test[69225:5086432] SMTP: client destroyed.

====================== Server Log

Nov 7 21:36:00 mailsvr postfix/smtpd[30187]: initializing the server-side TLS engine Nov 7 21:36:00 mailsvr postfix/smtpd[30187]: connect from XxXxXxX Nov 7 21:36:00 mailsvr postfix/smtpd[30187]: setting up TLS connection from XxXxXxX Nov 7 21:36:00 mailsvr postfix/smtpd[30187]: XxXxXxX: TLS cipher list "aNULL:-aNULL:HIGH:MEDIUM:+RC4:@STRENGTH" Nov 7 21:36:00 mailsvr postfix/smtpd[30187]: SSL_accept:before SSL initialization Nov 7 21:36:00 mailsvr postfix/smtpd[30187]: SSL_accept:before SSL initialization Nov 7 21:36:00 mailsvr postfix/smtpd[30187]: SSL_accept:SSLv3/TLS read client hello Nov 7 21:36:00 mailsvr postfix/smtpd[30187]: SSL_accept:SSLv3/TLS write server hello Nov 7 21:36:00 mailsvr postfix/smtpd[30187]: SSL_accept:SSLv3/TLS write certificate Nov 7 21:36:00 mailsvr postfix/smtpd[30187]: SSL_accept:SSLv3/TLS write key exchange Nov 7 21:36:00 mailsvr postfix/smtpd[30187]: SSL_accept:SSLv3/TLS write server done Nov 7 21:36:00 mailsvr postfix/smtpd[30187]: SSL_accept:SSLv3/TLS write server done Nov 7 21:36:00 mailsvr postfix/smtpd[30187]: SSL_accept:SSLv3/TLS read client key exchange Nov 7 21:36:00 mailsvr postfix/smtpd[30187]: SSL_accept:SSLv3/TLS read change cipher spec Nov 7 21:36:00 mailsvr postfix/smtpd[30187]: SSL_accept:SSLv3/TLS read finished Nov 7 21:36:00 mailsvr postfix/smtpd[30187]: SSL_accept:SSLv3/TLS write change cipher spec Nov 7 21:36:00 mailsvr postfix/smtpd[30187]: SSL_accept:SSLv3/TLS write finished Nov 7 21:36:00 mailsvr postfix/smtpd[30187]: XxXxXxX: save session 87F8EF510A78ACA8B158602A3251DAC1C91105F59CF0A4F5D26AA9A9DCD78873&s=smtp&l=269488143 to smtpd cache Nov 7 21:36:00 mailsvr postfix/tlsmgr[28321]: put smtpd session id=87F8EF510A78ACA8B158602A3251DAC1C91105F59CF0A4F5D26AA9A9DCD78873&s=smtp&l=269488143 [data 133 bytes] Nov 7 21:36:00 mailsvr postfix/tlsmgr[28321]: write smtpd TLS cache entry 87F8EF510A78ACA8B158602A3251DAC1C91105F59CF0A4F5D26AA9A9DCD78873&s=smtp&l=269488143: time=1699389360 [data 133 bytes] Nov 7 21:36:00 mailsvr postfix/smtpd[30187]: Anonymous TLS connection established from XxXxXxX: TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits) Nov 7 21:36:00 mailsvr dovecot: auth: Debug: Loading modules from directory: /usr/lib/dovecot/modules/auth Nov 7 21:36:00 mailsvr dovecot: auth: Debug: Module loaded: /usr/lib/dovecot/modules/auth/lib20_auth_var_expand_crypt.so Nov 7 21:36:00 mailsvr dovecot: auth: Debug: Module loaded: /usr/lib/dovecot/modules/auth/libdriver_mysql.so Nov 7 21:36:00 mailsvr dovecot: auth: Debug: Read auth token secret from /var/run/dovecot//auth-token-secret.dat Nov 7 21:36:00 mailsvr dovecot: auth: Debug: auth client connected (pid=0) Nov 7 21:36:00 mailsvr postfix/smtpd[30187]: lost connection after STARTTLS from XxXxXxX Nov 7 21:36:00 mailsvr postfix/smtpd[30187]: disconnect from XxXxXxX ehlo=1 starttls=1 commands=2

attila-tokes commented 11 months ago

The error QAbstractSocket::SocketError(13) is a QAbstractSocket::SslHandshakeFailedError, so this means the SSL handshake fails.

How are you setting setPeerVerifyMode(QSslSocket::VerifyNone)? I think it should be set on the internal socket of the SmtpClient class.

britus commented 11 months ago

Ah ok, thank you for your response. I moved the call "setPeerVerifyMode(QSslSocket::VerifyNone)" to before connectToHost(...) and now it works.