Indy IMAP4 over SSL - delphi

I've written a Windows service in Delphi 2007 using Indy 10 (10.5.8.0) that connects to an email server via IMAP and reads 10 emails, processes them and disconnects.
This is repeated throughout the day.
This works fine as long as the service doesn't connect using SSL/TLS.
When connecting using TLS it seems to work the first time, but gives a "start SSL negotiation failed" error on subsequent attempts.
I'm using the following Indy settings in my service:
FIMAP4.Port := 143
FIMAP4.UseTLS := utUseRequireTLS;
FIdSSLIOHandlerSocketOpenSSL1.SSLOptions.Method := sslvSSLv23;
The email server runs Exchange 2010 and operates TLS on port 143 and SSL on port 993.
I'm using the latest version of the ssl dlls (1.0.1.3).
I'm disconnecting by doing:
FIMAP4.IOHandler.InputBuffer.Clear;
FIMAP4.Disconnect;
UPDATE:
Extract from EurekaLog:
|IdExplicitTLSClientServerBase.pas|TIdExplicitTLSClient|TLSNotAvailable |416[4] |
|IdExplicitTLSClientServerBase.pas|TIdExplicitTLSClient|TLSNotAvailable |412[0] |
|IdExplicitTLSClientServerBase.pas|TIdExplicitTLSClient|ProcessTLSNotAvail|342[2] |
|IdExplicitTLSClientServerBase.pas|TIdExplicitTLSClient|ProcessTLSNotAvail|341[1] |
|IdIMAP4.pas |TIdIMAP4 |Login |1957[16]|
|IdIMAP4.pas |TIdIMAP4 |Connect |2019[28]|
This would seem to indicate TLS is NOT available (I would have expected TLSNotAvailable to raise a
EIdTLSClientTLSNotAvailable.Create(RSTLSSLSSLNotAvailable);
rather than a
EIdTLSClientTLSNotAvailable.Create(RSTLSSLSSLCmdFailed);
but this has led me to wonder if the email server does not support TLS, at least on port 143).
If I don't use TLS, I get a:
EIdReplyIMAP4Error: Command received in Invalid state.
|IdReplyIMAP4.pas |TIdReplyIMAP4 |RaiseReplyError |242[1] |
|IdTCPConnection.pas|TIdTCPConnection|RaiseExceptionForLastCmdResult|576[1] |
|IdIMAP4.pas |TIdIMAP4 |Login |1970[29]|
|IdIMAP4.pas |TIdIMAP4 |Connect |2019[28]|
This seems to be an authentication failed message, although I'm not sure why.

Try using sslvTLSv1 instead of sslvSSLv23.

Fixed!
Two critical settings were wrong:
FIdSSLIOHandlerSocketOpenSSL1.SSLOptions.Method := sslvTLSv1;
FIMAP4.UseTLS := utUseImplicitTLS;
setting these up (still on port 143) resolved the issue.

The latest Indy 10 version works much better at TLS, SSL, IMAP, SMTP, and SASL, than the one that shipped in Delphi 2007. Heck, Delphi 2007 shipped in 2006 at some point, and so the Indy 10 version is a snapshot from 2006, that's SIX YEARS AGO.
Upgrade your Indy10 sources, and then try different SSL options.
You may also have a bad SSL (ssleay, libssl) DLL version. Use the versions recommended on the Indy page.

Related

Deplhi with Indy and TLS, problems with https://testesmoleculares.com.br/ (Cloudflare)

I'm not have expertise with Delphi and TLS using Indy.
Maybe it's not a problem, just config, I need examples.
I tried some questions on Stackoverflow too, all unsuccessfully.
Page: https://testesmoleculares.com.br/
Errors:
---------------------------
Debugger Exception Notification
---------------------------
Project IntegradorApoiado.exe raised exception
class EIdOSSLUnderlyingCryptoError with message 'Error connecting with SSL.
error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure'.
---------------------------
Break Continue Help
---------------------------
Actual config:
LHandler.SSLOptions.Method := sslvSSLv2;
LHandler.SSLOptions.Mode := sslmClient;
LHandler.SSLOptions.SSLVersions := [sslvSSLv2];
Tried:
IdSSLIOHandlerSocketOpenSSL1.SSLOptions.Method := sslvTLSv1_2;
IdSSLIOHandlerSocketOpenSSL1.SSLOptions.SSLVersions := [sslvTLSv1_2];
Someone can help?
Maybe providing an example of POST? I'm using Delphi XE4
Thanks!
Github issue:
https://github.com/IndySockets/Indy/issues/317
Tried:
https://stackoverflow.com/a/49061252
ssl v3 poodle and move to tls with indy
Using Indy 10 IdHTTP with TLS 1.2
https://indy.fulgan.com/SSL/?C=M;O=A
Others
Without seeing the actual handshake data, it is difficult to say for sure why it is failing.
However, I will mention that sslvSSLv2 is for SSL v2.0, which nobody uses anymore, as it is no longer secure. Same with sslvSSLv3 (SSL v3.0). So, never use sslvSSLv2 and sslvSSLv3 (unless you HAVE to, for legacy purposes).
sslvTLSv1_2 is for TLS v1.2. A lot of servers are now migrating to this. But many servers haven't fully migrated yet. So, you should enable TLS v1.0 and v1.1 as well for wider acceptance, eg (do not use SSLOptions.Method at all, just use SSLOptions.SSLVersions):
LHandler.SSLOptions.Mode := sslmClient;
LHandler.SSLOptions.SSLVersions := [sslvTLSv1, sslvTLSv1_1, sslvTLSv1_2];
By specifying multiple TLS versions, that will allow OpenSSL to negotiate an available version that both peers support.
Also, make sure you are using an up-to-date version of Indy (if you are not already) so that you have all of its fixes for TLS support, such as use of SNI (which many servers now require TLS clients to use).
I can connect to https://testesmoleculares.com.br/ in a web browser using TLS 1.2, so it should be possible to connect to it using TLS 1.2 in Indy as well.
UPDATE: based on additional comments you have posted for this same issue in Indy's issue tracker (#317: TLS - Problems with https://testesmoleculares.com.br/ on Cloudflare), you are using an outdated version of Indy (10.6.0.4975), which does not fully handle TLS 1.2. You need to upgrade to the latest version (10.6.2) in order to use TLS 1.2 properly.

Indy "Connection Reset by Peer" Err. 10054 on Specific Website

I am encountering some problems using some code that worked for ages involving Indy and the download of a web page. I use RAD Studio 10.2 Tokyo.
The web page is as follows:
https://donet.rfi.it/RFIPlatform/showDoc.do?compartimentoHidden=AN&docTypeHidden=CC
The code I am using is part of an application which has the same code since 2011 and it always worked well. The code is as follows:
IDHTTP1.Get('https://donet.rfi.it/RFIPlatform/showDoc.do?compartimentoHidden=AN&docTypeHidden=CC');
I am getting a "Connection Reset by Peer 10054" error since the website went down, some days ago, and when it came up again, the code did not work anymore.
The aforementioned web page can be called from the browser, can even be downloaded with WGET, but Indy is failing.
I tried to play with various options (Cookie Handling, Handle Redirects, HTTPOptions, etc...) and I also updated the SSL libraries to 1.0.2q (Indy cannot use OpenSSL 1.1.0 yet), but the whole thing just doesn't want to work.
Can someone help me figure out what is going on? It has to be for sure something on the website, since the code I use is the same since 2011 and it has always worked. And before that, the same code worked in a similar application since 2008.
Indy's TIdSSLIOHandlerSocketOpenSSL component enables only TLS 1.0 by default. The website in question (https://donet.rfi.it) does not accept TLS 1.0 anymore (probably why it went offline, to update its software), it will accept only TLS 1.1+ now.
TIdHTTP is able to successfully establish a TCP/IP connection to donet.rfi.it:443, but as soon as TIdSSLIOHandlerSocketOpenSSL sends a TLS 1.0 handshake request, the server forcibly closes the TCP connection. You are getting the "connection reset by peer" error while TIdSSLIOHandlerSocketOpenSSL is trying to read the server's handshake response.
You need to configure TIdSSLIOHandlerSocketOpenSSL to enable TLS 1.1 and/or 1.2. You can do that via its SSLOptions.SSLVersions property. Then TIdHTTP.Get() will work again (I tested it).

Cannot use secure SMTP connection to Office365 with Delphi 2010 and Indy 10.5.5

Can anyone diagnose my failure to connect to smtp.office365.com using Delphi 2010 and Indy 10.5.5?
I have read the code examples provided by Indy expert Remy Lebeau in this question: Using INDY 10 SMTP with Office365.
I have taken each of the two alternate code samples (one using AuthType satDefault and the other satSASL) from that question and cut-and-pasted them into a console application. In each case get the following error when run:
EIdOSSLUnderlyingCryptoError: Error connecting with SSL.
Error connecting with SSL.
error:1408F10B:SSL routines:SSL3_GET_RECORD:wrong version number
When I change UseTLS to utUseExplicitTLS (as suggested in some other answers that state a plain text connection is required before attempting TLS) I get the following error:
EIdOSSLConnectError: Error connecting with SSL.
Error connecting with SSL.
EOF was observed that violates the protocol
I notice some references in other answers to setting SSLOptions.SSLVersions instead of SSLOptions.Method (which is said to be deprecated) but that property does not appear to exist on TIdSSLIOHandlerSocketOpenSSL in my version of Indy.
I believe I have the version of Indy which shipped with the D2010 updates but that it's somewhat out-of-date. I would be willing to update a newer version if one exists but can't seem to find anyway to do so without installing SVN, learning it, checking out from Development, and recompiling.
Finally, I've tried running all four variants of my console application with no DLLs present in the executable directory and with the DLLs from OpenSSL v1.0.2h present without any apparent effect.
EDIT: One other piece of information. The SMTP credentials supplied by the client (this project sends emails through a single dedicated outbound email address) is of the form project#myclientsdomain.com, rather than #office365.
Apparently, since Remy wrote the answer in the linked question, Office365 has stopped supporting SSLv3 (presumably to avoid a known exploit) and now requires TLS. The code from his answer in the original question works if you change these two lines:
idSMTP1.UseTLS := utUseImplicitTLS;
TIdSSLIOHandlerSocketOpenSSL(idSMTP1.IOHandler).SSLOptions.Method := sslvSSLv3;
to:
idSMTP1.UseTLS := utUseExplicitTLS;
TIdSSLIOHandlerSocketOpenSSL(idSMTP1.IOHandler).SSLOptions.Method := sslvTLSv1;

SSL3_GET_RECORD:wrong version number

I am using TIdTCPClient & TidTCPServer to sending data from Client to Server Through SSL.
I set the following code at server & Client
*strong text*Server Side
IdServerIOHandlerSSLOpenSSL1.SSLOptions.Mode = sslmServer
IdServerIOHandlerSSLOpenSSL1.SSLOptions.Method = sslvTLSv1
*strong text*Cleint Side
IdSSLIOHandlerSocketOpenSSL1.SSLOptions.Mode = sslmUnassigned
IdSSLIOHandlerSocketOpenSSL1.SSLOptions.Method = sslvTLSv1
Error is error:*1408F10B:SSL routines:SSL3_GET_RECORD:wrong version number at Server Side*
When I am executing the Server Application from Delphi IDE, Error message is shown, After click Continue button it establish the connection
At Server & Client both are using the Same DLL Versions
libeay32.dll - 0.9.8.18
ssleay32.dll - 0.9.8.18
I am using Delphi Version 2010 & Indy Version is 10.5.5.
The above error is not occurred in Same system. It is happend in client system.
the above dlls are placed at Exe Folder.
What am i doing wrong with above code or any wrongly configured..?
thanx in advance
There is no way you can have an established connection after that error occurs. It is an SSL handshake error. OpenSSL would have closed the connection after reporting the error to you.
You are using a bit of an outdated version of Indy. The current version is 10.5.9.

Connection Timed Out + SSL Connection + TCPIP + Indy10.5.5

I am developed one application for sending files client to server using TCP/IP Components using SSL.
Error is Connection Timed Out is occurred when Sending Large Files at Server Side
Server Side (SSL)
idTCPServer1.IOHandler := IdServerIOHandlerSSLOpenSSL1;
OnExecute
Acontext.Connection.IOHandler.LargeStream := True;
Acontext.Connection.IOHandler.ReadStream(filestream,Filesize); // Error is occurred here.
End;
Client Side (SSL)
idTCPClient1.IOHandler := IdSSLIOHandlerSocketOpenSSL1;
idTCPClient1.IOHandler.LargeStream := True;
idTCPClient1.IOHandler.WriteStream(fms,-1,True);
Connection is established between Client & Server. When send the files after some time Connection Timed out is error is shown at server & at client application going to Not Responding .
Here another scenario is when i connect the Client & Server as Normal using IdIOHandlerStack1 (at Client Side) and server side not assign any Handler. Files transfered successfully with out any problem.
I am using Delphi2010 & Indy10.5.5, DLL Version is 0.9.8.18
anyone guide to me
There is no WriteStream() method, there is a TStream overload of the Write() method instead.
You are telling Write() to send the stream size as a 64-bit integer, but you are not telling ReadStream() to read the stream size, so presumably you have read the full 64-bit file size beforehand using ReadInt64(), correct?
Also, 10.5.5 is an outdated version of Indy. The current version is 10.5.9. Try upgrading to make sure you have all of the latest bug fixes and such, and then report back if you are still having problems.

Resources