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

來源:互聯網
上載者:User

http://blog.csdn.net/CaesarZou/article/details/7999624

因為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中拷貝一段代碼過來:

 

[cpp] view plaincopyprint?

 
  1. #include <stdio.h>  
  2. #include <PCSC/winscard.h>  
  3.   
  4. int main(void) {  
  5.     SCARDCONTEXT m_hContext;  
  6.     SCARDHANDLE  m_hCard;  
  7.     SCARD_IO_REQUEST io;  
  8.     char pmszReaders[100];  
  9.     BYTE CAPDU[] = {0x00,0xA4,0x04,0x00,0x00};  
  10.     BYTE RAPDU[256+2];  
  11.     DWORD cch = 100;  
  12.     DWORD i = 0;  
  13.   
  14.     //Insert  
  15.     if(SCARD_S_SUCCESS != SCardEstablishContext(SCARD_SCOPE_USER, NULL, NULL, &m_hContext))  
  16.     {  
  17.         printf("Context error");  
  18.         return -1;  
  19.     }  
  20.   
  21.     //List Reader  
  22.     if(SCARD_S_SUCCESS != SCardListReaders(m_hContext, NULL, pmszReaders, &cch))  
  23.     {  
  24.         printf("List Reader error");  
  25.         return -2;  
  26.     }  
  27.   
  28.     printf("List Readers\n");  
  29.     while(i<cch)  
  30.     {  
  31.         printf("%s\n",pmszReaders+i);  
  32.         i += strlen(pmszReaders);  
  33.         i ++;  
  34.     }  
  35.   
  36.     //Connect first Reader  
  37.     io.cbPciLength = sizeof(SCARD_IO_REQUEST);  
  38.   
  39.     if(SCARD_S_SUCCESS != SCardConnect(m_hContext, pmszReaders, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0|SCARD_PROTOCOL_T1, &m_hCard, &io.dwProtocol))  
  40.     {  
  41.         printf("Connect Card error");  
  42.         return -3;  
  43.     }  
  44.   
  45.     //Transmit APDU  
  46.     cch = 256+2;  
  47.     if(SCARD_S_SUCCESS != SCardTransmit(m_hCard, &io, CAPDU, 5, NULL, RAPDU, &cch))  
  48.     {  
  49.         printf("Transmit APDU error");  
  50.         return -4;  
  51.     }  
  52.     //echo  
  53.     printf("Transmit APDU\n");  
  54.     printf("CAPDU: ");  
  55.     for(i=0;i<5;i++)  
  56.     {  
  57.         sprintf(pmszReaders,"%02X",CAPDU[i]);  
  58.         printf("%s",pmszReaders);  
  59.     }  
  60.     printf("\n");  
  61.     printf("RAPDU: ");  
  62.     for(i=0;i<cch;i++)  
  63.     {  
  64.         sprintf(pmszReaders,"%02X",RAPDU[i]);  
  65.         printf("%s",pmszReaders);  
  66.     }  
  67.     printf("\n");  
  68.   
  69.     //DisConnect  
  70.     SCardDisconnect(m_hCard, SCARD_EJECT_CARD);  
  71.   
  72.     //Eject  
  73.     SCardReleaseContext(m_hContext);  
  74.     //puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */  
  75.     return 0;  
  76. }  

#編譯運行,輸出:
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工程:猛擊下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.