예제 #1
0
        private static byte[] UnwrapApdu(byte[] response, TlsClientProtocol tls)
        {
            tls.OfferInput(response);
            int dataAvailable = tls.GetAvailableInputBytes();

            if (dataAvailable == 0)
            {
                throw new RemoteProtocolException("Error in APDU response");
            }

            byte[] unwrappedData = new byte[dataAvailable];
            tls.ReadInput(unwrappedData, 0, unwrappedData.Length);

            return(unwrappedData);
        }
예제 #2
0
파일: TLSSession.cs 프로젝트: jimsch/TCP
        private void ProcessInput(int cbRead)
        {
            byte[] data;

            byte[] result = new byte[cbRead];
            Array.Copy(_buffer, 0, result, 0, cbRead);

            if (_tlsClient != null)
            {
                _tlsClient.OfferInput(result);

                data = new byte[_tlsClient.GetAvailableInputBytes()];
                _tlsClient.ReadInput(data, 0, data.Length);
            }
            else
            {
                _tlsServer.OfferInput(result);

                data = new byte[_tlsServer.GetAvailableInputBytes()];
                _tlsServer.ReadInput(data, 0, data.Length);
            }

            if (data.Length == 0)
            {
                _stm.BeginRead(_buffer, 0, _buffer.Length, ReadCallback, this);
                return;
            }

            byte[] bytes = new byte[(_carryOver != null ? _carryOver.Length : 0) + data.Length];
            if (_carryOver != null)
            {
                Array.Copy(_carryOver, bytes, _carryOver.Length);
                Array.Copy(data, 0, bytes, _carryOver.Length, cbRead);
            }
            else
            {
                Array.Copy(data, bytes, data.Length);
            }

            int cbLeft = bytes.Length;

            while (cbLeft > 0)
            {
                int messageSize;

                //  Do I have a full record?

                int dataSize = (bytes[0] >> 4) & 0xf;
                switch (dataSize)
                {
                case 13:
                    messageSize = bytes[1] + 13 + 3;
                    break;

                case 14:
                    messageSize = (bytes[1] * 256 + bytes[2]) + 269 + 4;
                    break;

                case 15:
                    messageSize = ((bytes[1] * 256 + bytes[2]) * 256 + bytes[3]) * 256 + bytes[4] + 65805 + 6;
                    break;

                default:
                    messageSize = dataSize + 2;
                    break;
                }
                messageSize += (bytes[0] & 0xf); // Add token buffer

                if (cbLeft >= messageSize)
                {
                    byte[] message = new byte[messageSize];
                    int    offset;
                    Array.Copy(bytes, message, messageSize);
                    Array.Copy(bytes, messageSize, bytes, 0, cbLeft - messageSize);
                    offset  = cbLeft - messageSize;
                    cbLeft -= messageSize;

                    FireDataReceived(message, EndPoint, null, this); // M00BUG
                }
                else
                {
                    break;
                }
            }

            if (cbLeft > 0)
            {
                _carryOver = new byte[cbLeft];
                Array.Copy(bytes, _carryOver, cbLeft);
            }
            else
            {
                _carryOver = null;
            }
            _stm.BeginRead(_buffer, 0, _buffer.Length, ReadCallback, this);
        }