Ejemplo n.º 1
0
        /// <summary>
        /// Unpacks the header.
        /// </summary>
        /// <returns>The header.</returns>
        /// <param name="data">Data.</param>
        /// <param name="serverKey">Server key.</param>
        /// <param name="refOffset">Reference offset.</param>
        public static GenericPacket UnpackPacket(byte[] data)
        {
            UInt64 tidWithFlags, tid;

            tidWithFlags = tid = 0;
            byte[] nonce = new byte[24];
            //todo: clean up the GenericPacket classes to allow us to do this without
            UInt32 seq, ack, cid;

            seq = ack = cid = 0;
            byte[] epk = null, puzzle = null;
            bool   hasPK, hasPuzzle;
            uint   curIndex = 0;

            PackingHelpers.UnpackUint64(ref tidWithFlags, data, ref curIndex);
            //curIndex += 1;
            tid       = tidWithFlags & ~Common.TID_FLAGS;
            hasPK     = (tidWithFlags & Common.PUBLIC_KEY_FLAG) == Common.PUBLIC_KEY_FLAG;
            hasPuzzle = (tidWithFlags & Common.PUZZLE_FLAG) == Common.PUZZLE_FLAG;

            //PackingHelpers.UnpackUint64 (ref nonce, data, ref curIndex);
            Array.Copy(data, curIndex, nonce, 0, nonce.Length);
            curIndex += (uint)(nonce.Length);

            if (hasPK)
            {
                epk = new byte[EPK_SIZE];
                Array.Copy(data, curIndex, epk, 0, EPK_SIZE);
                curIndex += (EPK_SIZE + 1);
            }

            if (hasPuzzle)
            {
                puzzle = new byte[PUZZLE_SOLUTIONS_SIZE];
                Array.Copy(data, curIndex, puzzle, 0, PUZZLE_SOLUTIONS_SIZE);
                curIndex += PUZZLE_SOLUTIONS_SIZE + 1;
            }

            //The remainder of the GenericPacket is encrypted. If there is an E_PK present
            //then this is likely a abstractTunnel open GenericPacket. Unlock it with the server key
            if (hasPK && data.Length > curIndex)
            {
                //byte[] cipherPart = new byte[data.Length - curIndex];
                //byte[] decrypted = Sodium.PublicKeyBox.OpenDetached (cipherPart, nonce, epk, secretKey);
                //copy in the checksum
                //byte[] rawMessage = new byte[decrypted.Length - CHECKSUM_SIZE];
                //Array.Copy (decrypted, rawMessage, 0, rawMessage.Length);
                //here we have to create an OpenPacket
                //todo: is this strictly nessecary?
                byte[] cipherPart = new byte[data.Length - curIndex];
                Array.Copy(data, curIndex, cipherPart, 0, cipherPart.Length);
                if (!hasPuzzle)
                {
                    OpenTunnelPacket packet = new OpenTunnelPacket(tid, seq, cid, ack, epk, cipherPart);
                    packet.Nonce = nonce;
                    return(packet);
                }
                return(new OpenTunnelPacket(tid, seq, cid, ack, epk, cipherPart, puzzle));
            }
            else
            {
                EncryptedPacket packet = new EncryptedPacket(tid, cid, ack);
                packet.Seq   = seq;
                packet.Nonce = nonce;
                byte[] cipherPart = new byte[data.Length - curIndex];
                Array.Copy(data, curIndex, cipherPart, 0, cipherPart.Length);
                packet.CipherText = cipherPart;
                return(packet);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Unpacks the header from the local binary representation
        /// </summary>
        public virtual uint UnpackHeader()
        {
            UInt64 tidWithFlags, tid;

            tidWithFlags = tid = 0;
            byte[] nonce = new byte[24];
            //todo: clean up the GenericPacket classes to allow us to do this without
            UInt32 seq, ack;

            seq = ack = 0;
            byte[] epk = null, puzzle = null;
            bool   hasPK, hasPuzzle;
            uint   curIndex = 0;

            byte[] data = this.rawBytes;

            PackingHelpers.UnpackUint64(ref tidWithFlags, data, ref curIndex);
            //curIndex += 1;
            tid       = tidWithFlags & ~Common.TID_FLAGS;
            hasPK     = (tidWithFlags & Common.PUBLIC_KEY_FLAG) != 0;
            hasPuzzle = (tidWithFlags & Common.PUZZLE_FLAG) != 0;

            this.TID   = tid;
            this.Seq   = seq;
            this.Ack   = ack;
            this.Nonce = nonce;

            //PackingHelpers.UnpackUint64 (ref nonce, data, ref curIndex);
            Array.Copy(data, curIndex, nonce, 0, nonce.Length);
            curIndex += (uint)(nonce.Length);

            if (hasPK)
            {
                epk = new byte[EPK_SIZE];
                Array.Copy(data, curIndex, epk, 0, EPK_SIZE);
                curIndex += EPK_SIZE;
                this.EuphemeralPublicKey = epk;
            }

            if (hasPuzzle)
            {
                puzzle = new byte[PUZZLE_SOLUTIONS_SIZE];
                Array.Copy(data, curIndex, puzzle, 0, PUZZLE_SOLUTIONS_SIZE);
                curIndex += PUZZLE_SOLUTIONS_SIZE;
                this.PuzzleOrSolution = puzzle;
            }

            //The remainder of the GenericPacket is encrypted. If there is an E_PK present
            //then this is likely a abstractTunnel open GenericPacket. Unlock it with the server key

            /*if(hasPK && data.Length > curIndex){
             *  //byte[] cipherPart = new byte[data.Length - curIndex];
             *  //byte[] decrypted = Sodium.PublicKeyBox.OpenDetached (cipherPart, nonce, epk, secretKey);
             *  //copy in the checksum
             *  //byte[] rawMessage = new byte[decrypted.Length - CHECKSUM_SIZE];
             *  //Array.Copy (decrypted, rawMessage, 0, rawMessage.Length);
             *  //here we have to create an OpenPacket
             *  byte[] cipherPart = new byte[data.Length - curIndex];
             *  Array.Copy (data, curIndex, cipherPart, 0, cipherPart.Length);
             *  if (!hasPuzzle) {
             *      OpenTunnelPacket GenericPacket = new OpenTunnelPacket (tid, seq, id, ack, epk, cipherPart);
             *      GenericPacket.Nonce = nonce;
             *
             *  }
             * }else{*/
            //EncryptedPacket GenericPacket = new EncryptedPacket(tid, id, ack);
            //GenericPacket.Seq = seq;
            //GenericPacket.Nonce = nonce;

            //GenericPacket.CipherText = cipherPart;
            return(curIndex);
            //}
        }