ubuntu/Mac系統智慧卡操作全攻略1--訪問PC/SC讀卡器

鄒德強發表於2012-09-20

因為PC/SC是Windows的體系,以系統API的層面服務應用。所以一直以來智慧卡相關的讀卡器和工具都集中在Windows上,而在unix體系下則一直水土不服。值得慶幸的是隨著開源組織M.U.S.C.L.E (Movment for the Use of Smart in Linux Environment)的積極努力下,pcsclite作為Xnix下的PC/SC裝置框架和應用介面已經成為了事實上的標準,Mac的Lion系統更是已經在發行版裡面整合了此服務。下面以ubuntu 12.0.4 發行版為例子。

#首先安裝pcsc的守護程式pcscd和工具

sudo apt-get -y install libusb-dev

sudo apt-get -y install pcscd


#然後安裝支援pcsc的讀卡器驅動(例子為內建的ACR ACS38U,其它讀卡器也可以到網站下載安裝)

sudo apt-get -y install libacr38u


#連線讀卡器,插卡後執行掃描工具驗證安裝結果

pcsc_scan

結果如下:
PC/SC device scanner
V 1.4.18 (c) 2001-2011, Ludovic Rousseau <ludovic.rousseau@free.fr>
Compiled with PC/SC lite version: 1.7.4
Using reader plug'n play mechanism
Scanning present readers...
0: ACS ACR38U 00 00


Thu Sep 20 12:55:08 2012
Reader 0: ACS ACR38U 00 00
  Card state: Card inserted, Shared Mode, 
  ATR: 3B 1D 94 42 72 6F 61 64 54 68 69 6E 6B 69 00 00


ATR: 3B 1D 94 42 72 6F 61 64 54 68 69 6E 6B 69 00 00
+ TS = 3B --> Direct Convention
+ T0 = 1D, Y(1): 0001, K: 13 (historical bytes)
  TA(1) = 94 --> Fi=512, Di=8, 64 cycles/ETU

    62500 bits/s at 4 MHz, fMax for Fi = 5 MHz => 78125 bits/s
+ Historical bytes: 42 72 6F 61 64 54 68 69 6E 6B 69 00 00
  Category indicator byte: 42 (proprietary format)


Possibly identified card (using /usr/share/pcsc/smartcard_list.txt):
NONE


Your card is not present in the database.
You can get the latest version of the database from
  http://ludovic.rousseau.free.fr/softwares/pcsc-tools/smartcard_list.txt
or use: wget http://ludovic.rousseau.free.fr/softwares/pcsc-tools/smartcard_list.txt --output-document=/home/caesarzou/.smartcard_list.txt


If your ATR is still not in the latest version then please send a mail
to <ludovic.rousseau@free.fr> containing:


#到此為止,PC/SC驅動已經打通,現在我們試試發個APDU

sudo apt-get -y install pcsc-tools

gscriptor

這是一個圖形介面的工具,在Script框裡面輸入: 

00A4040000

點Run按鈕,可以看到連線提示,然後就是結果:

Beginning script execution...


Sending: 00 A4 04 00 00 
Received: 6C 12 
Wrong length Le: should be 0x12


Script was executed without error...

#恭喜你,卡片訪問成功,現在你一定心癢難耐,想建立你自己的應用了吧?

#安裝開發庫

sudo apt-get install libpcsclite-dev

#安裝eclipse的cdt作為開發環境

sudo apt-get -y install g++
sudo apt-get -y install eclipse eclipse-cdt

#開啟eclipse,新建一個C工程,在c檔案中加入

#include <PCSC/winscard.h>

#連結到pcsclite庫:在C/C++ build / GCC Linker / Libraries 增加 pcsclite

#現在你會幸福的發現,winscard.h裡面提供的型別定義和介面和windows是一致的,我們從windows中拷貝一段程式碼過來:

#include <stdio.h>
#include <PCSC/winscard.h>

int main(void) {
	SCARDCONTEXT m_hContext;
	SCARDHANDLE  m_hCard;
	SCARD_IO_REQUEST io;
	char pmszReaders[100];
	BYTE CAPDU[] = {0x00,0xA4,0x04,0x00,0x00};
	BYTE RAPDU[256+2];
	DWORD cch = 100;
	DWORD i = 0;

	//Insert
	if(SCARD_S_SUCCESS != SCardEstablishContext(SCARD_SCOPE_USER, NULL, NULL, &m_hContext))
	{
		printf("Context error");
		return -1;
	}

	//List Reader
	if(SCARD_S_SUCCESS != SCardListReaders(m_hContext, NULL, pmszReaders, &cch))
	{
		printf("List Reader error");
		return -2;
	}

	printf("List Readers\n");
	while(i<cch)
	{
		printf("%s\n",pmszReaders+i);
		i += strlen(pmszReaders);
		i ++;
	}

	//Connect first Reader
	io.cbPciLength = sizeof(SCARD_IO_REQUEST);

	if(SCARD_S_SUCCESS != SCardConnect(m_hContext, pmszReaders, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0|SCARD_PROTOCOL_T1, &m_hCard, &io.dwProtocol))
	{
		printf("Connect Card error");
		return -3;
	}

	//Transmit APDU
	cch = 256+2;
	if(SCARD_S_SUCCESS != SCardTransmit(m_hCard, &io, CAPDU, 5, NULL, RAPDU, &cch))
	{
		printf("Transmit APDU error");
		return -4;
	}
	//echo
	printf("Transmit APDU\n");
	printf("CAPDU: ");
	for(i=0;i<5;i++)
	{
		sprintf(pmszReaders,"%02X",CAPDU[i]);
		printf("%s",pmszReaders);
	}
	printf("\n");
	printf("RAPDU: ");
	for(i=0;i<cch;i++)
	{
		sprintf(pmszReaders,"%02X",RAPDU[i]);
		printf("%s",pmszReaders);
	}
	printf("\n");

	//DisConnect
	SCardDisconnect(m_hCard, SCARD_EJECT_CARD);

	//Eject
	SCardReleaseContext(m_hContext);
	//puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
	return 0;
}
#編譯執行,輸出:
List Readers
ACS ACR38U 00 00


Transmit APDU
CAPDU: 00A4040000
RAPDU: 6C12

大功告成!下一個攻略我會講一下在ubuntu上基於JavaCard環境和工具的配置,敬請期待。


注:

    ubuntu上libpcsclite的標頭檔案預設位置在/usr/include/PCSC中,多了個目錄,有的版本在編譯的時候可能有#include 檔案錯誤,可以自行修改如下:

sudo vim /usr/include/PCSC/winscard.h

將#include <pcsclite.h>修改為#include <PCSC/pcsclite>,儲存退出

sudo vim /usr/include/PCSC/pcsclite.h

將#include <wintypes.h>修改為#include <PCSC/wintypes.h>,儲存推出


附件:

ubuntu上的eclipse工程:猛擊下載

Mac上的xcode工程:猛擊下載


相關文章