22 releases (stable)

2.8.2 Dec 23, 2023
2.8.0 Dec 28, 2022
2.7.0 Feb 15, 2022
2.6.0 Aug 24, 2021
0.1.0 Feb 6, 2017

#66 in Hardware support

Download history 3532/week @ 2024-08-11 4595/week @ 2024-08-18 4283/week @ 2024-08-25 3942/week @ 2024-09-01 3322/week @ 2024-09-08 3744/week @ 2024-09-15 3515/week @ 2024-09-22 3848/week @ 2024-09-29 2985/week @ 2024-10-06 4025/week @ 2024-10-13 4148/week @ 2024-10-20 4129/week @ 2024-10-27 4586/week @ 2024-11-03 3914/week @ 2024-11-10 4000/week @ 2024-11-17 3223/week @ 2024-11-24

16,092 downloads per month
Used in 44 crates (18 directly)

MIT license

92KB
1.5K SLoC

pcsc-rust

crates.io docs.rs license

Linux, macOS, Windows:

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

  • Nice, safe API.
  • Tested on Linux, Windows, macOS.
  • Mostly zero overhead.

See the Documentation for more details.

See the pcsc/examples directory for some common tasks.

Contents

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

The pcsc crate contains high-level Rust wrappers.

Usage

In your Cargo.toml:

[dependencies]
pcsc = "2"

Example

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

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]

License

The MIT license.

Dependencies

~115KB