An existing connection was forcibly closed by the - WeOnlyDo Discussion board

An existing connection was forcibly closed by the (wodFtpDLX / wodFtpDLX.NET)

by Tamil, Thursday, December 18, 2008, 12:27 (5601 days ago)

I m getting the following error when i try to connect to FTPS server

Error:
An existing connection was forcibly closed by the remote host

Code:

mFtp1.Blocking = True
mFtp1.Hostname = sHost
mFtp1.Login = sLogin
mFtp1.Password = sPwd
mFtp1.TempAuthorization = 633651011318356000 | yPNPPyMBfUXE2bJHHBnAxHIl8FI=
mFtp1.Authentication = WeOnlyDo.Client.Authentications.Password
mFtp1.Protocol = WeOnlyDo.Client.Protocols.FTPSwithdata

Try
mFtp1.Connect()

If Not mFtp1.LastError Is Nothing Then
Return False
Else
Return True
End If
mFtp1.Disconnect(True)
mFtp1 = Nothing
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message, Ftp1 Connect Error , System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Stop)
Return False

End Try

Re: An existing connection was forcibly closed by

by wodDamir, Thursday, December 18, 2008, 12:48 (5601 days ago) @ Tamil

Tamil,

Can you tell me what line exactly throws this error? (If you remove the try...Catch block, component should throw an exception on the method that caused it.

Also, can you try using any other client on that server?

Regards,
Damba

Re: An existing connection was forcibly closed by

by tamil, Thursday, December 18, 2008, 12:53 (5601 days ago) @ wodDamir

Following line throw the error:

mFtp1.Connect()

Re: An existing connection was forcibly closed by

by tamil, Thursday, December 18, 2008, 12:56 (5601 days ago) @ tamil

We are using coreFTP server.

FTP and SFTP is working fine.
We are having only problem with FTPS.

Re: An existing connection was forcibly closed by

by tamil, Thursday, December 18, 2008, 12:59 (5601 days ago) @ tamil

Sometimes the following error also occurs
The handshake failed due to an unexpected packet format

Re: An existing connection was forcibly closed by

by wodDamir, Thursday, December 18, 2008, 13:26 (5601 days ago) @ tamil

Tamil,

Did you load a certificate in Core FTP Server?

FTPS protocol as to have one defined in order to work. Otherwise connection would be refused, since connection can't be secured.

I duplicated the The handshake failed due to an unexpected packet format with no Certificate loaded. As soon as I set Self-signed certificate, everything started working properly.

Regards,
Damba

Re: An existing connection was forcibly closed by

by tamil, Friday, December 19, 2008, 06:35 (5601 days ago) @ wodDamir

Thanks a lot Damba.

Its working for password authentication.
We want to use certificate authentication also.
When I tried to use certificate authentication , I have got the following error Failed to import certificate. .

Do you have any sample code for certificate authentication.

Re: An existing connection was forcibly closed by

by wodDamir, Friday, December 19, 2008, 08:31 (5600 days ago) @ tamil

Hi Tamil,

Here is an article from our database that shows how to load and use certificate to connecto to FTPS server:

http://www.weonlydo.com/index.asp?kb=1&View=entry&EntryID=74

Hope this helps.

Regards,
Damba

Re: An existing connection was forcibly closed by

by tamil, Monday, December 22, 2008, 07:48 (5598 days ago) @ wodDamir

Hi Damba,

I am getting the following error When I try to use certificate in our web application.
Error is The system cannot find the file specified .
But actually file is in that location.

Code:

Dim cert As System.Security.Cryptography.X509Certificates.X509Certificate2
Dim strCertPath As String = System.Configuration.ConfigurationSettings.AppSettings.Item( CertFilePath )
cert = New System.Security.Cryptography.X509Certificates.X509Certificate2
cert.Import( C: mp0000001.p12 , ftpstest , System.Security.Cryptography.X509Certificates.X509KeyStorageFlags.Exportable)
ftps1.Certificate = cert
ftps1.Authentication = WeOnlyDo.Client.Authentications.Both


error is on cert.Import line

Re: An existing connection was forcibly closed by

by woddrazen, Monday, December 22, 2008, 09:32 (5597 days ago) @ tamil

Tamil,

What happened when you change your Import line to this:
[code]cert.Import( C:\\tmp\\00000001.p12 , ftpstest , System.Security.Cryptography.X509Certificates.X509KeyStorageFlags.Exportable)[/code]
Drazen

Re: An existing connection was forcibly closed by

by tamil, Monday, December 22, 2008, 10:09 (5597 days ago) @ woddrazen

Still I am getting error even after changing like this


Code:
--------------------------------------------------------------------------------
cert.Import( C:\tmp\00000001.p12 , ftpstest , System.Security.Cryptography.X509Certificates.X509KeyStorageFlags.Exportable)

Re: An existing connection was forcibly closed by

by tamil, Monday, December 22, 2008, 10:21 (5597 days ago) @ tamil

Still I am getting error even after changing like this

Code:
--------------------------------------------------------------------------------
cert.Import( C:\ mp\0000001.p12 , ftpstest , System.Security.Cryptography.X509Certificates.X509KeyStorageFlags.Exportable)
--------------------------------------------------------------------------------

Re: An existing connection was forcibly closed by

by tamil, Monday, December 22, 2008, 11:13 (5597 days ago) @ tamil

Still I am getting error even if I use double slash.

In my previous message , its displaying single slash even if i give double double slash.

Re: An existing connection was forcibly closed by

by woddrazen, Monday, December 22, 2008, 11:45 (5597 days ago) @ tamil

Tamil,


Is there any chance you can send us your (or some test) certificate so we can test this on our side?

You can send it to: techsupport@weonlydo.com


Drazen

Re: An existing connection was forcibly closed by

by tamil, Monday, December 22, 2008, 12:47 (5597 days ago) @ woddrazen

The same key and file path is working for windows application.
But it is not working for web application.
I have sent key file to you.

Re: An existing connection was forcibly closed by

by tamil, Monday, December 29, 2008, 07:10 (5591 days ago) @ tamil

That issue occurs if we give
<identity impersonate= true /> in web.config file.

some suggestion to solve this issue. Please check the following link
http://support.microsoft.com/kb/948154

As per suggestion in the above link , I have given permission to the account for private key and modified the code like below.
Dim store As New X509Store( TrustedPeople , StoreLocation.CurrentUser)
store.Open(OpenFlags.[ReadOnly] Or OpenFlags.OpenExistingOnly)

Dim newCert As System.Security.Cryptography.X509Certificates.X509Certificate2 = store.Certificates.Find(X509FindType.FindBySubjectName, ftp.caldev2.com , False)(0)
ftps1.Certificate = newCert


But now I am getting the following error:
Failed to import private key.

If I give
ftps1.PrivateKey = newCert.PrivateKey

Error :
Keyset does not exist

Re: An existing connection was forcibly closed by

by woddrazen, Monday, December 29, 2008, 09:52 (5590 days ago) @ tamil

Tamil,


wodFtpDLX.NET in PrivateKey Property expect private key in XML format.

To convert your PrivateKey to XML format you can use our KeyManager application.

Here is download link for KeyManager application:
http://www.weonlydo.com/Samples/KeyManager.NET.zip

If you want to do that inside your code you should install wodSFTP.NET or wodSSH.NET. Inside that components we provide wodKeyManager.NET component which is used for loading, generating keys. You can then convert key to XML format.

Here is example for wodSFTP.NET component how to connect to server with private key using wodKeyManager.NET:
http://www.weonlydo.com/index.asp?kb=1&View=entry&EntryID=62


Drazen

Re: An existing connection was forcibly closed by

by tamil, Monday, December 29, 2008, 10:22 (5590 days ago) @ woddrazen

All the samples reading key file from specified path.

eg: keys.Load( C:0000001 , ftpstest )

Is there any possibility to load or find key without using folder path.
Because we are having the issue in reading file from path.

For eg:
For certificate we are installing it in certificare store and finding it using the following code.

Dim store As New X509Store( TrustedPeople , StoreLocation.CurrentUser)
store.Open(OpenFlags.[ReadOnly] Or OpenFlags.OpenExistingOnly)
Dim newCert As System.Security.Cryptography.X509Certificates.X509Certificate2 = store.Certificates.Find(X509FindType.FindBySubjectName, ftp.caldev2.com , False)(0)
ftps1.Certificate = newCert

Do you have any similar kind of code for keys.


Re: An existing connection was forcibly closed by

by woddrazen, Monday, December 29, 2008, 11:23 (5590 days ago) @ tamil

Tamil,


Did you try something like this:
[code]ftps1.PrivateKey = newCert.PrivateKey.ToXmlString(True)[/code]
Drazen

Re: An existing connection was forcibly closed by

by tamil, Monday, December 29, 2008, 12:32 (5590 days ago) @ woddrazen

I am getting the following error if I use
ftps1.PrivateKey = newCert.PrivateKey.ToXmlString(True)

error:
Key not valid for use in specified state.

Re: An existing connection was forcibly closed by

by woddrazen, Monday, December 29, 2008, 15:35 (5590 days ago) @ tamil

Tamil,


I cannot duplicate your problem. Here is ASP.NET code I'm using:
[code]ftp1 = New WeOnlyDo.Client.FtpDLX

Dim cert As System.Security.Cryptography.X509Certificates.X509Certificate2
cert = New System.Security.Cryptography.X509Certificates.X509Certificate2

cert.Import( c:\00000001.p12 , ftpstest , System.Security.Cryptography.X509Certificates.X509KeyStorageFlags.Exportable)

ftp1.Protocol = WeOnlyDo.Client.Protocols.FTPSwithdata
ftp1.Hostname = hostname
ftp1.Login = login
ftp1.Authentication = WeOnlyDo.Client.Authentications.Certificate
ftp1.Blocking = True
ftp1.Certificate = cert
ftp1.PrivateKey = cert.PrivateKey.ToXmlString(True)

ftp1.Connect()[/code]
Are you using same certificate you send us?

Did you try to connect there using some other FTPS client maybe? Is there any chance that we could connect to your server and duplicate your problem? You can send your private information to techsupport@weonydo.com


Drazen