Ubuntu/MAC Smart Card Operation overview 1-access PC/SC Card Reader

Source: Internet
Author: User

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

Because PC/SC is a Windows system, it serves applications at the system API level. So for a long time, card readers and tools related to smart cards have been concentrated on Windows, while on Unix systems, card readers have been reluctant to accept. Fortunately, with the open-source organization m.u. s. c.l. with the active efforts of E (movment for the use of smart in Linux environment), pcsclite has become a de facto standard for the PC/SC device framework and application interfaces under xnix, the Mac lion system has already integrated this service in the release version. The following uses the Ubuntu 12.0.4 release as an example.

 

# First install the pcsc daemon pcscd and tools

Sudo apt-get-y install libusb-Dev

Sudo apt-get-y install pcscd

 

# Then install a card reader driver that supports pcsc (for example, the built-in ACR acs38u, and other card readers can download and install it on the website)

Sudo apt-get-y install libacr38u

 

# Connect the card reader, plug the card, and run the scan tool to verify the installation result

Pcsc_scan

The result is as follows:
PC/SC device restart
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 mechanic
Scanning present readers...
0: ACS acr38u 00

Thu Sep 20 12:55:08 2012
Reader 0: ACS acr38u 00 00
Card state: card inserted, shared mode,
Recognition: 3B 1D 94 42 72 6f 61 64 54 68 69 6e 6B 69 00 00

Recognition: 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 = & gt; 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 ASD is still not in the latest version then please send a mail
To <ludovic.rousseau@free.fr> containing:

 

# So far, the PC/SC driver has been connected. Now let's try to send an APDU

Sudo apt-get-y install pcsc-Tools

Gscriptor

This is a graphical interface tool. In the script box, enter:

00a4040000

Click the run button. The connection prompt is displayed, and the result is displayed:

Beginning script execution...

Sending: 00 A4 04 00 00
Written ed: 6C 12
Wrong length le: shocould be 0x12

Script was executed without error...

# Congratulations, card access is successful. Now you must be impatient. Do you want to create your own applications?

# Install the Development Library

Sudo apt-Get install libpcsclite-Dev

# Install eclipse CDT as the development environment

Sudo apt-get-y install g ++
Sudo apt-get-y install eclipse-CDT

# Open eclipse, create a new C Project, and add

# Include <pcsc/winscard. h>

# Link to the pcsclite Library: Add pcsclite in C/C ++ build/GCC linker/Libraries

# Now you will be happy to find that the Type Definitions and interfaces provided in winscard. h are the same as those provided in windows. We copy a piece of code from Windows:

 

[CPP]View plaincopyprint?

 
  1. # Include <stdio. h>
  2. # Include <pcsc/winscard. h>
  3. Int main (void ){
  4. Scardcontext m_hcontext;
  5. Scardhandle m_hcard;
  6. Scard_io_request IO;
  7. Char pmszreaders [100];
  8. Byte capdu [] = {0x00, 0xa4, 0x04,0x00,0x00 };
  9. Byte rapdu [256 + 2];
  10. Dword cch = 100;
  11. DWORD I = 0;
  12. // Insert
  13. If (scard_s_success! = Scardestablishcontext (scard_scope_user, null, null, & m_hcontext ))
  14. {
  15. Printf ("context error ");
  16. Return-1;
  17. }
  18. // List Reader
  19. If (scard_s_success! = Scardlistreaders (m_hcontext, null, pmszreaders, & CCH ))
  20. {
  21. Printf ("list reader error ");
  22. Return-2;
  23. }
  24. Printf ("list readers \ n ");
  25. While (I <CCH)
  26. {
  27. Printf ("% s \ n", pmszreaders + I );
  28. I + = strlen (pmszreaders );
  29. I ++;
  30. }
  31. // Connect First Reader
  32. Io. cbpcilength = sizeof (scard_io_request );
  33. If (scard_s_success! = Scardconnect (m_hcontext, pmszreaders, scard_mongo_shared, scard_protocol_t0 | scard_protocol_t1, & m_hcard, & Io. dwprotocol ))
  34. {
  35. Printf ("Connect card error ");
  36. Return-3;
  37. }
  38. // Transmit APDU
  39. CCH = 256 + 2;
  40. If (scard_s_success! = Scardtransmit (m_hcard, & Io, capdu, 5, null, rapdu, & CCH ))
  41. {
  42. Printf ("transmit APDU error ");
  43. Return-4;
  44. }
  45. // Echo
  46. Printf ("transmit APDU \ n ");
  47. Printf ("capdu :");
  48. For (I = 0; I <5; I ++)
  49. {
  50. Sprintf (pmszreaders, "% 02x", capdu [I]);
  51. Printf ("% s", pmszreaders );
  52. }
  53. Printf ("\ n ");
  54. Printf ("rapdu :");
  55. For (I = 0; I <CCH; I ++)
  56. {
  57. Sprintf (pmszreaders, "% 02x", rapdu [I]);
  58. Printf ("% s", pmszreaders );
  59. }
  60. Printf ("\ n ");
  61. // Disconnect
  62. Scarddisconnect (m_hcard, scard_eject_card );
  63. // Eject
  64. Scardreleasecontext (m_hcontext );
  65. // Puts ("!!! Hello world !!! ");/* Prints !!! Hello world !!! */
  66. Return 0;
  67. }

# Compile, run, and output:
List readers
ACS acr38u 00

Transmit APDU
Capdu: 00a4040000
Rapdu: 6c12

 

Success! Next, I will talk about the configuration based on the javacard environment and tool on Ubuntu, so stay tuned.

 

Note:

The header file of libpcsclite on Ubuntu is located in/usr/include/pcsc by default. multiple directories are added. Some versions may have # include file errors during compilation. You can modify them as follows:

Sudo Vim/usr/include/pcsc/winscard. h

Change # include <pcsclite. h> to # include <pcsc/pcsclite>, save and exit

Sudo Vim/usr/include/pcsc/pcsclite. h

Change # include <wintypes. h> to # include <pcsc/wintypes. h>.

 

Attachment:

Eclipse project on Ubuntu: Click to download

Xcode project on Mac: Click to download

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.