取得Smart Card的APDU Command

回覆文章
yehlu
Site Admin
文章: 3245
註冊時間: 2004-04-15 17:20:21
來自: CodeCharge Support Engineer

取得Smart Card的APDU Command

文章 yehlu »

http://www.minitw.com/archives/softtech ... iant=zh-tw

一般我們在使用網路ATM、自然人憑證時、iCash,把卡片插進讀卡機後

都是透過APDU Command來對卡片下指令進行查詢卡號、帳號、餘額...等

也就是說,只要取得相關的指令,我們也可以自己寫程式來操作

首先第一個問題是,要怎麼下指令。第二個問題是,要下什麼指令

第一個問題比較好解決,目前己經有現成的程式碼了,就不再贅述

請參考使用C#讀取自然人評證卡號

第二個問題,可以說簡單,也可以說複雜

複雜的來說呢~~必需先去K懂一堆SmartCard的相關文件,然後再去看指令集

當然,像我這樣懶惰取巧的人,是不太可能花這麼多時間去讀文件(此為不良示範...冏)

那麼簡單來說呢~~這才是本文的重點,就是只要知道那些現成的ActiveX 元件下了什麼指令就可以了

這麼說,只要把指令Sinffer起來就好囉。沒錯!正解

為了要達到這個目的,必需使用這個元件 WinSCard APDU View Utility

官方網址在這邊,官方載點在這邊,茶包分流載點

安裝方式官方網址裡面有。或是你可以參考下面:

1.下載WinSCard APDU View Utility,並解出winscard.dll
2.到C:\WINDOWS\system32\ 下面,複製一份你的原始winscard.dll成winscard_BAK.dll
3.把C:\WINDOWS\system32\winscard.dll 改檔名為 original.dll
4.把WinSCard APDU View Utility 解出的 winscard.dll,放到 C:\WINDOWS\system32\
5.重新開機

完成後,你就可以開始測試了。例如去MOICA內政部憑證管理中心測試你的自然人憑證。

20091208_1

讀卡機只要有動作的話,相關的指令會在你桌面上的 winscard.txt 檔案,內容大概長這樣子

SCardTransmit (handle 0xEA0A0000):
transmitted:
80 A4 00 00 02 3F 00
received:
90 00

SCardTransmit (handle 0xEA0A0000):
transmitted:
80 A4 00 00 02 09 00
received:
90 00

SCardTransmit (handle 0xEA0A0000):
transmitted:
80 A4 00 00 02 09 03
received:
90 00

SCardTransmit (handle 0xEA0A0000):
transmitted:
80 B0 00 00 10
received:
54 50 30 30 30 30 30 30 30 31 36 31 31 31 31 31 90 00

相關實作程式請參考使用讀卡機取得自然人憑證的卡號
yehlu
Site Admin
文章: 3245
註冊時間: 2004-04-15 17:20:21
來自: CodeCharge Support Engineer

Re: 取得Smart Card的APDU Command

文章 yehlu »

https://hanez.org/pcsc-for-php.html

PC/SC for PHP
Table of contents
1 About
2 Installation
3 API
3.1 scard_establish_context();
3.2 scard_is_valid_context($context);
3.3 scard_release_context($context);
3.4 scard_list_readers($context);
3.5 scard_connect($context, "OMNIKEY CardMan 5x21 00 00");
3.6 scard_reconnect($connection);
3.7 scard_disconnect($connection);
3.8 scard_transmit($connection, $apdu);
3.9 scard_status($connection);
3.10 scard_cancel($context);
4 Code
5 License
6 Links
About
This is the only extension for using PC/SC based smart cards with PHP. It is a wrapper to the wonderful and free project by Ludovic Rousseau, PCSC-Lite, which is the middleware to access a smart card using SCard API (PC/SC). Since PCSC-Lite is compatible to the winscard API it should be possible to compile this extension using a Windows(R) operating system. Currently I only take focus on Linux environments.

Thanks are going to Johann Dantant! He provides a PC/SC extension for PHP since 2005 and I reused some of his code. He allowed me to relicense these parts under the terms of the PHP license so I could integrate PCSC-Lite natively into PHP.

Installation
I recommend to install the PECL extension the "PHP" way:

pecl install pcsc-alpha
You can install the latest version by downloading the sources and compile yourself too... :)

wget http://pecl.php.net/get/pcsc-0.3.1.tgz
tar -xvf pcsc-0.3.1.tgz
cd pcsc-0.3.1
phpize
./configure
make
make install
After that you have all needed files in ./modules/

API
The extension currently provides the following API:

scard_establish_context();

Returns the application $context to the PC/SC resource manager.

scard_is_valid_context($context);

Returns TRUE if $context is valid or FALSE if $context is not valid.

scard_release_context($context);

Releases the application $context.

scard_list_readers($context);

Returns an array of available readers or FALSE.

Example:

array(3) {
[0]=>
string(26) "OMNIKEY CardMan 5x21 00 00"
[1]=>
string(26) "OMNIKEY CardMan 5x21 00 01"
[2]=>
string(76) "SCL01x Contactless Reader [SCL01x Contactless Reader] (21161009200722) 00 00"
}
scard_connect($context, "OMNIKEY CardMan 5x21 00 00");

Connects to a card. Returns the $connection to a reader or FALSE.

scard_reconnect($connection);

Returns the $connection to a reader or FALSE.

scard_disconnect($connection);

Disconnects the $connection to a card. Returns the TRUE if disconnecting was succesful or FALSE.

scard_transmit($connection, $apdu);

Returns the response $apdu as string or FALSE.

scard_status($connection);

Returns the status or FALSE.

scard_cancel($context);

Code
The code has successful been released to the official code repository pecl.php.net... ;)

PECL Project Page
PHP SVN Code Browser
You could get the code using the following command:

svn checkout http://svn.php.net/repository/pecl/pcsc/trunk pcsc
License
This code is licensed under the terms of the PHP License version 3.01. PCSC-Lite is licensed in a way where it is possible to integrate it native in the PHP environment.

Links
PC/SC Worgroup - Homepage and definition file downloads.
PC/SC - PC/SC at Wikipedia.
PC/SC-Lite - The free and open implementation of the PC/SC SCard API for UNIX
PCSC sample in PHP5 - Ludovic Rousseau about "PC/SC for PHP"
yehlu
Site Admin
文章: 3245
註冊時間: 2004-04-15 17:20:21
來自: CodeCharge Support Engineer

Re: 取得Smart Card的APDU Command

文章 yehlu »

回覆文章

回到「PHP」