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);
}
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);
}