Hi all!
After research, i have solved it. See it for more details :)
Implementing a Secure Socket
My code after read it :)
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
namespace SslTest
{
public partial class Form1 : Form
{
private const ushort SO_SECURE = 0x2001;
private const ushort SO_SEC_SSL = 0x2004;
private const int _SO_SSL_VALIDATE_CERT_HOOK = 0x08;
private const int SO_SSL_FAMILY = 0x00730000;
private const long _SO_SSL = ((2L << 27) | SO_SSL_FAMILY);
private const uint IOC_IN = 0x80000000;
private const long SO_SSL_SET_VALIDATE_CERT_HOOK = (IOC_IN | _SO_SSL | _SO_SSL_VALIDATE_CERT_HOOK);
public delegate int SSLVALIDATECERTFUNC(uint dwType, IntPtr pvArg, uint dwChainLen, IntPtr pCertChain, uint dwFlags);
private IntPtr hookFunc;
public Form1()
{
InitializeComponent();
}
private void btnReceive_Click(object sender, EventArgs e)
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
socket.SetSocketOption(SocketOptionLevel.Socket, (SocketOptionName)SO_SECURE, SO_SEC_SSL);
hookFunc = Marshal.GetFunctionPointerForDelegate(new SSLVALIDATECERTFUNC(ValidateCert));
byte[] inBuffer = new byte[8];
byte[] hookFuncBytes = BitConverter.GetBytes(hookFunc.ToInt32());
Array.Copy(hookFuncBytes, inBuffer, hookFuncBytes.Length);
unchecked
{
socket.IOControl((int)SO_SSL_SET_VALIDATE_CERT_HOOK, inBuffer, null);
}
IPHostEntry hostEntry = Dns.GetHostEntry("xxx.xxx.xxx.xxx");
IPEndPoint ip = new IPEndPoint(hostEntry.AddressList[0], 995);
socket.Connect(ip);
NetworkStream netStream = new NetworkStream(socket);
StreamReader reader = new StreamReader(netStream);
MessageBox.Show(reader.ReadLine());
netStream.Close();
reader.Close();
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
private int ValidateCert(uint dwType, IntPtr pvArg, uint dwChainLen, IntPtr pCertChain, uint dwFlags)
{
return 0;
}
}
}
Hope to help someone! ;)
Best Regards,
LuongHVM