Пример #1
0
        private static byte[] WrapApdu(byte[] apdu, TlsClientProtocol tls)
        {
            byte[]      header    = { 0x84, 0x00, 0x00, 0x00 };
            List <byte> finalApdu = new List <byte>();

            tls.OfferOutput(apdu, 0, apdu.Length);
            int dataAvailable = tls.GetAvailableOutputBytes();

            byte[] wrappedData = new byte[dataAvailable];
            tls.ReadOutput(wrappedData, 0, wrappedData.Length);

            if (wrappedData.Length <= 255)
            {
                finalApdu.AddRange(header);
                finalApdu.Add((byte)wrappedData.Length);
                finalApdu.AddRange(wrappedData);
            }
            else
            {
                finalApdu.AddRange(header);
                finalApdu.Add((byte)0x00);
                finalApdu.Add((byte)(wrappedData.Length >> 8));
                finalApdu.Add((byte)wrappedData.Length);
                finalApdu.AddRange(wrappedData);
            }

            return(finalApdu.ToArray());
        }
Пример #2
0
        public void WriteData()
        {
            if (_queue.Count == 0)
            {
                return;
            }
            lock (_writeLock) {
                if (_writing > 0)
                {
                    return;
                }
                _writing = 1;
            }

            while (Queue.Count > 0)
            {
                QueueItem q;
                int       cbRead;
                byte[]    buffer = new byte[1024];

                if (!_queue.TryDequeue(out q))
                {
                    break;
                }

                if (_tlsClient != null)
                {
                    _tlsClient.OfferOutput(q.Data, 0, q.Data.Length);
                    do
                    {
                        cbRead = _tlsClient.ReadOutput(buffer, 0, buffer.Length);

                        _stm.Write(buffer, 0, cbRead);
                    } while (cbRead > 0);
                }
                else if (_tlsServer != null)
                {
                    _tlsServer.OfferOutput(q.Data, 0, q.Data.Length);
                    do
                    {
                        cbRead = _tlsServer.ReadOutput(buffer, 0, buffer.Length);

                        _stm.Write(buffer, 0, cbRead);
                    } while (cbRead > 0);
                }
            }

            lock (_writeLock) {
                _writing = 0;
                if (_queue.Count > 0)
                {
                    WriteData();
                }
            }
        }