Exemple #1
0
 /// <summary>
 /// カードのアクセス権を解放する
 /// </summary>
 public void StopAccess()
 {
     if (cardContext != IntPtr.Zero)
     {
         WinSCard.Disconnect(cardContext, 0);
         cardContext = IntPtr.Zero;
     }
 }
Exemple #2
0
        private void PcscCommand(byte[] data, int len, out uint recvLen)
        {
            SCARD_IO_REQUEST reqCpi = WinSCard.GetPci(protocol);
            SCARD_IO_REQUEST resCpi = WinSCard.GetDefPci();

            recvLen = (uint)res.Length;
            if (!WinSCard.Transmit(cardContext, reqCpi, data, len, ref resCpi, res, ref recvLen))
            {
                throw GetException();
            }

            if (res[recvLen - 2] != 0x90 || res[recvLen - 1] != 0x00)
            {
                throw new PcscException(WinSCardError.SCARD_F_UNKNOWN_ERROR, new byte[] { res[recvLen - 2], res[recvLen - 1] });
            }
        }
Exemple #3
0
        /// <summary>
        /// ライブラリの解放
        /// (DisposeLibraryを実行した場合、次の利用では再びInitializeLibraryを実行する)
        /// </summary>
        /// <returns></returns>
        public bool DisposeLibrary()
        {
            if (cardContext != IntPtr.Zero)
            {
                WinSCard.Disconnect(cardContext, 0);
                cardContext = IntPtr.Zero;
            }

            if (rwContext != IntPtr.Zero)
            {
                WinSCard.ReleaseContext(rwContext);
                rwContext = IntPtr.Zero;
            }
            res = cmd = authKey = null;
            Debug.WriteLine("PcscReader Disposed!");
            return(true);
        }
Exemple #4
0
        /// <summary>
        /// カードオブジェクトの生成
        /// </summary>
        private NfcTag CreateCard()
        {
            uint status;

            byte[] atr;
            if (!WinSCard.GetStatus(cardContext, out status, out protocol, out atr))
            {
                return(null);
            }

            int  index   = 0;
            uint recvLen = 0;

            index += SetHeader(0xCA, 0);

            cmd[index++] = (byte)res.Length;

            PcscCommand(cmd, index, out recvLen);

            byte[] uid = new byte[recvLen - 2];
            Array.Copy(res, 0, uid, 0, uid.Length);

            if (atr[13] == 0x00 && atr[14] == 0x01)
            {
                return(new MifareCL(lib, uid));
            }
            else if (atr[13] == 0x00 && atr[14] == 0x02)
            {
                return(new MifareCL4K(lib, uid));
            }
            else if (atr[13] == 0x00 && atr[14] == 0x03)
            {
                return(new MifareUL(lib, uid));
            }
            else if (atr[13] == 0x00 && atr[14] == 0x3b)
            {
                return(new Felica(lib, uid));
            }
            else if (atr[13] == 0x00 && atr[14] == 0x3a)
            {
                return(new NTAG203(lib, uid));
            }
            return(null);
        }
Exemple #5
0
 /// <summary>
 /// ポーリング
 /// </summary>
 /// <returns>カード情報,NULL=未検出</returns>
 public NfcTag Polling()
 {
     cardContext = IntPtr.Zero;
     if (!WinSCard.Connect(rwContext, readerName, WinSCard.SCARD_SHARE_SHARED,
                           WinSCard.SCARD_PROTOCOL_T0 | WinSCard.SCARD_PROTOCOL_T1, ref cardContext, ref protocol))
     {
         return(null);
     }
     try
     {
         Debug.WriteLine("PcscReader:CardConnected!");
         return(CreateCard());
     }
     catch (PcscException)
     {
         StopAccess();
         return(null);
     }
 }
Exemple #6
0
        /// <summary>
        /// Pasoriで検出された例外を取得する
        /// </summary>
        private Exception GetException()
        {
            if (rwContext == IntPtr.Zero)
            {
                return(new PcscException(WinSCardError.SCARD_E_NO_READERS_AVAILABLE, null));
            }

            if (cardContext == IntPtr.Zero)
            {
                return(new PcscException(WinSCardError.SCARD_W_REMOVED_CARD, null));
            }

            if (WinSCard.GetLastError() != WinSCardError.SCARD_S_SUCCESS)
            {
                return(new PcscException(WinSCard.GetLastError(), null));
            }
            else
            {
                return(new PcscException(WinSCard.GetLastError(), WinSCard.GetLastRes()));
            }
        }