public byte[] EncodeToProtocol(Level level, byte[] timestamp, string hostName, string serviceName, string message, byte[] initVector)
        {
            byte[] bufOut = null;
            using (var stream = new MemoryStream(16 + HOST_NAME_SIZE + SERVICE_NAME_SIZE + PLUGIN_OUTPUT_SIZE))
            {
                stream.WriteShort(Nsca_VERSION); //bytes 0-1
                stream.WriteShort(0); //bytes 2-3
                stream.WriteInt(0); //bytes 4-8
                stream.Write(timestamp, 0, 4); //bytes 9-13
                stream.WriteShort((short)level); //bytes 14-15
                stream.WriteFixedString(hostName, HOST_NAME_SIZE);
                if (string.IsNullOrWhiteSpace(serviceName) == false)
                    stream.WriteFixedString(serviceName, SERVICE_NAME_SIZE); // process service check result
                else
                    stream.WriteFixedString("\x0", SERVICE_NAME_SIZE); // process host check result
                stream.WriteFixedString(message, PLUGIN_OUTPUT_SIZE);
                stream.WriteShort(0);

                int hash = CRC32.Compute(stream.ToArray());
                stream.Position = 4;
                stream.WriteInt(hash);

                var encryptor = EncryptorFactory.CreateEncryptor(settings.EncryptionType);
                bufOut = encryptor.Encrypt(stream.ToArray(), initVector, settings.Password);
            }
            return bufOut;
        }
Пример #2
0
 public static MemoryStream NewPkt(PktType p)
 {
     var ms = new MemoryStream();
     ms.WriteShort(_Header);
     ms.WriteShort((int)p);
     ms.WriteInt((int)ms.Length - HeadLength);
     return ms;
 }
        public byte[] EncodeToProtocol(Level level, byte[] timestamp, string hostName, string serviceName, string message, byte[] initVector)
        {
            using (var stream = new MemoryStream(16 + HOST_NAME_SIZE + SERVICE_NAME_SIZE + PLUGIN_OUTPUT_SIZE))
            {
                stream.WriteShort(NSCA_VERSION); //bytes 0-1
                stream.WriteShort(0); //bytes 2-3
                stream.WriteInt(0); //bytes 4-8
                stream.Write(timestamp, 0, 4); //bytes 9-13
                stream.WriteShort((short) level); //bytes 14-15
                stream.WriteFixedString(hostName, HOST_NAME_SIZE);
                stream.WriteFixedString(serviceName, SERVICE_NAME_SIZE);
                stream.WriteFixedString(message, PLUGIN_OUTPUT_SIZE);
                stream.WriteShort(0);

                int hash = CRC32.Compute(stream.ToArray());
                stream.Position = 4;
                stream.WriteInt(hash);

                var encryptor = EncryptorFactory.CreateEncryptor(settings.EncryptionType);
                return encryptor.Encrypt(stream.ToArray(), initVector, settings.Password);
            }
        }
Пример #4
0
        public override void Encode(Stream stream)
        {
            using (var body = new MemoryStream())
            {
                body.WriteShort(MessageIdentifier);

                foreach (var item in topics)
                {
                    body.WriteString(item);
                }

                FixedHeader.RemaingLength = (int)body.Length;
                FixedHeader.WriteTo(stream);
                body.WriteTo(stream);
            }
        }
        public static byte[] Serialize(this IColumnSpec columnSpec, object data)
        {
            byte[] rawData;
            switch (columnSpec.ColumnType)
            {
                case ColumnType.List:
                case ColumnType.Set:
                    var colType = columnSpec.CollectionValueType.ToType();
                    Type typedColl = typeof(CollectionAccessor<>).MakeGenericType(colType);
                    ICollectionAccessor coll = (ICollectionAccessor) Activator.CreateInstance(typedColl, data);
                    using (MemoryStream ms = new MemoryStream())
                    {
                        ms.WriteShort((short) coll.Count);
                        foreach (object elem in coll)
                        {
                            byte[] rawDataElem = Serialize(columnSpec.CollectionValueType, elem);
                            ms.WriteShortByteArray(rawDataElem);
                        }
                        rawData = ms.ToArray();
                    }
                    break;

                case ColumnType.Map:
                    IDictionary map = (IDictionary) data;
                    using (MemoryStream ms = new MemoryStream())
                    {
                        ms.WriteShort((short) map.Count);
                        foreach (DictionaryEntry de in map)
                        {
                            byte[] rawDataKey = Serialize(columnSpec.CollectionKeyType, de.Key);
                            ms.WriteShortByteArray(rawDataKey);
                            byte[] rawDataValue = Serialize(columnSpec.CollectionValueType, de.Value);
                            ms.WriteShortByteArray(rawDataValue);
                        }
                        rawData = ms.ToArray();
                    }
                    break;

                default:
                    rawData = Serialize(columnSpec.ColumnType, data);
                    break;
            }

            return rawData;
        }
Пример #6
0
        public void TestStreamshort()
        {
            short obj = 100;

            using (MemoryStream ms = new MemoryStream())
            {
                ms.WriteShort(obj);
                ms.Seek(0, SeekOrigin.Begin);
                short res = ms.ReadShort();

                Assert.IsTrue(obj.Equals(res), "Streaming failed!");
            }
        }
Пример #7
0
        public static byte[] Serialize(this IColumnSpec columnSpec, object data)
        {
            byte[] rawData;
            switch (columnSpec.ColumnType)
            {
                case ColumnType.List:
                case ColumnType.Set:
                    ICollection coll = (ICollection) data;
                    using (MemoryStream ms = new MemoryStream())
                    {
                        ms.WriteShort((short) coll.Count);
                        foreach (object elem in coll)
                        {
                            byte[] rawDataElem = Serialize(columnSpec.CollectionValueType, elem);
                            ms.WriteShortByteArray(rawDataElem);
                        }
                        rawData = ms.ToArray();
                    }
                    break;

                case ColumnType.Map:
                    IDictionary map = (IDictionary) data;
                    using (MemoryStream ms = new MemoryStream())
                    {
                        ms.WriteShort((short) map.Count);
                        foreach (DictionaryEntry de in map)
                        {
                            byte[] rawDataKey = Serialize(columnSpec.CollectionKeyType, de.Key);
                            ms.WriteShortByteArray(rawDataKey);
                            byte[] rawDataValue = Serialize(columnSpec.CollectionValueType, de.Value);
                            ms.WriteShortByteArray(rawDataValue);
                        }
                        rawData = ms.ToArray();
                    }
                    break;

                default:
                    rawData = Serialize(columnSpec.ColumnType, data);
                    break;
            }

            return rawData;
        }
Пример #8
0
        public static Message DecodeMessage2(Stream sr2, RSAParameters rsaprv, byte[] mypub)
        {
            Message msg = new Message();

            if(false)
            {
                var ms4 = new MemoryStream();
                var buf = new byte[4096];
                int len2;
                while((len2=sr2.Read(buf, 0, buf.Length))>0){
                    ms4.Write(buf, 0, len2);
                }
                var data = ms4.ToArray();
                System.Diagnostics.Trace.WriteLine(BitConverter.ToString(data));
                System.Diagnostics.Trace.WriteLine("Pub: " + BitConverter.ToString(mypub));
                sr2 = new MemoryStream(data);
            }

            var flags = sr2.ReadByte();
            var compressText = (flags & 1) != 0;
            var SignMessage = (flags & 2) != 0;

            sr2.Read(msg.msgid, 0, msg.msgid.Length);
            sr2.Read(msg.replyTo, 0, msg.replyTo.Length);

            var len = sr2.ReadShort();
            var szbuf = new byte[len];
            sr2.Read(szbuf, 0, szbuf.Length);
            if (compressText == false)
            {
                msg.msg = Encoding.UTF8.GetString(szbuf);
            }
            else
            {
                var b = new Byte[1024 * 4];
                using (var dec_sr = new BZip2InputStream(new MemoryStream(szbuf)))
                using (var ms2 = new MemoryStream())
                {
                    var readlen = 0;
                    while ((readlen = dec_sr.Read(b, 0, b.Length)) > 0)
                    {
                        ms2.Write(b, 0, readlen);
                    }
                    ms2.Flush();
                    msg.msg = Encoding.UTF8.GetString(ms2.ToArray());
                }
            }
            if (SignMessage)
            {
                var theirkeylen = sr2.ReadShort();
                var theirpubkey = new Byte[theirkeylen];
                sr2.Read(theirpubkey, 0, theirpubkey.Length);
                var hashlen = sr2.ReadShort();
                var hash = new Byte[hashlen];
                sr2.Read(hash, 0, hash.Length);

                //System.Diagnostics.Trace.WriteLine("Hash: " +BitConverter.ToString(hash));

                using (var ms2 = new MemoryStream())
                using (var rsa = new RSACryptoServiceProvider())
                {
                    ms2.Write(msg.msgid, 0, msg.msgid.Length);
                    ms2.Write(msg.replyTo, 0, msg.replyTo.Length);
                    var msgarr = Encoding.UTF8.GetBytes(msg.msg);
                    ms2.WriteShort(msgarr.Length);
                    ms2.Write(msgarr, 0, msgarr.Length);
                    ms2.WriteShort(theirpubkey.Length);
                    ms2.Write(theirpubkey, 0, theirpubkey.Length);
                    ms2.WriteShort(mypub.Length);
                    ms2.Write(mypub, 0, mypub.Length);
                    RSAParameters rsap2;
                    Shared.LoadKey2(Shared.pubToPem(theirpubkey), null, out rsap2);
                    rsa.ImportParameters(rsap2);
                    var b = rsa.VerifyData(ms2.ToArray(), SHA512OID, hash);
                    if (b)
                        msg.signed = true;
                    else
                        msg.forged = true;
                }
                msg.their_pubkey = theirpubkey;
            }

            return msg;
        }
Пример #9
0
        byte[] EncodeMessage(byte[] recipient_pubkey, byte[] msgid, byte[] replyTo, string txt, byte[] prvkey, byte[] pubkey, out byte[] aes_key, out byte[] aes_iv)
        {
            if (replyTo == null)
                replyTo = new byte[16];

            var txtbuf = Encoding.UTF8.GetBytes(txt);
            var SignMessage = prvkey != null;
            byte[] hash = null;
            if (SignMessage)
            {
                using (var rsa = new RSACryptoServiceProvider())
                {
                    RSAParameters rsap;
                    Shared.LoadKey2(Shared.prvToPem(prvkey), null, out rsap);
                    rsa.ImportParameters(rsap);
                    using (var ms = new MemoryStream()) //sign
                    {
                        ms.Write(msgid, 0, msgid.Length);
                        ms.Write(replyTo, 0, replyTo.Length);
                        ms.WriteShort((short)txtbuf.Length);
                        ms.Write(txtbuf, 0, txtbuf.Length);
                        ms.WriteShort((short)pubkey.Length);
                        ms.Write(pubkey, 0, pubkey.Length);
                        ms.WriteShort((short)recipient_pubkey.Length);
                        ms.Write(recipient_pubkey, 0, recipient_pubkey.Length);
                        ms.Position = 0;
                        hash = rsa.SignData(ms, SHA512OID);
                    }
                }
            }

            byte[] c1;
            using (var ms1 = new MemoryStream())
            using (var ms = new BZip2OutputStream(ms1))
            {
                ms.Write(txtbuf, 0, txtbuf.Length);
                ms.Close();
                c1 = ms1.ToArray();
            }

            var compressText = c1.Length < txtbuf.Length;

            byte[] aesmsg;
            using (var aes = new RijndaelManaged())
            {
                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (var encryptor = aes.CreateEncryptor())
                    using (CryptoStream sw2 = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        aes_key = aes.Key;
                        aes_iv = aes.IV;
                        sw2.WriteByte((Byte)((compressText ? 1 : 0) | (SignMessage ? 2 : 0)));
                        sw2.Write(msgid, 0, msgid.Length);
                        sw2.Write(replyTo, 0, replyTo.Length);
                        if (compressText)
                        {
                            sw2.WriteShort((short)c1.Length);
                            sw2.Write(c1, 0, c1.Length);
                        }
                        else
                        {
                            sw2.WriteShort((short)txtbuf.Length);
                            sw2.Write(txtbuf, 0, txtbuf.Length);
                        }
                        if (SignMessage)
                        {
                            sw2.WriteShort((short)pubkey.Length);
                            sw2.Write(pubkey, 0, pubkey.Length);
                            sw2.WriteShort((short)hash.Length);
                            sw2.Write(hash, 0, hash.Length);
                        }
                    }
                    msEncrypt.Flush();
                    aesmsg = msEncrypt.ToArray();
                }
            }
            return aesmsg;
        }
Пример #10
0
        public static byte[] Serialize(this CqlColumn cqlColumn, object data)
        {
            //null value check
            if (data == null)
                return null;

            byte[] rawData;
            switch (cqlColumn.CqlType)
            {
                case CqlType.List:
                case CqlType.Set:
                    if (!cqlColumn.CollectionValueType.HasValue)
                        throw new CqlException("CqlColumn collection type must has its value type set");

                    var coll = (IEnumerable)data;
                    using (var ms = new MemoryStream())
                    {
                        //write length placeholder
                        ms.Position = 2;
                        short count = 0;
                        foreach (object elem in coll)
                        {
                            byte[] rawDataElem = Serialize(cqlColumn.CollectionValueType.Value, elem);
                            ms.WriteShortByteArray(rawDataElem);
                            count++;
                        }
                        ms.Position = 0;
                        ms.WriteShort(count);
                        rawData = ms.ToArray();
                    }
                    break;

                case CqlType.Map:

                    if (!cqlColumn.CollectionKeyType.HasValue)
                        throw new CqlException("CqlColumn map type must has its key type set");

                    if (!cqlColumn.CollectionValueType.HasValue)
                        throw new CqlException("CqlColumn map type must has its value type set");

                    var map = (IDictionary)data;
                    using (var ms = new MemoryStream())
                    {
                        ms.WriteShort((short)map.Count);
                        foreach (DictionaryEntry de in map)
                        {
                            byte[] rawDataKey = Serialize(cqlColumn.CollectionKeyType.Value, de.Key);
                            ms.WriteShortByteArray(rawDataKey);
                            byte[] rawDataValue = Serialize(cqlColumn.CollectionValueType.Value, de.Value);
                            ms.WriteShortByteArray(rawDataValue);
                        }
                        rawData = ms.ToArray();
                    }
                    break;

                default:
                    rawData = Serialize(cqlColumn.CqlType, data);
                    break;
            }

            return rawData;
        }
Пример #11
0
        public override void Encode(Stream stream)
        {
            using (var body = new MemoryStream())
            {
                //variable header
                body.WriteString(ProtocolName);       //byte 1 - 8
                body.WriteByte(ProtocolVersion);      //byte 9

                //ConnectFlags.WriteTo(body);         //byte 10
                var flags = UsernameFlag.ToByte() << 7;
                flags |= PasswordFlag.ToByte() << 6;
                flags |= WillRetain.ToByte() << 5;
                flags |= ((byte)WillQos) << 3;
                flags |= WillFlag.ToByte() << 2;
                flags |=  CleanSession.ToByte() << 1;
                body.WriteByte((byte)flags);

                //keep alive
                body.WriteShort(KeepAlive);      //byte 11 - 12

                //payload
                body.WriteString(ClientId);
                if (WillFlag)
                {
                    body.WriteString(WillTopic);
                    body.WriteString(WillMessage);
                }
                if (UsernameFlag)
                    body.WriteString(Username);
                if (PasswordFlag)
                    body.WriteString(Password);

                FixedHeader.RemaingLength = (int)body.Length;
                FixedHeader.WriteTo(stream);
                body.WriteTo(stream);
            }
        }