public void HttpService050Test() { using (var clientChannel = new HttpServiceChannel(new Uri("http://192.168.0.235:5000/api/safevault"))) { clientChannel.CipherLib["rsa-public"] = RsaCipher.LoadFromPEM($"data\\server\\server.pem"); clientChannel.CipherLib["rsa-private"] = RsaCipher.LoadFromPEM($"data\\client\\test-user\\cer.pem", $"data\\client\\test-user\\cer.pem.key"); var dbxData = Random.Get(3838); var qm = new QueryMessage { Command = "dbx-Upload" }; qm.Params["username"] = "******"; qm.Params["uuid"] = "safevault"; qm.Params["password"] = "******"; qm.Params["md5"] = Security.Hash.MD5(dbxData); qm.Params["last-modified"] = "2017-01-01 12:00:00Z"; clientChannel.Encrypt(); clientChannel.WriteObject(qm); clientChannel.Write(dbxData); clientChannel.Post(); var response = clientChannel.ReadObject <ResponseMessage>(); Console.WriteLine(response.StatusCode + " " + response.StatusText); Assert.AreEqual(200, response.StatusCode); Console.WriteLine(response.Header["data"]); } }
public void ServiceChannel070Test() { var location = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); using (var stream1 = new MemoryStream()) using (var stream2 = new MemoryStream()) using (var clientChannel = new ServiceChannel()) using (var serviceChannel = new ServiceChannel()) { serviceChannel.SetReadStream(stream1, false); serviceChannel.CipherLib["rsa-private"] = RsaCipher.LoadFromPEM($"{location}\\data\\server\\server.pem", $"{location}\\data\\server\\server.pem.key"); clientChannel.SetWriteStream(stream2, false); clientChannel.CipherLib["rsa-public"] = RsaCipher.LoadFromPEM($"{location}\\data\\server\\server.pem"); clientChannel.Encrypt(); var data = Security.Random.Get(128 * 1024 + 1); clientChannel.Encrypt(); clientChannel.Write(data); clientChannel.Flush(); Console.WriteLine(stream2.Length); TransferData(stream2, stream1); var data1 = serviceChannel.Read(); Assert.AreEqual(Hash.MD5(data), Hash.MD5(data1)); } }
public void HttpService040Test() { using (var clientChannel = new HttpServiceChannel(new Uri("http://192.168.0.235:5000/api/safevault"))) { clientChannel.CipherLib["rsa-public"] = RsaCipher.LoadFromPEM($"data\\server\\server.pem"); clientChannel.CipherLib["rsa-private"] = RsaCipher.LoadFromX509Store("Evgeny Zyuzin SafeVault Client"); var qm = new QueryMessage { Command = "dbx-Download" }; qm.Params["username"] = "******"; qm.Params["uuid"] = "safevault"; qm.Params["password"] = "******"; clientChannel.Encrypt(); clientChannel.WriteObject(qm); clientChannel.Post(); var response = clientChannel.ReadObject <ResponseMessage>(); Console.WriteLine(response.StatusCode + " " + response.StatusText); Assert.AreEqual(200, response.StatusCode); Console.WriteLine(response.Header["data"]); var data = clientChannel.Read(); Assert.AreEqual(response.Header["md5"], Security.Hash.MD5(data)); Console.WriteLine(data.Length); } }
public ICipher GetCertificate(string certificatePath) { var cert = _cache.GetOrAdd(certificatePath, s => { if (global::System.IO.File.Exists(certificatePath) == false) { return(null); } var privateKeyPath = $"{certificatePath}.key"; if (global::System.IO.File.Exists(privateKeyPath) == false) { privateKeyPath = null; } return(RsaCipher.LoadFromPEM(certificatePath, privateKeyPath)); }); if (cert == null) { throw new FileNotFoundException("Certificate not Found"); } return(cert); }
public void DbxGetKeyCommandTest010() { using (var stream1 = new MemoryStream()) using (var channel1 = new ServiceChannel()) using (var channel2 = new ServiceChannel()) { channel1.SetWriteStream(stream1, canDispose: false); Context ctx = new Context(); ctx.ClientIP = IPAddress.None; ctx.Channel = channel1; ctx.Query = new QueryMessage { Command = "dbx-GetKey" }; ctx.Query.Params["username"] = "******"; ctx.Query.Params["uuid"] = "safevault"; ctx.Query.Params["password"] = OneTimePassword.Get(Base32.Decode("12345678"), 0); Command.Process(ctx); stream1.Position = 0; channel2.SetReadStream(stream1, canDispose: false); channel2.CipherLib["rsa-private"] = RsaCipher .LoadFromPEM($"{_location}/data/client/test-user/cer.pem", $"{_location}/data/client/test-user/cer.pem.key"); var response = channel2.ReadObject <ResponseMessage>(); Assert.AreEqual(200, response.StatusCode); var data = response.Header["data"]; Assert.AreEqual("1234567801234567890abcdefghiklmnopqvwxyz12345678012345678901234567890=", data); } }
public void ServiceChannel060Test() { var location = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); using (var stream1 = new MemoryStream()) using (var stream2 = new MemoryStream()) using (var clientChannel = new ServiceChannel()) using (var serviceChannel = new ServiceChannel()) { serviceChannel.SetReadStream(stream1, false); //serviceChannel.CipherLib["rsa-private"] = RsaCipher.LoadFromPEM($"{location}\\data\\server\\server.pem", $"{location}\\data\\server\\server.pem.key"); clientChannel.SetWriteStream(stream2, false); clientChannel.Encrypt(); clientChannel.WriteObject("HELO"); clientChannel.CipherLib["rsa-public"] = RsaCipher.LoadFromPEM($"{location}\\data\\server\\server.pem"); clientChannel.Encrypt(); clientChannel.WriteObject("HELO2"); clientChannel.Flush(); Console.WriteLine(stream2.Length); TransferData(stream2, stream1); var msg = serviceChannel.ReadObject <string>(); Assert.AreEqual("HELO", msg); Assert.Catch <SecureChannelException>(() => { var msg2 = serviceChannel.ReadObject <string>(); }); } }
public void RsaCipherTest() { var location = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); using (var cipher1 = RsaCipher.LoadFromPEM($"{location}\\data\\client\\test-user\\cer.pem")) using (var cipher2 = RsaCipher.LoadFromPEM($"{location}\\data\\client\\test-user\\cer.pem", $"{location}\\data\\client\\test-user\\cer.pem.key")) { var content = "password"; var passwordEnc = cipher1.Encrypt(Encoding.UTF8.GetBytes(content)); var password = Encoding.UTF8.GetString(cipher2.Decrypt(passwordEnc)); Assert.AreEqual(content, password); } }
public void RsaCipher020Test() { var location = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); using (var cipher1 = RsaCipher.LoadFromPEM($"{location}\\data\\client\\test-user\\cer.pem")) using (var cipher2 = RsaCipher.LoadFromPEM($"{location}\\data\\client\\test-user\\cer.pem", $"{location}\\data\\client\\test-user\\cer.pem.key")) { var content = Random.Get(32); var contentEnc = cipher1.Encrypt(content); var contentDec = cipher2.Decrypt(contentEnc); Assert.AreEqual(content, contentDec); } }
public void DbxUploadCommandTest010() { if (Directory.Exists($"{_location}/data/client/test-user/dbx")) { Directory.Delete($"{_location}/data/client/test-user/dbx", true); } using (var stream1 = new MemoryStream()) using (var stream2 = new MemoryStream()) using (var channel1 = new ServiceChannel()) using (var channel2 = new ServiceChannel()) { byte[] dbxData = Random.Get(256); channel2.SetReadStream(stream1, canDispose: false); channel2.SetWriteStream(stream2, canDispose: false); channel2.Write(dbxData); stream2.Position = 0; channel1.SetReadStream(stream2, canDispose: false); channel1.SetWriteStream(stream1, canDispose: false); Context ctx = new Context(); ctx.ClientIP = IPAddress.None; ctx.Channel = channel1; ctx.Query = new QueryMessage { Command = "dbx-Upload" }; ctx.Query.Params["username"] = "******"; ctx.Query.Params["uuid"] = "safevault"; ctx.Query.Params["password"] = "******"; ctx.Query.Params["md5"] = Hash.MD5(dbxData); ctx.Query.Params["last-modified"] = "2017-01-01 12:00:00Z"; Command.Process(ctx); stream1.Position = 0; channel2.CipherLib["rsa-private"] = RsaCipher .LoadFromPEM($"{_location}/data/client/test-user/cer.pem", $"{_location}/data/client/test-user/cer.pem.key"); var response = channel2.ReadObject <ResponseMessage>(); Assert.AreEqual(200, response.StatusCode); Assert.AreEqual("OK", response.Header["data"]); var data = File.ReadAllBytes($"{_location}/data/client/test-user/dbx/safevault.dbx"); Assert.AreEqual(dbxData, data); var fileInfo = new FileInfo($"{_location}/data/client/test-user/dbx/safevault.dbx"); Assert.AreEqual(fileInfo.CreationTime, DateTime.Parse(ctx.Query.Params["last-modified"])); } }
public void ServiceChannel010Test() { var location = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); using (var stream1 = new MemoryStream()) using (var stream2 = new MemoryStream()) using (var stream11 = new MemoryStream()) using (var stream21 = new MemoryStream()) using (var clientChannel = new ServiceChannel()) using (var serviceChannel = new ServiceChannel()) { serviceChannel.SetReadStream(stream1, false); serviceChannel.CipherLib["rsa-private"] = RsaCipher.LoadFromPEM($"{location}\\data\\server\\server.pem", $"{location}\\data\\server\\server.pem.key"); clientChannel.SetWriteStream(stream2, false); clientChannel.CipherLib["rsa-public"] = RsaCipher.LoadFromPEM($"{location}\\data\\server\\server.pem"); //clientChannel.CipherLib["rsa-private"] = RsaCipher.LoadFromPEM($"{location}\\data\\client\\test-user\\cer.pem", $"{location}\\data\\client\\test-user\\cer.pem.key"); clientChannel.Encrypt(); clientChannel.WriteObject("HELO"); clientChannel.Flush(); clientChannel.SetReadStream(stream21, false); Console.WriteLine(stream2.Length); TransferData(stream2, stream1); var value = serviceChannel.ReadObject <string>(); Console.WriteLine(value); Assert.AreEqual("HELO", value); //clientChannel.CipherLib["rsa-public"] = RsaCipher.LoadFromPEM($"{location}\\data\\client\\test-user\\cer.pem"); serviceChannel.SetWriteStream(stream11, false); serviceChannel.Encrypt(); serviceChannel.WriteObject("EHLO"); serviceChannel.Flush(); Console.WriteLine(stream11.Length); TransferData(stream11, stream21); Console.WriteLine(string.Join(" ", stream21.ToArray().Select(m => $"{m:X2}").ToArray())); var value1 = clientChannel.ReadObject <string>(); Console.WriteLine(value1); Assert.AreEqual("EHLO", value1); } }
public void HttpService020Test() { using (var clientChannel = new HttpServiceChannel(new Uri("http://192.168.0.235:5000/api/safevault"))) { clientChannel.CipherLib["rsa-public"] = RsaCipher.LoadFromPEM("data\\server\\server.pem"); clientChannel.CipherLib["rsa-private"] = RsaCipher.LoadFromPEM("data\\client\\test-user\\cer.pem", "data\\client\\test-user\\cer.pem.key"); var queryMessage = new QueryMessage(); queryMessage.Command = "ping"; clientChannel.Encrypt(); clientChannel.WriteObject(queryMessage); clientChannel.Post(); var response = clientChannel.ReadObject <ResponseMessage>(); Console.WriteLine(response.StatusCode); Console.WriteLine(response.StatusText); Console.WriteLine(response.Header["data"]); Assert.AreEqual(200, response.StatusCode); //Console.WriteLine(response.Header["data"]); } }
public void DbxSetKeyCommandTest010() { string newValue = Guid.NewGuid().ToString(); if (File.Exists($"{_location}/data/client/test-user/vault.conf.bak")) { File.Delete($"{_location}/data/client/test-user/vault.conf.bak"); } File.Copy($"{_location}/data/client/test-user/vault.conf", $"{_location}/data/client/test-user/vault.conf.bak"); try { using (var stream1 = new MemoryStream()) using (var channel1 = new ServiceChannel()) using (var channel2 = new ServiceChannel()) { channel1.SetWriteStream(stream1, canDispose: false); Context ctx = new Context(); ctx.ClientIP = IPAddress.None; ctx.Channel = channel1; ctx.Query = new QueryMessage { Command = "dbx-SetKey" }; ctx.Query.Params["username"] = "******"; ctx.Query.Params["uuid"] = "safevault"; ctx.Query.Params["password"] = OneTimePassword.Get(Base32.Decode("12345678"), 0); ctx.Query.Params["value"] = newValue; Command.Process(ctx); stream1.Position = 0; channel2.SetReadStream(stream1, canDispose: false); channel2.CipherLib["rsa-private"] = RsaCipher .LoadFromPEM($"{_location}/data/client/test-user/cer.pem", $"{_location}/data/client/test-user/cer.pem.key"); var response = channel2.ReadObject <ResponseMessage>(); Assert.AreEqual(200, response.StatusCode); var data = response.Header["data"]; Assert.AreEqual("OK", data); } Unity.Resolve <TokenList>().Reset(); using (var stream1 = new MemoryStream()) using (var channel1 = new ServiceChannel()) using (var channel2 = new ServiceChannel()) { channel1.SetWriteStream(stream1, canDispose: false); Context ctx = new Context(); ctx.ClientIP = IPAddress.None; ctx.Channel = channel1; ctx.Query = new QueryMessage { Command = "dbx-GetKey" }; ctx.Query.Params["username"] = "******"; ctx.Query.Params["uuid"] = "safevault"; ctx.Query.Params["password"] = OneTimePassword.Get(Base32.Decode("12345678"), 0); Command.Process(ctx); stream1.Position = 0; channel2.SetReadStream(stream1, canDispose: false); channel2.CipherLib["rsa-private"] = RsaCipher .LoadFromPEM($"{_location}/data/client/test-user/cer.pem", $"{_location}/data/client/test-user/cer.pem.key"); var response = channel2.ReadObject <ResponseMessage>(); Assert.AreEqual(200, response.StatusCode); var data = response.Header["data"]; Assert.AreEqual(newValue, data); } } finally { File.Copy($"{_location}/data/client/test-user/vault.conf.bak", $"{_location}/data/client/test-user/vault.conf", true); File.Delete($"{_location}/data/client/test-user/vault.conf.bak"); } }