/// <summary> /// Initiate new client to connect /// </summary> /// <param name="args"></param> private async void ProcessClientConnection(object args) { TcpClient client = (TcpClient)args; StreamReader sr; StreamWriter sw; try { sr = new StreamReader(client.GetStream(), Encoding.UTF8, true, BUFFER_SIZE); sw = new StreamWriter(client.GetStream(), Encoding.UTF8, BUFFER_SIZE); sw.AutoFlush = true; while (true) { bool testDecrypted = false; Console.Write("Enter AES encryption key: "); string tempKey = "/gUFEqNJ17CV52nJTs1ruB8BsZ8kNBpyDVMJH26AGIE="; //string tempKey = Console.ReadLine(); Console.Write("Enter AES IV: "); string tempIV = "UaTwfDMLvZb2uh96KTKmvQ=="; //string tempIV = Console.ReadLine(); Console.Write("Enter Password: "******"doyouknowdawae"; //string password = Console.ReadLine(); //Load key and IV into constructor EncryptionAES.password = tempKey; EncryptionAES.iv = tempIV; //Create(password); //Response r = Login(password); //while (r.Success == false) //{ // Console.Write("Enter AES encryption key: "); // tempKey = Console.ReadLine(); // Console.Write("Enter AES IV: "); // tempIV = Console.ReadLine(); // Console.Write("Enter Password: "******"true"); //Sending database public key to webserver in plain await sw.FlushAsync(); break; } else { CustomAPI.logging("Incorrect encryption key or IV entered"); } } if (!auth_bypass) { bool wsPublicKeyRetrieved = false; string password = "******"; int rsaKeySize = 1024; string dbPublicKey = null; string dbPrivateKey = null; string wsPublicKey = null; //bool proceed = true; //if (!proceed) //{ // return; //} while (true) { //TODO: Authentication between servers if (!wsPublicKeyRetrieved) { try { Console.WriteLine("Generating a {0} size key ...", rsaKeySize); EncryptionRSA.GenerateKeys(rsaKeySize, out dbPublicKey, out dbPrivateKey); await sw.WriteLineAsync(dbPublicKey); //Sending database public key to webserver in plain await sw.FlushAsync(); wsPublicKey = sr.ReadLine(); Console.WriteLine("Webserver public key received"); wsPublicKeyRetrieved = true; } catch (Exception e) { CustomAPI.logging(e.Message); wsPublicKeyRetrieved = false; } } else if (!keyExchanged) { using (ECDiffieHellmanCng ecd = new ECDiffieHellmanCng()) { ecd.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash; ecd.KeySize = 256; ecd.HashAlgorithm = CngAlgorithm.Sha256; tempKey = ecd.PublicKey.ToByteArray(); //Generate shared secret key string encryptTempKey = EncryptionRSA.Encrypt(Convert.ToBase64String(tempKey), rsaKeySize, wsPublicKey); await sw.WriteLineAsync(encryptTempKey); //Sending database temp key to webserver server await sw.FlushAsync(); string encryptedWsTempKey = sr.ReadLine(); //Retrieve webserver temp key byte[] wsTempKey = Convert.FromBase64String(EncryptionRSA.Decrypt(encryptedWsTempKey, rsaKeySize, dbPrivateKey)); masterKey = ecd.DeriveKeyMaterial(CngKey.Import(wsTempKey, CngKeyBlobFormat.EccPublicBlob)); CustomAPI.readContent(masterKey); Console.Write("Master Key Generated"); iv = Convert.FromBase64String(sr.ReadLine()); //IV Generated from webserver set to here keyExchanged = true; } } else { break; //Configuration between servers have been completed } } } if (keyExchanged) { ListeningToClient(sr, sw); } } catch (Exception e) { Console.WriteLine(e); if (client != null) { client.Close(); } } }
/// <summary> /// Webserver attempts to connect to the database /// </summary> public static void RegisterDataAccess() { //TODO: Key exchange between database server and webserver try { client = new TcpClient("127.0.0.1", 3351); sr = new StreamReader(client.GetStream(), Encoding.UTF8, true, BUFFER_SIZE); sw = new StreamWriter(client.GetStream(), Encoding.UTF8, BUFFER_SIZE); sw.AutoFlush = true; Debug.WriteLine("Waiting for database server"); bool proceed = false; while (!proceed) { proceed = Convert.ToBoolean(sr.ReadLine()); Debug.WriteLine("WAITING"); } if (!auth_bypass) { bool dbPublicKeyRetrieved = false; bool keyExchanged = false; string password = "******"; //Key in from user int rsaKeySize = 1024; string wsPublicKey = null; string wsPrivateKey = null; string dbPublicKey = null; while (true) { //TODO: Authentication between servers if (!dbPublicKeyRetrieved) { Debug.WriteLine("Generating a {0} size key", rsaKeySize); EncryptionRSA.GenerateKeys(rsaKeySize, out wsPublicKey, out wsPrivateKey); sw.WriteLine(wsPublicKey); //Sending database public key to webserver in plain sw.Flush(); dbPublicKey = sr.ReadLine(); dbPublicKeyRetrieved = true; } else if (!keyExchanged) { using (ECDiffieHellmanCng ecd = new ECDiffieHellmanCng()) { ecd.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash; ecd.KeySize = 256; ecd.HashAlgorithm = CngAlgorithm.Sha256; tempKey = ecd.PublicKey.ToByteArray(); //Generate shared secret key string encryptTempKey = EncryptionRSA.Encrypt(Convert.ToBase64String(tempKey), rsaKeySize, dbPublicKey); sw.WriteLine(encryptTempKey); //Sending database temp key to webserver server sw.Flush(); string encryptDbTempKey = sr.ReadLine(); //Retrieve webserver temp key byte[] dbTempKey = Convert.FromBase64String(EncryptionRSA.Decrypt(encryptDbTempKey, rsaKeySize, wsPrivateKey)); masterKey = ecd.DeriveKeyMaterial(CngKey.Import(dbTempKey, CngKeyBlobFormat.EccPublicBlob)); CustomAPI.readContent(masterKey); Debug.WriteLine("Master Key Generated"); //Generate an new IV using (Aes aes = new AesCryptoServiceProvider()) { iv = aes.IV; } sw.WriteLine(Convert.ToBase64String(iv)); sw.Flush(); keyExchanged = true; } } else { break; } } } sw.AutoFlush = false; } catch (Exception e) { CustomAPI.logging(e.Message); } }