FTPS using c# 1.1

  • Thread starter Thread starter Simon
  • Start date Start date
S

Simon

this has caused me a couple of days of pain. i have a requirement to
connect to an ftp server endpoint that requires SSL authentication. i
can do the proof of concept using .Net 2 libraries and the
FtpWebRequest - download a file is fine and certificate policy class
is called. but this needs to be in 1.1, which means low level command
sending :( the problem seems to be i am not receiving any remote
certificates (?)

the certificate policy class is never called. i get a 234 ok after the
AUTH SSL but when it tries to issue the next command i get exception
saying 'An established connection was aborted by the software in your
host machine':

here is the code:

public class DefaultCertificatePolicy : ICertificatePolicy
{
public DefaultCertificatePolicy()
{
//
// TODO: Add constructor logic here
//
}
#region ICertificatePolicy Members

public bool CheckValidationResult(
ServicePoint srvPoint,
System.Security.Cryptography.X509Certificates.X509Certificate
certificate,
WebRequest request,
int certificateProblem)
{
return true;
}

#endregion
}

################################

//FTP component code

public void LogonViaSSL()
{
try
{
ServicePointManager.CertificatePolicy = new
DefaultCertificatePolicy();
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

clientSocket = new
Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
IPEndPoint ep = new IPEndPoint(IPAddress.Parse("***.***.***.***"),
****);

clientSocket.Connect(ep);

string response = GetResponse();

sendCommandNoReturn("AUTH SSL");

response = GetResponse();

sendCommandNoReturn("PBSZ 0");

response = GetResponse();

sendCommandNoReturn("PROT P");

response = GetResponse();

sendCommandNoReturn("USER **********");

response = GetResponse();

sendCommandNoReturn("PASS **********");

response = GetResponse();

sendCommandNoReturn("OPTS utf8 on");

response = GetResponse();

sendCommandNoReturn("PWD");

response = GetResponse();

sendCommandNoReturn("CWD /inbox/");

response = GetResponse();

sendCommandNoReturn("TYPE A");

response = GetResponse();

sendCommandNoReturn("PASV");

response = GetResponse();

//need to reconnect to the new client end point here

sendCommandNoReturn("LIST");

response = GetResponse();

sendCommandNoReturn("TYPE A");

response = GetResponse();

}
catch (Exception exception)
{
Cleanup();
}
}

private string GetResponse()
{
Encoding ASCII = Encoding.ASCII;
Byte[] buffer = new byte[512];
string mes = string.Empty;
int bytes;

try
{
char[] seperator = {'\n'};
//lets try and read a line
while(true)
{
//grab the next 512 bytes
bytes = clientSocket.Receive(buffer, buffer.Length, 0);
//convert to ascii and add to the mes string
mes += ASCII.GetString(buffer, 0, bytes);
//we've reached the last iteration
if(bytes < buffer.Length)
{
break;
}
}
}
catch (Exception exception)
{
Cleanup();
}

return mes;
}

private void sendCommandNoReturn(String command)
{
//send the passed in cmd
Byte[] cmdBytes = Encoding.ASCII.GetBytes((command+"\r
\n").ToCharArray());
clientSocket.Send(cmdBytes, cmdBytes.Length, 0);
}
 
Hi,

Pls see the folloing code :

http://ondotnet.com/pub/a/dotnet/2004/05/10/ftpdotnet.htm

Thanks
Shyam



Simon wrote:

FTPS using c# 1.1
10-May-08

this has caused me a couple of days of pain. i have a requirement t
connect to an ftp server endpoint that requires SSL authentication.
can do the proof of concept using .Net 2 libraries and th
FtpWebRequest - download a file is fine and certificate policy clas
is called. but this needs to be in 1.1, which means low level comman
sending :( the problem seems to be i am not receiving any remot
certificates (?

the certificate policy class is never called. i get a 234 ok after th
AUTH SSL but when it tries to issue the next command i get exceptio
saying 'An established connection was aborted by the software in you
host machine'

here is the code

public class DefaultCertificatePolicy : ICertificatePolic

public DefaultCertificatePolicy(

/
// TODO: Add constructor logic her
/

#region ICertificatePolicy Member

public bool CheckValidationResult
ServicePoint srvPoint
System.Security.Cryptography.X509Certificates.X509Certificat
certificate
WebRequest request
int certificateProblem

return true


#endregio


//FTP component cod

public void LogonViaSSL(

tr

ServicePointManager.CertificatePolicy = ne
DefaultCertificatePolicy()
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3

clientSocket = ne
Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp)
IPEndPoint ep = new IPEndPoint(IPAddress.Parse("***.***.***.***")
****)

clientSocket.Connect(ep)

string response = GetResponse()

sendCommandNoReturn("AUTH SSL")

response = GetResponse()

sendCommandNoReturn("PBSZ 0")

response = GetResponse()

sendCommandNoReturn("PROT P")

response = GetResponse()

sendCommandNoReturn("USER **********")

response = GetResponse()

sendCommandNoReturn("PASS **********")

response = GetResponse()

sendCommandNoReturn("OPTS utf8 on")

response = GetResponse()

sendCommandNoReturn("PWD")

response = GetResponse()

sendCommandNoReturn("CWD /inbox/")

response = GetResponse()

sendCommandNoReturn("TYPE A")

response = GetResponse()

sendCommandNoReturn("PASV")

response = GetResponse()

//need to reconnect to the new client end point her

sendCommandNoReturn("LIST")

response = GetResponse()

sendCommandNoReturn("TYPE A")

response = GetResponse()


catch (Exception exception

Cleanup()



private string GetResponse(

Encoding ASCII = Encoding.ASCII
Byte[] buffer = new byte[512]
string mes = string.Empty
int bytes

tr

char[] seperator = {'\n'}
//lets try and read a lin
while(true

//grab the next 512 byte
bytes = clientSocket.Receive(buffer, buffer.Length, 0)
//convert to ascii and add to the mes strin
mes += ASCII.GetString(buffer, 0, bytes)
//we've reached the last iteratio
if(bytes < buffer.Length

break



catch (Exception exception

Cleanup()


return mes


private void sendCommandNoReturn(String command

//send the passed in cm
Byte[] cmdBytes = Encoding.ASCII.GetBytes((command+"\
\n").ToCharArray())
clientSocket.Send(cmdBytes, cmdBytes.Length, 0)
}

Previous Posts In This Thread:

FTPS using c# 1.1
this has caused me a couple of days of pain. i have a requirement t
connect to an ftp server endpoint that requires SSL authentication.
can do the proof of concept using .Net 2 libraries and th
FtpWebRequest - download a file is fine and certificate policy clas
is called. but this needs to be in 1.1, which means low level comman
sending :( the problem seems to be i am not receiving any remot
certificates (?

the certificate policy class is never called. i get a 234 ok after th
AUTH SSL but when it tries to issue the next command i get exception
saying 'An established connection was aborted by the software in your
host machine':

here is the code:

public class DefaultCertificatePolicy : ICertificatePolicy
{
public DefaultCertificatePolicy()
{
//
// TODO: Add constructor logic here
//
}
#region ICertificatePolicy Members

public bool CheckValidationResult(
ServicePoint srvPoint,
System.Security.Cryptography.X509Certificates.X509Certificate
certificate,
WebRequest request,
int certificateProblem)
{
return true;
}

#endregion
}


//FTP component code

public void LogonViaSSL()
{
try
{
ServicePointManager.CertificatePolicy = new
DefaultCertificatePolicy();
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

clientSocket = new
Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
IPEndPoint ep = new IPEndPoint(IPAddress.Parse("***.***.***.***"),
****);

clientSocket.Connect(ep);

string response = GetResponse();

sendCommandNoReturn("AUTH SSL");

response = GetResponse();

sendCommandNoReturn("PBSZ 0");

response = GetResponse();

sendCommandNoReturn("PROT P");

response = GetResponse();

sendCommandNoReturn("USER **********");

response = GetResponse();

sendCommandNoReturn("PASS **********");

response = GetResponse();

sendCommandNoReturn("OPTS utf8 on");

response = GetResponse();

sendCommandNoReturn("PWD");

response = GetResponse();

sendCommandNoReturn("CWD /inbox/");

response = GetResponse();

sendCommandNoReturn("TYPE A");

response = GetResponse();

sendCommandNoReturn("PASV");

response = GetResponse();

//need to reconnect to the new client end point here

sendCommandNoReturn("LIST");

response = GetResponse();

sendCommandNoReturn("TYPE A");

response = GetResponse();

}
catch (Exception exception)
{
Cleanup();
}
}

private string GetResponse()
{
Encoding ASCII = Encoding.ASCII;
Byte[] buffer = new byte[512];
string mes = string.Empty;
int bytes;

try
{
char[] seperator = {'\n'};
//lets try and read a line
while(true)
{
//grab the next 512 bytes
bytes = clientSocket.Receive(buffer, buffer.Length, 0);
//convert to ascii and add to the mes string
mes += ASCII.GetString(buffer, 0, bytes);
//we've reached the last iteration
if(bytes < buffer.Length)
{
break;
}
}
}
catch (Exception exception)
{
Cleanup();
}

return mes;
}

private void sendCommandNoReturn(String command)
{
//send the passed in cmd
Byte[] cmdBytes = Encoding.ASCII.GetBytes((command+"\r
\n").ToCharArray());
clientSocket.Send(cmdBytes, cmdBytes.Length, 0);
}

FTPS using c# 1.1
You are taking on a huge task trying to implement FTPS yourself - even FTP. It is decidedly non-trivial.

Why not try one of the many FTPS products out there such as edtFTPnet/PRO?

It supports FTP, SFTP and FTPS in a single component, and I guarantee it will save you a lot of grief trying to write it yourself.

See

http://www.enterprisedt.com/products/edtftpnetpro/overview.html

for more details and trial download.


Submitted via EggHeadCafe - Software Developer Portal of Choice
BizTalk: Conditional looping incorporating the Greater Than functoid.
http://www.eggheadcafe.com/tutorial...2d-043c18df964c/biztalk-conditional-loop.aspx
 
Back
Top