From 67cd263c693983308d8cf0ace6484d086f667786 Mon Sep 17 00:00:00 2001 From: "Lance R. Vick" Date: Wed, 1 Nov 2023 14:55:00 -0700 Subject: [PATCH] initial example --- examples/rust_pcsc_static/Cargo.lock | 39 ++++++++++++++++++++++++ examples/rust_pcsc_static/Cargo.toml | 11 +++++++ examples/rust_pcsc_static/Dockerfile | 18 ++++++++++++ examples/rust_pcsc_static/main.rs | 44 ++++++++++++++++++++++++++++ 4 files changed, 112 insertions(+) create mode 100644 examples/rust_pcsc_static/Cargo.lock create mode 100644 examples/rust_pcsc_static/Cargo.toml create mode 100644 examples/rust_pcsc_static/Dockerfile create mode 100644 examples/rust_pcsc_static/main.rs diff --git a/examples/rust_pcsc_static/Cargo.lock b/examples/rust_pcsc_static/Cargo.lock new file mode 100644 index 0000000..f3437f3 --- /dev/null +++ b/examples/rust_pcsc_static/Cargo.lock @@ -0,0 +1,39 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "pcsc" +version = "2.8.0" +source = "git+https://github.com/bluetech/pcsc-rust?rev=9d7c1645a7fd9639d0585f3a4cc698b54d9746e5#9d7c1645a7fd9639d0585f3a4cc698b54d9746e5" +dependencies = [ + "bitflags", + "pcsc-sys", +] + +[[package]] +name = "pcsc-sys" +version = "1.2.0" +source = "git+https://github.com/bluetech/pcsc-rust?rev=9d7c1645a7fd9639d0585f3a4cc698b54d9746e5#9d7c1645a7fd9639d0585f3a4cc698b54d9746e5" +dependencies = [ + "pkg-config", +] + +[[package]] +name = "pcsc_demo" +version = "0.1.0" +dependencies = [ + "pcsc", +] + +[[package]] +name = "pkg-config" +version = "0.3.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ac9a59f73473f1b8d852421e59e64809f025994837ef743615c6d0c5b305160" diff --git a/examples/rust_pcsc_static/Cargo.toml b/examples/rust_pcsc_static/Cargo.toml new file mode 100644 index 0000000..ebf3a93 --- /dev/null +++ b/examples/rust_pcsc_static/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "pcsc_demo" +version = "0.1.0" +authors = ["Some Person "] + +[dependencies] +pcsc = { git = "https://github.com/bluetech/pcsc-rust", rev = "9d7c1645a7fd9639d0585f3a4cc698b54d9746e5" } + +[[bin]] +name = "pcsc_demo" +path = "main.rs" diff --git a/examples/rust_pcsc_static/Dockerfile b/examples/rust_pcsc_static/Dockerfile new file mode 100644 index 0000000..b12a9c8 --- /dev/null +++ b/examples/rust_pcsc_static/Dockerfile @@ -0,0 +1,18 @@ +ARG LIBSTD_IMAGE=ocirep:rust-libstd-musl +ARG LIBMUSL_IMAGE=ocirep:libmusl +ARG LIBPCSCLITE_IMAGE=ocirep:libpcsclite-static + +FROM ocirep:rust as build + +COPY --from=${LIBSTD_IMAGE} . lib +COPY --from=${LIBMUSL_IMAGE} . lib +COPY --from=${LIBPCSCLITE_IMAGE} . lib +COPY . . + +ENV PCSC_LIB_DIR='./lib' +ENV PCSC_LIB_NAME='static=pcsclite' +ENV RUSTFLAGS='-L lib -C target-feature=+crt-static' +RUN cargo build --target x86_64-unknown-linux-musl --release + +FROM static +COPY --from=build target/release/pcsc_demo . diff --git a/examples/rust_pcsc_static/main.rs b/examples/rust_pcsc_static/main.rs new file mode 100644 index 0000000..7db9ead --- /dev/null +++ b/examples/rust_pcsc_static/main.rs @@ -0,0 +1,44 @@ +extern crate pcsc; + +use pcsc::*; +use std::str; + +fn main() { + // Establish a PC/SC context. + let ctx = Context::establish(Scope::User) + .expect("failed to establish context"); + + // List available readers. + let mut readers_buf = [0; 2048]; + let mut readers = ctx.list_readers(&mut readers_buf) + .expect("failed to list readers"); + + // Use the first reader. + let reader = readers.next().ok_or(()) + .expect("no readers are connected"); + println!("Using reader: {:?}", reader); + + // Connect to the card. + let card = ctx.connect(reader, ShareMode::Shared, Protocols::T1) + .expect("failed to connect to card"); + + // Send an SELECT APDU command. + let apdu = b"\x00\xA4\x04\x00\x0A\xA0\x00\x00\x00\x62\x03\x01\x0C\x06\x01"; + let mut rapdu_buf = [0; MAX_BUFFER_SIZE]; + let rapdu = card.transmit(apdu, &mut rapdu_buf) + .expect("failed to transmit APDU to card"); + println!("{:?}", rapdu); + + // Send an COMMAND APDU command. + let apdu = b"\x00\x00\x00\x00"; + let mut rapdu_buf = [0; MAX_BUFFER_SIZE]; + let rapdu = card.transmit(apdu, &mut rapdu_buf) + .expect("failed to transmit APDU to card"); + println!("{:?}", rapdu); + + // remove the extra 2 SW bytes at the end + let text = &rapdu[0 .. rapdu.len()-2]; + + // convert to UTF-8 (ASCII in fact) + println!("{}", str::from_utf8(&text).unwrap()); +}