ubuntu/Mac系统智能卡操作全攻略1访问PC/SC读卡器

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中拷贝一段代码过来:

  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工程:猛击下载

原文地址:https://www.cnblogs.com/bigben0123/p/3108351.html