public SMSInitialMessage(IMORZEAccount account, IMORZEContact contact)
        {
            BufferBuilder bb = new BufferBuilder();

            m_account = account;
            bb.AddByte(1);//- init message
            m_newExt = contact.getInitalData();
            bb.AddByte((byte)m_newExt.HashID);
            bb.AddBytes(m_newExt.Ext);

            bb.AddByte((byte)m_newExt.SyncID);
            bb.AddBytes(m_newExt.SyncKey);
            bb.AddBytes(m_newExt.SyncIV);
            bb.AddUshort(0);//или CRC16
            byte [] data = bb.GetAllBytes();
            data = contact.EncryptPK(data);
            byte[] addressdata = m_newExt.SyncEncrypt(account.GetMyAccount());
            bb = new BufferBuilder();

            bb.AddByte(contact.XOR((byte)data.Length, 0x10));
            bb.AddBytes(data);

            bb.AddBytes(addressdata);
            AddMessageBody(bb.GetAllBytes(), m_newExt.HashID, null);
        }
Exemplo n.º 2
0
        public void AddMessageBody(byte [] msgBody, SMSHash hash, byte [] ext)
        {
            BufferBuilder bb = new BufferBuilder();

            bb.AddByte(0x32); //TTL
            byte idhash = (byte)hash;

            if (idhash != 0)
            {
                if (msgBody.Length < 0xFFFF)
                {
                    byte[] bthash = null;
                    switch (hash)
                    {
                    case SMSHash.MD5:
                        bthash = calcMD5(msgBody);
                        break;

                    default:
                        throw new Exception("Unsupported hash algorithm");
                    }
                    if (ext != null)
                    {
                        byte e = 0x80;
                        idhash |= e;
                        if (ext.Length != bthash.Length)
                        {
                            throw new Exception("Invalid ext");
                        }
                    }
                    bb.AddByte(idhash);

                    bb.AddBytes(bthash);
                    if (ext != null)
                    {
                        bb.AddBytes(ext);
                    }
                    bb.AddUshort((ushort)msgBody.Length);
                    bb.AddBytes(msgBody);
                    if (m_bodies == null)
                    {
                        m_bodies = new List <byte[]>();
                    }
                    m_bodies.Add(bb.GetAllBytes());
                }
                else
                {
                    throw new Exception("Unsupported length message");
                }
            }
            else
            {
                throw new Exception("did't select hash algorithm");
            }
        }
Exemplo n.º 3
0
 public virtual void Send(Stream sm)
 {
     if (m_bodies == null)
     {
         if (checkData() == true)
         {
             Monitor.Enter(sm);
             //---------------------------------
             long   size = m_mem.Length;
             byte[] buff = new byte[size];
             m_mem.Seek(0, SeekOrigin.Begin);
             m_mem.Read(buff, 0, 1);
             sm.Write(buff, 0, 1);
             byte[] packsize = BitConverter.GetBytes(size + m_packLenSize);
             m_mem.Read(buff, 0, buff.Length - 1);
             sm.Write(packsize, 0, 2);
             sm.Write(packsize, 2, 1);
             sm.Write(buff, 0, buff.Length - 1);
             //------------------------------
             Monitor.Exit(sm);
         }
     }
     else
     {
         BufferBuilder bb         = new BufferBuilder();
         int           bodiessize = 0;
         for (int i = 0; i < m_bodies.Count; i++)
         {
             bodiessize += m_bodies[i].Length;
         }
         bodiessize += 4;
         if (bodiessize < 0xFFFFFF)
         {
             byte[] sz = BitConverter.GetBytes(bodiessize);
             bb.AddByte(m_cmd);
             bb.AddBytes(sz, 3);
             for (int i = 0; i < m_bodies.Count; i++)
             {
                 bb.AddBytes(m_bodies[i]);
             }
             byte[] bts = bb.GetAllBytes();
             Monitor.Enter(sm);
             //
             sm.Write(bts, 0, bts.Length);
             Monitor.Exit(sm);
         }
         else
         {
             throw new Exception("data length is invalid");
         }
     }
 }
Exemplo n.º 4
0
        private bool recvNewMessage(ExtKey key, byte[] msg, SMSHash hashid, byte[] hash)
        {
            ushort nummsg = BitConverter.ToUInt16(msg, 1);

            ushort retry = BitConverter.ToUInt16(msg, 3);

            string       text = Encoding.UTF8.GetString(msg, 5, msg.Length - 5);
            MORZEMessage mmsg = new MORZEMessage(text, hashid, hash, nummsg);

            if (OnRecvMessage != null)
            {
                OnRecvMessage(this, mmsg);
            }



            BufferBuilder bb = new BufferBuilder();

            bb.AddByte(4); // Type 4 - notify recived message
            bb.AddByte((byte)hashid);
            bb.AddBytes(hash);


            byte [] netmsg;
            bool    bres = false;
            string  err  = SMSCrypt.SyncEncode(key.SyncID, bb.GetAllBytes(), key.SyncKey, key.SyncIV, out netmsg);

            if (string.IsNullOrEmpty(err) == true)
            {
                if (m_responseMsg == null)
                {
                    m_responseMsg = new List <byte[]>();
                }
                m_responseMsg.Add(netmsg);
                bres = true;
            }

            return(bres);
        }
Exemplo n.º 5
0
        public override void Send(Stream sm)
        {
            BufferBuilder bb;

            bb = new BufferBuilder();
            for (int i = 0; i < m_hsh.Count; i++)
            {
                bb.AddByte((byte)m_hsh[i]);
                bb.AddBytes(m_exts[i]);
            }
            byte[] buff;
            buff = bb.GetAllBytes();
            buff = m_net.Encrypt(buff);
            base.WriteBytes(buff);
            base.Send(sm);
        }
        bool operateAsyncType1(byte[] data, byte[] tail)
        {
            bool isSuccess = false;
            int  off       = 1;

            byte[]      ext    = null;
            byte[]      sync   = null;
            byte[]      iv     = null;
            SMSHash     hashid = SMSHash.None;
            SMSSyncAlgo syncid = SMSSyncAlgo.None;

            int extlen = 0;
            int synlen = 0;

            switch (data[off])
            {
            case 1:    //MD5
                extlen = 0x10;
                hashid = SMSHash.MD5;

                break;
            }

            if (extlen > 0)
            {
                off++;
                ext = new byte[extlen];
                Array.Copy(data, off, ext, 0, ext.Length);
                off += ext.Length;
            }

            if (hashid != SMSHash.None)
            {
                switch (data[off])
                {
                case 1:    //DES
                    syncid = SMSSyncAlgo.DES;
                    synlen = 8;
                    break;
                }
            }
            if (synlen > 0)
            {
                off++;
                sync = new byte[synlen];
                iv   = new byte[synlen];
                Array.Copy(data, off, sync, 0, sync.Length);
                off += sync.Length;
                Array.Copy(data, off, iv, 0, iv.Length);
                off += iv.Length;
            }
            if (off + 2 == data.Length) // +2  - CRC16
            {
                byte[] contactaddres;
                string cont = null;
                if (string.IsNullOrEmpty(SMSCrypt.SyncDecode(syncid, tail, sync, iv, out contactaddres)) == false)
                {
                    contactaddres = null;
                }
                if (contactaddres != null)
                {
                    try
                    {
                        cont = Encoding.ASCII.GetString(contactaddres);
                        Convert.FromBase64String(cont.Substring(4));
                        isSuccess = m_acc.updateSynKey(hashid, ext, syncid, sync, iv, cont);
                    }
                    catch
                    {
                        isSuccess = false;
                    }
                }
            }
            if (isSuccess == true)
            {
                MORZESendMessage cmdMsgTyp2;

                //---------------
                cmdMsgTyp2 = new MORZESendMessage();

                BufferBuilder bb = new BufferBuilder();
                bb.AddByte(2); // Type 2 - уведомление о получении ключей
                bb.AddByte((byte)hashid);
                bb.AddBytes(SMSCrypt.CalcHash(hashid, sync));

                //byte[] msg = bb.GetAllBytes();
                //bb = new BufferBuilder();
                //bb.AddByte((byte)((int)0x80 ^ (int)hashid));
                //bb.AddBytes(SMSCrypt.CalcHash(hashid, msg));
                //bb.AddBytes(ext);
                //bb.AddBytes(msg);
                //cmdMsgTyp2.WriteBytes(bb.GetAllBytes());
                byte[] res;
                if (string.IsNullOrEmpty(SMSCrypt.SyncEncode(syncid, bb.GetAllBytes(), sync, iv, out res)) == true)
                {//send notify to client
                    cmdMsgTyp2.AddMessageBody(res, hashid, ext);
                    if (m_responses == null)
                    {
                        m_responses = new List <SMSSendCommand>();
                    }

                    //set server filter setting
                    SMSSendExt setext = new SMSSendExt();
                    setext.pushExt(hashid, ext);
                    m_responses.Add(setext);
                    m_responses.Add(cmdMsgTyp2);
                }
                else
                {
                    isSuccess = false;
                }
            }
            return(isSuccess);
        }