예제 #1
0
        public void SaveLoadMessageCryptoContainer()
        {
            // arrange
            const string refLogin    = "******";
            const string refPassword = "******";
            var          msg         = new ClAuthRespond
            {
                Login    = refLogin,
                Password = refPassword,
            };

            using (var csp = CryptoProviderFactory.Instance.CreateSymmetric(SdmSymmetricAlgorithm.AES))
            {
                using (var container = new MessageCryptoContainer())
                {
                    foreach (var p in protocols)
                    {
                        // act
                        container.Store(msg, csp, p);
                        MultiprotocolSaveLoad(container, () =>
                        {
                            var emsg = (ClAuthRespond)container.Extract(msg.Id, csp, p);
                            // assert
                            Assert.AreEqual(emsg.Login, refLogin);
                            Assert.AreEqual(emsg.Password, refPassword);
                        });
                    }
                }
            }
        }
예제 #2
0
        private bool ReceiveMessage(MsgHeader hdr, out IMessage msg)
        {
            msg = null;
            // XXX: could be optimized - use one large buffer + unclosable MemoryStream
            var buf = new byte[hdr.Size];

            try
            {
                netStream.Read(buf, 0, buf.Length);
            }
            catch (IOException e)
            {
                if (NetUtil.CheckConnectionReset(e))
                {
                    OnServerConnectionReset();
                    return(false);
                }
                throw;
            }
            using (var ms = new MemoryStream(buf))
            {
                var msWrap = ms.AsUnclosable();
                if ((hdr.Flags & MessageFlags.Secure) == MessageFlags.Secure)
                {
                    using (var container = new MessageCryptoContainer())
                    {
                        container.Load(msWrap, cfg.Protocol);
                        symCp.Key = sessionKey;
                        msg       = container.Extract(hdr.Id, symCp, cfg.Protocol);
                    }
                }
                else
                {
                    msg = MessageFactory.CreateMessage(hdr.Id);
                    try
                    {
                        msg.Load(msWrap, cfg.Protocol);
                    }
                    catch (MessageLoadException e)
                    {
                        Root.Log(LogLevel.Warning, "Client: bad message received from server ({0})", e.Message);
                        Disconnect();
                        return(false);
                    }
                }
            }
            return(true);
        }