#smartcard #pcsc

pcsc

Bindings to the PC/SC API for smart card communication

12 releases (5 stable)

2.1.1 Feb 11, 2019
2.0.0 Dec 3, 2018
1.0.1 Feb 25, 2018
1.0.0 Dec 5, 2017
0.1.0-alpha4 Jan 28, 2017

#8 in Hardware support

Download history 15/week @ 2019-04-23 40/week @ 2019-04-30 23/week @ 2019-05-07 54/week @ 2019-05-14 82/week @ 2019-05-21 32/week @ 2019-05-28 42/week @ 2019-06-04 23/week @ 2019-06-11 32/week @ 2019-06-18 72/week @ 2019-06-25 77/week @ 2019-07-02 42/week @ 2019-07-09 266/week @ 2019-07-16 9/week @ 2019-07-23 25/week @ 2019-07-30

136 downloads per month
Used in 2 crates

MIT license

68KB
1K SLoC

pcsc-rust

crates.io docs.rs license

Linux, macOS: Travis CI Windows: AppVeyor

Rust bindings to the PC/SC API for smart card communication.

  • Nice, safe API.
  • Works on all major operating systems.
  • Mostly zero overhead.

See the Documentation for more details.

See the pcsc/examples directory for some common tasks.

Contents

The pcsc-sys crate contains the direct, low-level bindings to the C API.

The pcsc crate contains the high-level Rust wrappers.

Usage

In your Cargo.toml:

[dependencies]
pcsc = "2"

In your crate:

extern crate pcsc;

Example

Connect to the card in the first available reader, send the card an APDU command, print the APDU response.

extern crate pcsc;

use pcsc::*;

fn main() {
    // Establish a PC/SC context.
    let ctx = match Context::establish(Scope::User) {
        Ok(ctx) => ctx,
        Err(err) => {
            eprintln!("Failed to establish context: {}", err);
            std::process::exit(1);
        }
    };

    // List available readers.
    let mut readers_buf = [0; 2048];
    let mut readers = match ctx.list_readers(&mut readers_buf) {
        Ok(readers) => readers,
        Err(err) => {
            eprintln!("Failed to list readers: {}", err);
            std::process::exit(1);
        }
    };

    // Use the first reader.
    let reader = match readers.next() {
        Some(reader) => reader,
        None => {
            println!("No readers are connected.");
            return;
        }
    };
    println!("Using reader: {:?}", reader);

    // Connect to the card.
    let card = match ctx.connect(reader, ShareMode::Shared, Protocols::ANY) {
        Ok(card) => card,
        Err(Error::NoSmartcard) => {
            println!("A smartcard is not present in the reader.");
            return;
        }
        Err(err) => {
            eprintln!("Failed to connect to card: {}", err);
            std::process::exit(1);
        }
    };

    // Send an APDU command.
    let apdu = b"\x00\xa4\x04\x00\x0A\xA0\x00\x00\x00\x62\x03\x01\x0C\x06\x01";
    println!("Sending APDU: {:?}", apdu);
    let mut rapdu_buf = [0; MAX_BUFFER_SIZE];
    let rapdu = match card.transmit(apdu, &mut rapdu_buf) {
        Ok(rapdu) => rapdu,
        Err(err) => {
            eprintln!("Failed to transmit APDU command to card: {}", err);
            std::process::exit(1);
        }
    };
    println!("APDU response: {:?}", rapdu);
}

Example output:

$ ./target/debug/examples/readme
Using reader: "SCM Microsystems Inc. SCR 355 [CCID Interface] 00 00"
Sending APDU: [0, 164, 4, 0, 10, 160, 0, 0, 0, 98, 3, 1, 12, 6, 1]
APDU response: [106, 130]

Status

  • There are still some minor TODOs remaining.

Help is welcome!

License

The MIT license.

Dependencies

~31KB