static void find_superpeer() { Console.Write("Destination: "); string dest_key = Console.ReadLine(); IPAddress ipAddress = IPAddress.Parse(local_ip); IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, local_port); //Connect to server TcpClient client = new TcpClient(ipLocalEndPoint); client.Connect(server_ip, server_port); SslStream sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null); authenticate_server(sslStream); TCPCommunication.send_message_tcp(sslStream, "FIND_P"); TCPCommunication.send_message_tcp(sslStream, HashString.GetHashString(pubKey.ToString())); string response = TCPCommunication.recieve_message_tcp(sslStream); if (String.Compare(response, "ACCEPT") == 0) { TCPCommunication.send_message_tcp(sslStream, dest_key); response = TCPCommunication.recieve_message_tcp(sslStream); string[] temp_split = response.Split(':'); dest_ip = temp_split[1]; dest_port = Int32.Parse(temp_split[2]); Console.WriteLine($"destination peer in {dest_ip}:{dest_port}"); //TCPCommunication.send_message_tcp(sslStream, pubKey.ToString()); //response = TCPCommunication.recieve_message_tcp(sslStream); //Console.WriteLine(response); sslStream.Close(); client.Close(); client = new TcpClient(ipLocalEndPoint); Console.WriteLine("Client connecting"); client.Connect(dest_ip, dest_port); Console.WriteLine("Client connected"); sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null); authenticate_server(sslStream); req_connection(sslStream, client, dest_key); sslStream.Close(); client.Close(); } else if (String.Compare(response, "REJECT") == 0) { Console.WriteLine("Connection rejected"); sslStream.Close(); client.Close(); } }
static void anonym_peer() { IPAddress ipAddress = IPAddress.Parse(local_ip); IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, local_port); //Connect to server TcpClient client = new TcpClient(ipLocalEndPoint); client.Connect(server_ip, server_port); SslStream sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null); authenticate_server(sslStream); TCPCommunication.send_message_tcp(sslStream, "ANONYM_P"); TCPCommunication.send_message_tcp(sslStream, HashString.GetHashString(pubKey.ToString())); string response = TCPCommunication.recieve_message_tcp(sslStream); if (String.Compare(response, "ACCEPT") == 0) { node = new ECDiffieHellmanOpenSsl(); ECParameters node_ep = node.ExportParameters(false); pubKey = new PublicKeyCoordinates(node_ep.Q.X, node_ep.Q.Y); string hash = HashString.GetHashString(pubKey.ToString()); TCPCommunication.send_message_tcp(sslStream, hash); response = TCPCommunication.recieve_message_tcp(sslStream); Console.WriteLine(response); sslStream.Close(); client.Close(); } else if (String.Compare(response, "REJECT") == 0) { Console.WriteLine("Connection rejected"); sslStream.Close(); client.Close(); } }
static void init_connection(SslStream sslStream) { //Authenticate certificate authenticate_server(sslStream); TCPCommunication.send_message_tcp(sslStream, "INIT_P"); string response = TCPCommunication.recieve_message_tcp(sslStream); Console.WriteLine(response); node = new ECDiffieHellmanOpenSsl(); ECParameters node_ep = node.ExportParameters(false); pubKey = new PublicKeyCoordinates(node_ep.Q.X, node_ep.Q.Y); Console.WriteLine("My hash key: " + HashString.GetHashString(pubKey.ToString())); //Console.WriteLine(pubKey.ToString()); TCPCommunication.send_message_tcp(sslStream, pubKey.ToString()); }
static void request_keys() { Thread.Sleep(4000); Console.WriteLine("Requesting public keys"); IPAddress ipAddress = IPAddress.Parse(local_ip); IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, local_port); //Connect to server TcpClient client = new TcpClient(ipLocalEndPoint); try { client.Connect(server_ip, server_port); } catch (Exception e) { Console.WriteLine("try again!!!"); Thread.Sleep(1000); client.Connect(server_ip, server_port); } SslStream sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null); authenticate_server(sslStream); TCPCommunication.send_message_tcp(sslStream, "REQ_P"); string response = TCPCommunication.recieve_message_tcp(sslStream); string[] temp_split = response.Split("/"); for (int i = 0; i < temp_split.Length; ++i) { print_key(temp_split[i]); } sslStream.Close(); client.Close(); }
static void listen_connection(SslStream sslStream, TcpClient client) { myAes = Aes.Create(); myAes.Key = new byte[16] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 }; myAes.IV = new byte[16] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; TCPCommunication.send_message_tcp(sslStream, "LISTEN_P"); TCPCommunication.send_message_tcp(sslStream, HashString.GetHashString(pubKey.ToString())); string response = TCPCommunication.recieve_message_tcp(sslStream); if (String.Compare(response, "ACCEPT") == 0) { byte[] data = new Byte[256]; data = Encoding.UTF8.GetBytes(pubKey.ToString()); sslStream.Write(data); sslStream.Flush(); data = new Byte[256]; sslStream.Read(data, 0, data.Length); response = Encoding.UTF8.GetString(data); PublicKeyCoordinates request_key = JsonConvert.DeserializeObject <PublicKeyCoordinates>(response); sslStream.Close(); client.Close(); ECDiffieHellmanOpenSsl temp = new ECDiffieHellmanOpenSsl(); ECParameters epTemp = temp.ExportParameters(false); epTemp.Q.X = request_key.X; epTemp.Q.Y = request_key.Y; ECDiffieHellmanPublicKey servePubKey = ECDiffieHellman.Create(epTemp).PublicKey; byte[] sharedKey = node.DeriveKeyMaterial(servePubKey); Console.WriteLine(BitConverter.ToString(sharedKey).Replace("-", "")); //myAes.Key = sharedKey; //myAes.Key = new byte[16] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 }; DTLSClient dtls_client = new DTLSClient(server_ip, server_port.ToString(), new byte[] { 0xBA, 0xA0 }); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { dtls_client.Unbuffer = "winpty.exe"; dtls_client.Unbuffer_Args = "-Xplain -Xallow-non-tty"; } else { dtls_client.Unbuffer = "stdbuf"; dtls_client.Unbuffer_Args = "-i0 -o0"; } dtls_client.Start(); /* statpair IOStream = new statpair(new StreamReader(Console.OpenStandardInput()), new StreamWriter(Console.OpenStandardOutput())); * new Thread(() => dtls_client.GetStream().CopyTo(IOStream, 16)).Start();*/ read_relay(dtls_client); /*while(true) * { * string input = Console.ReadLine(); * byte[] encryptedData = EncryptStringToBytes_Aes(input, myAes.Key, myAes.IV); * dtls_client.GetStream().Write(encryptedData); * //dtls_client.GetStream().Write(Encoding.Default.GetBytes(input+Environment.NewLine)); * }*/ dtls_client.WaitForExit(); } else if (String.Compare(response, "REJECT") == 0) { Console.WriteLine("Connection rejected"); sslStream.Close(); client.Close(); } }
static void req_connection(SslStream sslStream, TcpClient client, string dest_key) { myAes = Aes.Create(); myAes.Key = new byte[16] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 }; myAes.IV = new byte[16] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; TCPCommunication.send_message_tcp(sslStream, "CONNECT_P"); TCPCommunication.send_message_tcp(sslStream, HashString.GetHashString(pubKey.ToString())); string response = TCPCommunication.recieve_message_tcp(sslStream); if (String.Compare(response, "ACCEPT") == 0) { TCPCommunication.send_message_tcp(sslStream, dest_key); response = TCPCommunication.recieve_message_tcp(sslStream); Console.WriteLine(response); if (String.Compare(response, "ACCEPT") == 0) { response = TCPCommunication.recieve_message_tcp(sslStream); int dtls_port = Int32.Parse(response); byte[] data = new Byte[256]; data = Encoding.UTF8.GetBytes(pubKey.ToString()); sslStream.Write(data); sslStream.Flush(); data = new Byte[256]; sslStream.Read(data, 0, data.Length); response = Encoding.UTF8.GetString(data); PublicKeyCoordinates listen_key = JsonConvert.DeserializeObject <PublicKeyCoordinates>(response); sslStream.Close(); client.Close(); ECDiffieHellmanOpenSsl temp = new ECDiffieHellmanOpenSsl(); ECParameters epTemp = temp.ExportParameters(false); epTemp.Q.X = listen_key.X; epTemp.Q.Y = listen_key.Y; ECDiffieHellmanPublicKey servePubKey = ECDiffieHellman.Create(epTemp).PublicKey; byte[] sharedKey = node.DeriveKeyMaterial(servePubKey); Console.WriteLine(BitConverter.ToString(sharedKey).Replace("-", "")); //myAes.Key = sharedKey; //myAes.Key = new byte[16] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 }; DTLSClient dtls_client = new DTLSClient(server_ip, dtls_port.ToString(), new byte[] { 0xBA, 0xA0 }); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { dtls_client.Unbuffer = "winpty.exe"; dtls_client.Unbuffer_Args = "-Xplain -Xallow-non-tty"; } else { dtls_client.Unbuffer = "stdbuf"; dtls_client.Unbuffer_Args = "-i0 -o0"; } dtls_client.Start(); /*statpair IOStream = new statpair(new StreamReader(Console.OpenStandardInput()), new StreamWriter(Console.OpenStandardOutput())); * new Thread(() => dtls_client.GetStream().CopyTo(IOStream, 16)).Start();*/ //new Thread(() => read_relay(dtls_client)).Start(); UdpClient receivingUdpClient = new UdpClient(32000); //Creates an IPEndPoint to record the IP Address and port number of the sender. // The IPEndPoint will allow you to read datagrams sent from any source. IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0); /*Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); * * IPAddress broadcast = IPAddress.Parse("127.0.0.1"); * * //byte[] sendbuf = Encoding.ASCII.GetBytes(args[0]); * IPEndPoint ep = new IPEndPoint(broadcast, 11000);*/ dtls_client.GetStream().Write(Encoding.Default.GetBytes("SUCCESS\n")); dtls_client.GetStream().Write(Encoding.Default.GetBytes("SUCCESS\n")); //dtls_client.GetStream().Write(Encoding.Default.GetBytes("SUCCESS")); while (true) { byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint); //dtls_client.GetStream().Write(receiveBytes); //dtls_client.GetStream().Flush(); string input = BitConverter.ToString(receiveBytes) + '\n'; //Console.WriteLine(input); byte[] send = Encoding.Default.GetBytes(input); //Console.WriteLine(receiveBytes); dtls_client.GetStream().Write(send); //Thread.Sleep(50); //byte[] rec = Encoding.Default.GetBytes(cut_str); //Console.WriteLine(bytes); //s.SendTo(bytes, ep); //dtls_client.GetStream().Write(Encoding.Default.GetBytes(input)); /*string input = Encoding.Default.GetString(receiveBytes); * * byte[] send = Encoding.Default.GetBytes(input); * * s.SendTo(send, ep);*/ /*byte[] out_byte = Encoding.Default.GetBytes(input); * * string out_str = Encoding.Default.GetString(out_byte); * * String[] arr=out_str.Split('-'); * byte[] bytes=new byte[arr.Length]; * for(int i=0; i<arr.Length; i++) bytes[i]=Convert.ToByte(arr[i],16); * * s.SendTo(bytes, ep);*/ /*String[] arr=input.Split('-'); * byte[] bytes=new byte[arr.Length]; * for(int i=0; i<arr.Length; i++) bytes[i]=Convert.ToByte(arr[i],16);*/ /*String[] arr_in=input.Split('-'); * byte[] array_in=new byte[arr.Length]; * for(int i=0; i<arr.Length; i++) array[i]=Convert.ToByte(arr[i],16); * * string out_str = BitConverter.ToString(out_bt); * * String[] arr=out_str.Split('-'); * byte[] bytes=new byte[arr.Length]; * for(int i=0; i<arr.Length; i++) bytes[i]=Convert.ToByte(arr[i],16); * * //byte[] bytes = BitConverter.GetBytes(input); * * s.SendTo(bytes, ep);*/ //string input = BitConverter.ToString(receiveBytes); //byte[] encryptedData = EncryptStringToBytes_Aes(BitConverter.ToString(receiveBytes), myAes.Key, myAes.IV); //dtls_client.GetStream().Write(encryptedData); //dtls_client.GetStream().Write(receiveBytes); //dtls_client.GetStream().Write(bytes); //dtls_client.GetStream().Write(); } dtls_client.WaitForExit(); } else if (String.Compare(response, "REJECT") == 0) { Console.WriteLine("Connection rejected"); } } else if (String.Compare(response, "REJECT") == 0) { Console.WriteLine("Connection rejected"); sslStream.Close(); client.Close(); } }
static void listen_connection(SslStream sslStream, TcpClient client) { /*myAes = Aes.Create(); * myAes.Key = new byte[16] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 }; * myAes.IV = new byte[16] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; */ TCPCommunication.send_message_tcp(sslStream, "LISTEN_P"); TCPCommunication.send_message_tcp(sslStream, HashString.GetHashString(pubKey.ToString())); string response = TCPCommunication.recieve_message_tcp(sslStream); if (String.Compare(response, "ACCEPT") == 0) { Console.WriteLine("Start authenticating"); Byte[] data = new Byte[2048]; sslStream.Read(data, 0, data.Length); string message = Encoding.UTF8.GetString(data); string P_str = message; RsaKeyParameters[] P = restructure_P(P_str); Console.WriteLine("P: " + P_str); Console.WriteLine(); data = new Byte[2048]; sslStream.Read(data, 0, data.Length); message = Encoding.UTF8.GetString(data); string X_str = message; byte[][] X = restructure_X(X_str); Console.WriteLine("X: " + X_str); Console.WriteLine(); response = TCPCommunication.recieve_message_tcp(sslStream); string m = response; Console.WriteLine("m: " + m); Console.WriteLine(); data = new Byte[64]; sslStream.Read(data, 0, data.Length); byte[] v = data; Console.WriteLine("v: " + ByteArrayToString(v)); Console.WriteLine(); if (ring_verify(P, v, X, m)) { Console.WriteLine("Authentication success"); } else { Console.WriteLine("Authentication failure"); } /*byte[] data = new Byte[256]; * data = Encoding.UTF8.GetBytes(pubKey.ToString()); * sslStream.Write(data); * sslStream.Flush();*/ /*data = new Byte[256]; * sslStream.Read(data, 0, data.Length); * response = Encoding.UTF8.GetString(data); * PublicKeyCoordinates request_key = JsonConvert.DeserializeObject<PublicKeyCoordinates>(response); * * sslStream.Close(); * client.Close(); * * * * ECDiffieHellmanOpenSsl temp = new ECDiffieHellmanOpenSsl(); * ECParameters epTemp = temp.ExportParameters(false); * * epTemp.Q.X = request_key.X; * epTemp.Q.Y = request_key.Y; * * ECDiffieHellmanPublicKey servePubKey = ECDiffieHellman.Create(epTemp).PublicKey; * byte[] sharedKey = node.DeriveKeyMaterial(servePubKey); * Console.WriteLine(BitConverter.ToString(sharedKey).Replace("-", "")); * * //myAes.Key = sharedKey; * //myAes.Key = new byte[16] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 }; * * * DTLSClient dtls = new DTLSClient(server_ip, server_port.ToString(), new byte[] { 0xBA, 0xA0 }); * * if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) * { * dtls.Unbuffer = "winpty.exe"; * dtls.Unbuffer_Args = "-Xplain -Xallow-non-tty"; * } * else * { * dtls.Unbuffer = "stdbuf"; * dtls.Unbuffer_Args = "-i0 -o0"; * } * dtls.Start(); * * byte[] bytes; * * new Thread(() => read_relay(dtls)).Start(); * * while (true) * { * string input = Console.ReadLine(); * byte[] encryptedData = EncryptStringToBytes_Aes(input, myAes.Key, myAes.IV); * //dtls.GetStream().Write(Encoding.Default.GetBytes(input+Environment.NewLine)); * dtls.GetStream().Write(encryptedData); * } * dtls.WaitForExit();*/ } else if (String.Compare(response, "REJECT") == 0) { Console.WriteLine("Connection rejected"); sslStream.Close(); client.Close(); } }
static void req_connection(SslStream sslStream, TcpClient client, string dest_key) { /*myAes = Aes.Create(); * myAes.Key = new byte[16] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 }; * myAes.IV = new byte[16] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; */ Console.WriteLine("requesting"); TCPCommunication.send_message_tcp(sslStream, "CONNECT_P"); TCPCommunication.send_message_tcp(sslStream, HashString.GetHashString(pubKey.ToString())); string response = TCPCommunication.recieve_message_tcp(sslStream); if (String.Compare(response, "ACCEPT") == 0) { TCPCommunication.send_message_tcp(sslStream, dest_key); response = TCPCommunication.recieve_message_tcp(sslStream); Console.WriteLine(response); if (String.Compare(response, "ACCEPT") == 0) { Console.WriteLine("Start Authenticating"); ring_authenticate(sslStream); /*response = TCPCommunication.recieve_message_tcp(sslStream); * int dtls_port = Int32.Parse(response); * * * byte[] data = new Byte[256]; * data = Encoding.UTF8.GetBytes(pubKey.ToString()); * * sslStream.Write(data); * sslStream.Flush(); * * data = new Byte[256]; * sslStream.Read(data, 0, data.Length); * response = Encoding.UTF8.GetString(data); * PublicKeyCoordinates listen_key = JsonConvert.DeserializeObject<PublicKeyCoordinates>(response); * * * * sslStream.Close(); * client.Close();*/ /*ECDiffieHellmanOpenSsl temp = new ECDiffieHellmanOpenSsl(); * ECParameters epTemp = temp.ExportParameters(false); * * epTemp.Q.X = listen_key.X; * epTemp.Q.Y = listen_key.Y; * * ECDiffieHellmanPublicKey servePubKey = ECDiffieHellman.Create(epTemp).PublicKey; * byte[] sharedKey = node.DeriveKeyMaterial(servePubKey); * Console.WriteLine(BitConverter.ToString(sharedKey).Replace("-", "")); * //myAes.Key = sharedKey; * //myAes.Key = new byte[16] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 }; * * DTLSClient dtls = new DTLSClient(dest_ip, dtls_port.ToString(), new byte[] { 0xBA, 0xA0 }); * * if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) * { * dtls.Unbuffer = "winpty.exe"; * dtls.Unbuffer_Args = "-Xplain -Xallow-non-tty"; * } * else * { * dtls.Unbuffer = "stdbuf"; * dtls.Unbuffer_Args = "-i0 -o0"; * } * dtls.Start(); * * new Thread(() => read_relay(dtls)).Start(); * * while (true) * { * string input = Console.ReadLine(); * byte[] encryptedData = EncryptStringToBytes_Aes(input, myAes.Key, myAes.IV); * //dtls.GetStream().Write(Encoding.Default.GetBytes(input+Environment.NewLine)); * dtls.GetStream().Write(encryptedData); * } * dtls.WaitForExit();*/ } else if (String.Compare(response, "REJECT") == 0) { Console.WriteLine("Connection rejected"); } } else if (String.Compare(response, "REJECT") == 0) { Console.WriteLine("Connection rejected"); sslStream.Close(); client.Close(); } }