Skip to content

Commit f05e2ff

Browse files
authored
Merge pull request #18 from siketyan/migrate-to-pcsc
feat(cli): Drop support of libnfc, use pcsc instead
2 parents 68059c7 + 200e8f7 commit f05e2ff

File tree

4 files changed

+105
-175
lines changed

4 files changed

+105
-175
lines changed

.github/workflows/rust.yml

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ jobs:
3232
override: true
3333
components: rustfmt,clippy
3434

35-
- name: Install libnfc
36-
run: sudo apt install libnfc5 libnfc-dev udev libudev-dev
35+
- name: Install libpcsclite
36+
run: sudo apt install libudev-dev libpcsclite-dev
3737

3838
- name: Run clippy
3939
uses: actions-rs/clippy-check@v1
@@ -56,21 +56,34 @@ jobs:
5656
strategy:
5757
matrix:
5858
target:
59+
- x86_64-pc-windows-msvc
60+
- x86_64-apple-darwin
5961
- x86_64-unknown-linux-gnu
6062
- aarch64-linux-android
6163
- armv7-linux-androideabi
6264
include:
65+
- target: x86_64-pc-windows-msvc
66+
host: windows-2022
67+
cli: true
68+
android: false
69+
- target: x86_64-apple-darwin
70+
host: macos-12
71+
cli: true
72+
android: false
6373
- target: x86_64-unknown-linux-gnu
74+
host: ubuntu-20.04
6475
cli: true
6576
android: false
6677
- target: aarch64-linux-android
78+
host: ubuntu-20.04
6779
cli: false
6880
android: true
6981
- target: armv7-linux-androideabi
82+
host: ubuntu-20.04
7083
cli: false
7184
android: true
7285

73-
runs-on: ubuntu-20.04
86+
runs-on: ${{ matrix.host }}
7487
steps:
7588
- uses: actions/checkout@v2
7689
with:
@@ -91,9 +104,9 @@ jobs:
91104
override: true
92105
target: ${{ matrix.target }}
93106

94-
- name: Install libnfc
95-
if: ${{ matrix.cli }}
96-
run: sudo apt install libnfc5 libnfc-dev udev libudev-dev pcscd libpcsclite-dev
107+
- name: Install libpcsclite
108+
if: ${{ matrix.cli && matrix.host == 'ubuntu-20.04' }}
109+
run: sudo apt install libudev-dev libpcsclite-dev
97110

98111
- name: Install Android NDK
99112
if: ${{ matrix.android }}

cli/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ edition = "2021"
1414
[dependencies]
1515
clap = { version = "3.1", features = ["derive"] }
1616
dialoguer = "0.10"
17+
hex = "0.4"
1718
jpki = { version = "0.1.5", path = ".." }
18-
nfc1-sys = { version = "0.2", features = ["conffiles", "envvars", "logging", "driver_pcsc"] }
19+
pcsc = "2.7"
1920
thiserror = "1.0"
21+
tracing = "0.1"
22+
tracing-subscriber = { version = "0.3", features = ["env-filter"] }

cli/src/main.rs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
mod nfc;
22

33
use std::fs::File;
4-
use std::io::{stdin, stdout, Read, Write};
4+
use std::io::{stderr, stdin, stdout, Read, Write};
55
use std::path::PathBuf;
66
use std::process::exit;
77

88
use clap::{ArgEnum, Parser, Subcommand};
99
use dialoguer::Password;
10+
use jpki::ap::jpki as jpki_ap;
1011
use jpki::nfc::apdu::{Command, Handler, Response};
12+
use tracing::{error, info};
13+
use tracing_subscriber::EnvFilter;
1114

1215
use crate::nfc::{Context, Initiator, Target};
1316

@@ -49,13 +52,15 @@ enum CertType {
4952
AuthCA,
5053
}
5154

52-
impl Into<jpki::ap::jpki::CertType> for CertType {
53-
fn into(self) -> jpki::ap::jpki::CertType {
55+
impl Into<jpki_ap::CertType> for CertType {
56+
fn into(self) -> jpki_ap::CertType {
57+
use jpki_ap::CertType::*;
58+
5459
match self {
55-
Self::Sign => jpki::ap::jpki::CertType::Sign,
56-
Self::SignCA => jpki::ap::jpki::CertType::SignCA,
57-
Self::Auth => jpki::ap::jpki::CertType::Auth,
58-
Self::AuthCA => jpki::ap::jpki::CertType::AuthCA,
60+
Self::Sign => Sign,
61+
Self::SignCA => SignCA,
62+
Self::Auth => Auth,
63+
Self::AuthCA => AuthCA,
5964
}
6065
}
6166
}
@@ -107,20 +112,20 @@ fn read_all<R: Read>(mut r: R) -> Result<Vec<u8>> {
107112
Ok(buffer)
108113
}
109114

110-
fn main() -> Result<()> {
115+
fn run() -> Result<()> {
111116
let cli: Cli = Cli::parse();
112117

113118
let ctx = Context::try_new().map_err(Error::NFC)?;
114119
let device = ctx.open().map_err(Error::NFC)?;
115-
let initiator = Initiator::try_from(device).map_err(Error::NFC)?;
116-
let target = initiator.select_dep_target().map_err(Error::NFC)?;
120+
let initiator = Initiator::from(device);
121+
let target = initiator.select_dep_target(ctx).map_err(Error::NFC)?;
117122

118123
let nfc_card = NfcCard { target };
119124
let card = jpki::Card::new(Box::new(nfc_card));
120125
let jpki_ap = jpki::ap::JpkiAp::open((), Box::new(card)).map_err(Error::APDU)?;
121126
match &cli.command {
122127
SubCommand::ReadCertificate { ty } => {
123-
let ty: jpki::ap::jpki::CertType = (*ty).into();
128+
let ty: jpki_ap::CertType = (*ty).into();
124129
let pin = if ty.is_pin_required() {
125130
prompt_password()?
126131
} else {
@@ -148,13 +153,24 @@ fn main() -> Result<()> {
148153
let certificate = read_all(File::open(certificate_path).map_err(Error::IO)?)?;
149154
let signature = read_all(File::open(signature_path).map_err(Error::IO)?)?;
150155
if jpki::digest::verify(certificate, read_all(stdin())?, signature) {
151-
println!("OK");
156+
info!("OK")
152157
} else {
153-
println!("NG");
158+
error!("NG");
154159
exit(1);
155160
}
156161
}
157162
}
158163

159164
Ok(())
160165
}
166+
167+
fn main() {
168+
tracing_subscriber::fmt::fmt()
169+
.with_env_filter(EnvFilter::from_default_env())
170+
.with_writer(stderr)
171+
.init();
172+
173+
if let Err(e) = run() {
174+
error!("{}", e);
175+
}
176+
}

0 commit comments

Comments
 (0)